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]
pub enum SidebarEntry {
/// A non-selectable section label.
Header { label: SharedString },
Header {
label: SharedString,
prefix: Option<AnyElement>,
suffix: Option<AnyElement>,
},
/// A selectable row.
Row {
key: ElementId,
@@ -44,6 +48,8 @@ impl SidebarEntry {
{
Self::Header {
label: label.into(),
prefix: None,
suffix: None,
}
}
@@ -77,25 +83,27 @@ impl SidebarEntry {
self
}
/// Shows an element leading the row.
/// Shows an element leading the entry.
pub fn prefix<E>(mut self, prefix: E) -> Self
where
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());
}
self
}
/// Shows an element trailing the row.
/// Shows an element trailing the entry.
pub fn suffix<E>(mut self, suffix: E) -> Self
where
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());
}
self
}
@@ -277,7 +285,7 @@ impl RenderOnce for Sidebar {
None => Vec::new(),
};
// Headers are never selectable; only enabled rows are.
// Headers are never selectable, only enabled rows are.
let selectable: Vec<usize> = entries
.iter()
.enumerate()
@@ -294,7 +302,6 @@ impl RenderOnce for Sidebar {
.use_keyed_state("scroll", cx, |_, _| gpui::ScrollHandle::new())
.clone()
});
let scroll_area_id = (id.clone(), "entries");
let row_count = entries
@@ -412,17 +419,34 @@ impl SidebarEntry {
let theme = cx.theme();
match self {
SidebarEntry::Header { label } => {
SidebarEntry::Header {
label,
prefix,
suffix,
} => {
// The id is required for the role to surface in the a11y tree.
div()
.id(("sidebar-header".into(), label.clone()))
.flex()
.items_center()
.gap_2()
.px_2()
.pt_2()
.pb_1()
.typeset(TextRole::Headline, cx)
.text_color(theme.colors.muted_foreground)
.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()
}
SidebarEntry::Row {
@@ -471,7 +495,16 @@ impl SidebarEntry {
})
})
.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| {
row.child(
div()
+1 -1
View File
@@ -31,7 +31,7 @@ const MACOS: [Rung; 11] = [
// Title 3
(15.0, 20.0, FontWeight::NORMAL),
// Headline
(13.0, 16.0, FontWeight::BOLD),
(13.0, 16.0, FontWeight::SEMIBOLD),
// Body
(13.0, 16.0, FontWeight::NORMAL),
// Callout