@@ -4,17 +4,17 @@ use std::collections::{HashMap, VecDeque};
4
4
use std:: ptr;
5
5
use std:: rc:: Rc ;
6
6
7
- use objc2:: rc:: { Retained , WeakId } ;
7
+ use objc2:: rc:: Retained ;
8
8
use objc2:: runtime:: { AnyObject , Sel } ;
9
- use objc2:: { declare_class, msg_send_id, mutability, sel , ClassType , DeclaredClass } ;
9
+ use objc2:: { declare_class, msg_send_id, mutability, ClassType , DeclaredClass } ;
10
10
use objc2_app_kit:: {
11
11
NSApplication , NSCursor , NSEvent , NSEventPhase , NSResponder , NSTextInputClient ,
12
- NSTrackingRectTag , NSView , NSViewFrameDidChangeNotification ,
12
+ NSTrackingRectTag , NSView ,
13
13
} ;
14
14
use objc2_foundation:: {
15
15
MainThreadMarker , NSArray , NSAttributedString , NSAttributedStringKey , NSCopying ,
16
- NSMutableAttributedString , NSNotFound , NSNotificationCenter , NSObject , NSObjectProtocol ,
17
- NSPoint , NSRange , NSRect , NSSize , NSString , NSUInteger ,
16
+ NSMutableAttributedString , NSNotFound , NSObject , NSObjectProtocol , NSPoint , NSRange , NSRect ,
17
+ NSSize , NSString , NSUInteger ,
18
18
} ;
19
19
20
20
use super :: app_state:: AppState ;
@@ -134,9 +134,6 @@ pub struct ViewState {
134
134
marked_text : RefCell < Retained < NSMutableAttributedString > > ,
135
135
accepts_first_mouse : bool ,
136
136
137
- // Weak reference because the window keeps a strong reference to the view
138
- _ns_window : WeakId < WinitWindow > ,
139
-
140
137
/// The state of the `Option` as `Alt`.
141
138
option_as_alt : Cell < OptionAsAlt > ,
142
139
}
@@ -177,9 +174,10 @@ declare_class!(
177
174
self . ivars( ) . tracking_rect. set( Some ( tracking_rect) ) ;
178
175
}
179
176
180
- #[ method( frameDidChange: ) ]
181
- fn frame_did_change( & self , _event: & NSEvent ) {
182
- trace_scope!( "frameDidChange:" ) ;
177
+ // Not a normal method on `NSView`, it's triggered by `NSViewFrameDidChangeNotification`.
178
+ #[ method( viewFrameDidChangeNotification: ) ]
179
+ fn frame_did_change( & self , _notification: Option <& AnyObject >) {
180
+ trace_scope!( "NSViewFrameDidChangeNotification" ) ;
183
181
if let Some ( tracking_rect) = self . ivars( ) . tracking_rect. take( ) {
184
182
self . removeTrackingRect( tracking_rect) ;
185
183
}
@@ -203,10 +201,7 @@ declare_class!(
203
201
fn draw_rect( & self , _rect: NSRect ) {
204
202
trace_scope!( "drawRect:" ) ;
205
203
206
- // It's a workaround for https://github.com/rust-windowing/winit/issues/2640, don't replace with `self.window_id()`.
207
- if let Some ( window) = self . ivars( ) . _ns_window. load( ) {
208
- self . ivars( ) . app_state. handle_redraw( window. id( ) ) ;
209
- }
204
+ self . ivars( ) . app_state. handle_redraw( self . window( ) . id( ) ) ;
210
205
211
206
// This is a direct subclass of NSView, no need to call superclass' drawRect:
212
207
}
@@ -806,11 +801,10 @@ declare_class!(
806
801
impl WinitView {
807
802
pub ( super ) fn new (
808
803
app_state : & Rc < AppState > ,
809
- window : & WinitWindow ,
810
804
accepts_first_mouse : bool ,
811
805
option_as_alt : OptionAsAlt ,
806
+ mtm : MainThreadMarker ,
812
807
) -> Retained < Self > {
813
- let mtm = MainThreadMarker :: from ( window) ;
814
808
let this = mtm. alloc ( ) . set_ivars ( ViewState {
815
809
app_state : Rc :: clone ( app_state) ,
816
810
cursor_state : Default :: default ( ) ,
@@ -825,34 +819,24 @@ impl WinitView {
825
819
forward_key_to_app : Default :: default ( ) ,
826
820
marked_text : Default :: default ( ) ,
827
821
accepts_first_mouse,
828
- _ns_window : WeakId :: new ( & window. retain ( ) ) ,
829
822
option_as_alt : Cell :: new ( option_as_alt) ,
830
823
} ) ;
831
824
let this: Retained < Self > = unsafe { msg_send_id ! [ super ( this) , init] } ;
832
825
833
- this. setPostsFrameChangedNotifications ( true ) ;
834
- let notification_center = unsafe { NSNotificationCenter :: defaultCenter ( ) } ;
835
- unsafe {
836
- notification_center. addObserver_selector_name_object (
837
- & this,
838
- sel ! ( frameDidChange: ) ,
839
- Some ( NSViewFrameDidChangeNotification ) ,
840
- Some ( & this) ,
841
- )
842
- }
843
-
844
826
* this. ivars ( ) . input_source . borrow_mut ( ) = this. current_input_source ( ) ;
845
827
846
828
this
847
829
}
848
830
849
831
fn window ( & self ) -> Retained < WinitWindow > {
850
- // TODO: Simply use `window` property on `NSView`.
851
- // That only returns a window _after_ the view has been attached though!
852
- // (which is incompatible with `frameDidChange:`)
853
- //
854
- // unsafe { msg_send_id![self, window] }
855
- self . ivars ( ) . _ns_window . load ( ) . expect ( "view to have a window" )
832
+ let window = ( * * self ) . window ( ) . expect ( "view must be installed in a window" ) ;
833
+
834
+ if !window. isKindOfClass ( WinitWindow :: class ( ) ) {
835
+ unreachable ! ( "view installed in non-WinitWindow" ) ;
836
+ }
837
+
838
+ // SAFETY: Just checked that the window is `WinitWindow`
839
+ unsafe { Retained :: cast ( window) }
856
840
}
857
841
858
842
fn queue_event ( & self , event : WindowEvent ) {
0 commit comments