2023-11-07 19:47:49 +00:00
|
|
|
use crate::Workspace;
|
2023-11-06 22:35:49 +00:00
|
|
|
use gpui::{
|
2023-11-08 23:23:05 +00:00
|
|
|
div, px, AnyView, Component, Div, EventEmitter, ParentElement, Render, StatelessInteractive,
|
|
|
|
Styled, Subscription, View, ViewContext,
|
2023-11-06 22:35:49 +00:00
|
|
|
};
|
2023-11-07 19:47:49 +00:00
|
|
|
use std::{any::TypeId, sync::Arc};
|
2023-11-07 21:23:41 +00:00
|
|
|
use ui::v_stack;
|
2023-11-06 22:35:49 +00:00
|
|
|
|
|
|
|
pub struct ModalLayer {
|
|
|
|
open_modal: Option<AnyView>,
|
2023-11-08 21:45:36 +00:00
|
|
|
subscription: Option<Subscription>,
|
2023-11-08 23:23:05 +00:00
|
|
|
registered_modals: Vec<(TypeId, Box<dyn Fn(Div<Workspace>) -> Div<Workspace>>)>,
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 21:45:36 +00:00
|
|
|
pub enum ModalEvent {
|
|
|
|
Dismissed,
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 23:23:05 +00:00
|
|
|
impl ModalLayer {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
open_modal: None,
|
|
|
|
subscription: None,
|
|
|
|
registered_modals: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
2023-11-06 22:35:49 +00:00
|
|
|
|
|
|
|
pub fn register_modal<A: 'static, V, B>(&mut self, action: A, build_view: B)
|
|
|
|
where
|
2023-11-09 04:03:46 +00:00
|
|
|
V: EventEmitter<ModalEvent> + Render,
|
2023-11-08 21:45:36 +00:00
|
|
|
B: Fn(&mut Workspace, &mut ViewContext<Workspace>) -> Option<View<V>> + 'static,
|
2023-11-06 22:35:49 +00:00
|
|
|
{
|
|
|
|
let build_view = Arc::new(build_view);
|
|
|
|
|
|
|
|
self.registered_modals.push((
|
|
|
|
TypeId::of::<A>(),
|
|
|
|
Box::new(move |mut div| {
|
|
|
|
let build_view = build_view.clone();
|
|
|
|
|
2023-11-08 21:45:36 +00:00
|
|
|
div.on_action(move |workspace, event: &A, cx| {
|
2023-11-08 23:23:05 +00:00
|
|
|
let Some(new_modal) = (build_view)(workspace, cx) else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
workspace.modal_layer().show_modal(new_modal, cx);
|
2023-11-08 21:45:36 +00:00
|
|
|
})
|
2023-11-06 22:35:49 +00:00
|
|
|
}),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2023-11-09 04:03:46 +00:00
|
|
|
pub fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Workspace>)
|
|
|
|
where
|
|
|
|
V: EventEmitter<ModalEvent> + Render,
|
|
|
|
{
|
|
|
|
self.subscription = Some(cx.subscribe(&new_modal, |this, modal, e, cx| match e {
|
|
|
|
ModalEvent::Dismissed => this.modal_layer().hide_modal(cx),
|
2023-11-08 21:45:36 +00:00
|
|
|
}));
|
|
|
|
self.open_modal = Some(new_modal.into());
|
|
|
|
cx.notify();
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 23:23:05 +00:00
|
|
|
pub fn hide_modal(&mut self, cx: &mut ViewContext<Workspace>) {
|
2023-11-08 21:45:36 +00:00
|
|
|
self.open_modal.take();
|
|
|
|
self.subscription.take();
|
|
|
|
cx.notify();
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 23:23:05 +00:00
|
|
|
pub fn wrapper_element(&self, cx: &ViewContext<Workspace>) -> Div<Workspace> {
|
2023-11-07 22:27:08 +00:00
|
|
|
let mut parent = div().relative().size_full();
|
2023-11-06 22:35:49 +00:00
|
|
|
|
2023-11-08 23:23:05 +00:00
|
|
|
for (_, action) in self.registered_modals.iter() {
|
2023-11-07 20:23:08 +00:00
|
|
|
parent = (action)(parent);
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 20:23:08 +00:00
|
|
|
parent.when_some(self.open_modal.as_ref(), |parent, open_modal| {
|
|
|
|
let container1 = div()
|
|
|
|
.absolute()
|
2023-11-07 21:23:41 +00:00
|
|
|
.flex()
|
|
|
|
.flex_col()
|
|
|
|
.items_center()
|
2023-11-07 20:23:08 +00:00
|
|
|
.size_full()
|
|
|
|
.top_0()
|
|
|
|
.left_0()
|
2023-11-07 21:23:41 +00:00
|
|
|
.z_index(400);
|
2023-11-07 20:23:08 +00:00
|
|
|
|
|
|
|
// transparent layer
|
2023-11-07 22:27:08 +00:00
|
|
|
let container2 = v_stack().h(px(0.0)).relative().top_20();
|
2023-11-07 20:23:08 +00:00
|
|
|
|
|
|
|
parent.child(container1.child(container2.child(open_modal.clone())))
|
|
|
|
})
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// impl Render for ModalLayer {
|
|
|
|
// type Element = Div<Self>;
|
|
|
|
|
|
|
|
// fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
|
|
|
|
// let mut div = div();
|
|
|
|
// for (type_id, build_view) in cx.global::<ModalRegistry>().registered_modals {
|
|
|
|
// div = div.useful_on_action(
|
|
|
|
// type_id,
|
|
|
|
// Box::new(|this, _: dyn Any, phase, cx: &mut ViewContext<Self>| {
|
|
|
|
// if phase == DispatchPhase::Capture {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// self.workspace.update(cx, |workspace, cx| {
|
|
|
|
// self.open_modal = Some(build_view(workspace, cx));
|
|
|
|
// });
|
|
|
|
// cx.notify();
|
|
|
|
// }),
|
|
|
|
// )
|
|
|
|
// }
|
|
|
|
|
|
|
|
// div
|
|
|
|
// }
|
|
|
|
// }
|