2023-11-07 19:47:49 +00:00
|
|
|
use crate::Workspace;
|
2023-11-06 22:35:49 +00:00
|
|
|
use gpui::{
|
2023-11-10 03:58:35 +00:00
|
|
|
div, px, AnyView, Component, Div, EventEmitter, FocusHandle, ParentElement, Render,
|
|
|
|
StatefulInteractivity, StatelessInteractive, Styled, Subscription, View, ViewContext,
|
2023-11-10 04:51:48 +00:00
|
|
|
VisualContext, WindowContext,
|
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
|
|
|
|
2023-11-10 03:58:35 +00:00
|
|
|
pub struct ActiveModal {
|
|
|
|
modal: AnyView,
|
|
|
|
subscription: Subscription,
|
|
|
|
previous_focus_handle: Option<FocusHandle>,
|
|
|
|
focus_handle: FocusHandle,
|
|
|
|
}
|
|
|
|
|
2023-11-06 22:35:49 +00:00
|
|
|
pub struct ModalLayer {
|
2023-11-10 03:58:35 +00:00
|
|
|
active_modal: Option<ActiveModal>,
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 03:58:35 +00:00
|
|
|
pub trait Modal: Render + EventEmitter<ModalEvent> {
|
|
|
|
fn focus(&self, cx: &mut WindowContext);
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-11-10 04:51:48 +00:00
|
|
|
Self { active_modal: None }
|
2023-11-08 23:23:05 +00:00
|
|
|
}
|
2023-11-06 22:35:49 +00:00
|
|
|
|
2023-11-10 04:51:48 +00:00
|
|
|
pub fn toggle_modal<V, B>(&mut self, cx: &mut ViewContext<Workspace>, build_view: B)
|
2023-11-06 22:35:49 +00:00
|
|
|
where
|
2023-11-10 03:58:35 +00:00
|
|
|
V: Modal,
|
2023-11-10 04:51:48 +00:00
|
|
|
B: FnOnce(&mut ViewContext<V>) -> V,
|
2023-11-10 04:11:10 +00:00
|
|
|
{
|
|
|
|
let previous_focus = cx.focused();
|
|
|
|
|
|
|
|
if let Some(active_modal) = &self.active_modal {
|
|
|
|
if active_modal.modal.clone().downcast::<V>().is_ok() {
|
|
|
|
self.hide_modal(cx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-11-10 04:51:48 +00:00
|
|
|
let new_modal = cx.build_view(build_view);
|
|
|
|
self.show_modal(new_modal, cx);
|
2023-11-10 04:11:10 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 04:51:48 +00:00
|
|
|
pub fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Workspace>)
|
|
|
|
where
|
2023-11-10 04:11:10 +00:00
|
|
|
V: Modal,
|
2023-11-09 04:03:46 +00:00
|
|
|
{
|
2023-11-10 03:58:35 +00:00
|
|
|
self.active_modal = Some(ActiveModal {
|
|
|
|
modal: new_modal.clone().into(),
|
2023-11-10 04:51:48 +00:00
|
|
|
subscription: cx.subscribe(&new_modal, |workspace, modal, e, cx| match e {
|
|
|
|
ModalEvent::Dismissed => workspace.modal_layer.hide_modal(cx),
|
2023-11-10 03:58:35 +00:00
|
|
|
}),
|
2023-11-10 04:51:48 +00:00
|
|
|
previous_focus_handle: cx.focused(),
|
2023-11-10 03:58:35 +00:00
|
|
|
focus_handle: cx.focus_handle(),
|
|
|
|
});
|
2023-11-10 04:51:48 +00:00
|
|
|
new_modal.update(cx, |modal, cx| modal.focus(cx));
|
2023-11-08 21:45:36 +00:00
|
|
|
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-10 03:58:35 +00:00
|
|
|
if let Some(active_modal) = self.active_modal.take() {
|
|
|
|
if let Some(previous_focus) = active_modal.previous_focus_handle {
|
|
|
|
if active_modal.focus_handle.contains_focused(cx) {
|
|
|
|
previous_focus.focus(cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 21:45:36 +00:00
|
|
|
cx.notify();
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 17:33:36 +00:00
|
|
|
pub fn wrapper_element(
|
|
|
|
&self,
|
|
|
|
cx: &ViewContext<Workspace>,
|
|
|
|
) -> Div<Workspace, StatefulInteractivity<Workspace>> {
|
2023-11-10 04:51:48 +00:00
|
|
|
let parent = div().id("boop");
|
2023-11-10 03:58:35 +00:00
|
|
|
parent.when_some(self.active_modal.as_ref(), |parent, open_modal| {
|
2023-11-07 20:23:08 +00:00
|
|
|
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
|
|
|
|
2023-11-10 03:58:35 +00:00
|
|
|
let container2 = v_stack()
|
|
|
|
.h(px(0.0))
|
|
|
|
.relative()
|
|
|
|
.top_20()
|
2023-11-10 04:11:10 +00:00
|
|
|
.track_focus(&open_modal.focus_handle)
|
2023-11-10 04:51:48 +00:00
|
|
|
.on_mouse_down_out(|workspace: &mut Workspace, event, cx| {
|
|
|
|
workspace.modal_layer.hide_modal(cx);
|
2023-11-10 04:11:10 +00:00
|
|
|
});
|
2023-11-07 20:23:08 +00:00
|
|
|
|
2023-11-10 03:58:35 +00:00
|
|
|
parent.child(container1.child(container2.child(open_modal.modal.clone())))
|
2023-11-07 20:23:08 +00:00
|
|
|
})
|
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
|
|
|
|
// }
|
|
|
|
// }
|