Open a new workspace on File > New if none exists

Nathan Sobo created

Change summary

zed/src/menus.rs     |  2 +-
zed/src/workspace.rs | 16 +++++++++++++++-
2 files changed, 16 insertions(+), 2 deletions(-)

Detailed changes

zed/src/menus.rs 🔗

@@ -29,7 +29,7 @@ pub fn menus(state: AppState) -> Vec<Menu<'static>> {
                     name: "New",
                     keystroke: Some("cmd-n"),
                     action: "workspace:new_file",
-                    arg: None,
+                    arg: Some(Box::new(state.clone())),
                 },
                 MenuItem::Separator,
                 MenuItem::Action {

zed/src/workspace.rs 🔗

@@ -29,6 +29,7 @@ use std::{
 pub fn init(cx: &mut MutableAppContext) {
     cx.add_global_action("workspace:open", open);
     cx.add_global_action("workspace:open_paths", open_paths);
+    cx.add_global_action("workspace:new_file", open_new);
     cx.add_global_action("app:quit", quit);
     cx.add_action("workspace:save", Workspace::save_active_item);
     cx.add_action("workspace:debug_elements", Workspace::debug_elements);
@@ -98,6 +99,19 @@ fn open_paths(params: &OpenParams, cx: &mut MutableAppContext) {
     });
 }
 
+fn open_new(app_state: &AppState, cx: &mut MutableAppContext) {
+    cx.add_window(|cx| {
+        let mut view = Workspace::new(
+            0,
+            app_state.settings.clone(),
+            app_state.language_registry.clone(),
+            cx,
+        );
+        view.open_new_file(&app_state, cx);
+        view
+    });
+}
+
 fn quit(_: &(), cx: &mut MutableAppContext) {
     cx.platform().quit();
 }
@@ -449,7 +463,7 @@ impl Workspace {
         }
     }
 
-    pub fn open_new_file(&mut self, _: &(), cx: &mut ViewContext<Self>) {
+    pub fn open_new_file(&mut self, _: &AppState, cx: &mut ViewContext<Self>) {
         let buffer = cx.add_model(|cx| Buffer::new(self.replica_id, "", cx));
         let buffer_view =
             cx.add_view(|cx| Editor::for_buffer(buffer.clone(), self.settings.clone(), cx));