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
/// Size with width, height and dirty flag. If the dirty flag is `true`,
/// layout tasks will handle this objects in its arrange and measure
/// tasks.
#[derive(Copy, Clone, PartialEq)]
pub struct DirtySize {
    width: f64,
    height: f64,
    dirty: bool,
}

impl Default for DirtySize {
    fn default() -> Self {
        DirtySize {
            width: 0.0,
            height: 0.0,
            dirty: true,
        }
    }
}

impl DirtySize {
    /// Creates a new dirty size with default values.
    pub fn new() -> Self {
        DirtySize::default()
    }

    pub fn width(&self) -> f64 {
        self.width
    }

    pub fn set_width(&mut self, width: f64) {
        if (self.width - width).abs() > std::f64::EPSILON {
            self.dirty = true;
        }

        self.width = width;
    }

    pub fn height(&self) -> f64 {
        self.height
    }

    pub fn set_height(&mut self, height: f64) {
        if (self.height - height).abs() > std::f64::EPSILON {
            self.dirty = true;
        }

        self.height = height;
    }

    pub fn size(&self) -> (f64, f64) {
        (self.width, self.height)
    }

    pub fn set_size(&mut self, width: f64, height: f64) {
        if (self.width - width).abs() > std::f64::EPSILON
            && (self.height - height).abs() > std::f64::EPSILON
        {
            self.dirty = true
        }

        self.width = width;
        self.height = height;
    }

    /// Gets the dirty flag.
    pub fn dirty(&self) -> bool {
        self.dirty
    }

    /// Sets the dirty flag.
    pub fn set_dirty(&mut self, dirty: bool) {
        self.dirty = dirty;
    }
}

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

    #[test]
    fn test_set_width() {
        let width = 10.0;

        let mut dirty_size = DirtySize::default();

        dirty_size.set_width(width);

        assert!(crate::f64_cmp(dirty_size.width(), width));
        assert!(dirty_size.dirty());
    }

    #[test]
    fn test_set_height() {
        let height = 10.0;

        let mut dirty_size = DirtySize::default();
        dirty_size.set_height(height);

        assert!(crate::f64_cmp(dirty_size.height(), height));
        assert!(dirty_size.dirty());
    }

    #[test]
    fn test_set_size() {
        let size = (10.0, 20.0);

        let mut dirty_size = DirtySize::default();

        dirty_size.set_size(size.0, size.1);

        assert_eq!(dirty_size.size(), size);
        assert!(dirty_size.dirty());
    }

    #[test]
    fn test_set_dirty() {
        let mut dirty_size = DirtySize::default();

        dirty_size.set_dirty(false);

        assert!(!dirty_size.dirty());
    }
}