1use std::{cell::RefCell, rc::Rc};
2
3use gpui::{
4 AnyElement, AnyWindowHandle, App, AppContext, Context, ElementId, Entity, FocusHandle, Hsla,
5 InteractiveElement, IntoElement, MouseButton, ParentElement, Pixels, Render, RenderOnce, Role,
6 ScrollHandle, SharedString, StatefulInteractiveElement, Styled, Subscription, WeakEntity,
7 WeakFocusHandle, Window, WindowKind, WindowOptions, anchored, div, point,
8 prelude::FluentBuilder, px,
9};
10use gpui_base::{DialogChangeReason, TestSupportExt, active_focus_trap};
11use gpui_material_foundation::{
12 MaterialRoot, Scrim, Surface, color, isolated_scroll, set_window_material_theme,
13 window_material_theme,
14};
15use gpui_material_theme::{ColorRole, CornerRole, CornerShape, Direction, Elevation, TypeRole};
16
17use crate::{Button, ButtonVariant, IconButton};
18
19pub(super) fn exiting_panel<E: StatefulInteractiveElement + ParentElement>(
20 panel: E,
21 id: &ElementId,
22 state: &Entity<DialogState>,
23 window: &mut Window,
24 cx: &mut App,
25) -> E {
26 let focus = state.read(cx).focus.clone();
27 let guard = window.use_keyed_state((id.clone(), "exiting-focus"), cx, |_, _| {
30 let previous = Rc::new(RefCell::new(None::<WeakFocusHandle>));
31 (previous, None::<Subscription>)
32 });
33 let target = window
34 .focused(cx)
35 .filter(|handle| !focus.contains(handle, window));
36 guard.update(cx, |(previous, guard), cx| {
37 if let Some(target) = target {
38 *previous.borrow_mut() = Some(target.downgrade());
39 }
40 if guard.is_none() {
41 let previous = previous.clone();
42 let state = state.downgrade();
43 *guard = Some(window.on_focus_in(&focus, cx, move |window, cx| {
44 if state
46 .upgrade()
47 .is_some_and(|state| state.read(cx).is_open())
48 {
49 return;
50 }
51 if let Some(handle) = previous
52 .borrow()
53 .as_ref()
54 .and_then(WeakFocusHandle::upgrade)
55 {
56 handle.focus(window, cx);
57 } else {
58 window.blur(cx);
59 }
60 }));
61 }
62 });
63 panel
64 .role(Role::Group)
65 .track_focus(&focus)
66 .capture_key_down(|_, _, cx| cx.stop_propagation())
67 .a11y_synthetic_children(|tree| tree.parent_node().set_hidden())
68 .child(div().absolute().inset_0().occlude())
69}
70
71pub(super) fn modal_backdrop(
73 scrim: Scrim,
74 dismiss: impl Fn(&mut Window, &mut App) + 'static,
75) -> impl IntoElement {
76 div()
77 .absolute()
78 .inset_0()
79 .occlude()
80 .on_any_mouse_down(move |event, window, cx| {
81 cx.stop_propagation();
82 if event.button == MouseButton::Left {
83 dismiss(window, cx);
84 }
85 })
86 .on_scroll(|_, _, cx| cx.stop_propagation())
87 .child(scrim)
88}
89
90#[derive(Clone, Copy, Debug, Eq, PartialEq)]
92pub enum DialogCloseReason {
93 Confirm,
94 Cancel,
95 Escape,
96 OutsideClick,
97 WindowClose,
98 Programmatic,
99}
100
101struct NativeDialog {
102 parent: AnyWindowHandle,
103 child: AnyWindowHandle,
104 _closed: Subscription,
105}
106
107pub struct DialogState {
109 pub(super) focus: FocusHandle,
110 previous: Option<FocusHandle>,
111 cancel_focus: FocusHandle,
112 confirm_focus: FocusHandle,
113 open: bool,
114 generation: u64,
115 last_close_reason: Option<DialogCloseReason>,
116 native: Option<NativeDialog>,
117 pub(super) scroll: ScrollHandle,
118}
119
120impl DialogState {
121 pub fn new(cx: &mut Context<Self>) -> Self {
122 Self {
123 focus: cx.focus_handle(),
124 previous: None,
125 cancel_focus: cx.focus_handle(),
126 confirm_focus: cx.focus_handle(),
127 open: false,
128 generation: 0,
129 last_close_reason: None,
130 native: None,
131 scroll: ScrollHandle::new(),
132 }
133 }
134
135 pub fn is_open(&self) -> bool {
136 self.open
137 }
138
139 pub fn last_close_reason(&self) -> Option<DialogCloseReason> {
141 self.last_close_reason
142 }
143
144 fn begin(
145 &mut self,
146 previous: Option<FocusHandle>,
147 window: &mut Window,
148 cx: &mut Context<Self>,
149 ) {
150 self.previous = previous;
151 self.open = true;
152 self.generation = self.generation.wrapping_add(1);
153 self.focus.focus(window, cx);
154 cx.notify();
155 }
156
157 pub fn open(&mut self, window: &mut Window, cx: &mut Context<Self>) {
159 if !self.open {
160 self.begin(window.focused(cx), window, cx);
161 }
162 }
163
164 pub fn close(&mut self, window: &mut Window, cx: &mut Context<Self>) {
167 self.finish(DialogCloseReason::Programmatic, window, cx);
168 }
169
170 fn finish(&mut self, reason: DialogCloseReason, window: &mut Window, cx: &mut Context<Self>) {
171 if !self.open {
172 return;
173 }
174 self.open = false;
175 self.last_close_reason = Some(reason);
176 let previous = self.previous.take();
177 if let Some(native) = self.native.take() {
178 if reason != DialogCloseReason::WindowClose {
179 if native.child.window_id() == window.window_handle().window_id() {
180 window.remove_window();
181 } else {
182 native
183 .child
184 .update(cx, |_, window, _| window.remove_window())
185 .ok();
186 }
187 }
188 restore_parent(native.parent, previous, cx);
189 } else if self.focus.contains_focused(window, cx) || self.focus.is_focused(window) {
190 if let Some(previous) = previous {
191 previous.focus(window, cx);
192 } else {
193 window.blur(cx);
194 }
195 }
196 cx.notify();
197 }
198}
199
200fn restore_parent(parent: AnyWindowHandle, previous: Option<FocusHandle>, cx: &mut App) {
201 if let Some(previous) = previous {
202 cx.defer(move |cx| {
203 parent
204 .update(cx, |_, window, cx| {
205 if window.focused(cx).is_none() {
206 previous.focus(window, cx);
207 }
208 })
209 .ok();
210 });
211 }
212}
213
214#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
216pub enum DialogVariant {
217 #[default]
218 Basic,
219 FullScreen,
222}
223
224#[derive(Clone, Copy, Debug, Eq, PartialEq)]
226pub struct DialogProperties {
227 pub dismiss_on_escape: bool,
228 pub dismiss_on_outside_click: bool,
229}
230
231impl Default for DialogProperties {
232 fn default() -> Self {
233 Self {
234 dismiss_on_escape: true,
235 dismiss_on_outside_click: true,
236 }
237 }
238}
239
240#[derive(Clone, Copy, Debug, Default)]
242pub struct DialogColors {
243 pub container: Option<Hsla>,
244 pub icon: Option<Hsla>,
245 pub title: Option<Hsla>,
246 pub text: Option<Hsla>,
247}
248
249type Confirm = Rc<dyn Fn(&mut Window, &mut App) -> bool>;
250pub(super) type ModalDismissRequest = Rc<dyn Fn(DialogCloseReason, &mut Window, &mut App) -> bool>;
251pub(super) type ModalClosed = Rc<dyn Fn(DialogCloseReason, &mut Window, &mut App)>;
252type ActionSlot = Box<dyn FnOnce(DialogAction) -> AnyElement>;
253type BodySlot = Box<dyn FnOnce(DialogAction, DialogAction) -> AnyElement>;
254
255#[derive(Clone)]
256pub(super) struct ModalSession {
257 state: WeakEntity<DialogState>,
258 generation: u64,
259 dismiss_request: Option<ModalDismissRequest>,
260 on_close: Option<ModalClosed>,
261}
262
263impl ModalSession {
264 pub(super) fn new(
265 state: &Entity<DialogState>,
266 dismiss_request: Option<ModalDismissRequest>,
267 on_close: Option<ModalClosed>,
268 cx: &App,
269 ) -> Self {
270 Self {
271 state: state.downgrade(),
272 generation: state.read(cx).generation,
273 dismiss_request,
274 on_close,
275 }
276 }
277
278 fn current(&self, window: &Window, cx: &App) -> bool {
279 self.state.upgrade().is_some_and(|state| {
280 let dialog = state.read(cx);
281 dialog.open
282 && dialog.generation == self.generation
283 && dialog.focus.contains_focused(window, cx)
284 && active_focus_trap(window, cx).is_none_or(|focus| focus == dialog.focus)
285 })
286 }
287
288 pub(super) fn dismiss(
289 &self,
290 reason: DialogCloseReason,
291 window: &mut Window,
292 cx: &mut App,
293 ) -> bool {
294 self.current(window, cx)
295 && self
296 .dismiss_request
297 .as_ref()
298 .is_none_or(|request| request(reason, window, cx))
299 && self.finish(reason, window, cx)
300 }
301
302 fn finish(&self, reason: DialogCloseReason, window: &mut Window, cx: &mut App) -> bool {
303 let Some(state) = self.state.upgrade() else {
304 return false;
305 };
306 let accepted = state.update(cx, |state, cx| {
307 if !state.open || state.generation != self.generation {
308 return false;
309 }
310 state.finish(reason, window, cx);
311 true
312 });
313 if accepted && let Some(on_close) = &self.on_close {
314 on_close(reason, window, cx);
315 }
316 accepted
317 }
318}
319
320#[derive(Clone)]
322pub struct DialogAction {
323 session: ModalSession,
324 reason: DialogCloseReason,
325 validate: Option<Confirm>,
326}
327
328impl DialogAction {
329 pub fn activate(&self, window: &mut Window, cx: &mut App) {
331 if self.session.current(window, cx)
332 && self
333 .validate
334 .as_ref()
335 .is_none_or(|validate| validate(window, cx))
336 {
337 self.session.finish(self.reason, window, cx);
338 }
339 }
340}
341
342#[derive(IntoElement)]
344pub struct Dialog {
345 id: ElementId,
346 state: Entity<DialogState>,
347 title: SharedString,
348 show_title: bool,
349 title_content: Option<AnyElement>,
350 icon: Option<AnyElement>,
351 description: Option<SharedString>,
352 body: Option<BodySlot>,
353 variant: DialogVariant,
354 unstructured: bool,
355 confirm: Option<(SharedString, Confirm)>,
356 confirm_content: Option<ActionSlot>,
357 dismiss_content: Option<ActionSlot>,
358 cancel: SharedString,
359 show_cancel: bool,
360 layer: usize,
361 properties: DialogProperties,
362 shape: Option<CornerShape>,
363 colors: DialogColors,
364 tonal_elevation: Option<Elevation>,
365 shadow_elevation: Option<Elevation>,
366 scrim_color: Option<Hsla>,
367 scrim_opacity: f32,
368 width: Pixels,
369 dismiss_request: Option<ModalDismissRequest>,
370 on_close: Option<ModalClosed>,
371}
372
373impl Dialog {
374 pub fn new(
375 id: impl Into<ElementId>,
376 state: &Entity<DialogState>,
377 title: impl Into<SharedString>,
378 ) -> Self {
379 Self {
380 id: id.into(),
381 state: state.clone(),
382 title: title.into(),
383 show_title: true,
384 title_content: None,
385 icon: None,
386 description: None,
387 body: None,
388 variant: DialogVariant::Basic,
389 unstructured: false,
390 confirm: None,
391 confirm_content: None,
392 dismiss_content: None,
393 cancel: "Cancel".into(),
394 show_cancel: true,
395 layer: 0,
396 properties: DialogProperties::default(),
397 shape: None,
398 colors: DialogColors::default(),
399 tonal_elevation: None,
400 shadow_elevation: None,
401 scrim_color: None,
402 scrim_opacity: 0.32,
403 width: px(560.),
404 dismiss_request: None,
405 on_close: None,
406 }
407 }
408
409 pub fn unstructured(
411 id: impl Into<ElementId>,
412 state: &Entity<DialogState>,
413 accessibility_label: impl Into<SharedString>,
414 ) -> Self {
415 Self {
416 unstructured: true,
417 ..Self::new(id, state, accessibility_label)
418 }
419 }
420
421 pub fn open_window(
425 state: &Entity<DialogState>,
426 parent: &mut Window,
427 mut options: WindowOptions,
428 cx: &mut App,
429 build: impl Fn(&Entity<DialogState>, &mut Window, &mut App) -> Self + 'static,
430 ) -> gpui::Result<Option<AnyWindowHandle>> {
431 let current = state.read(cx);
432 if current.is_open() {
433 return Ok(current.native.as_ref().map(|native| native.child));
434 }
435 let parent_handle = parent.window_handle();
436 if cx
437 .active_window()
438 .is_none_or(|active| active.window_id() != parent_handle.window_id())
439 {
440 return Err(std::io::Error::new(
441 std::io::ErrorKind::InvalidInput,
442 "the dialog's parent window must be active",
443 )
444 .into());
445 }
446 options.kind = WindowKind::Dialog;
447 options.focus = true;
448 options.show = true;
449 let previous = parent.focused(cx);
450 let theme = window_material_theme(parent, cx).clone();
451 let state = state.clone();
452 let window = MaterialRoot::open_window(options, cx, move |window, cx| {
453 set_window_material_theme(Some(theme), window, cx);
454 let child = window.window_handle();
455 let weak = state.downgrade();
456 let closed = cx.on_window_closed(move |cx, id| {
457 if id == child.window_id()
458 && let Some(state) = weak.upgrade()
459 {
460 state.update(cx, |state, cx| {
461 if let Some(native) = state.native.take() {
462 state.open = false;
463 state.last_close_reason = Some(DialogCloseReason::Programmatic);
464 restore_parent(native.parent, state.previous.take(), cx);
465 cx.notify();
466 }
467 });
468 }
469 });
470 state.update(cx, |state, cx| {
471 state.native = Some(NativeDialog {
472 parent: parent_handle,
473 child,
474 _closed: closed,
475 });
476 state.begin(previous, window, cx);
477 });
478 cx.new(|cx: &mut Context<NativeDialogView>| {
479 cx.observe(&state, |_, _, cx| cx.notify()).detach();
480 NativeDialogView {
481 state,
482 build: Box::new(build),
483 }
484 })
485 })?;
486 Ok(Some(window.into()))
487 }
488
489 pub fn variant(mut self, variant: DialogVariant) -> Self {
490 self.variant = variant;
491 self
492 }
493
494 pub fn icon(mut self, icon: impl IntoElement) -> Self {
495 self.icon = Some(icon.into_any_element());
496 self
497 }
498
499 pub fn title_content(mut self, title: impl IntoElement) -> Self {
501 self.title_content = Some(title.into_any_element());
502 self
503 }
504
505 pub fn show_title(mut self, show: bool) -> Self {
506 self.show_title = show;
507 self
508 }
509
510 pub fn description(mut self, description: impl Into<SharedString>) -> Self {
511 self.description = Some(description.into());
512 self
513 }
514
515 pub fn body(mut self, body: impl IntoElement) -> Self {
516 let body = body.into_any_element();
517 self.body = Some(Box::new(|_, _| body));
518 self
519 }
520
521 pub fn body_content<E: IntoElement>(
523 mut self,
524 body: impl FnOnce(DialogAction, DialogAction) -> E + 'static,
525 ) -> Self {
526 self.body = Some(Box::new(move |confirm, dismiss| {
527 body(confirm, dismiss).into_any_element()
528 }));
529 self
530 }
531
532 pub fn confirm(
534 mut self,
535 label: impl Into<SharedString>,
536 action: impl Fn(&mut Window, &mut App) -> bool + 'static,
537 ) -> Self {
538 self.confirm = Some((label.into(), Rc::new(action)));
539 self
540 }
541
542 pub fn confirm_content<E: IntoElement>(
544 mut self,
545 content: impl FnOnce(DialogAction) -> E + 'static,
546 ) -> Self {
547 self.confirm_content = Some(Box::new(move |confirm| content(confirm).into_any_element()));
548 self
549 }
550
551 pub fn dismiss_content<E: IntoElement>(
553 mut self,
554 content: impl FnOnce(DialogAction) -> E + 'static,
555 ) -> Self {
556 self.dismiss_content = Some(Box::new(move |dismiss| content(dismiss).into_any_element()));
557 self
558 }
559
560 pub fn cancel_label(mut self, label: impl Into<SharedString>) -> Self {
561 self.cancel = label.into();
562 self
563 }
564
565 pub fn show_cancel(mut self, show: bool) -> Self {
567 self.show_cancel = show;
568 self
569 }
570
571 pub fn layer(mut self, layer: usize) -> Self {
573 self.layer = layer;
574 self
575 }
576
577 pub fn properties(mut self, properties: DialogProperties) -> Self {
578 self.properties = properties;
579 self
580 }
581
582 pub fn on_dismiss_request(
584 mut self,
585 request: impl Fn(DialogCloseReason, &mut Window, &mut App) -> bool + 'static,
586 ) -> Self {
587 self.dismiss_request = Some(Rc::new(request));
588 self
589 }
590
591 pub fn on_close(
593 mut self,
594 close: impl Fn(DialogCloseReason, &mut Window, &mut App) + 'static,
595 ) -> Self {
596 self.on_close = Some(Rc::new(close));
597 self
598 }
599
600 pub fn shape(mut self, shape: CornerShape) -> Self {
601 self.shape = Some(shape);
602 self
603 }
604
605 pub fn colors(mut self, colors: DialogColors) -> Self {
606 self.colors = colors;
607 self
608 }
609
610 pub fn tonal_elevation(mut self, elevation: Elevation) -> Self {
612 self.tonal_elevation = Some(elevation);
613 self
614 }
615
616 pub fn shadow_elevation(mut self, elevation: Elevation) -> Self {
617 self.shadow_elevation = Some(elevation);
618 self
619 }
620
621 pub fn scrim_color(mut self, color: Hsla) -> Self {
622 self.scrim_color = Some(color);
623 self
624 }
625
626 pub fn scrim_opacity(mut self, opacity: f32) -> Self {
628 self.scrim_opacity = if opacity.is_nan() {
629 0.32
630 } else {
631 opacity.clamp(0., 1.)
632 };
633 self
634 }
635
636 pub fn width(mut self, width: Pixels) -> Self {
638 if f32::from(width).is_finite() {
639 self.width = width.max(px(0.));
640 }
641 self
642 }
643}
644
645type BuildNativeDialog = Box<dyn Fn(&Entity<DialogState>, &mut Window, &mut App) -> Dialog>;
646
647struct NativeDialogView {
648 state: Entity<DialogState>,
649 build: BuildNativeDialog,
650}
651
652impl Render for NativeDialogView {
653 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
654 div()
655 .size_full()
656 .child((self.build)(&self.state, window, cx))
657 }
658}
659
660impl RenderOnce for Dialog {
661 fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement {
662 let state = self.state.read(cx);
663 let native = state.native.is_some();
664 if let Some(native) = &state.native
665 && native.child.window_id() != window.window_handle().window_id()
666 {
667 return div().into_any_element();
668 }
669 let open = state.open;
670 let focus = state.focus.clone();
671 let scroll = state.scroll.clone();
672 let cancel_focus = state.cancel_focus.clone();
673 let confirm_focus = state.confirm_focus.clone();
674 let session = ModalSession::new(
675 &self.state,
676 self.dismiss_request.take(),
677 self.on_close.take(),
678 cx,
679 );
680 let presence = crate::motion::presence(&self.id, open, 200, window, cx);
681 if !presence.should_render() {
682 return div().into_any_element();
683 }
684 let progress = if native { 1. } else { presence.progress };
685 let viewport = window.viewport_size();
686 let full_screen = self.variant == DialogVariant::FullScreen && viewport.width < px(600.);
687 let window_filling = full_screen || native;
688 let margin = if window_filling { px(0.) } else { px(48.) };
689 let available_width = (viewport.width - margin).max(px(0.));
690 let width = if full_screen {
691 available_width
692 } else {
693 self.width.min(available_width)
694 };
695 let max_height = (viewport.height - margin).max(px(0.));
696 let motion_offset = px((1. - progress) * if full_screen { 32. } else { 8. });
697 let theme = window_material_theme(window, cx);
698 let rtl = theme.layout.direction == Direction::Rtl;
699 let colors = &theme.colors;
700 let icon_color = self
701 .colors
702 .icon
703 .unwrap_or_else(|| color(colors.color(ColorRole::Secondary)));
704 let title_color = self
705 .colors
706 .title
707 .unwrap_or_else(|| color(colors.color(ColorRole::OnSurface)));
708 let text_color = self
709 .colors
710 .text
711 .unwrap_or_else(|| color(colors.color(ColorRole::OnSurfaceVariant)));
712
713 let confirm_action = DialogAction {
714 session: session.clone(),
715 reason: DialogCloseReason::Confirm,
716 validate: self
717 .confirm
718 .as_ref()
719 .map(|(_, validator)| validator.clone()),
720 };
721 let dismiss_action = DialogAction {
722 session: session.clone(),
723 reason: DialogCloseReason::Cancel,
724 validate: None,
725 };
726 let keyboard_confirmation =
727 self.confirm.is_some() && self.confirm_content.is_none() && !self.unstructured;
728 let body = self
729 .body
730 .map(|body| body(confirm_action.clone(), dismiss_action.clone()));
731 let role = if self.tonal_elevation.is_some() {
732 ColorRole::Surface
733 } else {
734 ColorRole::SurfaceContainerHigh
735 };
736 let mut surface = Surface::new(role)
737 .flex()
738 .flex_col()
739 .max_h(max_height)
740 .content_color(text_color);
741 if let Some(container) = self.colors.container {
742 surface = surface.container_color(container);
743 } else if self.unstructured && self.tonal_elevation.is_none() {
744 surface = surface.container_color(gpui::transparent_black());
745 }
746 if let Some(tonal) = self.tonal_elevation {
747 surface = surface.tonal_elevation(tonal);
748 }
749 if let Some(shape) = self.shape.or_else(|| {
750 (!full_screen && !self.unstructured && !native).then(|| CornerRole::ExtraLarge.shape())
751 }) {
752 surface = surface.shape(shape);
753 }
754 let shadow =
755 self.shadow_elevation
756 .unwrap_or(if full_screen || self.unstructured || native {
757 Elevation::Level0
758 } else {
759 Elevation::Level3
760 });
761 surface = surface.shadow_elevation(shadow);
762 if self.unstructured {
763 surface = surface
764 .min_w(px(280.).min(width))
765 .max_w(width)
766 .children(body);
767 } else {
768 let icon = self
769 .icon
770 .map(|icon| div().text_color(icon_color).child(icon));
771 let has_icon = icon.is_some();
772 let heading = self.show_title.then(|| {
773 let heading = self
774 .title_content
775 .unwrap_or_else(|| self.title.clone().into_any_element());
776 super::text_style(
777 div()
778 .min_w_0()
779 .text_color(title_color)
780 .when(has_icon && !full_screen, Styled::text_center)
781 .child(heading),
782 if full_screen {
783 TypeRole::TitleLarge
784 } else {
785 TypeRole::HeadlineSmall
786 },
787 1.,
788 window,
789 cx,
790 )
791 });
792 let supporting = self.description.map(|description| {
793 super::text_style(
794 div().id("description").role(Role::Label).child(description),
795 TypeRole::BodyMedium,
796 1.,
797 window,
798 cx,
799 )
800 });
801 let confirm_for_button = confirm_action.clone();
802 let confirm = match self.confirm_content {
803 Some(content) => Some(content(confirm_for_button)),
804 None => self.confirm.map(|(label, _)| {
805 Button::new("confirm", label)
806 .track_focus(&confirm_focus)
807 .variant(ButtonVariant::Text)
808 .on_activate(move |window, cx| confirm_for_button.activate(window, cx))
809 .into_any_element()
810 }),
811 };
812 let cancel = match self.dismiss_content {
813 Some(content) => Some(content(dismiss_action)),
814 None if self.show_cancel && full_screen => Some(
815 IconButton::new(
816 "cancel",
817 self.cancel,
818 crate::Icon::new(crate::IconName::Close),
819 )
820 .track_focus(&cancel_focus)
821 .on_activate(move |window, cx| dismiss_action.activate(window, cx))
822 .into_any_element(),
823 ),
824 None if self.show_cancel => Some(
825 Button::new("cancel", self.cancel)
826 .track_focus(&cancel_focus)
827 .variant(ButtonVariant::Text)
828 .on_activate(move |window, cx| dismiss_action.activate(window, cx))
829 .into_any_element(),
830 ),
831 None => None,
832 };
833 let content = isolated_scroll("dialog-content", &scroll)
834 .test_support()
835 .min_h_0()
836 .flex()
837 .flex_col()
838 .gap(px(16.))
839 .children(supporting)
840 .children(body);
841 surface = surface.w(width);
842 if full_screen {
843 surface = surface
844 .h(viewport.height)
845 .child(
846 div()
847 .id("dialog-app-bar")
848 .test_support()
849 .flex()
850 .flex_none()
851 .items_center()
852 .h(px(56.))
853 .px(px(4.))
854 .gap(px(8.))
855 .when(rtl, Styled::flex_row_reverse)
856 .children(cancel)
857 .children(icon)
858 .child(div().flex_1().min_w_0().children(heading))
859 .children(confirm),
860 )
861 .child(content.flex_1().p(px(24.)));
862 } else {
863 surface = surface
864 .p(px(24.))
865 .children(icon.map(|icon| {
866 div()
867 .flex_none()
868 .flex()
869 .justify_center()
870 .mb(px(16.))
871 .child(icon)
872 }))
873 .children(heading.map(|heading| div().flex_none().mb(px(16.)).child(heading)))
874 .child(content)
875 .children((cancel.is_some() || confirm.is_some()).then(|| {
876 div()
877 .flex()
878 .flex_none()
879 .mt(px(24.))
880 .when(rtl, Styled::flex_row_reverse)
881 .justify_end()
882 .flex_wrap()
883 .gap(px(8.))
884 .children(cancel)
885 .children(confirm)
886 }));
887 }
888 }
889
890 let panel = div()
891 .id(self.id.clone())
892 .test_support()
893 .relative()
894 .max_w(available_width)
895 .max_h(max_height)
896 .opacity(progress)
897 .mt(motion_offset)
898 .when(open, |panel| panel.occlude())
899 .child(surface);
900 let backdrop = Scrim::new((self.id.clone(), "scrim"))
901 .opacity(if native {
902 0.
903 } else {
904 self.scrim_opacity * progress
905 })
906 .when_some(self.scrim_color, Scrim::color);
907 if !open {
908 return gpui::deferred(
909 anchored().position(point(px(0.), px(0.))).child(
910 div()
911 .id((self.id.clone(), "dialog-exit"))
912 .absolute()
913 .top_0()
914 .left_0()
915 .w(viewport.width)
916 .h(viewport.height)
917 .flex()
918 .items_center()
919 .justify_center()
920 .child(backdrop)
921 .child(exiting_panel(panel, &self.id, &self.state, window, cx)),
922 ),
923 )
924 .with_priority(10 + self.layer)
925 .into_any_element();
926 }
927 if native {
928 let session = session.clone();
929 window.on_window_should_close(cx, move |window, cx| {
930 session.dismiss(DialogCloseReason::WindowClose, window, cx)
931 });
932 }
933 let properties = self.properties;
934 let outside = session.clone();
935 let keyboard_focus = focus.clone();
936 gpui_base::Dialog::new(cx)
937 .focus_handle(focus)
938 .layer(self.layer, true)
939 .accessibility_label(self.title)
940 .close_on_backdrop_press(false)
941 .backdrop(modal_backdrop(backdrop, move |window, cx| {
942 if !native && properties.dismiss_on_outside_click {
943 outside.dismiss(DialogCloseReason::OutsideClick, window, cx);
944 }
945 }))
946 .on_ok(move |_, window, cx| {
947 if keyboard_confirmation && keyboard_focus.is_focused(window) {
948 confirm_action.activate(window, cx);
949 } else {
950 cx.propagate();
952 }
953 false
954 })
955 .on_open_change(move |_, reason, window, cx| {
956 if reason == DialogChangeReason::Cancel && properties.dismiss_on_escape {
957 session.dismiss(DialogCloseReason::Escape, window, cx);
958 }
959 })
960 .popup(panel)
961 .into_any_element()
962 }
963}