From 41358f34e3ed7c7080bee1f1aa273e79c982201b Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 12 Apr 2021 14:25:00 -0700 Subject: [PATCH] Set up menu handler in App::new Co-Authored-By: Nathan Sobo --- gpui/src/app.rs | 25 +++++++++---------------- zed/src/main.rs | 11 ++--------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/gpui/src/app.rs b/gpui/src/app.rs index 3c84da64dd..97030a48e3 100644 --- a/gpui/src/app.rs +++ b/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(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(self, mut callback: F) -> Self where F: 'static + FnMut(Vec, &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() { diff --git a/zed/src/main.rs b/zed/src/main.rs index b5fc625eec..7edb0eecd5 100644 --- a/zed/src/main.rs +++ b/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);