modal_layer.rs

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