layout: replace sidebar toggle slots with tab prefix and suffix

This commit is contained in:
2026-11-14 16:00:41 +07:00
parent bf8a92a133
commit 93860e3c1d
3 changed files with 41 additions and 84 deletions
+33 -36
View File
@@ -20,7 +20,6 @@ use gpui_native_icons::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.
@@ -783,8 +782,10 @@ 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<Entity<LayoutState>>,
/// Builds the pill bar's leading element on each frame.
tab_prefix: Option<Rc<dyn Fn() -> AnyElement>>,
/// Builds the pill bar's trailing element on each frame.
tab_suffix: Option<Rc<dyn Fn() -> AnyElement>>,
}
impl Dock {
@@ -798,7 +799,8 @@ impl Dock {
state,
window_controls: false,
traffic_light_pad: false,
sidebar_toggles: None,
tab_prefix: None,
tab_suffix: None,
}
}
@@ -814,9 +816,21 @@ impl Dock {
self
}
/// Renders the sidebar toggles in the pill bar.
pub fn sidebar_toggles(mut self, state: Entity<LayoutState>) -> Self {
self.sidebar_toggles = Some(state);
/// Leads the pill bar with `prefix`, rebuilt on each frame.
pub fn tab_prefix<F>(mut self, prefix: F) -> Self
where
F: Fn() -> AnyElement + 'static,
{
self.tab_prefix = Some(Rc::new(prefix));
self
}
/// Trails the pill bar with `suffix`, rebuilt on each frame.
pub fn tab_suffix<F>(mut self, suffix: F) -> Self
where
F: Fn() -> AnyElement + 'static,
{
self.tab_suffix = Some(Rc::new(suffix));
self
}
}
@@ -828,7 +842,8 @@ 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,
prefix: self.tab_prefix,
suffix: self.tab_suffix,
};
let root = render_node(&self.id, &self.state, &tree, &titlebar, window, cx);
@@ -852,8 +867,10 @@ struct Titlebar {
window_controls: bool,
traffic_light_pad: bool,
height: Pixels,
/// The layout whose sidebar toggles lead and trail the tab bar.
toggles: Option<Entity<LayoutState>>,
/// Builds the tab bar's leading element on each frame.
prefix: Option<Rc<dyn Fn() -> AnyElement>>,
/// Builds the tab bar's trailing element on each frame.
suffix: Option<Rc<dyn Fn() -> AnyElement>>,
}
/// The right sidebar content of the dock's active panel.
@@ -1106,28 +1123,8 @@ fn render_pane(
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,
)
});
let prefix = titlebar.prefix.as_ref().map(|prefix| prefix());
let suffix = titlebar.suffix.as_ref().map(|suffix| suffix());
h_flex()
.id(bar_id.clone())
@@ -1142,8 +1139,8 @@ fn render_pane(
padding_x
})
.pr(padding_x)
.when_some(toggle_left, |bar, button| {
bar.child(div().flex_none().mr_1().child(button))
.when_some(prefix, |bar, element| {
bar.child(div().flex_none().mr_1().child(element))
})
.child(
div()
@@ -1166,8 +1163,8 @@ fn render_pane(
window,
cx,
))
.when_some(toggle_right, |bar, button| {
bar.child(div().flex_none().ml_1().child(button))
.when_some(suffix, |bar, element| {
bar.child(div().flex_none().ml_1().child(element))
})
.when(titlebar.window_controls, |bar| {
bar.child(WindowControls::new(SharedString::from(format!(
+7 -46
View File
@@ -23,25 +23,12 @@ 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<Toasts>,
#[cfg(feature = "alert")]
@@ -56,7 +43,6 @@ 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")]
@@ -80,19 +66,6 @@ impl LayoutState {
}
}
/// Sets where the sidebar show/hide toggles live.
pub fn set_sidebar_toggles(&mut self, toggles: SidebarToggles, cx: &mut Context<Self>) {
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
@@ -251,7 +224,6 @@ 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()
@@ -270,7 +242,6 @@ impl RenderOnce for Shell {
material: left_material,
height: titlebar_height,
padding_x: metrics.dense_padding_x,
toggle: strip_toggles,
};
layout
.child(
@@ -330,7 +301,6 @@ impl RenderOnce for Shell {
material: false,
height: titlebar_height,
padding_x: metrics.dense_padding_x,
toggle: strip_toggles,
},
window,
cx,
@@ -357,12 +327,7 @@ impl RenderOnce for Shell {
}
/// One sidebar show/hide toggle.
pub(crate) fn sidebar_toggle_button<I>(
id: I,
side: Side,
state: &Entity<LayoutState>,
cx: &App,
) -> Button
fn sidebar_toggle_button<I>(id: I, side: Side, state: &Entity<LayoutState>, cx: &App) -> Button
where
I: Into<ElementId>,
{
@@ -406,8 +371,6 @@ struct Titlebar {
material: bool,
height: Pixels,
padding_x: Pixels,
/// Whether the strip carries the sidebar's hide toggle.
toggle: bool,
}
fn sidebar_region<I>(
@@ -468,14 +431,12 @@ where
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::Left => strip.child(sidebar_toggle_button(
(id, "sidebar-toggle"),
Side::Left,
state,
cx,
)),
Side::Right => strip.child(WindowControls::new((id, "window-controls"))),
})
.into_any_element()