modal_layer.rs

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