From f5b18e819664b922bd98ade2f7602c94bdf76a18 Mon Sep 17 00:00:00 2001 From: Seo Sanghyeon Date: Wed, 19 Jan 2022 14:04:17 +0900 Subject: [PATCH] Adapt to Core Graphics coordinate system --- src/cg.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/cg.rs b/src/cg.rs index 4cc1700..7574e33 100644 --- a/src/cg.rs +++ b/src/cg.rs @@ -9,18 +9,25 @@ use core_graphics::geometry::{CGPoint, CGSize, CGRect}; use core_graphics::image::CGImage; use core_graphics::sys; -pub struct CGImpl; +pub struct CGImpl { + view: *mut Object, +} impl CGImpl { pub unsafe fn new(handle: AppKitHandle) -> Result> { let window = handle.ns_window as *mut Object; + let view = handle.ns_view as *mut Object; let cls = class!(NSGraphicsContext); let graphics_context: *mut Object = msg_send![cls, graphicsContextWithWindow:window]; if graphics_context.is_null() { return Err(SoftBufferError::PlatformError(Some("Graphics context is null".into()), None)); } let _: () = msg_send![cls, setCurrentContext:graphics_context]; - Ok(Self) + Ok( + Self { + view, + } + ) } } @@ -47,8 +54,10 @@ impl GraphicsContextImpl for CGImpl { false, kCGRenderingIntentDefault, ); - let origin = CGPoint { x: 0f64, y: 0f64 }; - let size = CGSize { width: width as f64, height: height as f64 }; + let frame: CGRect = msg_send![self.view, frame]; + // In Core Graphics, (0, 0) is bottom left, not top left + let origin = CGPoint { x: 0f64, y: frame.size.height }; + let size = CGSize { width: width as f64, height: -(height as f64) }; let rect = CGRect { origin, size }; context.draw_image(rect, &image); }