Skip to content

Commit

Permalink
Adapt to Core Graphics coordinate system
Browse files Browse the repository at this point in the history
  • Loading branch information
sanxiyn committed Jan 19, 2022
1 parent 7ff0d65 commit f5b18e8
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/cg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)
}
}

Expand All @@ -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);
}
Expand Down

0 comments on commit f5b18e8

Please sign in to comment.