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