Activate previous pane and next pane via `cmd-k cmd-left` and `cmd-k cmd-right`

Antonio Scandurra and Nathan Sobo created

Co-Authored-By: Nathan Sobo <nathan@zed.dev>

Change summary

crates/workspace/src/workspace.rs | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)

Detailed changes

crates/workspace/src/workspace.rs 🔗

@@ -78,6 +78,8 @@ action!(Unfollow);
 action!(JoinProject, JoinProjectParams);
 action!(Save);
 action!(DebugElements);
+action!(ActivatePreviousPane);
+action!(ActivateNextPane);
 
 pub fn init(client: &Arc<Client>, cx: &mut MutableAppContext) {
     pane::init(cx);
@@ -111,10 +113,18 @@ pub fn init(client: &Arc<Client>, cx: &mut MutableAppContext) {
     cx.add_action(Workspace::debug_elements);
     cx.add_action(Workspace::toggle_sidebar_item);
     cx.add_action(Workspace::toggle_sidebar_item_focus);
+    cx.add_action(|workspace: &mut Workspace, _: &ActivatePreviousPane, cx| {
+        workspace.activate_previous_pane(cx)
+    });
+    cx.add_action(|workspace: &mut Workspace, _: &ActivateNextPane, cx| {
+        workspace.activate_next_pane(cx)
+    });
     cx.add_bindings(vec![
         Binding::new("ctrl-alt-cmd-f", FollowNextCollaborator, None),
         Binding::new("cmd-s", Save, None),
         Binding::new("cmd-alt-i", DebugElements, None),
+        Binding::new("cmd-k cmd-left", ActivatePreviousPane, None),
+        Binding::new("cmd-k cmd-right", ActivateNextPane, None),
         Binding::new(
             "cmd-shift-!",
             ToggleSidebarItem(SidebarItemId {
@@ -1159,6 +1169,20 @@ impl Workspace {
         self.activate_pane(self.panes[next_ix].clone(), cx);
     }
 
+    pub fn activate_previous_pane(&mut self, cx: &mut ViewContext<Self>) {
+        let ix = self
+            .panes
+            .iter()
+            .position(|pane| pane == &self.active_pane)
+            .unwrap();
+        let prev_ix = if ix == 0 {
+            self.panes.len() - 1
+        } else {
+            ix - 1
+        };
+        self.activate_pane(self.panes[prev_ix].clone(), cx);
+    }
+
     fn activate_pane(&mut self, pane: ViewHandle<Pane>, cx: &mut ViewContext<Self>) {
         if self.active_pane != pane {
             self.active_pane = pane.clone();