torin/values/
gaps.rs

1pub use euclid::Rect;
2
3use crate::{
4    geometry::Length,
5    scaled::Scaled,
6};
7
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[derive(PartialEq, Clone, Debug, Default, Copy)]
10pub struct Gaps {
11    top: Length,
12    right: Length,
13    bottom: Length,
14    left: Length,
15}
16
17impl From<f32> for Gaps {
18    fn from(padding: f32) -> Self {
19        Gaps::new_all(padding)
20    }
21}
22
23impl From<(f32, f32)> for Gaps {
24    fn from((vertical, horizontal): (f32, f32)) -> Self {
25        Gaps::new(vertical, horizontal, vertical, horizontal)
26    }
27}
28
29impl From<(f32, f32, f32, f32)> for Gaps {
30    fn from((top, right, bottom, left): (f32, f32, f32, f32)) -> Self {
31        Gaps::new(top, right, bottom, left)
32    }
33}
34
35impl Gaps {
36    pub const fn new(top: f32, right: f32, bottom: f32, left: f32) -> Self {
37        Self {
38            top: Length::new(top),
39            right: Length::new(right),
40            bottom: Length::new(bottom),
41            left: Length::new(left),
42        }
43    }
44
45    pub const fn new_all(gaps: f32) -> Self {
46        Self::new(gaps, gaps, gaps, gaps)
47    }
48
49    pub fn fill_vertical(&mut self, value: f32) {
50        self.top = Length::new(value);
51        self.bottom = Length::new(value);
52    }
53
54    pub fn fill_horizontal(&mut self, value: f32) {
55        self.right = Length::new(value);
56        self.left = Length::new(value);
57    }
58
59    pub fn fill_all(&mut self, value: f32) {
60        self.fill_horizontal(value);
61        self.fill_vertical(value);
62    }
63
64    pub fn horizontal(&self) -> f32 {
65        (self.right + self.left).get()
66    }
67
68    pub fn vertical(&self) -> f32 {
69        (self.top + self.bottom).get()
70    }
71
72    pub fn top(&self) -> f32 {
73        self.top.get()
74    }
75
76    pub fn right(&self) -> f32 {
77        self.right.get()
78    }
79
80    pub fn bottom(&self) -> f32 {
81        self.bottom.get()
82    }
83
84    pub fn left(&self) -> f32 {
85        self.left.get()
86    }
87
88    pub fn pretty(&self) -> String {
89        format!(
90            "({}, {}, {}, {})",
91            self.top(),
92            self.right(),
93            self.bottom(),
94            self.left()
95        )
96    }
97}
98
99impl Scaled for Gaps {
100    fn scale(&mut self, scale: f32) {
101        self.left *= scale;
102        self.right *= scale;
103        self.top *= scale;
104        self.bottom *= scale;
105    }
106}