Skip to main content

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