data: add sidebar header prefix and suffix slots

This commit is contained in:
2026-11-14 15:02:12 +07:00
parent 19154527f2
commit bf8a92a133
2 changed files with 48 additions and 15 deletions
+45 -12
View File
@@ -24,7 +24,11 @@ type EntriesFn = Box<dyn Fn(&mut Window, &mut App) -> Vec<SidebarEntry>>;
#[must_use] #[must_use]
pub enum SidebarEntry { pub enum SidebarEntry {
/// A non-selectable section label. /// A non-selectable section label.
Header { label: SharedString }, Header {
label: SharedString,
prefix: Option<AnyElement>,
suffix: Option<AnyElement>,
},
/// A selectable row. /// A selectable row.
Row { Row {
key: ElementId, key: ElementId,
@@ -44,6 +48,8 @@ impl SidebarEntry {
{ {
Self::Header { Self::Header {
label: label.into(), label: label.into(),
prefix: None,
suffix: None,
} }
} }
@@ -77,25 +83,27 @@ impl SidebarEntry {
self self
} }
/// Shows an element leading the row. /// Shows an element leading the entry.
pub fn prefix<E>(mut self, prefix: E) -> Self pub fn prefix<E>(mut self, prefix: E) -> Self
where where
E: IntoElement, E: IntoElement,
{ {
if let Self::Row { prefix: slot, .. } = &mut self { let slot = match &mut self {
Self::Row { prefix: slot, .. } | Self::Header { prefix: slot, .. } => slot,
};
*slot = Some(prefix.into_any_element()); *slot = Some(prefix.into_any_element());
}
self self
} }
/// Shows an element trailing the row. /// Shows an element trailing the entry.
pub fn suffix<E>(mut self, suffix: E) -> Self pub fn suffix<E>(mut self, suffix: E) -> Self
where where
E: IntoElement, E: IntoElement,
{ {
if let Self::Row { suffix: slot, .. } = &mut self { let slot = match &mut self {
Self::Row { suffix: slot, .. } | Self::Header { suffix: slot, .. } => slot,
};
*slot = Some(suffix.into_any_element()); *slot = Some(suffix.into_any_element());
}
self self
} }
@@ -277,7 +285,7 @@ impl RenderOnce for Sidebar {
None => Vec::new(), None => Vec::new(),
}; };
// Headers are never selectable; only enabled rows are. // Headers are never selectable, only enabled rows are.
let selectable: Vec<usize> = entries let selectable: Vec<usize> = entries
.iter() .iter()
.enumerate() .enumerate()
@@ -294,7 +302,6 @@ impl RenderOnce for Sidebar {
.use_keyed_state("scroll", cx, |_, _| gpui::ScrollHandle::new()) .use_keyed_state("scroll", cx, |_, _| gpui::ScrollHandle::new())
.clone() .clone()
}); });
let scroll_area_id = (id.clone(), "entries"); let scroll_area_id = (id.clone(), "entries");
let row_count = entries let row_count = entries
@@ -412,17 +419,34 @@ impl SidebarEntry {
let theme = cx.theme(); let theme = cx.theme();
match self { match self {
SidebarEntry::Header { label } => { SidebarEntry::Header {
label,
prefix,
suffix,
} => {
// The id is required for the role to surface in the a11y tree. // The id is required for the role to surface in the a11y tree.
div() div()
.id(("sidebar-header".into(), label.clone())) .id(("sidebar-header".into(), label.clone()))
.flex()
.items_center()
.gap_2()
.px_2() .px_2()
.pt_2() .pt_2()
.pb_1() .pb_1()
.typeset(TextRole::Headline, cx) .typeset(TextRole::Headline, cx)
.text_color(theme.colors.muted_foreground) .text_color(theme.colors.muted_foreground)
.role(Role::Heading) .role(Role::Heading)
.child(label) .when_some(prefix, |header, prefix| header.child(prefix))
.child(
div()
.flex_1()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.child(label),
)
.when_some(suffix, |header, suffix| header.child(suffix))
.into_any_element() .into_any_element()
} }
SidebarEntry::Row { SidebarEntry::Row {
@@ -471,7 +495,16 @@ impl SidebarEntry {
}) })
}) })
.when_some(prefix, |row, prefix| row.child(prefix)) .when_some(prefix, |row, prefix| row.child(prefix))
.child(div().flex_1().text_color(label_color).child(label)) .child(
div()
.flex_1()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.text_color(label_color)
.child(label),
)
.when_some(detail, |row, detail| { .when_some(detail, |row, detail| {
row.child( row.child(
div() div()
+1 -1
View File
@@ -31,7 +31,7 @@ const MACOS: [Rung; 11] = [
// Title 3 // Title 3
(15.0, 20.0, FontWeight::NORMAL), (15.0, 20.0, FontWeight::NORMAL),
// Headline // Headline
(13.0, 16.0, FontWeight::BOLD), (13.0, 16.0, FontWeight::SEMIBOLD),
// Body // Body
(13.0, 16.0, FontWeight::NORMAL), (13.0, 16.0, FontWeight::NORMAL),
// Callout // Callout