application.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use crate::application::{ ApplicationDelegate };
use crate::errors::{ Result };
use objc2::{ DefinedClass, MainThreadOnly, define_class, msg_send, sel };
use objc2::rc::{ Retained };
use objc2::runtime::{ ProtocolObject };
use objc2_foundation::{
MainThreadMarker,
NSBundle,
NSNotification,
NSObject,
NSObjectProtocol,
NSProcessInfo,
NSString,
ns_string,
};
use objc2_app_kit::{
NSApplication,
NSApplicationDelegate,
NSApplicationActivationPolicy,
NSEventModifierFlags,
NSMenu,
NSMenuItem,
};
use std::cell::{ RefCell };
struct AppkitApplicationIvars {
delegate: RefCell<Box<dyn ApplicationDelegate>>,
}
impl std::fmt::Debug for AppkitApplicationIvars {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("AppkitApplicationIvars")
.field("delegate", &"dyn ApplicationDelegate { ... }")
.finish()
}
}
impl AppkitApplicationIvars {
fn new<D: ApplicationDelegate + 'static>(delegate: D) -> AppkitApplicationIvars {
AppkitApplicationIvars {
delegate: RefCell::new(Box::new(delegate)),
}
}
}
define_class!{
#[derive(Debug)]
#[unsafe(super = NSObject)]
#[thread_kind = MainThreadOnly]
#[ivars = AppkitApplicationIvars]
struct AppkitApplicationDelegate;
unsafe impl NSObjectProtocol for AppkitApplicationDelegate {}
unsafe impl NSApplicationDelegate for AppkitApplicationDelegate {
#[unsafe(method(applicationDidFinishLaunching:))]
fn application_did_finish_launching(&self, notification: &NSNotification) {
let ns_application =
notification.object()
.expect("applicationDidFinishLaunching notification should have an object")
.downcast::<NSApplication>()
.expect("applicationDidFinishLaunching notification object should be an NSApplication");
ns_application.setActivationPolicy(NSApplicationActivationPolicy::Regular);
ns_application.activate();
let mtm = MainThreadMarker::from(self);
let application_title = match NSBundle::mainBundle().name() {
Some(bundle_name) => bundle_name,
None => NSProcessInfo::processInfo().processName(),
};
let about_item_title = format!("About {}", application_title);
let about_item = unsafe {
NSMenuItem::initWithTitle_action_keyEquivalent(
mtm.alloc(),
&NSString::from_str(&about_item_title),
Some(sel!(orderFrontStandardAboutPanel:)),
ns_string!("")
)
};
let separator_item_1 = NSMenuItem::separatorItem(mtm);
let services_menu = NSMenu::new(mtm);
let services_item = unsafe {
NSMenuItem::initWithTitle_action_keyEquivalent(
mtm.alloc(),
ns_string!("Services"),
None,
ns_string!(""),
)
};
services_item.setSubmenu(Some(&services_menu));
ns_application.setServicesMenu(Some(&services_menu));
let separator_item_2 = NSMenuItem::separatorItem(mtm);
let hide_item_title = format!("Hide {}", application_title);
let hide_item = unsafe {
NSMenuItem::initWithTitle_action_keyEquivalent(
mtm.alloc(),
&NSString::from_str(&hide_item_title),
Some(sel!(hide:)),
ns_string!("h"),
)
};
let hide_others_item_title = ns_string!("Hide Others");
let hide_others_item = unsafe {
NSMenuItem::initWithTitle_action_keyEquivalent(
mtm.alloc(),
hide_others_item_title,
Some(sel!(hideOtherApplications:)),
ns_string!("h"),
)
};
hide_others_item.setKeyEquivalentModifierMask(NSEventModifierFlags::Option | NSEventModifierFlags::Command);
let show_all_item_title = ns_string!("Show All");
let show_all_item = unsafe {
NSMenuItem::initWithTitle_action_keyEquivalent(
mtm.alloc(),
show_all_item_title,
Some(sel!(unhideAllApplications:)),
ns_string!(""),
)
};
let separator_item_3 = NSMenuItem::separatorItem(mtm);
let quit_item_title = format!("Quit {}", application_title);
let quit_item = unsafe {
NSMenuItem::initWithTitle_action_keyEquivalent(
mtm.alloc(),
&NSString::from_str(&quit_item_title),
Some(sel!(terminate:)),
ns_string!("q"),
)
};
let application_menu = NSMenu::new(mtm);
application_menu.addItem(&about_item);
application_menu.addItem(&separator_item_1);
application_menu.addItem(&services_item);
application_menu.addItem(&separator_item_2);
application_menu.addItem(&hide_item);
application_menu.addItem(&hide_others_item);
application_menu.addItem(&show_all_item);
application_menu.addItem(&separator_item_3);
application_menu.addItem(&quit_item);
let application_menu_item = NSMenuItem::new(mtm);
application_menu_item.setSubmenu(Some(&application_menu));
let menu_bar = NSMenu::new(mtm);
menu_bar.addItem(&application_menu_item);
ns_application.setMainMenu(Some(&menu_bar));
self
.ivars()
.delegate
.borrow_mut()
.on_ready();
}
}
}
impl AppkitApplicationDelegate {
fn new<D: ApplicationDelegate + 'static>(mtm: MainThreadMarker, delegate: D) -> Retained<AppkitApplicationDelegate> {
let this = AppkitApplicationDelegate::alloc(mtm)
.set_ivars(AppkitApplicationIvars::new(delegate));
unsafe {
msg_send![super(this), init]
}
}
}
pub fn run<D: ApplicationDelegate + 'static>(delegate: D) -> Result<()> {
let mtm = MainThreadMarker::new()
.expect("Must be called from the main thread");
let appkit_delegate = AppkitApplicationDelegate::new(mtm, delegate);
let ns_application = NSApplication::sharedApplication(mtm);
ns_application.setDelegate(Some(ProtocolObject::from_ref(&*appkit_delegate)));
ns_application.run();
Ok(())
}
pub fn quit() {
let mtm = MainThreadMarker::new()
.expect("Must be called from the main thread");
NSApplication::sharedApplication(mtm)
.terminate(None)
}