diff --git a/gpui/examples/text.rs b/gpui/examples/text.rs index 2dad6e0786..df1f367cdb 100644 --- a/gpui/examples/text.rs +++ b/gpui/examples/text.rs @@ -10,12 +10,10 @@ use simplelog::SimpleLogger; fn main() { SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger"); - let app = gpui::App::new(()).unwrap(); - app.on_finish_launching(|app| { - app.platform().activate(true); - app.add_window(|_| TextView); - }) - .run(); + gpui::App::new(()).unwrap().run(|ctx| { + ctx.platform().activate(true); + ctx.add_window(|_| TextView); + }); } struct TextView; diff --git a/gpui/src/app.rs b/gpui/src/app.rs index ab3f471e73..48720eabcb 100644 --- a/gpui/src/app.rs +++ b/gpui/src/app.rs @@ -92,8 +92,8 @@ impl App { let platform = platform::test::platform(); let foreground = Rc::new(executor::Foreground::test()); let ctx = Rc::new(RefCell::new(MutableAppContext::new( - foreground.clone(), - Arc::new(platform), + foreground, + Rc::new(platform), asset_source, ))); ctx.borrow_mut().weak_self = Some(Rc::downgrade(&ctx)); @@ -110,7 +110,7 @@ impl App { let foreground = Rc::new(executor::Foreground::test()); let ctx = Rc::new(RefCell::new(MutableAppContext::new( foreground.clone(), - Arc::new(platform), + Rc::new(platform), asset_source, ))); let mut ctx_ref = ctx.borrow_mut(); @@ -200,24 +200,19 @@ impl App { self } - pub fn on_finish_launching(self, callback: F) -> Self - where - F: 'static + FnOnce(&mut MutableAppContext), - { - let ctx = self.0.clone(); - self.0 - .borrow() - .platform - .on_finish_launching(Box::new(move || callback(&mut *ctx.borrow_mut()))); - self - } - pub fn set_menus(&self, menus: &[Menu]) { self.0.borrow().platform.set_menus(menus); } - pub fn run(self) { - platform::current::run(); + pub fn run(self, on_finish_launching: F) + where + F: 'static + FnOnce(&mut MutableAppContext), + { + let platform = self.platform(); + platform.run(Box::new(move || { + let mut ctx = self.0.borrow_mut(); + on_finish_launching(&mut *ctx); + })) } pub fn on_window_invalidated( @@ -354,7 +349,7 @@ impl App { self.0.borrow().font_cache.clone() } - pub fn platform(&self) -> Arc { + pub fn platform(&self) -> Rc { self.0.borrow().platform.clone() } } @@ -394,7 +389,7 @@ type GlobalActionCallback = dyn FnMut(&dyn Any, &mut MutableAppContext); pub struct MutableAppContext { weak_self: Option>>, - platform: Arc, + platform: Rc, font_cache: Arc, assets: Arc, ctx: AppContext, @@ -422,7 +417,7 @@ pub struct MutableAppContext { impl MutableAppContext { pub fn new( foreground: Rc, - platform: Arc, + platform: Rc, asset_source: impl AssetSource, ) -> Self { let fonts = platform.fonts(); @@ -466,7 +461,7 @@ impl MutableAppContext { &self.ctx } - pub fn platform(&self) -> Arc { + pub fn platform(&self) -> Rc { self.platform.clone() } diff --git a/gpui/src/platform/mac/mod.rs b/gpui/src/platform/mac/mod.rs index 07ada3650a..c2a88c12a4 100644 --- a/gpui/src/platform/mac/mod.rs +++ b/gpui/src/platform/mac/mod.rs @@ -12,15 +12,11 @@ use cocoa::base::{BOOL, NO, YES}; pub use dispatcher::Dispatcher; pub use fonts::FontSystem; use platform::MacPlatform; -use std::sync::Arc; +use std::rc::Rc; use window::Window; -pub fn platform() -> Arc { - MacPlatform::new() -} - -pub fn run() { - MacPlatform::run(); +pub fn platform() -> Rc { + Rc::new(MacPlatform::new()) } trait BoolExt { diff --git a/gpui/src/platform/mac/platform.rs b/gpui/src/platform/mac/platform.rs index 40bd3566a5..4201100a6d 100644 --- a/gpui/src/platform/mac/platform.rs +++ b/gpui/src/platform/mac/platform.rs @@ -29,7 +29,7 @@ use std::{ sync::Arc, }; -const MAC_PLATFORM_IVAR: &'static str = "runner"; +const MAC_PLATFORM_IVAR: &'static str = "platform"; static mut APP_CLASS: *const Class = ptr::null(); static mut APP_DELEGATE_CLASS: *const Class = ptr::null(); @@ -90,35 +90,12 @@ struct Callbacks { } impl MacPlatform { - pub fn new() -> Arc { - let result = Arc::new(Self { + pub fn new() -> Self { + Self { dispatcher: Arc::new(Dispatcher), fonts: Arc::new(FontSystem::new()), callbacks: Default::default(), menu_item_actions: Default::default(), - }); - - unsafe { - let app: id = msg_send![APP_CLASS, sharedApplication]; - let app_delegate: id = msg_send![APP_DELEGATE_CLASS, new]; - let self_ptr = result.as_ref() as *const Self as *const c_void; - app.setDelegate_(app_delegate); - (*app).set_ivar(MAC_PLATFORM_IVAR, self_ptr); - (*app_delegate).set_ivar(MAC_PLATFORM_IVAR, self_ptr); - } - - result - } - - pub fn run() { - unsafe { - let pool = NSAutoreleasePool::new(nil); - let app: id = msg_send![APP_CLASS, sharedApplication]; - - app.run(); - pool.drain(); - (*app).set_ivar(MAC_PLATFORM_IVAR, null_mut::()); - (*app.delegate()).set_ivar(MAC_PLATFORM_IVAR, null_mut::()); } } @@ -220,8 +197,25 @@ impl platform::Platform for MacPlatform { self.callbacks.borrow_mut().open_files = Some(callback); } - fn on_finish_launching(&self, callback: Box ()>) { - self.callbacks.borrow_mut().finish_launching = Some(callback); + fn run(&self, on_finish_launching: Box ()>) { + self.callbacks.borrow_mut().finish_launching = Some(on_finish_launching); + + unsafe { + let app: id = msg_send![APP_CLASS, sharedApplication]; + let app_delegate: id = msg_send![APP_DELEGATE_CLASS, new]; + app.setDelegate_(app_delegate); + + let self_ptr = self as *const Self as *const c_void; + (*app).set_ivar(MAC_PLATFORM_IVAR, self_ptr); + (*app_delegate).set_ivar(MAC_PLATFORM_IVAR, self_ptr); + + let pool = NSAutoreleasePool::new(nil); + app.run(); + pool.drain(); + + (*app).set_ivar(MAC_PLATFORM_IVAR, null_mut::()); + (*app.delegate()).set_ivar(MAC_PLATFORM_IVAR, null_mut::()); + } } fn dispatcher(&self) -> Arc { diff --git a/gpui/src/platform/mod.rs b/gpui/src/platform/mod.rs index 6e5d53585f..39825c941e 100644 --- a/gpui/src/platform/mod.rs +++ b/gpui/src/platform/mod.rs @@ -28,7 +28,7 @@ pub trait Platform { fn on_resign_active(&self, callback: Box); fn on_event(&self, callback: Box bool>); fn on_open_files(&self, callback: Box)>); - fn on_finish_launching(&self, callback: Box ()>); + fn run(&self, on_finish_launching: Box ()>); fn dispatcher(&self) -> Arc; fn fonts(&self) -> Arc; diff --git a/gpui/src/platform/test.rs b/gpui/src/platform/test.rs index 1cd4399c1b..b0d19232dc 100644 --- a/gpui/src/platform/test.rs +++ b/gpui/src/platform/test.rs @@ -39,7 +39,9 @@ impl super::Platform for Platform { fn on_open_files(&self, _: Box)>) {} - fn on_finish_launching(&self, _: Box ()>) {} + fn run(&self, _on_finish_launching: Box ()>) { + unimplemented!() + } fn dispatcher(&self) -> Arc { self.dispatcher.clone() diff --git a/zed/src/main.rs b/zed/src/main.rs index 6d171a5a29..b214924a80 100644 --- a/zed/src/main.rs +++ b/zed/src/main.rs @@ -35,7 +35,7 @@ fn main() { _ => ctx.dispatch_global_action(command, ()), } }) - .on_finish_launching(move |ctx| { + .run(move |ctx| { workspace::init(ctx); editor::init(ctx); file_finder::init(ctx); @@ -54,8 +54,7 @@ fn main() { }, ); } - }) - .run(); + }); } fn init_logger() {