1use bytemuck::{Pod, Zeroable};
2use core::fmt::Debug;
3use derive_more::{Add, AddAssign, Div, Mul, Sub, SubAssign};
4use refineable::Refineable;
5use std::ops::{Add, Mul, Sub, SubAssign};
6
7#[derive(
8 Refineable, Default, Add, AddAssign, Sub, SubAssign, Mul, Div, Copy, Debug, PartialEq, Eq, Hash,
9)]
10#[refineable(debug)]
11#[repr(C)]
12pub struct Point<T: Clone + Debug> {
13 pub x: T,
14 pub y: T,
15}
16
17pub fn point<T: Clone + Debug>(x: T, y: T) -> Point<T> {
18 Point { x, y }
19}
20
21impl<T: Clone + Debug> Point<T> {
22 pub fn new(x: T, y: T) -> Self {
23 Self { x, y }
24 }
25
26 pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Point<U> {
27 Point {
28 x: f(self.x.clone()),
29 y: f(self.y.clone()),
30 }
31 }
32}
33
34impl<T: Clone + Debug + Sub<Output = T>> SubAssign<Size<T>> for Point<T> {
35 fn sub_assign(&mut self, rhs: Size<T>) {
36 self.x = self.x.clone() - rhs.width;
37 self.y = self.y.clone() - rhs.height;
38 }
39}
40
41impl<T: Clone + Debug + std::cmp::PartialOrd> Point<T> {
42 pub fn max(&self, other: &Self) -> Self {
43 Point {
44 x: if self.x >= other.x {
45 self.x.clone()
46 } else {
47 other.x.clone()
48 },
49 y: if self.y >= other.y {
50 self.y.clone()
51 } else {
52 other.y.clone()
53 },
54 }
55 }
56}
57
58impl<T: Clone + Debug> Clone for Point<T> {
59 fn clone(&self) -> Self {
60 Self {
61 x: self.x.clone(),
62 y: self.y.clone(),
63 }
64 }
65}
66
67unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Point<T> {}
68
69unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Point<T> {}
70
71#[derive(Refineable, Default, Clone, Copy, Debug, PartialEq)]
72#[refineable(debug)]
73pub struct Size<T: Clone + Debug> {
74 pub width: T,
75 pub height: T,
76}
77
78pub fn size<T: Clone + Debug>(width: T, height: T) -> Size<T> {
79 Size { width, height }
80}
81
82impl<T: Clone + Debug> Size<T> {
83 pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Size<U> {
84 Size {
85 width: f(self.width.clone()),
86 height: f(self.height.clone()),
87 }
88 }
89}
90
91impl From<Size<Option<Pixels>>> for Size<Option<f32>> {
92 fn from(val: Size<Option<Pixels>>) -> Self {
93 Size {
94 width: val.width.map(|p| p.0 as f32),
95 height: val.height.map(|p| p.0 as f32),
96 }
97 }
98}
99
100impl Size<Length> {
101 pub fn full() -> Self {
102 Self {
103 width: relative(1.).into(),
104 height: relative(1.).into(),
105 }
106 }
107}
108
109impl Size<DefiniteLength> {
110 pub fn zero() -> Self {
111 Self {
112 width: px(0.).into(),
113 height: px(0.).into(),
114 }
115 }
116}
117
118impl Size<Length> {
119 pub fn auto() -> Self {
120 Self {
121 width: Length::Auto,
122 height: Length::Auto,
123 }
124 }
125}
126
127#[derive(Refineable, Clone, Default, Debug, PartialEq)]
128#[refineable(debug)]
129pub struct Bounds<T: Clone + Debug> {
130 pub origin: Point<T>,
131 pub size: Size<T>,
132}
133
134impl<T: Clone + Debug + Copy + Add<T, Output = T>> Bounds<T> {
135 pub fn upper_right(&self) -> Point<T> {
136 Point {
137 x: self.origin.x + self.size.width,
138 y: self.origin.y,
139 }
140 }
141
142 pub fn lower_right(&self) -> Point<T> {
143 Point {
144 x: self.origin.x + self.size.width,
145 y: self.origin.y + self.size.height,
146 }
147 }
148}
149
150impl<T: Clone + Debug + Copy + PartialOrd + Add<T, Output = T>> Bounds<T> {
151 pub fn contains_point(&self, point: Point<T>) -> bool {
152 point.x >= self.origin.x
153 && point.x <= self.origin.x + self.size.width
154 && point.y >= self.origin.y
155 && point.y <= self.origin.y + self.size.height
156 }
157}
158
159impl<T: Clone + Debug + Copy> Copy for Bounds<T> {}
160
161#[derive(Refineable, Clone, Default, Debug)]
162#[refineable(debug)]
163pub struct Edges<T: Clone + Debug> {
164 pub top: T,
165 pub right: T,
166 pub bottom: T,
167 pub left: T,
168}
169
170impl Edges<Length> {
171 pub fn auto() -> Self {
172 Self {
173 top: Length::Auto,
174 right: Length::Auto,
175 bottom: Length::Auto,
176 left: Length::Auto,
177 }
178 }
179
180 pub fn zero() -> Self {
181 Self {
182 top: px(0.).into(),
183 right: px(0.).into(),
184 bottom: px(0.).into(),
185 left: px(0.).into(),
186 }
187 }
188}
189
190impl Edges<DefiniteLength> {
191 pub fn zero() -> Self {
192 Self {
193 top: px(0.).into(),
194 right: px(0.).into(),
195 bottom: px(0.).into(),
196 left: px(0.).into(),
197 }
198 }
199}
200
201impl Edges<AbsoluteLength> {
202 pub fn zero() -> Self {
203 Self {
204 top: px(0.).into(),
205 right: px(0.).into(),
206 bottom: px(0.).into(),
207 left: px(0.).into(),
208 }
209 }
210
211 pub fn to_pixels(&self, rem_size: Pixels) -> Edges<Pixels> {
212 Edges {
213 top: self.top.to_pixels(rem_size),
214 right: self.right.to_pixels(rem_size),
215 bottom: self.bottom.to_pixels(rem_size),
216 left: self.left.to_pixels(rem_size),
217 }
218 }
219}
220
221impl Edges<Pixels> {
222 pub fn is_empty(&self) -> bool {
223 self.top == px(0.) && self.right == px(0.) && self.bottom == px(0.) && self.left == px(0.)
224 }
225}
226
227#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
228#[repr(transparent)]
229pub struct Pixels(pub(crate) f32);
230
231impl Pixels {
232 pub fn round(&self) -> Self {
233 Self(self.0.round())
234 }
235}
236
237impl Mul<f32> for Pixels {
238 type Output = Pixels;
239
240 fn mul(self, other: f32) -> Pixels {
241 Pixels(self.0 * other)
242 }
243}
244
245impl Mul<Pixels> for Pixels {
246 type Output = Pixels;
247
248 fn mul(self, rhs: Pixels) -> Self::Output {
249 Pixels(self.0 * rhs.0)
250 }
251}
252
253impl Eq for Pixels {}
254
255impl Ord for Pixels {
256 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
257 self.0.partial_cmp(&other.0).unwrap()
258 }
259}
260
261impl std::hash::Hash for Pixels {
262 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
263 self.0.to_bits().hash(state);
264 }
265}
266
267impl From<f64> for Pixels {
268 fn from(val: f64) -> Self {
269 Pixels(val as f32)
270 }
271}
272
273impl From<f32> for Pixels {
274 fn from(val: f32) -> Self {
275 Pixels(val)
276 }
277}
278
279unsafe impl bytemuck::Pod for Pixels {}
280unsafe impl bytemuck::Zeroable for Pixels {}
281
282impl Debug for Pixels {
283 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
284 write!(f, "{} px", self.0)
285 }
286}
287
288impl From<Pixels> for f32 {
289 fn from(pixels: Pixels) -> Self {
290 pixels.0
291 }
292}
293
294impl From<&Pixels> for f32 {
295 fn from(pixels: &Pixels) -> Self {
296 pixels.0
297 }
298}
299
300impl From<Pixels> for f64 {
301 fn from(pixels: Pixels) -> Self {
302 pixels.0 as f64
303 }
304}
305
306#[derive(Clone, Copy, Default, Add, Sub, Mul, Div)]
307pub struct Rems(f32);
308
309impl Mul<Pixels> for Rems {
310 type Output = Pixels;
311
312 fn mul(self, other: Pixels) -> Pixels {
313 Pixels(self.0 * other.0)
314 }
315}
316
317impl Debug for Rems {
318 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
319 write!(f, "{} rem", self.0)
320 }
321}
322
323#[derive(Clone, Copy, Debug)]
324pub enum AbsoluteLength {
325 Pixels(Pixels),
326 Rems(Rems),
327}
328
329impl From<Pixels> for AbsoluteLength {
330 fn from(pixels: Pixels) -> Self {
331 AbsoluteLength::Pixels(pixels)
332 }
333}
334
335impl From<Rems> for AbsoluteLength {
336 fn from(rems: Rems) -> Self {
337 AbsoluteLength::Rems(rems)
338 }
339}
340
341impl AbsoluteLength {
342 pub fn to_pixels(&self, rem_size: Pixels) -> Pixels {
343 match self {
344 AbsoluteLength::Pixels(pixels) => *pixels,
345 AbsoluteLength::Rems(rems) => *rems * rem_size,
346 }
347 }
348}
349
350impl Default for AbsoluteLength {
351 fn default() -> Self {
352 px(0.).into()
353 }
354}
355
356/// A non-auto length that can be defined in pixels, rems, or percent of parent.
357#[derive(Clone, Copy)]
358pub enum DefiniteLength {
359 Absolute(AbsoluteLength),
360 /// A fraction of the parent's size between 0 and 1.
361 Fraction(f32),
362}
363
364impl Debug for DefiniteLength {
365 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
366 match self {
367 DefiniteLength::Absolute(length) => Debug::fmt(length, f),
368 DefiniteLength::Fraction(fract) => write!(f, "{}%", (fract * 100.0) as i32),
369 }
370 }
371}
372
373impl From<Pixels> for DefiniteLength {
374 fn from(pixels: Pixels) -> Self {
375 Self::Absolute(pixels.into())
376 }
377}
378
379impl From<Rems> for DefiniteLength {
380 fn from(rems: Rems) -> Self {
381 Self::Absolute(rems.into())
382 }
383}
384
385impl From<AbsoluteLength> for DefiniteLength {
386 fn from(length: AbsoluteLength) -> Self {
387 Self::Absolute(length)
388 }
389}
390
391impl Default for DefiniteLength {
392 fn default() -> Self {
393 Self::Absolute(AbsoluteLength::default())
394 }
395}
396
397/// A length that can be defined in pixels, rems, percent of parent, or auto.
398#[derive(Clone, Copy)]
399pub enum Length {
400 Definite(DefiniteLength),
401 Auto,
402}
403
404impl Debug for Length {
405 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
406 match self {
407 Length::Definite(definite_length) => write!(f, "{:?}", definite_length),
408 Length::Auto => write!(f, "auto"),
409 }
410 }
411}
412
413pub fn relative(fraction: f32) -> DefiniteLength {
414 DefiniteLength::Fraction(fraction).into()
415}
416
417pub fn rems(rems: f32) -> Rems {
418 Rems(rems)
419}
420
421pub fn px(pixels: f32) -> Pixels {
422 Pixels(pixels)
423}
424
425pub fn auto() -> Length {
426 Length::Auto
427}
428
429impl From<Pixels> for Length {
430 fn from(pixels: Pixels) -> Self {
431 Self::Definite(pixels.into())
432 }
433}
434
435impl From<Rems> for Length {
436 fn from(rems: Rems) -> Self {
437 Self::Definite(rems.into())
438 }
439}
440
441impl From<DefiniteLength> for Length {
442 fn from(length: DefiniteLength) -> Self {
443 Self::Definite(length)
444 }
445}
446
447impl From<AbsoluteLength> for Length {
448 fn from(length: AbsoluteLength) -> Self {
449 Self::Definite(length.into())
450 }
451}
452
453impl Default for Length {
454 fn default() -> Self {
455 Self::Definite(DefiniteLength::default())
456 }
457}
458
459impl From<()> for Length {
460 fn from(_: ()) -> Self {
461 Self::Definite(DefiniteLength::default())
462 }
463}