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
+3 -7
View File
@@ -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 {
+22 -28
View File
@@ -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> {
+1 -1
View File
@@ -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>;
+3 -1
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_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()