modal_layer.rs

  1use gpui::{
  2    div, prelude::*, px, AnyView, Div, FocusHandle, ManagedView, Render, Subscription, Task, View,
  3    ViewContext, WindowContext,
  4};
  5use ui::{h_stack, v_stack};
  6
  7pub trait ModalView: ManagedView {
  8    fn dismiss(&mut self, cx: &mut ViewContext<Self>) -> Task<bool> {
  9        Task::ready(true)
 10    }
 11}
 12
 13trait ModalViewHandle {
 14    fn should_dismiss(&mut self, cx: &mut WindowContext) -> Task<bool>;
 15    fn view(&self) -> AnyView;
 16}
 17
 18impl<V: ModalView> ModalViewHandle for View<V> {
 19    fn should_dismiss(&mut self, cx: &mut WindowContext) -> Task<bool> {
 20        self.update(cx, |this, cx| this.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    subscription: Subscription,
 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            self.hide_modal(cx);
 52            if is_close {
 53                return;
 54            }
 55        }
 56        let new_modal = cx.build_view(build_view);
 57        self.show_modal(new_modal, cx);
 58    }
 59
 60    pub fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>)
 61    where
 62        V: ModalView,
 63    {
 64        self.active_modal = Some(ActiveModal {
 65            modal: Box::new(new_modal.clone()),
 66            subscription: cx.subscribe(&new_modal, |this, modal, e, cx| this.hide_modal(cx)),
 67            previous_focus_handle: cx.focused(),
 68            focus_handle: cx.focus_handle(),
 69        });
 70        cx.focus_view(&new_modal);
 71        cx.notify();
 72    }
 73
 74    pub fn hide_modal(&mut self, cx: &mut ViewContext<Self>) {
 75        let Some(active_modal) = self.active_modal.as_mut() else {
 76            return;
 77        };
 78
 79        let dismiss = active_modal.modal.should_dismiss(cx);
 80
 81        cx.spawn(|this, mut cx| async move {
 82            if dismiss.await {
 83                this.update(&mut cx, |this, cx| {
 84                    if let Some(active_modal) = this.active_modal.take() {
 85                        if let Some(previous_focus) = active_modal.previous_focus_handle {
 86                            if active_modal.focus_handle.contains_focused(cx) {
 87                                previous_focus.focus(cx);
 88                            }
 89                        }
 90                        cx.notify();
 91                    }
 92                })
 93                .ok();
 94            }
 95        })
 96        .detach();
 97    }
 98
 99    pub fn active_modal<V>(&self) -> Option<View<V>>
100    where
101        V: 'static,
102    {
103        let active_modal = self.active_modal.as_ref()?;
104        active_modal.modal.view().downcast::<V>().ok()
105    }
106}
107
108impl Render for ModalLayer {
109    type Element = Div;
110
111    fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
112        let Some(active_modal) = &self.active_modal else {
113            return div();
114        };
115
116        div()
117            .absolute()
118            .size_full()
119            .top_0()
120            .left_0()
121            .z_index(400)
122            .child(
123                v_stack()
124                    .h(px(0.0))
125                    .top_20()
126                    .flex()
127                    .flex_col()
128                    .items_center()
129                    .track_focus(&active_modal.focus_handle)
130                    .child(
131                        h_stack()
132                            .on_mouse_down_out(cx.listener(|this, _, cx| {
133                                this.hide_modal(cx);
134                            }))
135                            .child(active_modal.modal.view()),
136                    ),
137            )
138    }
139}