@@ -9,18 +9,25 @@ use core_graphics::geometry::{CGPoint, CGSize, CGRect};
9
9
use core_graphics:: image:: CGImage ;
10
10
use core_graphics:: sys;
11
11
12
- pub struct CGImpl ;
12
+ pub struct CGImpl {
13
+ view : * mut Object ,
14
+ }
13
15
14
16
impl CGImpl {
15
17
pub unsafe fn new < W : HasRawWindowHandle > ( handle : AppKitHandle ) -> Result < Self , SoftBufferError < W > > {
16
18
let window = handle. ns_window as * mut Object ;
19
+ let view = handle. ns_view as * mut Object ;
17
20
let cls = class ! ( NSGraphicsContext ) ;
18
21
let graphics_context: * mut Object = msg_send ! [ cls, graphicsContextWithWindow: window] ;
19
22
if graphics_context. is_null ( ) {
20
23
return Err ( SoftBufferError :: PlatformError ( Some ( "Graphics context is null" . into ( ) ) , None ) ) ;
21
24
}
22
25
let _: ( ) = msg_send ! [ cls, setCurrentContext: graphics_context] ;
23
- Ok ( Self )
26
+ Ok (
27
+ Self {
28
+ view,
29
+ }
30
+ )
24
31
}
25
32
}
26
33
@@ -47,8 +54,10 @@ impl GraphicsContextImpl for CGImpl {
47
54
false ,
48
55
kCGRenderingIntentDefault,
49
56
) ;
50
- let origin = CGPoint { x : 0f64 , y : 0f64 } ;
51
- let size = CGSize { width : width as f64 , height : height as f64 } ;
57
+ let frame: CGRect = msg_send ! [ self . view, frame] ;
58
+ // In Core Graphics, (0, 0) is bottom left, not top left
59
+ let origin = CGPoint { x : 0f64 , y : frame. size . height } ;
60
+ let size = CGSize { width : width as f64 , height : -( height as f64 ) } ;
52
61
let rect = CGRect { origin, size } ;
53
62
context. draw_image ( rect, & image) ;
54
63
}
0 commit comments