2023-11-06 22:35:49 +00:00
|
|
|
use gpui::{
|
2023-12-13 03:30:30 +00:00
|
|
|
div, prelude::*, px, AnyView, DismissEvent, Div, FocusHandle, ManagedView, Render,
|
|
|
|
Subscription, View, ViewContext, 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-12-08 20:26:06 +00:00
|
|
|
pub trait ModalView: ManagedView {
|
2023-12-13 03:30:30 +00:00
|
|
|
fn on_before_dismiss(&mut self, cx: &mut ViewContext<Self>) -> bool {
|
|
|
|
true
|
2023-12-08 20:26:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait ModalViewHandle {
|
2023-12-13 03:30:30 +00:00
|
|
|
fn on_before_dismiss(&mut self, cx: &mut WindowContext) -> bool;
|
2023-12-08 20:26:06 +00:00
|
|
|
fn view(&self) -> AnyView;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V: ModalView> ModalViewHandle for View<V> {
|
2023-12-13 03:30:30 +00:00
|
|
|
fn on_before_dismiss(&mut self, cx: &mut WindowContext) -> bool {
|
|
|
|
self.update(cx, |this, cx| this.on_before_dismiss(cx))
|
2023-12-08 20:26:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> AnyView {
|
|
|
|
self.clone().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 03:58:35 +00:00
|
|
|
pub struct ActiveModal {
|
2023-12-08 20:26:06 +00:00
|
|
|
modal: Box<dyn ModalViewHandle>,
|
2023-11-10 03:58:35 +00:00
|
|
|
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-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-12-08 20:26:06 +00:00
|
|
|
V: ModalView,
|
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-12-08 20:26:06 +00:00
|
|
|
let is_close = active_modal.modal.view().downcast::<V>().is_ok();
|
2023-12-13 03:30:30 +00:00
|
|
|
let did_close = self.hide_modal(cx);
|
|
|
|
if is_close || !did_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-12-13 03:30:30 +00:00
|
|
|
fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>)
|
2023-11-10 04:51:48 +00:00
|
|
|
where
|
2023-12-08 20:26:06 +00:00
|
|
|
V: ModalView,
|
2023-11-09 04:03:46 +00:00
|
|
|
{
|
2023-11-10 03:58:35 +00:00
|
|
|
self.active_modal = Some(ActiveModal {
|
2023-12-08 20:26:06 +00:00
|
|
|
modal: Box::new(new_modal.clone()),
|
2023-12-13 03:30:30 +00:00
|
|
|
subscription: cx.subscribe(&new_modal, |this, modal, _: &DismissEvent, cx| {
|
|
|
|
this.hide_modal(cx);
|
|
|
|
}),
|
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-17 05:46:44 +00:00
|
|
|
cx.focus_view(&new_modal);
|
2023-11-08 21:45:36 +00:00
|
|
|
cx.notify();
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 03:30:30 +00:00
|
|
|
fn hide_modal(&mut self, cx: &mut ViewContext<Self>) -> bool {
|
2023-12-08 20:26:06 +00:00
|
|
|
let Some(active_modal) = self.active_modal.as_mut() else {
|
2023-12-13 03:30:30 +00:00
|
|
|
return false;
|
2023-12-08 20:26:06 +00:00
|
|
|
};
|
2023-11-10 03:58:35 +00:00
|
|
|
|
2023-12-13 03:30:30 +00:00
|
|
|
let dismiss = active_modal.modal.on_before_dismiss(cx);
|
|
|
|
if !dismiss {
|
|
|
|
return false;
|
|
|
|
}
|
2023-12-08 20:26:06 +00:00
|
|
|
|
2023-12-13 03:30:30 +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-12-08 20:26:06 +00:00
|
|
|
}
|
2023-12-13 03:30:30 +00:00
|
|
|
cx.notify();
|
|
|
|
}
|
|
|
|
true
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
2023-11-14 16:28:18 +00:00
|
|
|
|
2023-11-15 20:07:18 +00:00
|
|
|
pub fn active_modal<V>(&self) -> Option<View<V>>
|
2023-11-14 16:28:18 +00:00
|
|
|
where
|
|
|
|
V: 'static,
|
|
|
|
{
|
|
|
|
let active_modal = self.active_modal.as_ref()?;
|
2023-12-08 20:26:06 +00:00
|
|
|
active_modal.modal.view().downcast::<V>().ok()
|
2023-11-14 16:28:18 +00:00
|
|
|
}
|
2023-11-10 05:11:11 +00:00
|
|
|
}
|
2023-11-06 22:35:49 +00:00
|
|
|
|
2023-11-20 22:46:01 +00:00
|
|
|
impl Render for ModalLayer {
|
|
|
|
type Element = Div;
|
2023-11-10 05:11:11 +00:00
|
|
|
|
|
|
|
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()
|
2023-11-20 22:46:01 +00:00
|
|
|
.on_mouse_down_out(cx.listener(|this, _, cx| {
|
2023-11-14 04:42:27 +00:00
|
|
|
this.hide_modal(cx);
|
2023-11-20 22:46:01 +00:00
|
|
|
}))
|
2023-12-08 20:26:06 +00:00
|
|
|
.child(active_modal.modal.view()),
|
2023-11-14 04:42:27 +00:00
|
|
|
),
|
2023-11-10 05:11:11 +00:00
|
|
|
)
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
|
|
|
}
|