layout: add unified shell titlebar

This commit is contained in:
2026-11-14 18:17:19 +07:00
parent ce77809132
commit 648cbb9c52
5 changed files with 246 additions and 90 deletions
+3 -1
View File
@@ -17,7 +17,7 @@ gpui = { workspace = true, features = ["test-support"] }
[features] [features]
default = ["full"] default = ["full"]
full = ["dock", "resize_handle", "resizable", "shell", "window_controls", "toast", "alert"] full = ["dock", "resize_handle", "resizable", "shell", "titlebar", "window_controls", "toast", "alert"]
dock = [ dock = [
"shell", "shell",
"resize_handle", "resize_handle",
@@ -32,12 +32,14 @@ resize_handle = []
resizable = ["resize_handle"] resizable = ["resize_handle"]
shell = [ shell = [
"resize_handle", "resize_handle",
"titlebar",
"window_controls", "window_controls",
"dep:gpui-native-core", "dep:gpui-native-core",
"gpui-native-core/button", "gpui-native-core/button",
"dep:gpui-native-data", "dep:gpui-native-data",
"dep:gpui-native-icons", "dep:gpui-native-icons",
] ]
titlebar = ["window_controls"]
toast = ["shell", "gpui-native-core/toast"] toast = ["shell", "gpui-native-core/toast"]
alert = ["shell", "gpui-native-core/alert"] alert = ["shell", "gpui-native-core/alert"]
window_controls = ["dep:gpui-native-icons"] window_controls = ["dep:gpui-native-icons"]
+3
View File
@@ -12,6 +12,9 @@ pub mod resizable;
#[cfg(feature = "shell")] #[cfg(feature = "shell")]
pub mod shell; pub mod shell;
#[cfg(feature = "titlebar")]
pub mod titlebar;
#[cfg(feature = "window_controls")] #[cfg(feature = "window_controls")]
pub mod window_controls; pub mod window_controls;
+109 -86
View File
@@ -1,5 +1,6 @@
//! The application shell: a left sidebar (on by default), a right sidebar //! The application shell: an optional titlebar above a left sidebar (on by
//! (off by default), and the center region that hosts the dock. //! default), a right sidebar (off by default), and the center region that
//! hosts the dock.
use gpui::prelude::FluentBuilder as _; use gpui::prelude::FluentBuilder as _;
use gpui::{ use gpui::{
@@ -20,6 +21,7 @@ use gpui_native_theme::{
}; };
use crate::resize_handle::ResizeHandle; use crate::resize_handle::ResizeHandle;
use crate::titlebar::Titlebar;
use crate::window_controls::{TRAFFIC_LIGHT_PAD, WindowControls, titlebar_strip}; use crate::window_controls::{TRAFFIC_LIGHT_PAD, WindowControls, titlebar_strip};
use crate::window_frame::frame; use crate::window_frame::frame;
@@ -173,6 +175,8 @@ impl Side {
pub struct Shell { pub struct Shell {
id: ElementId, id: ElementId,
state: Entity<LayoutState>, state: Entity<LayoutState>,
/// The titlebar drawn above the regions, replacing the strips.
titlebar: Option<Titlebar>,
left_sidebar: Option<Sidebar>, left_sidebar: Option<Sidebar>,
right_sidebar: Option<AnyElement>, right_sidebar: Option<AnyElement>,
center: Option<AnyElement>, center: Option<AnyElement>,
@@ -189,6 +193,7 @@ impl Shell {
Self { Self {
id: id.into(), id: id.into(),
state, state,
titlebar: None,
left_sidebar: None, left_sidebar: None,
right_sidebar: None, right_sidebar: None,
center: None, center: None,
@@ -202,6 +207,12 @@ impl Shell {
self 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. /// Sets the left sidebar's navigation list.
pub fn left_sidebar(mut self, sidebar: Sidebar) -> Self { pub fn left_sidebar(mut self, sidebar: Sidebar) -> Self {
self.left_sidebar = Some(sidebar); self.left_sidebar = Some(sidebar);
@@ -236,6 +247,7 @@ impl RenderOnce for Shell {
let id = self.id.clone(); let id = self.id.clone();
let state = self.state; let state = self.state;
let titlebar = self.titlebar;
let mut left = self.left_sidebar; let mut left = self.left_sidebar;
let mut right = self.right_sidebar; let mut right = self.right_sidebar;
@@ -248,50 +260,63 @@ impl RenderOnce for Shell {
.as_ref() .as_ref()
.is_some_and(|sidebar| sidebar.is_transparent()); .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()
.flex_row() .flex_row()
.w_full() .w_full()
.h_full() .flex_1()
.min_h_0()
.overflow_hidden() .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 width = state.read(cx).left_sidebar_width();
let titlebar = Titlebar { row.child(
side: Side::Left, v_flex()
material: left_material, .w(width)
height: titlebar_height, .h_full()
padding_x: metrics.dense_padding_x, .overflow_hidden()
toggle: self.toggle_button, .when(!left_material, |region| region.bg(background))
}; .child(
layout left.take()
.child( .map(|sidebar| {
v_flex() sidebar
.w(width) .when_some(left_strip, |sidebar, strip| {
.h_full() sidebar.strip(sidebar_strip(
.overflow_hidden()
.when(!left_material, |region| region.bg(background))
.child(
left.take()
.map(|sidebar| {
sidebar
.strip(sidebar_strip(
(id.clone(), "left"), (id.clone(), "left"),
titlebar, strip,
&state, &state,
window, window,
cx, cx,
)) ))
.into_any_element() })
}) .into_any_element()
.unwrap_or_else(|| div().into_any_element()), })
), .unwrap_or_else(|| div().into_any_element()),
) ),
.child(ResizeHandle::new((id.clone(), "left-resize")).on_resize({ )
let state = state.clone(); .child(ResizeHandle::new((id.clone(), "left-resize")).on_resize({
move |delta, _, cx| { let state = state.clone();
state.update(cx, |state, cx| state.resize_left(delta, cx)); move |delta, _, cx| {
} state.update(cx, |state, cx| state.resize_left(delta, cx));
})) }
}))
}) })
.child( .child(
div() div()
@@ -302,32 +327,32 @@ impl RenderOnce for Shell {
.bg(background) .bg(background)
.child(center.take().unwrap_or_else(|| div().into_any_element())), .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(); let width = state.read(cx).right_sidebar_width();
layout row.child(ResizeHandle::new((id.clone(), "right-resize")).on_resize({
.child(ResizeHandle::new((id.clone(), "right-resize")).on_resize({ let state = state.clone();
let state = state.clone(); move |delta, _, cx| {
move |delta, _, cx| { state.update(cx, |state, cx| state.resize_right(delta, cx));
state.update(cx, |state, cx| state.resize_right(delta, cx)); }
} }))
})) .child(sidebar_region(
.child(sidebar_region( (id.clone(), "right"),
(id.clone(), "right"), width,
width, right.take().unwrap_or_else(|| div().into_any_element()),
right.take().unwrap_or_else(|| div().into_any_element()), &state,
&state, right_strip,
Titlebar { window,
side: Side::Right, cx,
material: false, ))
height: titlebar_height,
padding_x: metrics.dense_padding_x,
toggle: false,
},
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 // Wraps the layout in a window frame
let frame = frame(layout, window, cx); let frame = frame(layout, window, cx);
@@ -383,9 +408,8 @@ where
/// Which titlebar duties a sidebar's header strip carries. /// Which titlebar duties a sidebar's header strip carries.
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
struct Titlebar { struct Strip {
side: Side, side: Side,
material: bool,
height: Pixels, height: Pixels,
padding_x: Pixels, padding_x: Pixels,
/// Whether the strip carries the sidebar's hide toggle. /// Whether the strip carries the sidebar's hide toggle.
@@ -397,29 +421,30 @@ fn sidebar_region<I>(
width: Pixels, width: Pixels,
content: AnyElement, content: AnyElement,
state: &Entity<LayoutState>, state: &Entity<LayoutState>,
titlebar: Titlebar, strip: Option<Strip>,
window: &mut Window, window: &mut Window,
cx: &mut App, cx: &mut App,
) -> AnyElement ) -> AnyElement
where where
I: Into<ElementId>, I: Into<ElementId>,
{ {
let theme = cx.theme(); let background = cx.theme().colors.background;
let background = theme.colors.background;
v_flex() v_flex()
.w(width) .w(width)
.h_full() .h_full()
.overflow_hidden() .overflow_hidden()
.when(!titlebar.material, |region| region.bg(background)) .bg(background)
.child(sidebar_strip(id, titlebar, state, window, cx)) .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)) .child(div().flex_1().min_h_0().overflow_hidden().child(content))
.into_any_element() .into_any_element()
} }
fn sidebar_strip<I>( fn sidebar_strip<I>(
id: I, id: I,
titlebar: Titlebar, strip: Strip,
state: &Entity<LayoutState>, state: &Entity<LayoutState>,
window: &mut Window, window: &mut Window,
cx: &mut App, cx: &mut App,
@@ -431,34 +456,32 @@ where
h_flex() h_flex()
.id((id.clone(), "strip")) .id((id.clone(), "strip"))
.debug_selector(move || format!("sidebar-strip-{}", titlebar.side.name())) .debug_selector(move || format!("sidebar-strip-{}", strip.side.name()))
.flex_none() .flex_none()
.h(titlebar.height) .h(strip.height)
.occlude() .occlude()
.items_center() .items_center()
.map(|strip| match titlebar.side { .px(strip.padding_x)
Side::Left => strip.when(cfg!(target_os = "macos"), |strip| { .when(
strip.pl(TRAFFIC_LIGHT_PAD) cfg!(target_os = "macos") && strip.side == Side::Left,
}), |row| row.pl(TRAFFIC_LIGHT_PAD),
Side::Right => strip, )
})
.px(titlebar.padding_x)
.child(titlebar_strip( .child(titlebar_strip(
div().id((id.clone(), "strip-drag")).flex_1().self_stretch(), div().id((id.clone(), "strip-drag")).flex_1().self_stretch(),
(id.clone(), "strip-drag"), (id.clone(), "strip-drag"),
window, window,
cx, cx,
)) ))
.map(|strip| match titlebar.side { .when(strip.side == Side::Left && strip.toggle, |row| {
Side::Left => strip.when(titlebar.toggle, |strip| { row.child(sidebar_toggle_button(
strip.child(sidebar_toggle_button( (id.clone(), "sidebar-toggle"),
(id, "sidebar-toggle"), Side::Left,
Side::Left, state,
state, cx,
cx, ))
)) })
}), .when(strip.side == Side::Right, |row| {
Side::Right => strip.child(WindowControls::new((id, "window-controls"))), row.child(WindowControls::new((id, "window-controls")))
}) })
.into_any_element() .into_any_element()
} }
+77
View File
@@ -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<AnyElement>,
}
impl Titlebar {
/// Creates the titlebar.
pub fn new<I>(id: I) -> Self
where
I: Into<ElementId>,
{
Self {
id: id.into(),
children: Vec::new(),
}
}
}
impl ParentElement for Titlebar {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
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")))
}
}
+54 -3
View File
@@ -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::resizable::{Resizable, ResizableState};
use gpui_native_layout::resize_handle::ResizeHandle; use gpui_native_layout::resize_handle::ResizeHandle;
use gpui_native_layout::shell::{LayoutState, Shell}; use gpui_native_layout::shell::{LayoutState, Shell};
use gpui_native_layout::titlebar::Titlebar;
use gpui_native_theme::{ use gpui_native_theme::{
ActiveTheme as _, CODE_OVERSCROLL_MAX, Theme, ThemeVariant, active_preset, mode_override, ActiveTheme as _, CODE_OVERSCROLL_MAX, Theme, ThemeVariant, active_preset, mode_override,
set_mode_override, set_preset, set_mode_override, set_preset,
@@ -228,6 +229,8 @@ struct Gallery {
sidebar_transparent: bool, sidebar_transparent: bool,
/// Whether a film-grain overlay textures the window. /// Whether a film-grain overlay textures the window.
grain: bool, grain: bool,
/// Whether the shell draws the unified titlebar.
titlebar: bool,
} }
impl Gallery { impl Gallery {
@@ -286,9 +289,14 @@ impl Gallery {
}); });
let center = Dock::new("gallery-dock", self.dock_state.clone()) 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
.window_controls(!self.layout_state.read(cx).is_right_sidebar_visible()) // to its right and the shell titlebar has not taken the chrome over.
.traffic_light_pad(!self.layout_state.read(cx).is_left_sidebar_visible()); .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()); let right_sidebar = DockSidebar::new("gallery-dock-sidebar", self.dock_state.clone());
@@ -296,6 +304,33 @@ impl Gallery {
.left_sidebar(left_sidebar) .left_sidebar(left_sidebar)
.center(center) .center(center)
.right_sidebar(right_sidebar) .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<Self>) -> Dialog { fn render_dialog(open: bool, gallery: WeakEntity<Self>) -> Dialog {
@@ -600,6 +635,7 @@ impl Render for WelcomePanel {
let dock_state = self.dock_state.clone(); let dock_state = self.dock_state.clone();
let gallery = self.gallery.clone(); let gallery = self.gallery.clone();
let grain_gallery = self.gallery.clone(); let grain_gallery = self.gallery.clone();
let titlebar_gallery = self.gallery.clone();
ScrollArea::new("welcome-demo") ScrollArea::new("welcome-demo")
.size_full() .size_full()
@@ -682,6 +718,20 @@ impl Render for WelcomePanel {
.log_err(); .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( .child(
Button::new("welcome-split") Button::new("welcome-split")
.label("Split active right") .label("Split active right")
@@ -4486,6 +4536,7 @@ fn main() {
alert_open: false, alert_open: false,
sidebar_transparent: true, sidebar_transparent: true,
grain: true, grain: true,
titlebar: false,
}); });
let palette = cx.new(|cx| { let palette = cx.new(|cx| {