better but still broken

This commit is contained in:
Kay Simmons 2023-01-25 17:05:57 -08:00
parent 5eac797a93
commit a369fb8033
9 changed files with 294 additions and 236 deletions

View file

@ -1,27 +1,99 @@
use cocoa::foundation::{NSPoint, NSRect, NSSize};
use pathfinder_geometry::{rect::RectF, vector::Vector2F};
use cocoa::{
appkit::NSWindow,
base::id,
foundation::{NSPoint, NSRect, NSSize},
};
use objc::{msg_send, sel, sel_impl};
use pathfinder_geometry::{
rect::RectF,
vector::{vec2f, Vector2F},
};
///! Macos screen have a y axis that goings up from the bottom of the screen and
///! an origin at the bottom left of the main display.
pub trait Vector2FExt {
fn to_ns_point(&self) -> NSPoint;
fn to_ns_size(&self) -> NSSize;
/// Converts self to an NSPoint with y axis pointing up.
fn to_screen_ns_point(&self, native_window: id) -> NSPoint;
}
impl Vector2FExt for Vector2F {
fn to_screen_ns_point(&self, native_window: id) -> NSPoint {
unsafe {
let point = NSPoint::new(self.x() as f64, -self.y() as f64);
msg_send![native_window, convertPointToScreen: point]
}
}
}
pub trait RectFExt {
/// Converts self to an NSRect with y axis pointing up.
/// The resulting NSRect will have an origin at the bottom left of the rectangle.
/// Also takes care of converting from window scaled coordinates to screen coordinates
fn to_screen_ns_rect(&self, native_window: id) -> NSRect;
/// Converts self to an NSRect with y axis point up.
/// The resulting NSRect will have an origin at the bottom left of the rectangle.
/// Unlike to_screen_ns_rect, coordinates are not converted and are assumed to already be in screen scale
fn to_ns_rect(&self) -> NSRect;
}
impl Vector2FExt for Vector2F {
fn to_ns_point(&self) -> NSPoint {
NSPoint::new(self.x() as f64, self.y() as f64)
}
fn to_ns_size(&self) -> NSSize {
NSSize::new(self.x() as f64, self.y() as f64)
}
}
impl RectFExt for RectF {
fn to_screen_ns_rect(&self, native_window: id) -> NSRect {
unsafe { native_window.convertRectToScreen_(self.to_ns_rect()) }
}
fn to_ns_rect(&self) -> NSRect {
NSRect::new(self.origin().to_ns_point(), self.size().to_ns_size())
dbg!(&self);
NSRect::new(
NSPoint::new(
dbg!(self.origin_x() as f64),
dbg!(-(self.origin_y() - self.height()) as f64),
),
NSSize::new(self.width() as f64, self.height() as f64),
)
}
}
pub trait NSPointExt {
/// Converts self to a Vector2F with y axis pointing down.
/// Also takes care of converting from window scaled coordinates to screen coordinates
fn to_window_vector2f(&self, native_window: id) -> Vector2F;
}
impl NSPointExt for NSPoint {
fn to_window_vector2f(&self, native_window: id) -> Vector2F {
unsafe {
let point: NSPoint = msg_send![native_window, convertPointFromScreen: self];
vec2f(point.x as f32, -point.y as f32)
}
}
}
pub trait NSRectExt {
/// Converts self to a RectF with y axis pointing down.
/// The resulting RectF will have an origin at the top left of the rectangle.
/// Also takes care of converting from screen scale coordinates to window coordinates
fn to_window_rectf(&self, native_window: id) -> RectF;
/// Converts self to a RectF with y axis pointing down.
/// The resulting RectF will have an origin at the top left of the rectangle.
/// Unlike to_screen_ns_rect, coordinates are not converted and are assumed to already be in screen scale
fn to_rectf(&self) -> RectF;
}
impl NSRectExt for NSRect {
fn to_window_rectf(&self, native_window: id) -> RectF {
unsafe {
dbg!(self.origin.x);
let rect: NSRect = native_window.convertRectFromScreen_(*self);
rect.to_rectf()
}
}
fn to_rectf(&self) -> RectF {
RectF::new(
vec2f(
dbg!(self.origin.x as f32),
dbg!(-(self.origin.y - self.size.height) as f32),
),
vec2f(self.size.width as f32, self.size.height as f32),
)
}
}