Adapt to Core Graphics coordinate system

This commit is contained in:
Seo Sanghyeon 2022-01-19 14:04:17 +09:00
parent 7ff0d65793
commit f5b18e8196

View file

@ -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<W: HasRawWindowHandle>(handle: AppKitHandle) -> Result<Self, SoftBufferError<W>> {
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);
}