@@ -12,15 +12,15 @@ use icrate::{
12
12
NSWindowStyleMaskResizable , NSWindowStyleMaskTitled ,
13
13
} ,
14
14
Foundation :: {
15
- ns_string, NSNotification , NSObject , NSObjectProtocol , NSPoint , NSRect , NSSize ,
16
- NSURLRequest , NSURL ,
15
+ ns_string, MainThreadMarker , NSNotification , NSObject , NSObjectProtocol , NSPoint , NSRect ,
16
+ NSSize , NSURLRequest , NSURL ,
17
17
} ,
18
18
WebKit :: { WKNavigation , WKNavigationDelegate , WKWebView } ,
19
19
} ;
20
20
use objc2:: {
21
21
declare:: { Ivar , IvarDrop } ,
22
22
declare_class, msg_send, msg_send_id,
23
- mutability:: InteriorMutable ,
23
+ mutability:: MainThreadOnly ,
24
24
rc:: Id ,
25
25
runtime:: { AnyObject , ProtocolObject , Sel } ,
26
26
sel, ClassType ,
@@ -47,7 +47,7 @@ declare_class!(
47
47
48
48
unsafe impl ClassType for Delegate {
49
49
type Super = NSObject ;
50
- type Mutability = InteriorMutable ;
50
+ type Mutability = MainThreadOnly ;
51
51
const NAME : & ' static str = "Delegate" ;
52
52
}
53
53
@@ -68,9 +68,9 @@ declare_class!(
68
68
#[ method( applicationDidFinishLaunching: ) ]
69
69
#[ allow( non_snake_case) ]
70
70
unsafe fn applicationDidFinishLaunching( & self , _notification: & NSNotification ) {
71
+ let mtm = MainThreadMarker :: from( self ) ;
71
72
// create the app window
72
73
let window = {
73
- let this = NSWindow :: alloc( ) ;
74
74
let content_rect = NSRect :: new( NSPoint :: new( 0. , 0. ) , NSSize :: new( 1024. , 768. ) ) ;
75
75
let style = NSWindowStyleMaskClosable
76
76
| NSWindowStyleMaskResizable
@@ -79,7 +79,7 @@ declare_class!(
79
79
let flag = false ;
80
80
unsafe {
81
81
NSWindow :: initWithContentRect_styleMask_backing_defer(
82
- this ,
82
+ mtm . alloc ( ) ,
83
83
content_rect,
84
84
style,
85
85
backing_store_type,
@@ -90,16 +90,14 @@ declare_class!(
90
90
91
91
// create the web view
92
92
let web_view = {
93
- let this = WKWebView :: alloc( ) ;
94
93
let frame_rect = NSRect :: ZERO ;
95
- unsafe { WKWebView :: initWithFrame( this , frame_rect) }
94
+ unsafe { WKWebView :: initWithFrame( mtm . alloc ( ) , frame_rect) }
96
95
} ;
97
96
98
97
// create the nav bar view
99
98
let nav_bar = {
100
99
let frame_rect = NSRect :: ZERO ;
101
- let this = NSStackView :: alloc( ) ;
102
- let this = unsafe { NSStackView :: initWithFrame( this, frame_rect) } ;
100
+ let this = unsafe { NSStackView :: initWithFrame( mtm. alloc( ) , frame_rect) } ;
103
101
unsafe {
104
102
this. setOrientation( NSUserInterfaceLayoutOrientationHorizontal ) ;
105
103
this. setAlignment( NSLayoutAttributeHeight ) ;
@@ -112,8 +110,7 @@ declare_class!(
112
110
// create the nav buttons view
113
111
let nav_buttons = {
114
112
let frame_rect = NSRect :: ZERO ;
115
- let this = NSStackView :: alloc( ) ;
116
- let this = unsafe { NSStackView :: initWithFrame( this, frame_rect) } ;
113
+ let this = unsafe { NSStackView :: initWithFrame( mtm. alloc( ) , frame_rect) } ;
117
114
unsafe {
118
115
this. setOrientation( NSUserInterfaceLayoutOrientationHorizontal ) ;
119
116
this. setAlignment( NSLayoutAttributeHeight ) ;
@@ -130,7 +127,7 @@ declare_class!(
130
127
let target = Some :: <& AnyObject >( & web_view) ;
131
128
let action = Some ( sel!( goBack) ) ;
132
129
let this =
133
- unsafe { NSButton :: buttonWithTitle_target_action( title, target, action) } ;
130
+ unsafe { NSButton :: buttonWithTitle_target_action( title, target, action, mtm ) } ;
134
131
unsafe { this. setBezelStyle( NSBezelStyleShadowlessSquare ) } ;
135
132
this
136
133
} ;
@@ -142,7 +139,7 @@ declare_class!(
142
139
let target = Some :: <& AnyObject >( & web_view) ;
143
140
let action = Some ( sel!( goForward) ) ;
144
141
let this =
145
- unsafe { NSButton :: buttonWithTitle_target_action( title, target, action) } ;
142
+ unsafe { NSButton :: buttonWithTitle_target_action( title, target, action, mtm ) } ;
146
143
unsafe { this. setBezelStyle( NSBezelStyleShadowlessSquare ) } ;
147
144
this
148
145
} ;
@@ -155,8 +152,7 @@ declare_class!(
155
152
// create the url text field
156
153
let nav_url = {
157
154
let frame_rect = NSRect :: ZERO ;
158
- let this = NSTextField :: alloc( ) ;
159
- let this = unsafe { NSTextField :: initWithFrame( this, frame_rect) } ;
155
+ let this = unsafe { NSTextField :: initWithFrame( mtm. alloc( ) , frame_rect) } ;
160
156
unsafe {
161
157
this. setDrawsBackground( true ) ;
162
158
this. setBackgroundColor( Some ( & NSColor :: lightGrayColor( ) ) ) ;
@@ -173,8 +169,7 @@ declare_class!(
173
169
// create the window content view
174
170
let content_view = {
175
171
let frame_rect = unsafe { window. frame( ) } ;
176
- let this = NSStackView :: alloc( ) ;
177
- let this = unsafe { NSStackView :: initWithFrame( this, frame_rect) } ;
172
+ let this = unsafe { NSStackView :: initWithFrame( mtm. alloc( ) , frame_rect) } ;
178
173
unsafe {
179
174
this. setOrientation( NSUserInterfaceLayoutOrientationVertical ) ;
180
175
this. setAlignment( NSLayoutAttributeWidth ) ;
@@ -217,7 +212,7 @@ declare_class!(
217
212
menu_app_item. setSubmenu( Some ( & menu_app_menu) ) ;
218
213
menu. addItem( & menu_app_item) ;
219
214
220
- let app = NSApplication :: sharedApplication( ) ;
215
+ let app = NSApplication :: sharedApplication( mtm ) ;
221
216
app. setMainMenu( Some ( & menu) ) ;
222
217
}
223
218
@@ -286,19 +281,20 @@ declare_class!(
286
281
) ;
287
282
288
283
impl Delegate {
289
- pub fn new ( ) -> Id < Self > {
290
- unsafe { msg_send_id ! [ Self :: alloc( ) , init] }
284
+ pub fn new ( mtm : MainThreadMarker ) -> Id < Self > {
285
+ unsafe { msg_send_id ! [ mtm . alloc( ) , init] }
291
286
}
292
287
}
293
288
294
289
unsafe impl NSObjectProtocol for Delegate { }
295
290
296
291
fn main ( ) {
297
- let app = unsafe { NSApplication :: sharedApplication ( ) } ;
292
+ let mtm = MainThreadMarker :: new ( ) . unwrap ( ) ;
293
+ let app = unsafe { NSApplication :: sharedApplication ( mtm) } ;
298
294
unsafe { app. setActivationPolicy ( NSApplicationActivationPolicyRegular ) } ;
299
295
300
296
// initialize the delegate
301
- let delegate = Delegate :: new ( ) ;
297
+ let delegate = Delegate :: new ( mtm ) ;
302
298
303
299
// configure the application delegate
304
300
unsafe {
0 commit comments