geometry.rs

 1use cocoa::{
 2    base::id,
 3    foundation::{NSPoint, NSRect},
 4};
 5use objc::{msg_send, sel, sel_impl};
 6use pathfinder_geometry::{
 7    rect::RectF,
 8    vector::{vec2f, Vector2F},
 9};
10
11///! Macos screen have a y axis that goings up from the bottom of the screen and
12///! an origin at the bottom left of the main display.
13
14pub trait Vector2FExt {
15    /// Converts self to an NSPoint with y axis pointing up.
16    fn to_screen_ns_point(&self, native_window: id, window_height: f64) -> NSPoint;
17}
18impl Vector2FExt for Vector2F {
19    fn to_screen_ns_point(&self, native_window: id, window_height: f64) -> NSPoint {
20        unsafe {
21            let point = NSPoint::new(self.x() as f64, window_height - self.y() as f64);
22            msg_send![native_window, convertPointToScreen: point]
23        }
24    }
25}
26
27pub trait NSRectExt {
28    fn to_rectf(&self) -> RectF;
29    fn intersects(&self, other: Self) -> bool;
30}
31
32impl NSRectExt for NSRect {
33    fn to_rectf(&self) -> RectF {
34        RectF::new(
35            vec2f(self.origin.x as f32, self.origin.y as f32),
36            vec2f(self.size.width as f32, self.size.height as f32),
37        )
38    }
39
40    fn intersects(&self, other: Self) -> bool {
41        self.size.width > 0.
42            && self.size.height > 0.
43            && other.size.width > 0.
44            && other.size.height > 0.
45            && self.origin.x <= other.origin.x + other.size.width
46            && self.origin.x + self.size.width >= other.origin.x
47            && self.origin.y <= other.origin.y + other.size.height
48            && self.origin.y + self.size.height >= other.origin.y
49    }
50}