sidebar: replace header/footer/content slots with custom entries
This commit is contained in:
+140
-174
@@ -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<dyn Fn(Option<usize>, &mut Window, &mut App)>;
|
||||
type EntriesFn = Box<dyn Fn(&mut Window, &mut App) -> Vec<SidebarEntry>>;
|
||||
|
||||
/// 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<AnyElement>,
|
||||
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<E>(element: E) -> Self
|
||||
where
|
||||
E: IntoElement,
|
||||
{
|
||||
Self::Custom(element.into_any_element())
|
||||
}
|
||||
|
||||
/// Shows muted trailing text.
|
||||
pub fn detail<D>(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<usize>,
|
||||
transparent: bool,
|
||||
grain: bool,
|
||||
border_right: bool,
|
||||
padded: bool,
|
||||
a11y_label: Option<SharedString>,
|
||||
on_change: Option<ChangeHandler>,
|
||||
entries: Option<EntriesFn>,
|
||||
header: Option<AnyElement>,
|
||||
content: Option<AnyElement>,
|
||||
footer: Option<AnyElement>,
|
||||
strip: Option<AnyElement>,
|
||||
}
|
||||
|
||||
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<F>(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<E>(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<E>(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<E>(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<E>(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<L>(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<usize> = 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<AnyElement> = 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<usize> = 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<AnyElement> = 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,
|
||||
|
||||
Reference in New Issue
Block a user