feat: pr file tree item switching

This commit is contained in:
2026-05-28 22:28:59 +01:00
parent 75c3b73665
commit 101b144f53
6 changed files with 201 additions and 109 deletions

View File

@@ -28,6 +28,7 @@ pub(crate) struct FileTree {
pub(crate) struct Item {
item: util::file::FileTreeItem,
is_expanded: bool,
is_highlighed: bool,
on_click: Box<dyn Fn(&mut gpui::Window, &mut gpui::App) + 'static>,
}
@@ -38,6 +39,7 @@ struct FileTreeStateInner {
list_state: gpui::ListState,
collapsed_dirs: HashSet<Arc<str>>,
visible_items: Vec<usize>,
highlighted_items: HashSet<usize>,
}
pub(crate) fn file_tree(
@@ -57,6 +59,7 @@ impl FileTreeState {
list_state: gpui::ListState::new(0, gpui::ListAlignment::Top, px(50.)),
collapsed_dirs: HashSet::new(),
visible_items: Vec::new(),
highlighted_items: HashSet::new(),
})))
}
@@ -66,6 +69,12 @@ impl FileTreeState {
state.visible_items = (0..items.len()).collect::<Vec<_>>();
}
pub(crate) fn highlight_item(&mut self, index: usize) {
let mut state = self.0.borrow_mut();
state.highlighted_items.clear();
state.highlighted_items.insert(index);
}
pub(crate) fn toggle_directory(
&mut self,
dir_path: &Arc<str>,
@@ -125,6 +134,7 @@ impl gpui::RenderOnce for FileTree {
let item = (self.item)(item_index, window, cx);
let item = Item {
is_expanded: !state.collapsed_dirs.contains(&item.full_path),
is_highlighed: state.highlighted_items.contains(&item_index),
item,
on_click: Box::new(move |window, cx| {
if let Some(f) = &on_item_click {
@@ -153,7 +163,13 @@ impl gpui::RenderOnce for Item {
.py_0p5()
.pl(px(8. + (8 * self.item.level) as f32))
.rounded_sm()
.hover(|it| it.bg(theme.colors.surface_hover))
.hover(|it| {
if self.is_highlighed {
it
} else {
it.bg(theme.colors.surface_hover)
}
})
.on_click(move |_, window, cx| (self.on_click)(window, cx))
.when(
matches!(self.item.kind, FileTreeItemKind::Directory),
@@ -171,6 +187,10 @@ impl gpui::RenderOnce for Item {
.when(matches!(self.item.kind, FileTreeItemKind::File), |it| {
it.child(font_icon(FontIcon::FileBracesCorner).size_3())
})
.when(self.is_highlighed, |it| {
it.bg(theme.colors.accent_muted)
.text_color(theme.colors.accent_fg)
})
.child(text(gpui::SharedString::new(self.item.name)).text_sm())
}
}