Add "Close Window" command

Max Brunsfeld created

Change summary

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(+)

Detailed changes

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",

crates/workspace/src/pane.rs 🔗

@@ -482,6 +482,14 @@ impl Pane {
         })
     }
 
+    pub fn close_all_items(
+        workspace: &mut Workspace,
+        pane: ViewHandle<Pane>,
+        cx: &mut ViewContext<Workspace>,
+    ) -> Task<Result<()>> {
+        Self::close_items(workspace, pane, cx, |_| true)
+    }
+
     pub fn close_items(
         workspace: &mut Workspace,
         pane: ViewHandle<Pane>,

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<AppState>, 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<Workspace>| {
@@ -881,6 +883,29 @@ impl Workspace {
         }
     }
 
+    fn close(&mut self, _: &CloseWindow, cx: &mut ViewContext<Self>) -> Option<Task<Result<()>>> {
+        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<PathBuf>,

crates/zed/src/menus.rs 🔗

@@ -58,6 +58,10 @@ pub fn menus() -> Vec<Menu<'static>> {
                     name: "Close Editor",
                     action: Box::new(workspace::CloseActiveItem),
                 },
+                MenuItem::Action {
+                    name: "Close Window",
+                    action: Box::new(workspace::CloseWindow),
+                },
             ],
         },
         Menu {