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 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) =
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 (branch, can_commit, tooltip, title, co_authors)
253 });
254
255 let branch_picker_button = panel_button(branch)
256 .icon(IconName::GitBranch)
257 .icon_size(IconSize::Small)
258 .icon_color(Color::Placeholder)
259 .color(Color::Muted)
260 .icon_position(IconPosition::Start)
261 .tooltip(Tooltip::for_action_title(
262 "Switch Branch",
263 &zed_actions::git::Branch,
264 ))
265 .on_click(cx.listener(|_, _, window, cx| {
266 window.dispatch_action(zed_actions::git::Branch.boxed_clone(), cx);
267 }))
268 .style(ButtonStyle::Transparent);
269
270 let branch_picker = PopoverMenu::new("popover-button")
271 .menu({
272 let branch_list = self.branch_list.clone();
273 move |_window, _cx| Some(branch_list.clone())
274 })
275 .trigger_with_tooltip(
276 branch_picker_button,
277 Tooltip::for_action_title("Switch Branch", &zed_actions::git::Branch),
278 )
279 .anchor(Corner::BottomLeft)
280 .offset(gpui::Point {
281 x: px(0.0),
282 y: px(-2.0),
283 });
284
285 let close_kb_hint =
286 if let Some(close_kb) = ui::KeyBinding::for_action(&menu::Cancel, window, cx) {
287 Some(
288 KeybindingHint::new(close_kb, cx.theme().colors().editor_background)
289 .suffix("Cancel"),
290 )
291 } else {
292 None
293 };
294
295 let panel_editor_focus_handle =
296 git_panel.update(cx, |git_panel, cx| git_panel.editor_focus_handle(cx));
297
298 let commit_button = panel_filled_button(commit_label)
299 .tooltip(move |window, cx| {
300 Tooltip::for_action_in(tooltip, &Commit, &panel_editor_focus_handle, window, cx)
301 })
302 .disabled(!can_commit)
303 .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| {
304 telemetry::event!("Git Committed", source = "Git Modal");
305 this.git_panel
306 .update(cx, |git_panel, cx| git_panel.commit_changes(window, cx));
307 cx.emit(DismissEvent);
308 }));
309
310 h_flex()
311 .group("commit_editor_footer")
312 .flex_none()
313 .w_full()
314 .items_center()
315 .justify_between()
316 .w_full()
317 .h(px(self.properties.footer_height))
318 .gap_1()
319 .child(h_flex().gap_1().child(branch_picker).children(co_authors))
320 .child(div().flex_1())
321 .child(
322 h_flex()
323 .items_center()
324 .justify_end()
325 .flex_none()
326 .px_1()
327 .gap_4()
328 .children(close_kb_hint)
329 .child(commit_button),
330 )
331 }
332
333 fn dismiss(&mut self, _: &menu::Cancel, _: &mut Window, cx: &mut Context<Self>) {
334 cx.emit(DismissEvent);
335 }
336
337 fn commit(&mut self, _: &git::Commit, window: &mut Window, cx: &mut Context<Self>) {
338 telemetry::event!("Git Committed", source = "Git Modal");
339 self.git_panel
340 .update(cx, |git_panel, cx| git_panel.commit_changes(window, cx));
341 cx.emit(DismissEvent);
342 }
343}
344
345impl Render for CommitModal {
346 fn render(&mut self, window: &mut Window, cx: &mut Context<'_, Self>) -> impl IntoElement {
347 let properties = self.properties;
348 let width = px(properties.modal_width);
349 let container_padding = px(properties.container_padding);
350 let border_radius = properties.modal_border_radius;
351 let editor_focus_handle = self.commit_editor.focus_handle(cx);
352
353 v_flex()
354 .id("commit-modal")
355 .key_context("GitCommit")
356 .on_action(cx.listener(Self::dismiss))
357 .on_action(cx.listener(Self::commit))
358 .on_action(
359 cx.listener(|this, _: &zed_actions::git::Branch, window, cx| {
360 this.branch_list.update(cx, |branch_list, cx| {
361 branch_list.popover_handle.toggle(window, cx);
362 })
363 }),
364 )
365 .elevation_3(cx)
366 .overflow_hidden()
367 .flex_none()
368 .relative()
369 .bg(cx.theme().colors().elevated_surface_background)
370 .rounded(px(border_radius))
371 .border_1()
372 .border_color(cx.theme().colors().border)
373 .w(width)
374 .p(container_padding)
375 .child(
376 v_flex()
377 .id("editor-container")
378 .justify_between()
379 .p_2()
380 .size_full()
381 .gap_2()
382 .rounded(properties.editor_border_radius())
383 .overflow_hidden()
384 .cursor_text()
385 .bg(cx.theme().colors().editor_background)
386 .border_1()
387 .border_color(cx.theme().colors().border_variant)
388 .on_click(cx.listener(move |_, _: &ClickEvent, window, _cx| {
389 window.focus(&editor_focus_handle);
390 }))
391 .child(
392 div()
393 .flex_1()
394 .size_full()
395 .child(self.render_commit_editor(window, cx)),
396 )
397 .child(self.render_footer(window, cx)),
398 )
399 }
400}