@@ -16,25 +16,26 @@ use std::sync::Arc;
16
16
17
17
pub struct CGImpl {
18
18
layer : CALayer ,
19
+ window : id ,
19
20
}
20
21
21
22
impl CGImpl {
22
23
pub unsafe fn new ( handle : AppKitWindowHandle ) -> Result < Self , SoftBufferError > {
23
24
let window = handle. ns_window as id ;
25
+ let window: id = msg_send ! [ window, retain] ;
24
26
let view = handle. ns_view as id ;
25
27
let layer = CALayer :: new ( ) ;
26
28
unsafe {
27
29
let subview: id = NSView :: alloc ( nil) . initWithFrame_ ( NSView :: frame ( view) ) ;
28
30
layer. set_contents_gravity ( ContentsGravity :: TopLeft ) ;
29
31
layer. set_needs_display_on_bounds_change ( false ) ;
30
- layer. set_contents_scale ( window. backingScaleFactor ( ) ) ;
31
32
subview. setLayer ( layer. id ( ) ) ;
32
33
subview. setAutoresizingMask_ ( NSViewWidthSizable | NSViewHeightSizable ) ;
33
34
34
35
view. addSubview_ ( subview) ; // retains subview (+1) = 2
35
36
let _: ( ) = msg_send ! [ subview, release] ; // releases subview (-1) = 1
36
37
}
37
- Ok ( Self { layer } )
38
+ Ok ( Self { layer, window } )
38
39
}
39
40
40
41
pub ( crate ) unsafe fn set_buffer ( & mut self , buffer : & [ u32 ] , width : u16 , height : u16 ) {
@@ -62,8 +63,20 @@ impl CGImpl {
62
63
transaction:: begin ( ) ;
63
64
transaction:: set_disable_actions ( true ) ;
64
65
65
- unsafe { self . layer . set_contents ( image. as_ptr ( ) as id ) } ;
66
+ unsafe {
67
+ self . layer
68
+ . set_contents_scale ( self . window . backingScaleFactor ( ) ) ;
69
+ self . layer . set_contents ( image. as_ptr ( ) as id ) ;
70
+ } ;
66
71
67
72
transaction:: commit ( ) ;
68
73
}
69
74
}
75
+
76
+ impl Drop for CGImpl {
77
+ fn drop ( & mut self ) {
78
+ unsafe {
79
+ let _: ( ) = msg_send ! [ self . window, release] ;
80
+ }
81
+ }
82
+ }
0 commit comments