geometry.rs

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