feat: use uniform list for tree component
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -260,42 +260,38 @@ impl RepoFilesView {
|
||||
let change_view = view.clone();
|
||||
let disclosure_view = view;
|
||||
|
||||
div()
|
||||
.flex_1()
|
||||
.min_h_0()
|
||||
.w_full()
|
||||
.child(
|
||||
Tree::new("repo-tree")
|
||||
.selected(selected)
|
||||
.items(move |_, _| Arc::clone(&rows))
|
||||
.on_change({
|
||||
let view = change_view;
|
||||
move |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}");
|
||||
}
|
||||
// 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({
|
||||
let view = change_view;
|
||||
move |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}");
|
||||
}
|
||||
}
|
||||
})
|
||||
.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({
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
}) {
|
||||
log::warn!("failed to toggle folder: {error}");
|
||||
}
|
||||
}
|
||||
})
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
|
||||
@@ -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,42 +1440,45 @@ 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(
|
||||
div()
|
||||
.font_family(cx.theme().mono_font)
|
||||
.text_sm()
|
||||
.text_color(cx.theme().colors.muted_foreground)
|
||||
.child(SharedString::from(short)),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.max_w(px(220.))
|
||||
.text_sm()
|
||||
.text_ellipsis()
|
||||
.whitespace_nowrap()
|
||||
.child(commit.summary.clone()),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.text_sm()
|
||||
.text_color(cx.theme().colors.muted_foreground)
|
||||
.child(relative_time_secs(commit.time)),
|
||||
h_flex()
|
||||
.w_full()
|
||||
.gap_2()
|
||||
.child(
|
||||
Icon::named(IconRole::GitCommit)
|
||||
.color(cx.theme().colors.muted_foreground),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.text_sm()
|
||||
.text_color(cx.theme().colors.muted_foreground)
|
||||
.child(SharedString::from(&short)),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.max_w(px(220.))
|
||||
.text_sm()
|
||||
.text_ellipsis()
|
||||
.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(),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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,47 +396,47 @@ 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 mut list_children = Vec::new();
|
||||
let mut custom_children = Vec::new();
|
||||
|
||||
let children: Vec<AnyElement> = entries
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(index, entry)| {
|
||||
let is_row = matches!(entry, SidebarEntry::Row { .. });
|
||||
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;
|
||||
}
|
||||
if is_row {
|
||||
row_position += 1;
|
||||
}
|
||||
|
||||
let (position_in_set, size_of_set) = if is_row {
|
||||
(row_position, row_count)
|
||||
} else {
|
||||
(0, 0)
|
||||
};
|
||||
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();
|
||||
let rendered = entry.render_entry(
|
||||
cx,
|
||||
index,
|
||||
position_in_set,
|
||||
size_of_set,
|
||||
selected == Some(index),
|
||||
material,
|
||||
&on_change,
|
||||
&focus,
|
||||
);
|
||||
|
||||
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)
|
||||
@@ -476,18 +474,21 @@ 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),
|
||||
),
|
||||
)
|
||||
.into_any_element(),
|
||||
v_flex()
|
||||
.gap_0p5()
|
||||
.when(padded, |content| content.p_2())
|
||||
.children(list_children),
|
||||
),
|
||||
)
|
||||
.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()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,92 +575,109 @@ 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| {
|
||||
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,
|
||||
.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);
|
||||
}
|
||||
}) {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user