Skip to main content

gpui_component/dock/
tab_panel.rs

1//! The gpui-component appearance for a tab group.
2//!
3//! `gpui_base::dock::TabGroup` owns the behavior — membership, the displayed
4//! tab, drag hit-testing, the zoom flag — and draws none of it. Everything
5//! visible is here: the tab bar, the toolbar, the ellipsis menu, the dock
6//! collapse affordances, the drop placeholder, and the styled drag preview.
7
8use 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
41/// Names the tab bar's zoom button in the debug-bounds map, so a test can ask
42/// a really-drawn frame whether the control was offered.
43const ZOOM_CONTROL_SELECTOR: &str = "dock-tab-bar-zoom-control";
44
45/// The size the styled drag preview occupies, reported to base so a drop
46/// placeholder knows where to fly in from.
47const DRAG_PREVIEW_SIZE: gpui::Size<gpui::Pixels> = gpui::size(px(96.), px(30.));
48
49/// Motion for the drop placeholder as it follows the drop that would land.
50///
51/// Critically damped, so the placeholder never reads as covering a region the
52/// drop would not take. The tolerance is coarse: this runs during a drag, where
53/// a frame spent on half a pixel competes with the drag itself.
54const PLACEHOLDER_SPRING: Spring = Spring::new(Duration::from_millis(200)).with_epsilon(0.5);
55
56/// The preview that follows the cursor while a panel is dragged.
57///
58/// `gpui_base::dock::DragPanel` is the payload and draws nothing; this is the
59/// appearance half, reintroduced here.
60pub 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
84/// A panel's title, or its registered name when it reached base without this
85/// crate's handle and so carries no presentation. See [`PanelHandle::of`].
86pub(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    /// Panels already warned about. `panel_title` sits on the render path, so
101    /// an unguarded warning would repeat at frame rate and bury the very
102    /// signal it exists to give.
103    ///
104    /// Keyed by panel rather than a bare `Once` so a second wrongly installed
105    /// panel is still named, and a runtime set rather than a `debug_assert!`
106    /// so a release build says it too — the consequence is a shipped app whose
107    /// tabs are titleless, which is exactly when someone needs to be told.
108    /// Thread-local because rendering happens on one thread, so no lock is
109    /// needed.
110    static WARNED_UNWRAPPED: RefCell<HashSet<PanelId>> = RefCell::new(HashSet::new());
111}
112
113/// Say once, per panel, that a panel reached the skin without its
114/// presentation handle. Silent otherwise, and visual-only: the panel docks,
115/// drags and persists, it just has no title. The shorter method is the wrong
116/// one — `DockLayout::panel` and `DockArea::add_panel` accept a
117/// `gpui_component::dock::Panel` and store the bare entity — so this says
118/// which panel and what to call instead.
119fn 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
133/// Where the zoom affordance goes for the group's displayed panel, or `None`
134/// when there is none to offer.
135///
136/// Two questions, and both have to be asked. [`Panel::zoom_control`] says
137/// *where* the control appears; [`gpui_base::dock::Panel::zoomable`] says
138/// whether zooming happens at all, and base refuses a zoom that fails it. The
139/// old dock had a single `zoomable() -> Option<PanelControl>` that could not
140/// disagree with itself; split across the seam it can, and a panel answering
141/// `zoomable() == false` with `zoom_control() == Some(Toolbar)` would
142/// otherwise draw a button that does nothing.
143fn 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
151/// The payload for dragging the tab at `ix` out of its group, or `None` when
152/// this group must not be rearranged.
153///
154/// The guard is the skin's. [`TabGroupContext::drag_panel`] answers for any
155/// tab in range whether or not the group may be rearranged, so a tab bar that
156/// forgets to ask [`TabGroupContext::is_draggable`] makes a group that has
157/// nowhere to go — a dock's last group, a locked dock — draggable anyway. The
158/// old dock asked the same question, spelled `state.draggable`.
159fn 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
166/// The left-most, top-most tab group in a container — where a left dock's
167/// collapse affordance goes. Mirrors the old `StackPanel::left_top_tab_panel`.
168fn 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
176/// The right-most, top-most tab group. A vertical split stacks its children,
177/// so its *first* child is the top one; a horizontal split's last child is the
178/// right-most. Mirrors the old `StackPanel::right_top_tab_panel`.
179fn 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
191/// One tab group's appearance.
192///
193/// Built per group — `DockAreaRenderer::tab_group_renderer` is called once per
194/// container — so the tab bar's scroll position belongs to the group whose
195/// tabs it scrolls.
196pub(crate) struct TabGroupSkin {
197    shared: Rc<SkinShared>,
198    scroll_handle: ScrollHandle,
199    /// The displayed tab the last frame drew, so a change scrolls the new tab
200    /// into view. The old dock recorded this at the moment of selection; the
201    /// group now owns selection, so the skin notices instead of being told.
202    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    /// Whether a dock's collapse affordance belongs in *this* group's tab bar,
215    /// and which way it points. `None` means this group draws none.
216    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        // A dock that does not exist is not collapsible, so this covers the
229        // old `left_dock.is_some()` test too.
230        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    /// The trailing controls: the panel's own buttons, the zoom affordance,
279    /// and the ellipsis menu.
280    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                            // Whether this button was drawn is the whole of
325                            // the `zoom_control` decision, and there is no
326                            // other way to ask a drawn tree about it. A no-op
327                            // outside test builds; see `debug_selector`.
328                            .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    /// The one-panel title bar: no tabs, just the title and the controls.
365    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    /// The full tab bar.
438    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        // Bring a newly displayed tab into view. The group owns selection now,
470        // so the skin notices the change rather than being told about it.
471        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 -1 for avoid border overlap with the first tab
485                        .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                            // A collapsed group shows no tab as active: the
512                            // strip is a way back in, not a selection. The
513                            // comparison is against the panel on screen, not
514                            // the stored index: a hidden displayed tab falls
515                            // back to the first visible one.
516                            .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                                    // Clicking the strip of a collapsed bottom
524                                    // dock is how it is opened again.
525                                    if is_bottom_dock && collapsed {
526                                        _ = area.update(cx, |area, cx| {
527                                            area.toggle_dock(DockPlacement::Bottom, window, cx)
528                                        });
529                                    }
530                                }
531                            })
532                            // A collapsed group is a strip of tabs with no
533                            // content, so there is nothing to rearrange in it.
534                            .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                // Empty space so a panel can be moved past the last tab.
586                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                                // A panel dropped past its own last tab lands
600                                // in the final slot; one from elsewhere is
601                                // appended in the background.
602                                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        // The column, the fill and the clip are base's now, applied around
649        // this. What is left is the background and the two actions.
650        div()
651            .id("tab-panel")
652            .bg(cx.theme().tokens.background)
653            // A collapsed group is a strip of tabs with no content, and the
654            // actions act on content. The old dock gated them the same way.
655            .when(!group.is_collapsed(), |this| {
656                this.on_action({
657                    let group = group.clone();
658                    move |_: &ToggleZoom, window, cx| {
659                        // The affordance decides the control, so a panel that
660                        // offers none is not zoomed *in* by the keybinding
661                        // either. Zooming out is never refused: a panel that
662                        // stopped offering the control while zoomed would
663                        // otherwise strand the user with no way back.
664                        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        // The fill and the collapsed-group exception are base's; the padding
696        // is this skin's, and is the only reason this hook is implemented.
697        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        // The placeholder chases the drop it would land in. Its rect was
751        // previously replayed from the drag source on every epoch, so crossing
752        // several drop zones in one drag restarted the walk at each one; the
753        // springs carry it through instead, and the element no longer needs an
754        // outer frame to hold the destination while an inner one walks to it.
755        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    /// Draws nothing, but runs the real gates the skin's tab bar runs and
838    /// records what they decided for every group it was asked to draw.
839    #[derive(Default)]
840    struct Recorded {
841        /// One entry per tab: whether its tab would start a drag.
842        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    /// The gate carried forward from the old `TabPanel::render`, which wrapped
907    /// `on_drag` in `.when(state.draggable, ..)`. Base does not enforce it:
908    /// `TabGroupContext::drag_panel` answers for any tab in range.
909    #[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    /// A panel that allows zooming and asks for the control in the toolbar.
957    /// `Probe`'s default is `PanelControl::Menu`, which draws no button.
958    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    /// A panel that offers no zoom control but leaves base's `zoomable`
997    /// default alone. Withholding the control is meant to be enough.
998    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    /// A panel that says "never zoom" in base's half but still names a place
1037    /// for the control in this crate's half.
1038    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    /// Draw one panel through the real [`DockSkin`] and report whether its tab
1073    /// bar offered a zoom control.
1074    ///
1075    /// The real skin, not a recorder: the bug this guards is `render_toolbar`
1076    /// asking only half the question, so a test that calls `zoom_control`
1077    /// itself would pass with the bug in place.
1078    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    /// The two halves of the old `zoomable()` can now disagree, and base is
1100    /// the one that decides. A control drawn against base's refusal is dead:
1101    /// pressing it does nothing.
1102    #[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    /// The other half: a panel that allows zoom and asks for a toolbar control
1117    /// gets one drawn. Without this the test above would also pass a skin that
1118    /// never draws a zoom control at all.
1119    #[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    /// The centre and the bottom dock share the centre column, and both get
1130    /// height.
1131    ///
1132    /// `DockSkin::center_frame` is a flex column holding the centre's root
1133    /// split and the bottom dock, and the centre's split frame sits between
1134    /// them. This pins that the frame carries a size at all: strip
1135    /// `split_frame` of both `size_full` and `flex_1` and every panel here
1136    /// measures zero. It does not pin *which* of the two does the work —
1137    /// either alone passes.
1138    #[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                // A split inside a split, so the *nested* `split_frame` — the
1156                // one that sits inside a `resizable_panel` — is exercised too,
1157                // not only the centre's root.
1158                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    /// Whichever slots of a split are hidden, the drawn ones fill it.
1201    ///
1202    /// `render_node` pins every slot but one to a fixed size and lets the
1203    /// remaining one absorb whatever the container has spare. Picking that
1204    /// slot by tree position alone picks a hidden one whenever the trailing
1205    /// container's panels are all hidden — nothing draws there, nothing
1206    /// grows, and the split stops short of its frame, showing a band of the
1207    /// frame's own background under the last visible panel. Hiding a slot is
1208    /// an everyday event: a panel that is only meaningful for some symbols
1209    /// answers `visible` with `false` for the rest.
1210    ///
1211    /// Every subset is covered because the defect is positional: only the
1212    /// cases that hide the trailing slot fail, and a test that hid one fixed
1213    /// slot would pass against a fix that only special-cased that slot.
1214    #[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        // Each slot spends a tab bar out of its height and the probe under it
1260        // measures the rest, so the drawn slots account for the whole dock
1261        // once one tab bar per drawn slot is added back.
1262        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        // Every subset except "all three hidden", which gives the whole node
1271        // up to *its* parent and so has no container of its own to fill.
1272        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                    // A sentinel, so a slot that stopped drawing is not read
1277                    // as one that kept the height it had.
1278                    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    /// The old dock installed `ToggleZoom` and `ClosePanel` on the tab panel
1309    /// itself; base installs neither, so the skin's `frame` is the only place
1310    /// the keybindings reach.
1311    #[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    /// Withholding the control withholds the whole affordance, keybinding
1348    /// included. The two halves of the zoom question are asked in one place —
1349    /// the skin's `frame` — so a doc claiming the action gets through anyway
1350    /// would send a panel author looking for a second switch that does not
1351    /// exist.
1352    #[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    /// The tab group's own frame has to be a flex column.
1384    ///
1385    /// gpui's default display is Block, and block layout ignores a child's
1386    /// `flex_grow`. With a plain `div()` frame the content region's `flex_1`
1387    /// does nothing, `#tab-content` sizes to its content, and its only child
1388    /// is the panel view positioned absolutely by `cached` — which
1389    /// contributes no content height. The whole chain resolves to zero and
1390    /// the dock draws a tab bar with nothing under it.
1391    ///
1392    /// This asserts a dimension, which the project's testing guidance
1393    /// discourages, because a zero-height content region is not a cosmetic
1394    /// difference: it is the panel not rendering at all, and no behavioral
1395    /// test in this crate can see it. `set_active` still fires, the layout
1396    /// still round-trips, and the window still opens.
1397    #[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        // The tab bar is 30px and the padded content region adds none for a
1424        // single tab, so the panel should get nearly the whole window.
1425        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    /// A collapsed group is a strip of tabs with no content, and the actions
1433    /// act on content. The old `TabPanel::bind_actions` gated them the same
1434    /// way.
1435    ///
1436    /// The bottom dock, not a side one: a closed left or right dock draws
1437    /// nothing at all, so it would pass this whether or not the gate exists.
1438    #[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}