Focus command palette when hitting cmd-shift-p if file finder is open

Antonio Scandurra created

...and vice versa.

Change summary

crates/gpui/src/app.rs        | 101 ++++++++++++++++++++++++++++++------
crates/gpui/src/app/window.rs |   7 +-
2 files changed, 87 insertions(+), 21 deletions(-)

Detailed changes

crates/gpui/src/app.rs 🔗

@@ -1451,27 +1451,13 @@ impl AppContext {
                 self.views_metadata.remove(&(window_id, view_id));
                 let mut view = self.views.remove(&(window_id, view_id)).unwrap();
                 view.release(self);
-                let change_focus_to = self.windows.get_mut(&window_id).and_then(|window| {
+                if let Some(window) = self.windows.get_mut(&window_id) {
                     window.parents.remove(&view_id);
                     window
                         .invalidation
                         .get_or_insert_with(Default::default)
                         .removed
                         .push(view_id);
-                    if window.focused_view_id == Some(view_id) {
-                        Some(window.root_view().id())
-                    } else {
-                        None
-                    }
-                });
-
-                if let Some(view_id) = change_focus_to {
-                    self.pending_effects
-                        .push_back(Effect::Focus(FocusEffect::View {
-                            window_id,
-                            view_id: Some(view_id),
-                            is_forced: false,
-                        }));
                 }
 
                 self.pending_effects
@@ -1708,8 +1694,69 @@ impl AppContext {
                             if let Some(invalidation) = invalidation {
                                 let appearance = cx.window.platform_window.appearance();
                                 cx.invalidate(invalidation, appearance);
-                                if cx.layout(refreshing).log_err().is_some() {
+                                if let Some(old_parents) = cx.layout(refreshing).log_err() {
                                     updated_windows.insert(window_id);
+
+                                    if let Some(focused_view_id) = cx.focused_view_id() {
+                                        let old_ancestors = std::iter::successors(
+                                            Some(focused_view_id),
+                                            |&view_id| old_parents.get(&view_id).copied(),
+                                        )
+                                        .collect::<HashSet<_>>();
+                                        let new_ancestors =
+                                            cx.ancestors(focused_view_id).collect::<HashSet<_>>();
+
+                                        // Notify the old ancestors of the focused view when they don't contain it anymore.
+                                        for old_ancestor in old_ancestors.iter().copied() {
+                                            if !new_ancestors.contains(&old_ancestor) {
+                                                if let Some(mut view) =
+                                                    cx.views.remove(&(window_id, old_ancestor))
+                                                {
+                                                    view.focus_out(
+                                                        focused_view_id,
+                                                        cx,
+                                                        old_ancestor,
+                                                    );
+                                                    cx.views
+                                                        .insert((window_id, old_ancestor), view);
+                                                }
+                                            }
+                                        }
+
+                                        // Notify the new ancestors of the focused view if they contain it now.
+                                        for new_ancestor in new_ancestors.iter().copied() {
+                                            if !old_ancestors.contains(&new_ancestor) {
+                                                if let Some(mut view) =
+                                                    cx.views.remove(&(window_id, new_ancestor))
+                                                {
+                                                    view.focus_in(
+                                                        focused_view_id,
+                                                        cx,
+                                                        new_ancestor,
+                                                    );
+                                                    cx.views
+                                                        .insert((window_id, new_ancestor), view);
+                                                }
+                                            }
+                                        }
+
+                                        // When the previously-focused view has been dropped and
+                                        // there isn't any pending focus, focus the root view.
+                                        let root_view_id = cx.window.root_view().id();
+                                        if focused_view_id != root_view_id
+                                            && !cx.views.contains_key(&(window_id, focused_view_id))
+                                            && !focus_effects.contains_key(&window_id)
+                                        {
+                                            focus_effects.insert(
+                                                window_id,
+                                                FocusEffect::View {
+                                                    window_id,
+                                                    view_id: Some(root_view_id),
+                                                    is_forced: false,
+                                                },
+                                            );
+                                        }
+                                    }
                                 }
                             }
                         });
@@ -1886,9 +1933,27 @@ impl AppContext {
     fn handle_focus_effect(&mut self, effect: FocusEffect) {
         let window_id = effect.window_id();
         self.update_window(window_id, |cx| {
+            // Ensure the newly-focused view still exists, otherwise focus
+            // the root view instead.
             let focused_id = match effect {
-                FocusEffect::View { view_id, .. } => view_id,
-                FocusEffect::ViewParent { view_id, .. } => cx.ancestors(view_id).skip(1).next(),
+                FocusEffect::View { view_id, .. } => {
+                    if let Some(view_id) = view_id {
+                        if cx.views.contains_key(&(window_id, view_id)) {
+                            Some(view_id)
+                        } else {
+                            Some(cx.root_view().id())
+                        }
+                    } else {
+                        None
+                    }
+                }
+                FocusEffect::ViewParent { view_id, .. } => Some(
+                    cx.window
+                        .parents
+                        .get(&view_id)
+                        .copied()
+                        .unwrap_or(cx.root_view().id()),
+                ),
             };
 
             let focus_changed = cx.window.focused_view_id != focused_id;

crates/gpui/src/app/window.rs 🔗

@@ -29,6 +29,7 @@ use sqlez::{
 };
 use std::{
     any::TypeId,
+    mem,
     ops::{Deref, DerefMut, Range},
 };
 use util::ResultExt;
@@ -890,7 +891,7 @@ impl<'a> WindowContext<'a> {
         Ok(element)
     }
 
-    pub(crate) fn layout(&mut self, refreshing: bool) -> Result<()> {
+    pub(crate) fn layout(&mut self, refreshing: bool) -> Result<HashMap<usize, usize>> {
         let window_size = self.window.platform_window.content_size();
         let root_view_id = self.window.root_view().id();
         let mut rendered_root = self.window.rendered_views.remove(&root_view_id).unwrap();
@@ -923,11 +924,11 @@ impl<'a> WindowContext<'a> {
             }
         }
 
-        self.window.parents = new_parents;
+        let old_parents = mem::replace(&mut self.window.parents, new_parents);
         self.window
             .rendered_views
             .insert(root_view_id, rendered_root);
-        Ok(())
+        Ok(old_parents)
     }
 
     pub(crate) fn paint(&mut self) -> Result<Scene> {