1// #![allow(unused, dead_code)]
2
3use crate::branch_picker::{self, BranchList};
4use crate::git_panel::{commit_message_editor, GitPanel};
5use git::{Commit, GenerateCommitMessage};
6use panel::{panel_button, panel_editor_style, panel_filled_button};
7use ui::{prelude::*, KeybindingHint, PopoverMenu, Tooltip};
8
9use editor::{Editor, EditorElement};
10use gpui::*;
11use util::ResultExt;
12use workspace::{
13 dock::{Dock, PanelHandle},
14 ModalView, Workspace,
15};
16
17// nate: It is a pain to get editors to size correctly and not overflow.
18//
19// this can get replaced with a simple flex layout with more time/a more thoughtful approach.
20#[derive(Debug, Clone, Copy)]
21pub struct ModalContainerProperties {
22 pub modal_width: f32,
23 pub editor_height: f32,
24 pub footer_height: f32,
25 pub container_padding: f32,
26 pub modal_border_radius: f32,
27}
28
29impl ModalContainerProperties {
30 pub fn new(window: &Window, preferred_char_width: usize) -> Self {
31 let container_padding = 5.0;
32
33 // Calculate width based on character width
34 let mut modal_width = 460.0;
35 let style = window.text_style().clone();
36 let font_id = window.text_system().resolve_font(&style.font());
37 let font_size = style.font_size.to_pixels(window.rem_size());
38
39 if let Ok(em_width) = window.text_system().em_width(font_id, font_size) {
40 modal_width = preferred_char_width as f32 * em_width.0 + (container_padding * 2.0);
41 }
42
43 Self {
44 modal_width,
45 editor_height: 300.0,
46 footer_height: 24.0,
47 container_padding,
48 modal_border_radius: 12.0,
49 }
50 }
51
52 pub fn editor_border_radius(&self) -> Pixels {
53 px(self.modal_border_radius - self.container_padding / 2.0)
54 }
55}
56
57pub fn init(cx: &mut App) {
58 cx.observe_new(|workspace: &mut Workspace, window, cx| {
59 let Some(window) = window else {
60 return;
61 };
62 CommitModal::register(workspace, window, cx)
63 })
64 .detach();
65}
66
67pub struct CommitModal {
68 branch_list: Entity<BranchList>,
69 git_panel: Entity<GitPanel>,
70 commit_editor: Entity<Editor>,
71 restore_dock: RestoreDock,
72 properties: ModalContainerProperties,
73}
74
75impl Focusable for CommitModal {
76 fn focus_handle(&self, cx: &App) -> gpui::FocusHandle {
77 self.commit_editor.focus_handle(cx)
78 }
79}
80
81impl EventEmitter<DismissEvent> for CommitModal {}
82impl ModalView for CommitModal {
83 fn on_before_dismiss(
84 &mut self,
85 window: &mut Window,
86 cx: &mut Context<Self>,
87 ) -> workspace::DismissDecision {
88 self.git_panel.update(cx, |git_panel, cx| {
89 git_panel.set_modal_open(false, cx);
90 });
91 self.restore_dock
92 .dock
93 .update(cx, |dock, cx| {
94 if let Some(active_index) = self.restore_dock.active_index {
95 dock.activate_panel(active_index, window, cx)
96 }
97 dock.set_open(self.restore_dock.is_open, window, cx)
98 })
99 .log_err();
100 workspace::DismissDecision::Dismiss(true)
101 }
102}
103
104struct RestoreDock {
105 dock: WeakEntity<Dock>,
106 is_open: bool,
107 active_index: Option<usize>,
108}
109
110impl CommitModal {
111 pub fn register(workspace: &mut Workspace, _: &mut Window, _cx: &mut Context<Workspace>) {
112 workspace.register_action(|workspace, _: &Commit, window, cx| {
113 CommitModal::toggle(workspace, window, cx);
114 });
115 }
116
117 pub fn toggle(workspace: &mut Workspace, window: &mut Window, cx: &mut Context<'_, Workspace>) {
118 let Some(git_panel) = workspace.panel::<GitPanel>(cx) else {
119 return;
120 };
121
122 git_panel.update(cx, |git_panel, cx| {
123 git_panel.set_modal_open(true, cx);
124 });
125
126 let dock = workspace.dock_at_position(git_panel.position(window, cx));
127 let is_open = dock.read(cx).is_open();
128 let active_index = dock.read(cx).active_panel_index();
129 let dock = dock.downgrade();
130 let restore_dock_position = RestoreDock {
131 dock,
132 is_open,
133 active_index,
134 };
135
136 workspace.open_panel::<GitPanel>(window, cx);
137 workspace.toggle_modal(window, cx, move |window, cx| {
138 CommitModal::new(git_panel, restore_dock_position, window, cx)
139 })
140 }
141
142 fn new(
143 git_panel: Entity<GitPanel>,
144 restore_dock: RestoreDock,
145 window: &mut Window,
146 cx: &mut Context<Self>,
147 ) -> Self {
148 let panel = git_panel.read(cx);
149 let active_repository = panel.active_repository.clone();
150 let suggested_commit_message = panel.suggest_commit_message();
151
152 let commit_editor = git_panel.update(cx, |git_panel, cx| {
153 git_panel.set_modal_open(true, cx);
154 let buffer = git_panel.commit_message_buffer(cx).clone();
155 let panel_editor = git_panel.commit_editor.clone();
156 let project = git_panel.project.clone();
157
158 cx.new(|cx| {
159 let mut editor =
160 commit_message_editor(buffer, None, project.clone(), false, window, cx);
161 editor.sync_selections(panel_editor, cx).detach();
162
163 editor
164 })
165 });
166
167 let commit_message = commit_editor.read(cx).text(cx);
168
169 if let Some(suggested_commit_message) = suggested_commit_message {
170 if commit_message.is_empty() {
171 commit_editor.update(cx, |editor, cx| {
172 editor.set_placeholder_text(suggested_commit_message, cx);
173 });
174 }
175 }
176
177 let focus_handle = commit_editor.focus_handle(cx);
178
179 cx.on_focus_out(&focus_handle, window, |this, _, window, cx| {
180 if !this
181 .branch_list
182 .focus_handle(cx)
183 .contains_focused(window, cx)
184 {
185 cx.emit(DismissEvent);
186 }
187 })
188 .detach();
189
190 let properties = ModalContainerProperties::new(window, 50);
191
192 Self {
193 branch_list: branch_picker::popover(active_repository.clone(), window, cx),
194 git_panel,
195 commit_editor,
196 restore_dock,
197 properties,
198 }
199 }
200
201 fn commit_editor_element(&self, window: &mut Window, cx: &mut Context<Self>) -> EditorElement {
202 let editor_style = panel_editor_style(true, window, cx);
203 EditorElement::new(&self.commit_editor, editor_style)
204 }
205
206 pub fn render_commit_editor(
207 &self,
208 window: &mut Window,
209 cx: &mut Context<Self>,
210 ) -> impl IntoElement {
211 let properties = self.properties;
212 let padding_t = 3.0;
213 let padding_b = 6.0;
214 // magic number for editor not to overflow the container??
215 let extra_space_hack = 1.5 * window.line_height();
216
217 v_flex()
218 .h(px(properties.editor_height + padding_b + padding_t) + extra_space_hack)
219 .w_full()
220 .flex_none()
221 .rounded(properties.editor_border_radius())
222 .overflow_hidden()
223 .px_1p5()
224 .pt(px(padding_t))
225 .pb(px(padding_b))
226 .child(
227 div()
228 .h(px(properties.editor_height))
229 .w_full()
230 .child(self.commit_editor_element(window, cx)),
231 )
232 }
233
234 pub fn render_footer(&self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
235 let git_panel = self.git_panel.clone();
236
237 let (branch, can_commit, tooltip, commit_label, co_authors, generate_commit_message) =
238 self.git_panel.update(cx, |git_panel, cx| {
239 let branch = git_panel
240 .active_repository
241 .as_ref()
242 .and_then(|repo| {
243 repo.read(cx)
244 .repository_entry
245 .branch()
246 .map(|b| b.name.clone())
247 })
248 .unwrap_or_else(|| "<no branch>".into());
249 let (can_commit, tooltip) = git_panel.configure_commit_button(cx);
250 let title = git_panel.commit_button_title();
251 let co_authors = git_panel.render_co_authors(cx);
252 let generate_commit_message = git_panel.render_generate_commit_message_button(cx);
253 (
254 branch,
255 can_commit,
256 tooltip,
257 title,
258 co_authors,
259 generate_commit_message,
260 )
261 });
262
263 let branch_picker_button = panel_button(branch)
264 .icon(IconName::GitBranch)
265 .icon_size(IconSize::Small)
266 .icon_color(Color::Placeholder)
267 .color(Color::Muted)
268 .icon_position(IconPosition::Start)
269 .tooltip(Tooltip::for_action_title(
270 "Switch Branch",
271 &zed_actions::git::Branch,
272 ))
273 .on_click(cx.listener(|_, _, window, cx| {
274 window.dispatch_action(zed_actions::git::Branch.boxed_clone(), cx);
275 }))
276 .style(ButtonStyle::Transparent);
277
278 let branch_picker = PopoverMenu::new("popover-button")
279 .menu({
280 let branch_list = self.branch_list.clone();
281 move |_window, _cx| Some(branch_list.clone())
282 })
283 .trigger_with_tooltip(
284 branch_picker_button,
285 Tooltip::for_action_title("Switch Branch", &zed_actions::git::Branch),
286 )
287 .anchor(Corner::BottomLeft)
288 .offset(gpui::Point {
289 x: px(0.0),
290 y: px(-2.0),
291 });
292
293 let close_kb_hint =
294 if let Some(close_kb) = ui::KeyBinding::for_action(&menu::Cancel, window, cx) {
295 Some(
296 KeybindingHint::new(close_kb, cx.theme().colors().editor_background)
297 .suffix("Cancel"),
298 )
299 } else {
300 None
301 };
302
303 let panel_editor_focus_handle =
304 git_panel.update(cx, |git_panel, cx| git_panel.editor_focus_handle(cx));
305
306 let commit_button = panel_filled_button(commit_label)
307 .tooltip({
308 let panel_editor_focus_handle = panel_editor_focus_handle.clone();
309 move |window, cx| {
310 Tooltip::for_action_in(tooltip, &Commit, &panel_editor_focus_handle, window, cx)
311 }
312 })
313 .disabled(!can_commit)
314 .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| {
315 telemetry::event!("Git Committed", source = "Git Modal");
316 this.git_panel
317 .update(cx, |git_panel, cx| git_panel.commit_changes(window, cx));
318 cx.emit(DismissEvent);
319 }));
320
321 h_flex()
322 .group("commit_editor_footer")
323 .flex_none()
324 .w_full()
325 .items_center()
326 .justify_between()
327 .w_full()
328 .h(px(self.properties.footer_height))
329 .gap_1()
330 .child(
331 h_flex()
332 .gap_1()
333 .child(branch_picker)
334 .children(generate_commit_message)
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
354 fn commit(&mut self, _: &git::Commit, window: &mut Window, cx: &mut Context<Self>) {
355 telemetry::event!("Git Committed", source = "Git Modal");
356 self.git_panel
357 .update(cx, |git_panel, cx| git_panel.commit_changes(window, cx));
358 cx.emit(DismissEvent);
359 }
360}
361
362impl Render for CommitModal {
363 fn render(&mut self, window: &mut Window, cx: &mut Context<'_, Self>) -> impl IntoElement {
364 let properties = self.properties;
365 let width = px(properties.modal_width);
366 let container_padding = px(properties.container_padding);
367 let border_radius = properties.modal_border_radius;
368 let editor_focus_handle = self.commit_editor.focus_handle(cx);
369
370 v_flex()
371 .id("commit-modal")
372 .key_context("GitCommit")
373 .on_action(cx.listener(Self::dismiss))
374 .on_action(cx.listener(Self::commit))
375 .on_action(cx.listener(|this, _: &GenerateCommitMessage, _, cx| {
376 this.git_panel.update(cx, |panel, cx| {
377 panel.generate_commit_message(cx);
378 })
379 }))
380 .on_action(
381 cx.listener(|this, _: &zed_actions::git::Branch, window, cx| {
382 toggle_branch_picker(this, window, cx);
383 }),
384 )
385 .on_action(
386 cx.listener(|this, _: &zed_actions::git::CheckoutBranch, window, cx| {
387 toggle_branch_picker(this, window, cx);
388 }),
389 )
390 .on_action(
391 cx.listener(|this, _: &zed_actions::git::Switch, window, cx| {
392 toggle_branch_picker(this, window, cx);
393 }),
394 )
395 .elevation_3(cx)
396 .overflow_hidden()
397 .flex_none()
398 .relative()
399 .bg(cx.theme().colors().elevated_surface_background)
400 .rounded(px(border_radius))
401 .border_1()
402 .border_color(cx.theme().colors().border)
403 .w(width)
404 .p(container_padding)
405 .child(
406 v_flex()
407 .id("editor-container")
408 .justify_between()
409 .p_2()
410 .size_full()
411 .gap_2()
412 .rounded(properties.editor_border_radius())
413 .overflow_hidden()
414 .cursor_text()
415 .bg(cx.theme().colors().editor_background)
416 .border_1()
417 .border_color(cx.theme().colors().border_variant)
418 .on_click(cx.listener(move |_, _: &ClickEvent, window, _cx| {
419 window.focus(&editor_focus_handle);
420 }))
421 .child(
422 div()
423 .flex_1()
424 .size_full()
425 .child(self.render_commit_editor(window, cx)),
426 )
427 .child(self.render_footer(window, cx)),
428 )
429 }
430}
431
432fn toggle_branch_picker(
433 this: &mut CommitModal,
434 window: &mut Window,
435 cx: &mut Context<'_, CommitModal>,
436) {
437 this.branch_list.update(cx, |branch_list, cx| {
438 branch_list.popover_handle.toggle(window, cx);
439 })
440}