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