//! The gpui-component appearance for a tab group. //! //! `gpui_base::dock::TabGroup` owns the behavior — membership, the displayed //! tab, drag hit-testing, the zoom flag — and draws none of it. Everything //! visible is here: the tab bar, the toolbar, the ellipsis menu, the dock //! collapse affordances, the drop placeholder, and the styled drag preview. use std::{ cell::{Cell, RefCell}, collections::HashSet, rc::Rc, sync::Arc, }; use gpui::{ Anchor, AnyElement, AnyView, App, AppContext as _, Context, Div, Empty, InteractiveElement as _, IntoElement, ParentElement as _, Render, ScrollHandle, SharedString, Stateful, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Window, div, prelude::FluentBuilder as _, px, }; use gpui_base::{ dock::{ AnyDrag, DockPlacement, DragPanel, DropIndicator, NodeId, PaneNode, PaneRef, PanelId, TabGroupContext, TabGroupRenderer, }, spring, }; use rust_i18n::t; use crate::{ ActiveTheme as _, IconName, Selectable as _, Sizable as _, button::{Button, ButtonCustomVariant, ButtonVariants as _}, dock::{ClosePanel, PanelControl, PanelHandle, PanelStyle, SkinShared, ToggleZoom}, h_flex, menu::DropdownMenu as _, tab::{Tab, TabBar}, }; /// Names the tab bar's zoom button in the debug-bounds map, so a test can ask /// a really-drawn frame whether the control was offered. const ZOOM_CONTROL_SELECTOR: &str = "dock-tab-bar-zoom-control"; /// Debug-bounds selector for a tab's close (X) button, for tests. const CLOSE_BUTTON_SELECTOR: &str = "dock-tab-close-button"; /// The size the styled drag preview occupies, reported to base so a drop /// placeholder knows where to fly in from. const DRAG_PREVIEW_SIZE: gpui::Size = gpui::size(px(96.), px(30.)); /// The preview that follows the cursor while a panel is dragged. /// /// `gpui_base::dock::DragPanel` is the payload and draws nothing; this is the /// appearance half, reintroduced here. pub struct DragPanelPreview { panel: Arc, } impl Render for DragPanelPreview { fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { div() .id("drag-panel") .cursor_grab() .py_1() .px_3() .w_24() .overflow_hidden() .whitespace_nowrap() .border_1() .border_color(cx.theme().border) .rounded(cx.theme().radius) .text_color(cx.theme().tab_foreground) .bg(cx.theme().tokens.tab_active) .opacity(0.75) .child(panel_title(&self.panel, window, cx)) } } /// A panel's title, or its registered name when it reached base without this /// crate's handle and so carries no presentation. See [`PanelHandle::of`]. pub(crate) fn panel_title( panel: &Arc, window: &mut Window, cx: &mut App, ) -> AnyElement { let Some(handle) = PanelHandle::of(panel) else { let name = panel.panel_name(cx); warn_unwrapped_once(panel.panel_id(cx), name); return SharedString::from(name).into_any_element(); }; handle.title(window, cx) } thread_local! { /// Panels already warned about. `panel_title` sits on the render path, so /// an unguarded warning would repeat at frame rate and bury the very /// signal it exists to give. /// /// Keyed by panel rather than a bare `Once` so a second wrongly installed /// panel is still named, and a runtime set rather than a `debug_assert!` /// so a release build says it too — the consequence is a shipped app whose /// tabs are titleless, which is exactly when someone needs to be told. /// Thread-local because rendering happens on one thread, so no lock is /// needed. static WARNED_UNWRAPPED: RefCell> = RefCell::new(HashSet::new()); } /// Say once, per panel, that a panel reached the skin without its /// presentation handle. Silent otherwise, and visual-only: the panel docks, /// drags and persists, it just has no title. The shorter method is the wrong /// one — `DockLayout::panel` and `DockArea::add_panel` accept a /// `gpui_component::dock::Panel` and store the bare entity — so this says /// which panel and what to call instead. fn warn_unwrapped_once(panel: PanelId, name: &'static str) { if !WARNED_UNWRAPPED.with(|warned| warned.borrow_mut().insert(panel)) { return; } tracing::warn!( panel = name, "dock panel reached the skin without its presentation handle, so it \ draws its panel name instead of its title; install it with \ `gpui_component::dock::panel_handle(..)` and `DockLayout::panel_view` \ / `DockArea::add_panel_view` rather than `DockLayout::panel` / \ `DockArea::add_panel`" ); } /// Where the zoom affordance goes for the group's displayed panel, or `None` /// when there is none to offer. /// /// Two questions, and both have to be asked. [`Panel::zoom_control`] says /// *where* the control appears; [`gpui_base::dock::Panel::zoomable`] says /// whether zooming happens at all, and base refuses a zoom that fails it. The /// old dock had a single `zoomable() -> Option` that could not /// disagree with itself; split across the seam it can, and a panel answering /// `zoomable() == false` with `zoom_control() == Some(Toolbar)` would /// otherwise draw a button that does nothing. fn zoom_control(group: &TabGroupContext, cx: &App) -> Option { let panel = group.active_panel()?; panel .zoomable(cx) .then(|| PanelHandle::of(panel).and_then(|handle| handle.zoom_control(cx))) .flatten() } /// The payload for dragging the tab at `ix` out of its group, or `None` when /// this group must not be rearranged. /// /// The guard is the skin's. [`TabGroupContext::drag_panel`] answers for any /// tab in range whether or not the group may be rearranged, so a tab bar that /// forgets to ask [`TabGroupContext::is_draggable`] makes a group that has /// nowhere to go — a dock's last group, a locked dock — draggable anyway. The /// old dock asked the same question, spelled `state.draggable`. fn tab_drag(group: &TabGroupContext, ix: usize, cx: &App) -> Option { group .is_draggable() .then(|| group.drag_panel(ix, cx)) .flatten() } /// The left-most, top-most tab group in a container — where a left dock's /// collapse affordance goes. Mirrors the old `StackPanel::left_top_tab_panel`. fn left_top_group(node: &PaneNode) -> Option { match node.kind() { PaneRef::Tabs { .. } => Some(node.id()), PaneRef::Split { children, .. } => children.first().and_then(left_top_group), } } /// The right-most, top-most tab group. A vertical split stacks its children, /// so its *first* child is the top one; a horizontal split's last child is the /// right-most. Mirrors the old `StackPanel::right_top_tab_panel`. fn right_top_group(node: &PaneNode) -> Option { match node.kind() { PaneRef::Tabs { .. } => Some(node.id()), PaneRef::Split { axis, children, .. } => match axis { gpui::Axis::Vertical => children.first(), gpui::Axis::Horizontal => children.last(), } .and_then(right_top_group), } } /// One tab group's appearance. /// /// Built per group — `DockAreaRenderer::tab_group_renderer` is called once per /// container — so the tab bar's scroll position belongs to the group whose /// tabs it scrolls. pub(crate) struct TabGroupSkin { shared: Rc, scroll_handle: ScrollHandle, /// The displayed tab the last frame drew, so a change scrolls the new tab /// into view. The old dock recorded this at the moment of selection; the /// group now owns selection, so the skin notices instead of being told. last_active_ix: Cell>, } impl TabGroupSkin { pub(crate) fn new(shared: Rc) -> Self { Self { shared, scroll_handle: ScrollHandle::default(), last_active_ix: Cell::new(None), } } /// Whether a dock's collapse affordance belongs in *this* group's tab bar, /// and which way it points. `None` means this group draws none. fn dock_toggle_button( &self, placement: DockPlacement, group: &TabGroupContext, cx: &mut App, ) -> Option