5
5
use std:: fmt;
6
6
use std:: sync:: Once ;
7
7
8
- use crate :: id_shim:: Id ;
9
8
use block:: ConcreteBlock ;
10
9
use objc:: declare:: ClassDecl ;
10
+ use objc:: rc:: { Id , Owned } ;
11
11
use objc:: runtime:: { Class , Object , Sel } ;
12
- use objc:: { class, msg_send, sel} ;
12
+ use objc:: { class, msg_send, msg_send_id , sel} ;
13
13
14
14
use crate :: events:: EventModifierFlag ;
15
15
use crate :: foundation:: { id, nil, NSString , NSUInteger } ;
@@ -40,7 +40,7 @@ fn make_menu_item<S: AsRef<str>>(
40
40
key : Option < & str > ,
41
41
action : Option < Sel > ,
42
42
modifiers : Option < & [ EventModifierFlag ] >
43
- ) -> Id < Object > {
43
+ ) -> Id < Object , Owned > {
44
44
unsafe {
45
45
let title = NSString :: new ( title. as_ref ( ) ) ;
46
46
@@ -53,14 +53,22 @@ fn make_menu_item<S: AsRef<str>>(
53
53
// Stock menu items that use selectors targeted at system pieces are just standard
54
54
// `NSMenuItem`s. If there's no custom ones, we use our subclass that has a slot to store a
55
55
// handler pointer.
56
- let alloc: id = msg_send ! [ register_menu_item_class( ) , alloc] ;
57
- let item = Id :: from_retained_ptr ( match action {
58
- Some ( a) => msg_send ! [ alloc, initWithTitle: & * title action: a keyEquivalent: & * key] ,
59
-
60
- None => msg_send ! [ alloc, initWithTitle: & * title
61
- action: sel!( fireBlockAction: )
62
- keyEquivalent: & * key]
63
- } ) ;
56
+ let alloc = msg_send_id ! [ register_menu_item_class( ) , alloc] ;
57
+ let item = match action {
58
+ Some ( a) => msg_send_id ! [
59
+ alloc,
60
+ initWithTitle: & * title,
61
+ action: a,
62
+ keyEquivalent: & * key,
63
+ ] ,
64
+ None => msg_send_id ! [
65
+ alloc,
66
+ initWithTitle: & * title,
67
+ action: sel!( fireBlockAction: ) ,
68
+ keyEquivalent: & * key,
69
+ ]
70
+ }
71
+ . unwrap ( ) ;
64
72
65
73
if let Some ( modifiers) = modifiers {
66
74
let mut key_mask: NSUInteger = 0 ;
@@ -86,7 +94,7 @@ pub enum MenuItem {
86
94
/// You can (and should) create this variant via the `new(title)` method, but if you need to do
87
95
/// something crazier, then wrap it in this and you can hook into the Cacao menu system
88
96
/// accordingly.
89
- Custom ( Id < Object > ) ,
97
+ Custom ( Id < Object , Owned > ) ,
90
98
91
99
/// Shows a standard "About" item, which will bring up the necessary window when clicked
92
100
/// (include a `credits.html` in your App to make use of here). The argument baked in here
@@ -153,7 +161,7 @@ pub enum MenuItem {
153
161
impl MenuItem {
154
162
/// Consumes and returns a handle for the underlying MenuItem. This is internal as we make a few assumptions
155
163
/// for how it interacts with our `Menu` setup, but this could be made public in the future.
156
- pub ( crate ) unsafe fn to_objc ( self ) -> Id < Object > {
164
+ pub ( crate ) unsafe fn to_objc ( self ) -> Id < Object , Owned > {
157
165
match self {
158
166
Self :: Custom ( objc) => objc,
159
167
@@ -210,8 +218,7 @@ impl MenuItem {
210
218
211
219
Self :: Separator => {
212
220
let cls = class ! ( NSMenuItem ) ;
213
- let separator: id = msg_send ! [ cls, separatorItem] ;
214
- Id :: from_ptr ( separator)
221
+ msg_send_id ! [ cls, separatorItem] . unwrap ( )
215
222
}
216
223
}
217
224
}
@@ -313,8 +320,8 @@ extern "C" fn fire_block_action(this: &Object, _: Sel, _item: id) {
313
320
///
314
321
/// In general, we do not want to do more than we need to here - menus are one of the last areas
315
322
/// where Carbon still lurks, and subclassing things can get weird.
316
- pub ( crate ) fn register_menu_item_class ( ) -> * const Class {
317
- static mut APP_CLASS : * const Class = 0 as * const Class ;
323
+ pub ( crate ) fn register_menu_item_class ( ) -> & ' static Class {
324
+ static mut APP_CLASS : Option < & ' static Class > = None ;
318
325
static INIT : Once = Once :: new ( ) ;
319
326
320
327
INIT . call_once ( || unsafe {
@@ -325,8 +332,8 @@ pub(crate) fn register_menu_item_class() -> *const Class {
325
332
decl. add_method ( sel ! ( dealloc) , dealloc_cacao_menuitem as extern "C" fn ( _, _) ) ;
326
333
decl. add_method ( sel ! ( fireBlockAction: ) , fire_block_action as extern "C" fn ( _, _, _) ) ;
327
334
328
- APP_CLASS = decl. register ( ) ;
335
+ APP_CLASS = Some ( decl. register ( ) ) ;
329
336
} ) ;
330
337
331
- unsafe { APP_CLASS }
338
+ unsafe { APP_CLASS . unwrap ( ) }
332
339
}
0 commit comments