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