nsh_core/geometry/
length.rs1use std::fmt;
7
8use super::{
9 Edges, Size,
10 shapes::Corners,
11 units::{IsZero, Percentage, Points, Rems},
12};
13
14#[derive(Clone, Copy, PartialEq, Eq, Hash)]
16pub enum AbsoluteLength {
17 Points(Points),
19 Rems(Rems),
21}
22
23impl AbsoluteLength {
24 #[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#[derive(Clone, Copy, PartialEq, Eq, Hash)]
76pub enum DefiniteLength {
77 Absolute(AbsoluteLength),
79 Fraction(Percentage),
81}
82
83impl DefiniteLength {
84 #[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 #[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#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
154pub enum Length {
155 Definite(DefiniteLength),
157 #[default]
159 Auto,
160}
161
162impl Length {
163 #[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#[inline]
229pub const fn relative(fraction: f32) -> DefiniteLength {
230 DefiniteLength::Fraction(Percentage(fraction))
231}
232
233#[inline]
235pub const fn auto() -> Length {
236 Length::Auto
237}
238
239impl Size<Length> {
240 pub fn auto() -> Self {
242 Size::new(Length::Auto, Length::Auto)
243 }
244
245 pub fn full() -> Self {
247 Size::new(relative(1.0).into(), relative(1.0).into())
248 }
249}
250
251impl Edges<Length> {
252 pub fn auto() -> Self {
254 Edges::all(Length::Auto)
255 }
256
257 pub fn zero() -> Self {
259 Edges::all(Points::ZERO.into())
260 }
261}
262
263impl Edges<DefiniteLength> {
264 pub fn zero() -> Self {
266 Edges::all(Points::ZERO.into())
267 }
268
269 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 pub fn zero() -> Self {
287 Edges::all(Points::ZERO.into())
288 }
289
290 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 pub fn to_points(self, rem_size: Points) -> Corners<Points> {
299 self.map(|l| l.to_points(rem_size))
300 }
301}