diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs index 8adea5ca12b..7fece4d3474 100644 --- a/crates/git_ui/src/git_panel.rs +++ b/crates/git_ui/src/git_panel.rs @@ -1299,10 +1299,7 @@ fn repository_display_name( pub struct GitPanel { pub(crate) active_repository: Option>, pub(crate) commit_editor: Entity, - /// Whether the commit editor should fill the vertical height of the panel. - commit_editor_expanded: bool, - /// Whether the commit editor is hidden, leaving only the commit button row. - commit_editor_collapsed: bool, + commit_editor_layout: CommitEditorLayout, /// Set once the user toggles the editor here, so an untouched workspace keeps /// following `git_panel.commit_editor` instead of pinning its first value. commit_editor_toggled: bool, @@ -1380,6 +1377,40 @@ pub struct GitPanel { change_entry_indices: HashMap, } +/// The layout of the commit editor within the Git panel +#[derive(Debug, PartialEq)] +enum CommitEditorLayout { + Embedded, + Expanded, + Collapsed, +} + +impl CommitEditorLayout { + fn is_expanded(&self) -> bool { + matches!(self, CommitEditorLayout::Expanded) + } + + fn is_collapsed(&self) -> bool { + matches!(self, CommitEditorLayout::Collapsed) + } + + fn editor_mode(&self) -> EditorMode { + match self { + CommitEditorLayout::Expanded => EditorMode::Full { + scale_ui_elements_with_buffer_font_size: false, + show_active_line_background: false, + sizing_behavior: SizingBehavior::ExcludeOverscrollMargin, + }, + CommitEditorLayout::Embedded | CommitEditorLayout::Collapsed => { + EditorMode::AutoHeight { + min_lines: MAX_PANEL_EDITOR_LINES, + max_lines: Some(MAX_PANEL_EDITOR_LINES), + } + } + } + } +} + #[derive(Clone, Debug, PartialEq, Eq)] struct BulkStaging { repo_id: RepositoryId, @@ -1553,7 +1584,7 @@ impl GitPanel { // whatever this workspace last persisted. if commit_editor != was_commit_editor { let collapsed = commit_editor == GitPanelCommitEditor::Collapsed; - if collapsed != this.commit_editor_collapsed { + if collapsed != this.commit_editor_layout.is_collapsed() { this.toggle_commit_editor(&Default::default(), window, cx); } } @@ -1715,8 +1746,11 @@ impl GitPanel { let mut this = Self { active_repository, commit_editor, - commit_editor_expanded: false, - commit_editor_collapsed, + commit_editor_layout: if commit_editor_collapsed { + CommitEditorLayout::Collapsed + } else { + CommitEditorLayout::Embedded + }, commit_editor_toggled: commit_editor_was_serialized, conflicted_count: 0, conflicted_staged_count: 0, @@ -2279,7 +2313,7 @@ impl GitPanel { let signoff_enabled = self.signoff_enabled; let commit_editor_collapsed = self .commit_editor_toggled - .then_some(self.commit_editor_collapsed); + .then_some(self.commit_editor_layout.is_collapsed()); let commit_messages = self.serialized_commit_messages(cx); let kvp = KeyValueStore::global(cx); @@ -2824,7 +2858,7 @@ impl GitPanel { return; } self.set_active_tab(GitPanelTab::Changes, window, cx); - if self.commit_editor_collapsed { + if self.commit_editor_layout.is_collapsed() { self.toggle_commit_editor(&ToggleCommitEditor, window, cx); } self.commit_editor.update(cx, |editor, cx| { @@ -7158,38 +7192,44 @@ impl GitPanel { _window: &mut Window, cx: &mut Context, ) { - self.commit_editor_expanded = !self.commit_editor_expanded; - self.commit_editor.update(cx, |editor, _cx| { - if self.commit_editor_expanded { - editor.set_mode(EditorMode::Full { - scale_ui_elements_with_buffer_font_size: false, - show_active_line_background: false, - sizing_behavior: SizingBehavior::ExcludeOverscrollMargin, - }) - } else { - editor.set_mode(EditorMode::AutoHeight { - min_lines: MAX_PANEL_EDITOR_LINES, - max_lines: Some(MAX_PANEL_EDITOR_LINES), - }) + let was_collapsed = self.commit_editor_layout.is_collapsed(); + self.commit_editor_layout = match self.commit_editor_layout { + CommitEditorLayout::Expanded => CommitEditorLayout::Embedded, + CommitEditorLayout::Embedded | CommitEditorLayout::Collapsed => { + CommitEditorLayout::Expanded } + }; + self.commit_editor.update(cx, |editor, _cx| { + editor.set_mode(self.commit_editor_layout.editor_mode()) }); - + if was_collapsed { + self.commit_editor_toggled = true; + self.serialize(cx); + } cx.notify(); } fn toggle_commit_editor( &mut self, _: &ToggleCommitEditor, - window: &mut Window, + _window: &mut Window, cx: &mut Context, ) { - self.commit_editor_collapsed = !self.commit_editor_collapsed; + let was_expanded = self.commit_editor_layout.is_expanded(); + self.commit_editor_layout = match self.commit_editor_layout { + CommitEditorLayout::Collapsed => CommitEditorLayout::Embedded, + CommitEditorLayout::Embedded | CommitEditorLayout::Expanded => { + CommitEditorLayout::Collapsed + } + }; self.commit_editor_toggled = true; - // Filling the panel and being hidden are mutually exclusive states, so - // collapsing has to undo the fill rather than leave it dangling. - if self.commit_editor_collapsed && self.commit_editor_expanded { - self.toggle_fill_commit_editor(&Default::default(), window, cx); + + if was_expanded { + self.commit_editor.update(cx, |editor, _cx| { + editor.set_mode(self.commit_editor_layout.editor_mode()); + }); } + self.serialize(cx); cx.notify(); } @@ -7462,7 +7502,7 @@ impl GitPanel { })), ) .child({ - let (icon, label) = if self.commit_editor_expanded { + let (icon, label) = if self.commit_editor_layout.is_expanded() { (IconName::Minimize, "Collapse Commit Editor") } else { (IconName::Maximize, "Expand Commit Editor") @@ -7490,7 +7530,7 @@ impl GitPanel { })) }); - let collapsed = self.commit_editor_collapsed; + let collapsed = self.commit_editor_layout.is_collapsed(); let toggle_commit_editor_button = { let (icon, label) = if collapsed { (IconName::ChevronUp, "Show Commit Editor") @@ -7510,7 +7550,9 @@ impl GitPanel { }; let footer = v_flex() - .when(self.commit_editor_expanded, |this| this.flex_1().min_h_0()) + .when(self.commit_editor_layout.is_expanded(), |this| { + this.flex_1().min_h_0() + }) .child(PanelRepoFooter::new( display_name, branch, @@ -7543,7 +7585,9 @@ impl GitPanel { panel_editor_container(window, cx) .id("commit-editor-container") .w_full() - .when(self.commit_editor_expanded, |this| this.flex_1().min_h_0()) + .when(self.commit_editor_layout.is_expanded(), |this| { + this.flex_1().min_h_0() + }) .border_t_1() .border_color(if title_exceeds_limit { cx.theme().status().warning_border @@ -10208,7 +10252,7 @@ impl Render for GitPanel { .map(|this| match self.active_tab { GitPanelTab::Changes => this .children(self.render_changes_header(window, cx)) - .when(!self.commit_editor_expanded, |this| { + .when(!self.commit_editor_layout.is_expanded(), |this| { this.map(|this| { if let Some(repo) = self.active_repository.clone() && has_entries @@ -10284,10 +10328,14 @@ impl editor::Addon for GitPanelAddon { impl Panel for GitPanel { fn activation_focus_handle(&self, cx: &App) -> FocusHandle { + let focus_commit_editor = match self.commit_editor_layout { + CommitEditorLayout::Collapsed => false, + CommitEditorLayout::Expanded => true, + CommitEditorLayout::Embedded => self.entries.is_empty(), + }; if self.active_tab == GitPanelTab::Changes && self.active_repository.is_some() - && !self.commit_editor_collapsed - && (self.entries.is_empty() || self.commit_editor_expanded) + && focus_commit_editor { self.commit_editor.focus_handle(cx) } else { @@ -15933,21 +15981,21 @@ mod tests { let panel = workspace.update_in(cx, GitPanel::new); panel.update_in(cx, |panel, window, cx| { - assert!(!panel.commit_editor_expanded); + assert_eq!(panel.commit_editor_layout, CommitEditorLayout::Embedded); assert!(matches!( panel.commit_editor.read(cx).mode().clone(), EditorMode::AutoHeight { .. } )); panel.toggle_fill_commit_editor(&ToggleFillCommitEditor, window, cx); - assert!(panel.commit_editor_expanded); + assert_eq!(panel.commit_editor_layout, CommitEditorLayout::Expanded); assert!(matches!( panel.commit_editor.read(cx).mode().clone(), EditorMode::Full { .. } )); panel.toggle_fill_commit_editor(&ToggleFillCommitEditor, window, cx); - assert!(!panel.commit_editor_expanded); + assert_eq!(panel.commit_editor_layout, CommitEditorLayout::Embedded); assert!(matches!( panel.commit_editor.read(cx).mode().clone(), EditorMode::AutoHeight { .. } @@ -15977,25 +16025,75 @@ mod tests { let panel = workspace.update_in(cx, GitPanel::new); panel.update_in(cx, |panel, window, cx| { - assert!(!panel.commit_editor_collapsed); + assert_eq!(panel.commit_editor_layout, CommitEditorLayout::Embedded); panel.toggle_commit_editor(&ToggleCommitEditor, window, cx); - assert!(panel.commit_editor_collapsed); + assert_eq!(panel.commit_editor_layout, CommitEditorLayout::Collapsed); panel.toggle_commit_editor(&ToggleCommitEditor, window, cx); - assert!(!panel.commit_editor_collapsed); + assert_eq!(panel.commit_editor_layout, CommitEditorLayout::Embedded); panel.toggle_fill_commit_editor(&ToggleFillCommitEditor, window, cx); - assert!(panel.commit_editor_expanded); + assert_eq!(panel.commit_editor_layout, CommitEditorLayout::Expanded); - // Collapsing has to undo the fill, the two states can't both hold. panel.toggle_commit_editor(&ToggleCommitEditor, window, cx); - assert!(panel.commit_editor_collapsed); - assert!(!panel.commit_editor_expanded); + assert_eq!(panel.commit_editor_layout, CommitEditorLayout::Collapsed); assert!(matches!( panel.commit_editor.read(cx).mode().clone(), EditorMode::AutoHeight { .. } )); + + panel.toggle_fill_commit_editor(&ToggleFillCommitEditor, window, cx); + assert_eq!(panel.commit_editor_layout, CommitEditorLayout::Expanded); + assert!(matches!( + panel.commit_editor.read(cx).mode().clone(), + EditorMode::Full { .. } + )); + }); + } + + #[gpui::test] + async fn test_toggle_fill_commit_editor_from_collapsed_persists_visibility( + cx: &mut TestAppContext, + ) { + init_test(cx); + let fs = FakeFs::new(cx.background_executor.clone()); + fs.insert_tree( + "/root", + json!({ "project": { ".git": {}, "src": { "main.rs": "fn main() {}" } } }), + ) + .await; + + let project = Project::test(fs.clone(), [Path::new(path!("/root/project"))], cx).await; + let window_handle = + cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx)); + let workspace = window_handle + .read_with(cx, |mw, _| mw.workspace().clone()) + .unwrap(); + workspace.update(cx, |workspace, _cx| { + workspace.set_random_database_id(); + }); + let cx = &mut VisualTestContext::from_window(window_handle.into(), cx); + cx.executor().run_until_parked(); + + let panel = workspace.update_in(cx, GitPanel::new); + + panel.update_in(cx, |panel, window, cx| { + panel.toggle_commit_editor(&ToggleCommitEditor, window, cx); + panel.toggle_fill_commit_editor(&ToggleFillCommitEditor, window, cx); + panel.focus_editor(&FocusEditor, window, cx); + }); + cx.executor().advance_clock(SERIALIZATION_THROTTLE_TIME); + cx.run_until_parked(); + + let async_cx = cx.update(|window, cx| window.to_async(cx)); + let restored_panel = GitPanel::load(workspace.downgrade(), async_cx) + .await + .expect("panel load should succeed"); + cx.run_until_parked(); + + restored_panel.read_with(cx, |panel, _| { + assert_eq!(panel.commit_editor_layout, CommitEditorLayout::Embedded); }); } @@ -16022,7 +16120,7 @@ mod tests { // `git_panel::ToggleFocus` to focus on the panel itself. panel.update_in(&mut cx, |panel, _window, cx| { assert!(!panel.entries.is_empty()); - assert!(!panel.commit_editor_expanded); + assert_eq!(panel.commit_editor_layout, CommitEditorLayout::Embedded); assert_eq!(panel.focus_handle(cx), panel.focus_handle.clone()); }); @@ -16031,7 +16129,7 @@ mod tests { // expanded. panel.update_in(&mut cx, |panel, window, cx| { panel.toggle_fill_commit_editor(&ToggleFillCommitEditor, window, cx); - assert!(panel.commit_editor_expanded); + assert_eq!(panel.commit_editor_layout, CommitEditorLayout::Expanded); }); cx.dispatch_action(super::ToggleFocus);