modal_layer.rs

  1use gpui::{
  2    AnyView, DismissEvent, Entity, FocusHandle, Focusable as _, ManagedView, MouseButton,
  3    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
 27trait ModalViewHandle {
 28    fn on_before_dismiss(&mut self, window: &mut Window, cx: &mut App) -> DismissDecision;
 29    fn view(&self) -> AnyView;
 30    fn fade_out_background(&self, cx: &mut App) -> bool;
 31}
 32
 33impl<V: ModalView> ModalViewHandle for Entity<V> {
 34    fn on_before_dismiss(&mut self, window: &mut Window, cx: &mut App) -> DismissDecision {
 35        self.update(cx, |this, cx| this.on_before_dismiss(window, cx))
 36    }
 37
 38    fn view(&self) -> AnyView {
 39        self.clone().into()
 40    }
 41
 42    fn fade_out_background(&self, cx: &mut App) -> bool {
 43        self.read(cx).fade_out_background()
 44    }
 45}
 46
 47pub struct ActiveModal {
 48    modal: Box<dyn ModalViewHandle>,
 49    _subscriptions: [Subscription; 2],
 50    previous_focus_handle: Option<FocusHandle>,
 51    focus_handle: FocusHandle,
 52}
 53
 54pub struct ModalLayer {
 55    active_modal: Option<ActiveModal>,
 56    dismiss_on_focus_lost: bool,
 57}
 58
 59impl Default for ModalLayer {
 60    fn default() -> Self {
 61        Self::new()
 62    }
 63}
 64
 65impl ModalLayer {
 66    pub fn new() -> Self {
 67        Self {
 68            active_modal: None,
 69            dismiss_on_focus_lost: false,
 70        }
 71    }
 72
 73    pub fn toggle_modal<V, B>(&mut self, window: &mut Window, cx: &mut Context<Self>, build_view: B)
 74    where
 75        V: ModalView,
 76        B: FnOnce(&mut Window, &mut Context<V>) -> V,
 77    {
 78        if let Some(active_modal) = &self.active_modal {
 79            let is_close = active_modal.modal.view().downcast::<V>().is_ok();
 80            let did_close = self.hide_modal(window, cx);
 81            if is_close || !did_close {
 82                return;
 83            }
 84        }
 85        let new_modal = cx.new(|cx| build_view(window, cx));
 86        self.show_modal(new_modal, window, cx);
 87    }
 88
 89    fn show_modal<V>(&mut self, new_modal: Entity<V>, window: &mut Window, cx: &mut Context<Self>)
 90    where
 91        V: ModalView,
 92    {
 93        let focus_handle = cx.focus_handle();
 94        self.active_modal = Some(ActiveModal {
 95            modal: Box::new(new_modal.clone()),
 96            _subscriptions: [
 97                cx.subscribe_in(
 98                    &new_modal,
 99                    window,
100                    |this, _, _: &DismissEvent, window, cx| {
101                        this.hide_modal(window, cx);
102                    },
103                ),
104                cx.on_focus_out(&focus_handle, window, |this, _event, window, cx| {
105                    if this.dismiss_on_focus_lost {
106                        this.hide_modal(window, cx);
107                    }
108                }),
109            ],
110            previous_focus_handle: window.focused(cx),
111            focus_handle,
112        });
113        cx.defer_in(window, move |_, window, cx| {
114            window.focus(&new_modal.focus_handle(cx));
115        });
116        cx.notify();
117    }
118
119    pub fn hide_modal(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
120        let Some(active_modal) = self.active_modal.as_mut() else {
121            self.dismiss_on_focus_lost = false;
122            return false;
123        };
124
125        match active_modal.modal.on_before_dismiss(window, cx) {
126            DismissDecision::Dismiss(dismiss) => {
127                self.dismiss_on_focus_lost = !dismiss;
128                if !dismiss {
129                    return false;
130                }
131            }
132            DismissDecision::Pending => {
133                self.dismiss_on_focus_lost = false;
134                return false;
135            }
136        }
137
138        if let Some(active_modal) = self.active_modal.take() {
139            if let Some(previous_focus) = active_modal.previous_focus_handle {
140                if active_modal.focus_handle.contains_focused(window, cx) {
141                    previous_focus.focus(window);
142                }
143            }
144            cx.notify();
145        }
146        true
147    }
148
149    pub fn active_modal<V>(&self) -> Option<Entity<V>>
150    where
151        V: 'static,
152    {
153        let active_modal = self.active_modal.as_ref()?;
154        active_modal.modal.view().downcast::<V>().ok()
155    }
156
157    pub fn has_active_modal(&self) -> bool {
158        self.active_modal.is_some()
159    }
160}
161
162impl Render for ModalLayer {
163    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
164        let Some(active_modal) = &self.active_modal else {
165            return div();
166        };
167
168        div()
169            .occlude()
170            .absolute()
171            .size_full()
172            .top_0()
173            .left_0()
174            .when(active_modal.modal.fade_out_background(cx), |el| {
175                let mut background = cx.theme().colors().elevated_surface_background;
176                background.fade_out(0.2);
177                el.bg(background)
178            })
179            .on_mouse_down(
180                MouseButton::Left,
181                cx.listener(|this, _, window, cx| {
182                    this.hide_modal(window, cx);
183                }),
184            )
185            .child(
186                v_flex()
187                    .h(px(0.0))
188                    .top_20()
189                    .flex()
190                    .flex_col()
191                    .items_center()
192                    .track_focus(&active_modal.focus_handle)
193                    .child(
194                        h_flex()
195                            .occlude()
196                            .child(active_modal.modal.view())
197                            .on_mouse_down(MouseButton::Left, |_, _, cx| {
198                                cx.stop_propagation();
199                            }),
200                    ),
201            )
202    }
203}