diff --git a/crates/layout/Cargo.toml b/crates/layout/Cargo.toml index 1cd1414..5011718 100644 --- a/crates/layout/Cargo.toml +++ b/crates/layout/Cargo.toml @@ -17,7 +17,7 @@ gpui = { workspace = true, features = ["test-support"] } [features] default = ["full"] -full = ["dock", "resize_handle", "resizable", "shell", "window_controls", "toast", "alert"] +full = ["dock", "resize_handle", "resizable", "shell", "titlebar", "window_controls", "toast", "alert"] dock = [ "shell", "resize_handle", @@ -32,12 +32,14 @@ resize_handle = [] resizable = ["resize_handle"] shell = [ "resize_handle", + "titlebar", "window_controls", "dep:gpui-native-core", "gpui-native-core/button", "dep:gpui-native-data", "dep:gpui-native-icons", ] +titlebar = ["window_controls"] toast = ["shell", "gpui-native-core/toast"] alert = ["shell", "gpui-native-core/alert"] window_controls = ["dep:gpui-native-icons"] diff --git a/crates/layout/src/lib.rs b/crates/layout/src/lib.rs index fbbf9e2..1505b35 100644 --- a/crates/layout/src/lib.rs +++ b/crates/layout/src/lib.rs @@ -12,6 +12,9 @@ pub mod resizable; #[cfg(feature = "shell")] pub mod shell; +#[cfg(feature = "titlebar")] +pub mod titlebar; + #[cfg(feature = "window_controls")] pub mod window_controls; diff --git a/crates/layout/src/shell.rs b/crates/layout/src/shell.rs index 0fe182e..95dd905 100644 --- a/crates/layout/src/shell.rs +++ b/crates/layout/src/shell.rs @@ -1,5 +1,6 @@ -//! The application shell: a left sidebar (on by default), a right sidebar -//! (off by default), and the center region that hosts the dock. +//! The application shell: an optional titlebar above a left sidebar (on by +//! default), a right sidebar (off by default), and the center region that +//! hosts the dock. use gpui::prelude::FluentBuilder as _; use gpui::{ @@ -20,6 +21,7 @@ use gpui_native_theme::{ }; use crate::resize_handle::ResizeHandle; +use crate::titlebar::Titlebar; use crate::window_controls::{TRAFFIC_LIGHT_PAD, WindowControls, titlebar_strip}; use crate::window_frame::frame; @@ -173,6 +175,8 @@ impl Side { pub struct Shell { id: ElementId, state: Entity, + /// The titlebar drawn above the regions, replacing the strips. + titlebar: Option, left_sidebar: Option, right_sidebar: Option, center: Option, @@ -189,6 +193,7 @@ impl Shell { Self { id: id.into(), state, + titlebar: None, left_sidebar: None, right_sidebar: None, center: None, @@ -202,6 +207,12 @@ impl Shell { self } + /// Sets the titlebar drawn above the regions, disabling the sidebar strips. + pub fn titlebar(mut self, titlebar: Titlebar) -> Self { + self.titlebar = Some(titlebar); + self + } + /// Sets the left sidebar's navigation list. pub fn left_sidebar(mut self, sidebar: Sidebar) -> Self { self.left_sidebar = Some(sidebar); @@ -236,6 +247,7 @@ impl RenderOnce for Shell { let id = self.id.clone(); let state = self.state; + let titlebar = self.titlebar; let mut left = self.left_sidebar; let mut right = self.right_sidebar; @@ -248,50 +260,63 @@ impl RenderOnce for Shell { .as_ref() .is_some_and(|sidebar| sidebar.is_transparent()); - let layout = div() + // A titlebar carries the strips' window duties, + // so the strips only render when it is absent. + let left_strip = titlebar.is_none().then_some(Strip { + side: Side::Left, + height: titlebar_height, + padding_x: metrics.dense_padding_x, + toggle: self.toggle_button, + }); + + // A titlebar carries the strips' window duties, + // so the strips only render when it is absent. + let right_strip = titlebar.is_none().then_some(Strip { + side: Side::Right, + height: titlebar_height, + padding_x: metrics.dense_padding_x, + toggle: false, + }); + + let row = div() .flex() .flex_row() .w_full() - .h_full() + .flex_1() + .min_h_0() .overflow_hidden() - .when(left_visible && left.is_some(), |layout| { + .when(left_visible && left.is_some(), |row| { let width = state.read(cx).left_sidebar_width(); - let titlebar = Titlebar { - side: Side::Left, - material: left_material, - height: titlebar_height, - padding_x: metrics.dense_padding_x, - toggle: self.toggle_button, - }; - layout - .child( - v_flex() - .w(width) - .h_full() - .overflow_hidden() - .when(!left_material, |region| region.bg(background)) - .child( - left.take() - .map(|sidebar| { - sidebar - .strip(sidebar_strip( + row.child( + v_flex() + .w(width) + .h_full() + .overflow_hidden() + .when(!left_material, |region| region.bg(background)) + .child( + left.take() + .map(|sidebar| { + sidebar + .when_some(left_strip, |sidebar, strip| { + sidebar.strip(sidebar_strip( (id.clone(), "left"), - titlebar, + strip, &state, window, cx, )) - .into_any_element() - }) - .unwrap_or_else(|| div().into_any_element()), - ), - ) - .child(ResizeHandle::new((id.clone(), "left-resize")).on_resize({ - let state = state.clone(); - move |delta, _, cx| { - state.update(cx, |state, cx| state.resize_left(delta, cx)); - } - })) + }) + .into_any_element() + }) + .unwrap_or_else(|| div().into_any_element()), + ), + ) + .child(ResizeHandle::new((id.clone(), "left-resize")).on_resize({ + let state = state.clone(); + move |delta, _, cx| { + state.update(cx, |state, cx| state.resize_left(delta, cx)); + } + })) }) .child( div() @@ -302,32 +327,32 @@ impl RenderOnce for Shell { .bg(background) .child(center.take().unwrap_or_else(|| div().into_any_element())), ) - .when(right_visible && right.is_some(), |layout| { + .when(right_visible && right.is_some(), |row| { let width = state.read(cx).right_sidebar_width(); - layout - .child(ResizeHandle::new((id.clone(), "right-resize")).on_resize({ - let state = state.clone(); - move |delta, _, cx| { - state.update(cx, |state, cx| state.resize_right(delta, cx)); - } - })) - .child(sidebar_region( - (id.clone(), "right"), - width, - right.take().unwrap_or_else(|| div().into_any_element()), - &state, - Titlebar { - side: Side::Right, - material: false, - height: titlebar_height, - padding_x: metrics.dense_padding_x, - toggle: false, - }, - window, - cx, - )) + row.child(ResizeHandle::new((id.clone(), "right-resize")).on_resize({ + let state = state.clone(); + move |delta, _, cx| { + state.update(cx, |state, cx| state.resize_right(delta, cx)); + } + })) + .child(sidebar_region( + (id.clone(), "right"), + width, + right.take().unwrap_or_else(|| div().into_any_element()), + &state, + right_strip, + window, + cx, + )) }); + let layout = v_flex() + .w_full() + .h_full() + .overflow_hidden() + .when_some(titlebar, |layout, titlebar| layout.child(titlebar)) + .child(row); + // Wraps the layout in a window frame let frame = frame(layout, window, cx); @@ -383,9 +408,8 @@ where /// Which titlebar duties a sidebar's header strip carries. #[derive(Clone, Copy)] -struct Titlebar { +struct Strip { side: Side, - material: bool, height: Pixels, padding_x: Pixels, /// Whether the strip carries the sidebar's hide toggle. @@ -397,29 +421,30 @@ fn sidebar_region( width: Pixels, content: AnyElement, state: &Entity, - titlebar: Titlebar, + strip: Option, window: &mut Window, cx: &mut App, ) -> AnyElement where I: Into, { - let theme = cx.theme(); - let background = theme.colors.background; + let background = cx.theme().colors.background; v_flex() .w(width) .h_full() .overflow_hidden() - .when(!titlebar.material, |region| region.bg(background)) - .child(sidebar_strip(id, titlebar, state, window, cx)) + .bg(background) + .when_some(strip, |region, strip| { + region.child(sidebar_strip(id, strip, state, window, cx)) + }) .child(div().flex_1().min_h_0().overflow_hidden().child(content)) .into_any_element() } fn sidebar_strip( id: I, - titlebar: Titlebar, + strip: Strip, state: &Entity, window: &mut Window, cx: &mut App, @@ -431,34 +456,32 @@ where h_flex() .id((id.clone(), "strip")) - .debug_selector(move || format!("sidebar-strip-{}", titlebar.side.name())) + .debug_selector(move || format!("sidebar-strip-{}", strip.side.name())) .flex_none() - .h(titlebar.height) + .h(strip.height) .occlude() .items_center() - .map(|strip| match titlebar.side { - Side::Left => strip.when(cfg!(target_os = "macos"), |strip| { - strip.pl(TRAFFIC_LIGHT_PAD) - }), - Side::Right => strip, - }) - .px(titlebar.padding_x) + .px(strip.padding_x) + .when( + cfg!(target_os = "macos") && strip.side == Side::Left, + |row| row.pl(TRAFFIC_LIGHT_PAD), + ) .child(titlebar_strip( div().id((id.clone(), "strip-drag")).flex_1().self_stretch(), (id.clone(), "strip-drag"), window, cx, )) - .map(|strip| match titlebar.side { - 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"))), + .when(strip.side == Side::Left && strip.toggle, |row| { + row.child(sidebar_toggle_button( + (id.clone(), "sidebar-toggle"), + Side::Left, + state, + cx, + )) + }) + .when(strip.side == Side::Right, |row| { + row.child(WindowControls::new((id, "window-controls"))) }) .into_any_element() } diff --git a/crates/layout/src/titlebar.rs b/crates/layout/src/titlebar.rs new file mode 100644 index 0000000..355d8e4 --- /dev/null +++ b/crates/layout/src/titlebar.rs @@ -0,0 +1,77 @@ +//! The window titlebar: the full-width strip drawn above the shell's regions; +//! it moves the window, hosts the window controls, and leads with the app's +//! own children. + +use gpui::prelude::FluentBuilder as _; +use gpui::{ + AnyElement, App, ElementId, InteractiveElement as _, IntoElement, ParentElement, RenderOnce, + SharedString, Styled as _, Window, div, +}; +use gpui_native_theme::{ActiveTheme as _, Size, TRAFFIC_LIGHT_PAD}; +use gpui_native_utils::h_flex; + +use crate::window_controls::{WindowControls, titlebar_strip}; + +/// The window's titlebar. +#[derive(IntoElement)] +#[must_use] +pub struct Titlebar { + id: ElementId, + children: Vec, +} + +impl Titlebar { + /// Creates the titlebar. + pub fn new(id: I) -> Self + where + I: Into, + { + Self { + id: id.into(), + children: Vec::new(), + } + } +} + +impl ParentElement for Titlebar { + fn extend(&mut self, elements: impl IntoIterator) { + self.children.extend(elements); + } +} + +impl RenderOnce for Titlebar { + fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { + let theme = cx.theme(); + let background = theme.colors.background; + let height = theme.titlebar_height; + let padding_x = theme.sizes.resolve(Size::default()).dense_padding_x; + + let id = self.id; + let bar_id = SharedString::from(format!("{id}-bar")); + + h_flex() + .id(id.clone()) + .debug_selector(move || bar_id.to_string()) + .flex_none() + .w_full() + .h(height) + .occlude() + .bg(background) + .px(padding_x) + .when(cfg!(target_os = "macos"), |bar| bar.pl(TRAFFIC_LIGHT_PAD)) + .child( + h_flex() + .min_w_0() + .gap_1() + .overflow_hidden() + .children(self.children), + ) + .child(titlebar_strip( + div().id((id.clone(), "drag")).flex_1().self_stretch(), + (id.clone(), "drag"), + window, + cx, + )) + .child(WindowControls::new((id, "window-controls"))) + } +} diff --git a/examples/gallery/src/main.rs b/examples/gallery/src/main.rs index dd84b13..9e888fc 100644 --- a/examples/gallery/src/main.rs +++ b/examples/gallery/src/main.rs @@ -53,6 +53,7 @@ use gpui_native_layout::dock::{Dock, DockSidebar, DockState, Panel, SplitDirecti use gpui_native_layout::resizable::{Resizable, ResizableState}; use gpui_native_layout::resize_handle::ResizeHandle; use gpui_native_layout::shell::{LayoutState, Shell}; +use gpui_native_layout::titlebar::Titlebar; use gpui_native_theme::{ ActiveTheme as _, CODE_OVERSCROLL_MAX, Theme, ThemeVariant, active_preset, mode_override, set_mode_override, set_preset, @@ -228,6 +229,8 @@ struct Gallery { sidebar_transparent: bool, /// Whether a film-grain overlay textures the window. grain: bool, + /// Whether the shell draws the unified titlebar. + titlebar: bool, } impl Gallery { @@ -286,9 +289,14 @@ 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. - .window_controls(!self.layout_state.read(cx).is_right_sidebar_visible()) - .traffic_light_pad(!self.layout_state.read(cx).is_left_sidebar_visible()); + // The pill bar is the titlebar strip whenever no right sidebar sits + // to its right and the shell titlebar has not taken the chrome over. + .window_controls( + !self.titlebar && !self.layout_state.read(cx).is_right_sidebar_visible(), + ) + .traffic_light_pad( + !self.titlebar && !self.layout_state.read(cx).is_left_sidebar_visible(), + ); let right_sidebar = DockSidebar::new("gallery-dock-sidebar", self.dock_state.clone()); @@ -296,6 +304,33 @@ impl Gallery { .left_sidebar(left_sidebar) .center(center) .right_sidebar(right_sidebar) + .when(self.titlebar, |shell| { + shell.titlebar(self.gallery_titlebar(cx)) + }) + } + + /// The demo titlebar: a sidebar toggle and the app title. + fn gallery_titlebar(&self, cx: &App) -> Titlebar { + let layout = self.layout_state.clone(); + let title_color = cx.theme().colors.muted_foreground; + + Titlebar::new("gallery-titlebar") + .child( + Button::new("titlebar-toggle-left") + .icon(IconRole::PanelLeft) + .variant(ButtonVariant::Ghost) + .small() + .accessibility_label("Toggle left sidebar") + .on_click(move |_, _, cx| { + layout.update(cx, LayoutState::toggle_left_sidebar); + }), + ) + .child( + div() + .text_sm() + .text_color(title_color) + .child("gpui-native-kit"), + ) } fn render_dialog(open: bool, gallery: WeakEntity) -> Dialog { @@ -600,6 +635,7 @@ impl Render for WelcomePanel { let dock_state = self.dock_state.clone(); let gallery = self.gallery.clone(); let grain_gallery = self.gallery.clone(); + let titlebar_gallery = self.gallery.clone(); ScrollArea::new("welcome-demo") .size_full() @@ -682,6 +718,20 @@ impl Render for WelcomePanel { .log_err(); }), ) + .child( + Button::new("welcome-toggle-titlebar") + .label("Toggle titlebar") + .variant(ButtonVariant::Secondary) + .small() + .on_click(move |_, _, cx| { + titlebar_gallery + .update(cx, |gallery, cx| { + gallery.titlebar = !gallery.titlebar; + cx.notify(); + }) + .log_err(); + }), + ) .child( Button::new("welcome-split") .label("Split active right") @@ -4486,6 +4536,7 @@ fn main() { alert_open: false, sidebar_transparent: true, grain: true, + titlebar: false, }); let palette = cx.new(|cx| {