1use std::{
9 cell::{Cell, RefCell},
10 collections::HashSet,
11 rc::Rc,
12 sync::Arc,
13 time::Duration,
14};
15
16use gpui::{
17 Anchor, AnyElement, AnyView, App, AppContext as _, Context, Div, Empty,
18 InteractiveElement as _, IntoElement, ParentElement as _, Render, ScrollHandle, SharedString,
19 Stateful, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Window, div,
20 prelude::FluentBuilder as _, px,
21};
22use gpui_base::{
23 Spring,
24 dock::{
25 AnyDrag, DockPlacement, DragPanel, DropIndicator, NodeId, PaneNode, PaneRef, PanelId,
26 TabGroupContext, TabGroupRenderer,
27 },
28 spring,
29};
30use rust_i18n::t;
31
32use crate::{
33 ActiveTheme as _, IconName, Selectable as _, Sizable as _,
34 button::{Button, ButtonVariants as _},
35 dock::{ClosePanel, PanelControl, PanelHandle, PanelStyle, SkinShared, ToggleZoom},
36 h_flex,
37 menu::DropdownMenu as _,
38 tab::{Tab, TabBar},
39};
40
41const ZOOM_CONTROL_SELECTOR: &str = "dock-tab-bar-zoom-control";
44
45const DRAG_PREVIEW_SIZE: gpui::Size<gpui::Pixels> = gpui::size(px(96.), px(30.));
48
49const PLACEHOLDER_SPRING: Spring = Spring::new(Duration::from_millis(200)).with_epsilon(0.5);
55
56pub struct DragPanelPreview {
61 panel: Arc<dyn gpui_base::dock::PanelView>,
62}
63
64impl Render for DragPanelPreview {
65 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
66 div()
67 .id("drag-panel")
68 .cursor_grab()
69 .py_1()
70 .px_3()
71 .w_24()
72 .overflow_hidden()
73 .whitespace_nowrap()
74 .border_1()
75 .border_color(cx.theme().border)
76 .rounded(cx.theme().radius)
77 .text_color(cx.theme().tab_foreground)
78 .bg(cx.theme().tokens.tab_active)
79 .opacity(0.75)
80 .child(panel_title(&self.panel, window, cx))
81 }
82}
83
84pub(crate) fn panel_title(
87 panel: &Arc<dyn gpui_base::dock::PanelView>,
88 window: &mut Window,
89 cx: &mut App,
90) -> AnyElement {
91 let Some(handle) = PanelHandle::of(panel) else {
92 let name = panel.panel_name(cx);
93 warn_unwrapped_once(panel.panel_id(cx), name);
94 return SharedString::from(name).into_any_element();
95 };
96 handle.title(window, cx)
97}
98
99thread_local! {
100 static WARNED_UNWRAPPED: RefCell<HashSet<PanelId>> = RefCell::new(HashSet::new());
111}
112
113fn warn_unwrapped_once(panel: PanelId, name: &'static str) {
120 if !WARNED_UNWRAPPED.with(|warned| warned.borrow_mut().insert(panel)) {
121 return;
122 }
123 tracing::warn!(
124 panel = name,
125 "dock panel reached the skin without its presentation handle, so it \
126 draws its panel name instead of its title; install it with \
127 `gpui_component::dock::panel_handle(..)` and `DockLayout::panel_view` \
128 / `DockArea::add_panel_view` rather than `DockLayout::panel` / \
129 `DockArea::add_panel`"
130 );
131}
132
133fn zoom_control(group: &TabGroupContext, cx: &App) -> Option<PanelControl> {
144 let panel = group.active_panel()?;
145 panel
146 .zoomable(cx)
147 .then(|| PanelHandle::of(panel).and_then(|handle| handle.zoom_control(cx)))
148 .flatten()
149}
150
151fn tab_drag(group: &TabGroupContext, ix: usize, cx: &App) -> Option<DragPanel> {
160 group
161 .is_draggable()
162 .then(|| group.drag_panel(ix, cx))
163 .flatten()
164}
165
166fn left_top_group(node: &PaneNode) -> Option<NodeId> {
169 match node.kind() {
170 PaneRef::Tabs { .. } => Some(node.id()),
171 PaneRef::Split { children, .. } => children.first().and_then(left_top_group),
172 PaneRef::Tiles { .. } => None,
173 }
174}
175
176fn right_top_group(node: &PaneNode) -> Option<NodeId> {
180 match node.kind() {
181 PaneRef::Tabs { .. } => Some(node.id()),
182 PaneRef::Split { axis, children, .. } => match axis {
183 gpui::Axis::Vertical => children.first(),
184 gpui::Axis::Horizontal => children.last(),
185 }
186 .and_then(right_top_group),
187 PaneRef::Tiles { .. } => None,
188 }
189}
190
191pub(crate) struct TabGroupSkin {
197 shared: Rc<SkinShared>,
198 scroll_handle: ScrollHandle,
199 last_active_ix: Cell<Option<usize>>,
203}
204
205impl TabGroupSkin {
206 pub(crate) fn new(shared: Rc<SkinShared>) -> Self {
207 Self {
208 shared,
209 scroll_handle: ScrollHandle::default(),
210 last_active_ix: Cell::new(None),
211 }
212 }
213
214 fn dock_toggle_button(
217 &self,
218 placement: DockPlacement,
219 group: &TabGroupContext,
220 cx: &mut App,
221 ) -> Option<Button> {
222 if group.is_zoomed() || !self.shared.is_toggle_button_visible() {
223 return None;
224 }
225
226 let area = self.shared.area().upgrade()?;
227 let area = area.read(cx);
228 if !area.is_dock_collapsible(placement) {
231 return None;
232 }
233
234 let designated = match placement {
235 DockPlacement::Left => area
236 .layout(DockPlacement::Center)
237 .and_then(|tree| left_top_group(tree.root())),
238 DockPlacement::Right => area
239 .layout(DockPlacement::Center)
240 .and_then(|tree| right_top_group(tree.root())),
241 DockPlacement::Bottom => area
242 .layout(DockPlacement::Bottom)
243 .and_then(|tree| left_top_group(tree.root())),
244 DockPlacement::Center => None,
245 };
246 if designated != Some(group.node()) {
247 return None;
248 }
249
250 let is_open = area.is_dock_open(placement);
251 let icon = match (placement, is_open) {
252 (DockPlacement::Left, true) => IconName::PanelLeft,
253 (DockPlacement::Left, false) => IconName::PanelLeftOpen,
254 (DockPlacement::Right, true) => IconName::PanelRight,
255 (DockPlacement::Right, false) => IconName::PanelRightOpen,
256 (DockPlacement::Bottom, true) => IconName::PanelBottom,
257 (DockPlacement::Bottom, false) => IconName::PanelBottomOpen,
258 (DockPlacement::Center, _) => return None,
259 };
260
261 let area = self.shared.area().clone();
262 Some(
263 Button::new(SharedString::from(format!("toggle-dock:{:?}", placement)))
264 .icon(icon)
265 .xsmall()
266 .ghost()
267 .tab_stop(false)
268 .tooltip(match is_open {
269 true => t!("Dock.Collapse"),
270 false => t!("Dock.Expand"),
271 })
272 .on_click(move |_, window, cx| {
273 _ = area.update(cx, |area, cx| area.toggle_dock(placement, window, cx));
274 }),
275 )
276 }
277
278 fn render_toolbar(
281 &self,
282 group: &TabGroupContext,
283 window: &mut Window,
284 cx: &mut App,
285 ) -> impl IntoElement {
286 if group.is_collapsed() {
287 return div();
288 }
289
290 let zoomed = group.is_zoomed();
291 let handle = group.active_panel().and_then(PanelHandle::of);
292 let control = zoom_control(group, cx);
293 let toolbar_zoom = control.is_some_and(|control| control.toolbar_visible());
294 let menu_zoom = control.is_some_and(|control| control.menu_visible());
295 let closable = group.is_closable();
296 let buttons = handle.and_then(|handle| handle.toolbar_buttons(window, cx));
297 let panel = handle.map(|handle| handle.panel());
298
299 h_flex()
300 .gap_1()
301 .occlude()
302 .when_some(buttons, |this, buttons| {
303 this.children(
304 buttons
305 .into_iter()
306 .map(|button| button.xsmall().ghost().tab_stop(false)),
307 )
308 })
309 .when_some(
310 match (zoomed, toolbar_zoom) {
311 (true, _) => Some(("zoom-out", IconName::Minimize, t!("Dock.Zoom Out"))),
312 (false, true) => Some(("zoom-in", IconName::Maximize, t!("Dock.Zoom In"))),
313 (false, false) => None,
314 },
315 |this, (id, icon, tooltip)| {
316 this.child(
317 Button::new(id)
318 .icon(icon)
319 .xsmall()
320 .ghost()
321 .tab_stop(false)
322 .tooltip_with_action(tooltip, &ToggleZoom, None)
323 .selected(zoomed)
324 .debug_selector(|| ZOOM_CONTROL_SELECTOR.to_string())
329 .on_click({
330 let group = group.clone();
331 move |_, window, cx| group.toggle_zoom(window, cx)
332 }),
333 )
334 },
335 )
336 .child(
337 Button::new("menu")
338 .icon(IconName::Ellipsis)
339 .xsmall()
340 .ghost()
341 .tab_stop(false)
342 .dropdown_menu(move |menu, window, cx| {
343 menu.when_some(panel.clone(), |menu, panel| {
344 panel.dropdown_menu(menu, window, cx)
345 })
346 .separator()
347 .menu_with_disabled(
348 match zoomed {
349 true => t!("Dock.Zoom Out"),
350 false => t!("Dock.Zoom In"),
351 },
352 Box::new(ToggleZoom),
353 !menu_zoom,
354 )
355 .when(closable, |menu| {
356 menu.separator()
357 .menu(t!("Dock.Close"), Box::new(ClosePanel))
358 })
359 })
360 .anchor(Anchor::TopRight),
361 )
362 }
363
364 fn render_title(
366 &self,
367 group: &TabGroupContext,
368 ix: usize,
369 window: &mut Window,
370 cx: &mut App,
371 ) -> AnyElement {
372 let panel = &group.panels()[ix];
373 let left_button = self.dock_toggle_button(DockPlacement::Left, group, cx);
374 let bottom_button = self.dock_toggle_button(DockPlacement::Bottom, group, cx);
375 let right_button = self.dock_toggle_button(DockPlacement::Right, group, cx);
376 let has_leading = left_button.is_some() || bottom_button.is_some();
377 let handle = PanelHandle::of(panel);
378 let title_style = handle.and_then(|handle| handle.title_style(cx));
379 let drag = tab_drag(group, ix, cx);
380
381 h_flex()
382 .justify_between()
383 .h(px(30.))
384 .py_2()
385 .pl_3()
386 .pr_2()
387 .when(left_button.is_some(), |this| this.pl_2())
388 .when(right_button.is_some(), |this| this.pr_2())
389 .when_some(title_style, |this, style| {
390 this.bg(style.background).text_color(style.foreground)
391 })
392 .when(has_leading, |this| {
393 this.child(
394 h_flex()
395 .flex_shrink_0()
396 .mr_1()
397 .gap_1()
398 .children(left_button)
399 .children(bottom_button),
400 )
401 })
402 .child(
403 div()
404 .id("tab")
405 .flex_1()
406 .min_w_16()
407 .overflow_hidden()
408 .text_ellipsis()
409 .whitespace_nowrap()
410 .child(panel_title(panel, window, cx))
411 .when_some(drag, |this, drag| {
412 this.on_drag(drag, {
413 let panel = panel.clone();
414 move |drag, offset, _, cx| {
415 cx.stop_propagation();
416 drag.set_drag_offset(offset);
417 drag.set_preview_size(DRAG_PREVIEW_SIZE);
418 cx.new(|_| DragPanelPreview {
419 panel: panel.clone(),
420 })
421 }
422 })
423 }),
424 )
425 .children(handle.and_then(|handle| handle.title_suffix(window, cx)))
426 .child(
427 h_flex()
428 .flex_shrink_0()
429 .ml_1()
430 .gap_1()
431 .child(self.render_toolbar(group, window, cx))
432 .children(right_button),
433 )
434 .into_any_element()
435 }
436
437 fn render_tabs(
439 &self,
440 group: &TabGroupContext,
441 window: &mut Window,
442 cx: &mut App,
443 ) -> AnyElement {
444 let left_button = self.dock_toggle_button(DockPlacement::Left, group, cx);
445 let bottom_button = self.dock_toggle_button(DockPlacement::Bottom, group, cx);
446 let right_button = self.dock_toggle_button(DockPlacement::Right, group, cx);
447 let has_leading = left_button.is_some() || bottom_button.is_some();
448 let is_bottom_dock = bottom_button.is_some();
449 let collapsed = group.is_collapsed();
450
451 let droppable = group.is_droppable();
452 let tabs_count = group.panels().len();
453 let active_ix = group.active_ix();
454 let displayed = group.active_panel().map(|panel| panel.panel_id(cx));
455 let visible: Vec<usize> = group
456 .panels()
457 .iter()
458 .enumerate()
459 .filter(|(_, panel)| panel.visible(cx))
460 .map(|(ix, _)| ix)
461 .collect();
462 let displayed_ix = displayed.and_then(|displayed| {
463 group
464 .panels()
465 .iter()
466 .position(|panel| panel.panel_id(cx) == displayed)
467 });
468
469 if self.last_active_ix.replace(Some(active_ix)) != Some(active_ix) {
472 if let Some(visible_ix) = visible.iter().position(|ix| *ix == active_ix) {
473 self.scroll_handle.scroll_to_item(visible_ix);
474 }
475 }
476
477 TabBar::new("tab-bar")
478 .track_scroll(&self.scroll_handle)
479 .when(has_leading, |this| {
480 this.prefix(
481 h_flex()
482 .items_center()
483 .top_0()
484 .right(-px(1.))
486 .border_r_1()
487 .border_b_1()
488 .h_full()
489 .border_color(cx.theme().border)
490 .bg(cx.theme().tokens.tab_bar)
491 .px_2()
492 .children(left_button)
493 .children(bottom_button),
494 )
495 })
496 .children(
497 visible
498 .into_iter()
499 .map(|ix| {
500 let panel = &group.panels()[ix];
501 let handle = PanelHandle::of(panel);
502 let drag = tab_drag(group, ix, cx);
503
504 Tab::new()
505 .ix(ix)
506 .tab_bar_prefix(has_leading)
507 .map(|this| match handle.and_then(|handle| handle.tab_name(cx)) {
508 Some(tab_name) => this.child(tab_name),
509 None => this.child(panel_title(panel, window, cx)),
510 })
511 .selected(!collapsed && Some(ix) == displayed_ix)
517 .on_click({
518 let group = group.clone();
519 let area = self.shared.area().clone();
520 move |_, window, cx| {
521 group.select_tab(ix, window, cx);
522
523 if is_bottom_dock && collapsed {
526 _ = area.update(cx, |area, cx| {
527 area.toggle_dock(DockPlacement::Bottom, window, cx)
528 });
529 }
530 }
531 })
532 .when(!collapsed, |this| {
535 this.when_some(drag, |this, drag| {
536 this.on_drag(drag, {
537 let panel = panel.clone();
538 move |drag, offset, _, cx| {
539 cx.stop_propagation();
540 drag.set_drag_offset(offset);
541 drag.set_preview_size(DRAG_PREVIEW_SIZE);
542 cx.new(|_| DragPanelPreview {
543 panel: panel.clone(),
544 })
545 }
546 })
547 })
548 .when(droppable, |this| {
549 this.drag_over::<DragPanel>(|this, _, _, cx| {
550 this.rounded_l_none()
551 .border_l_2()
552 .border_r_0()
553 .border_color(cx.theme().drag_border)
554 })
555 .on_drop({
556 let group = group.clone();
557 move |drag: &DragPanel, window, cx| {
558 group.drop_panel(
559 drag.clone(),
560 Some(ix),
561 true,
562 window,
563 cx,
564 );
565 }
566 })
567 .drag_over::<AnyDrag>(|this, _, _, cx| {
568 this.rounded_l_none()
569 .border_l_2()
570 .border_r_0()
571 .border_color(cx.theme().drag_border)
572 })
573 .on_drop({
574 let group = group.clone();
575 move |item: &AnyDrag, window, cx| {
576 group.drop_item(item.clone(), None, window, cx);
577 }
578 })
579 })
580 })
581 })
582 .collect::<Vec<_>>(),
583 )
584 .last_empty_space(
585 div()
587 .id("tab-bar-empty-space")
588 .h_full()
589 .flex_grow_1()
590 .min_w_16()
591 .when(droppable, |this| {
592 this.drag_over::<DragPanel>(|this, _, _, cx| {
593 this.bg(cx.theme().tokens.drop_target)
594 })
595 .on_drop({
596 let group = group.clone();
597 let node = group.node();
598 move |drag: &DragPanel, window, cx| {
599 let ix = (drag.source() == node).then(|| tabs_count - 1);
603 group.drop_panel(drag.clone(), ix, false, window, cx);
604 }
605 })
606 .drag_over::<AnyDrag>(|this, _, _, cx| {
607 this.bg(cx.theme().tokens.drop_target)
608 })
609 .on_drop({
610 let group = group.clone();
611 move |item: &AnyDrag, window, cx| {
612 group.drop_item(item.clone(), None, window, cx);
613 }
614 })
615 }),
616 )
617 .when(!collapsed, |this| {
618 this.suffix(
619 h_flex()
620 .items_center()
621 .top_0()
622 .right_0()
623 .border_l_1()
624 .border_b_1()
625 .h_full()
626 .border_color(cx.theme().border)
627 .bg(cx.theme().tokens.tab_bar)
628 .px_2()
629 .gap_1()
630 .children(
631 group
632 .active_panel()
633 .and_then(PanelHandle::of)
634 .and_then(|handle| handle.title_suffix(window, cx)),
635 )
636 .child(self.render_toolbar(group, window, cx))
637 .children(right_button),
638 )
639 })
640 .into_any_element()
641 }
642}
643
644impl TabGroupRenderer for TabGroupSkin {
645 fn frame(&self, group: &TabGroupContext, _: &mut Window, cx: &mut App) -> Stateful<Div> {
646 let control = zoom_control(group, cx);
647
648 div()
651 .id("tab-panel")
652 .bg(cx.theme().tokens.background)
653 .when(!group.is_collapsed(), |this| {
656 this.on_action({
657 let group = group.clone();
658 move |_: &ToggleZoom, window, cx| {
659 if !group.is_zoomed() && control.is_none() {
665 return;
666 }
667 group.toggle_zoom(window, cx);
668 }
669 })
670 .on_action({
671 let group = group.clone();
672 move |_: &ClosePanel, window, cx| {
673 let Some(panel) = group.active_panel() else {
674 return;
675 };
676 let panel = panel.panel_id(cx);
677 group.close(panel, window, cx);
678 }
679 })
680 })
681 }
682
683 fn content_frame(
684 &self,
685 group: &TabGroupContext,
686 _: &mut Window,
687 cx: &mut App,
688 ) -> Stateful<Div> {
689 let padded = group.panels().len() > 1
690 && group
691 .active_panel()
692 .and_then(PanelHandle::of)
693 .is_none_or(|handle| handle.inner_padding(cx));
694
695 div().id("active-panel").when(padded, |this| this.pt_2())
698 }
699
700 fn render_tab_bar(
701 &self,
702 group: &TabGroupContext,
703 window: &mut Window,
704 cx: &mut App,
705 ) -> AnyElement {
706 let visible: Vec<usize> = group
707 .panels()
708 .iter()
709 .enumerate()
710 .filter(|(_, panel)| panel.visible(cx))
711 .map(|(ix, _)| ix)
712 .collect();
713
714 match visible.as_slice() {
715 [] => Empty.into_any_element(),
716 [ix] if self.shared.panel_style() == PanelStyle::Auto => {
717 self.render_title(group, *ix, window, cx)
718 }
719 _ => self.render_tabs(group, window, cx),
720 }
721 }
722
723 fn render_active_panel(
724 &self,
725 panel: AnyView,
726 group: &TabGroupContext,
727 _: &mut Window,
728 _: &mut App,
729 ) -> AnyElement {
730 if group.is_collapsed() {
731 return Empty.into_any_element();
732 }
733
734 div()
735 .id("tab-content")
736 .overflow_y_scroll()
737 .overflow_x_hidden()
738 .flex_1()
739 .child(panel.cached(StyleRefinement::default().absolute().size_full()))
740 .into_any_element()
741 }
742
743 fn render_drop_indicator(
744 &self,
745 indicator: DropIndicator,
746 window: &mut Window,
747 cx: &mut App,
748 ) -> Option<AnyElement> {
749 let to = indicator.to();
750 let id = "drop-placeholder";
756 let left = spring((id, "left"), to.origin().x, PLACEHOLDER_SPRING, window, cx);
757 let top = spring((id, "top"), to.origin().y, PLACEHOLDER_SPRING, window, cx);
758 let width = spring(
759 (id, "width"),
760 to.size().width,
761 PLACEHOLDER_SPRING,
762 window,
763 cx,
764 );
765 let height = spring(
766 (id, "height"),
767 to.size().height,
768 PLACEHOLDER_SPRING,
769 window,
770 cx,
771 );
772
773 Some(
774 div()
775 .absolute()
776 .bg(cx.theme().tokens.drop_target)
777 .left(left)
778 .top(top)
779 .w(width)
780 .h(height)
781 .into_any_element(),
782 )
783 }
784}
785
786#[cfg(test)]
787mod tests {
788 use std::cell::RefCell;
789
790 use gpui::{
791 Entity, EventEmitter, FocusHandle, Focusable, Pixels, TestAppContext, VisualTestContext,
792 };
793 use gpui_base::dock::{
794 DockArea, DockAreaRenderer, DockLayout, DockPlacement, PanelEvent, TileContext,
795 TilesRenderer,
796 };
797
798 use super::*;
799 use crate::dock::{
800 DockSkin, Panel, panel_handle,
801 test_support::{HideableProbe, MeasuredProbe},
802 };
803
804 struct Probe {
805 focus_handle: FocusHandle,
806 }
807
808 impl Probe {
809 fn new(cx: &mut App) -> Entity<Self> {
810 cx.new(|cx| Self {
811 focus_handle: cx.focus_handle(),
812 })
813 }
814 }
815
816 impl gpui_base::dock::Panel for Probe {
817 fn panel_name(&self) -> &'static str {
818 "Probe"
819 }
820 }
821
822 impl Panel for Probe {}
823 impl EventEmitter<PanelEvent> for Probe {}
824
825 impl Focusable for Probe {
826 fn focus_handle(&self, _: &App) -> FocusHandle {
827 self.focus_handle.clone()
828 }
829 }
830
831 impl Render for Probe {
832 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
833 Empty
834 }
835 }
836
837 #[derive(Default)]
840 struct Recorded {
841 draggable: Vec<bool>,
843 }
844
845 struct Recorder {
846 log: Rc<RefCell<Recorded>>,
847 }
848
849 impl TabGroupRenderer for Recorder {
850 fn render_tab_bar(
851 &self,
852 group: &TabGroupContext,
853 _: &mut Window,
854 cx: &mut App,
855 ) -> AnyElement {
856 let mut log = self.log.borrow_mut();
857 for ix in 0..group.panels().len() {
858 log.draggable.push(tab_drag(group, ix, cx).is_some());
859 }
860 Empty.into_any_element()
861 }
862 }
863
864 impl TilesRenderer for Recorder {
865 fn render_drag_bar(&self, _: &TileContext, _: &mut Window, _: &mut App) -> AnyElement {
866 Empty.into_any_element()
867 }
868 }
869
870 impl DockAreaRenderer for Recorder {
871 fn frame(&self, _: &mut Window, _: &mut App) -> Stateful<Div> {
872 div().id("recorder").size_full()
873 }
874
875 fn tab_group_renderer(&self) -> Rc<dyn TabGroupRenderer> {
876 Rc::new(Recorder {
877 log: self.log.clone(),
878 })
879 }
880
881 fn tiles_renderer(&self) -> Rc<dyn TilesRenderer> {
882 Rc::new(Recorder {
883 log: self.log.clone(),
884 })
885 }
886 }
887
888 fn recording_area(
889 cx: &mut TestAppContext,
890 ) -> (
891 Entity<DockArea>,
892 Rc<RefCell<Recorded>>,
893 &mut VisualTestContext,
894 ) {
895 cx.update(|cx| {
896 crate::init(cx);
897 });
898 let log = Rc::new(RefCell::new(Recorded::default()));
899 let renderer = Rc::new(Recorder { log: log.clone() });
900 let (area, cx) = cx.add_window_view(|window, cx| {
901 DockArea::new("skin", None, window, cx).with_renderer(renderer)
902 });
903 (area, log, cx)
904 }
905
906 #[gpui::test]
910 fn the_last_group_in_a_dock_offers_no_drag(cx: &mut TestAppContext) {
911 let (area, log, cx) = recording_area(cx);
912
913 cx.update(|window, cx| {
914 let layout = DockLayout::tabs().panel_view(panel_handle(Probe::new(cx)), cx);
915 area.update(cx, |area, cx| area.set_center(layout, window, cx));
916 });
917 cx.run_until_parked();
918 log.borrow_mut().draggable.clear();
919 cx.update(|window, cx| window.draw(cx).clear(cx));
920
921 assert_eq!(
922 log.borrow().draggable,
923 vec![false],
924 "the only visible panel in the dock has nowhere to go, so its tab \
925 must not start a drag"
926 );
927 }
928
929 #[gpui::test]
930 fn a_group_beside_another_offers_a_drag(cx: &mut TestAppContext) {
931 let (area, log, cx) = recording_area(cx);
932
933 cx.update(|window, cx| {
934 let layout = DockLayout::h_split()
935 .child(
936 DockLayout::tabs().panel_view(panel_handle(Probe::new(cx)), cx),
937 None,
938 )
939 .child(
940 DockLayout::tabs().panel_view(panel_handle(Probe::new(cx)), cx),
941 None,
942 );
943 area.update(cx, |area, cx| area.set_center(layout, window, cx));
944 });
945 cx.run_until_parked();
946 log.borrow_mut().draggable.clear();
947 cx.update(|window, cx| window.draw(cx).clear(cx));
948
949 assert_eq!(
950 log.borrow().draggable,
951 vec![true, true],
952 "each group has somewhere to go, so both tabs start a drag"
953 );
954 }
955
956 struct ToolbarZoomProbe {
959 focus_handle: FocusHandle,
960 }
961
962 impl ToolbarZoomProbe {
963 fn new(cx: &mut App) -> Entity<Self> {
964 cx.new(|cx| Self {
965 focus_handle: cx.focus_handle(),
966 })
967 }
968 }
969
970 impl gpui_base::dock::Panel for ToolbarZoomProbe {
971 fn panel_name(&self) -> &'static str {
972 "ToolbarZoomProbe"
973 }
974 }
975
976 impl Panel for ToolbarZoomProbe {
977 fn zoom_control(&self, _: &App) -> Option<PanelControl> {
978 Some(PanelControl::Toolbar)
979 }
980 }
981
982 impl EventEmitter<PanelEvent> for ToolbarZoomProbe {}
983
984 impl Focusable for ToolbarZoomProbe {
985 fn focus_handle(&self, _: &App) -> FocusHandle {
986 self.focus_handle.clone()
987 }
988 }
989
990 impl Render for ToolbarZoomProbe {
991 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
992 Empty
993 }
994 }
995
996 struct NoControlProbe {
999 focus_handle: FocusHandle,
1000 }
1001
1002 impl NoControlProbe {
1003 fn new(cx: &mut App) -> Entity<Self> {
1004 cx.new(|cx| Self {
1005 focus_handle: cx.focus_handle(),
1006 })
1007 }
1008 }
1009
1010 impl gpui_base::dock::Panel for NoControlProbe {
1011 fn panel_name(&self) -> &'static str {
1012 "NoControlProbe"
1013 }
1014 }
1015
1016 impl Panel for NoControlProbe {
1017 fn zoom_control(&self, _: &App) -> Option<PanelControl> {
1018 None
1019 }
1020 }
1021
1022 impl EventEmitter<PanelEvent> for NoControlProbe {}
1023
1024 impl Focusable for NoControlProbe {
1025 fn focus_handle(&self, _: &App) -> FocusHandle {
1026 self.focus_handle.clone()
1027 }
1028 }
1029
1030 impl Render for NoControlProbe {
1031 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
1032 Empty
1033 }
1034 }
1035
1036 struct UnzoomableProbe {
1039 focus_handle: FocusHandle,
1040 }
1041
1042 impl gpui_base::dock::Panel for UnzoomableProbe {
1043 fn panel_name(&self) -> &'static str {
1044 "UnzoomableProbe"
1045 }
1046
1047 fn zoomable(&self, _: &App) -> bool {
1048 false
1049 }
1050 }
1051
1052 impl Panel for UnzoomableProbe {
1053 fn zoom_control(&self, _: &App) -> Option<PanelControl> {
1054 Some(PanelControl::Toolbar)
1055 }
1056 }
1057
1058 impl EventEmitter<PanelEvent> for UnzoomableProbe {}
1059
1060 impl Focusable for UnzoomableProbe {
1061 fn focus_handle(&self, _: &App) -> FocusHandle {
1062 self.focus_handle.clone()
1063 }
1064 }
1065
1066 impl Render for UnzoomableProbe {
1067 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
1068 Empty
1069 }
1070 }
1071
1072 fn drew_zoom_control(
1079 cx: &mut TestAppContext,
1080 panel: impl FnOnce(&mut App) -> Arc<dyn gpui_base::dock::PanelView>,
1081 ) -> bool {
1082 cx.update(|cx| {
1083 crate::init(cx);
1084 });
1085 let (area, cx) = cx.add_window_view(|window, cx| {
1086 let skin = DockSkin::new(cx);
1087 DockArea::new("skin", None, window, cx).with_renderer(skin)
1088 });
1089
1090 cx.update(|window, cx| {
1091 let layout = DockLayout::tabs().panel_view(panel(cx), cx);
1092 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1093 });
1094 cx.run_until_parked();
1095 cx.update(|window, cx| window.draw(cx).clear(cx));
1096 cx.debug_bounds(ZOOM_CONTROL_SELECTOR).is_some()
1097 }
1098
1099 #[gpui::test]
1103 fn a_panel_base_will_not_zoom_gets_no_zoom_control(cx: &mut TestAppContext) {
1104 let drew = drew_zoom_control(cx, |cx| {
1105 panel_handle(cx.new(|cx| UnzoomableProbe {
1106 focus_handle: cx.focus_handle(),
1107 }))
1108 });
1109
1110 assert!(
1111 !drew,
1112 "the panel names a place for the control, but base refuses the zoom"
1113 );
1114 }
1115
1116 #[gpui::test]
1120 fn a_zoomable_panel_gets_its_zoom_control(cx: &mut TestAppContext) {
1121 let drew = drew_zoom_control(cx, |cx| panel_handle(ToolbarZoomProbe::new(cx)));
1122
1123 assert!(
1124 drew,
1125 "a zoomable panel asking for a toolbar control gets one"
1126 );
1127 }
1128
1129 #[gpui::test]
1139 fn the_centre_and_the_bottom_dock_share_the_column(cx: &mut TestAppContext) {
1140 cx.update(|cx| {
1141 crate::init(cx);
1142 });
1143 let centre = Rc::new(Cell::new(px(0.)));
1144 let bottom = Rc::new(Cell::new(px(0.)));
1145 let (area, cx) = cx.add_window_view(|window, cx| {
1146 let skin = DockSkin::new(cx);
1147 DockArea::new("skin", None, window, cx).with_renderer(skin)
1148 });
1149
1150 let (centre_probe, bottom_probe) = (centre.clone(), bottom.clone());
1151 cx.update(|window, cx| {
1152 let centre_panel = MeasuredProbe::new(centre_probe, cx);
1153 let bottom_panel = MeasuredProbe::new(bottom_probe, cx);
1154 area.update(cx, |area, cx| {
1155 area.set_center(
1159 DockLayout::v_split().child(
1160 DockLayout::h_split().child(
1161 DockLayout::tabs().panel_view(panel_handle(centre_panel), cx),
1162 None,
1163 ),
1164 None,
1165 ),
1166 window,
1167 cx,
1168 );
1169 area.set_dock(
1170 DockPlacement::Bottom,
1171 DockLayout::tabs().panel_view(panel_handle(bottom_panel), cx),
1172 window,
1173 cx,
1174 );
1175 area.set_dock_size(DockPlacement::Bottom, px(200.), window, cx);
1176 });
1177 });
1178 cx.run_until_parked();
1179 cx.update(|window, cx| window.draw(cx).clear(cx));
1180
1181 let window_height = cx.update(|window, _| window.viewport_size().height);
1182 assert!(
1183 centre.get() > px(0.),
1184 "the centre panel must receive height; it got {:?}",
1185 centre.get()
1186 );
1187 assert!(
1188 bottom.get() > px(0.),
1189 "the bottom dock's panel must receive height; it got {:?}",
1190 bottom.get()
1191 );
1192 assert!(
1193 centre.get() < window_height - px(150.),
1194 "the centre must give the 200px bottom dock its share; the centre \
1195 got {:?} of {window_height:?}",
1196 centre.get()
1197 );
1198 }
1199
1200 #[gpui::test]
1215 fn a_split_fills_its_container_whichever_slots_are_hidden(cx: &mut TestAppContext) {
1216 cx.update(|cx| {
1217 crate::init(cx);
1218 });
1219 let heights: Vec<Rc<Cell<Pixels>>> = (0..3).map(|_| Rc::new(Cell::new(px(0.)))).collect();
1220 let (area, cx) = cx.add_window_view(|window, cx| {
1221 let skin = DockSkin::new(cx);
1222 DockArea::new("skin", None, window, cx).with_renderer(skin)
1223 });
1224
1225 let slots = heights.clone();
1226 let probes = cx.update(|window, cx| {
1227 let probes: Vec<_> = slots
1228 .iter()
1229 .map(|height| HideableProbe::new(height.clone(), cx))
1230 .collect();
1231 area.update(cx, |area, cx| {
1232 area.set_dock(
1233 DockPlacement::Right,
1234 probes.iter().zip([260., 320., 200.]).fold(
1235 DockLayout::v_split(),
1236 |split, (probe, size)| {
1237 split.child(
1238 DockLayout::tabs().panel_view(panel_handle(probe.clone()), cx),
1239 Some(px(size)),
1240 )
1241 },
1242 ),
1243 window,
1244 cx,
1245 );
1246 area.set_dock_size(DockPlacement::Right, px(380.), window, cx);
1247 });
1248 probes
1249 });
1250 cx.run_until_parked();
1251 let draw = |cx: &mut VisualTestContext| {
1252 cx.update(|window, _| window.refresh());
1253 cx.update(|window, cx| window.draw(cx).clear(cx));
1254 cx.update(|window, cx| window.draw(cx).clear(cx));
1255 };
1256 draw(cx);
1257
1258 let dock_height = cx.update(|window, _| window.viewport_size().height);
1259 let drawn: Pixels = heights.iter().map(|height| height.get()).sum();
1263 let bar = (dock_height - drawn) / 3.;
1264 assert!(
1265 bar > px(0.) && bar < px(60.),
1266 "the three slots fill the dock to begin with, one tab bar each; \
1267 that leaves {bar:?} per slot of {dock_height:?}"
1268 );
1269
1270 for hidden in 1..0b111u8 {
1273 let shown = (0..3).filter(|slot| hidden & (1 << slot) == 0);
1274 cx.update(|_, cx| {
1275 for (slot, probe) in probes.iter().enumerate() {
1276 heights[slot].set(px(-1.));
1279 probe.update(cx, |probe, cx| {
1280 probe.set_visible(hidden & (1 << slot) == 0, cx)
1281 });
1282 }
1283 });
1284 cx.run_until_parked();
1285 draw(cx);
1286
1287 let mut count = 0;
1288 let mut total = px(0.);
1289 for slot in shown {
1290 assert_ne!(
1291 heights[slot].get(),
1292 px(-1.),
1293 "hiding {hidden:03b}: slot {slot} is shown and must draw"
1294 );
1295 count += 1;
1296 total += heights[slot].get();
1297 }
1298 let empty = dock_height - total - bar * count as f32;
1299 assert!(
1300 empty.abs() < px(1.),
1301 "hiding {hidden:03b}: the drawn slots must take the hidden \
1302 ones' space between them; they left {empty:?} of \
1303 {dock_height:?} empty"
1304 );
1305 }
1306 }
1307
1308 #[gpui::test]
1312 fn the_zoom_action_reaches_the_group_through_the_skin(cx: &mut TestAppContext) {
1313 cx.update(|cx| {
1314 crate::init(cx);
1315 });
1316 let (area, cx) = cx.add_window_view(|window, cx| {
1317 let skin = DockSkin::new(cx);
1318 DockArea::new("skin", None, window, cx).with_renderer(skin)
1319 });
1320
1321 let panel = cx.update(|window, cx| {
1322 let panel = Probe::new(cx);
1323 let layout = DockLayout::tabs().panel_view(panel_handle(panel.clone()), cx);
1324 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1325 panel
1326 });
1327 cx.run_until_parked();
1328 cx.update(|window, cx| {
1329 panel.read(cx).focus_handle(cx).focus(window, cx);
1330 });
1331 cx.run_until_parked();
1332
1333 assert_eq!(cx.read(|cx| area.read(cx).is_zoomed()), false);
1334 cx.dispatch_action(ToggleZoom);
1335 cx.run_until_parked();
1336 assert_eq!(
1337 cx.read(|cx| area.read(cx).is_zoomed()),
1338 true,
1339 "the skin's frame is what carries the ToggleZoom handler"
1340 );
1341
1342 cx.dispatch_action(ToggleZoom);
1343 cx.run_until_parked();
1344 assert_eq!(cx.read(|cx| area.read(cx).is_zoomed()), false);
1345 }
1346
1347 #[gpui::test]
1353 fn the_zoom_action_refuses_a_panel_that_offers_no_control(cx: &mut TestAppContext) {
1354 cx.update(|cx| {
1355 crate::init(cx);
1356 });
1357 let (area, cx) = cx.add_window_view(|window, cx| {
1358 let skin = DockSkin::new(cx);
1359 DockArea::new("skin", None, window, cx).with_renderer(skin)
1360 });
1361
1362 let panel = cx.update(|window, cx| {
1363 let panel = NoControlProbe::new(cx);
1364 let layout = DockLayout::tabs().panel_view(panel_handle(panel.clone()), cx);
1365 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1366 panel
1367 });
1368 cx.run_until_parked();
1369 cx.update(|window, cx| {
1370 panel.read(cx).focus_handle(cx).focus(window, cx);
1371 });
1372 cx.run_until_parked();
1373
1374 cx.dispatch_action(ToggleZoom);
1375 cx.run_until_parked();
1376 assert_eq!(
1377 cx.read(|cx| area.read(cx).is_zoomed()),
1378 false,
1379 "no control means no zoom, however the zoom was asked for"
1380 );
1381 }
1382
1383 #[gpui::test]
1398 fn the_panel_content_region_gets_the_height_below_the_tab_bar(cx: &mut TestAppContext) {
1399 cx.update(|cx| {
1400 crate::init(cx);
1401 });
1402 let height = Rc::new(Cell::new(px(0.)));
1403 let (area, cx) = cx.add_window_view(|window, cx| {
1404 let skin = DockSkin::new(cx);
1405 DockArea::new("skin", None, window, cx).with_renderer(skin)
1406 });
1407
1408 let measured = height.clone();
1409 cx.update(|window, cx| {
1410 let panel = MeasuredProbe::new(measured, cx);
1411 let layout = DockLayout::tabs().panel_view(panel_handle(panel), cx);
1412 area.update(cx, |area, cx| area.set_center(layout, window, cx));
1413 });
1414 cx.run_until_parked();
1415 cx.update(|window, cx| window.draw(cx).clear(cx));
1416
1417 let window_height = cx.update(|window, _| window.viewport_size().height);
1418 let content = height.get();
1419 assert!(
1420 content > px(0.),
1421 "the panel must receive height; it got {content:?} in a {window_height:?} window"
1422 );
1423 assert!(
1426 content > window_height - px(60.),
1427 "the panel should fill what the tab bar leaves; it got {content:?} \
1428 of {window_height:?}"
1429 );
1430 }
1431
1432 #[gpui::test]
1439 fn a_collapsed_dock_ignores_the_zoom_action(cx: &mut TestAppContext) {
1440 cx.update(|cx| {
1441 crate::init(cx);
1442 });
1443 let (area, cx) = cx.add_window_view(|window, cx| {
1444 let skin = DockSkin::new(cx);
1445 DockArea::new("skin", None, window, cx).with_renderer(skin)
1446 });
1447
1448 let panel = cx.update(|window, cx| {
1449 let panel = Probe::new(cx);
1450 let layout = DockLayout::tabs().panel_view(panel_handle(panel.clone()), cx);
1451 area.update(cx, |area, cx| {
1452 area.set_dock(DockPlacement::Bottom, layout, window, cx);
1453 area.toggle_dock(DockPlacement::Bottom, window, cx);
1454 });
1455 panel
1456 });
1457 cx.run_until_parked();
1458 cx.update(|window, cx| {
1459 panel.read(cx).focus_handle(cx).focus(window, cx);
1460 });
1461 cx.run_until_parked();
1462
1463 cx.dispatch_action(ToggleZoom);
1464 cx.run_until_parked();
1465 assert_eq!(
1466 cx.read(|cx| area.read(cx).is_zoomed()),
1467 false,
1468 "a collapsed group installs no action handler"
1469 );
1470 }
1471}