Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Add bindings to NSInsetRect and NSRectToCGRect, and make various
Browse files Browse the repository at this point in the history
geometry types copyable.
  • Loading branch information
pcwalton committed May 27, 2016
1 parent 5fef233 commit 37b5ce6
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/foundation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::mem;
use std::ptr;
use base::{id, class, BOOL, nil};
use core_graphics::base::CGFloat;
use core_graphics::geometry::CGRect;
use libc;
use objc;

Expand All @@ -25,6 +28,7 @@ pub type NSUInteger = libc::c_ulong;
const UTF8_ENCODING: usize = 4;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct NSPoint {
pub x: f64,
pub y: f64,
Expand All @@ -50,6 +54,7 @@ unsafe impl objc::Encode for NSPoint {
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct NSSize {
pub width: f64,
pub height: f64,
Expand All @@ -75,6 +80,7 @@ unsafe impl objc::Encode for NSSize {
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct NSRect {
pub origin: NSPoint,
pub size: NSSize,
Expand All @@ -88,6 +94,20 @@ impl NSRect {
size: size
}
}

#[inline]
pub fn as_CGRect(&self) -> &CGRect {
unsafe {
mem::transmute::<&NSRect, &CGRect>(self)
}
}

#[inline]
pub fn inset(&self, x: CGFloat, y: CGFloat) -> NSRect {
unsafe {
NSInsetRect(*self, x, y)
}
}
}

#[repr(C)]
Expand Down Expand Up @@ -330,3 +350,9 @@ impl NSFastEnumeration for id {
}
}
}

#[link(name = "Foundation", kind = "framework")]
extern {
fn NSInsetRect(rect: NSRect, x: CGFloat, y: CGFloat) -> NSRect;
}

0 comments on commit 37b5ce6

Please sign in to comment.