1use std::{any::TypeId, sync::Arc};
2
3use gpui::{
4 div, AnyView, AppContext, DispatchPhase, Div, ParentElement, Render, StatelessInteractive,
5 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
31impl ModalRegistry {
32 pub fn register_modal<A: 'static, V, B>(&mut self, action: A, build_view: B)
33 where
34 V: Render,
35 B: Fn(&Workspace, &mut ViewContext<Workspace>) -> View<V> + 'static,
36 {
37 let build_view = Arc::new(build_view);
38
39 self.registered_modals.push((
40 TypeId::of::<A>(),
41 Box::new(move |mut div| {
42 let build_view = build_view.clone();
43
44 div.on_action(
45 move |workspace: &mut Workspace,
46 event: &A,
47 phase: DispatchPhase,
48 cx: &mut ViewContext<Workspace>| {
49 dbg!("GOT HERE");
50 if phase == DispatchPhase::Capture {
51 return;
52 }
53
54 let new_modal = (build_view)(workspace, cx);
55 workspace.modal_layer.update(cx, |modal_layer, _| {
56 modal_layer.open_modal = Some(new_modal.into());
57 });
58
59 cx.notify();
60 },
61 )
62 }),
63 ));
64 }
65}
66
67impl ModalLayer {
68 pub fn new() -> Self {
69 Self { open_modal: None }
70 }
71
72 pub fn render(&self, workspace: &Workspace, cx: &ViewContext<Workspace>) -> Div<Workspace> {
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// }