1// #![allow(unused, dead_code)]
2
3use crate::branch_picker::{self, BranchList};
4use crate::git_panel::{commit_message_editor, GitPanel};
5use git::Commit;
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, _: &Commit, 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 (can_commit, conflict)
122 });
123 if !can_commit {
124 let message = if conflict {
125 "There are still conflicts. You must stage these before committing."
126 } else {
127 "No changes to commit."
128 };
129 let prompt = window.prompt(PromptLevel::Warning, message, None, &["Ok"], cx);
130 cx.spawn(|_, _| async move {
131 prompt.await.ok();
132 })
133 .detach();
134 }
135
136 let dock = workspace.dock_at_position(git_panel.position(window, cx));
137 let is_open = dock.read(cx).is_open();
138 let active_index = dock.read(cx).active_panel_index();
139 let dock = dock.downgrade();
140 let restore_dock_position = RestoreDock {
141 dock,
142 is_open,
143 active_index,
144 };
145
146 let project = workspace.project().clone();
147 workspace.open_panel::<GitPanel>(window, cx);
148 workspace.toggle_modal(window, cx, move |window, cx| {
149 CommitModal::new(git_panel, restore_dock_position, project, window, cx)
150 })
151 });
152 }
153
154 fn new(
155 git_panel: Entity<GitPanel>,
156 restore_dock: RestoreDock,
157 project: Entity<Project>,
158 window: &mut Window,
159 cx: &mut Context<Self>,
160 ) -> Self {
161 let panel = git_panel.read(cx);
162 let suggested_message = panel.suggest_commit_message();
163
164 let commit_editor = git_panel.update(cx, |git_panel, cx| {
165 git_panel.set_modal_open(true, cx);
166 let buffer = git_panel.commit_message_buffer(cx).clone();
167 let project = git_panel.project.clone();
168 cx.new(|cx| commit_message_editor(buffer, None, project.clone(), false, window, cx))
169 });
170
171 let commit_message = commit_editor.read(cx).text(cx);
172
173 if let Some(suggested_message) = suggested_message {
174 if commit_message.is_empty() {
175 commit_editor.update(cx, |editor, cx| {
176 editor.set_text(suggested_message, window, cx);
177 editor.select_all(&Default::default(), window, cx);
178 });
179 } else {
180 if commit_message.as_str().trim() == suggested_message.trim() {
181 commit_editor.update(cx, |editor, cx| {
182 // select the message to make it easy to delete
183 editor.select_all(&Default::default(), window, cx);
184 });
185 }
186 }
187 }
188
189 let focus_handle = commit_editor.focus_handle(cx);
190
191 cx.on_focus_out(&focus_handle, window, |this, _, window, cx| {
192 if !this
193 .branch_list
194 .focus_handle(cx)
195 .contains_focused(window, cx)
196 {
197 cx.emit(DismissEvent);
198 }
199 })
200 .detach();
201
202 let properties = ModalContainerProperties::new(window, 50);
203
204 Self {
205 branch_list: branch_picker::popover(project.clone(), window, cx),
206 git_panel,
207 commit_editor,
208 restore_dock,
209 properties,
210 }
211 }
212
213 fn commit_editor_element(&self, window: &mut Window, cx: &mut Context<Self>) -> EditorElement {
214 let editor_style = panel_editor_style(true, window, cx);
215 EditorElement::new(&self.commit_editor, editor_style)
216 }
217
218 pub fn render_commit_editor(
219 &self,
220 window: &mut Window,
221 cx: &mut Context<Self>,
222 ) -> impl IntoElement {
223 let properties = self.properties;
224 let padding_t = 3.0;
225 let padding_b = 6.0;
226 // magic number for editor not to overflow the container??
227 let extra_space_hack = 1.5 * window.line_height();
228
229 v_flex()
230 .h(px(properties.editor_height + padding_b + padding_t) + extra_space_hack)
231 .w_full()
232 .flex_none()
233 .rounded(properties.editor_border_radius())
234 .overflow_hidden()
235 .px_1p5()
236 .pt(px(padding_t))
237 .pb(px(padding_b))
238 .child(
239 div()
240 .h(px(properties.editor_height))
241 .w_full()
242 .child(self.commit_editor_element(window, cx)),
243 )
244 }
245
246 pub fn render_footer(&self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
247 let git_panel = self.git_panel.clone();
248
249 let (branch, tooltip, commit_label, co_authors) =
250 self.git_panel.update(cx, |git_panel, cx| {
251 let branch = git_panel
252 .active_repository
253 .as_ref()
254 .and_then(|repo| {
255 repo.read(cx)
256 .repository_entry
257 .branch()
258 .map(|b| b.name.clone())
259 })
260 .unwrap_or_else(|| "<no branch>".into());
261 let tooltip = if git_panel.has_staged_changes() {
262 "Commit staged changes"
263 } else {
264 "Commit changes to tracked files"
265 };
266 let title = if git_panel.has_staged_changes() {
267 "Commit"
268 } else {
269 "Commit All"
270 };
271 let co_authors = git_panel.render_co_authors(cx);
272 (branch, tooltip, title, co_authors)
273 });
274
275 let branch_picker_button = panel_button(branch)
276 .icon(IconName::GitBranch)
277 .icon_size(IconSize::Small)
278 .icon_color(Color::Placeholder)
279 .color(Color::Muted)
280 .icon_position(IconPosition::Start)
281 .tooltip(Tooltip::for_action_title(
282 "Switch Branch",
283 &zed_actions::git::Branch,
284 ))
285 .on_click(cx.listener(|_, _, window, cx| {
286 window.dispatch_action(zed_actions::git::Branch.boxed_clone(), cx);
287 }))
288 .style(ButtonStyle::Transparent);
289
290 let branch_picker = PopoverButton::new(
291 self.branch_list.clone(),
292 Corner::BottomLeft,
293 branch_picker_button,
294 Tooltip::for_action_title("Switch Branch", &zed_actions::git::Branch),
295 );
296
297 let close_kb_hint =
298 if let Some(close_kb) = ui::KeyBinding::for_action(&menu::Cancel, window, cx) {
299 Some(
300 KeybindingHint::new(close_kb, cx.theme().colors().editor_background)
301 .suffix("Cancel"),
302 )
303 } else {
304 None
305 };
306
307 let (panel_editor_focus_handle, can_commit) = git_panel.update(cx, |git_panel, cx| {
308 (git_panel.editor_focus_handle(cx), git_panel.can_commit())
309 });
310
311 let commit_button = panel_filled_button(commit_label)
312 .tooltip(move |window, cx| {
313 Tooltip::for_action_in(tooltip, &Commit, &panel_editor_focus_handle, window, cx)
314 })
315 .disabled(!can_commit)
316 .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| {
317 this.git_panel
318 .update(cx, |git_panel, cx| git_panel.commit_changes(window, cx));
319 cx.emit(DismissEvent);
320 }));
321
322 h_flex()
323 .group("commit_editor_footer")
324 .flex_none()
325 .w_full()
326 .items_center()
327 .justify_between()
328 .w_full()
329 .h(px(self.properties.footer_height))
330 .gap_1()
331 .child(
332 h_flex()
333 .gap_1()
334 .child(branch_picker.render(window, cx))
335 .children(co_authors),
336 )
337 .child(div().flex_1())
338 .child(
339 h_flex()
340 .items_center()
341 .justify_end()
342 .flex_none()
343 .px_1()
344 .gap_4()
345 .children(close_kb_hint)
346 .child(commit_button),
347 )
348 }
349
350 fn dismiss(&mut self, _: &menu::Cancel, _: &mut Window, cx: &mut Context<Self>) {
351 cx.emit(DismissEvent);
352 }
353 fn commit(&mut self, _: &git::Commit, window: &mut Window, cx: &mut Context<Self>) {
354 self.git_panel
355 .update(cx, |git_panel, cx| git_panel.commit_changes(window, cx));
356 cx.emit(DismissEvent);
357 }
358}
359
360impl Render for CommitModal {
361 fn render(&mut self, window: &mut Window, cx: &mut Context<'_, Self>) -> impl IntoElement {
362 let properties = self.properties;
363 let width = px(properties.modal_width);
364 let container_padding = px(properties.container_padding);
365 let border_radius = properties.modal_border_radius;
366 let editor_focus_handle = self.commit_editor.focus_handle(cx);
367
368 v_flex()
369 .id("commit-modal")
370 .key_context("GitCommit")
371 .on_action(cx.listener(Self::dismiss))
372 .on_action(cx.listener(Self::commit))
373 .on_action(
374 cx.listener(|this, _: &zed_actions::git::Branch, window, cx| {
375 this.branch_list.update(cx, |branch_list, cx| {
376 branch_list.menu_handle(window, cx).toggle(window, cx);
377 })
378 }),
379 )
380 .elevation_3(cx)
381 .overflow_hidden()
382 .flex_none()
383 .relative()
384 .bg(cx.theme().colors().elevated_surface_background)
385 .rounded(px(border_radius))
386 .border_1()
387 .border_color(cx.theme().colors().border)
388 .w(width)
389 .p(container_padding)
390 .child(
391 v_flex()
392 .id("editor-container")
393 .justify_between()
394 .p_2()
395 .size_full()
396 .gap_2()
397 .rounded(properties.editor_border_radius())
398 .overflow_hidden()
399 .cursor_text()
400 .bg(cx.theme().colors().editor_background)
401 .border_1()
402 .border_color(cx.theme().colors().border_variant)
403 .on_click(cx.listener(move |_, _: &ClickEvent, window, _cx| {
404 window.focus(&editor_focus_handle);
405 }))
406 .child(
407 div()
408 .flex_1()
409 .size_full()
410 .child(self.render_commit_editor(window, cx)),
411 )
412 .child(self.render_footer(window, cx)),
413 )
414 }
415}