From 1610e270d6422271bd39006214c2a5410bc320c9 Mon Sep 17 00:00:00 2001 From: Alex Viscreanu Date: Fri, 21 Jul 2023 13:16:00 +0200 Subject: [PATCH 1/3] feat(workspace): add action for closing inactive editors on all panes --- assets/keymaps/default.json | 1 + crates/workspace/src/workspace.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/assets/keymaps/default.json b/assets/keymaps/default.json index 883b0c1872..5c841d19b2 100644 --- a/assets/keymaps/default.json +++ b/assets/keymaps/default.json @@ -22,6 +22,7 @@ "alt-cmd-right": "pane::ActivateNextItem", "cmd-w": "pane::CloseActiveItem", "alt-cmd-t": "pane::CloseInactiveItems", + "ctrl-alt-cmd-w": "workspace::CloseInactiveEditors", "cmd-k u": "pane::CloseCleanItems", "cmd-k cmd-w": "pane::CloseAllItems", "cmd-shift-w": "workspace::CloseWindow", diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 0ebd01e1f7..6694cc06a3 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -122,6 +122,7 @@ actions!( NewFile, NewWindow, CloseWindow, + CloseInactiveEditors, AddFolderToProject, Unfollow, Save, @@ -239,6 +240,7 @@ pub fn init(app_state: Arc, cx: &mut AppContext) { cx.add_async_action(Workspace::follow_next_collaborator); cx.add_async_action(Workspace::close); + cx.add_async_action(Workspace::close_inactive_editors); cx.add_global_action(Workspace::close_global); cx.add_global_action(restart); cx.add_async_action(Workspace::save_all); @@ -1633,6 +1635,34 @@ impl Workspace { } } + pub fn close_inactive_editors( + &mut self, + _: &CloseInactiveEditors, + cx: &mut ViewContext, + ) -> Option>> { + let current_pane = self.active_pane(); + + // let mut tasks: Vec>> = Vec::new(); + current_pane + .update(cx, |pane, cx| { + pane.close_inactive_items(&CloseInactiveItems, cx).unwrap() + }) + .detach_and_log_err(cx); + + for pane in self.panes() { + if pane.id() == current_pane.id() { + continue; + } + + pane.update(cx, |pane: &mut Pane, cx| { + pane.close_all_items(&CloseAllItems, cx).unwrap() + }) + .detach_and_log_err(cx); + } + + Some(Task::ready(Ok(()))) + } + pub fn toggle_dock(&mut self, dock_side: DockPosition, cx: &mut ViewContext) { let dock = match dock_side { DockPosition::Left => &self.left_dock, From a0fc515cfca9677dde32cef8be22321d1304a13e Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Thu, 27 Jul 2023 17:58:43 -0700 Subject: [PATCH 2/3] Rework close_inactive_items to await all tasks Update action name to be more accurate --- assets/keymaps/default.json | 2 +- crates/workspace/src/pane.rs | 4 +++ crates/workspace/src/workspace.rs | 42 ++++++++++++++++++++----------- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/assets/keymaps/default.json b/assets/keymaps/default.json index 5c841d19b2..090385458e 100644 --- a/assets/keymaps/default.json +++ b/assets/keymaps/default.json @@ -22,7 +22,7 @@ "alt-cmd-right": "pane::ActivateNextItem", "cmd-w": "pane::CloseActiveItem", "alt-cmd-t": "pane::CloseInactiveItems", - "ctrl-alt-cmd-w": "workspace::CloseInactiveEditors", + "ctrl-alt-cmd-w": "workspace::CloseInactiveTabsAndPanes", "cmd-k u": "pane::CloseCleanItems", "cmd-k cmd-w": "pane::CloseAllItems", "cmd-shift-w": "workspace::CloseWindow", diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index 2972c307f2..ee658c9cc9 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -746,6 +746,10 @@ impl Pane { _: &CloseAllItems, cx: &mut ViewContext, ) -> Option>> { + if self.items.is_empty() { + return None; + } + Some(self.close_items(cx, move |_| true)) } diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 6694cc06a3..827b0b8427 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -21,6 +21,7 @@ use drag_and_drop::DragAndDrop; use futures::{ channel::{mpsc, oneshot}, future::try_join_all, + stream::FuturesUnordered, FutureExt, StreamExt, }; use gpui::{ @@ -122,7 +123,7 @@ actions!( NewFile, NewWindow, CloseWindow, - CloseInactiveEditors, + CloseInactiveTabsAndPanes, AddFolderToProject, Unfollow, Save, @@ -240,7 +241,7 @@ pub fn init(app_state: Arc, cx: &mut AppContext) { cx.add_async_action(Workspace::follow_next_collaborator); cx.add_async_action(Workspace::close); - cx.add_async_action(Workspace::close_inactive_editors); + cx.add_async_action(Workspace::close_inactive_items_and_panes); cx.add_global_action(Workspace::close_global); cx.add_global_action(restart); cx.add_async_action(Workspace::save_all); @@ -1635,32 +1636,43 @@ impl Workspace { } } - pub fn close_inactive_editors( + pub fn close_inactive_items_and_panes( &mut self, - _: &CloseInactiveEditors, + _: &CloseInactiveTabsAndPanes, cx: &mut ViewContext, ) -> Option>> { let current_pane = self.active_pane(); - // let mut tasks: Vec>> = Vec::new(); - current_pane - .update(cx, |pane, cx| { - pane.close_inactive_items(&CloseInactiveItems, cx).unwrap() - }) - .detach_and_log_err(cx); + let mut tasks = Vec::new(); + + if let Some(current_pane_close) = current_pane.update(cx, |pane, cx| { + pane.close_inactive_items(&CloseInactiveItems, cx) + }) { + tasks.push(current_pane_close); + }; for pane in self.panes() { if pane.id() == current_pane.id() { continue; } - pane.update(cx, |pane: &mut Pane, cx| { - pane.close_all_items(&CloseAllItems, cx).unwrap() - }) - .detach_and_log_err(cx); + if let Some(close_pane_items) = pane.update(cx, |pane: &mut Pane, cx| { + pane.close_all_items(&CloseAllItems, cx) + }) { + tasks.push(close_pane_items) + } } - Some(Task::ready(Ok(()))) + if tasks.is_empty() { + None + } else { + Some(cx.spawn(|_, _| async move { + for task in tasks { + task.await? + } + Ok(()) + })) + } } pub fn toggle_dock(&mut self, dock_side: DockPosition, cx: &mut ViewContext) { From 4735b07088269e5b99e9299dbb9dd620e70f4497 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Thu, 27 Jul 2023 18:00:33 -0700 Subject: [PATCH 3/3] Fix warning --- crates/workspace/src/workspace.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 827b0b8427..a8ba017655 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -21,7 +21,6 @@ use drag_and_drop::DragAndDrop; use futures::{ channel::{mpsc, oneshot}, future::try_join_all, - stream::FuturesUnordered, FutureExt, StreamExt, }; use gpui::{