1use std::{
9 cell::{Cell, RefCell},
10 collections::HashSet,
11 rc::Rc,
12 sync::Arc,
13};
14
15use gpui::{
16 Anchor, AnyElement, AnyView, App, AppContext as _, Context, Div, Empty,
17 InteractiveElement as _, IntoElement, ParentElement as _, Render, ScrollHandle, SharedString,
18 Stateful, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Window, div,
19 prelude::FluentBuilder as _, px,
20};
21use gpui_base::{
22 dock::{
23 AnyDrag, DockPlacement, DragPanel, DropIndicator, NodeId, PaneNode, PaneRef, PanelId,
24 TabGroupContext, TabGroupRenderer,
25 },
26 spring,
27};
28use rust_i18n::t;
29
30use crate::{
31 ActiveTheme as _, IconName, Selectable as _, Sizable as _,
32 button::{Button, ButtonCustomVariant, ButtonVariants as _},
33 dock::{ClosePanel, PanelControl, PanelHandle, PanelStyle, SkinShared, ToggleZoom},
34 h_flex,
35 menu::DropdownMenu as _,
36 tab::{Tab, TabBar},
37};
38
39const ZOOM_CONTROL_SELECTOR: &str = "dock-tab-bar-zoom-control";
42
43const CLOSE_BUTTON_SELECTOR: &str = "dock-tab-close-button";
45
46const DRAG_PREVIEW_SIZE: gpui::Size<gpui::Pixels> = gpui::size(px(96.), px(30.));
49
50pub struct DragPanelPreview {
55 panel: Arc<dyn gpui_base::dock::PanelView>,
56}
57
58impl Render for DragPanelPreview {
59 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
60 div()
61 .id("drag-panel")
62 .cursor_grab()
63 .py_1()
64 .px_3()
65 .w_24()
66 .overflow_hidden()
67 .whitespace_nowrap()
68 .border_1()
69 .border_color(cx.theme().border)
70 .rounded(cx.theme().radius)
71 .text_color(cx.theme().tab_foreground)
72 .bg(cx.theme().tokens.tab_active)
73 .opacity(0.75)
74 .child(panel_title(&self.panel, window, cx))
75 }
76}
77
78pub(crate) fn panel_title(
81 panel: &Arc<dyn gpui_base::dock::PanelView>,
82 window: &mut Window,
83 cx: &mut App,
84) -> AnyElement {
85 let Some(handle) = PanelHandle::of(panel) else {
86 let name = panel.panel_name(cx);
87 warn_unwrapped_once(panel.panel_id(cx), name);
88 return SharedString::from(name).into_any_element();
89 };
90 handle.title(window, cx)
91}
92
93thread_local! {
94 static WARNED_UNWRAPPED: RefCell<HashSet<PanelId>> = RefCell::new(HashSet::new());
105}
106
107fn warn_unwrapped_once(panel: PanelId, name: &'static str) {
114 if !WARNED_UNWRAPPED.with(|warned| warned.borrow_mut().insert(panel)) {
115 return;
116 }
117 tracing::warn!(
118 panel = name,
119 "dock panel reached the skin without its presentation handle, so it \
120 draws its panel name instead of its title; install it with \
121 `gpui_component::dock::panel_handle(..)` and `DockLayout::panel_view` \
122 / `DockArea::add_panel_view` rather than `DockLayout::panel` / \
123 `DockArea::add_panel`"
124 );
125}
126
127fn zoom_control(group: &TabGroupContext, cx: &App) -> Option<PanelControl> {
138 let panel = group.active_panel()?;
139 panel
140 .zoomable(cx)
141 .then(|| PanelHandle::of(panel).and_then(|handle| handle.zoom_control(cx)))
142 .flatten()
143}
144
145fn tab_drag(group: &TabGroupContext, ix: usize, cx: &App) -> Option<DragPanel> {
154 group
155 .is_draggable()
156 .then(|| group.drag_panel(ix, cx))
157 .flatten()
158}
159
160fn left_top_group(node: &PaneNode) -> Option<NodeId> {
163 match node.kind() {
164 PaneRef::Tabs { .. } => Some(node.id()),
165 PaneRef::Split { children, .. } => children.first().and_then(left_top_group),
166 }
167}
168
169fn right_top_group(node: &PaneNode) -> Option<NodeId> {
173 match node.kind() {
174 PaneRef::Tabs { .. } => Some(node.id()),
175 PaneRef::Split { axis, children, .. } => match axis {
176 gpui::Axis::Vertical => children.first(),
177 gpui::Axis::Horizontal => children.last(),
178 }
179 .and_then(right_top_group),
180 }
181}
182
183pub(crate) struct TabGroupSkin {
189 shared: Rc<SkinShared>,
190 scroll_handle: ScrollHandle,
191 last_active_ix: Cell<Option<usize>>,
195}
196
197impl TabGroupSkin {
198 pub(crate) fn new(shared: Rc<SkinShared>) -> Self {
199 Self {
200 shared,
201 scroll_handle: ScrollHandle::default(),
202 last_active_ix: Cell::new(None),
203 }
204 }
205
206 fn dock_toggle_button(
209 &self,
210 placement: DockPlacement,
211 group: &TabGroupContext,
212 cx: &mut App,
213 ) -> Option<Button> {
214 if group.is_zoomed() || !self.shared.is_toggle_button_visible() {
215 return None;
216 }
217
218 let area = self.shared.area().upgrade()?;
219 let area = area.read(cx);
220 if !area.is_dock_collapsible(placement) {
223 return None;
224 }
225
226 let designated = match placement {
227 DockPlacement::Left => area
228 .layout(DockPlacement::Center)
229 .and_then(|tree| left_top_group(tree.root())),
230 DockPlacement::Right => area
231 .layout(DockPlacement::Center)
232 .and_then(|tree| right_top_group(tree.root())),
233 DockPlacement::Bottom => area
234 .layout(DockPlacement::Bottom)
235 .and_then(|tree| left_top_group(tree.root())),
236 DockPlacement::Center => None,
237 };
238 if designated != Some(group.node()) {
239 return None;
240 }
241
242 let is_open = area.is_dock_open(placement);
243 let icon = match (placement, is_open) {
244 (DockPlacement::Left, true) => IconName::PanelLeft,
245 (DockPlacement::Left, false) => IconName::PanelLeftOpen,
246 (DockPlacement::Right, true) => IconName::PanelRight,
247 (DockPlacement::Right, false) => IconName::PanelRightOpen,
248 (DockPlacement::Bottom, true) => IconName::PanelBottom,
249 (DockPlacement::Bottom, false) => IconName::PanelBottomOpen,
250 (DockPlacement::Center, _) => return None,
251 };
252
253 let area = self.shared.area().clone();
254 Some(
255 Button::new(SharedString::from(format!("toggle-dock:{:?}", placement)))
256 .icon(icon)
257 .xsmall()
258 .ghost()
259 .tab_stop(false)
260 .tooltip(match is_open {
261 true => t!("Dock.Collapse"),
262 false => t!("Dock.Expand"),
263 })
264 .on_click(move |_, window, cx| {
265 _ = area.update(cx, |area, cx| area.toggle_dock(placement, window, cx));
266 }),
267 )
268 }
269
270 fn render_toolbar(
273 &self,
274 group: &TabGroupContext,
275 window: &mut Window,
276 cx: &mut App,
277 ) -> impl IntoElement {
278 if group.is_collapsed() {
279 return div();
280 }
281
282 let zoomed = group.is_zoomed();
283 let handle = group.active_panel().and_then(PanelHandle::of);
284 let control = zoom_control(group, cx);
285 let toolbar_zoom = control.is_some_and(|control| control.toolbar_visible());
286 let menu_zoom = control.is_some_and(|control| control.menu_visible());
287 let closable = group.is_closable();
288 let buttons = handle.and_then(|handle| handle.toolbar_buttons(window, cx));
289 let panel = handle.map(|handle| handle.panel());
290
291 h_flex()
292 .gap_1()
293 .occlude()
294 .when_some(buttons, |this, buttons| {
295 this.children(
296 buttons
297 .into_iter()
298 .map(|button| button.xsmall().ghost().tab_stop(false)),
299 )
300 })
301 .when_some(
302 match (zoomed, toolbar_zoom) {
303 (true, _) => Some(("zoom-out", IconName::Minimize, t!("Dock.Zoom Out"))),
304 (false, true) => Some(("zoom-in", IconName::Maximize, t!("Dock.Zoom In"))),
305 (false, false) => None,
306 },
307 |this, (id, icon, tooltip)| {
308 this.child(
309 Button::new(id)
310 .icon(icon)
311 .xsmall()
312 .ghost()
313 .tab_stop(false)
314 .tooltip_with_action(tooltip, &ToggleZoom, None)
315 .selected(zoomed)
316 .debug_selector(|| ZOOM_CONTROL_SELECTOR.to_string())
321 .on_click({
322 let group = group.clone();
323 move |_, window, cx| group.toggle_zoom(window, cx)
324 }),
325 )
326 },
327 )
328 .child(
329 Button::new("menu")
330 .icon(IconName::Ellipsis)
331 .xsmall()
332 .ghost()
333 .tab_stop(false)
334 .dropdown_menu(move |menu, window, cx| {
335 menu.when_some(panel.clone(), |menu, panel| {
336 panel.dropdown_menu(menu, window, cx)
337 })
338 .separator()
339 .menu_with_disabled(
340 match zoomed {
341 true => t!("Dock.Zoom Out"),
342 false => t!("Dock.Zoom In"),
343 },
344 Box::new(ToggleZoom),
345 !menu_zoom,
346 )
347 .when(closable, |menu| {
348 menu.separator()
349 .menu(t!("Dock.Close"), Box::new(ClosePanel))
350 })
351 })
352 .anchor(Anchor::TopRight),
353 )
354 }
355
356 fn render_title(
358 &self,
359 group: &TabGroupContext,
360 ix: usize,
361 window: &mut Window,
362 cx: &mut App,
363 ) -> AnyElement {
364 let panel = &group.panels()[ix];
365 let left_button = self.dock_toggle_button(DockPlacement::Left, group, cx);
366 let bottom_button = self.dock_toggle_button(DockPlacement::Bottom, group, cx);
367 let right_button = self.dock_toggle_button(DockPlacement::Right, group, cx);
368 let has_leading = left_button.is_some() || bottom_button.is_some();
369 let handle = PanelHandle::of(panel);
370 let title_style = handle.and_then(|handle| handle.title_style(cx));
371 let drag = tab_drag(group, ix, cx);
372
373 h_flex()
374 .justify_between()
375 .h(px(30.))
376 .py_2()
377 .pl_3()
378 .pr_2()
379 .when(left_button.is_some(), |this| this.pl_2())
380 .when(right_button.is_some(), |this| this.pr_2())
381 .when_some(title_style, |this, style| {
382 this.bg(style.background).text_color(style.foreground)
383 })
384 .when(has_leading, |this| {
385 this.child(
386 h_flex()
387 .flex_shrink_0()
388 .mr_1()
389 .gap_1()
390 .children(left_button)
391 .children(bottom_button),
392 )
393 })
394 .child(
395 div()
396 .id("tab")
397 .flex_1()
398 .min_w_16()
399 .overflow_hidden()
400 .text_ellipsis()
401 .whitespace_nowrap()
402 .child(panel_title(panel, window, cx))
403 .when_some(drag, |this, drag| {
404 this.on_drag(drag, {
405 let panel = panel.clone();
406 move |drag, offset, _, cx| {
407 cx.stop_propagation();
408 drag.set_drag_offset(offset);
409 drag.set_preview_size(DRAG_PREVIEW_SIZE);
410 cx.new(|_| DragPanelPreview {
411 panel: panel.clone(),
412 })
413 }
414 })
415 }),
416 )
417 .children(handle.and_then(|handle| handle.title_suffix(window, cx)))
418 .child(
419 h_flex()
420 .flex_shrink_0()
421 .ml_1()
422 .gap_1()
423 .child(self.render_toolbar(group, window, cx))
424 .children(right_button),
425 )
426 .into_any_element()
427 }
428
429 fn render_tabs(
431 &self,
432 group: &TabGroupContext,
433 window: &mut Window,
434 cx: &mut App,
435 ) -> AnyElement {
436 let left_button = self.dock_toggle_button(DockPlacement::Left, group, cx);
437 let bottom_button = self.dock_toggle_button(DockPlacement::Bottom, group, cx);
438 let right_button = self.dock_toggle_button(DockPlacement::Right, group, cx);
439 let has_leading = left_button.is_some() || bottom_button.is_some();
440 let is_bottom_dock = bottom_button.is_some();
441 let collapsed = group.is_collapsed();
442
443 let droppable = group.is_droppable();
444 let tabs_count = group.panels().len();
445 let active_ix = group.active_ix();
446 let displayed = group.active_panel().map(|panel| panel.panel_id(cx));
447 let visible: Vec<usize> = group
448 .panels()
449 .iter()
450 .enumerate()
451 .filter(|(_, panel)| panel.visible(cx))
452 .map(|(ix, _)| ix)
453 .collect();
454 let displayed_ix = displayed.and_then(|displayed| {
455 group
456 .panels()
457 .iter()
458 .position(|panel| panel.panel_id(cx) == displayed)
459 });
460
461 if self.last_active_ix.replace(Some(active_ix)) != Some(active_ix) {
464 if let Some(visible_ix) = visible.iter().position(|ix| *ix == active_ix) {
465 self.scroll_handle.scroll_to_item(visible_ix);
466 }
467 }
468
469 TabBar::new("tab-bar")
470 .track_scroll(&self.scroll_handle)
471 .when(has_leading, |this| {
472 this.prefix(
473 h_flex()
474 .items_center()
475 .top_0()
476 .right(-px(1.))
478 .border_r_1()
479 .border_b_1()
480 .h_full()
481 .border_color(cx.theme().border)
482 .bg(cx.theme().tokens.tab_bar)
483 .px_2()
484 .children(left_button)
485 .children(bottom_button),
486 )
487 })
488 .children(
489 visible
490 .into_iter()
491 .map(|ix| {
492 let panel = &group.panels()[ix];
493 let handle = PanelHandle::of(panel);
494 let drag = tab_drag(group, ix, cx);
495
496 Tab::new()
497 .ix(ix)
498 .tab_bar_prefix(has_leading)
499 .map(|this| match handle.and_then(|handle| handle.tab_name(cx)) {
500 Some(tab_name) => this.child(tab_name),
501 None => this.child(panel_title(panel, window, cx)),
502 })
503 .when(
509 !collapsed
510 && self.shared.close_button_visible.get()
511 && group.is_panel_closable(panel.panel_id(cx), cx),
512 |this| {
513 this.suffix(
514 Button::new(("close-tab", ix))
515 .icon(IconName::Close)
516 .xsmall()
517 .custom(
523 ButtonCustomVariant::new(cx)
524 .foreground(cx.theme().secondary_foreground)
525 .hover(*cx.theme().tokens.secondary_hover)
526 .active(*cx.theme().tokens.secondary_active),
527 )
528 .ml(-px(8.))
535 .mr_2()
536 .tab_stop(false)
537 .debug_selector(|| CLOSE_BUTTON_SELECTOR.to_string())
538 .on_click({
539 let group = group.clone();
540 let panel_id = panel.panel_id(cx);
541 move |_, window, cx| {
542 cx.stop_propagation();
543 group.close(panel_id, window, cx);
544 }
545 }),
546 )
547 },
548 )
549 .selected(!collapsed && Some(ix) == displayed_ix)
555 .on_click({
556 let group = group.clone();
557 let area = self.shared.area().clone();
558 move |_, window, cx| {
559 group.select_tab(ix, window, cx);
560
561 if is_bottom_dock && collapsed {
564 _ = area.update(cx, |area, cx| {
565 area.toggle_dock(DockPlacement::Bottom, window, cx)
566 });
567 }
568 }
569 })
570 .when(!collapsed, |this| {
573 this.when_some(drag, |this, drag| {
574 this.on_drag(drag, {
575 let panel = panel.clone();
576 move |drag, offset, _, cx| {
577 cx.stop_propagation();
578 drag.set_drag_offset(offset);
579 drag.set_preview_size(DRAG_PREVIEW_SIZE);
580 cx.new(|_| DragPanelPreview {
581 panel: panel.clone(),
582 })
583 }
584 })
585 })
586 .when(droppable, |this| {
587 this.drag_over::<DragPanel>(|this, _, _, cx| {
588 this.rounded_l_none()
589 .border_l_2()
590 .border_r_0()
591 .border_color(cx.theme().drag_border)
592 })
593 .on_drop({
594 let group = group.clone();
595 move |drag: &DragPanel, window, cx| {
596 group.drop_panel(
597 drag.clone(),
598 Some(ix),
599 true,
600 window,
601 cx,
602 );
603 }
604 })
605 .drag_over::<AnyDrag>(|this, _, _, cx| {
606 this.rounded_l_none()
607 .border_l_2()
608 .border_r_0()
609 .border_color(cx.theme().drag_border)
610 })
611 .on_drop({
612 let group = group.clone();
613 move |item: &AnyDrag, window, cx| {
614 group.drop_item(item.clone(), None, window, cx);
615 }
616 })
617 })
618 })
619 })
620 .collect::<Vec<_>>(),
621 )
622 .last_empty_space(
623 div()
625 .id("tab-bar-empty-space")
626 .h_full()
627 .flex_grow_1()
628 .min_w_16()
629 .when(droppable, |this| {
630 this.drag_over::<DragPanel>(|this, _, _, cx| {
631 this.bg(cx.theme().tokens.drop_target)
632 })
633 .on_drop({
634 let group = group.clone();
635 let node = group.node();
636 move |drag: &DragPanel, window, cx| {
637 let ix = (drag.source() == node).then(|| tabs_count - 1);
641 group.drop_panel(drag.clone(), ix, false, window, cx);
642 }
643 })
644 .drag_over::<AnyDrag>(|this, _, _, cx| {
645 this.bg(cx.theme().tokens.drop_target)
646 })
647 .on_drop({
648 let group = group.clone();
649 move |item: &AnyDrag, window, cx| {
650 group.drop_item(item.clone(), None, window, cx);
651 }
652 })
653 }),
654 )
655 .when(!collapsed, |this| {
656 this.suffix(
657 h_flex()
658 .items_center()
659 .top_0()
660 .right_0()
661 .border_l_1()
662 .border_b_1()
663 .h_full()
664 .border_color(cx.theme().border)
665 .bg(cx.theme().tokens.tab_bar)
666 .px_2()
667 .gap_1()
668 .children(
669 group
670 .active_panel()
671 .and_then(PanelHandle::of)
672 .and_then(|handle| handle.title_suffix(window, cx)),
673 )
674 .child(self.render_toolbar(group, window, cx))
675 .children(right_button),
676 )
677 })
678 .into_any_element()
679 }
680}
681
682impl TabGroupRenderer for TabGroupSkin {
683 fn frame(&self, group: &TabGroupContext, _: &mut Window, cx: &mut App) -> Stateful<Div> {
684 let control = zoom_control(group, cx);
685
686 div()
689 .id("tab-panel")
690 .bg(cx.theme().tokens.background)
691 .when(!group.is_collapsed(), |this| {
694 this.on_action({
695 let group = group.clone();
696 move |_: &ToggleZoom, window, cx| {
697 if !group.is_zoomed() && control.is_none() {
703 return;
704 }
705 group.toggle_zoom(window, cx);
706 }
707 })
708 .on_action({
709 let group = group.clone();
710 move |_: &ClosePanel, window, cx| {
711 let Some(panel) = group.active_panel() else {
712 return;
713 };
714 let panel = panel.panel_id(cx);
715 group.close(panel, window, cx);
716 }
717 })
718 })
719 }
720
721 fn content_frame(
722 &self,
723 group: &TabGroupContext,
724 _: &mut Window,
725 cx: &mut App,
726 ) -> Stateful<Div> {
727 let padded = group.panels().len() > 1
728 && group
729 .active_panel()
730 .and_then(PanelHandle::of)
731 .is_none_or(|handle| handle.inner_padding(cx));
732
733 div().id("active-panel").when(padded, |this| this.pt_2())
736 }
737
738 fn render_tab_bar(
739 &self,
740 group: &TabGroupContext,
741 window: &mut Window,
742 cx: &mut App,
743 ) -> AnyElement {
744 let visible: Vec<usize> = group
745 .panels()
746 .iter()
747 .enumerate()
748 .filter(|(_, panel)| panel.visible(cx))
749 .map(|(ix, _)| ix)
750 .collect();
751
752 match visible.as_slice() {
753 [] => Empty.into_any_element(),
754 [ix] if self.shared.panel_style() == PanelStyle::Auto => {
755 let panel = &group.panels()[*ix];
757 if PanelHandle::of(panel).is_some_and(|handle| !handle.title_bar(cx)) {
758 return Empty.into_any_element();
759 }
760 self.render_title(group, *ix, window, cx)
761 }
762 _ => self.render_tabs(group, window, cx),
763 }
764 }
765
766 fn render_active_panel(
767 &self,
768 panel: AnyView,
769 group: &TabGroupContext,
770 _: &mut Window,
771 _: &mut App,
772 ) -> AnyElement {
773 if group.is_collapsed() {
774 return Empty.into_any_element();
775 }
776
777 div()
778 .id("tab-content")
779 .overflow_y_scroll()
780 .overflow_x_hidden()
781 .flex_1()
782 .child(panel.cached(StyleRefinement::default().absolute().size_full()))
783 .into_any_element()
784 }
785
786 fn render_drop_indicator(
787 &self,
788 indicator: DropIndicator,
789 window: &mut Window,
790 cx: &mut App,
791 ) -> Option<AnyElement> {
792 let to = indicator.to();
793 let id = "drop-placeholder";
799 let placeholder_spring = cx.theme().motion_tokens().spring_move.with_epsilon(0.5);
800 let left = spring((id, "left"), to.origin().x, placeholder_spring, window, cx);
801 let top = spring((id, "top"), to.origin().y, placeholder_spring, window, cx);
802 let width = spring(
803 (id, "width"),
804 to.size().width,
805 placeholder_spring,
806 window,
807 cx,
808 );
809 let height = spring(
810 (id, "height"),
811 to.size().height,
812 placeholder_spring,
813 window,
814 cx,
815 );
816
817 Some(
818 div()
819 .absolute()
820 .bg(cx.theme().tokens.drop_target)
821 .left(left)
822 .top(top)
823 .w(width)
824 .h(height)
825 .into_any_element(),
826 )
827 }
828}
829
830#[cfg(test)]
831mod tests {
832 use std::{
833 cell::{Cell, RefCell},
834 sync::Arc,
835 };
836
837 use gpui::{
838 Entity, EventEmitter, FocusHandle, Focusable, Modifiers, MouseButton, Pixels,
839 TestAppContext, VisualTestContext,
840 };
841 use gpui_base::dock::{DockArea, DockAreaRenderer, DockLayout, DockPlacement, PanelEvent};
842
843 use super::*;
844 use crate::{
845 ElementExt as _,
846 dock::{
847 DockSkin, Panel, panel_handle,
848 test_support::{HideableProbe, MeasuredProbe},
849 },
850 };
851
852 struct Probe {
853 focus_handle: FocusHandle,
854 }
855
856 impl Probe {
857 fn new(cx: &mut App) -> Entity<Self> {
858 cx.new(|cx| Self {
859 focus_handle: cx.focus_handle(),
860 })
861 }
862 }
863
864 impl gpui_base::dock::Panel for Probe {
865 fn panel_name(&self) -> &'static str {
866 "Probe"
867 }
868 }
869
870 impl Panel for Probe {}
871 impl EventEmitter<PanelEvent> for Probe {}
872
873 impl Focusable for Probe {
874 fn focus_handle(&self, _: &App) -> FocusHandle {
875 self.focus_handle.clone()
876 }
877 }
878
879 impl Render for Probe {
880 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
881 Empty
882 }
883 }
884
885 #[derive(Default)]
888 struct Recorded {
889 draggable: Vec<bool>,
891 }
892
893 struct Recorder {
894 log: Rc<RefCell<Recorded>>,
895 }
896
897 impl TabGroupRenderer for Recorder {
898 fn render_tab_bar(
899 &self,
900 group: &TabGroupContext,
901 _: &mut Window,
902 cx: &mut App,
903 ) -> AnyElement {
904 let mut log = self.log.borrow_mut();
905 for ix in 0..group.panels().len() {
906 log.draggable.push(tab_drag(group, ix, cx).is_some());
907 }
908 Empty.into_any_element()
909 }
910 }
911
912 impl DockAreaRenderer for Recorder {
913 fn frame(&self, _: &mut Window, _: &mut App) -> Stateful<Div> {
914 div().id("recorder").size_full()
915 }
916
917 fn tab_group_renderer(&self) -> Rc<dyn TabGroupRenderer> {
918 Rc::new(Recorder {
919 log: self.log.clone(),
920 })
921 }
922 }
923
924 fn recording_area(
925 cx: &mut TestAppContext,
926 ) -> (
927 Entity<DockArea>,
928 Rc<RefCell<Recorded>>,
929 &mut VisualTestContext,
930 ) {
931 cx.update(|cx| {
932 crate::init(cx);
933 });
934 let log = Rc::new(RefCell::new(Recorded::default()));
935 let renderer = Rc::new(Recorder { log: log.clone() });
936 let (area, cx) = cx.add_window_view(|window, cx| {
937 DockArea::new("skin", None, window, cx).with_renderer(renderer)
938 });
939 (area, log, cx)
940 }
941
942 #[gpui::test]
946 fn the_last_group_in_a_dock_offers_no_drag(cx: &mut TestAppContext) {
947 let (area, log, cx) = recording_area(cx);
948
949 cx.update(|window, cx| {
950 let layout = DockLayout::tabs().panel_view(panel_handle(Probe::new(cx)), cx);
951 area.update(cx, |area, cx| area.set_center(layout, window, cx));
952 });
953 cx.run_until_parked();
954 log.borrow_mut().draggable.clear();
955 cx.update(|window, cx| {
956 window.refresh();
958 window.draw(cx).clear(cx);
959 });
960
961 assert_eq!(
962 log.borrow().draggable,
963 vec![false],
964 "the only visible panel in the dock has nowhere to go, so its tab \
965 must not start a drag"
966 );
967 }
968
969 #[gpui::test]
970 fn a_group_beside_another_offers_a_drag(cx: &mut TestAppContext) {
971 let (area, log, cx) = recording_area(cx);
972
973 cx.update(|window, cx| {
974 let layout = DockLayout::h_split()
975 .child(
976 DockLayout::tabs().panel_view(panel_handle(Probe::new(cx)), cx),
977 None,
978 )
979 .child(
980 DockLayout::tabs().panel_view(panel_handle(Probe::new(cx)), cx),
981 None,
982 );
983 area.update(cx, |area, cx| area.set_center(layout, window, cx));
984 });
985 cx.run_until_parked();
986 log.borrow_mut().draggable.clear();
987 cx.update(|window, cx| {
988 window.refresh();
990 window.draw(cx).clear(cx);
991 });
992
993 assert_eq!(
994 log.borrow().draggable,
995 vec![true, true],
996 "each group has somewhere to go, so both tabs start a drag"
997 );
998 }
999
1000 struct ToolbarZoomProbe {
1003 focus_handle: FocusHandle,
1004 }
1005
1006 impl ToolbarZoomProbe {
1007 fn new(cx: &mut App) -> Entity<Self> {
1008 cx.new(|cx| Self {
1009 focus_handle: cx.focus_handle(),
1010 })
1011 }
1012 }
1013
1014 impl gpui_base::dock::Panel for ToolbarZoomProbe {
1015 fn panel_name(&self) -> &'static str {
1016 "ToolbarZoomProbe"
1017 }
1018 }
1019
1020 impl Panel for ToolbarZoomProbe {
1021 fn zoom_control(&self, _: &App) -> Option<PanelControl> {
1022 Some(PanelControl::Toolbar)
1023 }
1024 }
1025
1026 impl EventEmitter<PanelEvent> for ToolbarZoomProbe {}
1027
1028 impl Focusable for ToolbarZoomProbe {
1029 fn focus_handle(&self, _: &App) -> FocusHandle {
1030 self.focus_handle.clone()
1031 }
1032 }
1033
1034 impl Render for ToolbarZoomProbe {
1035 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
1036 Empty
1037 }
1038 }
1039
1040 struct NoControlProbe {
1043 focus_handle: FocusHandle,
1044 }
1045
1046 impl NoControlProbe {
1047 fn new(cx: &mut App) -> Entity<Self> {
1048 cx.new(|cx| Self {
1049 focus_handle: cx.focus_handle(),
1050 })
1051 }
1052 }
1053
1054 impl gpui_base::dock::Panel for NoControlProbe {
1055 fn panel_name(&self) -> &'static str {
1056 "NoControlProbe"
1057 }
1058 }
1059
1060 impl Panel for NoControlProbe {
1061 fn zoom_control(&self, _: &App) -> Option<PanelControl> {
1062 None
1063 }
1064 }
1065
1066 impl EventEmitter<PanelEvent> for NoControlProbe {}
1067
1068 impl Focusable for NoControlProbe {
1069 fn focus_handle(&self, _: &App) -> FocusHandle {
1070 self.focus_handle.clone()
1071 }
1072 }
1073
1074 impl Render for NoControlProbe {
1075 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
1076 Empty
1077 }
1078 }
1079
1080 struct UnzoomableProbe {
1083 focus_handle: FocusHandle,
1084 }
1085
1086 impl gpui_base::dock::Panel for UnzoomableProbe {
1087 fn panel_name(&self) -> &'static str {
1088 "UnzoomableProbe"
1089 }
1090
1091 fn zoomable(&self, _: &App) -> bool {
1092 false
1093 }
1094 }
1095
1096 impl Panel for UnzoomableProbe {
1097 fn zoom_control(&self, _: &App) -> Option<PanelControl> {
1098 Some(PanelControl::Toolbar)
1099 }
1100 }
1101
1102 impl EventEmitter<PanelEvent> for UnzoomableProbe {}
1103
1104 impl Focusable for UnzoomableProbe {
1105 fn focus_handle(&self, _: &App) -> FocusHandle {
1106 self.focus_handle.clone()
1107 }
1108 }
1109
1110 impl Render for UnzoomableProbe {
1111 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
1112 Empty
1113 }
1114 }
1115
1116 fn drew_zoom_control(
1123 cx: &mut TestAppContext,
1124 panel: impl FnOnce(&mut App) -> Arc<dyn gpui_base::dock::PanelView>,
1125 ) -> bool {
1126 cx.update(|cx| {
1127 crate::init(cx);
1128 });
1129 let (area, cx) = cx.add_window_view(|window, cx| {
1130 let skin = DockSkin::new(cx);
1131 DockArea::new("skin", None, window, cx).with_renderer(skin)
1132 });
1133
1134 cx.update(|window, cx| {
1135 let layout = DockLayout::tabs().panel_view(panel(cx), cx);
1136 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1137 });
1138 cx.run_until_parked();
1139 cx.update(|window, cx| window.draw(cx).clear(cx));
1140 cx.debug_bounds(ZOOM_CONTROL_SELECTOR).is_some()
1141 }
1142
1143 #[gpui::test]
1147 fn a_panel_base_will_not_zoom_gets_no_zoom_control(cx: &mut TestAppContext) {
1148 let drew = drew_zoom_control(cx, |cx| {
1149 panel_handle(cx.new(|cx| UnzoomableProbe {
1150 focus_handle: cx.focus_handle(),
1151 }))
1152 });
1153
1154 assert!(
1155 !drew,
1156 "the panel names a place for the control, but base refuses the zoom"
1157 );
1158 }
1159
1160 #[gpui::test]
1164 fn a_zoomable_panel_gets_its_zoom_control(cx: &mut TestAppContext) {
1165 let drew = drew_zoom_control(cx, |cx| panel_handle(ToolbarZoomProbe::new(cx)));
1166
1167 assert!(
1168 drew,
1169 "a zoomable panel asking for a toolbar control gets one"
1170 );
1171 }
1172
1173 #[gpui::test]
1183 fn the_centre_and_the_bottom_dock_share_the_column(cx: &mut TestAppContext) {
1184 cx.update(|cx| {
1185 crate::init(cx);
1186 });
1187 let centre = Rc::new(Cell::new(px(0.)));
1188 let bottom = Rc::new(Cell::new(px(0.)));
1189 let (area, cx) = cx.add_window_view(|window, cx| {
1190 let skin = DockSkin::new(cx);
1191 DockArea::new("skin", None, window, cx).with_renderer(skin)
1192 });
1193
1194 let (centre_probe, bottom_probe) = (centre.clone(), bottom.clone());
1195 cx.update(|window, cx| {
1196 let centre_panel = MeasuredProbe::new(centre_probe, cx);
1197 let bottom_panel = MeasuredProbe::new(bottom_probe, cx);
1198 area.update(cx, |area, cx| {
1199 area.set_center(
1203 DockLayout::v_split().child(
1204 DockLayout::h_split().child(
1205 DockLayout::tabs().panel_view(panel_handle(centre_panel), cx),
1206 None,
1207 ),
1208 None,
1209 ),
1210 window,
1211 cx,
1212 );
1213 area.set_dock(
1214 DockPlacement::Bottom,
1215 DockLayout::tabs().panel_view(panel_handle(bottom_panel), cx),
1216 window,
1217 cx,
1218 );
1219 area.set_dock_size(DockPlacement::Bottom, px(200.), window, cx);
1220 });
1221 });
1222 cx.run_until_parked();
1223 cx.update(|window, cx| window.draw(cx).clear(cx));
1224
1225 let window_height = cx.update(|window, _| window.viewport_size().height);
1226 assert!(
1227 centre.get() > px(0.),
1228 "the centre panel must receive height; it got {:?}",
1229 centre.get()
1230 );
1231 assert!(
1232 bottom.get() > px(0.),
1233 "the bottom dock's panel must receive height; it got {:?}",
1234 bottom.get()
1235 );
1236 assert!(
1237 centre.get() < window_height - px(150.),
1238 "the centre must give the 200px bottom dock its share; the centre \
1239 got {:?} of {window_height:?}",
1240 centre.get()
1241 );
1242 }
1243
1244 #[gpui::test]
1259 fn a_split_fills_its_container_whichever_slots_are_hidden(cx: &mut TestAppContext) {
1260 cx.update(|cx| {
1261 crate::init(cx);
1262 });
1263 let heights: Vec<Rc<Cell<Pixels>>> = (0..3).map(|_| Rc::new(Cell::new(px(0.)))).collect();
1264 let (area, cx) = cx.add_window_view(|window, cx| {
1265 let skin = DockSkin::new(cx);
1266 DockArea::new("skin", None, window, cx).with_renderer(skin)
1267 });
1268
1269 let slots = heights.clone();
1270 let probes = cx.update(|window, cx| {
1271 let probes: Vec<_> = slots
1272 .iter()
1273 .map(|height| HideableProbe::new(height.clone(), cx))
1274 .collect();
1275 area.update(cx, |area, cx| {
1276 area.set_dock(
1277 DockPlacement::Right,
1278 probes.iter().zip([260., 320., 200.]).fold(
1279 DockLayout::v_split(),
1280 |split, (probe, size)| {
1281 split.child(
1282 DockLayout::tabs().panel_view(panel_handle(probe.clone()), cx),
1283 Some(px(size)),
1284 )
1285 },
1286 ),
1287 window,
1288 cx,
1289 );
1290 area.set_dock_size(DockPlacement::Right, px(380.), window, cx);
1291 });
1292 probes
1293 });
1294 cx.run_until_parked();
1295 let draw = |cx: &mut VisualTestContext| {
1296 cx.update(|window, _| window.refresh());
1297 cx.update(|window, cx| window.draw(cx).clear(cx));
1298 cx.update(|window, cx| window.draw(cx).clear(cx));
1299 };
1300 draw(cx);
1301
1302 let dock_height = cx.update(|window, _| window.viewport_size().height);
1303 let drawn: Pixels = heights.iter().map(|height| height.get()).sum();
1307 let bar = (dock_height - drawn) / 3.;
1308 assert!(
1309 bar > px(0.) && bar < px(60.),
1310 "the three slots fill the dock to begin with, one tab bar each; \
1311 that leaves {bar:?} per slot of {dock_height:?}"
1312 );
1313
1314 for hidden in 1..0b111u8 {
1317 let shown = (0..3).filter(|slot| hidden & (1 << slot) == 0);
1318 cx.update(|_, cx| {
1319 for (slot, probe) in probes.iter().enumerate() {
1320 heights[slot].set(px(-1.));
1323 probe.update(cx, |probe, cx| {
1324 probe.set_visible(hidden & (1 << slot) == 0, cx)
1325 });
1326 }
1327 });
1328 cx.run_until_parked();
1329 draw(cx);
1330
1331 let mut count = 0;
1332 let mut total = px(0.);
1333 for slot in shown {
1334 assert_ne!(
1335 heights[slot].get(),
1336 px(-1.),
1337 "hiding {hidden:03b}: slot {slot} is shown and must draw"
1338 );
1339 count += 1;
1340 total += heights[slot].get();
1341 }
1342 let empty = dock_height - total - bar * count as f32;
1343 assert!(
1344 empty.abs() < px(1.),
1345 "hiding {hidden:03b}: the drawn slots must take the hidden \
1346 ones' space between them; they left {empty:?} of \
1347 {dock_height:?} empty"
1348 );
1349 }
1350 }
1351
1352 #[gpui::test]
1356 fn the_zoom_action_reaches_the_group_through_the_skin(cx: &mut TestAppContext) {
1357 cx.update(|cx| {
1358 crate::init(cx);
1359 });
1360 let (area, cx) = cx.add_window_view(|window, cx| {
1361 let skin = DockSkin::new(cx);
1362 DockArea::new("skin", None, window, cx).with_renderer(skin)
1363 });
1364
1365 let panel = cx.update(|window, cx| {
1366 let panel = Probe::new(cx);
1367 let layout = DockLayout::tabs().panel_view(panel_handle(panel.clone()), cx);
1368 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1369 panel
1370 });
1371 cx.run_until_parked();
1372 cx.update(|window, cx| {
1373 panel.read(cx).focus_handle(cx).focus(window, cx);
1374 });
1375 cx.run_until_parked();
1376
1377 assert_eq!(cx.read(|cx| area.read(cx).is_zoomed()), false);
1378 cx.dispatch_action(ToggleZoom);
1379 cx.run_until_parked();
1380 assert_eq!(
1381 cx.read(|cx| area.read(cx).is_zoomed()),
1382 true,
1383 "the skin's frame is what carries the ToggleZoom handler"
1384 );
1385
1386 cx.dispatch_action(ToggleZoom);
1387 cx.run_until_parked();
1388 assert_eq!(cx.read(|cx| area.read(cx).is_zoomed()), false);
1389 }
1390
1391 #[gpui::test]
1397 fn the_zoom_action_refuses_a_panel_that_offers_no_control(cx: &mut TestAppContext) {
1398 cx.update(|cx| {
1399 crate::init(cx);
1400 });
1401 let (area, cx) = cx.add_window_view(|window, cx| {
1402 let skin = DockSkin::new(cx);
1403 DockArea::new("skin", None, window, cx).with_renderer(skin)
1404 });
1405
1406 let panel = cx.update(|window, cx| {
1407 let panel = NoControlProbe::new(cx);
1408 let layout = DockLayout::tabs().panel_view(panel_handle(panel.clone()), cx);
1409 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1410 panel
1411 });
1412 cx.run_until_parked();
1413 cx.update(|window, cx| {
1414 panel.read(cx).focus_handle(cx).focus(window, cx);
1415 });
1416 cx.run_until_parked();
1417
1418 cx.dispatch_action(ToggleZoom);
1419 cx.run_until_parked();
1420 assert_eq!(
1421 cx.read(|cx| area.read(cx).is_zoomed()),
1422 false,
1423 "no control means no zoom, however the zoom was asked for"
1424 );
1425 }
1426
1427 #[gpui::test]
1442 fn the_panel_content_region_gets_the_height_below_the_tab_bar(cx: &mut TestAppContext) {
1443 cx.update(|cx| {
1444 crate::init(cx);
1445 });
1446 let height = Rc::new(Cell::new(px(0.)));
1447 let (area, cx) = cx.add_window_view(|window, cx| {
1448 let skin = DockSkin::new(cx);
1449 DockArea::new("skin", None, window, cx).with_renderer(skin)
1450 });
1451
1452 let measured = height.clone();
1453 cx.update(|window, cx| {
1454 let panel = MeasuredProbe::new(measured, cx);
1455 let layout = DockLayout::tabs().panel_view(panel_handle(panel), cx);
1456 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1457 });
1458 cx.run_until_parked();
1459 cx.update(|window, cx| window.draw(cx).clear(cx));
1460
1461 let window_height = cx.update(|window, _| window.viewport_size().height);
1462 let content = height.get();
1463 assert!(
1464 content > px(0.),
1465 "the panel must receive height; it got {content:?} in a {window_height:?} window"
1466 );
1467 assert!(
1470 content > window_height - px(60.),
1471 "the panel should fill what the tab bar leaves; it got {content:?} \
1472 of {window_height:?}"
1473 );
1474 }
1475
1476 #[gpui::test]
1483 fn a_collapsed_dock_ignores_the_zoom_action(cx: &mut TestAppContext) {
1484 cx.update(|cx| {
1485 crate::init(cx);
1486 });
1487 let (area, cx) = cx.add_window_view(|window, cx| {
1488 let skin = DockSkin::new(cx);
1489 DockArea::new("skin", None, window, cx).with_renderer(skin)
1490 });
1491
1492 let panel = cx.update(|window, cx| {
1493 let panel = Probe::new(cx);
1494 let layout = DockLayout::tabs().panel_view(panel_handle(panel.clone()), cx);
1495 area.update(cx, |area, cx| {
1496 area.set_dock(DockPlacement::Bottom, layout, window, cx);
1497 area.toggle_dock(DockPlacement::Bottom, window, cx);
1498 });
1499 panel
1500 });
1501 cx.run_until_parked();
1502 cx.update(|window, cx| {
1503 panel.read(cx).focus_handle(cx).focus(window, cx);
1504 });
1505 cx.run_until_parked();
1506
1507 cx.dispatch_action(ToggleZoom);
1508 cx.run_until_parked();
1509 assert_eq!(
1510 cx.read(|cx| area.read(cx).is_zoomed()),
1511 false,
1512 "a collapsed group installs no action handler"
1513 );
1514 }
1515
1516 #[gpui::test]
1519 fn a_panel_without_a_title_bar_gets_the_whole_group(cx: &mut TestAppContext) {
1520 struct Chromeless {
1521 focus_handle: FocusHandle,
1522 height: Rc<Cell<Pixels>>,
1523 }
1524
1525 impl gpui_base::dock::Panel for Chromeless {
1526 fn panel_name(&self) -> &'static str {
1527 "Chromeless"
1528 }
1529 }
1530
1531 impl Panel for Chromeless {
1532 fn title_bar(&self, _: &App) -> bool {
1533 false
1534 }
1535 }
1536
1537 impl EventEmitter<PanelEvent> for Chromeless {}
1538
1539 impl Focusable for Chromeless {
1540 fn focus_handle(&self, _: &App) -> FocusHandle {
1541 self.focus_handle.clone()
1542 }
1543 }
1544
1545 impl Render for Chromeless {
1546 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
1547 let height = self.height.clone();
1548 div()
1549 .size_full()
1550 .on_prepaint(move |bounds, _, _| height.set(bounds.size.height))
1551 }
1552 }
1553
1554 cx.update(|cx| crate::init(cx));
1555
1556 let measure = |cx: &mut TestAppContext, chromeless: bool| -> Pixels {
1557 let height = Rc::new(Cell::new(px(0.)));
1558 let (area, cx) = cx.add_window_view(|window, cx| {
1559 let skin = DockSkin::new(cx);
1560 DockArea::new("skin", None, window, cx).with_renderer(skin)
1561 });
1562 let measured = height.clone();
1563 cx.update(|window, cx| {
1564 let panel: Arc<dyn gpui_base::dock::PanelView> = if chromeless {
1565 panel_handle(cx.new(|cx| Chromeless {
1566 focus_handle: cx.focus_handle(),
1567 height: measured,
1568 }))
1569 } else {
1570 panel_handle(MeasuredProbe::new(measured, cx))
1571 };
1572 let layout = DockLayout::tabs().panel_view(panel, cx);
1573 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1574 });
1575 cx.run_until_parked();
1576 cx.update(|window, cx| window.draw(cx).clear(cx));
1577 height.get()
1578 };
1579
1580 let with_title = measure(cx, false);
1581 let without = measure(cx, true);
1582 assert!(with_title > px(0.), "the probe must have been drawn");
1583 assert_eq!(
1584 without,
1585 with_title + px(30.),
1586 "the 30px the title bar took go to the panel instead"
1587 );
1588 }
1589
1590 struct TabProbe {
1592 focus_handle: FocusHandle,
1593 closable: bool,
1594 activated: Rc<Cell<bool>>,
1595 }
1596
1597 impl TabProbe {
1598 fn new(closable: bool, cx: &mut App) -> Entity<Self> {
1599 cx.new(|cx| Self {
1600 focus_handle: cx.focus_handle(),
1601 closable,
1602 activated: Rc::new(Cell::new(false)),
1603 })
1604 }
1605 }
1606
1607 impl gpui_base::dock::Panel for TabProbe {
1608 fn panel_name(&self) -> &'static str {
1609 "TabProbe"
1610 }
1611
1612 fn closable(&self, _: &App) -> bool {
1613 self.closable
1614 }
1615
1616 fn set_active(&mut self, active: bool, _: &mut Window, _: &mut Context<Self>) {
1617 if active {
1618 self.activated.set(true);
1619 }
1620 }
1621 }
1622
1623 impl Panel for TabProbe {}
1624
1625 impl EventEmitter<PanelEvent> for TabProbe {}
1626
1627 impl Focusable for TabProbe {
1628 fn focus_handle(&self, _: &App) -> FocusHandle {
1629 self.focus_handle.clone()
1630 }
1631 }
1632
1633 impl Render for TabProbe {
1634 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
1635 Empty
1636 }
1637 }
1638
1639 fn drew_close_button(cx: &mut TestAppContext, closable: bool, enabled: bool) -> bool {
1644 cx.update(|cx| crate::init(cx));
1645 let mut skin = None;
1646 let (area, cx) = cx.add_window_view(|window, cx| {
1647 let renderer = DockSkin::new(cx);
1648 skin = Some(renderer.clone());
1649 DockArea::new("skin", None, window, cx).with_renderer(renderer)
1650 });
1651 let skin = skin.expect("skin constructed with the area");
1652 if enabled {
1653 cx.update(|_, cx| skin.set_close_button_visible(true, cx));
1654 }
1655 cx.update(|window, cx| {
1656 let under_test = TabProbe::new(closable, cx);
1657 let filler = TabProbe::new(false, cx);
1658 let layout = DockLayout::tabs()
1659 .panel_view(panel_handle(under_test), cx)
1660 .panel_view(panel_handle(filler), cx);
1661 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1662 });
1663 cx.run_until_parked();
1664 cx.update(|window, cx| window.draw(cx).clear(cx));
1665 let button = cx.debug_bounds(CLOSE_BUTTON_SELECTOR);
1666 if enabled && closable {
1667 assert_eq!(
1668 button.as_ref().map(|bounds| bounds.size.width),
1669 Some(px(20.)),
1670 "the close button keeps the standard XS hover target"
1671 );
1672 }
1673 button.is_some()
1674 }
1675
1676 #[gpui::test]
1678 fn a_closable_panel_gets_a_close_button(cx: &mut TestAppContext) {
1679 assert!(
1680 drew_close_button(cx, true, true),
1681 "a closable panel's tab must draw a close button"
1682 );
1683 }
1684
1685 #[gpui::test]
1687 fn a_non_closable_panel_gets_no_close_button(cx: &mut TestAppContext) {
1688 assert!(
1689 !drew_close_button(cx, false, true),
1690 "a panel that reports itself non-closable must draw no close button"
1691 );
1692 }
1693
1694 #[gpui::test]
1695 fn close_buttons_are_off_by_default(cx: &mut TestAppContext) {
1696 assert!(!drew_close_button(cx, true, false));
1697 }
1698
1699 #[gpui::test]
1700 fn close_button_visibility_updates_after_the_skin_setting_changes(cx: &mut TestAppContext) {
1701 cx.update(|cx| crate::init(cx));
1702 let mut skin = None;
1703 let (area, cx) = cx.add_window_view(|window, cx| {
1704 let renderer = DockSkin::new(cx);
1705 skin = Some(renderer.clone());
1706 DockArea::new("skin", None, window, cx).with_renderer(renderer)
1707 });
1708 let skin = skin.expect("skin constructed with the area");
1709 cx.update(|window, cx| {
1710 let panel = TabProbe::new(true, cx);
1711 let filler = TabProbe::new(false, cx);
1712 area.update(cx, |area, cx| {
1713 area.set_center(
1714 DockLayout::tabs()
1715 .panel_view(panel_handle(panel), cx)
1716 .panel_view(panel_handle(filler), cx),
1717 window,
1718 cx,
1719 )
1720 });
1721 });
1722
1723 for (visible, expected) in [(false, false), (true, true), (false, false)] {
1724 cx.update(|_, cx| skin.set_close_button_visible(visible, cx));
1725 cx.run_until_parked();
1726 cx.update(|window, cx| window.draw(cx).clear(cx));
1727 assert_eq!(cx.debug_bounds(CLOSE_BUTTON_SELECTOR).is_some(), expected);
1728 }
1729 }
1730
1731 #[gpui::test]
1735 fn clicking_a_close_button_removes_only_that_tab(cx: &mut TestAppContext) {
1736 cx.update(|cx| crate::init(cx));
1737 let mut skin = None;
1738 let (area, cx) = cx.add_window_view(|window, cx| {
1739 let renderer = DockSkin::new(cx);
1740 skin = Some(renderer.clone());
1741 DockArea::new("skin", None, window, cx).with_renderer(renderer)
1742 });
1743 cx.update(|_, cx| skin.unwrap().set_close_button_visible(true, cx));
1744
1745 let (closable_id, filler_id, activated) = cx.update(|window, cx| {
1746 let filler = TabProbe::new(false, cx);
1747 let closable = TabProbe::new(true, cx);
1748 let activated = closable.read(cx).activated.clone();
1749 let filler_handle = panel_handle(filler);
1750 let closable_handle = panel_handle(closable);
1751 let ids = (closable_handle.panel_id(cx), filler_handle.panel_id(cx));
1752 let layout = DockLayout::tabs()
1755 .panel_view(filler_handle, cx)
1756 .panel_view(closable_handle, cx);
1757 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1758 (ids.0, ids.1, activated)
1759 });
1760 cx.run_until_parked();
1761 cx.update(|window, cx| window.draw(cx).clear(cx));
1762
1763 assert!(
1764 cx.update(|_, cx| area.read(cx).panel(closable_id).is_some()),
1765 "the closable tab starts owned by the area"
1766 );
1767
1768 let button = cx
1769 .debug_bounds(CLOSE_BUTTON_SELECTOR)
1770 .expect("the non-active closable tab draws a close button");
1771 cx.simulate_mouse_down(button.center(), MouseButton::Left, Modifiers::none());
1772 cx.simulate_mouse_up(button.center(), MouseButton::Left, Modifiers::none());
1773 cx.run_until_parked();
1774
1775 assert!(
1776 cx.update(|_, cx| area.read(cx).panel(closable_id).is_none()),
1777 "the close click removes exactly the tab it belongs to"
1778 );
1779 assert!(
1780 cx.update(|_, cx| area.read(cx).panel(filler_id).is_some()),
1781 "the displayed filler tab is untouched"
1782 );
1783 assert!(
1784 !activated.get(),
1785 "stop_propagation keeps the close click from also selecting the tab"
1786 );
1787 }
1788
1789 #[gpui::test]
1793 fn a_collapsed_group_draws_no_close_button(cx: &mut TestAppContext) {
1794 cx.update(|cx| crate::init(cx));
1795 let mut skin = None;
1796 let (area, cx) = cx.add_window_view(|window, cx| {
1797 let renderer = DockSkin::new(cx);
1798 skin = Some(renderer.clone());
1799 DockArea::new("skin", None, window, cx).with_renderer(renderer)
1800 });
1801 cx.update(|_, cx| skin.unwrap().set_close_button_visible(true, cx));
1802 cx.update(|window, cx| {
1805 let a = TabProbe::new(true, cx);
1806 let b = TabProbe::new(true, cx);
1807 let layout = DockLayout::tabs()
1808 .panel_view(panel_handle(a), cx)
1809 .panel_view(panel_handle(b), cx);
1810 area.update(cx, |area, cx| {
1811 area.set_dock(DockPlacement::Bottom, layout, window, cx);
1812 if !area.is_dock_open(DockPlacement::Bottom) {
1813 area.toggle_dock(DockPlacement::Bottom, window, cx);
1814 }
1815 });
1816 });
1817 cx.run_until_parked();
1818 cx.update(|window, cx| window.draw(cx).clear(cx));
1819 assert!(
1820 cx.debug_bounds(CLOSE_BUTTON_SELECTOR).is_some(),
1821 "an open group with two closable panels offers a close button"
1822 );
1823
1824 cx.update(|window, cx| {
1827 area.update(cx, |area, cx| {
1828 area.toggle_dock(DockPlacement::Bottom, window, cx)
1829 });
1830 });
1831 cx.run_until_parked();
1832 cx.update(|window, cx| window.draw(cx).clear(cx));
1833 assert!(
1834 cx.debug_bounds(CLOSE_BUTTON_SELECTOR).is_none(),
1835 "a collapsed group must not offer a close button"
1836 );
1837 }
1838}