fix modal exchange (#3620)

- Fix opening GoToLine from Command
- Extend test to cover go_to_line behavior too

Release Notes:

- N/A
This commit is contained in:
Conrad Irwin
2023-12-12 21:30:34 -07:00
committed by GitHub
6 changed files with 88 additions and 36 deletions
+27 -29
View File
@@ -1,23 +1,23 @@
use gpui::{
div, prelude::*, px, AnyView, Div, FocusHandle, ManagedView, Render, Subscription, Task, View,
ViewContext, WindowContext,
div, prelude::*, px, AnyView, DismissEvent, Div, FocusHandle, ManagedView, Render,
Subscription, 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)
fn on_before_dismiss(&mut self, cx: &mut ViewContext<Self>) -> bool {
true
}
}
trait ModalViewHandle {
fn should_dismiss(&mut self, cx: &mut WindowContext) -> Task<bool>;
fn on_before_dismiss(&mut self, cx: &mut WindowContext) -> 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 on_before_dismiss(&mut self, cx: &mut WindowContext) -> bool {
self.update(cx, |this, cx| this.on_before_dismiss(cx))
}
fn view(&self) -> AnyView {
@@ -48,8 +48,8 @@ impl ModalLayer {
{
if let Some(active_modal) = &self.active_modal {
let is_close = active_modal.modal.view().downcast::<V>().is_ok();
self.hide_modal(cx);
if is_close {
let did_close = self.hide_modal(cx);
if is_close || !did_close {
return;
}
}
@@ -57,13 +57,15 @@ impl ModalLayer {
self.show_modal(new_modal, cx);
}
pub fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>)
fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>)
where
V: ModalView,
{
self.active_modal = Some(ActiveModal {
modal: Box::new(new_modal.clone()),
subscription: cx.subscribe(&new_modal, |this, modal, e, cx| this.hide_modal(cx)),
subscription: cx.subscribe(&new_modal, |this, modal, _: &DismissEvent, cx| {
this.hide_modal(cx);
}),
previous_focus_handle: cx.focused(),
focus_handle: cx.focus_handle(),
});
@@ -71,29 +73,25 @@ impl ModalLayer {
cx.notify();
}
pub fn hide_modal(&mut self, cx: &mut ViewContext<Self>) {
fn hide_modal(&mut self, cx: &mut ViewContext<Self>) -> bool {
let Some(active_modal) = self.active_modal.as_mut() else {
return;
return false;
};
let dismiss = active_modal.modal.should_dismiss(cx);
let dismiss = active_modal.modal.on_before_dismiss(cx);
if !dismiss {
return false;
}
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();
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);
}
}
})
.detach();
cx.notify();
}
true
}
pub fn active_modal<V>(&self) -> Option<View<V>>