Adapt to Core Graphics coordinate system
This commit is contained in:
parent
7ff0d65793
commit
f5b18e8196
1 changed files with 13 additions and 4 deletions
17
src/cg.rs
17
src/cg.rs
|
|
@ -9,18 +9,25 @@ use core_graphics::geometry::{CGPoint, CGSize, CGRect};
|
||||||
use core_graphics::image::CGImage;
|
use core_graphics::image::CGImage;
|
||||||
use core_graphics::sys;
|
use core_graphics::sys;
|
||||||
|
|
||||||
pub struct CGImpl;
|
pub struct CGImpl {
|
||||||
|
view: *mut Object,
|
||||||
|
}
|
||||||
|
|
||||||
impl CGImpl {
|
impl CGImpl {
|
||||||
pub unsafe fn new<W: HasRawWindowHandle>(handle: AppKitHandle) -> Result<Self, SoftBufferError<W>> {
|
pub unsafe fn new<W: HasRawWindowHandle>(handle: AppKitHandle) -> Result<Self, SoftBufferError<W>> {
|
||||||
let window = handle.ns_window as *mut Object;
|
let window = handle.ns_window as *mut Object;
|
||||||
|
let view = handle.ns_view as *mut Object;
|
||||||
let cls = class!(NSGraphicsContext);
|
let cls = class!(NSGraphicsContext);
|
||||||
let graphics_context: *mut Object = msg_send![cls, graphicsContextWithWindow:window];
|
let graphics_context: *mut Object = msg_send![cls, graphicsContextWithWindow:window];
|
||||||
if graphics_context.is_null() {
|
if graphics_context.is_null() {
|
||||||
return Err(SoftBufferError::PlatformError(Some("Graphics context is null".into()), None));
|
return Err(SoftBufferError::PlatformError(Some("Graphics context is null".into()), None));
|
||||||
}
|
}
|
||||||
let _: () = msg_send![cls, setCurrentContext:graphics_context];
|
let _: () = msg_send![cls, setCurrentContext:graphics_context];
|
||||||
Ok(Self)
|
Ok(
|
||||||
|
Self {
|
||||||
|
view,
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,8 +54,10 @@ impl GraphicsContextImpl for CGImpl {
|
||||||
false,
|
false,
|
||||||
kCGRenderingIntentDefault,
|
kCGRenderingIntentDefault,
|
||||||
);
|
);
|
||||||
let origin = CGPoint { x: 0f64, y: 0f64 };
|
let frame: CGRect = msg_send![self.view, frame];
|
||||||
let size = CGSize { width: width as f64, height: height as f64 };
|
// 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 };
|
let rect = CGRect { origin, size };
|
||||||
context.draw_image(rect, &image);
|
context.draw_image(rect, &image);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue