zng_unit/
px_dip.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
use std::{cmp, fmt, ops};

use serde::{Deserialize, Serialize};

use crate::{side_offsets::SideOffsets2D, CornerRadius2D, Factor};

/// Same value used in `60`.
const DIP_TO_PX: i32 = 60;

/// Device pixel.
///
/// Represents an actual device pixel, not descaled by the pixel scale factor.
#[derive(Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, bytemuck::Zeroable, bytemuck::Pod)]
#[repr(transparent)]
#[serde(transparent)]
pub struct Px(pub i32);
impl Px {
    /// See [`DipToPx`].
    pub fn from_dip(dip: Dip, scale_factor: Factor) -> Px {
        Px((dip.0 as f32 / DIP_TO_PX as f32 * scale_factor.0).round() as i32)
    }

    /// Compares and returns the maximum of two pixel values.
    pub fn max(self, other: Px) -> Px {
        Px(self.0.max(other.0))
    }

    /// Compares and returns the minimum of two pixel values.
    pub fn min(self, other: Px) -> Px {
        Px(self.0.min(other.0))
    }

    /// Computes the saturating absolute value of `self`.
    pub fn abs(self) -> Px {
        Px(self.0.saturating_abs())
    }

    /// [`i32::MAX`].
    pub const MAX: Px = Px(i32::MAX);

    /// [`i32::MIN`].
    pub const MIN: Px = Px(i32::MIN);
}
impl fmt::Debug for Px {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}px", self.0)
    }
}
impl fmt::Display for Px {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}px", self.0)
    }
}
impl num_traits::ToPrimitive for Px {
    fn to_i32(&self) -> Option<i32> {
        Some(self.0)
    }
    fn to_i64(&self) -> Option<i64> {
        Some(self.0 as i64)
    }

    fn to_u64(&self) -> Option<u64> {
        Some(self.0 as u64)
    }
}
impl num_traits::NumCast for Px {
    fn from<T: num_traits::ToPrimitive>(n: T) -> Option<Self> {
        if let Some(p) = n.to_i32() {
            Some(Px(p))
        } else {
            n.to_f32().map(|p| Px(p as i32))
        }
    }
}
impl num_traits::Zero for Px {
    fn zero() -> Self {
        Px(0)
    }

    fn is_zero(&self) -> bool {
        self.0 == 0
    }
}
impl num_traits::One for Px {
    fn one() -> Self {
        Px(1)
    }
}
impl euclid::num::Round for Px {
    fn round(self) -> Self {
        self
    }
}
impl euclid::num::Ceil for Px {
    fn ceil(self) -> Self {
        self
    }
}
impl euclid::num::Floor for Px {
    fn floor(self) -> Self {
        self
    }
}
impl num_traits::Num for Px {
    type FromStrRadixErr = <i32 as num_traits::Num>::FromStrRadixErr;

    fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
        num_traits::Num::from_str_radix(str, radix).map(Px)
    }
}
impl num_traits::Signed for Px {
    fn abs(&self) -> Self {
        Px(self.0.abs())
    }

    fn abs_sub(&self, other: &Self) -> Self {
        Px(num_traits::Signed::abs_sub(&self.0, &other.0))
    }

    fn signum(&self) -> Self {
        Px(num_traits::Signed::signum(&self.0))
    }

    fn is_positive(&self) -> bool {
        self.0 > 0
    }

    fn is_negative(&self) -> bool {
        self.0 < 0
    }
}
impl From<i32> for Px {
    fn from(px: i32) -> Self {
        Px(px)
    }
}
impl ops::Add for Px {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Px(self.0.saturating_add(rhs.0))
    }
}
impl ops::AddAssign for Px {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}
impl ops::Sub for Px {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        Px(self.0.saturating_sub(rhs.0))
    }
}
impl ops::SubAssign for Px {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}
impl ops::Mul<f32> for Px {
    type Output = Px;

    fn mul(self, rhs: f32) -> Self::Output {
        Px((self.0 as f32 * rhs).round() as i32)
    }
}
impl ops::MulAssign<f32> for Px {
    fn mul_assign(&mut self, rhs: f32) {
        *self = *self * rhs;
    }
}
impl ops::Mul<i32> for Px {
    type Output = Px;

