1use gpui::{
2 AnyView, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable as _, ManagedView,
3 MouseButton, Subscription,
4};
5use ui::prelude::*;
6
7#[derive(Debug)]
8pub enum DismissDecision {
9 Dismiss(bool),
10 Pending,
11}
12
13pub trait ModalView: ManagedView {
14 fn on_before_dismiss(
15 &mut self,
16 _window: &mut Window,
17 _: &mut Context<Self>,
18 ) -> DismissDecision {
19 DismissDecision::Dismiss(true)
20 }
21
22 fn fade_out_background(&self) -> bool {
23 false
24 }
25
26 fn render_bare(&self) -> bool {
27 false
28 }
29
30 /// Returns whether this [`ModalView`] is the command palette.
31 ///
32 /// This breaks the encapsulation of the [`ModalView`] trait a little bit, but there doesn't seem to be an
33 /// immediate, more elegant way to have the workspace know about the command palette (due to dependency arrow
34 /// directions).
35 fn is_command_palette(&self) -> bool {
36 false
37 }
38}
39
40trait ModalViewHandle {
41 fn on_before_dismiss(&mut self, window: &mut Window, cx: &mut App) -> DismissDecision;
42 fn view(&self) -> AnyView;
43 fn fade_out_background(&self, cx: &mut App) -> bool;
44 fn render_bare(&self, cx: &mut App) -> bool;
45 fn is_command_palette(&self, cx: &App) -> bool;
46}
47
48impl<V: ModalView> ModalViewHandle for Entity<V> {
49 fn on_before_dismiss(&mut self, window: &mut Window, cx: &mut App) -> DismissDecision {
50 self.update(cx, |this, cx| this.on_before_dismiss(window, cx))
51 }
52
53 fn view(&self) -> AnyView {
54 self.clone().into()
55 }
56
57 fn fade_out_background(&self, cx: &mut App) -> bool {
58 self.read(cx).fade_out_background()
59 }
60
61 fn render_bare(&self, cx: &mut App) -> bool {
62 self.read(cx).render_bare()
63 }
64
65 fn is_command_palette(&self, cx: &App) -> bool {
66 self.read(cx).is_command_palette()
67 }
68}
69
70pub struct ActiveModal {
71 modal: Box<dyn ModalViewHandle>,
72 _subscriptions: [Subscription; 2],
73 previous_focus_handle: Option<FocusHandle>,
74 focus_handle: FocusHandle,
75}
76
77pub struct ModalLayer {
78 active_modal: Option<ActiveModal>,
79 dismiss_on_focus_lost: bool,
80}
81
82pub(crate) struct ModalOpenedEvent;
83
84impl EventEmitter<ModalOpenedEvent> for ModalLayer {}
85
86impl Default for ModalLayer {
87 fn default() -> Self {
88 Self::new()
89 }
90}
91
92impl ModalLayer {
93 pub fn new() -> Self {
94 Self {
95 active_modal: None,
96 dismiss_on_focus_lost: false,
97 }
98 }
99
100 /// Toggles a modal of type `V`. If a modal of the same type is currently active,
101 /// it will be hidden. If a different modal is active, it will be replaced with the new one.
102 /// If no modal is active, the new modal will be shown.
103 ///
104 /// If closing the current modal fails (e.g., due to `on_before_dismiss` returning
105 /// `DismissDecision::Dismiss(false)` or `DismissDecision::Pending`), the new modal
106 /// will not be shown.
107 pub fn toggle_modal<V, B>(&mut self, window: &mut Window, cx: &mut Context<Self>, build_view: B)
108 where
109 V: ModalView,
110 B: FnOnce(&mut Window, &mut Context<V>) -> V,
111 {
112 if let Some(active_modal) = &self.active_modal {
113 let should_close = active_modal.modal.view().downcast::<V>().is_ok();
114 let did_close = self.hide_modal(window, cx);
115 if should_close || !did_close {
116 return;
117 }
118 }
119 let new_modal = cx.new(|cx| build_view(window, cx));
120 self.show_modal(new_modal, window, cx);
121 cx.emit(ModalOpenedEvent);
122 }
123
124 /// Shows a modal and sets up subscriptions for dismiss events and focus tracking.
125 /// The modal is automatically focused after being shown.
126 fn show_modal<V>(&mut self, new_modal: Entity<V>, window: &mut Window, cx: &mut Context<Self>)
127 where
128 V: ModalView,
129 {
130 let focus_handle = cx.focus_handle();
131 self.active_modal = Some(ActiveModal {
132 modal: Box::new(new_modal.clone()),
133 _subscriptions: [
134 cx.subscribe_in(
135 &new_modal,
136 window,
137 |this, _, _: &DismissEvent, window, cx| {
138 this.hide_modal(window, cx);
139 },
140 ),
141 cx.on_focus_out(&focus_handle, window, |this, _event, window, cx| {
142 if this.dismiss_on_focus_lost {
143 this.hide_modal(window, cx);
144 }
145 }),
146 ],
147 previous_focus_handle: window.focused(cx),
148 focus_handle,
149 });
150 cx.defer_in(window, move |_, window, cx| {
151 window.focus(&new_modal.focus_handle(cx), cx);
152 });
153 cx.notify();
154 }
155
156 /// Attempts to hide the currently active modal.
157 ///
158 /// The modal's `on_before_dismiss` method is called to determine if dismissal should proceed.
159 /// If dismissal is allowed, the modal is removed and focus is restored to the previously
160 /// focused element.
161 ///
162 /// Returns `true` if the modal was successfully hidden, `false` otherwise.
163 pub fn hide_modal(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
164 let Some(active_modal) = self.active_modal.as_mut() else {
165 self.dismiss_on_focus_lost = false;
166 return false;
167 };
168
169 match active_modal.modal.on_before_dismiss(window, cx) {
170 DismissDecision::Dismiss(should_dismiss) => {
171 if !should_dismiss {
172 self.dismiss_on_focus_lost = !should_dismiss;
173 return false;
174 }
175 }
176 DismissDecision::Pending => {
177 self.dismiss_on_focus_lost = false;
178 return false;
179 }
180 }
181
182 if let Some(active_modal) = self.active_modal.take() {
183 if let Some(previous_focus) = active_modal.previous_focus_handle
184 && active_modal.focus_handle.contains_focused(window, cx)
185 {
186 previous_focus.focus(window, cx);
187 }
188 cx.notify();
189 }
190 self.dismiss_on_focus_lost = false;
191 true
192 }
193
194 /// Returns the currently active modal if it is of type `V`.
195 pub fn active_modal<V>(&self) -> Option<Entity<V>>
196 where
197 V: 'static,
198 {
199 let active_modal = self.active_modal.as_ref()?;
200 active_modal.modal.view().downcast::<V>().ok()
201 }
202
203 pub fn has_active_modal(&self) -> bool {
204 self.active_modal.is_some()
205 }
206
207 /// Returns whether the active modal is the command palette.
208 pub fn is_active_modal_command_palette(&self, cx: &App) -> bool {
209 self.active_modal
210 .as_ref()
211 .map_or(false, |modal| modal.modal.is_command_palette(cx))
212 }
213}
214
215impl Render for ModalLayer {
216 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
217 let Some(active_modal) = &self.active_modal else {
218 return div().into_any_element();
219 };
220
221 if active_modal.modal.render_bare(cx) {
222 return active_modal.modal.view().into_any_element();
223 }
224
225 div()
226 .absolute()
227 .size_full()
228 .inset_0()
229 .occlude()
230 .when(active_modal.modal.fade_out_background(cx), |this| {
231 let mut background = cx.theme().colors().elevated_surface_background;
232 background.fade_out(0.2);
233 this.bg(background)
234 })
235 .on_mouse_down(
236 MouseButton::Left,
237 cx.listener(|this, _, window, cx| {
238 this.hide_modal(window, cx);
239 }),
240 )
241 .child(
242 v_flex()
243 .h(px(0.0))
244 .top_20()
245 .items_center()
246 .track_focus(&active_modal.focus_handle)
247 .child(
248 h_flex()
249 .occlude()
250 .child(active_modal.modal.view())
251 .on_mouse_down(MouseButton::Left, |_, _, cx| {
252 cx.stop_propagation();
253 }),
254 ),
255 )
256 .into_any_element()
257 }
258}