commit_modal.rs

  1// #![allow(unused, dead_code)]
  2
  3use crate::branch_picker::{self, BranchList};
  4use crate::git_panel::{commit_message_editor, GitPanel};
  5use git::{Commit, ExpandCommitEditor};
  6use panel::{panel_button, panel_editor_style, panel_filled_button};
  7use project::Project;
  8use ui::{prelude::*, KeybindingHint, PopoverButton, Tooltip, TriggerablePopover};
  9
 10use editor::{Editor, EditorElement};
 11use gpui::*;
 12use util::ResultExt;
 13use workspace::{
 14    dock::{Dock, PanelHandle},
 15    ModalView, Workspace,
 16};
 17
 18// nate: It is a pain to get editors to size correctly and not overflow.
 19//
 20// this can get replaced with a simple flex layout with more time/a more thoughtful approach.
 21#[derive(Debug, Clone, Copy)]
 22pub struct ModalContainerProperties {
 23    pub modal_width: f32,
 24    pub editor_height: f32,
 25    pub footer_height: f32,
 26    pub container_padding: f32,
 27    pub modal_border_radius: f32,
 28}
 29
 30impl ModalContainerProperties {
 31    pub fn new(window: &Window, preferred_char_width: usize) -> Self {
 32        let container_padding = 5.0;
 33
 34        // Calculate width based on character width
 35        let mut modal_width = 460.0;
 36        let style = window.text_style().clone();
 37        let font_id = window.text_system().resolve_font(&style.font());
 38        let font_size = style.font_size.to_pixels(window.rem_size());
 39
 40        if let Ok(em_width) = window.text_system().em_width(font_id, font_size) {
 41            modal_width = preferred_char_width as f32 * em_width.0 + (container_padding * 2.0);
 42        }
 43
 44        Self {
 45            modal_width,
 46            editor_height: 300.0,
 47            footer_height: 24.0,
 48            container_padding,
 49            modal_border_radius: 12.0,
 50        }
 51    }
 52
 53    pub fn editor_border_radius(&self) -> Pixels {
 54        px(self.modal_border_radius - self.container_padding / 2.0)
 55    }
 56}
 57
 58pub fn init(cx: &mut App) {
 59    cx.observe_new(|workspace: &mut Workspace, window, cx| {
 60        let Some(window) = window else {
 61            return;
 62        };
 63        CommitModal::register(workspace, window, cx)
 64    })
 65    .detach();
 66}
 67
 68pub struct CommitModal {
 69    branch_list: Entity<BranchList>,
 70    git_panel: Entity<GitPanel>,
 71    commit_editor: Entity<Editor>,
 72    restore_dock: RestoreDock,
 73    properties: ModalContainerProperties,
 74}
 75
 76impl Focusable for CommitModal {
 77    fn focus_handle(&self, cx: &App) -> gpui::FocusHandle {
 78        self.commit_editor.focus_handle(cx)
 79    }
 80}
 81
 82impl EventEmitter<DismissEvent> for CommitModal {}
 83impl ModalView for CommitModal {
 84    fn on_before_dismiss(
 85        &mut self,
 86        window: &mut Window,
 87        cx: &mut Context<Self>,
 88    ) -> workspace::DismissDecision {
 89        self.git_panel.update(cx, |git_panel, cx| {
 90            git_panel.set_modal_open(false, cx);
 91        });
 92        self.restore_dock
 93            .dock
 94            .update(cx, |dock, cx| {
 95                if let Some(active_index) = self.restore_dock.active_index {
 96                    dock.activate_panel(active_index, window, cx)
 97                }
 98                dock.set_open(self.restore_dock.is_open, window, cx)
 99            })
100            .log_err();
101        workspace::DismissDecision::Dismiss(true)
102    }
103}
104
105struct RestoreDock {
106    dock: WeakEntity<Dock>,
107    is_open: bool,
108    active_index: Option<usize>,
109}
110
111impl CommitModal {
112    pub fn register(workspace: &mut Workspace, _: &mut Window, _cx: &mut Context<Workspace>) {
113        workspace.register_action(|workspace, _: &ExpandCommitEditor, window, cx| {
114            let Some(git_panel) = workspace.panel::<GitPanel>(cx) else {
115                return;
116            };
117
118            let (can_commit, conflict) = git_panel.update(cx, |git_panel, cx| {
119                let can_commit = git_panel.can_commit();
120                let conflict = git_panel.has_unstaged_conflicts();
121                if can_commit {
122                    git_panel.set_modal_open(true, cx);
123                }
124                (can_commit, conflict)
125            });
126            if !can_commit {
127                let message = if conflict {
128                    "There are still conflicts. You must stage these before committing."
129                } else {
130                    "No changes to commit."
131                };
132                let prompt = window.prompt(PromptLevel::Warning, message, None, &["Ok"], cx);
133                cx.spawn(|_, _| async move {
134                    prompt.await.ok();
135                })
136                .detach();
137                return;
138            }
139
140            let dock = workspace.dock_at_position(git_panel.position(window, cx));
141            let is_open = dock.read(cx).is_open();
142            let active_index = dock.read(cx).active_panel_index();
143            let dock = dock.downgrade();
144            let restore_dock_position = RestoreDock {
145                dock,
146                is_open,
147                active_index,
148            };
149
150            let project = workspace.project().clone();
151            workspace.open_panel::<GitPanel>(window, cx);
152            workspace.toggle_modal(window, cx, move |window, cx| {
153                CommitModal::new(git_panel, restore_dock_position, project, window, cx)
154            })
155        });
156    }
157
158    fn new(
159        git_panel: Entity<GitPanel>,
160        restore_dock: RestoreDock,
161        project: Entity<Project>,
162        window: &mut Window,
163        cx: &mut Context<Self>,
164    ) -> Self {
165        let panel = git_panel.read(cx);
166        let suggested_message = panel.suggest_commit_message();
167
168        let commit_editor = git_panel.update(cx, |git_panel, cx| {
169            git_panel.set_modal_open(true, cx);
170            let buffer = git_panel.commit_message_buffer(cx).clone();
171            let project = git_panel.project.clone();
172            cx.new(|cx| commit_message_editor(buffer, None, project.clone(), false, window, cx))
173        });
174
175        let commit_message = commit_editor.read(cx).text(cx);
176
177        if let Some(suggested_message) = suggested_message {
178            if commit_message.is_empty() {
179                commit_editor.update(cx, |editor, cx| {
180                    editor.set_text(suggested_message, window, cx);
181                    editor.select_all(&Default::default(), window, cx);
182                });
183            } else {
184                if commit_message.as_str().trim() == suggested_message.trim() {
185                    commit_editor.update(cx, |editor, cx| {
186                        // select the message to make it easy to delete
187                        editor.select_all(&Default::default(), window, cx);
188                    });
189                }
190            }
191        }
192
193        let focus_handle = commit_editor.focus_handle(cx);
194
195        cx.on_focus_out(&focus_handle, window, |this, _, window, cx| {
196            if !this
197                .branch_list
198                .focus_handle(cx)
199                .contains_focused(window, cx)
200            {
201                cx.emit(DismissEvent);
202            }
203        })
204        .detach();
205
206        let properties = ModalContainerProperties::new(window, 50);
207
208        Self {
209            branch_list: branch_picker::popover(project.clone(), window, cx),
210            git_panel,
211            commit_editor,
212            restore_dock,
213            properties,
214        }
215    }
216
217    fn commit_editor_element(&self, window: &mut Window, cx: &mut Context<Self>) -> EditorElement {
218        let editor_style = panel_editor_style(true, window, cx);
219        EditorElement::new(&self.commit_editor, editor_style)
220    }
221
222    pub fn render_commit_editor(
223        &self,
224        window: &mut Window,
225        cx: &mut Context<Self>,
226    ) -> impl IntoElement {
227        let properties = self.properties;
228        let padding_t = 3.0;
229        let padding_b = 6.0;
230        // magic number for editor not to overflow the container??
231        let extra_space_hack = 1.5 * window.line_height();
232
233        v_flex()
234            .h(px(properties.editor_height + padding_b + padding_t) + extra_space_hack)
235            .w_full()
236            .flex_none()
237            .rounded(properties.editor_border_radius())
238            .overflow_hidden()
239            .px_1p5()
240            .pt(px(padding_t))
241            .pb(px(padding_b))
242            .child(
243                div()
244                    .h(px(properties.editor_height))
245                    .w_full()
246                    .child(self.commit_editor_element(window, cx)),
247            )
248    }
249
250    pub fn render_footer(&self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
251        let git_panel = self.git_panel.clone();
252
253        let (branch, tooltip, commit_label, co_authors) =
254            self.git_panel.update(cx, |git_panel, cx| {
255                let branch = git_panel
256                    .active_repository
257                    .as_ref()
258                    .and_then(|repo| {
259                        repo.read(cx)
260                            .repository_entry
261                            .branch()
262                            .map(|b| b.name.clone())
263                    })
264                    .unwrap_or_else(|| "<no branch>".into());
265                let tooltip = if git_panel.has_staged_changes() {
266                    "Commit staged changes"
267                } else {
268                    "Commit changes to tracked files"
269                };
270                let title = if git_panel.has_staged_changes() {
271                    "Commit"
272                } else {
273                    "Commit All"
274                };
275                let co_authors = git_panel.render_co_authors(cx);
276                (branch, tooltip, title, co_authors)
277            });
278
279        let branch_picker_button = panel_button(branch)
280            .icon(IconName::GitBranch)
281            .icon_size(IconSize::Small)
282            .icon_color(Color::Placeholder)
283            .color(Color::Muted)
284            .icon_position(IconPosition::Start)
285            .tooltip(Tooltip::for_action_title(
286                "Switch Branch",
287                &zed_actions::git::Branch,
288            ))
289            .on_click(cx.listener(|_, _, window, cx| {
290                window.dispatch_action(zed_actions::git::Branch.boxed_clone(), cx);
291            }))
292            .style(ButtonStyle::Transparent);
293
294        let branch_picker = PopoverButton::new(
295            self.branch_list.clone(),
296            Corner::BottomLeft,
297            branch_picker_button,
298            Tooltip::for_action_title("Switch Branch", &zed_actions::git::Branch),
299        );
300
301        let close_kb_hint =
302            if let Some(close_kb) = ui::KeyBinding::for_action(&menu::Cancel, window, cx) {
303                Some(
304                    KeybindingHint::new(close_kb, cx.theme().colors().editor_background)
305                        .suffix("Cancel"),
306                )
307            } else {
308                None
309            };
310
311        let (panel_editor_focus_handle, can_commit) = git_panel.update(cx, |git_panel, cx| {
312            (git_panel.editor_focus_handle(cx), git_panel.can_commit())
313        });
314
315        let commit_button = panel_filled_button(commit_label)
316            .tooltip(move |window, cx| {
317                Tooltip::for_action_in(tooltip, &Commit, &panel_editor_focus_handle, window, cx)
318            })
319            .disabled(!can_commit)
320            .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| {
321                this.git_panel
322                    .update(cx, |git_panel, cx| git_panel.commit_changes(window, cx));
323                cx.emit(DismissEvent);
324            }));
325
326        h_flex()
327            .group("commit_editor_footer")
328            .flex_none()
329            .w_full()
330            .items_center()
331            .justify_between()
332            .w_full()
333            .h(px(self.properties.footer_height))
334            .gap_1()
335            .child(
336                h_flex()
337                    .gap_1()
338                    .child(branch_picker.render(window, cx))
339                    .children(co_authors),
340            )
341            .child(div().flex_1())
342            .child(
343                h_flex()
344                    .items_center()
345                    .justify_end()
346                    .flex_none()
347                    .px_1()
348                    .gap_4()
349                    .children(close_kb_hint)
350                    .child(commit_button),
351            )
352    }
353
354    fn dismiss(&mut self, _: &menu::Cancel, _: &mut Window, cx: &mut Context<Self>) {
355        cx.emit(DismissEvent);
356    }
357    fn commit(&mut self, _: &git::Commit, window: &mut Window, cx: &mut Context<Self>) {
358        self.git_panel
359            .update(cx, |git_panel, cx| git_panel.commit_changes(window, cx));
360        cx.emit(DismissEvent);
361    }
362}
363
364impl Render for CommitModal {
365    fn render(&mut self, window: &mut Window, cx: &mut Context<'_, Self>) -> impl IntoElement {
366        let properties = self.properties;
367        let width = px(properties.modal_width);
368        let container_padding = px(properties.container_padding);
369        let border_radius = properties.modal_border_radius;
370        let editor_focus_handle = self.commit_editor.focus_handle(cx);
371
372        v_flex()
373            .id("commit-modal")
374            .key_context("GitCommit")
375            .on_action(cx.listener(Self::dismiss))
376            .on_action(cx.listener(Self::commit))
377            .on_action(
378                cx.listener(|this, _: &zed_actions::git::Branch, window, cx| {
379                    this.branch_list.update(cx, |branch_list, cx| {
380                        branch_list.menu_handle(window, cx).toggle(window, cx);
381                    })
382                }),
383            )
384            .elevation_3(cx)
385            .overflow_hidden()
386            .flex_none()
387            .relative()
388            .bg(cx.theme().colors().elevated_surface_background)
389            .rounded(px(border_radius))
390            .border_1()
391            .border_color(cx.theme().colors().border)
392            .w(width)
393            .p(container_padding)
394            .child(
395                v_flex()
396                    .id("editor-container")
397                    .justify_between()
398                    .p_2()
399                    .size_full()
400                    .gap_2()
401                    .rounded(properties.editor_border_radius())
402                    .overflow_hidden()
403                    .cursor_text()
404                    .bg(cx.theme().colors().editor_background)
405                    .border_1()
406                    .border_color(cx.theme().colors().border_variant)
407                    .on_click(cx.listener(move |_, _: &ClickEvent, window, _cx| {
408                        window.focus(&editor_focus_handle);
409                    }))
410                    .child(
411                        div()
412                            .flex_1()
413                            .size_full()
414                            .child(self.render_commit_editor(window, cx)),
415                    )
416                    .child(self.render_footer(window, cx)),
417            )
418    }
419}