2023-11-06 22:35:49 +00:00
|
|
|
use gpui::{
|
2023-11-10 05:11:11 +00:00
|
|
|
div, px, AnyView, Div, EventEmitter, FocusHandle, ParentElement, Render, StatelessInteractive,
|
|
|
|
Styled, Subscription, View, ViewContext, VisualContext, WindowContext,
|
2023-11-06 22:35:49 +00:00
|
|
|
};
|
2023-11-14 04:42:27 +00:00
|
|
|
use ui::{h_stack, 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 05:11:11 +00:00
|
|
|
pub fn toggle_modal<V, B>(&mut self, cx: &mut ViewContext<Self>, 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
|
|
|
{
|
|
|
|
if let Some(active_modal) = &self.active_modal {
|
2023-11-10 05:23:36 +00:00
|
|
|
let is_close = active_modal.modal.clone().downcast::<V>().is_ok();
|
|
|
|
self.hide_modal(cx);
|
|
|
|
if is_close {
|
2023-11-10 04:11:10 +00:00
|
|
|
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 05:11:11 +00:00
|
|
|
pub fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>)
|
2023-11-10 04:51:48 +00:00
|
|
|
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 05:11:11 +00:00
|
|
|
subscription: cx.subscribe(&new_modal, |this, modal, e, cx| match e {
|
|
|
|
ModalEvent::Dismissed => this.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-10 05:11:11 +00:00
|
|
|
pub fn hide_modal(&mut self, cx: &mut ViewContext<Self>) {
|
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-10 05:11:11 +00:00
|
|
|
}
|
2023-11-06 22:35:49 +00:00
|
|
|
|
2023-11-10 05:11:11 +00:00
|
|
|
impl Render for ModalLayer {
|
|
|
|
type Element = Div<Self>;
|
|
|
|
|
|
|
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
|
|
|
|
let Some(active_modal) = &self.active_modal else {
|
|
|
|
return div();
|
|
|
|
};
|
|
|
|
|
|
|
|
div()
|
|
|
|
.absolute()
|
|
|
|
.size_full()
|
|
|
|
.top_0()
|
|
|
|
.left_0()
|
|
|
|
.z_index(400)
|
|
|
|
.child(
|
|
|
|
v_stack()
|
|
|
|
.h(px(0.0))
|
|
|
|
.top_20()
|
2023-11-14 04:42:27 +00:00
|
|
|
.flex()
|
|
|
|
.flex_col()
|
|
|
|
.items_center()
|
2023-11-10 05:11:11 +00:00
|
|
|
.track_focus(&active_modal.focus_handle)
|
2023-11-14 04:42:27 +00:00
|
|
|
.child(
|
|
|
|
h_stack()
|
|
|
|
// needed to prevent mouse events leaking to the
|
|
|
|
// UI below. // todo! for gpui3.
|
|
|
|
.on_any_mouse_down(|_, _, cx| cx.stop_propagation())
|
|
|
|
.on_any_mouse_up(|_, _, cx| cx.stop_propagation())
|
|
|
|
.on_mouse_down_out(|this: &mut Self, event, cx| {
|
|
|
|
this.hide_modal(cx);
|
|
|
|
})
|
|
|
|
.child(active_modal.modal.clone()),
|
|
|
|
),
|
2023-11-10 05:11:11 +00:00
|
|
|
)
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
|
|
|
}
|