Detailed changes
@@ -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;
@@ -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<F>(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<F>(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<F: 'static + FnMut(WindowInvalidation, &mut MutableAppContext)>(
@@ -354,7 +349,7 @@ impl App {
self.0.borrow().font_cache.clone()
}
- pub fn platform(&self) -> Arc<dyn platform::Platform> {
+ pub fn platform(&self) -> Rc<dyn platform::Platform> {
self.0.borrow().platform.clone()
}
}
@@ -394,7 +389,7 @@ type GlobalActionCallback = dyn FnMut(&dyn Any, &mut MutableAppContext);
pub struct MutableAppContext {
weak_self: Option<rc::Weak<RefCell<Self>>>,
- platform: Arc<dyn platform::Platform>,
+ platform: Rc<dyn platform::Platform>,
font_cache: Arc<FontCache>,
assets: Arc<AssetCache>,
ctx: AppContext,
@@ -422,7 +417,7 @@ pub struct MutableAppContext {
impl MutableAppContext {
pub fn new(
foreground: Rc<executor::Foreground>,
- platform: Arc<dyn platform::Platform>,
+ platform: Rc<dyn platform::Platform>,
asset_source: impl AssetSource,
) -> Self {
let fonts = platform.fonts();
@@ -466,7 +461,7 @@ impl MutableAppContext {
&self.ctx
}
- pub fn platform(&self) -> Arc<dyn platform::Platform> {
+ pub fn platform(&self) -> Rc<dyn platform::Platform> {
self.platform.clone()
}
@@ -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<dyn super::Platform> {
- MacPlatform::new()
-}
-
-pub fn run() {
- MacPlatform::run();
+pub fn platform() -> Rc<dyn super::Platform> {
+ Rc::new(MacPlatform::new())
}
trait BoolExt {
@@ -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<dyn platform::Platform> {
- 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::<c_void>());
- (*app.delegate()).set_ivar(MAC_PLATFORM_IVAR, null_mut::<c_void>());
}
}
@@ -220,8 +197,25 @@ impl platform::Platform for MacPlatform {
self.callbacks.borrow_mut().open_files = Some(callback);
}
- fn on_finish_launching(&self, callback: Box<dyn FnOnce() -> ()>) {
- self.callbacks.borrow_mut().finish_launching = Some(callback);
+ fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>) {
+ 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::<c_void>());
+ (*app.delegate()).set_ivar(MAC_PLATFORM_IVAR, null_mut::<c_void>());
+ }
}
fn dispatcher(&self) -> Arc<dyn platform::Dispatcher> {
@@ -28,7 +28,7 @@ pub trait Platform {
fn on_resign_active(&self, callback: Box<dyn FnMut()>);
fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
fn on_open_files(&self, callback: Box<dyn FnMut(Vec<PathBuf>)>);
- fn on_finish_launching(&self, callback: Box<dyn FnOnce() -> ()>);
+ fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>);
fn dispatcher(&self) -> Arc<dyn Dispatcher>;
fn fonts(&self) -> Arc<dyn FontSystem>;
@@ -39,7 +39,9 @@ impl super::Platform for Platform {
fn on_open_files(&self, _: Box<dyn FnMut(Vec<std::path::PathBuf>)>) {}
- fn on_finish_launching(&self, _: Box<dyn FnOnce() -> ()>) {}
+ fn run(&self, _on_finish_launching: Box<dyn FnOnce() -> ()>) {
+ unimplemented!()
+ }
fn dispatcher(&self) -> Arc<dyn super::Dispatcher> {
self.dispatcher.clone()
@@ -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() {