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