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, rems,
20};
21use gpui_base::{
22 LogicalStyledExt as _,
23 dock::{
24 AnyDrag, DockPlacement, DragPanel, DropIndicator, NodeId, PaneNode, PaneRef, PanelId,
25 TabGroupContext, TabGroupRenderer,
26 },
27 spring,
28};
29use rust_i18n::t;
30
31use crate::{
32 ActiveTheme as _, IconName, Selectable as _, Sizable as _,
33 button::{Button, ButtonCustomVariant, ButtonVariants as _},
34 dock::{
35 ClosePanel, PanelControl, PanelHandle, PanelStyle, SkinShared, ToggleZoom,
36 toggle_dock_by_user,
37 },
38 h_flex,
39 menu::DropdownMenu as _,
40 tab::{Tab, TabBar},
41};
42
43const ZOOM_CONTROL_SELECTOR: &str = "dock-tab-bar-zoom-control";
46
47const CLOSE_BUTTON_SELECTOR: &str = "dock-tab-close-button";
49
50const TAB_SELECTOR: &str = "dock-tab";
52
53const DOCK_TOGGLE_SELECTOR: &str = "dock-toggle";
56
57const DRAG_PREVIEW_SIZE: gpui::Size<gpui::Pixels> = gpui::size(px(96.), px(30.));
60
61pub struct DragPanelPreview {
66 panel: Arc<dyn gpui_base::dock::PanelView>,
67}
68
69impl Render for DragPanelPreview {
70 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
71 div()
72 .id("drag-panel")
73 .cursor_grab()
74 .py_1()
75 .px_3()
76 .w_24()
77 .overflow_hidden()
78 .whitespace_nowrap()
79 .border_1()
80 .border_color(cx.theme().border)
81 .rounded(cx.theme().radius)
82 .text_color(cx.theme().tab_foreground)
83 .bg(cx.theme().tokens.tab_active)
84 .opacity(0.75)
85 .child(panel_title(&self.panel, window, cx))
86 }
87}
88
89pub(crate) fn panel_title(
92 panel: &Arc<dyn gpui_base::dock::PanelView>,
93 window: &mut Window,
94 cx: &mut App,
95) -> AnyElement {
96 let Some(handle) = PanelHandle::of(panel) else {
97 let name = panel.panel_name(cx);
98 warn_unwrapped_once(panel.panel_id(cx), name);
99 return SharedString::from(name).into_any_element();
100 };
101 handle.title(window, cx)
102}
103
104thread_local! {
105 static WARNED_UNWRAPPED: RefCell<HashSet<PanelId>> = RefCell::new(HashSet::new());
116}
117
118fn warn_unwrapped_once(panel: PanelId, name: &'static str) {
125 if !WARNED_UNWRAPPED.with(|warned| warned.borrow_mut().insert(panel)) {
126 return;
127 }
128 tracing::warn!(
129 panel = name,
130 "dock panel reached the skin without its presentation handle, so it \
131 draws its panel name instead of its title; install it with \
132 `gpui_component::dock::panel_handle(..)` and `DockLayout::panel_view` \
133 / `DockArea::add_panel_view` rather than `DockLayout::panel` / \
134 `DockArea::add_panel`"
135 );
136}
137
138fn zoom_control(group: &TabGroupContext, cx: &App) -> Option<PanelControl> {
149 let panel = group.active_panel()?;
150 panel
151 .zoomable(cx)
152 .then(|| PanelHandle::of(panel).and_then(|handle| handle.zoom_control(cx)))
153 .flatten()
154}
155
156fn tab_drag(group: &TabGroupContext, ix: usize, cx: &App) -> Option<DragPanel> {
165 group
166 .is_draggable()
167 .then(|| group.drag_panel(ix, cx))
168 .flatten()
169}
170
171fn left_top_group(node: &PaneNode) -> Option<NodeId> {
174 match node.kind() {
175 PaneRef::Tabs { .. } => Some(node.id()),
176 PaneRef::Split { children, .. } => children.first().and_then(left_top_group),
177 }
178}
179
180fn right_top_group(node: &PaneNode) -> Option<NodeId> {
184 match node.kind() {
185 PaneRef::Tabs { .. } => Some(node.id()),
186 PaneRef::Split { axis, children, .. } => match axis {
187 gpui::Axis::Vertical => children.first(),
188 gpui::Axis::Horizontal => children.last(),
189 }
190 .and_then(right_top_group),
191 }
192}
193
194pub(crate) struct TabGroupSkin {
200 shared: Rc<SkinShared>,
201 scroll_handle: ScrollHandle,
202 last_active_ix: Rc<Cell<Option<usize>>>,
206}
207
208impl TabGroupSkin {
209 pub(crate) fn new(shared: Rc<SkinShared>) -> Self {
210 Self {
211 shared,
212 scroll_handle: ScrollHandle::default(),
213 last_active_ix: Rc::new(Cell::new(None)),
214 }
215 }
216
217 fn dock_toggle_button(
220 &self,
221 placement: DockPlacement,
222 group: &TabGroupContext,
223 cx: &mut App,
224 ) -> Option<Button> {
225 if group.is_zoomed() || !self.shared.is_toggle_button_visible() {
226 return None;
227 }
228
229 let area = self.shared.area().upgrade()?;
230 let area = area.read(cx);
231 if !area.is_dock_collapsible(placement) {
234 return None;
235 }
236
237 let designated = match placement {
238 DockPlacement::Left => area
239 .layout(DockPlacement::Center)
240 .and_then(|tree| left_top_group(tree.root())),
241 DockPlacement::Right => area
242 .layout(DockPlacement::Center)
243 .and_then(|tree| right_top_group(tree.root())),
244 DockPlacement::Bottom => area
245 .layout(DockPlacement::Bottom)
246 .and_then(|tree| left_top_group(tree.root())),
247 DockPlacement::Center => None,
248 };
249 if designated != Some(group.node()) {
250 return None;
251 }
252
253 let is_open = area.is_dock_open(placement);
254 let icon = match (
256 placement.physical(gpui_base::LayoutDirection::global(cx)),
257 is_open,
258 ) {
259 (DockPlacement::Left, true) => IconName::PanelLeft,
260 (DockPlacement::Left, false) => IconName::PanelLeftOpen,
261 (DockPlacement::Right, true) => IconName::PanelRight,
262 (DockPlacement::Right, false) => IconName::PanelRightOpen,
263 (DockPlacement::Bottom, true) => IconName::PanelBottom,
264 (DockPlacement::Bottom, false) => IconName::PanelBottomOpen,
265 (DockPlacement::Center, _) => return None,
266 };
267
268 let area = self.shared.area().clone();
269 Some(
270 Button::new(SharedString::from(format!("toggle-dock:{:?}", placement)))
271 .icon(icon)
272 .xsmall()
273 .ghost()
274 .tab_stop(false)
275 .tooltip(match is_open {
276 true => t!("Dock.Collapse"),
277 false => t!("Dock.Expand"),
278 })
279 .debug_selector(move || format!("{DOCK_TOGGLE_SELECTOR}-{placement:?}"))
280 .on_click(move |_, window, cx| {
281 _ = area.update(cx, |area, cx| {
282 toggle_dock_by_user(area, placement, window, cx)
283 });
284 }),
285 )
286 }
287
288 fn render_toolbar(
291 &self,
292 group: &TabGroupContext,
293 window: &mut Window,
294 cx: &mut App,
295 ) -> impl IntoElement {
296 if group.is_collapsed() {
297 return div();
298 }
299
300 let zoomed = group.is_zoomed();
301 let handle = group.active_panel().and_then(PanelHandle::of);
302 let control = zoom_control(group, cx);
303 let toolbar_zoom = control.is_some_and(|control| control.toolbar_visible());
304 let menu_zoom = control.is_some_and(|control| control.menu_visible());
305 let closable = group.is_closable();
306 let buttons = handle.and_then(|handle| handle.toolbar_buttons(window, cx));
307 let panel = handle.map(|handle| handle.panel());
308
309 h_flex()
310 .gap_1()
311 .occlude()
312 .when_some(buttons, |this, buttons| {
313 this.children(
314 buttons
315 .into_iter()
316 .map(|button| button.xsmall().ghost().tab_stop(false)),
317 )
318 })
319 .when_some(
320 match (zoomed, toolbar_zoom) {
321 (true, _) => Some(("zoom-out", IconName::Minimize, t!("Dock.Zoom Out"))),
322 (false, true) => Some(("zoom-in", IconName::Maximize, t!("Dock.Zoom In"))),
323 (false, false) => None,
324 },
325 |this, (id, icon, tooltip)| {
326 this.child(
327 Button::new(id)
328 .icon(icon)
329 .xsmall()
330 .ghost()
331 .tab_stop(false)
332 .tooltip_with_action(tooltip, &ToggleZoom, None)
333 .selected(zoomed)
334 .debug_selector(|| ZOOM_CONTROL_SELECTOR.to_string())
339 .on_click({
340 let group = group.clone();
341 move |_, window, cx| group.toggle_zoom(window, cx)
342 }),
343 )
344 },
345 )
346 .child(
347 Button::new("menu")
348 .icon(IconName::Ellipsis)
349 .xsmall()
350 .ghost()
351 .tab_stop(false)
352 .dropdown_menu(move |menu, window, cx| {
353 menu.when_some(panel.clone(), |menu, panel| {
354 panel.dropdown_menu(menu, window, cx)
355 })
356 .separator()
357 .menu_with_disabled(
358 match zoomed {
359 true => t!("Dock.Zoom Out"),
360 false => t!("Dock.Zoom In"),
361 },
362 Box::new(ToggleZoom),
363 !menu_zoom,
364 )
365 .when(closable, |menu| {
366 menu.separator()
367 .menu(t!("Dock.Close"), Box::new(ClosePanel))
368 })
369 })
370 .anchor(Anchor::TopRight),
371 )
372 }
373
374 fn render_title(
376 &self,
377 group: &TabGroupContext,
378 ix: usize,
379 window: &mut Window,
380 cx: &mut App,
381 ) -> AnyElement {
382 let panel = &group.panels()[ix];
383 let left_button = self.dock_toggle_button(DockPlacement::Left, group, cx);
384 let bottom_button = self.dock_toggle_button(DockPlacement::Bottom, group, cx);
385 let right_button = self.dock_toggle_button(DockPlacement::Right, group, cx);
386 let has_leading = left_button.is_some() || bottom_button.is_some();
387 let handle = PanelHandle::of(panel);
388 let title_style = handle.and_then(|handle| handle.title_style(cx));
389 let drag = tab_drag(group, ix, cx);
390
391 h_flex()
392 .flex_row_dir(cx)
393 .justify_between()
394 .h(px(30.))
395 .py_2()
396 .ps(rems(0.75), cx)
397 .pe(rems(0.5), cx)
398 .when(left_button.is_some(), |this| this.ps(rems(0.5), cx))
399 .when(right_button.is_some(), |this| this.pe(rems(0.5), cx))
400 .when_some(title_style, |this, style| {
401 this.bg(style.background).text_color(style.foreground)
402 })
403 .when(has_leading, |this| {
404 this.child(
405 h_flex()
406 .flex_shrink_0()
407 .me(rems(0.25), cx)
408 .gap_1()
409 .children(left_button)
410 .children(bottom_button),
411 )
412 })
413 .child(
414 div()
415 .id("tab")
416 .flex_1()
417 .min_w_16()
418 .overflow_hidden()
419 .text_ellipsis()
420 .whitespace_nowrap()
421 .child(panel_title(panel, window, cx))
422 .when_some(drag, |this, drag| {
423 this.on_drag(drag, {
424 let panel = panel.clone();
425 move |drag, offset, _, cx| {
426 cx.stop_propagation();
427 drag.set_drag_offset(offset);
428 drag.set_preview_size(DRAG_PREVIEW_SIZE);
429 cx.new(|_| DragPanelPreview {
430 panel: panel.clone(),
431 })
432 }
433 })
434 }),
435 )
436 .children(handle.and_then(|handle| handle.title_suffix(window, cx)))
437 .child(
438 h_flex()
439 .flex_shrink_0()
440 .ms(rems(0.25), cx)
441 .gap_1()
442 .child(self.render_toolbar(group, window, cx))
443 .children(right_button),
444 )
445 .into_any_element()
446 }
447
448 fn render_tabs(
450 &self,
451 group: &TabGroupContext,
452 window: &mut Window,
453 cx: &mut App,
454 ) -> AnyElement {
455 let left_button = self.dock_toggle_button(DockPlacement::Left, group, cx);
456 let bottom_button = self.dock_toggle_button(DockPlacement::Bottom, group, cx);
457 let right_button = self.dock_toggle_button(DockPlacement::Right, group, cx);
458 let has_leading = left_button.is_some() || bottom_button.is_some();
459 let is_bottom_dock = bottom_button.is_some();
460 let collapsed = group.is_collapsed();
461
462 let droppable = group.is_droppable();
463 let tabs_count = group.panels().len();
464 let active_ix = group.active_ix();
465 let displayed = group.active_panel().map(|panel| panel.panel_id(cx));
466 let visible: Vec<usize> = group
467 .panels()
468 .iter()
469 .enumerate()
470 .filter(|(_, panel)| panel.visible(cx))
471 .map(|(ix, _)| ix)
472 .collect();
473 let displayed_ix = displayed.and_then(|displayed| {
474 group
475 .panels()
476 .iter()
477 .position(|panel| panel.panel_id(cx) == displayed)
478 });
479
480 if self.last_active_ix.replace(Some(active_ix)) != Some(active_ix) {
483 if let Some(visible_ix) = visible.iter().position(|ix| *ix == active_ix) {
484 self.scroll_handle.scroll_to_item(visible_ix);
485 }
486 }
487
488 TabBar::new("tab-bar")
489 .track_scroll(&self.scroll_handle)
490 .when(has_leading, |this| {
491 this.prefix(
492 h_flex()
493 .items_center()
494 .top_0()
495 .inset_e(-px(1.), cx)
497 .border_e(px(1.), cx)
498 .border_b_1()
499 .h_full()
500 .border_color(cx.theme().border)
501 .bg(cx.theme().tokens.tab_bar)
502 .px_2()
503 .children(left_button)
504 .children(bottom_button),
505 )
506 })
507 .children(
508 visible
509 .into_iter()
510 .map(|ix| {
511 let panel = &group.panels()[ix];
512 let handle = PanelHandle::of(panel);
513 let drag = tab_drag(group, ix, cx);
514
515 Tab::new()
516 .ix(ix)
517 .debug_selector(move || format!("{TAB_SELECTOR}-{ix}"))
518 .tab_bar_prefix(has_leading)
519 .map(|this| match handle.and_then(|handle| handle.tab_name(cx)) {
520 Some(tab_name) => this.child(tab_name),
521 None => this.child(panel_title(panel, window, cx)),
522 })
523 .when(
529 !collapsed
530 && self.shared.close_button_visible.get()
531 && group.is_panel_closable(panel.panel_id(cx), cx),
532 |this| {
533 this.suffix(
534 Button::new(("close-tab", ix))
535 .icon(IconName::Close)
536 .xsmall()
537 .custom(
543 ButtonCustomVariant::new(cx)
544 .foreground(cx.theme().secondary_foreground)
545 .hover(*cx.theme().tokens.secondary_hover)
546 .active(*cx.theme().tokens.secondary_active),
547 )
548 .ms(-px(8.), cx)
555 .me(rems(0.5), cx)
556 .tab_stop(false)
557 .debug_selector(|| CLOSE_BUTTON_SELECTOR.to_string())
558 .on_click({
559 let group = group.clone();
560 let panel_id = panel.panel_id(cx);
561 move |_, window, cx| {
562 cx.stop_propagation();
563 group.close(panel_id, window, cx);
564 }
565 }),
566 )
567 },
568 )
569 .selected(!collapsed && Some(ix) == displayed_ix)
575 .on_click({
576 let group = group.clone();
577 let area = self.shared.area().clone();
578 let last_active_ix = self.last_active_ix.clone();
579 move |_, window, cx| {
580 last_active_ix.set(Some(ix));
583 group.select_tab(ix, window, cx);
584
585 if is_bottom_dock && collapsed {
588 _ = area.update(cx, |area, cx| {
589 toggle_dock_by_user(
590 area,
591 DockPlacement::Bottom,
592 window,
593 cx,
594 )
595 });
596 }
597 }
598 })
599 .when(!collapsed, |this| {
602 this.when_some(drag, |this, drag| {
603 this.on_drag(drag, {
604 let panel = panel.clone();
605 move |drag, offset, _, cx| {
606 cx.stop_propagation();
607 drag.set_drag_offset(offset);
608 drag.set_preview_size(DRAG_PREVIEW_SIZE);
609 cx.new(|_| DragPanelPreview {
610 panel: panel.clone(),
611 })
612 }
613 })
614 })
615 .when(droppable, |this| {
616 this.drag_over::<DragPanel>(|this, _, _, cx| {
617 this.rounded_s(px(0.), cx)
618 .border_s(px(2.), cx)
619 .border_e(px(0.), cx)
620 .border_color(cx.theme().drag_border)
621 })
622 .on_drop({
623 let group = group.clone();
624 move |drag: &DragPanel, window, cx| {
625 group.drop_panel(
626 drag.clone(),
627 Some(ix),
628 true,
629 window,
630 cx,
631 );
632 }
633 })
634 .drag_over::<AnyDrag>(|this, _, _, cx| {
635 this.rounded_s(px(0.), cx)
636 .border_s(px(2.), cx)
637 .border_e(px(0.), cx)
638 .border_color(cx.theme().drag_border)
639 })
640 .on_drop({
641 let group = group.clone();
642 move |item: &AnyDrag, window, cx| {
643 group.drop_item(item.clone(), None, window, cx);
644 }
645 })
646 })
647 })
648 })
649 .collect::<Vec<_>>(),
650 )
651 .last_empty_space(
652 div()
654 .id("tab-bar-empty-space")
655 .h_full()
656 .flex_grow_1()
657 .min_w_16()
658 .when(droppable, |this| {
659 this.drag_over::<DragPanel>(|this, _, _, cx| {
660 this.bg(cx.theme().tokens.drop_target)
661 })
662 .on_drop({
663 let group = group.clone();
664 let node = group.node();
665 move |drag: &DragPanel, window, cx| {
666 let ix = (drag.source() == node).then(|| tabs_count - 1);
670 group.drop_panel(drag.clone(), ix, false, window, cx);
671 }
672 })
673 .drag_over::<AnyDrag>(|this, _, _, cx| {
674 this.bg(cx.theme().tokens.drop_target)
675 })
676 .on_drop({
677 let group = group.clone();
678 move |item: &AnyDrag, window, cx| {
679 group.drop_item(item.clone(), None, window, cx);
680 }
681 })
682 }),
683 )
684 .when(!collapsed, |this| {
685 this.suffix(
686 h_flex()
687 .items_center()
688 .top_0()
689 .inset_e(px(0.), cx)
690 .border_s(px(1.), cx)
691 .border_b_1()
692 .h_full()
693 .border_color(cx.theme().border)
694 .bg(cx.theme().tokens.tab_bar)
695 .px_2()
696 .gap_1()
697 .children(
698 group
699 .active_panel()
700 .and_then(PanelHandle::of)
701 .and_then(|handle| handle.title_suffix(window, cx)),
702 )
703 .child(self.render_toolbar(group, window, cx))
704 .children(right_button),
705 )
706 })
707 .into_any_element()
708 }
709}
710
711impl TabGroupRenderer for TabGroupSkin {
712 fn frame(&self, group: &TabGroupContext, _: &mut Window, cx: &mut App) -> Stateful<Div> {
713 let control = zoom_control(group, cx);
714
715 div()
718 .id("tab-panel")
719 .bg(cx.theme().tokens.background)
720 .when(!group.is_collapsed(), |this| {
723 this.on_action({
724 let group = group.clone();
725 move |_: &ToggleZoom, window, cx| {
726 if !group.is_zoomed() && control.is_none() {
732 return;
733 }
734 group.toggle_zoom(window, cx);
735 }
736 })
737 .on_action({
738 let group = group.clone();
739 move |_: &ClosePanel, window, cx| {
740 let Some(panel) = group.active_panel() else {
741 return;
742 };
743 let panel = panel.panel_id(cx);
744 group.close(panel, window, cx);
745 }
746 })
747 })
748 }
749
750 fn content_frame(
751 &self,
752 group: &TabGroupContext,
753 _: &mut Window,
754 cx: &mut App,
755 ) -> Stateful<Div> {
756 let padded = group.panels().len() > 1
757 && group
758 .active_panel()
759 .and_then(PanelHandle::of)
760 .is_none_or(|handle| handle.inner_padding(cx));
761
762 div().id("active-panel").when(padded, |this| this.pt_2())
765 }
766
767 fn render_tab_bar(
768 &self,
769 group: &TabGroupContext,
770 window: &mut Window,
771 cx: &mut App,
772 ) -> AnyElement {
773 let visible: Vec<usize> = group
774 .panels()
775 .iter()
776 .enumerate()
777 .filter(|(_, panel)| panel.visible(cx))
778 .map(|(ix, _)| ix)
779 .collect();
780
781 match visible.as_slice() {
782 [] => Empty.into_any_element(),
783 [ix] if self.shared.panel_style() == PanelStyle::Auto => {
784 let panel = &group.panels()[*ix];
786 if PanelHandle::of(panel).is_some_and(|handle| !handle.title_bar(cx)) {
787 return Empty.into_any_element();
788 }
789 self.render_title(group, *ix, window, cx)
790 }
791 _ => self.render_tabs(group, window, cx),
792 }
793 }
794
795 fn render_active_panel(
796 &self,
797 panel: AnyView,
798 group: &TabGroupContext,
799 _: &mut Window,
800 _: &mut App,
801 ) -> AnyElement {
802 if group.is_collapsed() {
803 return Empty.into_any_element();
804 }
805
806 div()
807 .id("tab-content")
808 .overflow_y_scroll()
809 .overflow_x_hidden()
810 .flex_1()
811 .child(panel.cached(StyleRefinement::default().absolute().size_full()))
812 .into_any_element()
813 }
814
815 fn render_drop_indicator(
816 &self,
817 indicator: DropIndicator,
818 window: &mut Window,
819 cx: &mut App,
820 ) -> Option<AnyElement> {
821 let to = indicator.to();
822 let id = "drop-placeholder";
828 let placeholder_spring = cx.theme().motion_tokens().spring_move.with_epsilon(0.5);
829 let left = spring((id, "left"), to.origin().x, placeholder_spring, window, cx);
830 let top = spring((id, "top"), to.origin().y, placeholder_spring, window, cx);
831 let width = spring(
832 (id, "width"),
833 to.size().width,
834 placeholder_spring,
835 window,
836 cx,
837 );
838 let height = spring(
839 (id, "height"),
840 to.size().height,
841 placeholder_spring,
842 window,
843 cx,
844 );
845
846 Some(
847 div()
848 .absolute()
849 .bg(cx.theme().tokens.drop_target)
850 .left(left)
851 .top(top)
852 .w(width)
853 .h(height)
854 .into_any_element(),
855 )
856 }
857}
858
859#[cfg(test)]
860mod tests {
861 use std::{
862 cell::{Cell, RefCell},
863 sync::Arc,
864 };
865
866 use gpui::{
867 Entity, EventEmitter, FocusHandle, Focusable, Modifiers, MouseButton, Pixels,
868 TestAppContext, VisualTestContext,
869 };
870 use gpui_base::dock::{DockArea, DockAreaRenderer, DockLayout, DockPlacement, PanelEvent};
871
872 use super::*;
873 use crate::{
874 ElementExt as _,
875 dock::{
876 DockSkin, Panel, panel_handle,
877 test_support::{HideableProbe, MeasuredProbe},
878 },
879 };
880
881 struct Probe {
882 focus_handle: FocusHandle,
883 }
884
885 impl Probe {
886 fn new(cx: &mut App) -> Entity<Self> {
887 cx.new(|cx| Self {
888 focus_handle: cx.focus_handle(),
889 })
890 }
891 }
892
893 impl gpui_base::dock::Panel for Probe {
894 fn panel_name(&self) -> &'static str {
895 "Probe"
896 }
897 }
898
899 impl Panel for Probe {}
900 impl EventEmitter<PanelEvent> for Probe {}
901
902 impl Focusable for Probe {
903 fn focus_handle(&self, _: &App) -> FocusHandle {
904 self.focus_handle.clone()
905 }
906 }
907
908 impl Render for Probe {
909 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
910 Empty
911 }
912 }
913
914 #[derive(Default)]
917 struct Recorded {
918 draggable: Vec<bool>,
920 }
921
922 struct Recorder {
923 log: Rc<RefCell<Recorded>>,
924 }
925
926 impl TabGroupRenderer for Recorder {
927 fn render_tab_bar(
928 &self,
929 group: &TabGroupContext,
930 _: &mut Window,
931 cx: &mut App,
932 ) -> AnyElement {
933 let mut log = self.log.borrow_mut();
934 for ix in 0..group.panels().len() {
935 log.draggable.push(tab_drag(group, ix, cx).is_some());
936 }
937 Empty.into_any_element()
938 }
939 }
940
941 impl DockAreaRenderer for Recorder {
942 fn frame(&self, _: &mut Window, _: &mut App) -> Stateful<Div> {
943 div().id("recorder").size_full()
944 }
945
946 fn tab_group_renderer(&self) -> Rc<dyn TabGroupRenderer> {
947 Rc::new(Recorder {
948 log: self.log.clone(),
949 })
950 }
951 }
952
953 fn recording_area(
954 cx: &mut TestAppContext,
955 ) -> (
956 Entity<DockArea>,
957 Rc<RefCell<Recorded>>,
958 &mut VisualTestContext,
959 ) {
960 cx.update(|cx| {
961 crate::init(cx);
962 });
963 let log = Rc::new(RefCell::new(Recorded::default()));
964 let renderer = Rc::new(Recorder { log: log.clone() });
965 let (area, cx) = cx.add_window_view(|window, cx| {
966 DockArea::new("skin", None, window, cx).with_renderer(renderer)
967 });
968 (area, log, cx)
969 }
970
971 #[gpui::test]
975 fn the_last_group_in_a_dock_offers_no_drag(cx: &mut TestAppContext) {
976 let (area, log, cx) = recording_area(cx);
977
978 cx.update(|window, cx| {
979 let layout = DockLayout::tabs().panel_view(panel_handle(Probe::new(cx)), cx);
980 area.update(cx, |area, cx| area.set_center(layout, window, cx));
981 });
982 cx.run_until_parked();
983 log.borrow_mut().draggable.clear();
984 cx.update(|window, cx| crate::test_redraw(window, cx));
985
986 assert_eq!(
987 log.borrow().draggable,
988 vec![false],
989 "the only visible panel in the dock has nowhere to go, so its tab \
990 must not start a drag"
991 );
992 }
993
994 #[gpui::test]
995 fn a_group_beside_another_offers_a_drag(cx: &mut TestAppContext) {
996 let (area, log, cx) = recording_area(cx);
997
998 cx.update(|window, cx| {
999 let layout = DockLayout::h_split()
1000 .child(
1001 DockLayout::tabs().panel_view(panel_handle(Probe::new(cx)), cx),
1002 None,
1003 )
1004 .child(
1005 DockLayout::tabs().panel_view(panel_handle(Probe::new(cx)), cx),
1006 None,
1007 );
1008 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1009 });
1010 cx.run_until_parked();
1011 log.borrow_mut().draggable.clear();
1012 cx.update(|window, cx| crate::test_redraw(window, cx));
1013
1014 assert_eq!(
1015 log.borrow().draggable,
1016 vec![true, true],
1017 "each group has somewhere to go, so both tabs start a drag"
1018 );
1019 }
1020
1021 struct ToolbarZoomProbe {
1024 focus_handle: FocusHandle,
1025 }
1026
1027 impl ToolbarZoomProbe {
1028 fn new(cx: &mut App) -> Entity<Self> {
1029 cx.new(|cx| Self {
1030 focus_handle: cx.focus_handle(),
1031 })
1032 }
1033 }
1034
1035 impl gpui_base::dock::Panel for ToolbarZoomProbe {
1036 fn panel_name(&self) -> &'static str {
1037 "ToolbarZoomProbe"
1038 }
1039 }
1040
1041 impl Panel for ToolbarZoomProbe {
1042 fn zoom_control(&self, _: &App) -> Option<PanelControl> {
1043 Some(PanelControl::Toolbar)
1044 }
1045 }
1046
1047 impl EventEmitter<PanelEvent> for ToolbarZoomProbe {}
1048
1049 impl Focusable for ToolbarZoomProbe {
1050 fn focus_handle(&self, _: &App) -> FocusHandle {
1051 self.focus_handle.clone()
1052 }
1053 }
1054
1055 impl Render for ToolbarZoomProbe {
1056 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
1057 Empty
1058 }
1059 }
1060
1061 struct NoControlProbe {
1064 focus_handle: FocusHandle,
1065 }
1066
1067 impl NoControlProbe {
1068 fn new(cx: &mut App) -> Entity<Self> {
1069 cx.new(|cx| Self {
1070 focus_handle: cx.focus_handle(),
1071 })
1072 }
1073 }
1074
1075 impl gpui_base::dock::Panel for NoControlProbe {
1076 fn panel_name(&self) -> &'static str {
1077 "NoControlProbe"
1078 }
1079 }
1080
1081 impl Panel for NoControlProbe {
1082 fn zoom_control(&self, _: &App) -> Option<PanelControl> {
1083 None
1084 }
1085 }
1086
1087 impl EventEmitter<PanelEvent> for NoControlProbe {}
1088
1089 impl Focusable for NoControlProbe {
1090 fn focus_handle(&self, _: &App) -> FocusHandle {
1091 self.focus_handle.clone()
1092 }
1093 }
1094
1095 impl Render for NoControlProbe {
1096 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
1097 Empty
1098 }
1099 }
1100
1101 struct UnzoomableProbe {
1104 focus_handle: FocusHandle,
1105 }
1106
1107 impl gpui_base::dock::Panel for UnzoomableProbe {
1108 fn panel_name(&self) -> &'static str {
1109 "UnzoomableProbe"
1110 }
1111
1112 fn zoomable(&self, _: &App) -> bool {
1113 false
1114 }
1115 }
1116
1117 impl Panel for UnzoomableProbe {
1118 fn zoom_control(&self, _: &App) -> Option<PanelControl> {
1119 Some(PanelControl::Toolbar)
1120 }
1121 }
1122
1123 impl EventEmitter<PanelEvent> for UnzoomableProbe {}
1124
1125 impl Focusable for UnzoomableProbe {
1126 fn focus_handle(&self, _: &App) -> FocusHandle {
1127 self.focus_handle.clone()
1128 }
1129 }
1130
1131 impl Render for UnzoomableProbe {
1132 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
1133 Empty
1134 }
1135 }
1136
1137 fn drew_zoom_control(
1144 cx: &mut TestAppContext,
1145 panel: impl FnOnce(&mut App) -> Arc<dyn gpui_base::dock::PanelView>,
1146 ) -> bool {
1147 cx.update(|cx| {
1148 crate::init(cx);
1149 });
1150 let (area, cx) = cx.add_window_view(|window, cx| {
1151 let skin = DockSkin::new(cx);
1152 DockArea::new("skin", None, window, cx).with_renderer(skin)
1153 });
1154
1155 cx.update(|window, cx| {
1156 let layout = DockLayout::tabs().panel_view(panel(cx), cx);
1157 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1158 });
1159 cx.run_until_parked();
1160 cx.update(|window, cx| crate::test_redraw(window, cx));
1161 cx.debug_bounds(ZOOM_CONTROL_SELECTOR).is_some()
1162 }
1163
1164 #[gpui::test]
1168 fn a_panel_base_will_not_zoom_gets_no_zoom_control(cx: &mut TestAppContext) {
1169 let drew = drew_zoom_control(cx, |cx| {
1170 panel_handle(cx.new(|cx| UnzoomableProbe {
1171 focus_handle: cx.focus_handle(),
1172 }))
1173 });
1174
1175 assert!(
1176 !drew,
1177 "the panel names a place for the control, but base refuses the zoom"
1178 );
1179 }
1180
1181 #[gpui::test]
1185 fn a_zoomable_panel_gets_its_zoom_control(cx: &mut TestAppContext) {
1186 let drew = drew_zoom_control(cx, |cx| panel_handle(ToolbarZoomProbe::new(cx)));
1187
1188 assert!(
1189 drew,
1190 "a zoomable panel asking for a toolbar control gets one"
1191 );
1192 }
1193
1194 #[gpui::test]
1204 fn the_centre_and_the_bottom_dock_share_the_column(cx: &mut TestAppContext) {
1205 cx.update(|cx| {
1206 crate::init(cx);
1207 });
1208 let centre = Rc::new(Cell::new(px(0.)));
1209 let bottom = Rc::new(Cell::new(px(0.)));
1210 let (area, cx) = cx.add_window_view(|window, cx| {
1211 let skin = DockSkin::new(cx);
1212 DockArea::new("skin", None, window, cx).with_renderer(skin)
1213 });
1214
1215 let (centre_probe, bottom_probe) = (centre.clone(), bottom.clone());
1216 cx.update(|window, cx| {
1217 let centre_panel = MeasuredProbe::new(centre_probe, cx);
1218 let bottom_panel = MeasuredProbe::new(bottom_probe, cx);
1219 area.update(cx, |area, cx| {
1220 area.set_center(
1224 DockLayout::v_split().child(
1225 DockLayout::h_split().child(
1226 DockLayout::tabs().panel_view(panel_handle(centre_panel), cx),
1227 None,
1228 ),
1229 None,
1230 ),
1231 window,
1232 cx,
1233 );
1234 area.set_dock(
1235 DockPlacement::Bottom,
1236 DockLayout::tabs().panel_view(panel_handle(bottom_panel), cx),
1237 window,
1238 cx,
1239 );
1240 area.set_dock_size(DockPlacement::Bottom, px(200.), window, cx);
1241 });
1242 });
1243 cx.run_until_parked();
1244 cx.update(|window, cx| crate::test_redraw(window, cx));
1245
1246 let window_height = cx.update(|window, _| window.viewport_size().height);
1247 assert!(
1248 centre.get() > px(0.),
1249 "the centre panel must receive height; it got {:?}",
1250 centre.get()
1251 );
1252 assert!(
1253 bottom.get() > px(0.),
1254 "the bottom dock's panel must receive height; it got {:?}",
1255 bottom.get()
1256 );
1257 assert!(
1258 centre.get() < window_height - px(150.),
1259 "the centre must give the 200px bottom dock its share; the centre \
1260 got {:?} of {window_height:?}",
1261 centre.get()
1262 );
1263 }
1264
1265 #[gpui::test]
1280 fn a_split_fills_its_container_whichever_slots_are_hidden(cx: &mut TestAppContext) {
1281 cx.update(|cx| {
1282 crate::init(cx);
1283 });
1284 let heights: Vec<Rc<Cell<Pixels>>> = (0..3).map(|_| Rc::new(Cell::new(px(0.)))).collect();
1285 let (area, cx) = cx.add_window_view(|window, cx| {
1286 let skin = DockSkin::new(cx);
1287 DockArea::new("skin", None, window, cx).with_renderer(skin)
1288 });
1289
1290 let slots = heights.clone();
1291 let probes = cx.update(|window, cx| {
1292 let probes: Vec<_> = slots
1293 .iter()
1294 .map(|height| HideableProbe::new(height.clone(), cx))
1295 .collect();
1296 area.update(cx, |area, cx| {
1297 area.set_dock(
1298 DockPlacement::Right,
1299 probes.iter().zip([260., 320., 200.]).fold(
1300 DockLayout::v_split(),
1301 |split, (probe, size)| {
1302 split.child(
1303 DockLayout::tabs().panel_view(panel_handle(probe.clone()), cx),
1304 Some(px(size)),
1305 )
1306 },
1307 ),
1308 window,
1309 cx,
1310 );
1311 area.set_dock_size(DockPlacement::Right, px(380.), window, cx);
1312 });
1313 probes
1314 });
1315 cx.run_until_parked();
1316 let draw = |cx: &mut VisualTestContext| {
1317 cx.update(|window, _| window.refresh());
1318 cx.update(|window, cx| crate::test_redraw(window, cx));
1319 cx.update(|window, cx| crate::test_redraw(window, cx));
1320 };
1321 draw(cx);
1322
1323 let dock_height = cx.update(|window, _| window.viewport_size().height);
1324 let drawn: Pixels = heights.iter().map(|height| height.get()).sum();
1328 let bar = (dock_height - drawn) / 3.;
1329 assert!(
1330 bar > px(0.) && bar < px(60.),
1331 "the three slots fill the dock to begin with, one tab bar each; \
1332 that leaves {bar:?} per slot of {dock_height:?}"
1333 );
1334
1335 for hidden in 1..0b111u8 {
1338 let shown = (0..3).filter(|slot| hidden & (1 << slot) == 0);
1339 cx.update(|_, cx| {
1340 for (slot, probe) in probes.iter().enumerate() {
1341 heights[slot].set(px(-1.));
1344 probe.update(cx, |probe, cx| {
1345 probe.set_visible(hidden & (1 << slot) == 0, cx)
1346 });
1347 }
1348 });
1349 cx.run_until_parked();
1350 draw(cx);
1351
1352 let mut count = 0;
1353 let mut total = px(0.);
1354 for slot in shown {
1355 assert_ne!(
1356 heights[slot].get(),
1357 px(-1.),
1358 "hiding {hidden:03b}: slot {slot} is shown and must draw"
1359 );
1360 count += 1;
1361 total += heights[slot].get();
1362 }
1363 let empty = dock_height - total - bar * count as f32;
1364 assert!(
1365 empty.abs() < px(1.),
1366 "hiding {hidden:03b}: the drawn slots must take the hidden \
1367 ones' space between them; they left {empty:?} of \
1368 {dock_height:?} empty"
1369 );
1370 }
1371 }
1372
1373 #[gpui::test]
1377 fn the_zoom_action_reaches_the_group_through_the_skin(cx: &mut TestAppContext) {
1378 cx.update(|cx| {
1379 crate::init(cx);
1380 });
1381 let (area, cx) = cx.add_window_view(|window, cx| {
1382 let skin = DockSkin::new(cx);
1383 DockArea::new("skin", None, window, cx).with_renderer(skin)
1384 });
1385
1386 let panel = cx.update(|window, cx| {
1387 let panel = Probe::new(cx);
1388 let layout = DockLayout::tabs().panel_view(panel_handle(panel.clone()), cx);
1389 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1390 panel
1391 });
1392 cx.run_until_parked();
1393 cx.update(|window, cx| {
1394 panel.read(cx).focus_handle(cx).focus(window, cx);
1395 });
1396 cx.run_until_parked();
1397
1398 assert_eq!(cx.read(|cx| area.read(cx).is_zoomed()), false);
1399 cx.dispatch_action(ToggleZoom);
1400 cx.run_until_parked();
1401 assert_eq!(
1402 cx.read(|cx| area.read(cx).is_zoomed()),
1403 true,
1404 "the skin's frame is what carries the ToggleZoom handler"
1405 );
1406
1407 cx.dispatch_action(ToggleZoom);
1408 cx.run_until_parked();
1409 assert_eq!(cx.read(|cx| area.read(cx).is_zoomed()), false);
1410 }
1411
1412 #[gpui::test]
1418 fn the_zoom_action_refuses_a_panel_that_offers_no_control(cx: &mut TestAppContext) {
1419 cx.update(|cx| {
1420 crate::init(cx);
1421 });
1422 let (area, cx) = cx.add_window_view(|window, cx| {
1423 let skin = DockSkin::new(cx);
1424 DockArea::new("skin", None, window, cx).with_renderer(skin)
1425 });
1426
1427 let panel = cx.update(|window, cx| {
1428 let panel = NoControlProbe::new(cx);
1429 let layout = DockLayout::tabs().panel_view(panel_handle(panel.clone()), cx);
1430 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1431 panel
1432 });
1433 cx.run_until_parked();
1434 cx.update(|window, cx| {
1435 panel.read(cx).focus_handle(cx).focus(window, cx);
1436 });
1437 cx.run_until_parked();
1438
1439 cx.dispatch_action(ToggleZoom);
1440 cx.run_until_parked();
1441 assert_eq!(
1442 cx.read(|cx| area.read(cx).is_zoomed()),
1443 false,
1444 "no control means no zoom, however the zoom was asked for"
1445 );
1446 }
1447
1448 #[gpui::test]
1463 fn the_panel_content_region_gets_the_height_below_the_tab_bar(cx: &mut TestAppContext) {
1464 cx.update(|cx| {
1465 crate::init(cx);
1466 });
1467 let height = Rc::new(Cell::new(px(0.)));
1468 let (area, cx) = cx.add_window_view(|window, cx| {
1469 let skin = DockSkin::new(cx);
1470 DockArea::new("skin", None, window, cx).with_renderer(skin)
1471 });
1472
1473 let measured = height.clone();
1474 cx.update(|window, cx| {
1475 let panel = MeasuredProbe::new(measured, cx);
1476 let layout = DockLayout::tabs().panel_view(panel_handle(panel), cx);
1477 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1478 });
1479 cx.run_until_parked();
1480 cx.update(|window, cx| crate::test_redraw(window, cx));
1481
1482 let window_height = cx.update(|window, _| window.viewport_size().height);
1483 let content = height.get();
1484 assert!(
1485 content > px(0.),
1486 "the panel must receive height; it got {content:?} in a {window_height:?} window"
1487 );
1488 assert!(
1491 content > window_height - px(60.),
1492 "the panel should fill what the tab bar leaves; it got {content:?} \
1493 of {window_height:?}"
1494 );
1495 }
1496
1497 #[gpui::test]
1504 fn a_collapsed_dock_ignores_the_zoom_action(cx: &mut TestAppContext) {
1505 cx.update(|cx| {
1506 crate::init(cx);
1507 });
1508 let (area, cx) = cx.add_window_view(|window, cx| {
1509 let skin = DockSkin::new(cx);
1510 DockArea::new("skin", None, window, cx).with_renderer(skin)
1511 });
1512
1513 let panel = cx.update(|window, cx| {
1514 let panel = Probe::new(cx);
1515 let layout = DockLayout::tabs().panel_view(panel_handle(panel.clone()), cx);
1516 area.update(cx, |area, cx| {
1517 area.set_dock(DockPlacement::Bottom, layout, window, cx);
1518 area.toggle_dock(DockPlacement::Bottom, window, cx);
1519 });
1520 panel
1521 });
1522 cx.run_until_parked();
1523 cx.update(|window, cx| {
1524 panel.read(cx).focus_handle(cx).focus(window, cx);
1525 });
1526 cx.run_until_parked();
1527
1528 cx.dispatch_action(ToggleZoom);
1529 cx.run_until_parked();
1530 assert_eq!(
1531 cx.read(|cx| area.read(cx).is_zoomed()),
1532 false,
1533 "a collapsed group installs no action handler"
1534 );
1535 }
1536
1537 #[gpui::test]
1540 fn a_panel_without_a_title_bar_gets_the_whole_group(cx: &mut TestAppContext) {
1541 struct Chromeless {
1542 focus_handle: FocusHandle,
1543 height: Rc<Cell<Pixels>>,
1544 }
1545
1546 impl gpui_base::dock::Panel for Chromeless {
1547 fn panel_name(&self) -> &'static str {
1548 "Chromeless"
1549 }
1550 }
1551
1552 impl Panel for Chromeless {
1553 fn title_bar(&self, _: &App) -> bool {
1554 false
1555 }
1556 }
1557
1558 impl EventEmitter<PanelEvent> for Chromeless {}
1559
1560 impl Focusable for Chromeless {
1561 fn focus_handle(&self, _: &App) -> FocusHandle {
1562 self.focus_handle.clone()
1563 }
1564 }
1565
1566 impl Render for Chromeless {
1567 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
1568 let height = self.height.clone();
1569 div()
1570 .size_full()
1571 .on_prepaint(move |bounds, _, _| height.set(bounds.size.height))
1572 }
1573 }
1574
1575 cx.update(|cx| crate::init(cx));
1576
1577 let measure = |cx: &mut TestAppContext, chromeless: bool| -> Pixels {
1578 let height = Rc::new(Cell::new(px(0.)));
1579 let (area, cx) = cx.add_window_view(|window, cx| {
1580 let skin = DockSkin::new(cx);
1581 DockArea::new("skin", None, window, cx).with_renderer(skin)
1582 });
1583 let measured = height.clone();
1584 cx.update(|window, cx| {
1585 let panel: Arc<dyn gpui_base::dock::PanelView> = if chromeless {
1586 panel_handle(cx.new(|cx| Chromeless {
1587 focus_handle: cx.focus_handle(),
1588 height: measured,
1589 }))
1590 } else {
1591 panel_handle(MeasuredProbe::new(measured, cx))
1592 };
1593 let layout = DockLayout::tabs().panel_view(panel, cx);
1594 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1595 });
1596 cx.run_until_parked();
1597 cx.update(|window, cx| crate::test_redraw(window, cx));
1598 height.get()
1599 };
1600
1601 let with_title = measure(cx, false);
1602 let without = measure(cx, true);
1603 assert!(with_title > px(0.), "the probe must have been drawn");
1604 assert_eq!(
1605 without,
1606 with_title + px(30.),
1607 "the 30px the title bar took go to the panel instead"
1608 );
1609 }
1610
1611 struct TabProbe {
1613 focus_handle: FocusHandle,
1614 closable: bool,
1615 activated: Rc<Cell<bool>>,
1616 }
1617
1618 impl TabProbe {
1619 fn new(closable: bool, cx: &mut App) -> Entity<Self> {
1620 cx.new(|cx| Self {
1621 focus_handle: cx.focus_handle(),
1622 closable,
1623 activated: Rc::new(Cell::new(false)),
1624 })
1625 }
1626 }
1627
1628 impl gpui_base::dock::Panel for TabProbe {
1629 fn panel_name(&self) -> &'static str {
1630 "TabProbe"
1631 }
1632
1633 fn closable(&self, _: &App) -> bool {
1634 self.closable
1635 }
1636
1637 fn set_active(&mut self, active: bool, _: &mut Window, _: &mut Context<Self>) {
1638 if active {
1639 self.activated.set(true);
1640 }
1641 }
1642 }
1643
1644 impl Panel for TabProbe {}
1645
1646 impl EventEmitter<PanelEvent> for TabProbe {}
1647
1648 impl Focusable for TabProbe {
1649 fn focus_handle(&self, _: &App) -> FocusHandle {
1650 self.focus_handle.clone()
1651 }
1652 }
1653
1654 impl Render for TabProbe {
1655 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
1656 Empty
1657 }
1658 }
1659
1660 fn drew_close_button(cx: &mut TestAppContext, closable: bool, enabled: bool) -> bool {
1665 cx.update(|cx| crate::init(cx));
1666 let mut skin = None;
1667 let (area, cx) = cx.add_window_view(|window, cx| {
1668 let renderer = DockSkin::new(cx);
1669 skin = Some(renderer.clone());
1670 DockArea::new("skin", None, window, cx).with_renderer(renderer)
1671 });
1672 let skin = skin.expect("skin constructed with the area");
1673 if enabled {
1674 cx.update(|_, cx| skin.set_close_button_visible(true, cx));
1675 }
1676 cx.update(|window, cx| {
1677 let under_test = TabProbe::new(closable, cx);
1678 let filler = TabProbe::new(false, cx);
1679 let layout = DockLayout::tabs()
1680 .panel_view(panel_handle(under_test), cx)
1681 .panel_view(panel_handle(filler), cx);
1682 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1683 });
1684 cx.run_until_parked();
1685 cx.update(|window, cx| crate::test_redraw(window, cx));
1686 let button = cx.debug_bounds(CLOSE_BUTTON_SELECTOR);
1687 if enabled && closable {
1688 assert_eq!(
1689 button.as_ref().map(|bounds| bounds.size.width),
1690 Some(px(20.)),
1691 "the close button keeps the standard XS hover target"
1692 );
1693 }
1694 button.is_some()
1695 }
1696
1697 #[gpui::test]
1699 fn a_closable_panel_gets_a_close_button(cx: &mut TestAppContext) {
1700 assert!(
1701 drew_close_button(cx, true, true),
1702 "a closable panel's tab must draw a close button"
1703 );
1704 }
1705
1706 #[gpui::test]
1708 fn a_non_closable_panel_gets_no_close_button(cx: &mut TestAppContext) {
1709 assert!(
1710 !drew_close_button(cx, false, true),
1711 "a panel that reports itself non-closable must draw no close button"
1712 );
1713 }
1714
1715 #[gpui::test]
1716 fn close_buttons_are_off_by_default(cx: &mut TestAppContext) {
1717 assert!(!drew_close_button(cx, true, false));
1718 }
1719
1720 #[gpui::test]
1721 fn close_button_visibility_updates_after_the_skin_setting_changes(cx: &mut TestAppContext) {
1722 cx.update(|cx| crate::init(cx));
1723 let mut skin = None;
1724 let (area, cx) = cx.add_window_view(|window, cx| {
1725 let renderer = DockSkin::new(cx);
1726 skin = Some(renderer.clone());
1727 DockArea::new("skin", None, window, cx).with_renderer(renderer)
1728 });
1729 let skin = skin.expect("skin constructed with the area");
1730 cx.update(|window, cx| {
1731 let panel = TabProbe::new(true, cx);
1732 let filler = TabProbe::new(false, cx);
1733 area.update(cx, |area, cx| {
1734 area.set_center(
1735 DockLayout::tabs()
1736 .panel_view(panel_handle(panel), cx)
1737 .panel_view(panel_handle(filler), cx),
1738 window,
1739 cx,
1740 )
1741 });
1742 });
1743
1744 for (visible, expected) in [(false, false), (true, true), (false, false)] {
1745 cx.update(|_, cx| skin.set_close_button_visible(visible, cx));
1746 cx.run_until_parked();
1747 cx.update(|window, cx| crate::test_redraw(window, cx));
1748 assert_eq!(cx.debug_bounds(CLOSE_BUTTON_SELECTOR).is_some(), expected);
1749 }
1750 }
1751
1752 #[gpui::test]
1756 fn clicking_a_close_button_removes_only_that_tab(cx: &mut TestAppContext) {
1757 cx.update(|cx| crate::init(cx));
1758 let mut skin = None;
1759 let (area, cx) = cx.add_window_view(|window, cx| {
1760 let renderer = DockSkin::new(cx);
1761 skin = Some(renderer.clone());
1762 DockArea::new("skin", None, window, cx).with_renderer(renderer)
1763 });
1764 cx.update(|_, cx| skin.unwrap().set_close_button_visible(true, cx));
1765
1766 let (closable_id, filler_id, activated) = cx.update(|window, cx| {
1767 let filler = TabProbe::new(false, cx);
1768 let closable = TabProbe::new(true, cx);
1769 let activated = closable.read(cx).activated.clone();
1770 let filler_handle = panel_handle(filler);
1771 let closable_handle = panel_handle(closable);
1772 let ids = (closable_handle.panel_id(cx), filler_handle.panel_id(cx));
1773 let layout = DockLayout::tabs()
1776 .panel_view(filler_handle, cx)
1777 .panel_view(closable_handle, cx);
1778 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1779 (ids.0, ids.1, activated)
1780 });
1781 cx.run_until_parked();
1782 cx.update(|window, cx| crate::test_redraw(window, cx));
1783
1784 assert!(
1785 cx.update(|_, cx| area.read(cx).panel(closable_id).is_some()),
1786 "the closable tab starts owned by the area"
1787 );
1788
1789 let button = cx
1790 .debug_bounds(CLOSE_BUTTON_SELECTOR)
1791 .expect("the non-active closable tab draws a close button");
1792 cx.simulate_mouse_down(button.center(), MouseButton::Left, Modifiers::none());
1793 cx.simulate_mouse_up(button.center(), MouseButton::Left, Modifiers::none());
1794 cx.run_until_parked();
1795
1796 assert!(
1797 cx.update(|_, cx| area.read(cx).panel(closable_id).is_none()),
1798 "the close click removes exactly the tab it belongs to"
1799 );
1800 assert!(
1801 cx.update(|_, cx| area.read(cx).panel(filler_id).is_some()),
1802 "the displayed filler tab is untouched"
1803 );
1804 assert!(
1805 !activated.get(),
1806 "stop_propagation keeps the close click from also selecting the tab"
1807 );
1808 }
1809
1810 #[gpui::test]
1814 fn a_clicked_tab_is_revealed_by_the_tab_bar_not_snapped(cx: &mut TestAppContext) {
1815 cx.update(|cx| {
1816 crate::init(cx);
1817 cx.set_reduce_motion(false);
1818 });
1819 let (area, cx) = cx.add_window_view(|window, cx| {
1820 DockArea::new("skin", None, window, cx).with_renderer(DockSkin::new(cx))
1821 });
1822 cx.simulate_resize(gpui::size(px(300.), px(300.)));
1823 cx.update(|window, cx| {
1824 let layout = (0..8).fold(DockLayout::tabs(), |layout, _| {
1825 layout.panel_view(panel_handle(Probe::new(cx)), cx)
1826 });
1827 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1828 });
1829 cx.run_until_parked();
1830 cx.update(|window, cx| crate::test_redraw(window, cx));
1831
1832 let tab = |cx: &mut VisualTestContext, ix: usize| {
1834 cx.debug_bounds(format!("{TAB_SELECTOR}-{ix}").leak())
1835 .unwrap()
1836 };
1837 let first_left = |cx: &mut VisualTestContext| tab(cx, 0).left();
1838 let start = first_left(cx);
1839 let clipped = (0..8)
1842 .map(|ix| tab(cx, ix))
1843 .find(|tab| tab.right() > px(300.))
1844 .unwrap();
1845
1846 cx.simulate_click(
1847 clipped.origin + gpui::point(px(4.), px(16.)),
1848 Modifiers::none(),
1849 );
1850 cx.update(|window, cx| crate::test_redraw(window, cx));
1851 assert_eq!(first_left(cx), start, "the first frame has not snapped");
1852
1853 let mut lefts = Vec::new();
1854 for _ in 0..60 {
1855 cx.executor()
1856 .advance_clock(std::time::Duration::from_millis(16));
1857 cx.update(|window, cx| {
1858 window.simulate_next_frame(cx);
1859 crate::test_redraw(window, cx);
1860 });
1861 lefts.push(first_left(cx));
1862 }
1863 let end = *lefts.last().unwrap();
1864 assert!(end < start, "the strip scrolled");
1865 assert!(
1866 lefts.iter().any(|left| *left < start && *left > end),
1867 "the strip slid rather than jumped: {lefts:?}"
1868 );
1869 }
1870
1871 #[gpui::test]
1874 fn the_collapse_button_reports_the_toggle(cx: &mut TestAppContext) {
1875 cx.update(|cx| crate::init(cx));
1876 let (area, cx) = cx.add_window_view(|window, cx| {
1877 DockArea::new("toggle", None, window, cx).with_renderer(DockSkin::new(cx))
1878 });
1879 cx.simulate_resize(gpui::size(px(800.), px(600.)));
1880 let events = Rc::new(RefCell::new(Vec::new()));
1881 cx.update(|window, cx| {
1882 let center = DockLayout::tabs().panel_view(panel_handle(Probe::new(cx)), cx);
1883 let left = DockLayout::tabs().panel_view(panel_handle(Probe::new(cx)), cx);
1884 area.update(cx, |area, cx| {
1885 area.set_center(center, window, cx);
1886 area.set_dock(DockPlacement::Left, left, window, cx);
1887 });
1888 let events = events.clone();
1889 cx.subscribe(&area, move |_, event: &crate::dock::DockToggleEvent, _| {
1890 events
1891 .borrow_mut()
1892 .push((event.placement(), event.is_open()));
1893 })
1894 .detach();
1895 });
1896 cx.run_until_parked();
1897 cx.update(|window, cx| crate::test_redraw(window, cx));
1898
1899 let selector = format!("{DOCK_TOGGLE_SELECTOR}-{:?}", DockPlacement::Left).leak();
1900 let button = cx
1901 .debug_bounds(selector)
1902 .expect("the left dock offers a toggle");
1903 cx.simulate_click(button.center(), Modifiers::none());
1904 cx.run_until_parked();
1905 assert!(!cx.read(|cx| area.read(cx).is_dock_open(DockPlacement::Left)));
1906 assert_eq!(*events.borrow(), vec![(DockPlacement::Left, false)]);
1907
1908 cx.update(|window, cx| {
1909 area.update(cx, |area, cx| {
1910 area.toggle_dock(DockPlacement::Left, window, cx)
1911 });
1912 });
1913 cx.run_until_parked();
1914 assert_eq!(
1915 events.borrow().len(),
1916 1,
1917 "a toggle made by code is not reported"
1918 );
1919 }
1920
1921 #[gpui::test]
1924 fn opening_the_bottom_strip_reports_the_toggle(cx: &mut TestAppContext) {
1925 cx.update(|cx| crate::init(cx));
1926 let (area, cx) = cx.add_window_view(|window, cx| {
1927 DockArea::new("strip", None, window, cx).with_renderer(DockSkin::new(cx))
1928 });
1929 cx.simulate_resize(gpui::size(px(800.), px(600.)));
1930 let events = Rc::new(RefCell::new(Vec::new()));
1931 cx.update(|window, cx| {
1932 let layout = DockLayout::tabs()
1934 .panel_view(panel_handle(Probe::new(cx)), cx)
1935 .panel_view(panel_handle(Probe::new(cx)), cx);
1936 area.update(cx, |area, cx| {
1937 area.set_dock(DockPlacement::Bottom, layout, window, cx);
1938 area.toggle_dock(DockPlacement::Bottom, window, cx);
1939 });
1940 let events = events.clone();
1941 cx.subscribe(&area, move |_, event: &crate::dock::DockToggleEvent, _| {
1942 events
1943 .borrow_mut()
1944 .push((event.placement(), event.is_open()));
1945 })
1946 .detach();
1947 });
1948 cx.run_until_parked();
1949 cx.update(|window, cx| crate::test_redraw(window, cx));
1950 assert!(!cx.read(|cx| area.read(cx).is_dock_open(DockPlacement::Bottom)));
1951
1952 let tab = cx
1953 .debug_bounds(format!("{TAB_SELECTOR}-0").leak())
1954 .expect("the collapsed strip still draws its tab");
1955 cx.simulate_click(tab.center(), Modifiers::none());
1956 cx.run_until_parked();
1957 assert_eq!(*events.borrow(), vec![(DockPlacement::Bottom, true)]);
1958 }
1959
1960 #[gpui::test]
1962 fn a_refused_toggle_is_not_reported(cx: &mut TestAppContext) {
1963 cx.update(|cx| crate::init(cx));
1964 let (area, cx) = cx.add_window_view(|window, cx| {
1965 DockArea::new("refuse", None, window, cx).with_renderer(DockSkin::new(cx))
1966 });
1967 let events = Rc::new(RefCell::new(Vec::new()));
1968 cx.update(|window, cx| {
1969 let layout = DockLayout::tabs().panel_view(panel_handle(Probe::new(cx)), cx);
1970 area.update(cx, |area, cx| {
1971 area.set_dock(DockPlacement::Left, layout, window, cx);
1972 area.set_dock_collapsible(DockPlacement::Left, false, window, cx);
1973 });
1974 let events = events.clone();
1975 cx.subscribe(&area, move |_, event: &crate::dock::DockToggleEvent, _| {
1976 events
1977 .borrow_mut()
1978 .push((event.placement(), event.is_open()));
1979 })
1980 .detach();
1981 });
1982 cx.update(|window, cx| {
1983 area.update(cx, |area, cx| {
1984 toggle_dock_by_user(area, DockPlacement::Left, window, cx)
1985 });
1986 });
1987 cx.run_until_parked();
1988 assert!(cx.read(|cx| area.read(cx).is_dock_open(DockPlacement::Left)));
1989 assert!(events.borrow().is_empty());
1990 }
1991
1992 #[gpui::test]
1996 fn a_collapsed_group_draws_no_close_button(cx: &mut TestAppContext) {
1997 cx.update(|cx| crate::init(cx));
1998 let mut skin = None;
1999 let (area, cx) = cx.add_window_view(|window, cx| {
2000 let renderer = DockSkin::new(cx);
2001 skin = Some(renderer.clone());
2002 DockArea::new("skin", None, window, cx).with_renderer(renderer)
2003 });
2004 cx.update(|_, cx| skin.unwrap().set_close_button_visible(true, cx));
2005 cx.update(|window, cx| {
2008 let a = TabProbe::new(true, cx);
2009 let b = TabProbe::new(true, cx);
2010 let layout = DockLayout::tabs()
2011 .panel_view(panel_handle(a), cx)
2012 .panel_view(panel_handle(b), cx);
2013 area.update(cx, |area, cx| {
2014 area.set_dock(DockPlacement::Bottom, layout, window, cx);
2015 if !area.is_dock_open(DockPlacement::Bottom) {
2016 area.toggle_dock(DockPlacement::Bottom, window, cx);
2017 }
2018 });
2019 });
2020 cx.run_until_parked();
2021 cx.update(|window, cx| crate::test_redraw(window, cx));
2022 assert!(
2023 cx.debug_bounds(CLOSE_BUTTON_SELECTOR).is_some(),
2024 "an open group with two closable panels offers a close button"
2025 );
2026
2027 cx.update(|window, cx| {
2030 area.update(cx, |area, cx| {
2031 area.toggle_dock(DockPlacement::Bottom, window, cx)
2032 });
2033 });
2034 cx.run_until_parked();
2035 cx.update(|window, cx| crate::test_redraw(window, cx));
2036 assert!(
2037 cx.debug_bounds(CLOSE_BUTTON_SELECTOR).is_none(),
2038 "a collapsed group must not offer a close button"
2039 );
2040 }
2041}