1use std::{any::TypeId, sync::Arc};
2
3use gpui::{
4 div, hsla, px, red, AnyView, AppContext, Component, DispatchPhase, Div, ParentElement, Render,
5 StatelessInteractive, Styled, View, ViewContext,
6};
7use ui::v_stack;
8
9use crate::Workspace;
10
11pub struct ModalRegistry {
12 registered_modals: Vec<(TypeId, Box<dyn Fn(Div<Workspace>) -> Div<Workspace>>)>,
13}
14
15pub trait Modal {}
16
17#[derive(Clone)]
18pub struct ModalLayer {
19 open_modal: Option<AnyView>,
20}
21
22pub fn init_modal_registry(cx: &mut AppContext) {
23 cx.set_global(ModalRegistry {
24 registered_modals: Vec::new(),
25 });
26}
27
28struct ToggleModal {
29 name: String,
30}
31
32impl ModalRegistry {
33 pub fn register_modal<A: 'static, V, B>(&mut self, action: A, build_view: B)
34 where
35 V: Render,
36 B: Fn(&Workspace, &mut ViewContext<Workspace>) -> View<V> + 'static,
37 {
38 let build_view = Arc::new(build_view);
39
40 self.registered_modals.push((
41 TypeId::of::<A>(),
42 Box::new(move |mut div| {
43 let build_view = build_view.clone();
44
45 div.on_action(
46 move |workspace: &mut Workspace,
47 event: &A,
48 phase: DispatchPhase,
49 cx: &mut ViewContext<Workspace>| {
50 dbg!("GOT HERE");
51 if phase == DispatchPhase::Capture {
52 return;
53 }
54
55 let new_modal = (build_view)(workspace, cx);
56 workspace.modal_layer.update(cx, |modal_layer, _| {
57 modal_layer.open_modal = Some(new_modal.into());
58 });
59
60 cx.notify();
61 },
62 )
63 }),
64 ));
65 }
66}
67
68impl ModalLayer {
69 pub fn new() -> Self {
70 Self { open_modal: None }
71 }
72
73 // Workspace
74 // - ModalLayer parent
75 // - - container
76 // - - - modal
77 // - - - content of the modal
78 // - - content of the workspace
79
80 // app
81 // workspace
82 // container some layer that contains all modals and is 100% wide and high
83 // modal (this has a shadow, some witdht)
84 // whatever
85
86 pub fn render(&self, workspace: &Workspace, cx: &ViewContext<Workspace>) -> Div<Workspace> {
87 let mut parent = div().relative().bg(red()).size_full();
88
89 for (_, action) in cx.global::<ModalRegistry>().registered_modals.iter() {
90 parent = (action)(parent);
91 }
92
93 parent.when_some(self.open_modal.as_ref(), |parent, open_modal| {
94 let container1 = div()
95 .absolute()
96 .flex()
97 .flex_col()
98 .items_center()
99 .size_full()
100 .top_0()
101 .left_0()
102 .z_index(400);
103
104 // transparent layer
105 let container2 = v_stack()
106 .bg(hsla(0.5, 0.5, 0.5, 0.5))
107 .h(px(0.0))
108 .relative()
109 .top_20();
110
111 parent.child(container1.child(container2.child(open_modal.clone())))
112 })
113 }
114}
115
116// impl Render for ModalLayer {
117// type Element = Div<Self>;
118
119// fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
120// let mut div = div();
121// for (type_id, build_view) in cx.global::<ModalRegistry>().registered_modals {
122// div = div.useful_on_action(
123// type_id,
124// Box::new(|this, _: dyn Any, phase, cx: &mut ViewContext<Self>| {
125// if phase == DispatchPhase::Capture {
126// return;
127// }
128// self.workspace.update(cx, |workspace, cx| {
129// self.open_modal = Some(build_view(workspace, cx));
130// });
131// cx.notify();
132// }),
133// )
134// }
135
136// div
137// }
138// }