Use `cmd-9` and `cmd-shift-9` to toggle contacts panel focus/visibility

Antonio Scandurra created

Change summary

assets/keymaps/default.json                 | 14 ++++++++++++++
crates/contacts_panel/src/contacts_panel.rs |  4 ++++
crates/workspace/src/sidebar.rs             | 14 +++++++++++---
crates/workspace/src/workspace.rs           |  4 ++--
4 files changed, 31 insertions(+), 5 deletions(-)

Detailed changes

assets/keymaps/default.json 🔗

@@ -324,6 +324,20 @@
                     "side": "Left",
                     "item_index": 0
                 }
+            ],
+            "cmd-9": [
+                "workspace::ToggleSidebarItemFocus",
+                {
+                    "side": "Right",
+                    "item_index": 0
+                }
+            ],
+            "cmd-shift-(": [
+                "workspace::ToggleSidebarItem",
+                {
+                    "side": "Right",
+                    "item_index": 0
+                }
             ]
         }
     },

crates/contacts_panel/src/contacts_panel.rs 🔗

@@ -764,6 +764,10 @@ impl SidebarItem for ContactsPanel {
             .incoming_contact_requests()
             .is_empty()
     }
+
+    fn contains_focused_view(&self, cx: &AppContext) -> bool {
+        self.filter_editor.is_focused(cx)
+    }
 }
 
 fn render_icon_button(style: &IconButton, svg_path: &'static str) -> impl Element {

crates/workspace/src/sidebar.rs 🔗

@@ -10,10 +10,14 @@ use theme::Theme;
 
 pub trait SidebarItem: View {
     fn should_show_badge(&self, cx: &AppContext) -> bool;
+    fn contains_focused_view(&self, _: &AppContext) -> bool {
+        false
+    }
 }
 
 pub trait SidebarItemHandle {
     fn should_show_badge(&self, cx: &AppContext) -> bool;
+    fn is_focused(&self, cx: &AppContext) -> bool;
     fn to_any(&self) -> AnyViewHandle;
 }
 
@@ -25,6 +29,10 @@ where
         self.read(cx).should_show_badge(cx)
     }
 
+    fn is_focused(&self, cx: &AppContext) -> bool {
+        ViewHandle::is_focused(&self, cx) || self.read(cx).contains_focused_view(cx)
+    }
+
     fn to_any(&self) -> AnyViewHandle {
         self.into()
     }
@@ -114,10 +122,10 @@ impl Sidebar {
         cx.notify();
     }
 
-    pub fn active_item(&self) -> Option<&dyn SidebarItemHandle> {
+    pub fn active_item(&self) -> Option<&Rc<dyn SidebarItemHandle>> {
         self.active_item_ix
             .and_then(|ix| self.items.get(ix))
-            .map(|item| item.view.as_ref())
+            .map(|item| &item.view)
     }
 
     fn render_resize_handle(&self, theme: &Theme, cx: &mut RenderContext<Self>) -> ElementBox {
@@ -170,7 +178,7 @@ impl View for Sidebar {
 
             container.add_child(
                 Hook::new(
-                    ChildView::new(active_item)
+                    ChildView::new(active_item.to_any())
                         .constrained()
                         .with_max_width(*self.custom_width.borrow())
                         .boxed(),

crates/workspace/src/workspace.rs 🔗

@@ -1123,13 +1123,13 @@ impl Workspace {
         };
         let active_item = sidebar.update(cx, |sidebar, cx| {
             sidebar.activate_item(action.item_index, cx);
-            sidebar.active_item().map(|item| item.to_any())
+            sidebar.active_item().cloned()
         });
         if let Some(active_item) = active_item {
             if active_item.is_focused(cx) {
                 cx.focus_self();
             } else {
-                cx.focus(active_item);
+                cx.focus(active_item.to_any());
             }
         }
         cx.notify();