    fn mul(self, rhs: i32) -> Self::Output {
        Px(self.0 * rhs)
    }
}
impl ops::MulAssign<i32> for Px {
    fn mul_assign(&mut self, rhs: i32) {
        *self = *self * rhs;
    }
}
impl ops::Mul<Px> for Px {
    type Output = Px;

    fn mul(self, rhs: Px) -> Self::Output {
        Px(self.0.saturating_mul(rhs.0))
    }
}
impl ops::MulAssign<Px> for Px {
    fn mul_assign(&mut self, rhs: Px) {
        *self = *self * rhs;
    }
}
impl ops::Div<f32> for Px {
    type Output = Px;

    fn div(self, rhs: f32) -> Self::Output {
        Px((self.0 as f32 / rhs).round() as i32)
    }
}
impl ops::Div<i32> for Px {
    type Output = Px;

    fn div(self, rhs: i32) -> Self::Output {
        Px(self.0 / rhs)
    }
}
impl ops::Div<Px> for Px {
    type Output = Px;

    fn div(self, rhs: Px) -> Self::Output {
        Px(self.0 / rhs.0)
    }
}
impl ops::DivAssign<f32> for Px {
    fn div_assign(&mut self, rhs: f32) {
        *self = *self / rhs;
    }
}
impl ops::DivAssign<i32> for Px {
    fn div_assign(&mut self, rhs: i32) {
        *self = *self / rhs;
    }
}
impl ops::DivAssign<Px> for Px {
    fn div_assign(&mut self, rhs: Px) {
        *self = *self / rhs;
    }
}
impl ops::Neg for Px {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Px(self.0.saturating_neg())
    }
}
impl ops::Rem for Px {
    type Output = Self;

    fn rem(self, rhs: Self) -> Self::Output {
        Px(self.0 % rhs.0)
    }
}
impl std::iter::Sum for Px {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Px(0), |a, b| a + b)
    }
}
impl PartialEq<i32> for Px {
    fn eq(&self, other: &i32) -> bool {
        *self == Px(*other)
    }
}
impl PartialOrd<i32> for Px {
    fn partial_cmp(&self, other: &i32) -> Option<cmp::Ordering> {
        self.partial_cmp(&Px(*other))
    }
}

/// Device independent pixel.
///
/// Represent a device pixel descaled by the pixel scale factor.
///
/// Internally this is an `i32` that represents 1/60th of a pixel.
#[derive(Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, bytemuck::Zeroable, bytemuck::Pod)]
#[serde(from = "f32")]
#[serde(into = "f32")]
#[repr(transparent)]
pub struct Dip(i32);
impl Dip {
    /// New from round integer value.
    pub const fn new(dip: i32) -> Self {
        Dip(dip * DIP_TO_PX)
    }

    /// new from floating point.
    pub fn new_f32(dip: f32) -> Self {
        Dip((dip * DIP_TO_PX as f32).round() as i32)
    }

    /// See [`PxToDip`].
    pub fn from_px(px: Px, scale_factor: Factor) -> Dip {
        Dip((px.0 as f32 / scale_factor.0 * DIP_TO_PX as f32).round() as i32)
    }

    /// Returns `self` as [`f32`].
    pub fn to_f32(self) -> f32 {
        self.0 as f32 / DIP_TO_PX as f32
    }

    /// Returns `self` as [`i32`].
    pub fn to_i32(self) -> i32 {
        self.0 / DIP_TO_PX
    }

    /// Compares and returns the maximum of two pixel values.
    pub fn max(self, other: Dip) -> Dip {
        Dip(self.0.max(other.0))
    }

    /// Compares and returns the minimum of two pixel values.
    pub fn min(self, other: Dip) -> Dip {
        Dip(self.0.min(other.0))
    }

    /// Computes the saturating absolute value of `self`.
    pub fn abs(self) -> Dip {
        Dip(self.0.saturating_abs())
    }

    /// Maximum DIP value.
    pub const MAX: Dip = Dip(i32::MAX / DIP_TO_PX);
    /// Minimum DIP value.
    pub const MIN: Dip = Dip(i32::MIN / DIP_TO_PX);
}
impl fmt::Debug for Dip {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}
impl fmt::Display for Dip {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.to_f32(), f)?;
        write!(f, "dip")
    }
}
impl From<i32> for Dip {
    fn from(dip: i32) -> Self {
        Dip::new(dip)
    }
}
impl From<f32> for Dip {
    fn from(dip: f32) -> Self {
        Dip::new_f32(dip)
    }
}
impl From<Dip> for f32 {
    fn from(value: Dip) -> Self {
        value.to_f32()
    }
}
impl ops::Add for Dip {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Dip(self.0.saturating_add(rhs.0))
    }
}
impl ops::AddAssign for Dip {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}
impl ops::Sub for Dip {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        Dip(self.0.saturating_sub(rhs.0))
    }
}
impl ops::SubAssign for Dip {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}
impl ops::Neg for Dip {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Dip(self.0.saturating_neg())
    }
}
impl ops::Mul<f32> for Dip {
    type Output = Dip;

