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