Set up menu handler in App::new

Max Brunsfeld and Nathan Sobo created

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

Change summary

gpui/src/app.rs | 25 +++++++++----------------
zed/src/main.rs | 11 ++---------
2 files changed, 11 insertions(+), 25 deletions(-)

Detailed changes

gpui/src/app.rs 🔗

@@ -128,9 +128,16 @@ impl App {
         let foreground = Rc::new(executor::Foreground::platform(platform.dispatcher())?);
         let app = Self(Rc::new(RefCell::new(MutableAppContext::new(
             foreground,
-            platform,
+            platform.clone(),
             asset_source,
         ))));
+
+        let ctx = app.0.clone();
+        platform.on_menu_command(Box::new(move |command, arg| {
+            ctx.borrow_mut()
+                .dispatch_global_action_with_dyn_arg(command, arg.unwrap_or(&()));
+        }));
+
         app.0.borrow_mut().weak_self = Some(Rc::downgrade(&app.0));
         Ok(app)
     }
@@ -170,20 +177,6 @@ impl App {
         self
     }
 
-    pub fn on_menu_command<F>(self, mut callback: F) -> Self
-    where
-        F: 'static + FnMut(&str, Option<&(dyn Any + 'static)>, &mut MutableAppContext),
-    {
-        let ctx = self.0.clone();
-        self.0
-            .borrow()
-            .platform
-            .on_menu_command(Box::new(move |command, arg| {
-                callback(command, arg, &mut *ctx.borrow_mut())
-            }));
-        self
-    }
-
     pub fn on_open_files<F>(self, mut callback: F) -> Self
     where
         F: 'static + FnMut(Vec<PathBuf>, &mut MutableAppContext),
@@ -646,7 +639,7 @@ impl MutableAppContext {
         self.dispatch_global_action_with_dyn_arg(name, Box::new(arg).as_ref());
     }
 
-    pub fn dispatch_global_action_with_dyn_arg(&mut self, name: &str, arg: &dyn Any) {
+    fn dispatch_global_action_with_dyn_arg(&mut self, name: &str, arg: &dyn Any) {
         if let Some((name, mut handlers)) = self.global_actions.remove_entry(name) {
             self.pending_flushes += 1;
             for handler in handlers.iter_mut().rev() {

zed/src/main.rs 🔗

@@ -1,12 +1,9 @@
 use fs::OpenOptions;
-use gpui::PathPromptOptions;
 use log::LevelFilter;
 use simplelog::SimpleLogger;
 use std::{fs, path::PathBuf};
 use zed::{
-    assets, editor, file_finder, menus,
-    settings::{self, Settings},
-    watch::Receiver,
+    assets, editor, file_finder, menus, settings,
     workspace::{self, OpenParams},
 };
 
@@ -16,11 +13,7 @@ fn main() {
     let app = gpui::App::new(assets::Assets).unwrap();
     let (_, settings_rx) = settings::channel(&app.font_cache()).unwrap();
     app.set_menus(menus::menus(settings_rx.clone()));
-    app.on_menu_command(move |command, arg, ctx| {
-        eprintln!("command: {:?} {:?}", command, arg);
-        ctx.dispatch_global_action_with_dyn_arg(command, arg.unwrap_or(&()))
-    })
-    .run(move |ctx| {
+    app.run(move |ctx| {
         workspace::init(ctx);
         editor::init(ctx);
         file_finder::init(ctx);