1
1
#![ deny( unsafe_op_in_unsafe_fn) ]
2
- #![ cfg_attr( not( target_os = "macos" ) , allow( unused) ) ]
3
2
use std:: ptr:: NonNull ;
4
3
5
- use icrate:: Foundation :: { ns_string, NSCopying , NSObject , NSString } ;
4
+ use icrate:: AppKit :: { NSApplication , NSApplicationActivationPolicyRegular , NSApplicationDelegate } ;
5
+ use icrate:: Foundation :: {
6
+ ns_string, NSCopying , NSNotification , NSObject , NSObjectProtocol , NSString ,
7
+ } ;
6
8
use objc2:: declare:: { Ivar , IvarBool , IvarDrop , IvarEncode } ;
7
9
use objc2:: rc:: Id ;
8
- use objc2:: runtime:: AnyObject ;
10
+ use objc2:: runtime:: ProtocolObject ;
9
11
use objc2:: { declare_class, msg_send, msg_send_id, mutability, ClassType } ;
10
12
11
- #[ cfg( target_os = "macos" ) ]
12
- #[ link( name = "AppKit" , kind = "framework" ) ]
13
- extern "C" { }
14
-
15
- #[ cfg( target_os = "macos" ) ]
16
13
declare_class ! (
17
14
#[ derive( Debug ) ]
18
- struct CustomAppDelegate {
19
- pub ivar: IvarEncode <u8 , "_ivar" >,
15
+ struct AppDelegate {
16
+ ivar: IvarEncode <u8 , "_ivar" >,
20
17
another_ivar: IvarBool <"_another_ivar" >,
21
18
box_ivar: IvarDrop <Box <i32 >, "_box_ivar" >,
22
19
maybe_box_ivar: IvarDrop <Option <Box <i32 >>, "_maybe_box_ivar" >,
@@ -26,14 +23,14 @@ declare_class!(
26
23
27
24
mod ivars;
28
25
29
- unsafe impl ClassType for CustomAppDelegate {
26
+ unsafe impl ClassType for AppDelegate {
30
27
#[ inherits( NSObject ) ]
31
28
type Super = icrate:: AppKit :: NSResponder ;
32
29
type Mutability = mutability:: InteriorMutable ;
33
- const NAME : & ' static str = "MyCustomAppDelegate " ;
30
+ const NAME : & ' static str = "MyAppDelegate " ;
34
31
}
35
32
36
- unsafe impl CustomAppDelegate {
33
+ unsafe impl AppDelegate {
37
34
#[ method( initWith: another: ) ]
38
35
unsafe fn init_with(
39
36
this: * mut Self ,
@@ -42,70 +39,56 @@ declare_class!(
42
39
) -> Option <NonNull <Self >> {
43
40
let this: Option <& mut Self > = unsafe { msg_send![ super ( this) , init] } ;
44
41
45
- // TODO: `ns_string` can't be used inside closures; investigate!
46
- let s = ns_string!( "def" ) ;
47
-
48
42
this. map( |this| {
49
43
Ivar :: write( & mut this. ivar, ivar) ;
50
- * this. another_ivar = another_ivar;
51
- * this. maybe_box_ivar = None ;
52
- * this. maybe_id_ivar = Some ( s . copy( ) ) ;
44
+ Ivar :: write ( & mut this. another_ivar, another_ivar) ;
45
+ Ivar :: write ( & mut this. maybe_box_ivar, None ) ;
46
+ Ivar :: write ( & mut this. maybe_id_ivar, Some ( ns_string! ( "def" ) . copy( ) ) ) ;
53
47
Ivar :: write( & mut this. box_ivar, Box :: new( 2 ) ) ;
54
48
Ivar :: write( & mut this. id_ivar, NSString :: from_str( "abc" ) ) ;
55
49
NonNull :: from( this)
56
50
} )
57
51
}
58
-
59
- #[ method( myClassMethod) ]
60
- fn my_class_method( ) {
61
- println!( "A class method!" ) ;
62
- }
63
52
}
64
53
65
- // For some reason, `NSApplicationDelegate` is not a "real" protocol we
66
- // can retrieve using `objc_getProtocol` - it seems it is created by
67
- // `clang` only when used in Objective-C...
68
- //
69
- // TODO: Investigate this!
70
- unsafe impl CustomAppDelegate {
71
- /// This is `unsafe` because it expects `sender` to be valid
54
+ unsafe impl NSApplicationDelegate for AppDelegate {
72
55
#[ method( applicationDidFinishLaunching: ) ]
73
- unsafe fn did_finish_launching( & self , sender : * mut AnyObject ) {
56
+ fn did_finish_launching( & self , notification : & NSNotification ) {
74
57
println!( "Did finish launching!" ) ;
75
- // Do something with `sender`
76
- dbg!( sender ) ;
58
+ // Do something with the notification
59
+ dbg!( notification ) ;
77
60
}
78
61
79
- /// Some comment before `sel`.
80
62
#[ method( applicationWillTerminate: ) ]
81
- /// Some comment after `sel`.
82
- fn will_terminate( & self , _: * mut AnyObject ) {
63
+ fn will_terminate( & self , _notification: & NSNotification ) {
83
64
println!( "Will terminate!" ) ;
84
65
}
85
66
}
86
67
) ;
87
68
88
- #[ cfg( target_os = "macos" ) ]
89
- impl CustomAppDelegate {
69
+ unsafe impl NSObjectProtocol for AppDelegate { }
70
+
71
+ impl AppDelegate {
90
72
pub fn new ( ivar : u8 , another_ivar : bool ) -> Id < Self > {
91
73
unsafe { msg_send_id ! [ Self :: alloc( ) , initWith: ivar, another: another_ivar] }
92
74
}
93
75
}
94
76
95
- #[ cfg( target_os = "macos" ) ]
96
77
fn main ( ) {
97
- let delegate = CustomAppDelegate :: new ( 42 , true ) ;
78
+ let app = unsafe { NSApplication :: sharedApplication ( ) } ;
79
+ unsafe { app. setActivationPolicy ( NSApplicationActivationPolicyRegular ) } ;
80
+
81
+ // initialize the delegate
82
+ let delegate = AppDelegate :: new ( 42 , true ) ;
98
83
99
84
println ! ( "{delegate:?}" ) ;
100
- println ! ( "{:?}" , delegate. ivar) ;
101
- println ! ( "{:?}" , delegate. another_ivar) ;
102
- println ! ( "{:?}" , delegate. box_ivar) ;
103
- println ! ( "{:?}" , delegate. maybe_box_ivar) ;
104
- println ! ( "{:?}" , delegate. id_ivar) ;
105
- println ! ( "{:?}" , delegate. maybe_id_ivar) ;
106
- }
107
85
108
- #[ cfg( not( target_os = "macos" ) ) ]
109
- fn main ( ) {
110
- panic ! ( "This example uses AppKit, which is only present on macOS" ) ;
86
+ // configure the application delegate
87
+ unsafe {
88
+ let object = ProtocolObject :: from_ref ( & * delegate) ;
89
+ app. setDelegate ( Some ( object) ) ;
90
+ } ;
91
+
92
+ // run the app
93
+ unsafe { app. run ( ) } ;
111
94
}
0 commit comments