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