euv_core/style/length/impl.rs
1use super::*;
2
3/// Resolution helpers for `Length`.
4impl Length {
5 /// Resolves this length into logical pixels given parent and viewport dimensions.
6 ///
7 /// # Arguments
8 ///
9 /// - `&Self` - Length specification to resolve.
10 /// - `f32` - Parent dimension along the relevant axis.
11 /// - `Size` - Root viewport logical size.
12 ///
13 /// # Returns
14 ///
15 /// - `Option<f32>` - Resolved pixel size, or `None` for `Auto` and `Fraction`.
16 #[must_use]
17 #[inline(always)]
18 pub fn resolve(&self, parent_extent: f32, viewport: Size) -> Option<f32> {
19 match *self {
20 Self::Auto | Self::Fraction(_) => None,
21 Self::Px(px) => Some(px),
22 Self::Percent(pct) => Some(parent_extent * (pct * 0.01)),
23 Self::ViewportWidth(vw) => Some(viewport.get_width() * (vw * 0.01)),
24 Self::ViewportHeight(vh) => Some(viewport.get_height() * (vh * 0.01)),
25 }
26 }
27}
28
29/// Convenience constructors and sums for `Edges`.
30impl Edges {
31 /// Creates uniform edge insets on all four sides.
32 ///
33 /// # Arguments
34 ///
35 /// - `f32` - Uniform inset in logical pixels.
36 ///
37 /// # Returns
38 ///
39 /// - `Self` - Uniform `Edges`.
40 #[must_use]
41 #[inline(always)]
42 pub const fn all(value: f32) -> Self {
43 Self {
44 top: value,
45 right: value,
46 bottom: value,
47 left: value,
48 }
49 }
50
51 /// Creates symmetric vertical and horizontal edge insets.
52 ///
53 /// # Arguments
54 ///
55 /// - `f32` - Vertical (top and bottom) inset.
56 /// - `f32` - Horizontal (left and right) inset.
57 ///
58 /// # Returns
59 ///
60 /// - `Self` - Symmetric `Edges`.
61 #[must_use]
62 #[inline(always)]
63 pub const fn symmetric(vertical: f32, horizontal: f32) -> Self {
64 Self {
65 top: vertical,
66 right: horizontal,
67 bottom: vertical,
68 left: horizontal,
69 }
70 }
71
72 /// Returns total horizontal thickness (`left + right`).
73 ///
74 /// # Arguments
75 ///
76 /// - `&Self` - Edge insets.
77 ///
78 /// # Returns
79 ///
80 /// - `f32` - Combined horizontal inset.
81 #[must_use]
82 #[inline(always)]
83 pub fn horizontal(&self) -> f32 {
84 self.get_left() + self.get_right()
85 }
86
87 /// Returns total vertical thickness (`top + bottom`).
88 ///
89 /// # Arguments
90 ///
91 /// - `&Self` - Edge insets.
92 ///
93 /// # Returns
94 ///
95 /// - `f32` - Combined vertical inset.
96 #[must_use]
97 #[inline(always)]
98 pub fn vertical(&self) -> f32 {
99 self.get_top() + self.get_bottom()
100 }
101}
102
103/// Convenience constructors for `CornerRadius`.
104impl CornerRadius {
105 /// Creates uniform corner radius on all four corners.
106 ///
107 /// # Arguments
108 ///
109 /// - `f32` - Uniform radius in logical pixels.
110 ///
111 /// # Returns
112 ///
113 /// - `Self` - Uniform `CornerRadius`.
114 #[must_use]
115 #[inline(always)]
116 pub const fn all(radius: f32) -> Self {
117 Self {
118 top_left: radius,
119 top_right: radius,
120 bottom_right: radius,
121 bottom_left: radius,
122 }
123 }
124
125 /// Returns the maximum corner radius across all four corners.
126 ///
127 /// # Arguments
128 ///
129 /// - `&Self` - Corner radii.
130 ///
131 /// # Returns
132 ///
133 /// - `f32` - Largest radius in logical pixels.
134 #[must_use]
135 #[inline(always)]
136 pub fn max_radius(&self) -> f32 {
137 self.get_top_left()
138 .max(self.get_top_right())
139 .max(self.get_bottom_right())
140 .max(self.get_bottom_left())
141 }
142}
143
144/// Geometry queries and clipping helpers for `Rect`.
145impl Rect {
146 /// Returns the right edge coordinate (`x + width`).
147 ///
148 /// # Arguments
149 ///
150 /// - `&Self` - Rectangle.
151 ///
152 /// # Returns
153 ///
154 /// - `f32` - Right edge X coordinate.
155 #[must_use]
156 #[inline(always)]
157 pub fn right(&self) -> f32 {
158 self.get_x() + self.get_width()
159 }
160
161 /// Returns the bottom edge coordinate (`y + height`).
162 ///
163 /// # Arguments
164 ///
165 /// - `&Self` - Rectangle.
166 ///
167 /// # Returns
168 ///
169 /// - `f32` - Bottom edge Y coordinate.
170 #[must_use]
171 #[inline(always)]
172 pub fn bottom(&self) -> f32 {
173 self.get_y() + self.get_height()
174 }
175
176 /// Tests whether `point` lies inside this rectangle.
177 ///
178 /// # Arguments
179 ///
180 /// - `&Self` - Rectangle bounds.
181 /// - `Point` - Point to test.
182 ///
183 /// # Returns
184 ///
185 /// - `bool` - True when the point is within `[x, right)` and `[y, bottom)`.
186 #[must_use]
187 #[inline(always)]
188 pub fn contains(&self, point: Point) -> bool {
189 point.get_x() >= self.get_x()
190 && point.get_x() < self.right()
191 && point.get_y() >= self.get_y()
192 && point.get_y() < self.bottom()
193 }
194
195 /// Computes the intersection rectangle of `self` and `other`.
196 ///
197 /// # Arguments
198 ///
199 /// - `&Self` - First rectangle.
200 /// - `Self` - Second rectangle.
201 ///
202 /// # Returns
203 ///
204 /// - `Option<Self>` - Non-empty overlapping rectangle, or `None` when disjoint.
205 #[must_use]
206 #[inline(always)]
207 pub fn intersect(&self, other: Self) -> Option<Self> {
208 let x0: f32 = self.get_x().max(other.get_x());
209 let y0: f32 = self.get_y().max(other.get_y());
210 let x1: f32 = self.right().min(other.right());
211 let y1: f32 = self.bottom().min(other.bottom());
212 if x1 > x0 && y1 > y0 {
213 Some(Self::new(x0, y0, x1 - x0, y1 - y0))
214 } else {
215 None
216 }
217 }
218
219 /// Computes the bounding union of `self` and `other`.
220 ///
221 /// # Arguments
222 ///
223 /// - `&Self` - First rectangle.
224 /// - `Self` - Second rectangle.
225 ///
226 /// # Returns
227 ///
228 /// - `Self` - Enclosing rectangle containing both inputs.
229 #[must_use]
230 #[inline(always)]
231 pub fn union(&self, other: Self) -> Self {
232 if self.get_width() <= 0.0 || self.get_height() <= 0.0 {
233 return other;
234 }
235 if other.get_width() <= 0.0 || other.get_height() <= 0.0 {
236 return *self;
237 }
238 let x0: f32 = self.get_x().min(other.get_x());
239 let y0: f32 = self.get_y().min(other.get_y());
240 let x1: f32 = self.right().max(other.right());
241 let y1: f32 = self.bottom().max(other.bottom());
242 Self::new(x0, y0, x1 - x0, y1 - y0)
243 }
244
245 /// Insets the rectangle by `edges`, clamping width and height to non-negative values.
246 ///
247 /// # Arguments
248 ///
249 /// - `&Self` - Outer rectangle.
250 /// - `Edges` - Edge insets to subtract.
251 ///
252 /// # Returns
253 ///
254 /// - `Self` - Inset inner rectangle.
255 #[must_use]
256 #[inline(always)]
257 pub fn inset(&self, edges: Edges) -> Self {
258 let w: f32 = (self.get_width() - edges.horizontal()).max(0.0);
259 let h: f32 = (self.get_height() - edges.vertical()).max(0.0);
260 Self::new(
261 self.get_x() + edges.get_left(),
262 self.get_y() + edges.get_top(),
263 w,
264 h,
265 )
266 }
267}