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 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 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 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 ## v0.2.0 - 2026/10/21
+31 -35
View File
@@ -260,42 +260,38 @@ impl RepoFilesView {
let change_view = view.clone(); let change_view = view.clone();
let disclosure_view = view; let disclosure_view = view;
div() // Mirrors the sidebar's left inset, so rows stay aligned while the scrollbar hugs its border.
.flex_1() Tree::new("repo-tree")
.min_h_0() .pr_2()
.w_full() .selected(selected)
.child( .items(move |_, _| Arc::clone(&rows))
Tree::new("repo-tree") .on_change({
.selected(selected) let view = change_view;
.items(move |_, _| Arc::clone(&rows)) move |index, window, cx| {
.on_change({ if let Some(index) = index
let view = change_view; && let Err(error) = view.update(cx, |this, cx| {
move |index, window, cx| { this.activate_row(index, window, cx);
if let Some(index) = index })
&& let Err(error) = view.update(cx, |this, cx| { {
this.activate_row(index, window, cx); log::warn!("failed to activate tree row: {error}");
}) }
{ }
log::warn!("failed to activate tree row: {error}"); })
} .on_disclosure({
let view = disclosure_view;
move |index, _, cx| {
if let Err(error) = view.update(cx, |this, cx| {
let id = this.rows.get(index).map(|row| row.key().to_string());
if let Some(id) = id {
this.toggle_folder(&id);
cx.emit(RepoFilesEvent::Activated);
cx.notify();
} }
}) }) {
.on_disclosure({ log::warn!("failed to toggle folder: {error}");
let view = disclosure_view; }
move |index, _, cx| { }
if let Err(error) = view.update(cx, |this, cx| { })
let id = this.rows.get(index).map(|row| row.key().to_string());
if let Some(id) = id {
this.toggle_folder(&id);
cx.emit(RepoFilesEvent::Activated);
cx.notify();
}
}) {
log::warn!("failed to toggle folder: {error}");
}
}
}),
)
.into_any_element() .into_any_element()
} }
+33 -31
View File
@@ -6,9 +6,8 @@ use anyhow::Error;
use gpui::prelude::FluentBuilder as _; use gpui::prelude::FluentBuilder as _;
use gpui::{ use gpui::{
AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
ParentElement as _, PathPromptOptions, Render, SharedString, StatefulInteractiveElement as _, ParentElement as _, PathPromptOptions, Render, SharedString, StyleRefinement, Styled as _,
StyleRefinement, Styled as _, Subscription, SystemNotification, Task, WeakEntity, Window, div, Subscription, SystemNotification, Task, WeakEntity, Window, div, px,
px,
}; };
use metadata::ProfileDirectory; use metadata::ProfileDirectory;
use nostr::prelude::{PublicKey, Url}; use nostr::prelude::{PublicKey, Url};
@@ -1441,42 +1440,45 @@ impl RepoDetailView {
let commit_id = commit.id.clone(); let commit_id = commit.id.clone();
Some( Some(
h_flex() Button::new("last-commit")
.id("last-commit") .variant(ButtonVariant::Ghost)
.h_8() .fully_rounded(true)
.px_2()
.gap_2()
.min_w_0()
.rounded(cx.theme().radius.control)
.hover(|this| this.bg(cx.theme().colors.muted_hover))
.tooltip(Tooltip::text("View latest commit")) .tooltip(Tooltip::text("View latest commit"))
.on_click(move |_, window, cx| { .on_click(move |_, window, cx| {
history.update(cx, |history, cx| { history.update(cx, |history, cx| {
history.open_commit_diff(&commit_id, window, cx); history.open_commit_diff(&commit_id, window, cx);
}); });
}) })
.child(Icon::named(IconRole::GitCommit).color(cx.theme().colors.muted_foreground))
.child( .child(
div() h_flex()
.font_family(cx.theme().mono_font) .w_full()
.text_sm() .gap_2()
.text_color(cx.theme().colors.muted_foreground) .child(
.child(SharedString::from(short)), Icon::named(IconRole::GitCommit)
) .color(cx.theme().colors.muted_foreground),
.child( )
div() .child(
.max_w(px(220.)) div()
.text_sm() .text_sm()
.text_ellipsis() .text_color(cx.theme().colors.muted_foreground)
.whitespace_nowrap() .child(SharedString::from(&short)),
.child(commit.summary.clone()), )
) .child(
.child( div()
div() .max_w(px(220.))
.text_sm() .text_sm()
.text_color(cx.theme().colors.muted_foreground) .text_ellipsis()
.child(relative_time_secs(commit.time)), .whitespace_nowrap()
.child(SharedString::from(&commit.summary)),
)
.child(
div()
.text_sm()
.text_color(cx.theme().colors.muted_foreground)
.child(SharedString::from(relative_time_secs(commit.time))),
),
) )
.flex_1()
.into_any_element(), .into_any_element(),
) )
} }
+46 -45
View File
@@ -29,7 +29,6 @@ use crate::components::common::{
StyledExt as _, nav_key, paint_selectable_row, selectable_label_color, StyledExt as _, nav_key, paint_selectable_row, selectable_label_color,
}; };
use crate::components::grain::grain as grain_overlay; use crate::components::grain::grain as grain_overlay;
use crate::components::scroll_area::ScrollArea;
use crate::theme::{ActiveTheme as _, BORDER_WIDTH, SIDEBAR_DEFAULT_WIDTH}; use crate::theme::{ActiveTheme as _, BORDER_WIDTH, SIDEBAR_DEFAULT_WIDTH};
/// The change reporter, fired with the next selected row by pointer and keys. /// The change reporter, fired with the next selected row by pointer and keys.
@@ -397,47 +396,47 @@ impl RenderOnce for Sidebar {
.collect(); .collect();
let focus = crate::components::common::focus_handle(&id, window, cx); 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 let row_count = entries
.iter() .iter()
.filter(|entry| matches!(entry, SidebarEntry::Row { .. })) .filter(|entry| matches!(entry, SidebarEntry::Row { .. }))
.count(); .count();
let mut row_position = 0usize; let mut row_position = 0usize;
let mut list_children = Vec::new();
let mut custom_children = Vec::new();
let children: Vec<AnyElement> = entries for (index, entry) in entries.into_iter().enumerate() {
.into_iter() let is_row = matches!(entry, SidebarEntry::Row { .. });
.enumerate() let is_custom = matches!(entry, SidebarEntry::Custom(_));
.map(|(index, entry)| {
let is_row = matches!(entry, SidebarEntry::Row { .. });
if is_row { if is_row {
row_position += 1; row_position += 1;
} }
let (position_in_set, size_of_set) = if is_row { let (position_in_set, size_of_set) = if is_row {
(row_position, row_count) (row_position, row_count)
} else { } else {
(0, 0) (0, 0)
}; };
entry.render_entry( let rendered = entry.render_entry(
cx, cx,
index, index,
position_in_set, position_in_set,
size_of_set, size_of_set,
selected == Some(index), selected == Some(index),
material, material,
&on_change, &on_change,
&focus, &focus,
) );
})
.collect(); if is_custom {
custom_children.push(rendered);
} else {
list_children.push(rendered);
}
}
let theme = cx.theme(); let theme = cx.theme();
@@ -456,8 +455,7 @@ impl RenderOnce for Sidebar {
.child( .child(
div() div()
.id((id.clone(), "list")) .id((id.clone(), "list"))
.flex_1() .flex_none()
.min_h_0()
.track_focus(&focus) .track_focus(&focus)
.focusable() .focusable()
.tab_stop(true) .tab_stop(true)
@@ -476,18 +474,21 @@ impl RenderOnce for Sidebar {
} }
}) })
.child( .child(
ScrollArea::new(scroll_area_id) v_flex()
.handle(scroll.read(cx).clone()) .gap_0p5()
.h_full() .when(padded, |content| content.p_2())
.child( .children(list_children),
v_flex() ),
.gap_0p5()
.when(padded, |content| content.p_2())
.children(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())) .when(material && grain, |sidebar| sidebar.child(grain_overlay()))
} }
} }
+107 -78
View File
@@ -26,12 +26,14 @@ use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
AnyElement, App, ElementId, FocusHandle, InteractiveElement as _, IntoElement, KeyDownEvent, AnyElement, App, ElementId, FocusHandle, InteractiveElement as _, IntoElement, KeyDownEvent,
ParentElement as _, RenderOnce, Role, SharedString, StatefulInteractiveElement as _, 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::{ use crate::components::common::{
StyledExt as _, nav_key, paint_selectable_row, selectable_label_color, v_flex, 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::icons::{Icon, IconRole, icon};
use crate::theme::ActiveTheme as _; use crate::theme::ActiveTheme as _;
@@ -138,15 +140,14 @@ impl TreeItem {
let depth = self.depth.min(TREE_MAX_DEPTH) as f32; 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 }); let indent = rems(0.75 * if self.depth == 0 { 0.5 } else { depth + 1.0 });
div() h_flex()
.id((self.key.clone(), "tree-item")) .id((self.key.clone(), "tree-item"))
.relative() .relative()
.flex()
.items_center()
.gap_2() .gap_2()
.pl(indent) .pl(indent)
.pr_2() .pr_2()
.py_1() .py_1()
.w_full()
.min_h_8() .min_h_8()
.rounded(theme.radius.sm) .rounded(theme.radius.sm)
.role(Role::TreeItem) .role(Role::TreeItem)
@@ -551,10 +552,21 @@ impl gpui::Styled for Tree {
impl RenderOnce for Tree { impl RenderOnce for Tree {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { 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), Some(build) => build(window, cx),
None => Arc::default(), None => Arc::default(),
}; };
let count = items.len();
let selectable: Vec<usize> = items let selectable: Vec<usize> = items
.iter() .iter()
@@ -563,92 +575,109 @@ impl RenderOnce for Tree {
.map(|(index, _)| index) .map(|(index, _)| index)
.collect(); .collect();
let focus = crate::components::common::focus_handle(&self.id, window, cx); let focus = focus_handle(&id, window, cx);
let label = a11y_label.unwrap_or_else(|| SharedString::from(id.to_string()));
let label = self
.a11y_label
.clone()
.unwrap_or_else(|| SharedString::from(self.id.to_string()));
let sibling_sets = Self::sibling_sets(&items); let sibling_sets = Self::sibling_sets(&items);
let rows: Vec<AnyElement> = items let scroll = window.with_element_namespace(id.clone(), |window| {
.iter() window.use_keyed_state("scroll", cx, |_, _| UniformListScrollHandle::new())
.enumerate() });
.map(|(index, item)| { let scroll_handle = scroll.read(cx).clone();
let (position_in_set, size_of_set) = sibling_sets[index];
item.render_row( let rows_id = (id.clone(), "rows");
cx, let scroll_id = (id.clone(), "scroll");
index, let items_for_rows = Arc::clone(&items);
position_in_set, let on_change_for_rows = on_change.clone();
size_of_set, let on_disclosure_for_rows = on_disclosure.clone();
self.selected == Some(index), let focus_for_rows = focus.clone();
&self.on_change,
&self.on_disclosure, // Padding insets the rows, leaving the scrollbar on the tree's edge.
&focus, let mut style = style;
) let row_style = StyleRefinement {
}) padding: std::mem::take(&mut style.padding),
.collect(); ..Default::default()
};
v_flex() v_flex()
.id(self.id) .relative()
.gap_0p5() .id(id)
.text_sm() .text_sm()
.refine_style(&self.style) .refine_style(&style)
.size_full()
.track_focus(&focus) .track_focus(&focus)
.focusable() .focusable()
.tab_stop(true) .tab_stop(true)
.role(Role::Tree) .role(Role::Tree)
.aria_label(label) .aria_label(label)
.on_key_down({ .on_key_down(move |event: &KeyDownEvent, window, cx| {
let selected = self.selected; if nav_key(&selectable, selected, &event.keystroke.key, |next| {
let on_change = self.on_change.clone(); if let Some(on_change) = &on_change {
let on_disclosure = self.on_disclosure.clone(); on_change(next, window, cx);
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();
return;
}
match event.keystroke.key.as_str() {
"left" | "right" => {
let expanding = event.keystroke.key == "right";
let mut disclose = None;
let mut target = None;
if let Some(index) = selected
&& let Some(item) = items.get(index)
&& !item.disabled
{
if item.expandable && item.expanded != expanding {
disclose = Some(index);
} else if expanding {
target = Self::first_descendant(&items, &selectable, index);
} else {
target = Self::parent_of(&items, index)
.filter(|parent| selectable.contains(parent));
}
}
if let Some(index) = disclose
&& let Some(on_disclosure) = &on_disclosure
{
on_disclosure(index, window, cx);
} else if let Some(next) = target
&& let Some(on_change) = &on_change
{
on_change(Some(next), window, cx);
}
}
_ => return,
} }
}) {
cx.stop_propagation(); cx.stop_propagation();
return;
} }
match event.keystroke.key.as_str() {
"left" | "right" => {
let expanding = event.keystroke.key == "right";
let mut disclose = None;
let mut target = None;
if let Some(index) = selected
&& let Some(item) = items.get(index)
&& !item.disabled
{
if item.expandable && item.expanded != expanding {
disclose = Some(index);
} else if expanding {
target = Self::first_descendant(&items, &selectable, index);
} else {
target = Self::parent_of(&items, index)
.filter(|parent| selectable.contains(parent));
}
}
if let Some(index) = disclose
&& let Some(on_disclosure) = &on_disclosure
{
on_disclosure(index, window, cx);
} else if let Some(next) = target
&& let Some(on_change) = &on_change
{
on_change(Some(next), window, cx);
}
}
_ => 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))
} }
} }