Skip to main content

torin/values/
gaps.rs

1pub use euclid::Rect;
2
3use crate::{
4    geometry::Length,
5    scaled::Scaled,
6};
7
8/// Spacing applied around the four sides of an element, used for `padding` and `margin`.
9///
10/// Prefer the constructors [`Gaps::new_all`], [`Gaps::new_symmetric`] and [`Gaps::new`].
11/// It also implements `From<f32>`, `From<(f32, f32)>` and `From<(f32, f32, f32, f32)>`.
12///
13/// ```
14/// # use torin::prelude::*;
15/// let all = Gaps::new_all(10.0);
16/// let symmetric = Gaps::new_symmetric(5.0, 20.0); // (vertical, horizontal)
17/// let each = Gaps::new(1.0, 2.0, 3.0, 4.0); // (top, right, bottom, left)
18/// ```
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
20#[derive(PartialEq, Clone, Debug, Default, Copy)]
21pub struct Gaps {
22    top: Length,
23    right: Length,
24    bottom: Length,
25    left: Length,
26}
27
28impl From<f32> for Gaps {
29    fn from(padding: f32) -> Self {
30        Gaps::new_all(padding)
31    }
32}
33
34impl From<(f32, f32)> for Gaps {
35    fn from((vertical, horizontal): (f32, f32)) -> Self {
36        Gaps::new_symmetric(vertical, horizontal)
37    }
38}
39
40impl From<(f32, f32, f32, f32)> for Gaps {
41    fn from((top, right, bottom, left): (f32, f32, f32, f32)) -> Self {
42        Gaps::new(top, right, bottom, left)
43    }
44}
45
46impl Gaps {
47    /// Create [`Gaps`] with an individual value for each side, in `(top, right, bottom, left)` order.
48    pub const fn new(top: f32, right: f32, bottom: f32, left: f32) -> Self {
49        Self {
50            top: Length::new(top),
51            right: Length::new(right),
52            bottom: Length::new(bottom),
53            left: Length::new(left),
54        }
55    }
56
57    /// Create [`Gaps`] with the same value on all four sides.
58    pub const fn new_all(gaps: f32) -> Self {
59        Self::new(gaps, gaps, gaps, gaps)
60    }
61
62    /// Create [`Gaps`] with one value for the top and bottom, and another for the left and right.
63    pub const fn new_symmetric(vertical: f32, horizontal: f32) -> Self {
64        Self::new(vertical, horizontal, vertical, horizontal)
65    }
66
67    pub fn fill_vertical(&mut self, value: f32) {
68        self.top = Length::new(value);
69        self.bottom = Length::new(value);
70    }
71
72    pub fn fill_horizontal(&mut self, value: f32) {
73        self.right = Length::new(value);
74        self.left = Length::new(value);
75    }
76
77    pub fn fill_all(&mut self, value: f32) {
78        self.fill_horizontal(value);
79        self.fill_vertical(value);
80    }
81
82    pub fn horizontal(&self) -> f32 {
83        (self.right + self.left).get()
84    }
85
86    pub fn vertical(&self) -> f32 {
87        (self.top + self.bottom).get()
88    }
89
90    pub fn top(&self) -> f32 {
91        self.top.get()
92    }
93
94    pub fn right(&self) -> f32 {
95        self.right.get()
96    }
97
98    pub fn bottom(&self) -> f32 {
99        self.bottom.get()
100    }
101
102    pub fn left(&self) -> f32 {
103        self.left.get()
104    }
105
106    pub fn pretty(&self) -> String {
107        format!(
108            "({}, {}, {}, {})",
109            self.top(),
110            self.right(),
111            self.bottom(),
112            self.left()
113        )
114    }
115}
116
117impl Scaled for Gaps {
118    fn scale(&mut self, scale: f32) {
119        self.left *= scale;
120        self.right *= scale;
121        self.top *= scale;
122        self.bottom *= scale;
123    }
124}