    fn mul(self, rhs: f32) -> Self::Output {
        Dip((self.0 as f32 * rhs).round() as i32)
    }
}
impl ops::MulAssign<f32> for Dip {
    fn mul_assign(&mut self, rhs: f32) {
        *self = *self * rhs;
    }
}
impl ops::Mul<Dip> for Dip {
    type Output = Dip;

    fn mul(self, rhs: Dip) -> Self::Output {
        Dip(self.0.saturating_mul(rhs.to_i32()))
    }
}
impl ops::MulAssign<Dip> for Dip {
    fn mul_assign(&mut self, rhs: Dip) {
        *self = *self * rhs;
    }
}
impl ops::Div<f32> for Dip {
    type Output = Dip;

    fn div(self, rhs: f32) -> Self::Output {
        Dip((self.0 as f32 / rhs).round() as i32)
    }
}
impl ops::DivAssign<f32> for Dip {
    fn div_assign(&mut self, rhs: f32) {
        *self = *self / rhs;
    }
}
impl ops::Div<Dip> for Dip {
    type Output = Dip;

    fn div(self, rhs: Dip) -> Self::Output {
        Dip::new(self.0 / rhs.0)
    }
}
impl ops::DivAssign<Dip> for Dip {
    fn div_assign(&mut self, rhs: Dip) {
        *self = *self / rhs;
    }
}
impl ops::Rem for Dip {
    type Output = Self;

    fn rem(self, rhs: Self) -> Self::Output {
        Dip(self.0 % rhs.0)
    }
}
impl ops::RemAssign for Dip {
    fn rem_assign(&mut self, rhs: Self) {
        *self = *self % rhs;
    }
}
impl num_traits::ToPrimitive for Dip {
    fn to_i64(&self) -> Option<i64> {
        Some(Dip::to_i32(*self) as i64)
    }

    fn to_u64(&self) -> Option<u64> {
        if self.0 >= 0 {
            Some(Dip::to_i32(*self) as u64)
        } else {
            None
        }
    }

    fn to_f32(&self) -> Option<f32> {
        Some(Dip::to_f32(*self))
    }

    fn to_f64(&self) -> Option<f64> {
        Some(Dip::to_f32(*self) as f64)
    }
}
impl num_traits::NumCast for Dip {
    fn from<T: num_traits::ToPrimitive>(n: T) -> Option<Self> {
        #[expect(clippy::manual_map)]
        if let Some(n) = n.to_f32() {
            Some(Dip::new_f32(n))
        } else if let Some(n) = n.to_i32() {
            Some(Dip::new(n))
        } else {
            None
        }
    }
}
impl num_traits::Zero for Dip {
    fn zero() -> Self {
        Dip(0)
    }

    fn is_zero(&self) -> bool {
        self.0 == 0
    }
}
impl num_traits::One for Dip {
    fn one() -> Self {
        Dip::new(1)
    }
}
impl euclid::num::Round for Dip {
    fn round(self) -> Self {
        Dip::new_f32(self.to_f32().round())
    }
}
impl euclid::num::Ceil for Dip {
    fn ceil(self) -> Self {
        Dip::new_f32(self.to_f32().ceil())
    }
}
impl euclid::num::Floor for Dip {
    fn floor(self) -> Self {
        Dip::new_f32(self.to_f32().floor())
    }
}
impl num_traits::Signed for Dip {
    fn abs(&self) -> Self {
        Dip(self.0.abs())
    }

    fn abs_sub(&self, other: &Self) -> Self {
        Dip(num_traits::Signed::abs_sub(&self.0, &other.0))
    }

    fn signum(&self) -> Self {
        match self.0.cmp(&0) {
            cmp::Ordering::Less => Dip::new(-1),
            cmp::Ordering::Equal => Dip(0),
            cmp::Ordering::Greater => Dip::new(1),
        }
    }

