1use gpui::{
2 div, prelude::*, px, AnyView, DismissEvent, FocusHandle, ManagedView, Render, Subscription,
3 View, ViewContext, WindowContext,
4};
5use ui::{h_flex, v_flex};
6
7pub trait ModalView: ManagedView {
8 fn on_before_dismiss(&mut self, _: &mut ViewContext<Self>) -> bool {
9 true
10 }
11}
12
13trait ModalViewHandle {
14 fn on_before_dismiss(&mut self, cx: &mut WindowContext) -> bool;
15 fn view(&self) -> AnyView;
16}
17
18impl<V: ModalView> ModalViewHandle for View<V> {
19 fn on_before_dismiss(&mut self, cx: &mut WindowContext) -> bool {
20 self.update(cx, |this, cx| this.on_before_dismiss(cx))
21 }
22
23 fn view(&self) -> AnyView {
24 self.clone().into()
25 }
26}
27
28pub struct ActiveModal {
29 modal: Box<dyn ModalViewHandle>,
30 _subscription: Subscription,
31 previous_focus_handle: Option<FocusHandle>,
32 focus_handle: FocusHandle,
33}
34
35pub struct ModalLayer {
36 active_modal: Option<ActiveModal>,
37}
38
39impl ModalLayer {
40 pub fn new() -> Self {
41 Self { active_modal: None }
42 }
43
44 pub fn toggle_modal<V, B>(&mut self, cx: &mut ViewContext<Self>, build_view: B)
45 where
46 V: ModalView,
47 B: FnOnce(&mut ViewContext<V>) -> V,
48 {
49 if let Some(active_modal) = &self.active_modal {
50 let is_close = active_modal.modal.view().downcast::<V>().is_ok();
51 let did_close = self.hide_modal(cx);
52 if is_close || !did_close {
53 return;
54 }
55 }
56 let new_modal = cx.new_view(build_view);
57 self.show_modal(new_modal, cx);
58 }
59
60 fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>)
61 where
62 V: ModalView,
63 {
64 self.active_modal = Some(ActiveModal {
65 modal: Box::new(new_modal.clone()),
66 _subscription: cx.subscribe(&new_modal, |this, _, _: &DismissEvent, cx| {
67 this.hide_modal(cx);
68 }),
69 previous_focus_handle: cx.focused(),
70 focus_handle: cx.focus_handle(),
71 });
72 cx.focus_view(&new_modal);
73 cx.notify();
74 }
75
76 fn hide_modal(&mut self, cx: &mut ViewContext<Self>) -> bool {
77 let Some(active_modal) = self.active_modal.as_mut() else {
78 return false;
79 };
80
81 let dismiss = active_modal.modal.on_before_dismiss(cx);
82 if !dismiss {
83 return false;
84 }
85
86 if let Some(active_modal) = self.active_modal.take() {
87 if let Some(previous_focus) = active_modal.previous_focus_handle {
88 if active_modal.focus_handle.contains_focused(cx) {
89 previous_focus.focus(cx);
90 }
91 }
92 cx.notify();
93 }
94 true
95 }
96
97 pub fn active_modal<V>(&self) -> Option<View<V>>
98 where
99 V: 'static,
100 {
101 let active_modal = self.active_modal.as_ref()?;
102 active_modal.modal.view().downcast::<V>().ok()
103 }
104
105 pub fn has_active_modal(&self) -> bool {
106 self.active_modal.is_some()
107 }
108}
109
110impl Render for ModalLayer {
111 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
112 let Some(active_modal) = &self.active_modal else {
113 return div();
114 };
115
116 div()
117 .absolute()
118 .size_full()
119 .top_0()
120 .left_0()
121 .z_index(169)
122 .child(
123 v_flex()
124 .h(px(0.0))
125 .top_20()
126 .flex()
127 .flex_col()
128 .items_center()
129 .track_focus(&active_modal.focus_handle)
130 .child(
131 h_flex()
132 .on_mouse_down_out(cx.listener(|this, _, cx| {
133 this.hide_modal(cx);
134 }))
135 .child(active_modal.modal.view()),
136 ),
137 )
138 }
139}