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