    fn is_positive(&self) -> bool {
        self.0 > 0
    }

    fn is_negative(&self) -> bool {
        self.0 < 0
    }
}
impl num_traits::Num for Dip {
    type FromStrRadixErr = <i32 as num_traits::Num>::FromStrRadixErr;

    fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
        num_traits::Num::from_str_radix(str, radix).map(Dip::new)
    }
}
impl PartialEq<i32> for Dip {
    fn eq(&self, other: &i32) -> bool {
        *self == Dip::new(*other)
    }
}
impl PartialOrd<i32> for Dip {
    fn partial_cmp(&self, other: &i32) -> Option<cmp::Ordering> {
        self.partial_cmp(&Dip::new(*other))
    }
}
impl PartialEq<f32> for Dip {
    fn eq(&self, other: &f32) -> bool {
        *self == Dip::new_f32(*other)
    }
}
impl PartialOrd<f32> for Dip {
    fn partial_cmp(&self, other: &f32) -> Option<cmp::Ordering> {
        self.partial_cmp(&Dip::new_f32(*other))
    }
}

/// A point in device pixels.
pub type PxPoint = euclid::Point2D<Px, Px>;

/// A point in device independent pixels.
pub type DipPoint = euclid::Point2D<Dip, Dip>;

/// A vector in device pixels.
pub type PxVector = euclid::Vector2D<Px, Px>;

/// A vector in device independent pixels.
pub type DipVector = euclid::Vector2D<Dip, Dip>;

/// A size in device pixels.
pub type PxSize = euclid::Size2D<Px, Px>;

/// A size in device pixels.
pub type DipSize = euclid::Size2D<Dip, Dip>;

/// A rectangle in device pixels.
pub type PxRect = euclid::Rect<Px, Px>;

/// A rectangle box in device pixels.
pub type PxBox = euclid::Box2D<Px, Px>;

/// A rectangle in device independent pixels.
pub type DipRect = euclid::Rect<Dip, Dip>;

/// A rectangle box in device independent pixels.
pub type DipBox = euclid::Box2D<Dip, Dip>;

/// Side-offsets in device pixels.
pub type PxSideOffsets = SideOffsets2D<Px, Px>;
/// Side-offsets in device independent pixels.
pub type DipSideOffsets = SideOffsets2D<Dip, Dip>;

/// Corner-radius in device pixels.
pub type PxCornerRadius = CornerRadius2D<Px, Px>;

/// Corner-radius in device independent pixels.
pub type DipCornerRadius = CornerRadius2D<Dip, Dip>;

/// Conversion from [`Px`] to [`Dip`] units.
pub trait PxToDip {
    /// `Self` equivalent in [`Dip`] units.
    type AsDip;

    /// Divide the [`Px`] self by the scale.
    fn to_dip(self, scale_factor: Factor) -> Self::AsDip;
}

/// Conversion from [`Dip`] to [`Px`] units.
pub trait DipToPx {
    /// `Self` equivalent in [`Px`] units.
    type AsPx;

    /// Multiply the [`Dip`] self by the scale.
    fn to_px(self, scale_factor: Factor) -> Self::AsPx;
}

impl PxToDip for Px {
    type AsDip = Dip;

    fn to_dip(self, scale_factor: Factor) -> Self::AsDip {
        Dip::from_px(self, scale_factor)
    }
}

impl DipToPx for Dip {
    type AsPx = Px;

    fn to_px(self, scale_factor: Factor) -> Self::AsPx {
        Px::from_dip(self, scale_factor)
    }
}

impl PxToDip for PxPoint {
    type AsDip = DipPoint;

    fn to_dip(self, scale_factor: Factor) -> Self::AsDip {
        DipPoint::new(self.x.to_dip(scale_factor), self.y.to_dip(scale_factor))
    }
}

impl DipToPx for DipPoint {
    type AsPx = PxPoint;

    fn to_px(self, scale_factor: Factor) -> Self::AsPx {
        PxPoint::new(self.x.to_px(scale_factor), self.y.to_px(scale_factor))
    }
}
impl DipToPx for euclid::Point2D<f32, Dip> {
    type AsPx = euclid::Point2D<f32, Px>;

    fn to_px(self, scale_factor: Factor) -> Self::AsPx {
        euclid::point2(self.x * scale_factor.0, self.y * scale_factor.0)
    }
}

