modal_layer.rs

  1use std::{any::TypeId, sync::Arc};
  2
  3use gpui::{
  4    div, AnyView, AppContext, Component, Div, ParentElement, Render, StatelessInteractive, View,
  5    ViewContext,
  6};
  7
  8use crate::Workspace;
  9
 10pub struct ModalRegistry {
 11    registered_modals: Vec<(TypeId, Box<dyn Fn(Div<Workspace>) -> Div<Workspace>>)>,
 12}
 13
 14pub trait Modal {}
 15
 16#[derive(Clone)]
 17pub struct ModalLayer {
 18    open_modal: Option<AnyView>,
 19}
 20
 21pub fn init_modal_registry(cx: &mut AppContext) {
 22    cx.set_global(ModalRegistry {
 23        registered_modals: Vec::new(),
 24    });
 25}
 26
 27struct ToggleModal {
 28    name: String,
 29}
 30
 31// complete change of plan?
 32// on_action(ToggleModal{ name})
 33// register_modal(name, |workspace, cx| { ... })
 34
 35impl ModalRegistry {
 36    pub fn register_modal<A: 'static, V, B>(&mut self, action: A, build_view: B)
 37    where
 38        V: Render,
 39        B: Fn(&Workspace, &mut ViewContext<Workspace>) -> View<V> + 'static,
 40    {
 41        let build_view = Arc::new(build_view);
 42
 43        dbg!("yonder");
 44        self.registered_modals.push((
 45            TypeId::of::<A>(),
 46            Box::new(move |mut div| {
 47                let build_view = build_view.clone();
 48                dbg!("this point");
 49
 50                div.on_action(
 51                    move |workspace: &mut Workspace, event: &A, cx: &mut ViewContext<Workspace>| {
 52                        let new_modal = (build_view)(workspace, cx);
 53                        workspace.modal_layer.update(cx, |modal_layer, _| {
 54                            modal_layer.open_modal = Some(new_modal.into());
 55                        });
 56
 57                        cx.notify();
 58                    },
 59                )
 60            }),
 61        ));
 62    }
 63}
 64
 65impl ModalLayer {
 66    pub fn new() -> Self {
 67        Self { open_modal: None }
 68    }
 69
 70    pub fn render(&self, cx: &ViewContext<Workspace>) -> impl Component<Workspace> {
 71        dbg!("rendering ModalLayer");
 72
 73        let mut div = div();
 74
 75        // div, c workspace.toggle_modal()div.on_action()) {
 76        //
 77        // }
 78
 79        // for (type_id, action) in cx.global::<ModalRegistry>().registered_modals.iter() {
 80        //     div = div.useful_on_action(*type_id, action)
 81        // }
 82
 83        for (_, action) in cx.global::<ModalRegistry>().registered_modals.iter() {
 84            div = (action)(div);
 85        }
 86
 87        div.children(self.open_modal.clone())
 88    }
 89}
 90
 91// impl Render for ModalLayer {
 92//     type Element = Div<Self>;
 93
 94//     fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
 95//         let mut div = div();
 96//         for (type_id, build_view) in cx.global::<ModalRegistry>().registered_modals {
 97//             div = div.useful_on_action(
 98//                 type_id,
 99//                 Box::new(|this, _: dyn Any, phase, cx: &mut ViewContext<Self>| {
100//                     if phase == DispatchPhase::Capture {
101//                         return;
102//                     }
103//                     self.workspace.update(cx, |workspace, cx| {
104//                         self.open_modal = Some(build_view(workspace, cx));
105//                     });
106//                     cx.notify();
107//                 }),
108//             )
109//         }
110
111//         div
112//     }
113// }