1use std::{any::TypeId, sync::Arc};
2
3use gpui::{
4 div, AnyView, AppContext, Component, DispatchPhase, Div, ParentElement, Render,
5 StatelessInteractive, View, 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,
52 event: &A,
53 phase: DispatchPhase,
54 cx: &mut ViewContext<Workspace>| {
55 dbg!("GOT HERE");
56 if phase == DispatchPhase::Capture {
57 return;
58 }
59
60 let new_modal = (build_view)(workspace, cx);
61 workspace.modal_layer.update(cx, |modal_layer, _| {
62 modal_layer.open_modal = Some(new_modal.into());
63 });
64
65 cx.notify();
66 },
67 )
68 }),
69 ));
70 }
71}
72
73impl ModalLayer {
74 pub fn new() -> Self {
75 Self { open_modal: None }
76 }
77
78 pub fn render(&self, cx: &ViewContext<Workspace>) -> impl Component<Workspace> {
79 dbg!("rendering ModalLayer");
80
81 let mut div = div();
82
83 // div, c workspace.toggle_modal()div.on_action()) {
84 //
85 // }
86
87 // for (type_id, action) in cx.global::<ModalRegistry>().registered_modals.iter() {
88 // div = div.useful_on_action(*type_id, action)
89 // }
90
91 for (_, action) in cx.global::<ModalRegistry>().registered_modals.iter() {
92 div = (action)(div);
93 }
94
95 div.children(self.open_modal.clone())
96 }
97}
98
99// impl Render for ModalLayer {
100// type Element = Div<Self>;
101
102// fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
103// let mut div = div();
104// for (type_id, build_view) in cx.global::<ModalRegistry>().registered_modals {
105// div = div.useful_on_action(
106// type_id,
107// Box::new(|this, _: dyn Any, phase, cx: &mut ViewContext<Self>| {
108// if phase == DispatchPhase::Capture {
109// return;
110// }
111// self.workspace.update(cx, |workspace, cx| {
112// self.open_modal = Some(build_view(workspace, cx));
113// });
114// cx.notify();
115// }),
116// )
117// }
118
119// div
120// }
121// }