Pass the on_finish_launching callback to Platform::run

This commit is contained in:
Nathan Sobo 2021-04-09 21:33:17 -06:00
parent 079050541f
commit 448dace281
7 changed files with 51 additions and 67 deletions

View file

@ -10,12 +10,10 @@ use simplelog::SimpleLogger;
fn main() { fn main() {
SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger"); SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
let app = gpui::App::new(()).unwrap(); gpui::App::new(()).unwrap().run(|ctx| {
app.on_finish_launching(|app| { ctx.platform().activate(true);
app.platform().activate(true); ctx.add_window(|_| TextView);
app.add_window(|_| TextView); });
})
.run();
} }
struct TextView; struct TextView;

View file

@ -92,8 +92,8 @@ impl App {
let platform = platform::test::platform(); let platform = platform::test::platform();
let foreground = Rc::new(executor::Foreground::test()); let foreground = Rc::new(executor::Foreground::test());
let ctx = Rc::new(RefCell::new(MutableAppContext::new( let ctx = Rc::new(RefCell::new(MutableAppContext::new(
foreground.clone(), foreground,
Arc::new(platform), Rc::new(platform),
asset_source, asset_source,
))); )));
ctx.borrow_mut().weak_self = Some(Rc::downgrade(&ctx)); ctx.borrow_mut().weak_self = Some(Rc::downgrade(&ctx));
@ -110,7 +110,7 @@ impl App {
let foreground = Rc::new(executor::Foreground::test()); let foreground = Rc::new(executor::Foreground::test());
let ctx = Rc::new(RefCell::new(MutableAppContext::new( let ctx = Rc::new(RefCell::new(MutableAppContext::new(
foreground.clone(), foreground.clone(),
Arc::new(platform), Rc::new(platform),
asset_source, asset_source,
))); )));
let mut ctx_ref = ctx.borrow_mut(); let mut ctx_ref = ctx.borrow_mut();
@ -200,24 +200,19 @@ impl App {
self 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]) { pub fn set_menus(&self, menus: &[Menu]) {
self.0.borrow().platform.set_menus(menus); self.0.borrow().platform.set_menus(menus);
} }
pub fn run(self) { pub fn run<F>(self, on_finish_launching: F)
platform::current::run(); 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)>( pub fn on_window_invalidated<F: 'static + FnMut(WindowInvalidation, &mut MutableAppContext)>(
@ -354,7 +349,7 @@ impl App {
self.0.borrow().font_cache.clone() 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() self.0.borrow().platform.clone()
} }
} }
@ -394,7 +389,7 @@ type GlobalActionCallback = dyn FnMut(&dyn Any, &mut MutableAppContext);
pub struct MutableAppContext { pub struct MutableAppContext {
weak_self: Option<rc::Weak<RefCell<Self>>>, weak_self: Option<rc::Weak<RefCell<Self>>>,
platform: Arc<dyn platform::Platform>, platform: Rc<dyn platform::Platform>,
font_cache: Arc<FontCache>, font_cache: Arc<FontCache>,
assets: Arc<AssetCache>, assets: Arc<AssetCache>,
ctx: AppContext, ctx: AppContext,
@ -422,7 +417,7 @@ pub struct MutableAppContext {
impl MutableAppContext { impl MutableAppContext {
pub fn new( pub fn new(
foreground: Rc<executor::Foreground>, foreground: Rc<executor::Foreground>,
platform: Arc<dyn platform::Platform>, platform: Rc<dyn platform::Platform>,
asset_source: impl AssetSource, asset_source: impl AssetSource,
) -> Self { ) -> Self {
let fonts = platform.fonts(); let fonts = platform.fonts();
@ -466,7 +461,7 @@ impl MutableAppContext {
&self.ctx &self.ctx
} }
pub fn platform(&self) -> Arc<dyn platform::Platform> { pub fn platform(&self) -> Rc<dyn platform::Platform> {
self.platform.clone() self.platform.clone()
} }

View file

@ -12,15 +12,11 @@ use cocoa::base::{BOOL, NO, YES};
pub use dispatcher::Dispatcher; pub use dispatcher::Dispatcher;
pub use fonts::FontSystem; pub use fonts::FontSystem;
use platform::MacPlatform; use platform::MacPlatform;
use std::sync::Arc; use std::rc::Rc;
use window::Window; use window::Window;
pub fn platform() -> Arc<dyn super::Platform> { pub fn platform() -> Rc<dyn super::Platform> {
MacPlatform::new() Rc::new(MacPlatform::new())
}
pub fn run() {
MacPlatform::run();
} }
trait BoolExt { trait BoolExt {

View file

@ -29,7 +29,7 @@ use std::{
sync::Arc, 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_CLASS: *const Class = ptr::null();
static mut APP_DELEGATE_CLASS: *const Class = ptr::null(); static mut APP_DELEGATE_CLASS: *const Class = ptr::null();
@ -90,35 +90,12 @@ struct Callbacks {
} }
impl MacPlatform { impl MacPlatform {
pub fn new() -> Arc<dyn platform::Platform> { pub fn new() -> Self {
let result = Arc::new(Self { Self {
dispatcher: Arc::new(Dispatcher), dispatcher: Arc::new(Dispatcher),
fonts: Arc::new(FontSystem::new()), fonts: Arc::new(FontSystem::new()),
callbacks: Default::default(), callbacks: Default::default(),
menu_item_actions: 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); self.callbacks.borrow_mut().open_files = Some(callback);
} }
fn on_finish_launching(&self, callback: Box<dyn FnOnce() -> ()>) { fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>) {
self.callbacks.borrow_mut().finish_launching = Some(callback); 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> { fn dispatcher(&self) -> Arc<dyn platform::Dispatcher> {

View file

@ -28,7 +28,7 @@ pub trait Platform {
fn on_resign_active(&self, callback: Box<dyn FnMut()>); fn on_resign_active(&self, callback: Box<dyn FnMut()>);
fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>); fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
fn on_open_files(&self, callback: Box<dyn FnMut(Vec<PathBuf>)>); 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 dispatcher(&self) -> Arc<dyn Dispatcher>;
fn fonts(&self) -> Arc<dyn FontSystem>; fn fonts(&self) -> Arc<dyn FontSystem>;

View file

@ -39,7 +39,9 @@ impl super::Platform for Platform {
fn on_open_files(&self, _: Box<dyn FnMut(Vec<std::path::PathBuf>)>) {} 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> { fn dispatcher(&self) -> Arc<dyn super::Dispatcher> {
self.dispatcher.clone() self.dispatcher.clone()

View file

@ -35,7 +35,7 @@ fn main() {
_ => ctx.dispatch_global_action(command, ()), _ => ctx.dispatch_global_action(command, ()),
} }
}) })
.on_finish_launching(move |ctx| { .run(move |ctx| {
workspace::init(ctx); workspace::init(ctx);
editor::init(ctx); editor::init(ctx);
file_finder::init(ctx); file_finder::init(ctx);
@ -54,8 +54,7 @@ fn main() {
}, },
); );
} }
}) });
.run();
} }
fn init_logger() { fn init_logger() {