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