2023-12-23 23:07:55 +01:00
|
|
|
use icrate::AppKit::{NSApplicationActivationPolicy, NSApplicationDelegate};
|
|
|
|
|
use icrate::Foundation::{MainThreadMarker, NSObject, NSObjectProtocol};
|
2023-07-29 00:33:03 +02:00
|
|
|
use objc2::rc::Id;
|
2023-08-02 16:30:41 +02:00
|
|
|
use objc2::runtime::AnyObject;
|
2023-12-23 18:04:24 +01:00
|
|
|
use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass};
|
2021-04-30 11:31:28 +02:00
|
|
|
|
2022-09-02 18:46:18 +02:00
|
|
|
use super::app_state::AppState;
|
2022-06-08 11:50:26 -07:00
|
|
|
|
2023-12-23 18:04:24 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub(super) struct State {
|
|
|
|
|
activation_policy: NSApplicationActivationPolicy,
|
|
|
|
|
default_menu: bool,
|
|
|
|
|
activate_ignoring_other_apps: bool,
|
|
|
|
|
}
|
2022-06-09 10:08:52 -04:00
|
|
|
|
2023-12-23 18:04:24 +01:00
|
|
|
declare_class!(
|
|
|
|
|
pub(super) struct ApplicationDelegate;
|
2023-07-29 00:33:03 +02:00
|
|
|
|
2022-09-02 18:46:18 +02:00
|
|
|
unsafe impl ClassType for ApplicationDelegate {
|
|
|
|
|
type Super = NSObject;
|
2023-12-23 23:07:55 +01:00
|
|
|
type Mutability = mutability::MainThreadOnly;
|
2022-09-02 18:46:18 +02:00
|
|
|
const NAME: &'static str = "WinitApplicationDelegate";
|
|
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2023-12-23 18:04:24 +01:00
|
|
|
impl DeclaredClass for ApplicationDelegate {
|
|
|
|
|
type Ivars = State;
|
|
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2023-12-23 23:07:55 +01:00
|
|
|
unsafe impl NSObjectProtocol for ApplicationDelegate {}
|
|
|
|
|
|
|
|
|
|
unsafe impl NSApplicationDelegate for ApplicationDelegate {
|
2023-07-29 00:33:03 +02:00
|
|
|
#[method(applicationDidFinishLaunching:)]
|
2023-08-02 16:30:41 +02:00
|
|
|
fn did_finish_launching(&self, _sender: Option<&AnyObject>) {
|
2022-09-02 18:46:18 +02:00
|
|
|
trace_scope!("applicationDidFinishLaunching:");
|
2022-11-23 14:42:46 +02:00
|
|
|
AppState::launched(
|
2023-12-23 18:04:24 +01:00
|
|
|
self.ivars().activation_policy,
|
|
|
|
|
self.ivars().default_menu,
|
|
|
|
|
self.ivars().activate_ignoring_other_apps,
|
2022-11-23 14:42:46 +02:00
|
|
|
);
|
2022-09-02 18:46:18 +02:00
|
|
|
}
|
2021-04-30 11:31:28 +02:00
|
|
|
|
2023-07-29 00:33:03 +02:00
|
|
|
#[method(applicationWillTerminate:)]
|
2023-08-02 16:30:41 +02:00
|
|
|
fn will_terminate(&self, _sender: Option<&AnyObject>) {
|
2022-09-02 18:46:18 +02:00
|
|
|
trace_scope!("applicationWillTerminate:");
|
|
|
|
|
// TODO: Notify every window that it will be destroyed, like done in iOS?
|
2023-09-07 08:25:04 +02:00
|
|
|
AppState::internal_exit();
|
2022-09-02 18:46:18 +02:00
|
|
|
}
|
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(
|
2023-12-23 23:07:55 +01:00
|
|
|
mtm: MainThreadMarker,
|
2022-09-02 18:46:18 +02:00
|
|
|
activation_policy: NSApplicationActivationPolicy,
|
|
|
|
|
default_menu: bool,
|
2022-11-23 14:42:46 +02:00
|
|
|
activate_ignoring_other_apps: bool,
|
2023-07-29 00:33:03 +02:00
|
|
|
) -> Id<Self> {
|
2023-12-23 23:07:55 +01:00
|
|
|
let this = mtm.alloc().set_ivars(State {
|
2023-12-23 18:04:24 +01:00
|
|
|
activation_policy,
|
|
|
|
|
default_menu,
|
|
|
|
|
activate_ignoring_other_apps,
|
|
|
|
|
});
|
|
|
|
|
unsafe { msg_send_id![super(this), init] }
|
2021-04-30 11:31:28 +02:00
|
|
|
}
|
|
|
|
|
}
|