Skip to content

Commit f5b18e8

Browse files
committed
Adapt to Core Graphics coordinate system
1 parent 7ff0d65 commit f5b18e8

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/cg.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@ use core_graphics::geometry::{CGPoint, CGSize, CGRect};
99
use core_graphics::image::CGImage;
1010
use core_graphics::sys;
1111

12-
pub struct CGImpl;
12+
pub struct CGImpl {
13+
view: *mut Object,
14+
}
1315

1416
impl CGImpl {
1517
pub unsafe fn new<W: HasRawWindowHandle>(handle: AppKitHandle) -> Result<Self, SoftBufferError<W>> {
1618
let window = handle.ns_window as *mut Object;
19+
let view = handle.ns_view as *mut Object;
1720
let cls = class!(NSGraphicsContext);
1821
let graphics_context: *mut Object = msg_send![cls, graphicsContextWithWindow:window];
1922
if graphics_context.is_null() {
2023
return Err(SoftBufferError::PlatformError(Some("Graphics context is null".into()), None));
2124
}
2225
let _: () = msg_send![cls, setCurrentContext:graphics_context];
23-
Ok(Self)
26+
Ok(
27+
Self {
28+
view,
29+
}
30+
)
2431
}
2532
}
2633

@@ -47,8 +54,10 @@ impl GraphicsContextImpl for CGImpl {
4754
false,
4855
kCGRenderingIntentDefault,
4956
);
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) };
5261
let rect = CGRect { origin, size };
5362
context.draw_image(rect, &image);
5463
}

0 commit comments

Comments
 (0)