-
Notifications
You must be signed in to change notification settings - Fork 35
/
delegate.rs
83 lines (71 loc) · 2.55 KB
/
delegate.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#![deny(unsafe_op_in_unsafe_fn)]
use objc2::rc::Retained;
use objc2::runtime::ProtocolObject;
use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass, MainThreadMarker};
use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy, NSApplicationDelegate};
use objc2_foundation::{
ns_string, NSCopying, NSNotification, NSObject, NSObjectProtocol, NSString,
};
#[derive(Debug)]
#[allow(unused)]
struct Ivars {
ivar: u8,
another_ivar: bool,
box_ivar: Box<i32>,
maybe_box_ivar: Option<Box<i32>>,
id_ivar: Retained<NSString>,
maybe_id_ivar: Option<Retained<NSString>>,
}
declare_class!(
struct AppDelegate;
// SAFETY:
// - The superclass NSObject does not have any subclassing requirements.
// - Main thread only mutability is correct, since this is an application delegate.
// - `AppDelegate` does not implement `Drop`.
unsafe impl ClassType for AppDelegate {
type Super = NSObject;
type Mutability = mutability::MainThreadOnly;
const NAME: &'static str = "MyAppDelegate";
}
impl DeclaredClass for AppDelegate {
type Ivars = Ivars;
}
unsafe impl NSObjectProtocol for AppDelegate {}
unsafe impl NSApplicationDelegate for AppDelegate {
#[method(applicationDidFinishLaunching:)]
fn did_finish_launching(&self, notification: &NSNotification) {
println!("Did finish launching!");
// Do something with the notification
dbg!(notification);
}
#[method(applicationWillTerminate:)]
fn will_terminate(&self, _notification: &NSNotification) {
println!("Will terminate!");
}
}
);
impl AppDelegate {
fn new(ivar: u8, another_ivar: bool, mtm: MainThreadMarker) -> Retained<Self> {
let this = Self::alloc_main_thread(mtm);
let this = this.set_ivars(Ivars {
ivar,
another_ivar,
box_ivar: Box::new(2),
maybe_box_ivar: None,
id_ivar: NSString::from_str("abc"),
maybe_id_ivar: Some(ns_string!("def").copy()),
});
unsafe { msg_send_id![super(this), init] }
}
}
fn main() {
let mtm: MainThreadMarker = MainThreadMarker::new().unwrap();
let app = NSApplication::sharedApplication(mtm);
app.setActivationPolicy(NSApplicationActivationPolicy::Regular);
// configure the application delegate
let delegate = AppDelegate::new(42, true, mtm);
let object = ProtocolObject::from_ref(&*delegate);
app.setDelegate(Some(object));
// run the app
app.run();
}