orbtk_utils/
dirty_size.rs1#[derive(Copy, Clone, PartialEq)]
5pub struct DirtySize {
6 width: f64,
7 height: f64,
8 dirty: bool,
9}
10
11impl Default for DirtySize {
12 fn default() -> Self {
13 DirtySize {
14 width: 0.0,
15 height: 0.0,
16 dirty: true,
17 }
18 }
19}
20
21impl DirtySize {
22 pub fn new() -> Self {
24 DirtySize::default()
25 }
26
27 pub fn width(&self) -> f64 {
28 self.width
29 }
30
31 pub fn set_width(&mut self, width: f64) {
32 if (self.width - width).abs() > std::f64::EPSILON {
33 self.dirty = true;
34 }
35
36 self.width = width;
37 }
38
39 pub fn height(&self) -> f64 {
40 self.height
41 }
42
43 pub fn set_height(&mut self, height: f64) {
44 if (self.height - height).abs() > std::f64::EPSILON {
45 self.dirty = true;
46 }
47
48 self.height = height;
49 }
50
51 pub fn size(&self) -> (f64, f64) {
52 (self.width, self.height)
53 }
54
55 pub fn set_size(&mut self, width: f64, height: f64) {
56 if (self.width - width).abs() > std::f64::EPSILON
57 && (self.height - height).abs() > std::f64::EPSILON
58 {
59 self.dirty = true
60 }
61
62 self.width = width;
63 self.height = height;
64 }
65
66 pub fn dirty(&self) -> bool {
68 self.dirty
69 }
70
71 pub fn set_dirty(&mut self, dirty: bool) {
73 self.dirty = dirty;
74 }
75}
76
77#[cfg(test)]
78mod tests {
79 use crate::prelude::*;
80
81 #[test]
82 fn test_set_width() {
83 let width = 10.0;
84
85 let mut dirty_size = DirtySize::default();
86
87 dirty_size.set_width(width);
88
89 assert!(crate::f64_cmp(dirty_size.width(), width));
90 assert!(dirty_size.dirty());
91 }
92
93 #[test]
94 fn test_set_height() {
95 let height = 10.0;
96
97 let mut dirty_size = DirtySize::default();
98 dirty_size.set_height(height);
99
100 assert!(crate::f64_cmp(dirty_size.height(), height));
101 assert!(dirty_size.dirty());
102 }
103
104 #[test]
105 fn test_set_size() {
106 let size = (10.0, 20.0);
107
108 let mut dirty_size = DirtySize::default();
109
110 dirty_size.set_size(size.0, size.1);
111
112 assert_eq!(dirty_size.size(), size);
113 assert!(dirty_size.dirty());
114 }
115
116 #[test]
117 fn test_set_dirty() {
118 let mut dirty_size = DirtySize::default();
119
120 dirty_size.set_dirty(false);
121
122 assert!(!dirty_size.dirty());
123 }
124}