layout: add sidebar toggle placement options
This commit is contained in:
@@ -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<Entity<LayoutState>>,
|
||||
}
|
||||
|
||||
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<LayoutState>) -> 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<Entity<LayoutState>>,
|
||||
}
|
||||
|
||||
/// 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",
|
||||
|
||||
+102
-24
@@ -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<Toasts>,
|
||||
#[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<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
|
||||
@@ -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<I>(
|
||||
id: I,
|
||||
side: Side,
|
||||
state: &Entity<LayoutState>,
|
||||
cx: &App,
|
||||
) -> Button
|
||||
where
|
||||
I: Into<ElementId>,
|
||||
{
|
||||
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"))),
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user