commit
1fecd3c327
13 changed files with 115 additions and 58 deletions
|
@ -1,11 +1,32 @@
|
|||
use gpui::{
|
||||
div, prelude::*, px, AnyView, Div, FocusHandle, ManagedView, Render, Subscription, View,
|
||||
ViewContext,
|
||||
div, prelude::*, px, AnyView, Div, FocusHandle, ManagedView, Render, Subscription, Task, View,
|
||||
ViewContext, WindowContext,
|
||||
};
|
||||
use ui::{h_stack, v_stack};
|
||||
|
||||
pub trait ModalView: ManagedView {
|
||||
fn dismiss(&mut self, cx: &mut ViewContext<Self>) -> Task<bool> {
|
||||
Task::ready(true)
|
||||
}
|
||||
}
|
||||
|
||||
trait ModalViewHandle {
|
||||
fn should_dismiss(&mut self, cx: &mut WindowContext) -> Task<bool>;
|
||||
fn view(&self) -> AnyView;
|
||||
}
|
||||
|
||||
impl<V: ModalView> ModalViewHandle for View<V> {
|
||||
fn should_dismiss(&mut self, cx: &mut WindowContext) -> Task<bool> {
|
||||
self.update(cx, |this, cx| this.dismiss(cx))
|
||||
}
|
||||
|
||||
fn view(&self) -> AnyView {
|
||||
self.clone().into()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ActiveModal {
|
||||
modal: AnyView,
|
||||
modal: Box<dyn ModalViewHandle>,
|
||||
subscription: Subscription,
|
||||
previous_focus_handle: Option<FocusHandle>,
|
||||
focus_handle: FocusHandle,
|
||||
|
@ -22,11 +43,11 @@ impl ModalLayer {
|
|||
|
||||
pub fn toggle_modal<V, B>(&mut self, cx: &mut ViewContext<Self>, build_view: B)
|
||||
where
|
||||
V: ManagedView,
|
||||
V: ModalView,
|
||||
B: FnOnce(&mut ViewContext<V>) -> V,
|
||||
{
|
||||
if let Some(active_modal) = &self.active_modal {
|
||||
let is_close = active_modal.modal.clone().downcast::<V>().is_ok();
|
||||
let is_close = active_modal.modal.view().downcast::<V>().is_ok();
|
||||
self.hide_modal(cx);
|
||||
if is_close {
|
||||
return;
|
||||
|
@ -38,10 +59,10 @@ impl ModalLayer {
|
|||
|
||||
pub fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>)
|
||||
where
|
||||
V: ManagedView,
|
||||
V: ModalView,
|
||||
{
|
||||
self.active_modal = Some(ActiveModal {
|
||||
modal: new_modal.clone().into(),
|
||||
modal: Box::new(new_modal.clone()),
|
||||
subscription: cx.subscribe(&new_modal, |this, modal, e, cx| this.hide_modal(cx)),
|
||||
previous_focus_handle: cx.focused(),
|
||||
focus_handle: cx.focus_handle(),
|
||||
|
@ -51,15 +72,28 @@ impl ModalLayer {
|
|||
}
|
||||
|
||||
pub fn hide_modal(&mut self, cx: &mut ViewContext<Self>) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
let Some(active_modal) = self.active_modal.as_mut() else {
|
||||
return;
|
||||
};
|
||||
|
||||
cx.notify();
|
||||
let dismiss = active_modal.modal.should_dismiss(cx);
|
||||
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
if dismiss.await {
|
||||
this.update(&mut cx, |this, cx| {
|
||||
if let Some(active_modal) = this.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);
|
||||
}
|
||||
}
|
||||
cx.notify();
|
||||
}
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
pub fn active_modal<V>(&self) -> Option<View<V>>
|
||||
|
@ -67,7 +101,7 @@ impl ModalLayer {
|
|||
V: 'static,
|
||||
{
|
||||
let active_modal = self.active_modal.as_ref()?;
|
||||
active_modal.modal.clone().downcast::<V>().ok()
|
||||
active_modal.modal.view().downcast::<V>().ok()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -98,7 +132,7 @@ impl Render for ModalLayer {
|
|||
.on_mouse_down_out(cx.listener(|this, _, cx| {
|
||||
this.hide_modal(cx);
|
||||
}))
|
||||
.child(active_modal.modal.clone()),
|
||||
.child(active_modal.modal.view()),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -3414,7 +3414,7 @@ impl Workspace {
|
|||
self.modal_layer.read(cx).active_modal()
|
||||
}
|
||||
|
||||
pub fn toggle_modal<V: ManagedView, B>(&mut self, cx: &mut ViewContext<Self>, build: B)
|
||||
pub fn toggle_modal<V: ModalView, B>(&mut self, cx: &mut ViewContext<Self>, build: B)
|
||||
where
|
||||
B: FnOnce(&mut ViewContext<V>) -> V,
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue