feat: use uniform list for tree component
Rust / build (macos-latest, stable) (push) Canceled after 0s
Rust / build (ubuntu-latest, stable) (push) Canceled after 0s
Rust / build (windows-latest, stable) (push) Canceled after 0s

This commit is contained in:
2026-11-25 08:35:23 +07:00
parent bf06d29c2e
commit e54364b716
5 changed files with 218 additions and 189 deletions
+1
View File
@@ -33,6 +33,7 @@
- Fix text typed into a field under a panel appearing up to a quarter second late
- Fix an open dialog taking keyboard focus back from the command palette or a toast button on every repaint
- Fix the remove button of a Settings or grasp server row carrying its hover state over to the next row after a removal
- Fix the file tree's scrollbar sitting inset from the sidebar's right border, it now rides beside it
## v0.2.0 - 2026/10/21
+3 -7
View File
@@ -260,12 +260,9 @@ impl RepoFilesView {
let change_view = view.clone();
let disclosure_view = view;
div()
.flex_1()
.min_h_0()
.w_full()
.child(
// Mirrors the sidebar's left inset, so rows stay aligned while the scrollbar hugs its border.
Tree::new("repo-tree")
.pr_2()
.selected(selected)
.items(move |_, _| Arc::clone(&rows))
.on_change({
@@ -294,8 +291,7 @@ impl RepoFilesView {
log::warn!("failed to toggle folder: {error}");
}
}
}),
)
})
.into_any_element()
}
+18 -16
View File
@@ -6,9 +6,8 @@ use anyhow::Error;
use gpui::prelude::FluentBuilder as _;
use gpui::{
AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
ParentElement as _, PathPromptOptions, Render, SharedString, StatefulInteractiveElement as _,
StyleRefinement, Styled as _, Subscription, SystemNotification, Task, WeakEntity, Window, div,
px,
ParentElement as _, PathPromptOptions, Render, SharedString, StyleRefinement, Styled as _,
Subscription, SystemNotification, Task, WeakEntity, Window, div, px,
};
use metadata::ProfileDirectory;
use nostr::prelude::{PublicKey, Url};
@@ -1441,27 +1440,28 @@ impl RepoDetailView {
let commit_id = commit.id.clone();
Some(
h_flex()
.id("last-commit")
.h_8()
.px_2()
.gap_2()
.min_w_0()
.rounded(cx.theme().radius.control)
.hover(|this| this.bg(cx.theme().colors.muted_hover))
Button::new("last-commit")
.variant(ButtonVariant::Ghost)
.fully_rounded(true)
.tooltip(Tooltip::text("View latest commit"))
.on_click(move |_, window, cx| {
history.update(cx, |history, cx| {
history.open_commit_diff(&commit_id, window, cx);
});
})
.child(Icon::named(IconRole::GitCommit).color(cx.theme().colors.muted_foreground))
.child(
h_flex()
.w_full()
.gap_2()
.child(
Icon::named(IconRole::GitCommit)
.color(cx.theme().colors.muted_foreground),
)
.child(
div()
.font_family(cx.theme().mono_font)
.text_sm()
.text_color(cx.theme().colors.muted_foreground)
.child(SharedString::from(short)),
.child(SharedString::from(&short)),
)
.child(
div()
@@ -1469,14 +1469,16 @@ impl RepoDetailView {
.text_sm()
.text_ellipsis()
.whitespace_nowrap()
.child(commit.summary.clone()),
.child(SharedString::from(&commit.summary)),
)
.child(
div()
.text_sm()
.text_color(cx.theme().colors.muted_foreground)
.child(relative_time_secs(commit.time)),
.child(SharedString::from(relative_time_secs(commit.time))),
),
)
.flex_1()
.into_any_element(),
)
}
+25 -24
View File
@@ -29,7 +29,6 @@ use crate::components::common::{
StyledExt as _, nav_key, paint_selectable_row, selectable_label_color,
};
use crate::components::grain::grain as grain_overlay;
use crate::components::scroll_area::ScrollArea;
use crate::theme::{ActiveTheme as _, BORDER_WIDTH, SIDEBAR_DEFAULT_WIDTH};
/// The change reporter, fired with the next selected row by pointer and keys.
@@ -397,24 +396,19 @@ impl RenderOnce for Sidebar {
.collect();
let focus = crate::components::common::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 mut row_position = 0usize;
let mut list_children = Vec::new();
let mut custom_children = Vec::new();
for (index, entry) in entries.into_iter().enumerate() {
let is_row = matches!(entry, SidebarEntry::Row { .. });
let is_custom = matches!(entry, SidebarEntry::Custom(_));
if is_row {
row_position += 1;
@@ -426,7 +420,7 @@ impl RenderOnce for Sidebar {
(0, 0)
};
entry.render_entry(
let rendered = entry.render_entry(
cx,
index,
position_in_set,
@@ -435,9 +429,14 @@ impl RenderOnce for Sidebar {
material,
&on_change,
&focus,
)
})
.collect();
);
if is_custom {
custom_children.push(rendered);
} else {
list_children.push(rendered);
}
}
let theme = cx.theme();
@@ -456,8 +455,7 @@ impl RenderOnce for Sidebar {
.child(
div()
.id((id.clone(), "list"))
.flex_1()
.min_h_0()
.flex_none()
.track_focus(&focus)
.focusable()
.tab_stop(true)
@@ -475,19 +473,22 @@ impl RenderOnce for Sidebar {
}
}
})
.child(
ScrollArea::new(scroll_area_id)
.handle(scroll.read(cx).clone())
.h_full()
.child(
v_flex()
.gap_0p5()
.when(padded, |content| content.p_2())
.children(children),
.children(list_children),
),
)
.into_any_element(),
.when(!custom_children.is_empty(), |sidebar| {
sidebar.child(
v_flex()
.flex_1()
.min_h_0()
.when(padded, |content| content.pl_2())
.children(custom_children),
)
})
.when(material && grain, |sidebar| sidebar.child(grain_overlay()))
}
}
+69 -40
View File
@@ -26,12 +26,14 @@ use gpui::prelude::FluentBuilder;
use gpui::{
AnyElement, App, ElementId, FocusHandle, InteractiveElement as _, IntoElement, KeyDownEvent,
ParentElement as _, RenderOnce, Role, SharedString, StatefulInteractiveElement as _,
StyleRefinement, Styled as _, Window, div, rems,
StyleRefinement, Styled as _, UniformListScrollHandle, Window, div, rems, uniform_list,
};
use crate::common::{focus_handle, h_flex};
use crate::components::common::{
StyledExt as _, nav_key, paint_selectable_row, selectable_label_color, v_flex,
};
use crate::components::scrollbar::Scrollbar;
use crate::icons::{Icon, IconRole, icon};
use crate::theme::ActiveTheme as _;
@@ -138,15 +140,14 @@ impl TreeItem {
let depth = self.depth.min(TREE_MAX_DEPTH) as f32;
let indent = rems(0.75 * if self.depth == 0 { 0.5 } else { depth + 1.0 });
div()
h_flex()
.id((self.key.clone(), "tree-item"))
.relative()
.flex()
.items_center()
.gap_2()
.pl(indent)
.pr_2()
.py_1()
.w_full()
.min_h_8()
.rounded(theme.radius.sm)
.role(Role::TreeItem)
@@ -551,10 +552,21 @@ impl gpui::Styled for Tree {
impl RenderOnce for Tree {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let items = match self.items {
let Tree {
id,
selected,
a11y_label,
on_change,
on_disclosure,
items,
style,
} = self;
let items = match items {
Some(build) => build(window, cx),
None => Arc::default(),
};
let count = items.len();
let selectable: Vec<usize> = items
.iter()
@@ -563,49 +575,41 @@ impl RenderOnce for Tree {
.map(|(index, _)| index)
.collect();
let focus = crate::components::common::focus_handle(&self.id, window, cx);
let label = self
.a11y_label
.clone()
.unwrap_or_else(|| SharedString::from(self.id.to_string()));
let focus = focus_handle(&id, window, cx);
let label = a11y_label.unwrap_or_else(|| SharedString::from(id.to_string()));
let sibling_sets = Self::sibling_sets(&items);
let rows: Vec<AnyElement> = items
.iter()
.enumerate()
.map(|(index, item)| {
let (position_in_set, size_of_set) = sibling_sets[index];
item.render_row(
cx,
index,
position_in_set,
size_of_set,
self.selected == Some(index),
&self.on_change,
&self.on_disclosure,
&focus,
)
})
.collect();
let scroll = window.with_element_namespace(id.clone(), |window| {
window.use_keyed_state("scroll", cx, |_, _| UniformListScrollHandle::new())
});
let scroll_handle = scroll.read(cx).clone();
let rows_id = (id.clone(), "rows");
let scroll_id = (id.clone(), "scroll");
let items_for_rows = Arc::clone(&items);
let on_change_for_rows = on_change.clone();
let on_disclosure_for_rows = on_disclosure.clone();
let focus_for_rows = focus.clone();
// Padding insets the rows, leaving the scrollbar on the tree's edge.
let mut style = style;
let row_style = StyleRefinement {
padding: std::mem::take(&mut style.padding),
..Default::default()
};
v_flex()
.id(self.id)
.gap_0p5()
.relative()
.id(id)
.text_sm()
.refine_style(&self.style)
.refine_style(&style)
.size_full()
.track_focus(&focus)
.focusable()
.tab_stop(true)
.role(Role::Tree)
.aria_label(label)
.on_key_down({
let selected = self.selected;
let on_change = self.on_change.clone();
let on_disclosure = self.on_disclosure.clone();
move |event: &KeyDownEvent, window, cx| {
.on_key_down(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);
@@ -647,8 +651,33 @@ impl RenderOnce for Tree {
_ => return,
}
cx.stop_propagation();
}
})
.children(rows)
.child(
uniform_list(rows_id, count, move |range, _, cx| {
range
.map(|index| {
let Some(item) = items_for_rows.get(index) else {
return div().into_any_element();
};
let (position_in_set, size_of_set) = sibling_sets[index];
item.render_row(
cx,
index,
position_in_set,
size_of_set,
selected == Some(index),
&on_change_for_rows,
&on_disclosure_for_rows,
&focus_for_rows,
)
})
.collect()
})
.track_scroll(&scroll_handle)
.refine_style(&row_style)
.size_full(),
)
.child(Scrollbar::vertical(scroll_id, scroll_handle))
}
}