modal_layer.rs

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