layout: add unified shell titlebar
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
+109
-86
@@ -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<LayoutState>,
|
||||
/// The titlebar drawn above the regions, replacing the strips.
|
||||
titlebar: Option<Titlebar>,
|
||||
left_sidebar: Option<Sidebar>,
|
||||
right_sidebar: Option<AnyElement>,
|
||||
center: Option<AnyElement>,
|
||||
@@ -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<I>(
|
||||
width: Pixels,
|
||||
content: AnyElement,
|
||||
state: &Entity<LayoutState>,
|
||||
titlebar: Titlebar,
|
||||
strip: Option<Strip>,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> AnyElement
|
||||
where
|
||||
I: Into<ElementId>,
|
||||
{
|
||||
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<I>(
|
||||
id: I,
|
||||
titlebar: Titlebar,
|
||||
strip: Strip,
|
||||
state: &Entity<LayoutState>,
|
||||
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()
|
||||
}
|
||||
|
||||
@@ -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")))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user