From 21206800bcfee08d7ebdd87208389d1190c99f7e Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 20 May 2022 16:53:03 -0700 Subject: [PATCH] Add "Close Window" command --- assets/keymaps/default.json | 1 + crates/workspace/src/pane.rs | 8 ++++++++ crates/workspace/src/workspace.rs | 25 +++++++++++++++++++++++++ crates/zed/src/menus.rs | 4 ++++ 4 files changed, 38 insertions(+) diff --git a/assets/keymaps/default.json b/assets/keymaps/default.json index e80cd8a87f..40ac3d6fe3 100644 --- a/assets/keymaps/default.json +++ b/assets/keymaps/default.json @@ -14,6 +14,7 @@ "shift-cmd-{": "pane::ActivatePrevItem", "shift-cmd-}": "pane::ActivateNextItem", "cmd-w": "pane::CloseActiveItem", + "cmd-shift-W": "workspace::CloseWindow", "alt-cmd-w": "pane::CloseInactiveItems", "cmd-s": "workspace::Save", "cmd-shift-S": "workspace::SaveAs", diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index 97bb8a2bc0..a00ddef9b7 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -482,6 +482,14 @@ impl Pane { }) } + pub fn close_all_items( + workspace: &mut Workspace, + pane: ViewHandle, + cx: &mut ViewContext, + ) -> Task> { + Self::close_items(workspace, pane, cx, |_| true) + } + pub fn close_items( workspace: &mut Workspace, pane: ViewHandle, diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 3a4ee0f211..5805a66bd5 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -77,6 +77,7 @@ actions!( Open, NewFile, NewWindow, + CloseWindow, AddFolderToProject, Unfollow, Save, @@ -142,6 +143,7 @@ pub fn init(app_state: Arc, cx: &mut MutableAppContext) { cx.add_async_action(Workspace::toggle_follow); cx.add_async_action(Workspace::follow_next_collaborator); + cx.add_async_action(Workspace::close); cx.add_action(Workspace::add_folder_to_project); cx.add_action( |workspace: &mut Workspace, _: &Unfollow, cx: &mut ViewContext| { @@ -881,6 +883,29 @@ impl Workspace { } } + fn close(&mut self, _: &CloseWindow, cx: &mut ViewContext) -> Option>> { + let mut tasks = Vec::new(); + for pane in self.panes.clone() { + tasks.push(Pane::close_all_items(self, pane, cx)); + } + Some(cx.spawn(|this, mut cx| async move { + for task in tasks { + task.await?; + } + this.update(&mut cx, |this, cx| { + if this + .panes + .iter() + .all(|pane| pane.read(cx).items().next().is_none()) + { + let window_id = cx.window_id(); + cx.remove_window(window_id); + } + }); + Ok(()) + })) + } + pub fn open_paths( &mut self, mut abs_paths: Vec, diff --git a/crates/zed/src/menus.rs b/crates/zed/src/menus.rs index b7aabffd18..8fafddb79e 100644 --- a/crates/zed/src/menus.rs +++ b/crates/zed/src/menus.rs @@ -58,6 +58,10 @@ pub fn menus() -> Vec> { name: "Close Editor", action: Box::new(workspace::CloseActiveItem), }, + MenuItem::Action { + name: "Close Window", + action: Box::new(workspace::CloseWindow), + }, ], }, Menu {