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