1use bytemuck::{Pod, Zeroable};
2use core::fmt::Debug;
3use derive_more::{Add, AddAssign, Div, Mul, Sub, SubAssign};
4use refineable::Refineable;
5use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Sub, SubAssign};
6
7#[derive(
8 Refineable, Default, Add, AddAssign, Sub, SubAssign, Mul, 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 + Mul<S, Output = T>, S: Clone> MulAssign<S> for Point<T> {
35 fn mul_assign(&mut self, rhs: S) {
36 self.x = self.x.clone() * rhs.clone();
37 self.y = self.y.clone() * rhs;
38 }
39}
40
41impl<T: Clone + Debug + Sub<Output = T>> SubAssign<Size<T>> for Point<T> {
42 fn sub_assign(&mut self, rhs: Size<T>) {
43 self.x = self.x.clone() - rhs.width;
44 self.y = self.y.clone() - rhs.height;
45 }
46}
47
48impl<T: Clone + Debug + Add<Output = T> + Copy> AddAssign<T> for Point<T> {
49 fn add_assign(&mut self, rhs: T) {
50 self.x = self.x.clone() + rhs;
51 self.y = self.y.clone() + rhs;
52 }
53}
54
55impl<T: Clone + Debug + Div<S, Output = T>, S: Clone> Div<S> for Point<T> {
56 type Output = Self;
57
58 fn div(self, rhs: S) -> Self::Output {
59 Self {
60 x: self.x / rhs.clone(),
61 y: self.y / rhs,
62 }
63 }
64}
65
66impl<T: Clone + Debug + std::cmp::PartialOrd> Point<T> {
67 pub fn max(&self, other: &Self) -> Self {
68 Point {
69 x: if self.x >= other.x {
70 self.x.clone()
71 } else {
72 other.x.clone()
73 },
74 y: if self.y >= other.y {
75 self.y.clone()
76 } else {
77 other.y.clone()
78 },
79 }
80 }
81}
82
83impl<T: Clone + Debug> Clone for Point<T> {
84 fn clone(&self) -> Self {
85 Self {
86 x: self.x.clone(),
87 y: self.y.clone(),
88 }
89 }
90}
91
92unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Point<T> {}
93unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Point<T> {}
94
95#[derive(Refineable, Default, Clone, Copy, Debug, PartialEq, Div)]
96#[refineable(debug)]
97#[repr(C)]
98pub struct Size<T: Clone + Debug> {
99 pub width: T,
100 pub height: T,
101}
102
103unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Size<T> {}
104unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Size<T> {}
105
106pub fn size<T: Clone + Debug>(width: T, height: T) -> Size<T> {
107 Size { width, height }
108}
109
110impl<T: Clone + Debug> Size<T> {
111 pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Size<U> {
112 Size {
113 width: f(self.width.clone()),
114 height: f(self.height.clone()),
115 }
116 }
117}
118
119impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Size<T> {
120 fn mul_assign(&mut self, rhs: S) {
121 self.width = self.width.clone() * rhs.clone();
122 self.height = self.height.clone() * rhs;
123 }
124}
125
126impl From<Size<Option<Pixels>>> for Size<Option<f32>> {
127 fn from(val: Size<Option<Pixels>>) -> Self {
128 Size {
129 width: val.width.map(|p| p.0 as f32),
130 height: val.height.map(|p| p.0 as f32),
131 }
132 }
133}
134
135impl Size<Length> {
136 pub fn full() -> Self {
137 Self {
138 width: relative(1.).into(),
139 height: relative(1.).into(),
140 }
141 }
142}
143
144impl Size<DefiniteLength> {
145 pub fn zero() -> Self {
146 Self {
147 width: px(0.).into(),
148 height: px(0.).into(),
149 }
150 }
151}
152
153impl Size<Length> {
154 pub fn auto() -> Self {
155 Self {
156 width: Length::Auto,
157 height: Length::Auto,
158 }
159 }
160}
161
162#[derive(Refineable, Clone, Default, Debug, PartialEq)]
163#[refineable(debug)]
164#[repr(C)]
165pub struct Bounds<T: Clone + Debug> {
166 pub origin: Point<T>,
167 pub size: Size<T>,
168}
169
170unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Bounds<T> {}
171unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Bounds<T> {}
172
173impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Bounds<T> {
174 fn mul_assign(&mut self, rhs: S) {
175 self.origin *= rhs.clone();
176 self.size *= rhs;
177 }
178}
179
180impl<T: Clone + Debug + Div<S, Output = T>, S: Clone> Div<S> for Bounds<T>
181where
182 Size<T>: Div<S, Output = Size<T>>,
183{
184 type Output = Self;
185
186 fn div(self, rhs: S) -> Self {
187 Self {
188 origin: self.origin / rhs.clone(),
189 size: self.size / rhs,
190 }
191 }
192}
193
194impl<T: Clone + Debug + Add<T, Output = T>> Bounds<T> {
195 pub fn upper_right(&self) -> Point<T> {
196 Point {
197 x: self.origin.x.clone() + self.size.width.clone(),
198 y: self.origin.y.clone(),
199 }
200 }
201
202 pub fn lower_right(&self) -> Point<T> {
203 Point {
204 x: self.origin.x.clone() + self.size.width.clone(),
205 y: self.origin.y.clone() + self.size.height.clone(),
206 }
207 }
208}
209
210impl<T: Clone + Debug + PartialOrd + Add<T, Output = T>> Bounds<T> {
211 pub fn contains_point(&self, point: Point<T>) -> bool {
212 point.x >= self.origin.x
213 && point.x <= self.origin.x.clone() + self.size.width.clone()
214 && point.y >= self.origin.y
215 && point.y <= self.origin.y.clone() + self.size.height.clone()
216 }
217
218 pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Bounds<U> {
219 Bounds {
220 origin: self.origin.map(&f),
221 size: self.size.map(f),
222 }
223 }
224}
225
226impl<T: Clone + Debug + Copy> Copy for Bounds<T> {}
227
228#[derive(Refineable, Clone, Default, Debug)]
229#[refineable(debug)]
230#[repr(C)]
231pub struct Edges<T: Clone + Debug> {
232 pub top: T,
233 pub right: T,
234 pub bottom: T,
235 pub left: T,
236}
237
238impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Edges<T> {
239 fn mul_assign(&mut self, rhs: S) {
240 self.top = self.top.clone() * rhs.clone();
241 self.right = self.right.clone() * rhs.clone();
242 self.bottom = self.bottom.clone() * rhs.clone();
243 self.left = self.left.clone() * rhs.clone();
244 }
245}
246
247impl<T: Clone + Debug + Copy> Copy for Edges<T> {}
248
249unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Edges<T> {}
250
251unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Edges<T> {}
252
253impl Edges<Length> {
254 pub fn auto() -> Self {
255 Self {
256 top: Length::Auto,
257 right: Length::Auto,
258 bottom: Length::Auto,
259 left: Length::Auto,
260 }
261 }
262
263 pub fn zero() -> Self {
264 Self {
265 top: px(0.).into(),
266 right: px(0.).into(),
267 bottom: px(0.).into(),
268 left: px(0.).into(),
269 }
270 }
271}
272
273impl Edges<DefiniteLength> {
274 pub fn zero() -> Self {
275 Self {
276 top: px(0.).into(),
277 right: px(0.).into(),
278 bottom: px(0.).into(),
279 left: px(0.).into(),
280 }
281 }
282}
283
284impl Edges<AbsoluteLength> {
285 pub fn zero() -> Self {
286 Self {
287 top: px(0.).into(),
288 right: px(0.).into(),
289 bottom: px(0.).into(),
290 left: px(0.).into(),
291 }
292 }
293
294 pub fn to_pixels(&self, rem_size: Pixels) -> Edges<Pixels> {
295 Edges {
296 top: self.top.to_pixels(rem_size),
297 right: self.right.to_pixels(rem_size),
298 bottom: self.bottom.to_pixels(rem_size),
299 left: self.left.to_pixels(rem_size),
300 }
301 }
302}
303
304impl Edges<Pixels> {
305 pub fn is_empty(&self) -> bool {
306 self.top == px(0.) && self.right == px(0.) && self.bottom == px(0.) && self.left == px(0.)
307 }
308}
309
310#[derive(Refineable, Clone, Default, Debug)]
311#[refineable(debug)]
312#[repr(C)]
313pub struct Corners<T: Clone + Debug> {
314 pub top_left: T,
315 pub top_right: T,
316 pub bottom_right: T,
317 pub bottom_left: T,
318}
319
320impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Corners<T> {
321 fn mul_assign(&mut self, rhs: S) {
322 self.top_left = self.top_left.clone() * rhs.clone();
323 self.top_right = self.top_right.clone() * rhs.clone();
324 self.bottom_right = self.bottom_right.clone() * rhs.clone();
325 self.bottom_left = self.bottom_left.clone() * rhs;
326 }
327}
328
329impl<T: Clone + Debug + Copy> Copy for Corners<T> {}
330
331unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Corners<T> {}
332
333unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Corners<T> {}
334
335#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
336#[repr(transparent)]
337pub struct Pixels(pub(crate) f32);
338
339impl Mul<f32> for Pixels {
340 type Output = Pixels;
341
342 fn mul(self, other: f32) -> Pixels {
343 Pixels(self.0 * other)
344 }
345}
346
347impl Pixels {
348 pub fn round(&self) -> Self {
349 Self(self.0.round())
350 }
351
352 pub fn to_device_pixels(&self, scale: f32) -> DevicePixels {
353 DevicePixels((self.0 * scale).ceil() as u32)
354 }
355}
356
357impl Mul<Pixels> for Pixels {
358 type Output = Pixels;
359
360 fn mul(self, rhs: Pixels) -> Self::Output {
361 Pixels(self.0 * rhs.0)
362 }
363}
364
365impl Eq for Pixels {}
366
367impl Ord for Pixels {
368 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
369 self.0.partial_cmp(&other.0).unwrap()
370 }
371}
372
373impl std::hash::Hash for Pixels {
374 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
375 self.0.to_bits().hash(state);
376 }
377}
378
379impl From<f64> for Pixels {
380 fn from(val: f64) -> Self {
381 Pixels(val as f32)
382 }
383}
384
385impl From<f32> for Pixels {
386 fn from(val: f32) -> Self {
387 Pixels(val)
388 }
389}
390
391unsafe impl bytemuck::Pod for Pixels {}
392unsafe impl bytemuck::Zeroable for Pixels {}
393
394impl Debug for Pixels {
395 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
396 write!(f, "{} px", self.0)
397 }
398}
399
400impl From<Pixels> for f32 {
401 fn from(pixels: Pixels) -> Self {
402 pixels.0
403 }
404}
405
406impl From<&Pixels> for f32 {
407 fn from(pixels: &Pixels) -> Self {
408 pixels.0
409 }
410}
411
412impl From<Pixels> for f64 {
413 fn from(pixels: Pixels) -> Self {
414 pixels.0 as f64
415 }
416}
417
418#[derive(
419 Clone, Copy, Debug, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd,
420)]
421#[repr(transparent)]
422pub struct DevicePixels(pub(crate) u32);
423
424unsafe impl bytemuck::Pod for DevicePixels {}
425unsafe impl bytemuck::Zeroable for DevicePixels {}
426
427impl From<DevicePixels> for u32 {
428 fn from(device_pixels: DevicePixels) -> Self {
429 device_pixels.0
430 }
431}
432
433impl From<u32> for DevicePixels {
434 fn from(val: u32) -> Self {
435 DevicePixels(val)
436 }
437}
438
439#[derive(Clone, Copy, Default, Add, Sub, Mul, Div)]
440pub struct Rems(f32);
441
442impl Mul<Pixels> for Rems {
443 type Output = Pixels;
444
445 fn mul(self, other: Pixels) -> Pixels {
446 Pixels(self.0 * other.0)
447 }
448}
449
450impl Debug for Rems {
451 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
452 write!(f, "{} rem", self.0)
453 }
454}
455
456#[derive(Clone, Copy, Debug)]
457pub enum AbsoluteLength {
458 Pixels(Pixels),
459 Rems(Rems),
460}
461
462impl From<Pixels> for AbsoluteLength {
463 fn from(pixels: Pixels) -> Self {
464 AbsoluteLength::Pixels(pixels)
465 }
466}
467
468impl From<Rems> for AbsoluteLength {
469 fn from(rems: Rems) -> Self {
470 AbsoluteLength::Rems(rems)
471 }
472}
473
474impl AbsoluteLength {
475 pub fn to_pixels(&self, rem_size: Pixels) -> Pixels {
476 match self {
477 AbsoluteLength::Pixels(pixels) => *pixels,
478 AbsoluteLength::Rems(rems) => *rems * rem_size,
479 }
480 }
481}
482
483impl Default for AbsoluteLength {
484 fn default() -> Self {
485 px(0.).into()
486 }
487}
488
489/// A non-auto length that can be defined in pixels, rems, or percent of parent.
490#[derive(Clone, Copy)]
491pub enum DefiniteLength {
492 Absolute(AbsoluteLength),
493 /// A fraction of the parent's size between 0 and 1.
494 Fraction(f32),
495}
496
497impl Debug for DefiniteLength {
498 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
499 match self {
500 DefiniteLength::Absolute(length) => Debug::fmt(length, f),
501 DefiniteLength::Fraction(fract) => write!(f, "{}%", (fract * 100.0) as i32),
502 }
503 }
504}
505
506impl From<Pixels> for DefiniteLength {
507 fn from(pixels: Pixels) -> Self {
508 Self::Absolute(pixels.into())
509 }
510}
511
512impl From<Rems> for DefiniteLength {
513 fn from(rems: Rems) -> Self {
514 Self::Absolute(rems.into())
515 }
516}
517
518impl From<AbsoluteLength> for DefiniteLength {
519 fn from(length: AbsoluteLength) -> Self {
520 Self::Absolute(length)
521 }
522}
523
524impl Default for DefiniteLength {
525 fn default() -> Self {
526 Self::Absolute(AbsoluteLength::default())
527 }
528}
529
530/// A length that can be defined in pixels, rems, percent of parent, or auto.
531#[derive(Clone, Copy)]
532pub enum Length {
533 Definite(DefiniteLength),
534 Auto,
535}
536
537impl Debug for Length {
538 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
539 match self {
540 Length::Definite(definite_length) => write!(f, "{:?}", definite_length),
541 Length::Auto => write!(f, "auto"),
542 }
543 }
544}
545
546pub fn relative(fraction: f32) -> DefiniteLength {
547 DefiniteLength::Fraction(fraction).into()
548}
549
550pub fn rems(rems: f32) -> Rems {
551 Rems(rems)
552}
553
554pub fn px(pixels: f32) -> Pixels {
555 Pixels(pixels)
556}
557
558pub fn auto() -> Length {
559 Length::Auto
560}
561
562impl From<Pixels> for Length {
563 fn from(pixels: Pixels) -> Self {
564 Self::Definite(pixels.into())
565 }
566}
567
568impl From<Rems> for Length {
569 fn from(rems: Rems) -> Self {
570 Self::Definite(rems.into())
571 }
572}
573
574impl From<DefiniteLength> for Length {
575 fn from(length: DefiniteLength) -> Self {
576 Self::Definite(length)
577 }
578}
579
580impl From<AbsoluteLength> for Length {
581 fn from(length: AbsoluteLength) -> Self {
582 Self::Definite(length.into())
583 }
584}
585
586impl Default for Length {
587 fn default() -> Self {
588 Self::Definite(DefiniteLength::default())
589 }
590}
591
592impl From<()> for Length {
593 fn from(_: ()) -> Self {
594 Self::Definite(DefiniteLength::default())
595 }
596}