From 0fda22caf709862604c97662e98718b64cccdd88 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sun, 15 Nov 2026 20:43:05 +0700 Subject: [PATCH] sidebar: replace header/footer/content slots with custom entries --- crates/data/src/sidebar.rs | 314 ++++++++++++++++------------------- examples/gallery/src/main.rs | 77 ++++----- 2 files changed, 173 insertions(+), 218 deletions(-) diff --git a/crates/data/src/sidebar.rs b/crates/data/src/sidebar.rs index 86abcc5..294d5c1 100644 --- a/crates/data/src/sidebar.rs +++ b/crates/data/src/sidebar.rs @@ -1,7 +1,6 @@ //! The sidebar: a controlled list with section headers — the Finder -//! sidebar's model of rows grouped under non-selectable labels. Custom content -//! replaces the list and takes the whole container, sliding under a pinned -//! strip; the header and footer slots apply only to the entry list. +//! sidebar's model of rows grouped under non-selectable labels. Custom +//! entries slot user content between the rows. use std::rc::Rc; @@ -13,14 +12,14 @@ use gpui::{ }; use gpui_native_core::grain::grain as grain_overlay; use gpui_native_core::scroll_area::ScrollArea; -use gpui_native_theme::{ActiveTheme as _, TextRole, Typeset as _}; +use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, TextRole, Typeset as _}; use gpui_native_utils::{nav_key, paint_selectable_row, selectable_label_color}; /// The change reporter, fired with the next selected row by pointer and keys. type ChangeHandler = Rc, &mut Window, &mut App)>; type EntriesFn = Box Vec>; -/// One entry of a [`Sidebar`], a section header or a selectable row. +/// One entry of a [`Sidebar`], a section header, a selectable row, or custom content. #[must_use] pub enum SidebarEntry { /// A non-selectable section label. @@ -38,6 +37,8 @@ pub enum SidebarEntry { suffix: Option, disabled: bool, }, + /// Custom content occupying a slot in the list. + Custom(AnyElement), } impl SidebarEntry { @@ -69,6 +70,14 @@ impl SidebarEntry { } } + /// Wraps custom content occupying a slot in the list. + pub fn custom(element: E) -> Self + where + E: IntoElement, + { + Self::Custom(element.into_any_element()) + } + /// Shows muted trailing text. pub fn detail(mut self, detail: D) -> Self where @@ -88,10 +97,9 @@ impl SidebarEntry { where E: IntoElement, { - let slot = match &mut self { - Self::Row { prefix: slot, .. } | Self::Header { prefix: slot, .. } => slot, - }; - *slot = Some(prefix.into_any_element()); + if let Self::Row { prefix: slot, .. } | Self::Header { prefix: slot, .. } = &mut self { + *slot = Some(prefix.into_any_element()); + } self } @@ -100,10 +108,9 @@ impl SidebarEntry { where E: IntoElement, { - let slot = match &mut self { - Self::Row { suffix: slot, .. } | Self::Header { suffix: slot, .. } => slot, - }; - *slot = Some(suffix.into_any_element()); + if let Self::Row { suffix: slot, .. } | Self::Header { suffix: slot, .. } = &mut self { + *slot = Some(suffix.into_any_element()); + } self } @@ -131,13 +138,11 @@ pub struct Sidebar { selected: Option, transparent: bool, grain: bool, + border_right: bool, + padded: bool, a11y_label: Option, on_change: Option, entries: Option, - header: Option, - content: Option, - footer: Option, - strip: Option, } impl Sidebar { @@ -151,13 +156,11 @@ impl Sidebar { selected: None, transparent: false, grain: false, + border_right: true, + padded: true, a11y_label: None, on_change: None, entries: None, - header: None, - content: None, - footer: None, - strip: None, } } @@ -185,6 +188,28 @@ impl Sidebar { self } + /// Paints a hairline border on the sidebar's right edge. + pub fn border_right(mut self, border_right: bool) -> Self { + self.border_right = border_right; + self + } + + /// Whether the right-edge border is painted. + pub fn has_border_right(&self) -> bool { + self.border_right + } + + /// Insets the entry list from the sidebar's edges; the scrollbar stays on the edge. + pub fn padded(mut self, padded: bool) -> Self { + self.padded = padded; + self + } + + /// Whether the entry list is inset from the sidebar's edges. + pub fn is_padded(&self) -> bool { + self.padded + } + /// Reports the next selected row, from clicks and keys alike. pub fn on_change(mut self, handler: F) -> Self where @@ -203,42 +228,6 @@ impl Sidebar { self } - /// Pins an element above the list, ignored when content is set. - pub fn header(mut self, header: E) -> Self - where - E: IntoElement, - { - self.header = Some(header.into_any_element()); - self - } - - /// Replaces the list with custom content spanning the whole container. - pub fn content(mut self, content: E) -> Self - where - E: IntoElement, - { - self.content = Some(content.into_any_element()); - self - } - - /// Pins an element below the list, ignored when content is set. - pub fn footer(mut self, footer: E) -> Self - where - E: IntoElement, - { - self.footer = Some(footer.into_any_element()); - self - } - - /// Pins a titlebar strip at the sidebar's top, custom content renders behind it. - pub fn strip(mut self, strip: E) -> Self - where - E: IntoElement, - { - self.strip = Some(strip.into_any_element()); - self - } - /// Overrides the accessibility label, which defaults to the id. pub fn accessibility_label(mut self, label: L) -> Self where @@ -256,135 +245,113 @@ impl RenderOnce for Sidebar { selected, transparent: material, grain, + border_right, + padded, a11y_label, on_change, entries, - header, - content, - footer, - strip, } = self; - let custom = content.is_some(); - let (header, footer) = if custom { - (None, None) - } else { - (header, footer) - }; - let label = a11y_label.unwrap_or_else(|| SharedString::from(id.to_string())); let debug_id = id.clone(); - let body = match content { - Some(content) => div() - .flex_1() - .min_h_0() - .overflow_hidden() - .child(content) - .into_any_element(), - None => { - let entries = match entries { - Some(build) => build(window, cx), - None => Vec::new(), + let entries = match entries { + Some(build) => build(window, cx), + None => Vec::new(), + }; + + // Headers and custom entries are never selectable, only enabled rows are. + let selectable: Vec = entries + .iter() + .enumerate() + .filter(|(_, entry)| match entry { + SidebarEntry::Row { disabled, .. } => !disabled, + SidebarEntry::Header { .. } | SidebarEntry::Custom(_) => false, + }) + .map(|(index, _)| index) + .collect(); + + let focus = gpui_native_utils::focus_handle(&id, window, cx); + let scroll = window.with_element_namespace(id.clone(), |window| { + window + .use_keyed_state("scroll", cx, |_, _| gpui::ScrollHandle::new()) + .clone() + }); + let scroll_area_id = (id.clone(), "entries"); + + let row_count = entries + .iter() + .filter(|entry| matches!(entry, SidebarEntry::Row { .. })) + .count(); + let mut row_position = 0usize; + + let children: Vec = entries + .into_iter() + .enumerate() + .map(|(index, entry)| { + let is_row = matches!(entry, SidebarEntry::Row { .. }); + + if is_row { + row_position += 1; + } + + let (position_in_set, size_of_set) = if is_row { + (row_position, row_count) + } else { + (0, 0) }; - // Headers are never selectable, only enabled rows are. - let selectable: Vec = entries - .iter() - .enumerate() - .filter(|(_, entry)| match entry { - SidebarEntry::Row { disabled, .. } => !disabled, - SidebarEntry::Header { .. } => false, - }) - .map(|(index, _)| index) - .collect(); + entry.render_entry( + cx, + index, + position_in_set, + size_of_set, + selected == Some(index), + material, + &on_change, + &focus, + ) + }) + .collect(); - let focus = gpui_native_utils::focus_handle(&id, window, cx); - let scroll = window.with_element_namespace(id.clone(), |window| { - window - .use_keyed_state("scroll", cx, |_, _| gpui::ScrollHandle::new()) - .clone() - }); - let scroll_area_id = (id.clone(), "entries"); - - let row_count = entries - .iter() - .filter(|entry| matches!(entry, SidebarEntry::Row { .. })) - .count(); - - let mut row_position = 0usize; - let children: Vec = entries - .into_iter() - .enumerate() - .map(|(index, entry)| { - let is_row = matches!(entry, SidebarEntry::Row { .. }); - - if is_row { - row_position += 1; + let body = div() + .id((id.clone(), "list")) + .flex_1() + .min_h_0() + .track_focus(&focus) + .focusable() + .tab_stop(true) + .role(Role::List) + .aria_label(label) + .on_key_down({ + let on_change = on_change.clone(); + move |event: &KeyDownEvent, window, cx| { + if nav_key(&selectable, selected, &event.keystroke.key, |next| { + if let Some(on_change) = &on_change { + on_change(next, window, cx); } - - let (position_in_set, size_of_set) = if is_row { - (row_position, row_count) - } else { - (0, 0) - }; - - entry.render_entry( - cx, - index, - position_in_set, - size_of_set, - selected == Some(index), - material, - &on_change, - &focus, - ) - }) - .collect(); - - div() - .id((id.clone(), "list")) - .flex_1() - .min_h_0() - .track_focus(&focus) - .focusable() - .tab_stop(true) - .role(Role::List) - .aria_label(label.clone()) - .on_key_down({ - let on_change = on_change.clone(); - move |event: &KeyDownEvent, window, cx| { - if nav_key(&selectable, selected, &event.keystroke.key, |next| { - if let Some(on_change) = &on_change { - on_change(next, window, cx); - } - }) { - cx.stop_propagation(); - } - } - }) + }) { + cx.stop_propagation(); + } + } + }) + .child( + ScrollArea::new(scroll_area_id) + .handle(scroll.read(cx).clone()) + .h_full() .child( - ScrollArea::new(scroll_area_id) - .handle(scroll.read(cx).clone()) - .h_full() + div() + .flex() + .flex_col() .gap_0p5() + .when(padded, |content| content.px_2().py_1()) .children(children), - ) - .into_any_element() - } - }; + ), + ) + .into_any_element(); let theme = cx.theme(); - let (leading_strip, overlay_strip) = match strip { - Some(strip) if custom => ( - None, - Some(div().absolute().top_0().left_0().right_0().child(strip)), - ), - Some(strip) => (Some(strip), None), - None => (None, None), - }; - div() .id(id.clone()) .debug_selector(move || format!("sidebar-{debug_id}")) @@ -393,14 +360,12 @@ impl RenderOnce for Sidebar { .flex_col() .when(material, |sidebar| sidebar.bg(theme.material.background)) .text_size(theme.sizes.medium.text_size) - .when(custom, |sidebar| { - sidebar.role(Role::Group).aria_label(label) + .when(border_right, |sidebar| { + sidebar + .border_r(BORDER_WIDTH) + .border_color(theme.colors.border) }) - .when_some(leading_strip, |sidebar, strip| sidebar.child(strip)) - .when_some(header, |sidebar, header| sidebar.child(header)) .child(body) - .when_some(footer, |sidebar, footer| sidebar.child(footer)) - .when_some(overlay_strip, |sidebar, strip| sidebar.child(strip)) .when(material && grain, |sidebar| sidebar.child(grain_overlay())) } } @@ -451,6 +416,7 @@ impl SidebarEntry { .when_some(suffix, |header, suffix| header.child(suffix)) .into_any_element() } + SidebarEntry::Custom(element) => element, SidebarEntry::Row { key, label, diff --git a/examples/gallery/src/main.rs b/examples/gallery/src/main.rs index 74f02bf..97bdf6b 100644 --- a/examples/gallery/src/main.rs +++ b/examples/gallery/src/main.rs @@ -4257,7 +4257,7 @@ impl Render for SidebarDemo { ), )) .child(demo_section( - "header and footer slots stay pinned around the list", + "custom entries slot user content around the rows", colors, div() .w(rems(17.5)) @@ -4270,41 +4270,41 @@ impl Render for SidebarDemo { Sidebar::new("sidebar-demo-slots") .selected(self.selected) .transparent(true) - .header( - div() - .px_2() - .py_2() - .text_sm() - .font_weight(FontWeight::MEDIUM) - .child("Library"), - ) - .footer( - div() - .px_2() - .py_2() - .text_xs() - .text_color(colors.muted_foreground) - .child("3 of 7 items"), - ) .on_change(move |selected, _, cx| { view_slots.update(cx, |demo, cx| { demo.selected = selected; cx.notify(); }); }) - .entries(|_, _| { + .entries(move |_, _| { vec![ + SidebarEntry::custom( + div() + .px_2() + .py_2() + .text_sm() + .font_weight(FontWeight::MEDIUM) + .child("Library"), + ), SidebarEntry::header("Favorites"), SidebarEntry::row("Documents", "Documents"), SidebarEntry::row("Pictures", "Pictures").detail("24"), SidebarEntry::header("Locations"), SidebarEntry::row("Trash", "Trash"), + SidebarEntry::custom( + div() + .px_2() + .py_2() + .text_xs() + .text_color(colors.muted_foreground) + .child("3 of 7 items"), + ), ] }), ), )) .child(demo_section( - "custom content replaces the list; the sidebar is a bare container", + "a custom entry fills the list; the right border is off", colors, div() .w(rems(17.5)) @@ -4316,31 +4316,20 @@ impl Render for SidebarDemo { .child( Sidebar::new("sidebar-demo-custom") .transparent(true) - .header( - div() - .px_2() - .py_2() - .text_sm() - .font_weight(FontWeight::MEDIUM) - .child("Welcome"), - ) - .content( - div() - .size_full() - .flex() - .items_center() - .justify_center() - .text_color(colors.muted_foreground) - .child("Nothing here yet"), - ) - .footer( - div() - .px_2() - .py_2() - .text_xs() - .text_color(colors.muted_foreground) - .child("v1.0"), - ), + .border_right(false) + .entries(move |_, _| { + vec![SidebarEntry::custom( + div() + .w_full() + .px_2() + .py_6() + .flex() + .items_center() + .justify_center() + .text_color(colors.muted_foreground) + .child("Nothing here yet"), + )] + }), ), )) .child(