Close modals when focus leaves

This is more similar to zed1's behaviour, and allows us to work around
the difficulty in defining `on_mouse_down_out` for modals that have
overlays.
This commit is contained in:
Conrad Irwin 2024-01-15 19:20:35 -07:00
parent b565c44a9e
commit 62232060e6

View file

@ -27,7 +27,7 @@ impl<V: ModalView> ModalViewHandle for View<V> {
pub struct ActiveModal { pub struct ActiveModal {
modal: Box<dyn ModalViewHandle>, modal: Box<dyn ModalViewHandle>,
_subscription: Subscription, _subscriptions: [Subscription; 2],
previous_focus_handle: Option<FocusHandle>, previous_focus_handle: Option<FocusHandle>,
focus_handle: FocusHandle, focus_handle: FocusHandle,
} }
@ -61,13 +61,19 @@ impl ModalLayer {
where where
V: ModalView, V: ModalView,
{ {
let focus_handle = cx.focus_handle();
self.active_modal = Some(ActiveModal { self.active_modal = Some(ActiveModal {
modal: Box::new(new_modal.clone()), modal: Box::new(new_modal.clone()),
_subscription: cx.subscribe(&new_modal, |this, _, _: &DismissEvent, cx| { _subscriptions: [
this.hide_modal(cx); cx.subscribe(&new_modal, |this, _, _: &DismissEvent, cx| {
}), this.hide_modal(cx);
}),
cx.on_focus_out(&focus_handle, |this, cx| {
this.hide_modal(cx);
}),
],
previous_focus_handle: cx.focused(), previous_focus_handle: cx.focused(),
focus_handle: cx.focus_handle(), focus_handle,
}); });
cx.focus_view(&new_modal); cx.focus_view(&new_modal);
cx.notify(); cx.notify();
@ -127,13 +133,7 @@ impl Render for ModalLayer {
.flex_col() .flex_col()
.items_center() .items_center()
.track_focus(&active_modal.focus_handle) .track_focus(&active_modal.focus_handle)
.child( .child(h_flex().child(active_modal.modal.view())),
h_flex()
.on_mouse_down_out(cx.listener(|this, _, cx| {
this.hide_modal(cx);
}))
.child(active_modal.modal.view()),
),
) )
} }
} }