screencapturekit/cg/size.rs
1//! `CGSize` type for 2D dimensions
2
3use std::fmt;
4
5/// `CGSize` representation
6///
7/// Represents a 2D size with width and height.
8///
9/// # Examples
10///
11/// ```
12/// use screencapturekit::cg::CGSize;
13///
14/// let size = CGSize::new(1920.0, 1080.0);
15/// assert_eq!(size.aspect_ratio(), 1920.0 / 1080.0);
16/// assert_eq!(size.area(), 1920.0 * 1080.0);
17/// ```
18#[repr(C)]
19#[derive(Debug, Clone, Copy, PartialEq)]
20pub struct CGSize {
21 pub width: f64,
22 pub height: f64,
23}
24
25impl std::hash::Hash for CGSize {
26 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
27 self.width.to_bits().hash(state);
28 self.height.to_bits().hash(state);
29 }
30}
31
32impl Eq for CGSize {}
33
34impl CGSize {
35 /// Create a new size
36 ///
37 /// # Examples
38 ///
39 /// ```
40 /// use screencapturekit::cg::CGSize;
41 ///
42 /// let size = CGSize::new(800.0, 600.0);
43 /// assert_eq!(size.width, 800.0);
44 /// ```
45 pub const fn new(width: f64, height: f64) -> Self {
46 Self { width, height }
47 }
48
49 /// Create a zero-sized size
50 ///
51 /// # Examples
52 ///
53 /// ```
54 /// use screencapturekit::cg::CGSize;
55 ///
56 /// let size = CGSize::zero();
57 /// assert!(size.is_null());
58 /// ```
59 pub const fn zero() -> Self {
60 Self::new(0.0, 0.0)
61 }
62
63 /// Get the area (width * height)
64 pub const fn area(&self) -> f64 {
65 self.width * self.height
66 }
67
68 /// Get the aspect ratio (width / height)
69 pub fn aspect_ratio(&self) -> f64 {
70 if self.height == 0.0 {
71 0.0
72 } else {
73 self.width / self.height
74 }
75 }
76
77 /// Check if this is a square (width == height)
78 /// Note: Uses exact comparison, may not work well with computed values
79 #[allow(clippy::float_cmp)]
80 pub const fn is_square(&self) -> bool {
81 self.width == self.height
82 }
83
84 pub fn is_empty(&self) -> bool {
85 self.width <= 0.0 || self.height <= 0.0
86 }
87
88 /// Check if size is null (both dimensions are zero)
89 pub const fn is_null(&self) -> bool {
90 self.width == 0.0 && self.height == 0.0
91 }
92}
93
94impl Default for CGSize {
95 fn default() -> Self {
96 Self::zero()
97 }
98}
99
100impl fmt::Display for CGSize {
101 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102 write!(f, "{}x{}", self.width, self.height)
103 }
104}