1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
use objc2::encode::{Encode, Encoding, RefEncode};
use objc2::ffi::NSUInteger;
#[cfg(target_pointer_width = "64")]
type InnerFloat = f64;
#[cfg(not(target_pointer_width = "64"))]
type InnerFloat = f32;
/// The basic type for all floating-point values.
///
/// This is [`f32`] on 32-bit platforms and [`f64`] on 64-bit platforms.
///
/// This technically belongs to the `CoreGraphics` framework, but we define it
/// here for convenience.
///
/// See [Apple's documentation](https://developer.apple.com/documentation/coregraphics/cgfloat?language=objc).
// Defined in CoreGraphics/CGBase.h and CoreFoundation/CFCGTypes.h
// TODO: Use a newtype here?
pub type CGFloat = InnerFloat;
// NSGeometry types are aliases to CGGeometry types on iOS, tvOS, watchOS and
// macOS 64bit (and hence their Objective-C encodings are different).
//
// TODO: Adjust `objc2-encode` so that this is handled there, and so that we
// can effectively forget about it and use `NS` and `CG` types equally.
#[cfg(not(any(
feature = "gnustep-1-7",
all(target_os = "macos", target_pointer_width = "32")
)))]
mod names {
pub(super) const POINT: &str = "CGPoint";
pub(super) const SIZE: &str = "CGSize";
pub(super) const RECT: &str = "CGRect";
}
#[cfg(any(
feature = "gnustep-1-7",
all(target_os = "macos", target_pointer_width = "32")
))]
mod names {
pub(super) const POINT: &str = "_NSPoint";
pub(super) const SIZE: &str = "_NSSize";
pub(super) const RECT: &str = "_NSRect";
}
/// A point in a two-dimensional coordinate system.
///
/// This technically belongs to the `CoreGraphics` framework, but we define it
/// here for convenience.
///
/// See [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cgpoint?language=objc).
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub struct CGPoint {
/// The x-coordinate of the point.
pub x: CGFloat,
/// The y-coordinate of the point.
pub y: CGFloat,
}
unsafe impl Encode for CGPoint {
const ENCODING: Encoding =
Encoding::Struct(names::POINT, &[CGFloat::ENCODING, CGFloat::ENCODING]);
}
unsafe impl RefEncode for CGPoint {
const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}
impl CGPoint {
/// Create a new point with the given coordinates.
///
///
/// # Examples
///
/// ```
/// use objc2_foundation::CGPoint;
/// assert_eq!(CGPoint::new(10.0, -2.3), CGPoint { x: 10.0, y: -2.3 });
/// ```
#[inline]
#[doc(alias = "NSMakePoint")]
#[doc(alias = "CGPointMake")]
pub const fn new(x: CGFloat, y: CGFloat) -> Self {
Self { x, y }
}
/// A point with both coordinates set to `0.0`.
///
///
/// # Examples
///
/// ```
/// use objc2_foundation::CGPoint;
/// assert_eq!(CGPoint::ZERO, CGPoint { x: 0.0, y: 0.0 });
/// ```
#[doc(alias = "NSZeroPoint")]
#[doc(alias = "CGPointZero")]
#[doc(alias = "ORIGIN")]
pub const ZERO: Self = Self::new(0.0, 0.0);
}
/// A two-dimensional size.
///
/// As this is sometimes used to represent a distance vector, rather than a
/// physical size, the width and height are _not_ guaranteed to be
/// non-negative! Methods that expect that must use one of [`CGSize::abs`] or
/// [`CGRect::standardize`].
///
/// This technically belongs to the `CoreGraphics` framework, but we define it
/// here for convenience.
///
/// See [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cgsize?language=objc).
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub struct CGSize {
/// The dimensions along the x-axis.
pub width: CGFloat,
/// The dimensions along the y-axis.
pub height: CGFloat,
}
unsafe impl Encode for CGSize {
const ENCODING: Encoding =
Encoding::Struct(names::SIZE, &[CGFloat::ENCODING, CGFloat::ENCODING]);
}
unsafe impl RefEncode for CGSize {
const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}
impl CGSize {
/// Create a new size with the given dimensions.
///
///
/// # Examples
///
/// ```
/// use objc2_foundation::CGSize;
/// let size = CGSize::new(10.0, 2.3);
/// assert_eq!(size.width, 10.0);
/// assert_eq!(size.height, 2.3);
/// ```
///
/// Negative values are allowed (though often undesired).
///
/// ```
/// use objc2_foundation::CGSize;
/// let size = CGSize::new(-1.0, 0.0);
/// assert_eq!(size.width, -1.0);
/// ```
#[inline]
#[doc(alias = "NSMakeSize")]
#[doc(alias = "CGSizeMake")]
pub const fn new(width: CGFloat, height: CGFloat) -> Self {
// The documentation for NSSize explicitly says:
// > If the value of width or height is negative, however, the
// > behavior of some methods may be undefined.
//
// But since this type can come from FFI, we'll leave it up to the
// user to ensure that it is used safely.
Self { width, height }
}
/// Convert the size to a non-negative size.
///
/// This can be used to convert the size to a safe value.
///
///
/// # Examples
///
/// ```
/// use objc2_foundation::CGSize;
/// assert_eq!(CGSize::new(-1.0, 1.0).abs(), CGSize::new(1.0, 1.0));
/// ```
#[inline]
pub fn abs(self) -> Self {
Self::new(self.width.abs(), self.height.abs())
}
/// A size that is 0.0 in both dimensions.
///
///
/// # Examples
///
/// ```
/// use objc2_foundation::CGSize;
/// assert_eq!(CGSize::ZERO, CGSize { width: 0.0, height: 0.0 });
/// ```
#[doc(alias = "NSZeroSize")]
#[doc(alias = "CGSizeZero")]
pub const ZERO: Self = Self::new(0.0, 0.0);
}
/// The location and dimensions of a rectangle.
///
/// In the default Core Graphics coordinate space (macOS), the origin is
/// located in the lower-left corner of the rectangle and the rectangle
/// extends towards the upper-right corner.
///
/// If the context has a flipped coordinate space (iOS, tvOS, watchOS) the
/// origin is in the upper-left corner and the rectangle extends towards the
/// lower-right corner.
///
/// This technically belongs to the `CoreGraphics` framework, but we define it
/// here for convenience.
///
/// See [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cgrect?language=objc).
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub struct CGRect {
/// The coordinates of the rectangle’s origin.
pub origin: CGPoint,
/// The dimensions of the rectangle.
pub size: CGSize,
}
unsafe impl Encode for CGRect {
const ENCODING: Encoding =
Encoding::Struct(names::RECT, &[CGPoint::ENCODING, CGSize::ENCODING]);
}
unsafe impl RefEncode for CGRect {
const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}
impl CGRect {
/// Create a new rectangle with the given origin and dimensions.
///
///
/// # Examples
///
/// ```
/// use objc2_foundation::{CGPoint, CGRect, CGSize};
/// let origin = CGPoint::new(10.0, -2.3);
/// let size = CGSize::new(5.0, 0.0);
/// let rect = CGRect::new(origin, size);
/// ```
#[inline]
#[doc(alias = "NSMakeRect")]
#[doc(alias = "CGRectMake")]
pub const fn new(origin: CGPoint, size: CGSize) -> Self {
Self { origin, size }
}
/// A rectangle with origin (0.0, 0.0) and zero width and height.
#[doc(alias = "NSZeroRect")]
#[doc(alias = "CGRectZero")]
pub const ZERO: Self = Self::new(CGPoint::ZERO, CGSize::ZERO);
/// Returns a rectangle with a positive width and height.
///
/// This is often useful
///
///
/// # Examples
///
/// ```
/// use objc2_foundation::{CGPoint, CGRect, CGSize};
/// let origin = CGPoint::new(1.0, 1.0);
/// let size = CGSize::new(-5.0, -2.0);
/// let rect = CGRect::new(origin, size);
/// assert_eq!(rect.standardize().size, CGSize::new(5.0, 2.0));
/// ```
#[inline]
#[doc(alias = "CGRectStandardize")]
pub fn standardize(self) -> Self {
Self::new(self.origin, self.size.abs())
}
/// The smallest coordinate of the rectangle.
#[inline]
#[doc(alias = "CGRectGetMinX")]
#[doc(alias = "CGRectGetMinY")]
#[doc(alias = "NSMinX")]
#[doc(alias = "NSMinY")]
pub fn min(self) -> CGPoint {
self.origin
}
/// The center point of the rectangle.
#[inline]
#[doc(alias = "CGRectGetMidX")]
#[doc(alias = "CGRectGetMidY")]
#[doc(alias = "NSMidX")]
#[doc(alias = "NSMidY")]
pub fn mid(self) -> CGPoint {
CGPoint::new(
self.origin.x + (self.size.width * 0.5),
self.origin.y + (self.size.height * 0.5),
)
}
/// The largest coordinate of the rectangle.
#[inline]
#[doc(alias = "CGRectGetMaxX")]
#[doc(alias = "CGRectGetMaxY")]
#[doc(alias = "NSMaxX")]
#[doc(alias = "NSMaxY")]
pub fn max(self) -> CGPoint {
CGPoint::new(
self.origin.x + self.size.width,
self.origin.y + self.size.height,
)
}
/// Returns whether a rectangle has zero width or height.
///
///
/// # Examples
///
/// ```
/// use objc2_foundation::{CGPoint, CGRect, CGSize};
/// assert!(CGRect::ZERO.is_empty());
/// let point = CGPoint::new(1.0, 2.0);
/// assert!(CGRect::new(point, CGSize::ZERO).is_empty());
/// assert!(!CGRect::new(point, CGSize::new(1.0, 1.0)).is_empty());
/// ```
#[inline]
#[doc(alias = "CGRectIsEmpty")]
pub fn is_empty(self) -> bool {
!(self.size.width > 0.0 && self.size.height > 0.0)
// TODO: NaN handling?
// self.size.width <= 0.0 || self.size.height <= 0.0
}
// TODO: NSContainsRect / CGRectContainsRect
// TODO: NSDivideRect / CGRectDivide
// TODO: NSInsetRect / CGRectInset
// TODO: NSIntegralRect / CGRectIntegral
// TODO: NSIntersectionRect / CGRectIntersection
// TODO: NSUnionRect / CGRectUnion
// TODO: NSIntersectsRect / CGRectIntersectsRect
// TODO: NSMouseInRect
// TODO: NSMouseInRect
// TODO: NSPointInRect / CGRectContainsPoint
// TODO: NSOffsetRect / CGRectOffset
// TODO: CGRectIsNull
// TODO: CGRectIsInfinite
// TODO: CGRectInfinite
// TODO: CGRectNull
// TODO: NSHeight / CGRectGetHeight (standardized)
// TODO: NSWidth / CGRectGetWidth (standardized)
}
/// A point in a Cartesian coordinate system.
///
/// This is a convenience alias for [`CGPoint`]. For ease of use, it is
/// available on all platforms, though in practice it is only useful on macOS.
///
/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nspoint?language=objc).
pub type NSPoint = CGPoint;
/// A two-dimensional size.
///
/// This is a convenience alias for [`CGSize`]. For ease of use, it is
/// available on all platforms, though in practice it is only useful on macOS.
///
/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nssize?language=objc).
pub type NSSize = CGSize;
/// A rectangle.
///
/// This is a convenience alias for [`CGRect`]. For ease of use, it is
/// available on all platforms, though in practice it is only useful on macOS.
///
/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsrect?language=objc).
pub type NSRect = CGRect;
// NS_ENUM
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct NSRectEdge(pub NSUInteger);
unsafe impl Encode for NSRectEdge {
const ENCODING: Encoding = NSUInteger::ENCODING;
}
unsafe impl RefEncode for NSRectEdge {
const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}
#[allow(non_upper_case_globals)]
impl NSRectEdge {
#[doc(alias = "NSRectEdgeMinX")]
pub const MinX: Self = Self(0);
#[doc(alias = "NSRectEdgeMinY")]
pub const MinY: Self = Self(1);
#[doc(alias = "NSRectEdgeMaxX")]
pub const MaxX: Self = Self(2);
#[doc(alias = "NSRectEdgeMaxY")]
pub const MaxY: Self = Self(3);
pub const NSMinXEdge: Self = Self(NSRectEdge::MinX.0);
pub const NSMinYEdge: Self = Self(NSRectEdge::MinY.0);
pub const NSMaxXEdge: Self = Self(NSRectEdge::MaxX.0);
pub const NSMaxYEdge: Self = Self(NSRectEdge::MaxY.0);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cgsize_new() {
CGSize::new(1.0, 1.0);
CGSize::new(0.0, -0.0);
CGSize::new(-0.0, 0.0);
CGSize::new(-0.0, -0.0);
CGSize::new(-1.0, -1.0);
CGSize::new(-1.0, 1.0);
CGSize::new(1.0, -1.0);
}
// We know the Rust implementation handles NaN, infinite, negative zero
// and so on properly, so let's ensure that NSEqualXXX handles these as
// well (so that we're confident that the implementations are equivalent).
#[test]
#[cfg(any(
all(target_vendor = "apple", target_os = "macos"), // or macabi
feature = "gnustep-1-7"
))]
#[cfg(feature = "NSGeometry")]
fn test_partial_eq() {
use crate::Foundation::{NSEqualPoints, NSEqualRects, NSEqualSizes};
// We assume that comparisons handle e.g. `x` and `y` in the same way,
// therefore we set the coordinates / dimensions to the same.
let cases: &[(CGFloat, CGFloat)] = &[
(0.0, 0.0),
(-0.0, -0.0),
(0.0, -0.0),
(1.0, 1.0 + CGFloat::EPSILON),
(0.0, CGFloat::MIN_POSITIVE),
(0.0, CGFloat::EPSILON),
(1.0, 1.0),
(1.0, -1.0),
// Infinity
(CGFloat::INFINITY, CGFloat::INFINITY),
(CGFloat::INFINITY, CGFloat::NEG_INFINITY),
(CGFloat::NEG_INFINITY, CGFloat::NEG_INFINITY),
// NaN
(CGFloat::NAN, 0.0),
(CGFloat::NAN, 1.0),
(CGFloat::NAN, CGFloat::NAN),
(CGFloat::NAN, -CGFloat::NAN),
(-CGFloat::NAN, -CGFloat::NAN),
(CGFloat::NAN, CGFloat::INFINITY),
];
for case in cases {
let point_a = NSPoint::new(case.0, case.1);
let point_b = NSPoint::new(case.0, case.1);
let actual = unsafe { NSEqualPoints(point_a, point_b).as_bool() };
assert_eq!(point_a == point_b, actual);
if case.0 >= 0.0 && case.1 >= 0.0 {
let size_a = NSSize::new(case.0, case.1);
let size_b = NSSize::new(case.0, case.1);
let actual = unsafe { NSEqualSizes(size_a, size_b).as_bool() };
assert_eq!(size_a == size_b, actual);
let rect_a = NSRect::new(point_a, size_a);
let rect_b = NSRect::new(point_b, size_b);
let actual = unsafe { NSEqualRects(rect_a, rect_b).as_bool() };
assert_eq!(rect_a == rect_b, actual);
}
}
}
}