Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

git_panel: Add collapsable sections (#62441)

Sathwik Chirivellibd1b83a43a6c
diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs
index 3f59188947a..cb92cacfcd3 100644
--- a/crates/git_ui/src/git_panel.rs
+++ b/crates/git_ui/src/git_panel.rs
@@ -936,6 +936,7 @@ pub struct GitPanel {
add_coauthors: bool,
generate_commit_message_task: Option<Task<Option<()>>>,
entries: Vec<GitListEntry>,
+ collapsed_sections: HashSet<Section>,
view_mode: GitPanelViewMode,
tree_expanded_dirs: HashMap<TreeKey, bool>,
projected_entries_by_path: HashMap<RepoPath, SmallVec<[ProjectedChangeEntry; 2]>>,
@@ -1238,6 +1239,7 @@ impl GitPanel {
add_coauthors: true,
generate_commit_message_task: None,
entries: Vec::new(),
+ collapsed_sections: HashSet::default(),
view_mode: GitPanelViewMode::from_settings(cx),
tree_expanded_dirs: HashMap::default(),
projected_entries_by_path: HashMap::default(),
@@ -4807,7 +4809,12 @@ impl GitPanel {
continue;
}
- if section != Section::Tracked || group_by != GitPanelGroupBy::None {
+ let section_has_header =
+ section != Section::Tracked || group_by != GitPanelGroupBy::None;
+ let section_is_collapsed =
+ section_has_header && self.collapsed_sections.contains(&section);
+
+ if section_has_header {
push_entry(
self,
GitListEntry::Header(GitHeaderEntry { header: section }),
@@ -4822,7 +4829,7 @@ impl GitPanel {
self,
GitListEntry::EmptySection(section),
section,
- true,
+ !section_is_collapsed,
Some(&mut tree_state.logical_indices),
);
}
@@ -4834,7 +4841,7 @@ impl GitPanel {
self,
entry,
section,
- is_visible,
+ is_visible && !section_is_collapsed,
Some(&mut tree_state.logical_indices),
);
}
@@ -4943,6 +4950,38 @@ impl GitPanel {
})
}
+ fn visible_flat_entry_indices(&self) -> Vec<usize> {
+ let mut section = None;
+ self.entries
+ .iter()
+ .enumerate()
+ .filter_map(|(index, entry)| {
+ if let GitListEntry::Header(header) = entry {
+ section = Some(header.header);
+ return Some(index);
+ }
+
+ if section.is_some_and(|section| self.collapsed_sections.contains(&section)) {
+ None
+ } else {
+ Some(index)
+ }
+ })
+ .collect()
+ }
+
+ fn toggle_section_collapsed(
+ &mut self,
+ section: Section,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ if !self.collapsed_sections.remove(&section) {
+ self.collapsed_sections.insert(section);
+ }
+ self.update_visible_entries(window, cx);
+ }
+
fn stage_intent_for_entry_index(&self, ix: usize) -> StageIntent {
self.section_for_entry_index(ix)
.map_or(StageIntent::Toggle, StageIntent::for_section)
@@ -7005,7 +7044,7 @@ impl GitPanel {
) -> impl IntoElement {
let (is_tree_view, entry_count) = match &self.view_mode {
GitPanelViewMode::Tree(state) => (true, state.logical_indices.len()),
- GitPanelViewMode::Flat => (false, self.entries.len()),
+ GitPanelViewMode::Flat => (false, self.visible_flat_entry_indices().len()),
};
let repo = repo.downgrade();
@@ -7032,9 +7071,17 @@ impl GitPanel {
let mut items = Vec::with_capacity(range.end - range.start);
- for ix in range.into_iter().map(|ix| match &this.view_mode {
- GitPanelViewMode::Tree(state) => state.logical_indices[ix],
- GitPanelViewMode::Flat => ix,
+ let visible_flat_entry_indices =
+ matches!(this.view_mode, GitPanelViewMode::Flat)
+ .then(|| this.visible_flat_entry_indices());
+ for ix in range.into_iter().filter_map(|ix| match &this.view_mode {
+ GitPanelViewMode::Tree(state) => {
+ state.logical_indices.get(ix).copied()
+ }
+ GitPanelViewMode::Flat => visible_flat_entry_indices
+ .as_ref()
+ .and_then(|indices| indices.get(ix))
+ .copied(),
}) {
match &this.entries.get(ix) {
Some(GitListEntry::Status(entry)) => {
@@ -7145,8 +7192,10 @@ impl GitPanel {
let group_name: SharedString = format!("header_{}", ix).into();
let section = header.header;
let weak = cx.weak_entity();
+ let checkbox_weak = weak.clone();
let stage_intent = StageIntent::for_section(section);
let toggle_state = stage_intent.checkbox_state(|| self.header_state(header.header));
+ let is_collapsed = self.collapsed_sections.contains(&section);
let all_conflicts_resolved = section == Section::Conflict
&& self.conflicted_count > 0
@@ -7166,16 +7215,27 @@ impl GitPanel {
.pr_1()
.gap_2()
.justify_between()
- .when(!section_is_empty && !all_conflicts_resolved, |this| {
- this.cursor_pointer()
- .hover(|s| s.bg(cx.theme().colors().ghost_element_hover))
- })
+ .cursor_pointer()
+ .hover(|style| style.bg(cx.theme().colors().ghost_element_hover))
.border_1()
.border_r_2()
.child(
- Label::new(header.title())
- .color(Color::Muted)
- .size(LabelSize::Small),
+ h_flex()
+ .gap_1()
+ .child(
+ Icon::new(if is_collapsed {
+ IconName::ChevronRight
+ } else {
+ IconName::ChevronDown
+ })
+ .size(IconSize::XSmall)
+ .color(Color::Muted),
+ )
+ .child(
+ Label::new(header.title())
+ .color(Color::Muted)
+ .size(LabelSize::Small),
+ ),
)
.child(if section_is_empty {
gpui::Empty.into_any_element()
@@ -7183,7 +7243,24 @@ impl GitPanel {
let checkbox = Checkbox::new(checkbox_id, toggle_state)
.disabled(!has_write_access || all_conflicts_resolved)
.fill()
- .elevation(ElevationIndex::Surface);
+ .elevation(ElevationIndex::Surface)
+ .on_click(move |_, window, cx| {
+ cx.stop_propagation();
+ if !has_write_access || all_conflicts_resolved {
+ return;
+ }
+
+ checkbox_weak
+ .update(cx, |this, cx| {
+ this.toggle_staged_for_entry(
+ &GitListEntry::Header(GitHeaderEntry { header: section }),
+ stage_intent,
+ window,
+ cx,
+ );
+ })
+ .ok();
+ });
let tooltip_label = if all_conflicts_resolved {
Some("All conflicts marked as resolved")
} else {
@@ -7202,18 +7279,8 @@ impl GitPanel {
}
})
.on_click(move |_, window, cx| {
- if !has_write_access || section_is_empty || all_conflicts_resolved {
- return;
- }
-
weak.update(cx, |this, cx| {
- this.toggle_staged_for_entry(
- &GitListEntry::Header(GitHeaderEntry { header: section }),
- stage_intent,
- window,
- cx,
- );
- cx.stop_propagation();
+ this.toggle_section_collapsed(section, window, cx);
})
.ok();
})