impl PxToDip for PxSize {
    type AsDip = DipSize;

    fn to_dip(self, scale_factor: Factor) -> Self::AsDip {
        DipSize::new(self.width.to_dip(scale_factor), self.height.to_dip(scale_factor))
    }
}

impl DipToPx for DipSize {
    type AsPx = PxSize;

    fn to_px(self, scale_factor: Factor) -> Self::AsPx {
        PxSize::new(self.width.to_px(scale_factor), self.height.to_px(scale_factor))
    }
}

impl DipToPx for DipVector {
    type AsPx = PxVector;

    fn to_px(self, scale_factor: Factor) -> Self::AsPx {
        PxVector::new(self.x.to_px(scale_factor), self.y.to_px(scale_factor))
    }
}
impl PxToDip for PxVector {
    type AsDip = DipVector;

    fn to_dip(self, scale_factor: Factor) -> Self::AsDip {
        DipVector::new(self.x.to_dip(scale_factor), self.y.to_dip(scale_factor))
    }
}

impl PxToDip for PxRect {
    type AsDip = DipRect;

    fn to_dip(self, scale_factor: Factor) -> Self::AsDip {
        DipRect::new(self.origin.to_dip(scale_factor), self.size.to_dip(scale_factor))
    }
}

impl DipToPx for DipRect {
    type AsPx = PxRect;

    fn to_px(self, scale_factor: Factor) -> Self::AsPx {
        PxRect::new(self.origin.to_px(scale_factor), self.size.to_px(scale_factor))
    }
}

impl PxToDip for PxBox {
    type AsDip = DipBox;

    fn to_dip(self, scale_factor: Factor) -> Self::AsDip {
        DipBox::new(self.min.to_dip(scale_factor), self.max.to_dip(scale_factor))
    }
}

impl DipToPx for DipBox {
    type AsPx = PxBox;

    fn to_px(self, scale_factor: Factor) -> Self::AsPx {
        PxBox::new(self.min.to_px(scale_factor), self.max.to_px(scale_factor))
    }
}

impl DipToPx for DipSideOffsets {
    type AsPx = PxSideOffsets;

    fn to_px(self, scale_factor: Factor) -> Self::AsPx {
        PxSideOffsets::new(
            self.top.to_px(scale_factor),
            self.right.to_px(scale_factor),
            self.bottom.to_px(scale_factor),
            self.left.to_px(scale_factor),
        )
    }
}
impl PxToDip for PxSideOffsets {
    type AsDip = DipSideOffsets;

    fn to_dip(self, scale_factor: Factor) -> Self::AsDip {
        DipSideOffsets::new(
            self.top.to_dip(scale_factor),
            self.right.to_dip(scale_factor),
            self.bottom.to_dip(scale_factor),
            self.left.to_dip(scale_factor),
        )
    }
}

impl DipToPx for DipCornerRadius {
    type AsPx = PxCornerRadius;

    fn to_px(self, scale_factor: Factor) -> Self::AsPx {
        PxCornerRadius::new(
            self.top_left.to_px(scale_factor),
            self.top_right.to_px(scale_factor),
            self.bottom_left.to_px(scale_factor),
            self.bottom_right.to_px(scale_factor),
        )
    }
}
impl PxToDip for PxCornerRadius {
    type AsDip = DipCornerRadius;

    fn to_dip(self, scale_factor: Factor) -> Self::AsDip {
        DipCornerRadius::new(
            self.top_left.to_dip(scale_factor),
            self.top_right.to_dip(scale_factor),
            self.bottom_left.to_dip(scale_factor),
            self.bottom_right.to_dip(scale_factor),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn dip_px_1_1_conversion() {
        let px = Dip::new(100).to_px(Factor(1.0));
        assert_eq!(px, Px(100));
    }

    #[test]
    fn px_dip_1_1_conversion() {
        let dip = Px(100).to_dip(Factor(1.0));
        assert_eq!(dip, Dip::new(100));
    }

    #[test]
    fn dip_px_1_15_conversion() {
        let px = Dip::new(100).to_px(Factor(1.5));
        assert_eq!(px, Px(150));
    }

    #[test]
    fn px_dip_1_15_conversion() {
        let dip = Px(150).to_dip(Factor(1.5));
        assert_eq!(dip, Dip::new(100));
    }
}