1use gpui::{
2 AnyElement, App, ElementId, Entity, FocusHandle, Hsla, InteractiveElement, IntoElement,
3 ParentElement, Pixels, RenderOnce, Role, ScrollHandle, SharedString, Size,
4 StatefulInteractiveElement, Styled, Window, anchored, div, point, prelude::FluentBuilder, px,
5};
6use gpui_base::{DialogChangeReason, TestSupportExt};
7use gpui_material_foundation::{
8 Scrim, Surface, ThemeScope, color, current_material_theme, isolated_scroll,
9};
10use gpui_material_theme::{ColorRole, CornerRole, CornerShape, Direction, Elevation, TypeRole};
11
12use crate::{Button, ButtonVariant, IconButton};
13
14use super::{
15 ActionSlot, Confirm, Dialog, DialogAction, DialogCloseReason, DialogColors, DialogProperties,
16 DialogState, DialogVariant, ModalSession, exiting_panel, modal_backdrop,
17};
18
19#[allow(clippy::too_many_arguments)]
20fn build_surface_shell(
21 max_height: Pixels,
22 text_color: Hsla,
23 colors: DialogColors,
24 tonal_elevation: Option<Elevation>,
25 shadow_elevation: Option<Elevation>,
26 shape: Option<CornerShape>,
27 full_screen: bool,
28 unstructured: bool,
29 native: bool,
30) -> Surface {
31 let role = if tonal_elevation.is_some() {
32 ColorRole::Surface
33 } else {
34 ColorRole::SurfaceContainerHigh
35 };
36 let mut surface = Surface::new(role)
37 .flex()
38 .flex_col()
39 .max_h(max_height)
40 .content_color(text_color);
41 if let Some(container) = colors.container {
42 surface = surface.container_color(container);
43 } else if unstructured && tonal_elevation.is_none() {
44 surface = surface.container_color(gpui::transparent_black());
45 }
46 if let Some(tonal) = tonal_elevation {
47 surface = surface.tonal_elevation(tonal);
48 }
49 if let Some(shape) = shape.or_else(|| {
50 (!full_screen && !unstructured && !native).then(|| CornerRole::ExtraLarge.shape())
51 }) {
52 surface = surface.shape(shape);
53 }
54 let shadow = shadow_elevation.unwrap_or(if full_screen || unstructured || native {
55 Elevation::Level0
56 } else {
57 Elevation::Level3
58 });
59 surface.shadow_elevation(shadow)
60}
61
62#[allow(clippy::too_many_arguments)]
63fn build_actions(
64 confirm: Option<(SharedString, Confirm)>,
65 confirm_content: Option<ActionSlot>,
66 confirm_action: DialogAction,
67 confirm_focus: &FocusHandle,
68 dismiss_content: Option<ActionSlot>,
69 dismiss_action: DialogAction,
70 cancel_focus: &FocusHandle,
71 cancel_label: SharedString,
72 show_cancel: bool,
73 full_screen: bool,
74) -> (Option<AnyElement>, Option<AnyElement>) {
75 let confirm_element = match confirm_content {
76 Some(content) => Some(content(confirm_action)),
77 None => confirm.map(|(label, _)| {
78 Button::new("confirm", label)
79 .track_focus(confirm_focus)
80 .variant(ButtonVariant::Text)
81 .on_activate(move |window, cx| confirm_action.activate(window, cx))
82 .into_any_element()
83 }),
84 };
85 let cancel_element = match dismiss_content {
86 Some(content) => Some(content(dismiss_action)),
87 None if show_cancel && full_screen => Some(
88 IconButton::new(
89 "cancel",
90 cancel_label,
91 crate::Icon::new(crate::IconName::Close),
92 )
93 .track_focus(cancel_focus)
94 .on_activate(move |window, cx| dismiss_action.activate(window, cx))
95 .into_any_element(),
96 ),
97 None if show_cancel => Some(
98 Button::new("cancel", cancel_label)
99 .track_focus(cancel_focus)
100 .variant(ButtonVariant::Text)
101 .on_activate(move |window, cx| dismiss_action.activate(window, cx))
102 .into_any_element(),
103 ),
104 None => None,
105 };
106 (cancel_element, confirm_element)
107}
108
109struct StructuredDialogParts {
110 icon: Option<AnyElement>,
111 heading: Option<gpui::Div>,
112 content: AnyElement,
113 cancel: Option<AnyElement>,
114 confirm: Option<AnyElement>,
115}
116
117fn populate_structured_surface(
118 surface: Surface,
119 parts: StructuredDialogParts,
120 width: Pixels,
121 viewport_height: Pixels,
122 full_screen: bool,
123 rtl: bool,
124) -> Surface {
125 let surface = surface.w(width);
126 if full_screen {
127 surface
128 .h(viewport_height)
129 .child(
130 div()
131 .id("dialog-app-bar")
132 .test_support()
133 .flex()
134 .flex_none()
135 .items_center()
136 .h(px(56.))
137 .px(px(4.))
138 .gap(px(8.))
139 .when(rtl, Styled::flex_row_reverse)
140 .children(parts.cancel)
141 .children(parts.icon)
142 .child(div().flex_1().min_w_0().children(parts.heading))
143 .children(parts.confirm),
144 )
145 .child(parts.content)
146 } else {
147 surface
148 .p(px(24.))
149 .children(parts.icon.map(|icon| {
150 div()
151 .flex_none()
152 .flex()
153 .justify_center()
154 .mb(px(16.))
155 .child(icon)
156 }))
157 .children(
158 parts
159 .heading
160 .map(|heading| div().flex_none().mb(px(16.)).child(heading)),
161 )
162 .child(parts.content)
163 .children(
164 (parts.cancel.is_some() || parts.confirm.is_some()).then(|| {
165 div()
166 .flex()
167 .flex_none()
168 .mt(px(24.))
169 .when(rtl, Styled::flex_row_reverse)
170 .justify_end()
171 .flex_wrap()
172 .gap(px(8.))
173 .children(parts.cancel)
174 .children(parts.confirm)
175 }),
176 )
177 }
178}
179
180#[allow(clippy::too_many_arguments)]
181fn build_structured_parts(
182 icon: Option<AnyElement>,
183 icon_color: Hsla,
184 show_title: bool,
185 title: SharedString,
186 title_content: Option<AnyElement>,
187 title_color: Hsla,
188 description: Option<SharedString>,
189 body: Option<AnyElement>,
190 scroll: &ScrollHandle,
191 cancel: Option<AnyElement>,
192 confirm: Option<AnyElement>,
193 full_screen: bool,
194 window: &Window,
195 cx: &App,
196) -> StructuredDialogParts {
197 let icon = icon.map(|icon| div().text_color(icon_color).child(icon).into_any_element());
198 let has_icon = icon.is_some();
199 let heading = show_title.then(|| {
200 let heading = title_content.unwrap_or_else(|| title.into_any_element());
201 super::super::text_style(
202 div()
203 .min_w_0()
204 .text_color(title_color)
205 .when(has_icon && !full_screen, Styled::text_center)
206 .child(heading),
207 if full_screen {
208 TypeRole::TitleLarge
209 } else {
210 TypeRole::HeadlineSmall
211 },
212 1.,
213 window,
214 cx,
215 )
216 });
217 let supporting = description.map(|description| {
218 super::super::text_style(
219 div().id("description").role(Role::Label).child(description),
220 TypeRole::BodyMedium,
221 1.,
222 window,
223 cx,
224 )
225 });
226 let content = isolated_scroll("dialog-content", scroll)
227 .test_support()
228 .min_h_0()
229 .flex()
230 .flex_col()
231 .gap(px(16.))
232 .when(full_screen, |content| content.flex_1().p(px(24.)))
233 .children(supporting)
234 .children(body)
235 .into_any_element();
236 StructuredDialogParts {
237 icon,
238 heading,
239 content,
240 cancel,
241 confirm,
242 }
243}
244
245#[allow(clippy::too_many_arguments)]
246fn render_exiting_dialog<E: StatefulInteractiveElement + ParentElement + IntoElement>(
247 id: ElementId,
248 state: &Entity<DialogState>,
249 panel: E,
250 backdrop: Scrim,
251 viewport: Size<Pixels>,
252 layer: usize,
253 window: &mut Window,
254 cx: &mut App,
255) -> AnyElement {
256 gpui::deferred(
257 anchored().position(point(px(0.), px(0.))).child(
258 div()
259 .id((id.clone(), "dialog-exit"))
260 .absolute()
261 .top_0()
262 .left_0()
263 .w(viewport.width)
264 .h(viewport.height)
265 .flex()
266 .items_center()
267 .justify_center()
268 .child(ThemeScope::inherit(backdrop, window, cx))
269 .child(ThemeScope::inherit(
270 exiting_panel(panel, &id, state, window, cx),
271 window,
272 cx,
273 )),
274 ),
275 )
276 .with_priority(10 + layer)
277 .into_any_element()
278}
279
280#[allow(clippy::too_many_arguments)]
281fn render_active_dialog<E: IntoElement>(
282 title: SharedString,
283 panel: E,
284 backdrop: Scrim,
285 focus: FocusHandle,
286 session: ModalSession,
287 confirm_action: DialogAction,
288 keyboard_confirmation: bool,
289 layer: usize,
290 native: bool,
291 properties: DialogProperties,
292 window: &mut Window,
293 cx: &mut App,
294) -> AnyElement {
295 if native {
296 let session = session.clone();
297 window.on_window_should_close(cx, move |window, cx| {
298 session.dismiss(DialogCloseReason::WindowClose, window, cx)
299 });
300 }
301 let outside = session.clone();
302 let keyboard_focus = focus.clone();
303 gpui_base::Dialog::new(cx)
304 .focus_handle(focus)
305 .layer(layer, true)
306 .accessibility_label(title)
307 .close_on_backdrop_press(false)
308 .backdrop(ThemeScope::inherit(
309 modal_backdrop(backdrop, move |window, cx| {
310 if !native && properties.dismiss_on_outside_click {
311 outside.dismiss(DialogCloseReason::OutsideClick, window, cx);
312 }
313 }),
314 window,
315 cx,
316 ))
317 .on_ok(move |_, window, cx| {
318 if keyboard_confirmation && keyboard_focus.is_focused(window) {
319 confirm_action.activate(window, cx);
320 } else {
321 cx.propagate();
323 }
324 false
325 })
326 .on_open_change(move |_, reason, window, cx| {
327 if reason == DialogChangeReason::Cancel && properties.dismiss_on_escape {
328 session.dismiss(DialogCloseReason::Escape, window, cx);
329 }
330 })
331 .popup(ThemeScope::inherit(panel, window, cx))
332 .into_any_element()
333}
334
335impl RenderOnce for Dialog {
336 fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement {
337 let state = self.state.read(cx);
338 let native = state.native.is_some();
339 if let Some(native) = &state.native
340 && native.child.window_id() != window.window_handle().window_id()
341 {
342 return div().into_any_element();
343 }
344 let open = state.open;
345 let focus = state.focus.clone();
346 let scroll = state.scroll.clone();
347 let cancel_focus = state.cancel_focus.clone();
348 let confirm_focus = state.confirm_focus.clone();
349 let session = ModalSession::new(
350 &self.state,
351 self.dismiss_request.take(),
352 self.on_close.take(),
353 cx,
354 );
355 let presence = crate::motion::presence(&self.id, open, 200, window, cx);
356 if !presence.should_render() {
357 return div().into_any_element();
358 }
359 let progress = if native { 1. } else { presence.progress };
360 let viewport = window.viewport_size();
361 let full_screen = self.variant == DialogVariant::FullScreen && viewport.width < px(600.);
362 let window_filling = full_screen || native;
363 let margin = if window_filling { px(0.) } else { px(48.) };
364 let available_width = (viewport.width - margin).max(px(0.));
365 let width = if full_screen {
366 available_width
367 } else {
368 self.width.min(available_width)
369 };
370 let max_height = (viewport.height - margin).max(px(0.));
371 let motion_offset = px((1. - progress) * if full_screen { 32. } else { 8. });
372 let theme = current_material_theme(window, cx);
373 let rtl = theme.layout.direction == Direction::Rtl;
374 let colors = &theme.colors;
375 let icon_color = self
376 .colors
377 .icon
378 .unwrap_or_else(|| color(colors.color(ColorRole::Secondary)));
379 let title_color = self
380 .colors
381 .title
382 .unwrap_or_else(|| color(colors.color(ColorRole::OnSurface)));
383 let text_color = self
384 .colors
385 .text
386 .unwrap_or_else(|| color(colors.color(ColorRole::OnSurfaceVariant)));
387
388 let confirm_action = DialogAction {
389 session: session.clone(),
390 reason: DialogCloseReason::Confirm,
391 validate: self
392 .confirm
393 .as_ref()
394 .map(|(_, validator)| validator.clone()),
395 };
396 let dismiss_action = DialogAction {
397 session: session.clone(),
398 reason: DialogCloseReason::Cancel,
399 validate: None,
400 };
401 let keyboard_confirmation =
402 self.confirm.is_some() && self.confirm_content.is_none() && !self.unstructured;
403 let body = self
404 .body
405 .map(|body| body(confirm_action.clone(), dismiss_action.clone()));
406 let mut surface = build_surface_shell(
407 max_height,
408 text_color,
409 self.colors,
410 self.tonal_elevation,
411 self.shadow_elevation,
412 self.shape,
413 full_screen,
414 self.unstructured,
415 native,
416 );
417 if self.unstructured {
418 surface = surface
419 .min_w(px(280.).min(width))
420 .max_w(width)
421 .children(body);
422 } else {
423 let (cancel, confirm) = build_actions(
424 self.confirm,
425 self.confirm_content,
426 confirm_action.clone(),
427 &confirm_focus,
428 self.dismiss_content,
429 dismiss_action,
430 &cancel_focus,
431 self.cancel,
432 self.show_cancel,
433 full_screen,
434 );
435 let parts = build_structured_parts(
436 self.icon,
437 icon_color,
438 self.show_title,
439 self.title.clone(),
440 self.title_content,
441 title_color,
442 self.description,
443 body,
444 &scroll,
445 cancel,
446 confirm,
447 full_screen,
448 window,
449 cx,
450 );
451 surface = populate_structured_surface(
452 surface,
453 parts,
454 width,
455 viewport.height,
456 full_screen,
457 rtl,
458 );
459 }
460
461 let panel = div()
462 .id(self.id.clone())
463 .test_support()
464 .relative()
465 .max_w(available_width)
466 .max_h(max_height)
467 .opacity(progress)
468 .mt(motion_offset)
469 .when(open, |panel| panel.occlude())
470 .child(surface);
471 let backdrop = Scrim::new((self.id.clone(), "scrim"))
472 .opacity(if native {
473 0.
474 } else {
475 self.scrim_opacity * progress
476 })
477 .when_some(self.scrim_color, Scrim::color);
478 if !open {
479 return render_exiting_dialog(
480 self.id,
481 &self.state,
482 panel,
483 backdrop,
484 viewport,
485 self.layer,
486 window,
487 cx,
488 );
489 }
490 render_active_dialog(
491 self.title,
492 panel,
493 backdrop,
494 focus,
495 session,
496 confirm_action,
497 keyboard_confirmation,
498 self.layer,
499 native,
500 self.properties,
501 window,
502 cx,
503 )
504 }
505}