commit_modal.rs

  1use crate::branch_picker::{self, BranchList};
  2use crate::git_panel::{GitPanel, commit_message_editor};
  3use git::repository::CommitOptions;
  4use git::{Amend, Commit, GenerateCommitMessage};
  5use panel::{panel_button, panel_editor_style, panel_filled_button};
  6use ui::{
  7    ContextMenu, KeybindingHint, PopoverMenu, PopoverMenuHandle, SplitButton, Tooltip, prelude::*,
  8};
  9
 10use editor::{Editor, EditorElement};
 11use gpui::*;
 12use util::ResultExt;
 13use workspace::{
 14    ModalView, Workspace,
 15    dock::{Dock, PanelHandle},
 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 struct CommitModal {
 59    git_panel: Entity<GitPanel>,
 60    commit_editor: Entity<Editor>,
 61    restore_dock: RestoreDock,
 62    properties: ModalContainerProperties,
 63    branch_list_handle: PopoverMenuHandle<BranchList>,
 64    commit_menu_handle: PopoverMenuHandle<ContextMenu>,
 65}
 66
 67impl Focusable for CommitModal {
 68    fn focus_handle(&self, cx: &App) -> gpui::FocusHandle {
 69        self.commit_editor.focus_handle(cx)
 70    }
 71}
 72
 73impl EventEmitter<DismissEvent> for CommitModal {}
 74impl ModalView for CommitModal {
 75    fn on_before_dismiss(
 76        &mut self,
 77        window: &mut Window,
 78        cx: &mut Context<Self>,
 79    ) -> workspace::DismissDecision {
 80        self.git_panel.update(cx, |git_panel, cx| {
 81            git_panel.set_modal_open(false, cx);
 82        });
 83        self.restore_dock
 84            .dock
 85            .update(cx, |dock, cx| {
 86                if let Some(active_index) = self.restore_dock.active_index {
 87                    dock.activate_panel(active_index, window, cx)
 88                }
 89                dock.set_open(self.restore_dock.is_open, window, cx)
 90            })
 91            .log_err();
 92        workspace::DismissDecision::Dismiss(true)
 93    }
 94}
 95
 96struct RestoreDock {
 97    dock: WeakEntity<Dock>,
 98    is_open: bool,
 99    active_index: Option<usize>,
100}
101
102pub enum ForceMode {
103    Amend,
104    Commit,
105}
106
107impl CommitModal {
108    pub fn register(workspace: &mut Workspace) {
109        workspace.register_action(|workspace, _: &Commit, window, cx| {
110            CommitModal::toggle(workspace, Some(ForceMode::Commit), window, cx);
111        });
112        workspace.register_action(|workspace, _: &Amend, window, cx| {
113            CommitModal::toggle(workspace, Some(ForceMode::Amend), window, cx);
114        });
115    }
116
117    pub fn toggle(
118        workspace: &mut Workspace,
119        force_mode: Option<ForceMode>,
120        window: &mut Window,
121        cx: &mut Context<Workspace>,
122    ) {
123        let Some(git_panel) = workspace.panel::<GitPanel>(cx) else {
124            return;
125        };
126
127        git_panel.update(cx, |git_panel, cx| {
128            if let Some(force_mode) = force_mode {
129                match force_mode {
130                    ForceMode::Amend => {
131                        if !git_panel.amend_pending() {
132                            git_panel.set_amend_pending(true, cx);
133                            git_panel.load_last_commit_message_if_empty(cx);
134                        }
135                    }
136                    ForceMode::Commit => {
137                        if git_panel.amend_pending() {
138                            git_panel.set_amend_pending(false, cx);
139                        }
140                    }
141                }
142            }
143            git_panel.set_modal_open(true, cx);
144        });
145
146        let dock = workspace.dock_at_position(git_panel.position(window, cx));
147        let is_open = dock.read(cx).is_open();
148        let active_index = dock.read(cx).active_panel_index();
149        let dock = dock.downgrade();
150        let restore_dock_position = RestoreDock {
151            dock,
152            is_open,
153            active_index,
154        };
155
156        workspace.open_panel::<GitPanel>(window, cx);
157        workspace.toggle_modal(window, cx, move |window, cx| {
158            CommitModal::new(git_panel, restore_dock_position, window, cx)
159        })
160    }
161
162    fn new(
163        git_panel: Entity<GitPanel>,
164        restore_dock: RestoreDock,
165        window: &mut Window,
166        cx: &mut Context<Self>,
167    ) -> Self {
168        let panel = git_panel.read(cx);
169        let suggested_commit_message = panel.suggest_commit_message(cx);
170
171        let commit_editor = git_panel.update(cx, |git_panel, cx| {
172            git_panel.set_modal_open(true, cx);
173            let buffer = git_panel.commit_message_buffer(cx).clone();
174            let panel_editor = git_panel.commit_editor.clone();
175            let project = git_panel.project.clone();
176
177            cx.new(|cx| {
178                let mut editor =
179                    commit_message_editor(buffer, None, project.clone(), false, window, cx);
180                editor.sync_selections(panel_editor, cx).detach();
181
182                editor
183            })
184        });
185
186        let commit_message = commit_editor.read(cx).text(cx);
187
188        if let Some(suggested_commit_message) = suggested_commit_message {
189            if commit_message.is_empty() {
190                commit_editor.update(cx, |editor, cx| {
191                    editor.set_placeholder_text(suggested_commit_message, cx);
192                });
193            }
194        }
195
196        let focus_handle = commit_editor.focus_handle(cx);
197
198        cx.on_focus_out(&focus_handle, window, |this, _, window, cx| {
199            if !this.branch_list_handle.is_focused(window, cx)
200                && !this.commit_menu_handle.is_focused(window, cx)
201            {
202                cx.emit(DismissEvent);
203            }
204        })
205        .detach();
206
207        let properties = ModalContainerProperties::new(window, 50);
208
209        Self {
210            git_panel,
211            commit_editor,
212            restore_dock,
213            properties,
214            branch_list_handle: PopoverMenuHandle::default(),
215            commit_menu_handle: PopoverMenuHandle::default(),
216        }
217    }
218
219    fn commit_editor_element(&self, window: &mut Window, cx: &mut Context<Self>) -> EditorElement {
220        let editor_style = panel_editor_style(true, window, cx);
221        EditorElement::new(&self.commit_editor, editor_style)
222    }
223
224    pub fn render_commit_editor(
225        &self,
226        window: &mut Window,
227        cx: &mut Context<Self>,
228    ) -> impl IntoElement {
229        let properties = self.properties;
230        let padding_t = 3.0;
231        let padding_b = 6.0;
232        // magic number for editor not to overflow the container??
233        let extra_space_hack = 1.5 * window.line_height();
234
235        v_flex()
236            .h(px(properties.editor_height + padding_b + padding_t) + extra_space_hack)
237            .w_full()
238            .flex_none()
239            .rounded(properties.editor_border_radius())
240            .overflow_hidden()
241            .px_1p5()
242            .pt(px(padding_t))
243            .pb(px(padding_b))
244            .child(
245                div()
246                    .h(px(properties.editor_height))
247                    .w_full()
248                    .child(self.commit_editor_element(window, cx)),
249            )
250    }
251
252    fn render_git_commit_menu(
253        &self,
254        id: impl Into<ElementId>,
255        keybinding_target: Option<FocusHandle>,
256    ) -> impl IntoElement {
257        PopoverMenu::new(id.into())
258            .trigger(
259                ui::ButtonLike::new_rounded_right("commit-split-button-right")
260                    .layer(ui::ElevationIndex::ModalSurface)
261                    .size(ui::ButtonSize::None)
262                    .child(
263                        div()
264                            .px_1()
265                            .child(Icon::new(IconName::ChevronDownSmall).size(IconSize::XSmall)),
266                    ),
267            )
268            .menu(move |window, cx| {
269                Some(ContextMenu::build(window, cx, |context_menu, _, _| {
270                    context_menu
271                        .when_some(keybinding_target.clone(), |el, keybinding_target| {
272                            el.context(keybinding_target.clone())
273                        })
274                        .action("Amend...", Amend.boxed_clone())
275                }))
276            })
277            .with_handle(self.commit_menu_handle.clone())
278            .anchor(Corner::TopRight)
279    }
280
281    pub fn render_footer(&self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
282        let (
283            can_commit,
284            tooltip,
285            commit_label,
286            co_authors,
287            generate_commit_message,
288            active_repo,
289            is_amend_pending,
290            has_previous_commit,
291        ) = self.git_panel.update(cx, |git_panel, cx| {
292            let (can_commit, tooltip) = git_panel.configure_commit_button(cx);
293            let title = git_panel.commit_button_title();
294            let co_authors = git_panel.render_co_authors(cx);
295            let generate_commit_message = git_panel.render_generate_commit_message_button(cx);
296            let active_repo = git_panel.active_repository.clone();
297            let is_amend_pending = git_panel.amend_pending();
298            let has_previous_commit = active_repo
299                .as_ref()
300                .and_then(|repo| repo.read(cx).branch.as_ref())
301                .and_then(|branch| branch.most_recent_commit.as_ref())
302                .is_some();
303            (
304                can_commit,
305                tooltip,
306                title,
307                co_authors,
308                generate_commit_message,
309                active_repo,
310                is_amend_pending,
311                has_previous_commit,
312            )
313        });
314
315        let branch = active_repo
316            .as_ref()
317            .and_then(|repo| repo.read(cx).branch.as_ref())
318            .map(|b| b.name.clone())
319            .unwrap_or_else(|| "<no branch>".into());
320
321        let branch_picker_button = panel_button(branch)
322            .icon(IconName::GitBranch)
323            .icon_size(IconSize::Small)
324            .icon_color(Color::Placeholder)
325            .color(Color::Muted)
326            .icon_position(IconPosition::Start)
327            .tooltip(Tooltip::for_action_title(
328                "Switch Branch",
329                &zed_actions::git::Branch,
330            ))
331            .on_click(cx.listener(|_, _, window, cx| {
332                window.dispatch_action(zed_actions::git::Branch.boxed_clone(), cx);
333            }))
334            .style(ButtonStyle::Transparent);
335
336        let branch_picker = PopoverMenu::new("popover-button")
337            .menu(move |window, cx| Some(branch_picker::popover(active_repo.clone(), window, cx)))
338            .with_handle(self.branch_list_handle.clone())
339            .trigger_with_tooltip(
340                branch_picker_button,
341                Tooltip::for_action_title("Switch Branch", &zed_actions::git::Branch),
342            )
343            .anchor(Corner::BottomLeft)
344            .offset(gpui::Point {
345                x: px(0.0),
346                y: px(-2.0),
347            });
348        let focus_handle = self.focus_handle(cx);
349
350        let close_kb_hint =
351            if let Some(close_kb) = ui::KeyBinding::for_action(&menu::Cancel, window, cx) {
352                Some(
353                    KeybindingHint::new(close_kb, cx.theme().colors().editor_background)
354                        .suffix("Cancel"),
355                )
356            } else {
357                None
358            };
359
360        h_flex()
361            .group("commit_editor_footer")
362            .flex_none()
363            .w_full()
364            .items_center()
365            .justify_between()
366            .w_full()
367            .h(px(self.properties.footer_height))
368            .gap_1()
369            .child(
370                h_flex()
371                    .gap_1()
372                    .flex_shrink()
373                    .overflow_x_hidden()
374                    .child(
375                        h_flex()
376                            .flex_shrink()
377                            .overflow_x_hidden()
378                            .child(branch_picker),
379                    )
380                    .children(generate_commit_message)
381                    .children(co_authors),
382            )
383            .child(div().flex_1())
384            .child(
385                h_flex()
386                    .items_center()
387                    .justify_end()
388                    .flex_none()
389                    .px_1()
390                    .gap_4()
391                    .children(close_kb_hint)
392                    .when(is_amend_pending, |this| {
393                        let focus_handle = focus_handle.clone();
394                        this.child(
395                            panel_filled_button(commit_label)
396                                .tooltip(move |window, cx| {
397                                    if can_commit {
398                                        Tooltip::for_action_in(
399                                            tooltip,
400                                            &Amend,
401                                            &focus_handle,
402                                            window,
403                                            cx,
404                                        )
405                                    } else {
406                                        Tooltip::simple(tooltip, cx)
407                                    }
408                                })
409                                .disabled(!can_commit)
410                                .on_click(move |_, window, cx| {
411                                    window.dispatch_action(Box::new(git::Commit), cx);
412                                }),
413                        )
414                    })
415                    .when(!is_amend_pending, |this| {
416                        this.when(has_previous_commit, |this| {
417                            this.child(SplitButton::new(
418                                ui::ButtonLike::new_rounded_left(ElementId::Name(
419                                    format!("split-button-left-{}", commit_label).into(),
420                                ))
421                                .layer(ui::ElevationIndex::ModalSurface)
422                                .size(ui::ButtonSize::Compact)
423                                .child(
424                                    div()
425                                        .child(Label::new(commit_label).size(LabelSize::Small))
426                                        .mr_0p5(),
427                                )
428                                .on_click(move |_, window, cx| {
429                                    window.dispatch_action(Box::new(git::Commit), cx);
430                                })
431                                .disabled(!can_commit)
432                                .tooltip({
433                                    let focus_handle = focus_handle.clone();
434                                    move |window, cx| {
435                                        if can_commit {
436                                            Tooltip::with_meta_in(
437                                                tooltip,
438                                                Some(&git::Commit),
439                                                "git commit",
440                                                &focus_handle.clone(),
441                                                window,
442                                                cx,
443                                            )
444                                        } else {
445                                            Tooltip::simple(tooltip, cx)
446                                        }
447                                    }
448                                }),
449                                self.render_git_commit_menu(
450                                    ElementId::Name(
451                                        format!("split-button-right-{}", commit_label).into(),
452                                    ),
453                                    Some(focus_handle.clone()),
454                                )
455                                .into_any_element(),
456                            ))
457                        })
458                        .when(!has_previous_commit, |this| {
459                            this.child(
460                                panel_filled_button(commit_label)
461                                    .tooltip(move |window, cx| {
462                                        if can_commit {
463                                            Tooltip::with_meta_in(
464                                                tooltip,
465                                                Some(&git::Commit),
466                                                "git commit",
467                                                &focus_handle,
468                                                window,
469                                                cx,
470                                            )
471                                        } else {
472                                            Tooltip::simple(tooltip, cx)
473                                        }
474                                    })
475                                    .disabled(!can_commit)
476                                    .on_click(move |_, window, cx| {
477                                        window.dispatch_action(Box::new(git::Commit), cx);
478                                    }),
479                            )
480                        })
481                    }),
482            )
483    }
484
485    fn dismiss(&mut self, _: &menu::Cancel, _: &mut Window, cx: &mut Context<Self>) {
486        if self.git_panel.read(cx).amend_pending() {
487            self.git_panel
488                .update(cx, |git_panel, cx| git_panel.set_amend_pending(false, cx));
489        } else {
490            cx.emit(DismissEvent);
491        }
492    }
493
494    fn commit(&mut self, _: &git::Commit, window: &mut Window, cx: &mut Context<Self>) {
495        if self.git_panel.read(cx).amend_pending() {
496            return;
497        }
498        telemetry::event!("Git Committed", source = "Git Modal");
499        self.git_panel.update(cx, |git_panel, cx| {
500            git_panel.commit_changes(CommitOptions { amend: false }, window, cx)
501        });
502        cx.emit(DismissEvent);
503    }
504
505    fn amend(&mut self, _: &git::Amend, window: &mut Window, cx: &mut Context<Self>) {
506        if self
507            .commit_editor
508            .focus_handle(cx)
509            .contains_focused(window, cx)
510        {
511            if !self.git_panel.read(cx).amend_pending() {
512                self.git_panel.update(cx, |git_panel, cx| {
513                    git_panel.set_amend_pending(true, cx);
514                    git_panel.load_last_commit_message_if_empty(cx);
515                });
516            } else {
517                telemetry::event!("Git Amended", source = "Git Panel");
518                self.git_panel.update(cx, |git_panel, cx| {
519                    git_panel.set_amend_pending(false, cx);
520                    git_panel.commit_changes(CommitOptions { amend: true }, window, cx);
521                });
522                cx.emit(DismissEvent);
523            }
524        } else {
525            cx.propagate();
526        }
527    }
528
529    fn toggle_branch_selector(&mut self, window: &mut Window, cx: &mut Context<Self>) {
530        if self.branch_list_handle.is_focused(window, cx) {
531            self.focus_handle(cx).focus(window)
532        } else {
533            self.branch_list_handle.toggle(window, cx);
534        }
535    }
536}
537
538impl Render for CommitModal {
539    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
540        let properties = self.properties;
541        let width = px(properties.modal_width);
542        let container_padding = px(properties.container_padding);
543        let border_radius = properties.modal_border_radius;
544        let editor_focus_handle = self.commit_editor.focus_handle(cx);
545
546        v_flex()
547            .id("commit-modal")
548            .key_context("GitCommit")
549            .on_action(cx.listener(Self::dismiss))
550            .on_action(cx.listener(Self::commit))
551            .on_action(cx.listener(Self::amend))
552            .on_action(cx.listener(|this, _: &GenerateCommitMessage, _, cx| {
553                this.git_panel.update(cx, |panel, cx| {
554                    panel.generate_commit_message(cx);
555                })
556            }))
557            .on_action(
558                cx.listener(|this, _: &zed_actions::git::Branch, window, cx| {
559                    this.toggle_branch_selector(window, cx);
560                }),
561            )
562            .on_action(
563                cx.listener(|this, _: &zed_actions::git::CheckoutBranch, window, cx| {
564                    this.toggle_branch_selector(window, cx);
565                }),
566            )
567            .on_action(
568                cx.listener(|this, _: &zed_actions::git::Switch, window, cx| {
569                    this.toggle_branch_selector(window, cx);
570                }),
571            )
572            .elevation_3(cx)
573            .overflow_hidden()
574            .flex_none()
575            .relative()
576            .bg(cx.theme().colors().elevated_surface_background)
577            .rounded(px(border_radius))
578            .border_1()
579            .border_color(cx.theme().colors().border)
580            .w(width)
581            .p(container_padding)
582            .child(
583                v_flex()
584                    .id("editor-container")
585                    .justify_between()
586                    .p_2()
587                    .size_full()
588                    .gap_2()
589                    .rounded(properties.editor_border_radius())
590                    .overflow_hidden()
591                    .cursor_text()
592                    .bg(cx.theme().colors().editor_background)
593                    .border_1()
594                    .border_color(cx.theme().colors().border_variant)
595                    .on_click(cx.listener(move |_, _: &ClickEvent, window, _cx| {
596                        window.focus(&editor_focus_handle);
597                    }))
598                    .child(
599                        div()
600                            .flex_1()
601                            .size_full()
602                            .child(self.render_commit_editor(window, cx)),
603                    )
604                    .child(self.render_footer(window, cx)),
605            )
606    }
607}