Focus toggled elements when interacting with the sidebars

Antonio Scandurra created

Also, restore focus on the workspace when there is no active item
on the sidebar that was just toggled.

Change summary

gpui/src/app.rs              | 6 ++++++
zed/src/chat_panel.rs        | 4 ++++
zed/src/workspace.rs         | 5 +++++
zed/src/workspace/sidebar.rs | 2 +-
4 files changed, 16 insertions(+), 1 deletion(-)

Detailed changes

gpui/src/app.rs 🔗

@@ -2857,6 +2857,12 @@ impl Clone for AnyViewHandle {
     }
 }
 
+impl From<&AnyViewHandle> for AnyViewHandle {
+    fn from(handle: &AnyViewHandle) -> Self {
+        handle.clone()
+    }
+}
+
 impl<T: View> From<&ViewHandle<T>> for AnyViewHandle {
     fn from(handle: &ViewHandle<T>) -> Self {
         handle

zed/src/chat_panel.rs 🔗

@@ -214,6 +214,10 @@ impl View for ChatPanel {
         .with_style(&theme.chat_panel.container)
         .boxed()
     }
+
+    fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
+        cx.focus(&self.input_editor);
+    }
 }
 
 fn format_timestamp(mut timestamp: OffsetDateTime, mut now: OffsetDateTime) -> String {

zed/src/workspace.rs 🔗

@@ -767,6 +767,11 @@ impl Workspace {
             Side::Right => &mut self.right_sidebar,
         };
         sidebar.toggle_item(action.0.item_index);
+        if let Some(active_item) = sidebar.active_item() {
+            cx.focus(active_item);
+        } else {
+            cx.focus_self();
+        }
         cx.notify();
     }
 

zed/src/workspace/sidebar.rs 🔗

@@ -49,7 +49,7 @@ impl Sidebar {
         if self.active_item_ix == Some(item_ix) {
             self.active_item_ix = None;
         } else {
-            self.active_item_ix = Some(item_ix)
+            self.active_item_ix = Some(item_ix);
         }
     }