This commit is contained in:
2026-10-30 19:36:39 +07:00
parent b4a996e44c
commit 1711ce0e9d
22 changed files with 300 additions and 485 deletions
+31 -94
View File
@@ -5,12 +5,13 @@ use std::rc::Rc;
use gpui::prelude::FluentBuilder as _;
use gpui::{
AnyElement, App, ElementId, FocusHandle, Hsla, InteractiveElement as _, IntoElement,
KeyDownEvent, ParentElement as _, Pixels, RenderOnce, Role, SharedString,
StatefulInteractiveElement as _, Styled as _, Window, div,
AnyElement, App, ElementId, FocusHandle, InteractiveElement as _, IntoElement, KeyDownEvent,
ParentElement as _, RenderOnce, Role, SharedString, StatefulInteractiveElement as _,
Styled as _, Window, div,
};
use gpui_native_icons::{IconRole, icon};
use gpui_native_theme::ActiveTheme as _;
use gpui_native_theme::{ActiveTheme as _, Theme};
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)>;
@@ -148,36 +149,8 @@ impl Sidebar {
}
}
struct SidebarChrome {
foreground: Hsla,
muted: Hsla,
muted_foreground: Hsla,
primary: Hsla,
primary_foreground: Hsla,
radius_sm: Pixels,
icon_size: Pixels,
text_size: Pixels,
/// The entry count, for the rows' set-size a11y attributes.
count: usize,
}
impl RenderOnce for Sidebar {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let chrome = {
let theme = cx.theme();
SidebarChrome {
foreground: theme.colors.foreground,
muted: theme.colors.muted,
muted_foreground: theme.colors.muted_foreground,
primary: theme.ramps.primary.base,
primary_foreground: theme.colors.primary_foreground,
radius_sm: theme.radius.sm,
icon_size: theme.sizes.medium.icon_size,
text_size: theme.sizes.medium.text_size,
count: 0,
}
};
let entries = match self.entries {
Some(build) => build(window, cx),
None => Vec::new(),
@@ -195,30 +168,26 @@ impl RenderOnce for Sidebar {
.collect();
// Keyboard focus rides the container across frames.
let focus_handle = window.with_element_namespace(self.id.clone(), |window| {
window.use_keyed_state("sidebar-focus", cx, |_, cx| cx.focus_handle())
});
let focus = focus_handle.read(cx).clone();
let focus = gpui_native_utils::focus_handle(&self.id, window, cx);
let label = self
.a11y_label
.clone()
.unwrap_or_else(|| SharedString::from(self.id.to_string()));
let chrome = SidebarChrome {
count: entries.len(),
..chrome
};
// Entry paint reads the theme through this borrow.
let theme = cx.theme();
let children: Vec<AnyElement> = entries
.iter()
.enumerate()
.map(|(index, entry)| {
render_entry(
theme,
entry,
index,
entries.len(),
self.selected == Some(index),
&chrome,
&self.on_change,
&focus,
)
@@ -230,7 +199,7 @@ impl RenderOnce for Sidebar {
.flex()
.flex_col()
.gap_0p5()
.text_size(chrome.text_size)
.text_size(theme.sizes.medium.text_size)
.track_focus(&focus)
.focusable()
.tab_stop(true)
@@ -240,20 +209,13 @@ impl RenderOnce for Sidebar {
let selected = self.selected;
let on_change = self.on_change.clone();
move |event: &KeyDownEvent, window, cx| {
let target = match event.keystroke.key.as_str() {
"down" => adjacent(&selectable, selected, true),
"up" => adjacent(&selectable, selected, false),
"home" => selectable.first().copied(),
"end" => selectable.last().copied(),
_ => return,
};
if let Some(next) = target
&& Some(next) != selected
&& let Some(on_change) = &on_change
{
on_change(Some(next), 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();
}
})
.children(children)
@@ -261,10 +223,11 @@ impl RenderOnce for Sidebar {
}
fn render_entry(
theme: &Theme,
entry: &SidebarEntry,
index: usize,
count: usize,
selected: bool,
chrome: &SidebarChrome,
on_change: &Option<ChangeHandler>,
focus: &FocusHandle,
) -> AnyElement {
@@ -275,7 +238,7 @@ fn render_entry(
.pb_1()
.text_sm()
.font_weight(gpui::FontWeight::MEDIUM)
.text_color(chrome.muted_foreground)
.text_color(theme.colors.muted_foreground)
.child(label.clone())
.into_any_element(),
SidebarEntry::Row {
@@ -284,13 +247,7 @@ fn render_entry(
detail,
disabled: row_disabled,
} => {
let label_color = if *row_disabled {
chrome.muted_foreground
} else if selected {
chrome.primary_foreground
} else {
chrome.foreground
};
let label_color = selectable_label_color(theme, selected, *row_disabled);
let mut row = div()
.id(("sidebar-row", index))
@@ -299,20 +256,12 @@ fn render_entry(
.gap_2()
.px_2()
.py_1()
.rounded(chrome.radius_sm)
.rounded(theme.radius.sm)
.role(Role::ListItem)
.aria_selected(selected)
.aria_position_in_set(index + 1)
.aria_size_of_set(chrome.count)
.map(|row| {
if *row_disabled {
row
} else if selected {
row.bg(chrome.primary)
} else {
row.hover(|row| row.bg(chrome.muted))
}
});
.aria_size_of_set(count)
.map(|row| paint_selectable_row(theme, row, selected, *row_disabled));
if !*row_disabled {
row = row.cursor_pointer().on_click({
@@ -328,7 +277,11 @@ fn render_entry(
}
row = row.when_some(*row_icon, |row, role| {
row.child(icon(role).size(chrome.icon_size).color(label_color))
row.child(
icon(role)
.size(theme.sizes.medium.icon_size)
.color(label_color),
)
});
row = row.child(div().flex_1().text_color(label_color).child(label.clone()));
@@ -339,9 +292,9 @@ fn render_entry(
.flex_none()
.text_sm()
.text_color(if selected {
chrome.primary_foreground
theme.colors.primary_foreground
} else {
chrome.muted_foreground
theme.colors.muted_foreground
})
.child(detail.clone()),
);
@@ -351,19 +304,3 @@ fn render_entry(
}
}
}
/// The next selectable row in the direction.
fn adjacent(selectable: &[usize], selected: Option<usize>, forward: bool) -> Option<usize> {
if forward {
selectable
.iter()
.copied()
.find(|index| Some(*index) > selected)
} else {
selectable
.iter()
.copied()
.rev()
.find(|index| Some(*index) < selected)
}
}