From e6d6806693c86ae91ec81b1434766cb6af4d4497 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 9 Nov 2023 21:11:10 -0700 Subject: [PATCH] Tidy up some more modal behaviour --- .../command_palette2/src/command_palette.rs | 6 +-- crates/go_to_line2/src/go_to_line.rs | 4 -- crates/gpui2/src/elements/uniform_list.rs | 8 ---- crates/gpui2/src/interactive.rs | 6 +-- crates/workspace2/src/modal_layer.rs | 44 +++++++++++-------- 5 files changed, 27 insertions(+), 41 deletions(-) diff --git a/crates/command_palette2/src/command_palette.rs b/crates/command_palette2/src/command_palette.rs index 6fa24b7a2e..77d64d63da 100644 --- a/crates/command_palette2/src/command_palette.rs +++ b/crates/command_palette2/src/command_palette.rs @@ -65,11 +65,7 @@ impl CommandPalette { let delegate = CommandPaletteDelegate::new(cx.view().downgrade(), commands, previous_focus_handle, cx); - let picker = cx.build_view(|cx| { - let picker = Picker::new(delegate, cx); - picker.focus(cx); - picker - }); + let picker = cx.build_view(|cx| Picker::new(delegate, cx)); Self { picker } } } diff --git a/crates/go_to_line2/src/go_to_line.rs b/crates/go_to_line2/src/go_to_line.rs index cc41f63718..38b46df4e2 100644 --- a/crates/go_to_line2/src/go_to_line.rs +++ b/crates/go_to_line2/src/go_to_line.rs @@ -126,10 +126,6 @@ impl GoToLine { } fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext) { - self.active_editor.update(cx, |editor, cx| { - editor.focus(cx); - cx.notify(); - }); cx.emit(ModalEvent::Dismissed); } diff --git a/crates/gpui2/src/elements/uniform_list.rs b/crates/gpui2/src/elements/uniform_list.rs index 181803e1e6..2fe61f5909 100644 --- a/crates/gpui2/src/elements/uniform_list.rs +++ b/crates/gpui2/src/elements/uniform_list.rs @@ -291,11 +291,3 @@ impl Component for UniformList { AnyElement::new(self) } } - -#[cfg(test)] -mod test { - use crate::{self as gpui, TestAppContext}; - - #[gpui::test] - fn test_uniform_list(cx: &mut TestAppContext) {} -} diff --git a/crates/gpui2/src/interactive.rs b/crates/gpui2/src/interactive.rs index a546c1b40b..243eb3cb07 100644 --- a/crates/gpui2/src/interactive.rs +++ b/crates/gpui2/src/interactive.rs @@ -94,7 +94,6 @@ pub trait StatelessInteractive: Element { fn on_mouse_down_out( mut self, - button: MouseButton, handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext) + 'static, ) -> Self where @@ -103,10 +102,7 @@ pub trait StatelessInteractive: Element { self.stateless_interactivity() .mouse_down_listeners .push(Box::new(move |view, event, bounds, phase, cx| { - if phase == DispatchPhase::Capture - && event.button == button - && !bounds.contains_point(&event.position) - { + if phase == DispatchPhase::Capture && !bounds.contains_point(&event.position) { handler(view, event, cx) } })); diff --git a/crates/workspace2/src/modal_layer.rs b/crates/workspace2/src/modal_layer.rs index 8a3f724972..aa5b2e7848 100644 --- a/crates/workspace2/src/modal_layer.rs +++ b/crates/workspace2/src/modal_layer.rs @@ -55,31 +55,38 @@ impl ModalLayer { let build_view = build_view.clone(); div.on_action(move |workspace, event: &A, cx| { - let previous_focus = cx.focused(); - if let Some(active_modal) = &workspace.modal_layer().active_modal { - if active_modal.modal.clone().downcast::().is_ok() { - workspace.modal_layer().hide_modal(cx); - return; - } - } - let Some(new_modal) = (build_view)(cx) else { - return; - }; - workspace - .modal_layer() - .show_modal(previous_focus, new_modal, cx); + workspace.modal_layer().toggle_modal(build_view.clone(), cx) }) }), )); } + pub fn toggle_modal(&mut self, build_view: Arc, cx: &mut ViewContext) + where + V: Modal, + B: Fn(&mut WindowContext) -> Option> + 'static, + { + let previous_focus = cx.focused(); + + if let Some(active_modal) = &self.active_modal { + if active_modal.modal.clone().downcast::().is_ok() { + self.hide_modal(cx); + return; + } + } + let Some(new_modal) = (build_view)(cx) else { + return; + }; + self.show_modal(previous_focus, new_modal, cx); + } + pub fn show_modal( &mut self, previous_focus: Option, new_modal: View, cx: &mut ViewContext, ) where - V: EventEmitter + Render, + V: Modal, { self.active_modal = Some(ActiveModal { modal: new_modal.clone().into(), @@ -93,13 +100,9 @@ impl ModalLayer { } pub fn hide_modal(&mut self, cx: &mut ViewContext) { - dbg!("hiding..."); if let Some(active_modal) = self.active_modal.take() { - dbg!("something"); if let Some(previous_focus) = active_modal.previous_focus_handle { - dbg!("oohthing"); if active_modal.focus_handle.contains_focused(cx) { - dbg!("aahthing"); previous_focus.focus(cx); } } @@ -133,7 +136,10 @@ impl ModalLayer { .h(px(0.0)) .relative() .top_20() - .track_focus(&open_modal.focus_handle); + .track_focus(&open_modal.focus_handle) + .on_mouse_down_out(|workspace: &mut Workspace, _, cx| { + workspace.modal_layer().hide_modal(cx); + }); parent.child(container1.child(container2.child(open_modal.modal.clone()))) })