1use core::fmt::Debug;
2use derive_more::{Add, AddAssign, Div, DivAssign, Mul, Neg, Sub, SubAssign};
3use refineable::Refineable;
4use serde_derive::{Deserialize, Serialize};
5use std::{
6 cmp::{self, PartialOrd},
7 fmt,
8 ops::{Add, Div, Mul, MulAssign, Sub},
9};
10
11#[derive(Refineable, Default, Add, AddAssign, Sub, SubAssign, Copy, Debug, PartialEq, Eq, Hash)]
12#[refineable(Debug)]
13#[repr(C)]
14pub struct Point<T: Default + Clone + Debug> {
15 pub x: T,
16 pub y: T,
17}
18
19pub fn point<T: Clone + Debug + Default>(x: T, y: T) -> Point<T> {
20 Point { x, y }
21}
22
23impl<T: Clone + Debug + Default> Point<T> {
24 pub const fn new(x: T, y: T) -> Self {
25 Self { x, y }
26 }
27
28 pub fn zero() -> Self {
29 Self::new(T::default(), T::default())
30 }
31
32 pub fn map<U: Clone + Default + Debug>(&self, f: impl Fn(T) -> U) -> Point<U> {
33 Point {
34 x: f(self.x.clone()),
35 y: f(self.y.clone()),
36 }
37 }
38}
39
40impl Point<Pixels> {
41 pub fn scale(&self, factor: f32) -> Point<ScaledPixels> {
42 Point {
43 x: self.x.scale(factor),
44 y: self.y.scale(factor),
45 }
46 }
47
48 pub fn magnitude(&self) -> f64 {
49 ((self.x.0.powi(2) + self.y.0.powi(2)) as f64).sqrt()
50 }
51}
52
53impl<T, Rhs> Mul<Rhs> for Point<T>
54where
55 T: Mul<Rhs, Output = T> + Clone + Default + Debug,
56 Rhs: Clone + Debug,
57{
58 type Output = Point<T>;
59
60 fn mul(self, rhs: Rhs) -> Self::Output {
61 Point {
62 x: self.x * rhs.clone(),
63 y: self.y * rhs,
64 }
65 }
66}
67
68impl<T, S> MulAssign<S> for Point<T>
69where
70 T: Clone + Mul<S, Output = T> + Default + Debug,
71 S: Clone,
72{
73 fn mul_assign(&mut self, rhs: S) {
74 self.x = self.x.clone() * rhs.clone();
75 self.y = self.y.clone() * rhs;
76 }
77}
78
79impl<T, S> Div<S> for Point<T>
80where
81 T: Div<S, Output = T> + Clone + Default + Debug,
82 S: Clone,
83{
84 type Output = Self;
85
86 fn div(self, rhs: S) -> Self::Output {
87 Self {
88 x: self.x / rhs.clone(),
89 y: self.y / rhs,
90 }
91 }
92}
93
94impl<T> Point<T>
95where
96 T: PartialOrd + Clone + Default + Debug,
97{
98 pub fn max(&self, other: &Self) -> Self {
99 Point {
100 x: if self.x >= other.x {
101 self.x.clone()
102 } else {
103 other.x.clone()
104 },
105 y: if self.y >= other.y {
106 self.y.clone()
107 } else {
108 other.y.clone()
109 },
110 }
111 }
112
113 pub fn min(&self, other: &Self) -> Self {
114 Point {
115 x: if self.x <= other.x {
116 self.x.clone()
117 } else {
118 other.x.clone()
119 },
120 y: if self.y <= other.y {
121 self.y.clone()
122 } else {
123 other.y.clone()
124 },
125 }
126 }
127
128 pub fn clamp(&self, min: &Self, max: &Self) -> Self {
129 self.max(min).min(max)
130 }
131}
132
133impl<T: Clone + Default + Debug> Clone for Point<T> {
134 fn clone(&self) -> Self {
135 Self {
136 x: self.x.clone(),
137 y: self.y.clone(),
138 }
139 }
140}
141
142#[derive(Refineable, Default, Clone, Copy, PartialEq, Div, Hash, Serialize, Deserialize)]
143#[refineable(Debug)]
144#[repr(C)]
145pub struct Size<T: Clone + Default + Debug> {
146 pub width: T,
147 pub height: T,
148}
149
150pub fn size<T>(width: T, height: T) -> Size<T>
151where
152 T: Clone + Default + Debug,
153{
154 Size { width, height }
155}
156
157impl<T> Size<T>
158where
159 T: Clone + Default + Debug,
160{
161 pub fn map<U>(&self, f: impl Fn(T) -> U) -> Size<U>
162 where
163 U: Clone + Default + Debug,
164 {
165 Size {
166 width: f(self.width.clone()),
167 height: f(self.height.clone()),
168 }
169 }
170}
171
172impl Size<Pixels> {
173 pub fn scale(&self, factor: f32) -> Size<ScaledPixels> {
174 Size {
175 width: self.width.scale(factor),
176 height: self.height.scale(factor),
177 }
178 }
179}
180
181impl<T> Size<T>
182where
183 T: PartialOrd + Clone + Default + Debug,
184{
185 pub fn max(&self, other: &Self) -> Self {
186 Size {
187 width: if self.width >= other.width {
188 self.width.clone()
189 } else {
190 other.width.clone()
191 },
192 height: if self.height >= other.height {
193 self.height.clone()
194 } else {
195 other.height.clone()
196 },
197 }
198 }
199}
200
201impl<T> Sub for Size<T>
202where
203 T: Sub<Output = T> + Clone + Default + Debug,
204{
205 type Output = Size<T>;
206
207 fn sub(self, rhs: Self) -> Self::Output {
208 Size {
209 width: self.width - rhs.width,
210 height: self.height - rhs.height,
211 }
212 }
213}
214
215impl<T, Rhs> Mul<Rhs> for Size<T>
216where
217 T: Mul<Rhs, Output = Rhs> + Clone + Default + Debug,
218 Rhs: Clone + Default + Debug,
219{
220 type Output = Size<Rhs>;
221
222 fn mul(self, rhs: Rhs) -> Self::Output {
223 Size {
224 width: self.width * rhs.clone(),
225 height: self.height * rhs,
226 }
227 }
228}
229
230impl<T, S> MulAssign<S> for Size<T>
231where
232 T: Mul<S, Output = T> + Clone + Default + Debug,
233 S: Clone,
234{
235 fn mul_assign(&mut self, rhs: S) {
236 self.width = self.width.clone() * rhs.clone();
237 self.height = self.height.clone() * rhs;
238 }
239}
240
241impl<T> Eq for Size<T> where T: Eq + Default + Debug + Clone {}
242
243impl<T> Debug for Size<T>
244where
245 T: Clone + Default + Debug,
246{
247 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
248 write!(f, "Size {{ {:?} × {:?} }}", self.width, self.height)
249 }
250}
251
252impl<T: Clone + Default + Debug> From<Point<T>> for Size<T> {
253 fn from(point: Point<T>) -> Self {
254 Self {
255 width: point.x,
256 height: point.y,
257 }
258 }
259}
260
261impl From<Size<Pixels>> for Size<GlobalPixels> {
262 fn from(size: Size<Pixels>) -> Self {
263 Size {
264 width: GlobalPixels(size.width.0),
265 height: GlobalPixels(size.height.0),
266 }
267 }
268}
269
270impl From<Size<Pixels>> for Size<DefiniteLength> {
271 fn from(size: Size<Pixels>) -> Self {
272 Size {
273 width: size.width.into(),
274 height: size.height.into(),
275 }
276 }
277}
278
279impl From<Size<Pixels>> for Size<AbsoluteLength> {
280 fn from(size: Size<Pixels>) -> Self {
281 Size {
282 width: size.width.into(),
283 height: size.height.into(),
284 }
285 }
286}
287
288impl Size<Length> {
289 pub fn full() -> Self {
290 Self {
291 width: relative(1.).into(),
292 height: relative(1.).into(),
293 }
294 }
295}
296
297impl Size<DefiniteLength> {
298 pub fn zero() -> Self {
299 Self {
300 width: px(0.).into(),
301 height: px(0.).into(),
302 }
303 }
304}
305
306impl Size<Length> {
307 pub fn auto() -> Self {
308 Self {
309 width: Length::Auto,
310 height: Length::Auto,
311 }
312 }
313}
314
315#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
316#[refineable(Debug)]
317#[repr(C)]
318pub struct Bounds<T: Clone + Default + Debug> {
319 pub origin: Point<T>,
320 pub size: Size<T>,
321}
322
323impl<T> Bounds<T>
324where
325 T: Clone + Debug + Sub<Output = T> + Default,
326{
327 pub fn from_corners(upper_left: Point<T>, lower_right: Point<T>) -> Self {
328 let origin = Point {
329 x: upper_left.x.clone(),
330 y: upper_left.y.clone(),
331 };
332 let size = Size {
333 width: lower_right.x - upper_left.x,
334 height: lower_right.y - upper_left.y,
335 };
336 Bounds { origin, size }
337 }
338
339 pub fn new(origin: Point<T>, size: Size<T>) -> Self {
340 Bounds { origin, size }
341 }
342}
343
344impl<T> Bounds<T>
345where
346 T: Clone + Debug + PartialOrd + Add<T, Output = T> + Sub<Output = T> + Default + Half,
347{
348 pub fn intersects(&self, other: &Bounds<T>) -> bool {
349 let my_lower_right = self.lower_right();
350 let their_lower_right = other.lower_right();
351
352 self.origin.x < their_lower_right.x
353 && my_lower_right.x > other.origin.x
354 && self.origin.y < their_lower_right.y
355 && my_lower_right.y > other.origin.y
356 }
357
358 pub fn dilate(&mut self, amount: T) {
359 self.origin.x = self.origin.x.clone() - amount.clone();
360 self.origin.y = self.origin.y.clone() - amount.clone();
361 let double_amount = amount.clone() + amount;
362 self.size.width = self.size.width.clone() + double_amount.clone();
363 self.size.height = self.size.height.clone() + double_amount;
364 }
365
366 pub fn center(&self) -> Point<T> {
367 Point {
368 x: self.origin.x.clone() + self.size.width.clone().half(),
369 y: self.origin.y.clone() + self.size.height.clone().half(),
370 }
371 }
372}
373
374impl<T: Clone + Default + Debug + PartialOrd + Add<T, Output = T> + Sub<Output = T>> Bounds<T> {
375 pub fn intersect(&self, other: &Self) -> Self {
376 let upper_left = self.origin.max(&other.origin);
377 let lower_right = self.lower_right().min(&other.lower_right());
378 Self::from_corners(upper_left, lower_right)
379 }
380
381 pub fn union(&self, other: &Self) -> Self {
382 let top_left = self.origin.min(&other.origin);
383 let bottom_right = self.lower_right().max(&other.lower_right());
384 Bounds::from_corners(top_left, bottom_right)
385 }
386}
387
388impl<T, Rhs> Mul<Rhs> for Bounds<T>
389where
390 T: Mul<Rhs, Output = Rhs> + Clone + Default + Debug,
391 Point<T>: Mul<Rhs, Output = Point<Rhs>>,
392 Rhs: Clone + Default + Debug,
393{
394 type Output = Bounds<Rhs>;
395
396 fn mul(self, rhs: Rhs) -> Self::Output {
397 Bounds {
398 origin: self.origin * rhs.clone(),
399 size: self.size * rhs,
400 }
401 }
402}
403
404impl<T, S> MulAssign<S> for Bounds<T>
405where
406 T: Mul<S, Output = T> + Clone + Default + Debug,
407 S: Clone,
408{
409 fn mul_assign(&mut self, rhs: S) {
410 self.origin *= rhs.clone();
411 self.size *= rhs;
412 }
413}
414
415impl<T, S> Div<S> for Bounds<T>
416where
417 Size<T>: Div<S, Output = Size<T>>,
418 T: Div<S, Output = T> + Default + Clone + Debug,
419 S: Clone,
420{
421 type Output = Self;
422
423 fn div(self, rhs: S) -> Self {
424 Self {
425 origin: self.origin / rhs.clone(),
426 size: self.size / rhs,
427 }
428 }
429}
430
431impl<T> Bounds<T>
432where
433 T: Add<T, Output = T> + Clone + Default + Debug,
434{
435 pub fn top(&self) -> T {
436 self.origin.y.clone()
437 }
438
439 pub fn bottom(&self) -> T {
440 self.origin.y.clone() + self.size.height.clone()
441 }
442
443 pub fn left(&self) -> T {
444 self.origin.x.clone()
445 }
446
447 pub fn right(&self) -> T {
448 self.origin.x.clone() + self.size.width.clone()
449 }
450
451 pub fn upper_right(&self) -> Point<T> {
452 Point {
453 x: self.origin.x.clone() + self.size.width.clone(),
454 y: self.origin.y.clone(),
455 }
456 }
457
458 pub fn lower_right(&self) -> Point<T> {
459 Point {
460 x: self.origin.x.clone() + self.size.width.clone(),
461 y: self.origin.y.clone() + self.size.height.clone(),
462 }
463 }
464
465 pub fn lower_left(&self) -> Point<T> {
466 Point {
467 x: self.origin.x.clone(),
468 y: self.origin.y.clone() + self.size.height.clone(),
469 }
470 }
471}
472
473impl<T> Bounds<T>
474where
475 T: Add<T, Output = T> + PartialOrd + Clone + Default + Debug,
476{
477 pub fn contains_point(&self, point: &Point<T>) -> bool {
478 point.x >= self.origin.x
479 && point.x <= self.origin.x.clone() + self.size.width.clone()
480 && point.y >= self.origin.y
481 && point.y <= self.origin.y.clone() + self.size.height.clone()
482 }
483
484 pub fn map<U>(&self, f: impl Fn(T) -> U) -> Bounds<U>
485 where
486 U: Clone + Default + Debug,
487 {
488 Bounds {
489 origin: self.origin.map(&f),
490 size: self.size.map(f),
491 }
492 }
493}
494
495impl Bounds<Pixels> {
496 pub fn scale(&self, factor: f32) -> Bounds<ScaledPixels> {
497 Bounds {
498 origin: self.origin.scale(factor),
499 size: self.size.scale(factor),
500 }
501 }
502}
503
504impl<T: Clone + Debug + Copy + Default> Copy for Bounds<T> {}
505
506#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
507#[refineable(Debug)]
508#[repr(C)]
509pub struct Edges<T: Clone + Default + Debug> {
510 pub top: T,
511 pub right: T,
512 pub bottom: T,
513 pub left: T,
514}
515
516impl<T> Mul for Edges<T>
517where
518 T: Mul<Output = T> + Clone + Default + Debug,
519{
520 type Output = Self;
521
522 fn mul(self, rhs: Self) -> Self::Output {
523 Self {
524 top: self.top.clone() * rhs.top,
525 right: self.right.clone() * rhs.right,
526 bottom: self.bottom.clone() * rhs.bottom,
527 left: self.left.clone() * rhs.left,
528 }
529 }
530}
531
532impl<T, S> MulAssign<S> for Edges<T>
533where
534 T: Mul<S, Output = T> + Clone + Default + Debug,
535 S: Clone,
536{
537 fn mul_assign(&mut self, rhs: S) {
538 self.top = self.top.clone() * rhs.clone();
539 self.right = self.right.clone() * rhs.clone();
540 self.bottom = self.bottom.clone() * rhs.clone();
541 self.left = self.left.clone() * rhs;
542 }
543}
544
545impl<T: Clone + Default + Debug + Copy> Copy for Edges<T> {}
546
547impl<T: Clone + Default + Debug> Edges<T> {
548 pub fn all(value: T) -> Self {
549 Self {
550 top: value.clone(),
551 right: value.clone(),
552 bottom: value.clone(),
553 left: value,
554 }
555 }
556
557 pub fn map<U>(&self, f: impl Fn(&T) -> U) -> Edges<U>
558 where
559 U: Clone + Default + Debug,
560 {
561 Edges {
562 top: f(&self.top),
563 right: f(&self.right),
564 bottom: f(&self.bottom),
565 left: f(&self.left),
566 }
567 }
568
569 pub fn any<F: Fn(&T) -> bool>(&self, predicate: F) -> bool {
570 predicate(&self.top)
571 || predicate(&self.right)
572 || predicate(&self.bottom)
573 || predicate(&self.left)
574 }
575}
576
577impl Edges<Length> {
578 pub fn auto() -> Self {
579 Self {
580 top: Length::Auto,
581 right: Length::Auto,
582 bottom: Length::Auto,
583 left: Length::Auto,
584 }
585 }
586
587 pub fn zero() -> Self {
588 Self {
589 top: px(0.).into(),
590 right: px(0.).into(),
591 bottom: px(0.).into(),
592 left: px(0.).into(),
593 }
594 }
595}
596
597impl Edges<DefiniteLength> {
598 pub fn zero() -> Self {
599 Self {
600 top: px(0.).into(),
601 right: px(0.).into(),
602 bottom: px(0.).into(),
603 left: px(0.).into(),
604 }
605 }
606
607 pub fn to_pixels(&self, parent_size: Size<AbsoluteLength>, rem_size: Pixels) -> Edges<Pixels> {
608 Edges {
609 top: self.top.to_pixels(parent_size.height, rem_size),
610 right: self.right.to_pixels(parent_size.width, rem_size),
611 bottom: self.bottom.to_pixels(parent_size.height, rem_size),
612 left: self.left.to_pixels(parent_size.width, rem_size),
613 }
614 }
615}
616
617impl Edges<AbsoluteLength> {
618 pub fn zero() -> Self {
619 Self {
620 top: px(0.).into(),
621 right: px(0.).into(),
622 bottom: px(0.).into(),
623 left: px(0.).into(),
624 }
625 }
626
627 pub fn to_pixels(&self, rem_size: Pixels) -> Edges<Pixels> {
628 Edges {
629 top: self.top.to_pixels(rem_size),
630 right: self.right.to_pixels(rem_size),
631 bottom: self.bottom.to_pixels(rem_size),
632 left: self.left.to_pixels(rem_size),
633 }
634 }
635}
636
637impl Edges<Pixels> {
638 pub fn scale(&self, factor: f32) -> Edges<ScaledPixels> {
639 Edges {
640 top: self.top.scale(factor),
641 right: self.right.scale(factor),
642 bottom: self.bottom.scale(factor),
643 left: self.left.scale(factor),
644 }
645 }
646}
647
648#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
649#[refineable(Debug)]
650#[repr(C)]
651pub struct Corners<T: Clone + Default + Debug> {
652 pub top_left: T,
653 pub top_right: T,
654 pub bottom_right: T,
655 pub bottom_left: T,
656}
657
658impl Corners<AbsoluteLength> {
659 pub fn to_pixels(&self, size: Size<Pixels>, rem_size: Pixels) -> Corners<Pixels> {
660 let max = size.width.max(size.height) / 2.;
661 Corners {
662 top_left: self.top_left.to_pixels(rem_size).min(max),
663 top_right: self.top_right.to_pixels(rem_size).min(max),
664 bottom_right: self.bottom_right.to_pixels(rem_size).min(max),
665 bottom_left: self.bottom_left.to_pixels(rem_size).min(max),
666 }
667 }
668}
669
670impl Corners<Pixels> {
671 pub fn scale(&self, factor: f32) -> Corners<ScaledPixels> {
672 Corners {
673 top_left: self.top_left.scale(factor),
674 top_right: self.top_right.scale(factor),
675 bottom_right: self.bottom_right.scale(factor),
676 bottom_left: self.bottom_left.scale(factor),
677 }
678 }
679}
680
681impl<T: Clone + Default + Debug> Corners<T> {
682 pub fn map<U>(&self, f: impl Fn(&T) -> U) -> Corners<U>
683 where
684 U: Clone + Default + Debug,
685 {
686 Corners {
687 top_left: f(&self.top_left),
688 top_right: f(&self.top_right),
689 bottom_right: f(&self.bottom_right),
690 bottom_left: f(&self.bottom_left),
691 }
692 }
693}
694
695impl<T> Mul for Corners<T>
696where
697 T: Mul<Output = T> + Clone + Default + Debug,
698{
699 type Output = Self;
700
701 fn mul(self, rhs: Self) -> Self::Output {
702 Self {
703 top_left: self.top_left.clone() * rhs.top_left,
704 top_right: self.top_right.clone() * rhs.top_right,
705 bottom_right: self.bottom_right.clone() * rhs.bottom_right,
706 bottom_left: self.bottom_left.clone() * rhs.bottom_left,
707 }
708 }
709}
710
711impl<T, S> MulAssign<S> for Corners<T>
712where
713 T: Mul<S, Output = T> + Clone + Default + Debug,
714 S: Clone,
715{
716 fn mul_assign(&mut self, rhs: S) {
717 self.top_left = self.top_left.clone() * rhs.clone();
718 self.top_right = self.top_right.clone() * rhs.clone();
719 self.bottom_right = self.bottom_right.clone() * rhs.clone();
720 self.bottom_left = self.bottom_left.clone() * rhs;
721 }
722}
723
724impl<T> Copy for Corners<T> where T: Copy + Clone + Default + Debug {}
725
726#[derive(
727 Clone,
728 Copy,
729 Default,
730 Add,
731 AddAssign,
732 Sub,
733 SubAssign,
734 Neg,
735 Div,
736 DivAssign,
737 PartialEq,
738 PartialOrd,
739 Serialize,
740 Deserialize,
741)]
742#[repr(transparent)]
743pub struct Pixels(pub(crate) f32);
744
745impl std::ops::Div for Pixels {
746 type Output = f32;
747
748 fn div(self, rhs: Self) -> Self::Output {
749 self.0 / rhs.0
750 }
751}
752
753impl std::ops::DivAssign for Pixels {
754 fn div_assign(&mut self, rhs: Self) {
755 *self = Self(self.0 / rhs.0);
756 }
757}
758
759impl std::ops::RemAssign for Pixels {
760 fn rem_assign(&mut self, rhs: Self) {
761 self.0 %= rhs.0;
762 }
763}
764
765impl std::ops::Rem for Pixels {
766 type Output = Self;
767
768 fn rem(self, rhs: Self) -> Self {
769 Self(self.0 % rhs.0)
770 }
771}
772
773impl Mul<f32> for Pixels {
774 type Output = Pixels;
775
776 fn mul(self, other: f32) -> Pixels {
777 Pixels(self.0 * other)
778 }
779}
780
781impl Mul<usize> for Pixels {
782 type Output = Pixels;
783
784 fn mul(self, other: usize) -> Pixels {
785 Pixels(self.0 * other as f32)
786 }
787}
788
789impl Mul<Pixels> for f32 {
790 type Output = Pixels;
791
792 fn mul(self, rhs: Pixels) -> Self::Output {
793 Pixels(self * rhs.0)
794 }
795}
796
797impl MulAssign<f32> for Pixels {
798 fn mul_assign(&mut self, other: f32) {
799 self.0 *= other;
800 }
801}
802
803impl Pixels {
804 pub const ZERO: Pixels = Pixels(0.0);
805 pub const MAX: Pixels = Pixels(f32::MAX);
806
807 pub fn floor(&self) -> Self {
808 Self(self.0.floor())
809 }
810
811 pub fn round(&self) -> Self {
812 Self(self.0.round())
813 }
814
815 pub fn ceil(&self) -> Self {
816 Self(self.0.ceil())
817 }
818
819 pub fn scale(&self, factor: f32) -> ScaledPixels {
820 ScaledPixels(self.0 * factor)
821 }
822
823 pub fn pow(&self, exponent: f32) -> Self {
824 Self(self.0.powf(exponent))
825 }
826
827 pub fn abs(&self) -> Self {
828 Self(self.0.abs())
829 }
830}
831
832impl Mul<Pixels> for Pixels {
833 type Output = Pixels;
834
835 fn mul(self, rhs: Pixels) -> Self::Output {
836 Pixels(self.0 * rhs.0)
837 }
838}
839
840impl Eq for Pixels {}
841
842impl Ord for Pixels {
843 fn cmp(&self, other: &Self) -> cmp::Ordering {
844 self.0.partial_cmp(&other.0).unwrap()
845 }
846}
847
848impl std::hash::Hash for Pixels {
849 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
850 self.0.to_bits().hash(state);
851 }
852}
853
854impl From<f64> for Pixels {
855 fn from(pixels: f64) -> Self {
856 Pixels(pixels as f32)
857 }
858}
859
860impl From<f32> for Pixels {
861 fn from(pixels: f32) -> Self {
862 Pixels(pixels)
863 }
864}
865
866impl Debug for Pixels {
867 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
868 write!(f, "{} px", self.0)
869 }
870}
871
872impl From<Pixels> for f32 {
873 fn from(pixels: Pixels) -> Self {
874 pixels.0
875 }
876}
877
878impl From<&Pixels> for f32 {
879 fn from(pixels: &Pixels) -> Self {
880 pixels.0
881 }
882}
883
884impl From<Pixels> for f64 {
885 fn from(pixels: Pixels) -> Self {
886 pixels.0 as f64
887 }
888}
889
890impl From<Pixels> for u32 {
891 fn from(pixels: Pixels) -> Self {
892 pixels.0 as u32
893 }
894}
895
896impl From<u32> for Pixels {
897 fn from(pixels: u32) -> Self {
898 Pixels(pixels as f32)
899 }
900}
901
902impl From<Pixels> for usize {
903 fn from(pixels: Pixels) -> Self {
904 pixels.0 as usize
905 }
906}
907
908impl From<usize> for Pixels {
909 fn from(pixels: usize) -> Self {
910 Pixels(pixels as f32)
911 }
912}
913
914#[derive(
915 Add, AddAssign, Clone, Copy, Default, Div, Eq, Hash, Ord, PartialEq, PartialOrd, Sub, SubAssign,
916)]
917#[repr(transparent)]
918pub struct DevicePixels(pub(crate) i32);
919
920impl DevicePixels {
921 pub fn to_bytes(&self, bytes_per_pixel: u8) -> u32 {
922 self.0 as u32 * bytes_per_pixel as u32
923 }
924}
925
926impl fmt::Debug for DevicePixels {
927 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
928 write!(f, "{} px (device)", self.0)
929 }
930}
931
932impl From<DevicePixels> for i32 {
933 fn from(device_pixels: DevicePixels) -> Self {
934 device_pixels.0
935 }
936}
937
938impl From<i32> for DevicePixels {
939 fn from(device_pixels: i32) -> Self {
940 DevicePixels(device_pixels)
941 }
942}
943
944impl From<u32> for DevicePixels {
945 fn from(device_pixels: u32) -> Self {
946 DevicePixels(device_pixels as i32)
947 }
948}
949
950impl From<DevicePixels> for u32 {
951 fn from(device_pixels: DevicePixels) -> Self {
952 device_pixels.0 as u32
953 }
954}
955
956impl From<DevicePixels> for u64 {
957 fn from(device_pixels: DevicePixels) -> Self {
958 device_pixels.0 as u64
959 }
960}
961
962impl From<u64> for DevicePixels {
963 fn from(device_pixels: u64) -> Self {
964 DevicePixels(device_pixels as i32)
965 }
966}
967
968impl From<DevicePixels> for usize {
969 fn from(device_pixels: DevicePixels) -> Self {
970 device_pixels.0 as usize
971 }
972}
973
974impl From<usize> for DevicePixels {
975 fn from(device_pixels: usize) -> Self {
976 DevicePixels(device_pixels as i32)
977 }
978}
979
980#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
981#[repr(transparent)]
982pub struct ScaledPixels(pub(crate) f32);
983
984impl ScaledPixels {
985 pub fn floor(&self) -> Self {
986 Self(self.0.floor())
987 }
988
989 pub fn ceil(&self) -> Self {
990 Self(self.0.ceil())
991 }
992}
993
994impl Eq for ScaledPixels {}
995
996impl Debug for ScaledPixels {
997 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
998 write!(f, "{} px (scaled)", self.0)
999 }
1000}
1001
1002impl From<ScaledPixels> for DevicePixels {
1003 fn from(scaled: ScaledPixels) -> Self {
1004 DevicePixels(scaled.0.ceil() as i32)
1005 }
1006}
1007
1008impl From<DevicePixels> for ScaledPixels {
1009 fn from(device: DevicePixels) -> Self {
1010 ScaledPixels(device.0 as f32)
1011 }
1012}
1013
1014impl From<ScaledPixels> for f64 {
1015 fn from(scaled_pixels: ScaledPixels) -> Self {
1016 scaled_pixels.0 as f64
1017 }
1018}
1019
1020#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
1021#[repr(transparent)]
1022pub struct GlobalPixels(pub(crate) f32);
1023
1024impl Debug for GlobalPixels {
1025 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1026 write!(f, "{} px (global coordinate space)", self.0)
1027 }
1028}
1029
1030impl From<GlobalPixels> for f64 {
1031 fn from(global_pixels: GlobalPixels) -> Self {
1032 global_pixels.0 as f64
1033 }
1034}
1035
1036impl From<f64> for GlobalPixels {
1037 fn from(global_pixels: f64) -> Self {
1038 GlobalPixels(global_pixels as f32)
1039 }
1040}
1041
1042impl sqlez::bindable::StaticColumnCount for GlobalPixels {}
1043
1044impl sqlez::bindable::Bind for GlobalPixels {
1045 fn bind(
1046 &self,
1047 statement: &sqlez::statement::Statement,
1048 start_index: i32,
1049 ) -> anyhow::Result<i32> {
1050 self.0.bind(statement, start_index)
1051 }
1052}
1053
1054#[derive(Clone, Copy, Default, Add, Sub, Mul, Div, Neg)]
1055pub struct Rems(f32);
1056
1057impl Mul<Pixels> for Rems {
1058 type Output = Pixels;
1059
1060 fn mul(self, other: Pixels) -> Pixels {
1061 Pixels(self.0 * other.0)
1062 }
1063}
1064
1065impl Debug for Rems {
1066 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1067 write!(f, "{} rem", self.0)
1068 }
1069}
1070
1071#[derive(Clone, Copy, Debug, Neg)]
1072pub enum AbsoluteLength {
1073 Pixels(Pixels),
1074 Rems(Rems),
1075}
1076
1077impl AbsoluteLength {
1078 pub fn is_zero(&self) -> bool {
1079 match self {
1080 AbsoluteLength::Pixels(px) => px.0 == 0.,
1081 AbsoluteLength::Rems(rems) => rems.0 == 0.,
1082 }
1083 }
1084}
1085
1086impl From<Pixels> for AbsoluteLength {
1087 fn from(pixels: Pixels) -> Self {
1088 AbsoluteLength::Pixels(pixels)
1089 }
1090}
1091
1092impl From<Rems> for AbsoluteLength {
1093 fn from(rems: Rems) -> Self {
1094 AbsoluteLength::Rems(rems)
1095 }
1096}
1097
1098impl AbsoluteLength {
1099 pub fn to_pixels(&self, rem_size: Pixels) -> Pixels {
1100 match self {
1101 AbsoluteLength::Pixels(pixels) => *pixels,
1102 AbsoluteLength::Rems(rems) => *rems * rem_size,
1103 }
1104 }
1105}
1106
1107impl Default for AbsoluteLength {
1108 fn default() -> Self {
1109 px(0.).into()
1110 }
1111}
1112
1113/// A non-auto length that can be defined in pixels, rems, or percent of parent.
1114#[derive(Clone, Copy, Neg)]
1115pub enum DefiniteLength {
1116 Absolute(AbsoluteLength),
1117 /// A fraction of the parent's size between 0 and 1.
1118 Fraction(f32),
1119}
1120
1121impl DefiniteLength {
1122 pub fn to_pixels(&self, base_size: AbsoluteLength, rem_size: Pixels) -> Pixels {
1123 match self {
1124 DefiniteLength::Absolute(size) => size.to_pixels(rem_size),
1125 DefiniteLength::Fraction(fraction) => match base_size {
1126 AbsoluteLength::Pixels(px) => px * *fraction,
1127 AbsoluteLength::Rems(rems) => rems * rem_size * *fraction,
1128 },
1129 }
1130 }
1131}
1132
1133impl Debug for DefiniteLength {
1134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1135 match self {
1136 DefiniteLength::Absolute(length) => Debug::fmt(length, f),
1137 DefiniteLength::Fraction(fract) => write!(f, "{}%", (fract * 100.0) as i32),
1138 }
1139 }
1140}
1141
1142impl From<Pixels> for DefiniteLength {
1143 fn from(pixels: Pixels) -> Self {
1144 Self::Absolute(pixels.into())
1145 }
1146}
1147
1148impl From<Rems> for DefiniteLength {
1149 fn from(rems: Rems) -> Self {
1150 Self::Absolute(rems.into())
1151 }
1152}
1153
1154impl From<AbsoluteLength> for DefiniteLength {
1155 fn from(length: AbsoluteLength) -> Self {
1156 Self::Absolute(length)
1157 }
1158}
1159
1160impl Default for DefiniteLength {
1161 fn default() -> Self {
1162 Self::Absolute(AbsoluteLength::default())
1163 }
1164}
1165
1166/// A length that can be defined in pixels, rems, percent of parent, or auto.
1167#[derive(Clone, Copy)]
1168pub enum Length {
1169 Definite(DefiniteLength),
1170 Auto,
1171}
1172
1173impl Debug for Length {
1174 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1175 match self {
1176 Length::Definite(definite_length) => write!(f, "{:?}", definite_length),
1177 Length::Auto => write!(f, "auto"),
1178 }
1179 }
1180}
1181
1182pub fn relative(fraction: f32) -> DefiniteLength {
1183 DefiniteLength::Fraction(fraction).into()
1184}
1185
1186/// Returns the Golden Ratio, i.e. `~(1.0 + sqrt(5.0)) / 2.0`.
1187pub fn phi() -> DefiniteLength {
1188 relative(1.61803398875)
1189}
1190
1191pub fn rems(rems: f32) -> Rems {
1192 Rems(rems)
1193}
1194
1195pub const fn px(pixels: f32) -> Pixels {
1196 Pixels(pixels)
1197}
1198
1199pub fn auto() -> Length {
1200 Length::Auto
1201}
1202
1203impl From<Pixels> for Length {
1204 fn from(pixels: Pixels) -> Self {
1205 Self::Definite(pixels.into())
1206 }
1207}
1208
1209impl From<Rems> for Length {
1210 fn from(rems: Rems) -> Self {
1211 Self::Definite(rems.into())
1212 }
1213}
1214
1215impl From<DefiniteLength> for Length {
1216 fn from(length: DefiniteLength) -> Self {
1217 Self::Definite(length)
1218 }
1219}
1220
1221impl From<AbsoluteLength> for Length {
1222 fn from(length: AbsoluteLength) -> Self {
1223 Self::Definite(length.into())
1224 }
1225}
1226
1227impl Default for Length {
1228 fn default() -> Self {
1229 Self::Definite(DefiniteLength::default())
1230 }
1231}
1232
1233impl From<()> for Length {
1234 fn from(_: ()) -> Self {
1235 Self::Definite(DefiniteLength::default())
1236 }
1237}
1238
1239pub trait Half {
1240 fn half(&self) -> Self;
1241}
1242
1243impl Half for f32 {
1244 fn half(&self) -> Self {
1245 self / 2.
1246 }
1247}
1248
1249impl Half for DevicePixels {
1250 fn half(&self) -> Self {
1251 Self(self.0 / 2)
1252 }
1253}
1254
1255impl Half for ScaledPixels {
1256 fn half(&self) -> Self {
1257 Self(self.0 / 2.)
1258 }
1259}
1260
1261impl Half for Pixels {
1262 fn half(&self) -> Self {
1263 Self(self.0 / 2.)
1264 }
1265}
1266
1267impl Half for Rems {
1268 fn half(&self) -> Self {
1269 Self(self.0 / 2.)
1270 }
1271}
1272
1273impl Half for GlobalPixels {
1274 fn half(&self) -> Self {
1275 Self(self.0 / 2.)
1276 }
1277}
1278
1279pub trait IsZero {
1280 fn is_zero(&self) -> bool;
1281}
1282
1283impl IsZero for DevicePixels {
1284 fn is_zero(&self) -> bool {
1285 self.0 == 0
1286 }
1287}
1288
1289impl IsZero for ScaledPixels {
1290 fn is_zero(&self) -> bool {
1291 self.0 == 0.
1292 }
1293}
1294
1295impl IsZero for Pixels {
1296 fn is_zero(&self) -> bool {
1297 self.0 == 0.
1298 }
1299}
1300
1301impl IsZero for Rems {
1302 fn is_zero(&self) -> bool {
1303 self.0 == 0.
1304 }
1305}
1306
1307impl IsZero for AbsoluteLength {
1308 fn is_zero(&self) -> bool {
1309 match self {
1310 AbsoluteLength::Pixels(pixels) => pixels.is_zero(),
1311 AbsoluteLength::Rems(rems) => rems.is_zero(),
1312 }
1313 }
1314}
1315
1316impl IsZero for DefiniteLength {
1317 fn is_zero(&self) -> bool {
1318 match self {
1319 DefiniteLength::Absolute(length) => length.is_zero(),
1320 DefiniteLength::Fraction(fraction) => *fraction == 0.,
1321 }
1322 }
1323}
1324
1325impl IsZero for Length {
1326 fn is_zero(&self) -> bool {
1327 match self {
1328 Length::Definite(length) => length.is_zero(),
1329 Length::Auto => false,
1330 }
1331 }
1332}
1333
1334impl<T: IsZero + Debug + Clone + Default> IsZero for Point<T> {
1335 fn is_zero(&self) -> bool {
1336 self.x.is_zero() && self.y.is_zero()
1337 }
1338}
1339
1340impl<T> IsZero for Size<T>
1341where
1342 T: IsZero + Default + Debug + Clone,
1343{
1344 fn is_zero(&self) -> bool {
1345 self.width.is_zero() || self.height.is_zero()
1346 }
1347}
1348
1349impl<T: IsZero + Debug + Clone + Default> IsZero for Bounds<T> {
1350 fn is_zero(&self) -> bool {
1351 self.size.is_zero()
1352 }
1353}
1354
1355impl<T> IsZero for Corners<T>
1356where
1357 T: IsZero + Clone + Default + Debug,
1358{
1359 fn is_zero(&self) -> bool {
1360 self.top_left.is_zero()
1361 && self.top_right.is_zero()
1362 && self.bottom_right.is_zero()
1363 && self.bottom_left.is_zero()
1364 }
1365}
1366
1367#[cfg(test)]
1368mod tests {
1369 use super::*;
1370
1371 #[test]
1372 fn test_bounds_intersects() {
1373 let bounds1 = Bounds {
1374 origin: Point { x: 0.0, y: 0.0 },
1375 size: Size {
1376 width: 5.0,
1377 height: 5.0,
1378 },
1379 };
1380 let bounds2 = Bounds {
1381 origin: Point { x: 4.0, y: 4.0 },
1382 size: Size {
1383 width: 5.0,
1384 height: 5.0,
1385 },
1386 };
1387 let bounds3 = Bounds {
1388 origin: Point { x: 10.0, y: 10.0 },
1389 size: Size {
1390 width: 5.0,
1391 height: 5.0,
1392 },
1393 };
1394
1395 // Test Case 1: Intersecting bounds
1396 assert_eq!(bounds1.intersects(&bounds2), true);
1397
1398 // Test Case 2: Non-Intersecting bounds
1399 assert_eq!(bounds1.intersects(&bounds3), false);
1400
1401 // Test Case 3: Bounds intersecting with themselves
1402 assert_eq!(bounds1.intersects(&bounds1), true);
1403 }
1404}