Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Tidy up some more modal behaviour
Conrad Irwine6d6806693c8
diff --git a/crates/command_palette2/src/command_palette.rs b/crates/command_palette2/src/command_palette.rsindex 6fa24b7a2e6..77d64d63da1 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.rsindex cc41f637186..38b46df4e26 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>) {- 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.rsindex 181803e1e69..2fe61f5909a 100644--- a/crates/gpui2/src/elements/uniform_list.rs+++ b/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) {}-}diff --git a/crates/gpui2/src/interactive.rs b/crates/gpui2/src/interactive.rsindex a546c1b40b9..243eb3cb078 100644--- a/crates/gpui2/src/interactive.rs+++ b/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,) -> Selfwhere@@ -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)}}));diff --git a/crates/workspace2/src/modal_layer.rs b/crates/workspace2/src/modal_layer.rsindex 8a3f7249729..aa5b2e78482 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::<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())))})