Tidy up some more modal behaviour

Conrad Irwin created

Change summary

crates/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(-)

Detailed changes

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 }
     }
 }

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>) {
-        self.active_editor.update(cx, |editor, cx| {
-            editor.focus(cx);
-            cx.notify();
-        });
         cx.emit(ModalEvent::Dismissed);
     }
 

crates/gpui2/src/elements/uniform_list.rs 🔗

@@ -291,11 +291,3 @@ impl<V: 'static> Component<V> for UniformList<V> {
         AnyElement::new(self)
     }
 }
-
-#[cfg(test)]
-mod test {
-    use crate::{self as gpui, TestAppContext};
-
-    #[gpui::test]
-    fn test_uniform_list(cx: &mut TestAppContext) {}
-}

crates/gpui2/src/interactive.rs 🔗

@@ -94,7 +94,6 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
 
     fn on_mouse_down_out(
         mut self,
-        button: MouseButton,
         handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + 'static,
     ) -> Self
     where
@@ -103,10 +102,7 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
         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)
                 }
             }));

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::<V>().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<V, B>(&mut self, build_view: Arc<B>, cx: &mut ViewContext<Workspace>)
+    where
+        V: Modal,
+        B: Fn(&mut WindowContext) -> Option<View<V>> + 'static,
+    {
+        let previous_focus = cx.focused();
+
+        if let Some(active_modal) = &self.active_modal {
+            if active_modal.modal.clone().downcast::<V>().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<V>(
         &mut self,
         previous_focus: Option<FocusHandle>,
         new_modal: View<V>,
         cx: &mut ViewContext<Workspace>,
     ) where
-        V: EventEmitter<ModalEvent> + 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<Workspace>) {
-        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())))
         })