diff --git a/crates/layout/Cargo.toml b/crates/layout/Cargo.toml index 3b33963..1cd1414 100644 --- a/crates/layout/Cargo.toml +++ b/crates/layout/Cargo.toml @@ -19,6 +19,7 @@ gpui = { workspace = true, features = ["test-support"] } default = ["full"] full = ["dock", "resize_handle", "resizable", "shell", "window_controls", "toast", "alert"] dock = [ + "shell", "resize_handle", "dep:gpui-native-core", "dep:gpui-native-data", diff --git a/crates/layout/src/dock.rs b/crates/layout/src/dock.rs index 47efe6f..ca30a0c 100644 --- a/crates/layout/src/dock.rs +++ b/crates/layout/src/dock.rs @@ -19,6 +19,7 @@ use gpui_native_icons::{Icon, IconRole}; use gpui_native_theme::{ActiveTheme as _, Size}; use crate::resize_handle::ResizeHandle; +use crate::shell::{LayoutState, Side, SidebarToggles, sidebar_toggle_button}; use crate::window_controls::{TRAFFIC_LIGHT_PAD, WindowControls, titlebar_strip}; /// The narrowest either side of a split can get, as a fraction of the split. @@ -779,6 +780,8 @@ pub struct Dock { window_controls: bool, /// The pill bar leads the window: pad for the macOS traffic lights. traffic_light_pad: bool, + /// The layout whose sidebar toggles render in the pill bar. + sidebar_toggles: Option>, } impl Dock { @@ -792,6 +795,7 @@ impl Dock { state, window_controls: false, traffic_light_pad: false, + sidebar_toggles: None, } } @@ -806,6 +810,12 @@ impl Dock { self.traffic_light_pad = traffic_light_pad; self } + + /// Renders the sidebar toggles in the pill bar. + pub fn sidebar_toggles(mut self, state: Entity) -> Self { + self.sidebar_toggles = Some(state); + self + } } impl RenderOnce for Dock { @@ -815,6 +825,7 @@ impl RenderOnce for Dock { window_controls: self.window_controls, traffic_light_pad: self.traffic_light_pad, height: cx.theme().titlebar_height, + toggles: self.sidebar_toggles, }; let root = render_node(&self.id, &self.state, &tree, &titlebar, window, cx); @@ -833,11 +844,13 @@ impl RenderOnce for Dock { } /// Which titlebar duties the pane's pill bar carries. -#[derive(Clone, Copy, Default)] +#[derive(Clone, Default)] struct Titlebar { window_controls: bool, traffic_light_pad: bool, height: Pixels, + /// The layout whose sidebar toggles lead and trail the tab bar. + toggles: Option>, } /// The right sidebar content of the dock's active panel. @@ -1017,7 +1030,7 @@ fn render_pane( let dock_id = dock_id.clone(); let state = state.clone(); let pane = pane.clone(); - let titlebar = *titlebar; + let titlebar = titlebar.clone(); let padding_x = metrics.dense_padding_x; move |window, cx| { @@ -1092,6 +1105,32 @@ fn render_pane( let bar_id = SharedString::from(format!("{dock_id}-pane-{}-bar", pane.key)); let strip_id = SharedString::from(format!("{dock_id}-pane-{}-strip", pane.key)); + let tabs_scroll_id = + SharedString::from(format!("{dock_id}-pane-{}-tabs-scroll", pane.key)); + let tabs_fit_id = SharedString::from(format!("{dock_id}-pane-{}-tabs-fit", pane.key)); + + let toggles = titlebar + .toggles + .clone() + .filter(|state| state.read(cx).sidebar_toggles() == SidebarToggles::Tabbar); + + let toggle_left = toggles.as_ref().map(|state| { + sidebar_toggle_button( + SharedString::from(format!("{bar_id}-toggle-left")), + Side::Left, + state, + cx, + ) + }); + + let toggle_right = toggles.as_ref().map(|state| { + sidebar_toggle_button( + SharedString::from(format!("{bar_id}-toggle-right")), + Side::Right, + state, + cx, + ) + }); h_flex() .id(bar_id.clone()) @@ -1106,13 +1145,33 @@ fn render_pane( padding_x }) .pr(padding_x) - .child(tabs) + .when_some(toggle_left, |bar, button| { + bar.child(div().flex_none().mr_1().child(button)) + }) + .child( + div() + .id(tabs_scroll_id.clone()) + .debug_selector(move || tabs_scroll_id.to_string()) + .flex() + .min_w_0() + .overflow_x_scroll() + .child( + div() + .id(tabs_fit_id.clone()) + .debug_selector(move || tabs_fit_id.to_string()) + .flex_none() + .child(tabs), + ), + ) .child(titlebar_strip( div().id(strip_id.clone()).flex_1().self_stretch(), strip_id, window, cx, )) + .when_some(toggle_right, |bar, button| { + bar.child(div().flex_none().ml_1().child(button)) + }) .when(titlebar.window_controls, |bar| { bar.child(WindowControls::new(SharedString::from(format!( "{dock_id}-pane-{}-controls", diff --git a/crates/layout/src/shell.rs b/crates/layout/src/shell.rs index cf8ac5f..1c5f5b3 100644 --- a/crates/layout/src/shell.rs +++ b/crates/layout/src/shell.rs @@ -4,7 +4,8 @@ use gpui::prelude::FluentBuilder as _; use gpui::{ AnyElement, App, AppContext as _, Context, ElementId, Entity, InteractiveElement as _, - IntoElement, ParentElement as _, Pixels, RenderOnce, Styled as _, Window, div, px, + IntoElement, ParentElement as _, Pixels, RenderOnce, SharedString, Styled as _, Window, div, + px, }; #[cfg(feature = "alert")] use gpui_native_core::alert::{Alert, Alerts, AlertsView}; @@ -22,12 +23,25 @@ use crate::resize_handle::ResizeHandle; use crate::window_controls::{TRAFFIC_LIGHT_PAD, WindowControls, titlebar_strip}; use crate::window_frame::frame; +/// Where the sidebar show/hide toggles live. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] +pub enum SidebarToggles { + /// No toggles. + Hidden, + /// In each sidebar's header strip. + Strips, + #[default] + /// In the center dock's tab bar. + Tabbar, +} + /// The layout the application owns. pub struct LayoutState { left_visible: bool, right_visible: bool, left_width: Pixels, right_width: Pixels, + sidebar_toggles: SidebarToggles, #[cfg(feature = "toast")] toasts: Entity, #[cfg(feature = "alert")] @@ -42,6 +56,7 @@ impl LayoutState { right_visible: false, left_width: SIDEBAR_DEFAULT_WIDTH, right_width: SIDEBAR_DEFAULT_WIDTH, + sidebar_toggles: SidebarToggles::default(), #[cfg(feature = "toast")] toasts: _cx.new(|_| Toasts::default()), #[cfg(feature = "alert")] @@ -65,6 +80,19 @@ impl LayoutState { } } + /// Sets where the sidebar show/hide toggles live. + pub fn set_sidebar_toggles(&mut self, toggles: SidebarToggles, cx: &mut Context) { + if self.sidebar_toggles != toggles { + self.sidebar_toggles = toggles; + cx.notify(); + } + } + + /// Where the sidebar show/hide toggles live. + pub fn sidebar_toggles(&self) -> SidebarToggles { + self.sidebar_toggles + } + /// Whether the left sidebar is visible. pub fn is_left_sidebar_visible(&self) -> bool { self.left_visible @@ -142,7 +170,7 @@ impl LayoutState { /// Which sidebar a region or toggle belongs to. #[derive(Clone, Copy, PartialEq, Eq)] -enum Side { +pub(crate) enum Side { Left, Right, } @@ -223,6 +251,8 @@ impl RenderOnce for Shell { let left_visible = state.read(cx).is_left_sidebar_visible(); let right_visible = state.read(cx).is_right_sidebar_visible(); + let strip_toggles = state.read(cx).sidebar_toggles() == SidebarToggles::Strips; + let left_material = left .as_ref() .is_some_and(|sidebar| sidebar.is_transparent()); @@ -248,6 +278,7 @@ impl RenderOnce for Shell { material: left_material, height: titlebar_height, padding_x: metrics.dense_padding_x, + toggle: strip_toggles, }, window, cx, @@ -287,6 +318,7 @@ impl RenderOnce for Shell { material: false, height: titlebar_height, padding_x: metrics.dense_padding_x, + toggle: strip_toggles, }, window, cx, @@ -294,18 +326,67 @@ impl RenderOnce for Shell { }); // Wraps the layout in a window frame - frame(layout, window, cx) - .when(cfg!(feature = "toast"), |this| { - let toasts = state.read(cx).toasts.clone(); - this.child(ToastsView::new((id.clone(), "toasts"), toasts)) - }) - .when(cfg!(feature = "alert"), |this| { - let alerts = state.read(cx).alerts.clone(); - this.child(AlertsView::new((id, "alerts"), alerts)) - }) + let frame = frame(layout, window, cx); + + #[cfg(feature = "toast")] + let frame = frame.child(ToastsView::new( + (id.clone(), "toasts"), + state.read(cx).toasts.clone(), + )); + + #[cfg(feature = "alert")] + let frame = frame.child(AlertsView::new( + (id, "alerts"), + state.read(cx).alerts.clone(), + )); + + frame } } +/// One sidebar show/hide toggle. +pub(crate) fn sidebar_toggle_button( + id: I, + side: Side, + state: &Entity, + cx: &App, +) -> Button +where + I: Into, +{ + let name = side.name(); + + let visible = match side { + Side::Left => state.read(cx).is_left_sidebar_visible(), + Side::Right => state.read(cx).is_right_sidebar_visible(), + }; + + Button::new(id) + .icon(match side { + Side::Left => IconRole::PanelLeft, + Side::Right => IconRole::PanelRight, + }) + .variant(ButtonVariant::Ghost) + .small() + .accessibility_label(SharedString::from(format!( + "{} {} sidebar", + if visible { "Hide" } else { "Show" }, + name, + ))) + .debug_selector(move || format!("sidebar-toggle-{}", name)) + .on_click({ + let state = state.clone(); + move |_, _, cx| match side { + Side::Left => state.update(cx, |state, cx| { + state.set_left_sidebar_visible(!state.is_left_sidebar_visible(), cx) + }), + Side::Right => state.update(cx, |state, cx| { + state.set_right_sidebar_visible(!state.is_right_sidebar_visible(), cx) + }), + } + }) +} + /// Which titlebar duties a sidebar's header strip carries. #[derive(Clone, Copy)] struct Titlebar { @@ -313,6 +394,8 @@ struct Titlebar { material: bool, height: Pixels, padding_x: Pixels, + /// Whether the strip carries the sidebar's hide toggle. + toggle: bool, } /// One sidebar: a header strip with the hide toggle over the app's content. @@ -357,19 +440,14 @@ where cx, )) .map(|strip| match titlebar.side { - Side::Left => strip.child( - Button::new((id, "sidebar-toggle")) - .icon(IconRole::PanelLeft) - .variant(ButtonVariant::Ghost) - .small() - .accessibility_label("Hide left sidebar") - .on_click({ - let state = state.clone(); - move |_, _, cx| { - state.update(cx, |state, cx| state.set_left_sidebar_visible(false, cx)) - } - }), - ), + Side::Left => strip.when(titlebar.toggle, |strip| { + strip.child(sidebar_toggle_button( + (id, "sidebar-toggle"), + Side::Left, + state, + cx, + )) + }), Side::Right => strip.child(WindowControls::new((id, "window-controls"))), }); diff --git a/crates/layout/tests/shell.rs b/crates/layout/tests/shell.rs index 3a595b7..b0e3f4e 100644 --- a/crates/layout/tests/shell.rs +++ b/crates/layout/tests/shell.rs @@ -6,8 +6,8 @@ use gpui::{ VisualTestContext, Window, div, point, px, }; use gpui_native_data::sidebar::Sidebar; -use gpui_native_layout::shell::{LayoutState, Shell}; -use gpui_native_theme::{ActiveTheme as _, Size, Theme}; +use gpui_native_layout::shell::{LayoutState, Shell, SidebarToggles}; +use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Size, Theme}; struct Harness { state: Entity, @@ -95,6 +95,11 @@ fn left_sidebar_starts_visible_and_right_hidden() { assert!(state.read_with(&cx, |state, _| state.is_left_sidebar_visible())); assert!(!state.read_with(&cx, |state, _| state.is_right_sidebar_visible())); + assert_eq!( + state.read_with(&cx, |state, _| state.sidebar_toggles()), + SidebarToggles::Tabbar, + "the toggles default to the dock's tab bar" + ); assert!( bounds(&mut visual, "sidebar-test-left").is_some(), @@ -144,6 +149,14 @@ fn the_header_toggle_hides_its_sidebar() { let mut cx = TestAppContext::single(); let (state, mut visual) = layout_window(&mut cx); + // The strip carries the toggle only under the strips placement. + cx.update(|cx| { + state.update(cx, |state, cx| { + state.set_sidebar_toggles(SidebarToggles::Strips, cx) + }); + }); + redraw(&mut visual); + // The toggle sits at the trailing end of the sidebar's header strip, an // icon-only small button: derive its center from the theme metrics. let metrics = cx.update(|cx| cx.theme().sizes.resolve(Size::default())); @@ -322,3 +335,281 @@ mod alerts { ); } } + +/// Toggles placed in the dock's tab bar need both the shell and the dock. +#[cfg(all(feature = "shell", feature = "dock"))] +mod tabbar_toggles { + use super::*; + use gpui::{AppContext as _, ScrollDelta, ScrollEvent}; + use gpui_native_layout::dock::{Dock, DockState, PanelDescriptor}; + use gpui_native_layout::shell::SidebarToggles; + + struct DockPanel; + + impl Render for DockPanel { + fn render( + &mut self, + _window: &mut Window, + _cx: &mut gpui::Context, + ) -> impl gpui::IntoElement { + div().size_full() + } + } + + struct DockHarness { + state: Entity, + dock: Entity, + } + + impl Render for DockHarness { + fn render( + &mut self, + _window: &mut Window, + _cx: &mut gpui::Context, + ) -> impl gpui::IntoElement { + div().size_full().child( + Shell::new("test-layout", self.state.clone()) + .left_sidebar(Sidebar::new("test-left")) + .center( + Dock::new("test-dock", self.dock.clone()) + .sidebar_toggles(self.state.clone()), + ) + .right_sidebar( + div() + .size_full() + .debug_selector(|| "test-right".to_string()), + ), + ) + } + } + + fn tabbar_window( + cx: &mut TestAppContext, + open: &[&str], + ) -> (Entity, VisualTestContext) { + cx.update(Theme::init); + let state = cx.update(LayoutState::new); + let dock = cx.update(|cx| { + let dock = DockState::new(cx); + dock.update(cx, |dock, _| { + dock.register_panel(PanelDescriptor::new("one", "One", |_, cx| { + cx.new(|_| DockPanel).into() + })); + for index in 0..14 { + let tag = format!("panel-{index}"); + let id = tag.clone(); + dock.register_panel(PanelDescriptor::new(id, tag, |_, cx| { + cx.new(|_| DockPanel).into() + })); + } + }); + dock + }); + let state_for_window = state.clone(); + let dock_for_window = dock.clone(); + let window = cx.add_window(move |_, _| DockHarness { + state: state_for_window, + dock: dock_for_window, + }); + let mut visual = VisualTestContext::from_window(window.into(), cx); + visual.update(|window, cx| { + for id in open { + dock.update(cx, |dock, cx| dock.open_panel(id, window, cx)); + } + }); + redraw(&mut visual); + (state, visual) + } + + fn place_toggles_in_tabbar(state: &Entity, cx: &mut TestAppContext) { + cx.update(|cx| { + state.update(cx, |state, cx| { + state.set_sidebar_toggles(SidebarToggles::Tabbar, cx) + }); + }); + } + + #[test] + fn tabbar_toggles_sit_at_the_bar_edges() { + let mut cx = TestAppContext::single(); + let (state, mut visual) = tabbar_window(&mut cx, &["one"]); + place_toggles_in_tabbar(&state, &mut cx); + redraw(&mut visual); + + let metrics = cx.update(|cx| cx.theme().sizes.resolve(Size::default())); + let small = cx.update(|cx| cx.theme().sizes.resolve(Size::Small)); + let bar = bounds(&mut visual, "test-dock-pane-0-bar").expect("tab bar missing"); + let left = bounds(&mut visual, "sidebar-toggle-left").expect("left toggle missing"); + let right = bounds(&mut visual, "sidebar-toggle-right").expect("right toggle missing"); + let scroll = bounds(&mut visual, "test-dock-pane-0-tabs-scroll") + .expect("tab scroll container missing"); + + // The left toggle leads the bar after its leading pad; the right + // toggle trails it at the trailing pad. The button's outer width is + // its borders plus dense padding around the icon. + let toggle_width = BORDER_WIDTH * 2.0 + small.dense_padding_x * 2.0 + small.icon_size; + let left_center = bar.left() + metrics.dense_padding_x + toggle_width / 2.0; + let right_center = bar.right() - metrics.dense_padding_x - toggle_width / 2.0; + assert!( + (left.center().x - left_center).abs() < px(1.0), + "the left toggle must lead the tab bar" + ); + assert!( + (right.center().x - right_center).abs() < px(1.0), + "the right toggle must trail the tab bar" + ); + assert!( + left.center().y > bar.top() && left.center().y < bar.bottom(), + "the toggles must sit inside the tab bar" + ); + let gap = scroll.left() - left.right(); + assert!( + (gap - px(4.0)).abs() < px(0.5), + "the toggle and the tabs must sit one tab gap apart" + ); + } + + #[test] + fn an_overflowing_tab_bar_keeps_the_trailing_toggle_pinned() { + let mut cx = TestAppContext::single(); + let open = [ + "one", "panel-0", "panel-1", "panel-2", "panel-3", "panel-4", "panel-5", "panel-6", + "panel-7", "panel-8", "panel-9", "panel-10", "panel-11", "panel-12", "panel-13", + ]; + let (state, mut visual) = tabbar_window(&mut cx, &open); + place_toggles_in_tabbar(&state, &mut cx); + redraw(&mut visual); + + let metrics = cx.update(|cx| cx.theme().sizes.resolve(Size::default())); + let small = cx.update(|cx| cx.theme().sizes.resolve(Size::Small)); + let bar = bounds(&mut visual, "test-dock-pane-0-bar").expect("tab bar missing"); + let left = bounds(&mut visual, "sidebar-toggle-left").expect("left toggle missing"); + let right = bounds(&mut visual, "sidebar-toggle-right").expect("right toggle missing"); + let scroll = bounds(&mut visual, "test-dock-pane-0-tabs-scroll") + .expect("tab scroll container missing"); + + // The tabs outgrow the bar: the drag strip collapses, the scroll + // viewport spans between the toggles, and the trailing toggle stays + // pinned inside the bar instead of being pushed out of the window. + let leading_gap = scroll.left() - left.right(); + assert!((leading_gap - px(4.0)).abs() < px(0.5)); + let trailing_gap = right.left() - scroll.right(); + assert!( + (trailing_gap - px(4.0)).abs() < px(0.5), + "the viewport must reach the trailing toggle" + ); + assert!( + right.right() <= bar.right() + px(1.0), + "the trailing toggle must stay inside the bar" + ); + let toggle_width = BORDER_WIDTH * 2.0 + small.dense_padding_x * 2.0 + small.icon_size; + let right_center = bar.right() - metrics.dense_padding_x - toggle_width / 2.0; + assert!( + (right.center().x - right_center).abs() < px(1.0), + "the trailing toggle must stay at the bar's trailing pad" + ); + } + + #[test] + fn an_overflowing_tab_bar_scrolls_with_the_wheel() { + let mut cx = TestAppContext::single(); + let open = [ + "one", "panel-0", "panel-1", "panel-2", "panel-3", "panel-4", "panel-5", "panel-6", + "panel-7", "panel-8", "panel-9", "panel-10", "panel-11", "panel-12", "panel-13", + ]; + let (state, mut visual) = tabbar_window(&mut cx, &open); + place_toggles_in_tabbar(&state, &mut cx); + redraw(&mut visual); + + let scroll = bounds(&mut visual, "test-dock-pane-0-tabs-scroll") + .expect("tab scroll container missing"); + let fit = bounds(&mut visual, "test-dock-pane-0-tabs-fit").expect("tab strip missing"); + // The pills row is wider than the viewport, so the bar can scroll. + assert!( + fit.size.width > scroll.size.width + px(1.0), + "the tab strip must overflow the bar" + ); + + // A plain vertical wheel over the bar scrolls the strip toward its + // end: the bar only scrolls on the x axis, so gpui remaps the delta. + visual.simulate_event(ScrollEvent { + position: scroll.center(), + delta: ScrollDelta::Pixels(point(px(0.), px(-60.))), + modifiers: Modifiers::default(), + ..Default::default() + }); + redraw(&mut visual); + + let fit_after = + bounds(&mut visual, "test-dock-pane-0-tabs-fit").expect("tab strip missing"); + assert!( + fit_after.left() < fit.left() - px(10.0), + "the wheel must scroll the tab strip" + ); + } + + #[test] + fn the_tabbar_toggle_shows_and_hides_the_left_sidebar() { + let mut cx = TestAppContext::single(); + let (state, mut visual) = tabbar_window(&mut cx, &["one"]); + place_toggles_in_tabbar(&state, &mut cx); + redraw(&mut visual); + + let toggle = bounds(&mut visual, "sidebar-toggle-left").expect("left toggle missing"); + press(&mut visual, center_of(toggle), 1); + redraw(&mut visual); + assert!(!state.read_with(&cx, |state, _| state.is_left_sidebar_visible())); + assert!(bounds(&mut visual, "sidebar-test-left").is_none()); + + // The toggle outlives its hidden sidebar and brings it back. + let toggle = bounds(&mut visual, "sidebar-toggle-left") + .expect("left toggle missing while the sidebar is hidden"); + press(&mut visual, center_of(toggle), 1); + redraw(&mut visual); + assert!(state.read_with(&cx, |state, _| state.is_left_sidebar_visible())); + assert!(bounds(&mut visual, "sidebar-test-left").is_some()); + } + + #[test] + fn the_tabbar_right_toggle_opens_the_right_sidebar() { + let mut cx = TestAppContext::single(); + let (state, mut visual) = tabbar_window(&mut cx, &["one"]); + place_toggles_in_tabbar(&state, &mut cx); + redraw(&mut visual); + + assert!(bounds(&mut visual, "test-right").is_none()); + let toggle = bounds(&mut visual, "sidebar-toggle-right").expect("right toggle missing"); + press(&mut visual, center_of(toggle), 1); + redraw(&mut visual); + assert!(state.read_with(&cx, |state, _| state.is_right_sidebar_visible())); + assert!(bounds(&mut visual, "test-right").is_some()); + + let toggle = bounds(&mut visual, "sidebar-toggle-right").expect("right toggle missing"); + press(&mut visual, center_of(toggle), 1); + redraw(&mut visual); + assert!(!state.read_with(&cx, |state, _| state.is_right_sidebar_visible())); + assert!(bounds(&mut visual, "test-right").is_none()); + } + + #[test] + fn hidden_placement_renders_no_toggles() { + let mut cx = TestAppContext::single(); + let (state, mut visual) = layout_window(&mut cx); + + cx.update(|cx| { + state.update(cx, |state, cx| { + state.set_sidebar_toggles(SidebarToggles::Hidden, cx) + }); + }); + redraw(&mut visual); + + assert!( + bounds(&mut visual, "sidebar-toggle-left").is_none(), + "the strip must not carry a toggle when they are hidden" + ); + assert!( + bounds(&mut visual, "sidebar-test-left").is_some(), + "hiding the toggles must not hide the sidebar itself" + ); + } +} diff --git a/examples/gallery/src/main.rs b/examples/gallery/src/main.rs index a34b6fe..ec72b40 100644 --- a/examples/gallery/src/main.rs +++ b/examples/gallery/src/main.rs @@ -283,10 +283,10 @@ impl Gallery { }); let center = Dock::new("gallery-dock", self.dock_state.clone()) - // The pill bar is the titlebar strip whenever no right sidebar - // sits to its right. + // The pill bar is the titlebar strip whenever no right sidebar sits to its right. .window_controls(!self.layout_state.read(cx).is_right_sidebar_visible()) - .traffic_light_pad(!self.layout_state.read(cx).is_left_sidebar_visible()); + .traffic_light_pad(!self.layout_state.read(cx).is_left_sidebar_visible()) + .sidebar_toggles(self.layout_state.clone()); let right_sidebar = DockSidebar::new("gallery-dock-sidebar", self.dock_state.clone());