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, 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.focus_view(&new_modal);
100        cx.notify();
101    }
102
103    fn hide_modal(&mut self, cx: &mut ViewContext<Self>) -> bool {
104        let Some(active_modal) = self.active_modal.as_mut() else {
105            self.dismiss_on_focus_lost = false;
106            return false;
107        };
108
109        match active_modal.modal.on_before_dismiss(cx) {
110            DismissDecision::Dismiss(dismiss) => {
111                self.dismiss_on_focus_lost = !dismiss;
112                if !dismiss {
113                    return false;
114                }
115            }
116            DismissDecision::Pending => {
117                self.dismiss_on_focus_lost = false;
118                return false;
119            }
120        }
121
122        if let Some(active_modal) = self.active_modal.take() {
123            if let Some(previous_focus) = active_modal.previous_focus_handle {
124                if active_modal.focus_handle.contains_focused(cx) {
125                    previous_focus.focus(cx);
126                }
127            }
128            cx.notify();
129        }
130        true
131    }
132
133    pub fn active_modal<V>(&self) -> Option<View<V>>
134    where
135        V: 'static,
136    {
137        let active_modal = self.active_modal.as_ref()?;
138        active_modal.modal.view().downcast::<V>().ok()
139    }
140
141    pub fn has_active_modal(&self) -> bool {
142        self.active_modal.is_some()
143    }
144}
145
146impl Render for ModalLayer {
147    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
148        let Some(active_modal) = &self.active_modal else {
149            return div();
150        };
151
152        div()
153            .absolute()
154            .size_full()
155            .top_0()
156            .left_0()
157            .when(active_modal.modal.fade_out_background(cx), |el| {
158                let mut background = cx.theme().colors().elevated_surface_background;
159                background.fade_out(0.2);
160                el.bg(background)
161                    .occlude()
162                    .on_mouse_down_out(cx.listener(|this, _, cx| {
163                        this.hide_modal(cx);
164                    }))
165            })
166            .child(
167                v_flex()
168                    .h(px(0.0))
169                    .top_20()
170                    .flex()
171                    .flex_col()
172                    .items_center()
173                    .track_focus(&active_modal.focus_handle)
174                    .child(h_flex().occlude().child(active_modal.modal.view())),
175            )
176    }
177}