Skip to main content

nsh_core/geometry/
length.rs

1//! Lengths as styles give them, from the most to the least resolved:
2//! [`AbsoluteLength`] (points or rems), [`DefiniteLength`] (also a
3//! fraction of a reference length) and [`Length`] (also `auto`). Each
4//! resolves with what it needs: the rem size, the reference length.
5
6use std::fmt;
7
8use super::{
9    Edges, Size,
10    shapes::Corners,
11    units::{IsZero, Percentage, Points, Rems},
12};
13
14/// A length known without layout: points, or rems of a root font size.
15#[derive(Clone, Copy, PartialEq, Eq, Hash)]
16pub enum AbsoluteLength {
17    /// Points.
18    Points(Points),
19    /// Rems.
20    Rems(Rems),
21}
22
23impl AbsoluteLength {
24    /// In points, with a rem of `rem_size`.
25    #[inline]
26    pub fn to_points(self, rem_size: Points) -> Points {
27        match self {
28            AbsoluteLength::Points(p) => p,
29            AbsoluteLength::Rems(r) => r.to_points(rem_size),
30        }
31    }
32}
33
34impl Default for AbsoluteLength {
35    fn default() -> Self {
36        AbsoluteLength::Points(Points::ZERO)
37    }
38}
39
40impl IsZero for AbsoluteLength {
41    fn is_zero(&self) -> bool {
42        match self {
43            AbsoluteLength::Points(p) => p.is_zero(),
44            AbsoluteLength::Rems(r) => r.is_zero(),
45        }
46    }
47}
48
49impl From<Points> for AbsoluteLength {
50    #[inline]
51    fn from(p: Points) -> Self {
52        AbsoluteLength::Points(p)
53    }
54}
55
56impl From<Rems> for AbsoluteLength {
57    #[inline]
58    fn from(r: Rems) -> Self {
59        AbsoluteLength::Rems(r)
60    }
61}
62
63impl fmt::Debug for AbsoluteLength {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        match self {
66            AbsoluteLength::Points(p) => fmt::Debug::fmt(p, f),
67            AbsoluteLength::Rems(r) => fmt::Debug::fmt(r, f),
68        }
69    }
70}
71
72/// A length that is not `auto`: absolute, or a fraction of a reference
73/// length (the parent's size for a box, the gradient line for a color
74/// stop).
75#[derive(Clone, Copy, PartialEq, Eq, Hash)]
76pub enum DefiniteLength {
77    /// Points or rems.
78    Absolute(AbsoluteLength),
79    /// A fraction of the reference length (`0.5` is 50%).
80    Fraction(Percentage),
81}
82
83impl DefiniteLength {
84    /// In points: a fraction of `base_size`, rems of `rem_size`.
85    #[inline]
86    pub fn to_points(self, base_size: AbsoluteLength, rem_size: Points) -> Points {
87        match self {
88            DefiniteLength::Absolute(a) => a.to_points(rem_size),
89            DefiniteLength::Fraction(f) => f.of(base_size.to_points(rem_size)),
90        }
91    }
92
93    /// Whether the length is absolute (it does not depend on a reference).
94    #[inline]
95    pub fn is_absolute(self) -> bool {
96        matches!(self, DefiniteLength::Absolute(_))
97    }
98}
99
100impl Default for DefiniteLength {
101    fn default() -> Self {
102        DefiniteLength::Absolute(AbsoluteLength::default())
103    }
104}
105
106impl IsZero for DefiniteLength {
107    fn is_zero(&self) -> bool {
108        match self {
109            DefiniteLength::Absolute(a) => a.is_zero(),
110            DefiniteLength::Fraction(f) => f.is_zero(),
111        }
112    }
113}
114
115impl From<Points> for DefiniteLength {
116    #[inline]
117    fn from(p: Points) -> Self {
118        DefiniteLength::Absolute(p.into())
119    }
120}
121
122impl From<Rems> for DefiniteLength {
123    #[inline]
124    fn from(r: Rems) -> Self {
125        DefiniteLength::Absolute(r.into())
126    }
127}
128
129impl From<AbsoluteLength> for DefiniteLength {
130    #[inline]
131    fn from(a: AbsoluteLength) -> Self {
132        DefiniteLength::Absolute(a)
133    }
134}
135
136impl From<Percentage> for DefiniteLength {
137    #[inline]
138    fn from(f: Percentage) -> Self {
139        DefiniteLength::Fraction(f)
140    }
141}
142
143impl fmt::Debug for DefiniteLength {
144    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145        match self {
146            DefiniteLength::Absolute(a) => fmt::Debug::fmt(a, f),
147            DefiniteLength::Fraction(fr) => fmt::Debug::fmt(fr, f),
148        }
149    }
150}
151
152/// A length a style gives a box: definite, or `auto` (sized by layout).
153#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
154pub enum Length {
155    /// Points, rems or a fraction of the parent.
156    Definite(DefiniteLength),
157    /// Sized by layout.
158    #[default]
159    Auto,
160}
161
162impl Length {
163    /// In points against `base_size` and `rem_size`; `None` for `auto`.
164    #[inline]
165    pub fn to_points(self, base_size: AbsoluteLength, rem_size: Points) -> Option<Points> {
166        match self {
167            Length::Definite(d) => Some(d.to_points(base_size, rem_size)),
168            Length::Auto => None,
169        }
170    }
171}
172
173impl IsZero for Length {
174    fn is_zero(&self) -> bool {
175        match self {
176            Length::Definite(d) => d.is_zero(),
177            Length::Auto => false,
178        }
179    }
180}
181
182impl From<Points> for Length {
183    #[inline]
184    fn from(p: Points) -> Self {
185        Length::Definite(p.into())
186    }
187}
188
189impl From<Rems> for Length {
190    #[inline]
191    fn from(r: Rems) -> Self {
192        Length::Definite(r.into())
193    }
194}
195
196impl From<AbsoluteLength> for Length {
197    #[inline]
198    fn from(a: AbsoluteLength) -> Self {
199        Length::Definite(a.into())
200    }
201}
202
203impl From<Percentage> for Length {
204    #[inline]
205    fn from(f: Percentage) -> Self {
206        Length::Definite(f.into())
207    }
208}
209
210impl From<DefiniteLength> for Length {
211    #[inline]
212    fn from(d: DefiniteLength) -> Self {
213        Length::Definite(d)
214    }
215}
216
217impl fmt::Debug for Length {
218    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
219        match self {
220            Length::Definite(d) => fmt::Debug::fmt(d, f),
221            Length::Auto => f.write_str("auto"),
222        }
223    }
224}
225
226/// `fraction` of the reference length, the parent's size for a box
227/// (`relative(0.5)` is 50%).
228#[inline]
229pub const fn relative(fraction: f32) -> DefiniteLength {
230    DefiniteLength::Fraction(Percentage(fraction))
231}
232
233/// `auto`: sized by layout.
234#[inline]
235pub const fn auto() -> Length {
236    Length::Auto
237}
238
239impl Size<Length> {
240    /// Both dimensions `auto`.
241    pub fn auto() -> Self {
242        Size::new(Length::Auto, Length::Auto)
243    }
244
245    /// The whole of the parent in both dimensions.
246    pub fn full() -> Self {
247        Size::new(relative(1.0).into(), relative(1.0).into())
248    }
249}
250
251impl Edges<Length> {
252    /// `auto` on every edge.
253    pub fn auto() -> Self {
254        Edges::all(Length::Auto)
255    }
256
257    /// Zero on every edge.
258    pub fn zero() -> Self {
259        Edges::all(Points::ZERO.into())
260    }
261}
262
263impl Edges<DefiniteLength> {
264    /// Zero on every edge.
265    pub fn zero() -> Self {
266        Edges::all(Points::ZERO.into())
267    }
268
269    /// In points: fractions of `parent_size` along each edge's axis (the
270    /// height for the top and bottom edges, as GPUI resolves them; CSS
271    /// resolves a padding's percentages against the width on every edge,
272    /// which a `parent_size` of the width twice gives), rems of
273    /// `rem_size`.
274    pub fn to_points(self, parent_size: Size<AbsoluteLength>, rem_size: Points) -> Edges<Points> {
275        Edges {
276            top: self.top.to_points(parent_size.height, rem_size),
277            right: self.right.to_points(parent_size.width, rem_size),
278            bottom: self.bottom.to_points(parent_size.height, rem_size),
279            left: self.left.to_points(parent_size.width, rem_size),
280        }
281    }
282}
283
284impl Edges<AbsoluteLength> {
285    /// Zero on every edge.
286    pub fn zero() -> Self {
287        Edges::all(Points::ZERO.into())
288    }
289
290    /// In points, with a rem of `rem_size`.
291    pub fn to_points(self, rem_size: Points) -> Edges<Points> {
292        self.map(|l| l.to_points(rem_size))
293    }
294}
295
296impl Corners<AbsoluteLength> {
297    /// In points, with a rem of `rem_size`.
298    pub fn to_points(self, rem_size: Points) -> Corners<Points> {
299        self.map(|l| l.to_points(rem_size))
300    }
301}