screencapturekit/cg/
rect.rs1use std::fmt;
4
5use super::{CGPoint, CGSize};
6
7#[repr(C)]
22#[derive(Debug, Clone, Copy, PartialEq)]
23pub struct CGRect {
24 pub x: f64,
25 pub y: f64,
26 pub width: f64,
27 pub height: f64,
28}
29
30impl std::hash::Hash for CGRect {
31 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
32 self.x.to_bits().hash(state);
33 self.y.to_bits().hash(state);
34 self.width.to_bits().hash(state);
35 self.height.to_bits().hash(state);
36 }
37}
38
39impl Eq for CGRect {}
40
41impl CGRect {
42 pub const fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
53 Self {
54 x,
55 y,
56 width,
57 height,
58 }
59 }
60
61 pub const fn zero() -> Self {
72 Self::new(0.0, 0.0, 0.0, 0.0)
73 }
74
75 pub const fn with_origin_and_size(origin: CGPoint, size: CGSize) -> Self {
77 Self {
78 x: origin.x,
79 y: origin.y,
80 width: size.width,
81 height: size.height,
82 }
83 }
84
85 pub const fn origin(&self) -> CGPoint {
87 CGPoint::new(self.x, self.y)
88 }
89
90 pub const fn size(&self) -> CGSize {
92 CGSize::new(self.width, self.height)
93 }
94
95 pub const fn center(&self) -> CGPoint {
97 CGPoint::new(self.x + self.width / 2.0, self.y + self.height / 2.0)
98 }
99
100 pub const fn min_x(&self) -> f64 {
102 self.x
103 }
104
105 pub const fn min_y(&self) -> f64 {
107 self.y
108 }
109
110 pub const fn max_x(&self) -> f64 {
112 self.x + self.width
113 }
114
115 pub const fn max_y(&self) -> f64 {
117 self.y + self.height
118 }
119
120 pub const fn mid_x(&self) -> f64 {
122 self.x + self.width / 2.0
123 }
124
125 pub const fn mid_y(&self) -> f64 {
127 self.y + self.height / 2.0
128 }
129
130 pub fn is_empty(&self) -> bool {
131 self.width <= 0.0 || self.height <= 0.0
132 }
133
134 pub const fn is_null(&self) -> bool {
136 self.x == 0.0 && self.y == 0.0 && self.width == 0.0 && self.height == 0.0
137 }
138}
139
140impl Default for CGRect {
141 fn default() -> Self {
142 Self::zero()
143 }
144}
145
146impl fmt::Display for CGRect {
147 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148 write!(
149 f,
150 "({}, {}, {}, {})",
151 self.x, self.y, self.width, self.height
152 )
153 }
154}