modal_layer.rs

  1use gpui::{
  2    div, prelude::*, px, AnyView, DismissEvent, FocusHandle, ManagedView, Render, Subscription,
  3    View, ViewContext, WindowContext,
  4};
  5use ui::{h_flex, v_flex};
  6
  7pub trait ModalView: ManagedView {
  8    fn on_before_dismiss(&mut self, _: &mut ViewContext<Self>) -> bool {
  9        true
 10    }
 11}
 12
 13trait ModalViewHandle {
 14    fn on_before_dismiss(&mut self, cx: &mut WindowContext) -> bool;
 15    fn view(&self) -> AnyView;
 16}
 17
 18impl<V: ModalView> ModalViewHandle for View<V> {
 19    fn on_before_dismiss(&mut self, cx: &mut WindowContext) -> bool {
 20        self.update(cx, |this, cx| this.on_before_dismiss(cx))
 21    }
 22
 23    fn view(&self) -> AnyView {
 24        self.clone().into()
 25    }
 26}
 27
 28pub struct ActiveModal {
 29    modal: Box<dyn ModalViewHandle>,
 30    _subscriptions: [Subscription; 2],
 31    previous_focus_handle: Option<FocusHandle>,
 32    focus_handle: FocusHandle,
 33}
 34
 35pub struct ModalLayer {
 36    active_modal: Option<ActiveModal>,
 37}
 38
 39impl ModalLayer {
 40    pub fn new() -> Self {
 41        Self { active_modal: None }
 42    }
 43
 44    pub fn toggle_modal<V, B>(&mut self, cx: &mut ViewContext<Self>, build_view: B)
 45    where
 46        V: ModalView,
 47        B: FnOnce(&mut ViewContext<V>) -> V,
 48    {
 49        if let Some(active_modal) = &self.active_modal {
 50            let is_close = active_modal.modal.view().downcast::<V>().is_ok();
 51            let did_close = self.hide_modal(cx);
 52            if is_close || !did_close {
 53                return;
 54            }
 55        }
 56        let new_modal = cx.new_view(build_view);
 57        self.show_modal(new_modal, cx);
 58    }
 59
 60    fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>)
 61    where
 62        V: ModalView,
 63    {
 64        let focus_handle = cx.focus_handle();
 65        self.active_modal = Some(ActiveModal {
 66            modal: Box::new(new_modal.clone()),
 67            _subscriptions: [
 68                cx.subscribe(&new_modal, |this, _, _: &DismissEvent, cx| {
 69                    this.hide_modal(cx);
 70                }),
 71                cx.on_focus_out(&focus_handle, |this, cx| {
 72                    this.hide_modal(cx);
 73                }),
 74            ],
 75            previous_focus_handle: cx.focused(),
 76            focus_handle,
 77        });
 78        cx.focus_view(&new_modal);
 79        cx.notify();
 80    }
 81
 82    fn hide_modal(&mut self, cx: &mut ViewContext<Self>) -> bool {
 83        let Some(active_modal) = self.active_modal.as_mut() else {
 84            return false;
 85        };
 86
 87        let dismiss = active_modal.modal.on_before_dismiss(cx);
 88        if !dismiss {
 89            return false;
 90        }
 91
 92        if let Some(active_modal) = self.active_modal.take() {
 93            if let Some(previous_focus) = active_modal.previous_focus_handle {
 94                if active_modal.focus_handle.contains_focused(cx) {
 95                    previous_focus.focus(cx);
 96                }
 97            }
 98            cx.notify();
 99        }
100        true
101    }
102
103    pub fn active_modal<V>(&self) -> Option<View<V>>
104    where
105        V: 'static,
106    {
107        let active_modal = self.active_modal.as_ref()?;
108        active_modal.modal.view().downcast::<V>().ok()
109    }
110
111    pub fn has_active_modal(&self) -> bool {
112        self.active_modal.is_some()
113    }
114}
115
116impl Render for ModalLayer {
117    fn render(&mut self, _: &mut ViewContext<Self>) -> impl IntoElement {
118        let Some(active_modal) = &self.active_modal else {
119            return div();
120        };
121
122        div()
123            .absolute()
124            .size_full()
125            .top_0()
126            .left_0()
127            .z_index(169)
128            .child(
129                v_flex()
130                    .h(px(0.0))
131                    .top_20()
132                    .flex()
133                    .flex_col()
134                    .items_center()
135                    .track_focus(&active_modal.focus_handle)
136                    .child(h_flex().child(active_modal.modal.view())),
137            )
138    }
139}