2022-09-02 18:46:18 +02:00
|
|
|
use objc2::foundation::NSObject;
|
|
|
|
|
use objc2::rc::{Id, Shared};
|
|
|
|
|
use objc2::runtime::Object;
|
2022-09-08 16:45:29 +02:00
|
|
|
use objc2::{declare_class, msg_send, msg_send_id, ClassType};
|
2021-04-30 11:31:28 +02:00
|
|
|
|
2022-09-02 18:46:18 +02:00
|
|
|
use super::app_state::AppState;
|
2022-09-08 16:45:29 +02:00
|
|
|
use super::appkit::NSApplicationActivationPolicy;
|
2022-06-08 11:50:26 -07:00
|
|
|
|
2022-09-02 18:46:18 +02:00
|
|
|
declare_class!(
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub(super) struct ApplicationDelegate {
|
|
|
|
|
activation_policy: NSApplicationActivationPolicy,
|
|
|
|
|
default_menu: bool,
|
2022-11-23 14:42:46 +02:00
|
|
|
activate_ignoring_other_apps: bool,
|
2022-09-02 18:46:18 +02:00
|
|
|
}
|
2022-06-09 10:08:52 -04:00
|
|
|
|
2022-09-02 18:46:18 +02:00
|
|
|
unsafe impl ClassType for ApplicationDelegate {
|
|
|
|
|
type Super = NSObject;
|
|
|
|
|
const NAME: &'static str = "WinitApplicationDelegate";
|
|
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2022-09-02 18:46:18 +02:00
|
|
|
unsafe impl ApplicationDelegate {
|
|
|
|
|
#[sel(initWithActivationPolicy:defaultMenu:)]
|
|
|
|
|
fn init(
|
|
|
|
|
&mut self,
|
|
|
|
|
activation_policy: NSApplicationActivationPolicy,
|
|
|
|
|
default_menu: bool,
|
|
|
|
|
) -> Option<&mut Self> {
|
|
|
|
|
let this: Option<&mut Self> = unsafe { msg_send![super(self), init] };
|
|
|
|
|
this.map(|this| {
|
|
|
|
|
*this.activation_policy = activation_policy;
|
|
|
|
|
*this.default_menu = default_menu;
|
|
|
|
|
this
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2022-09-02 18:46:18 +02:00
|
|
|
#[sel(applicationDidFinishLaunching:)]
|
|
|
|
|
fn did_finish_launching(&self, _sender: *const Object) {
|
|
|
|
|
trace_scope!("applicationDidFinishLaunching:");
|
2022-11-23 14:42:46 +02:00
|
|
|
AppState::launched(
|
|
|
|
|
*self.activation_policy,
|
|
|
|
|
*self.default_menu,
|
|
|
|
|
*self.activate_ignoring_other_apps,
|
|
|
|
|
);
|
2022-09-02 18:46:18 +02:00
|
|
|
}
|
2021-04-30 11:31:28 +02:00
|
|
|
|
2022-09-02 18:46:18 +02:00
|
|
|
#[sel(applicationWillTerminate:)]
|
|
|
|
|
fn will_terminate(&self, _sender: *const Object) {
|
|
|
|
|
trace_scope!("applicationWillTerminate:");
|
|
|
|
|
// TODO: Notify every window that it will be destroyed, like done in iOS?
|
|
|
|
|
AppState::exit();
|
|
|
|
|
}
|
2021-04-30 11:31:28 +02:00
|
|
|
}
|
2022-09-02 18:46:18 +02:00
|
|
|
);
|
2021-04-30 11:31:28 +02:00
|
|
|
|
2022-09-02 18:46:18 +02:00
|
|
|
impl ApplicationDelegate {
|
|
|
|
|
pub(super) fn new(
|
|
|
|
|
activation_policy: NSApplicationActivationPolicy,
|
|
|
|
|
default_menu: bool,
|
2022-11-23 14:42:46 +02:00
|
|
|
activate_ignoring_other_apps: bool,
|
2022-09-02 18:46:18 +02:00
|
|
|
) -> Id<Self, Shared> {
|
|
|
|
|
unsafe {
|
|
|
|
|
msg_send_id![
|
|
|
|
|
msg_send_id![Self::class(), alloc],
|
|
|
|
|
initWithActivationPolicy: activation_policy,
|
|
|
|
|
defaultMenu: default_menu,
|
2022-11-23 14:42:46 +02:00
|
|
|
activateIgnoringOtherApps: activate_ignoring_other_apps,
|
2022-09-02 18:46:18 +02:00
|
|
|
]
|
|
|
|
|
}
|
2021-04-30 11:31:28 +02:00
|
|
|
}
|
|
|
|
|
}
|