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_symmetric(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 const fn new_symmetric(vertical: f32, horizontal: f32) -> Self {
50 Self::new(vertical, horizontal, vertical, horizontal)
51 }
52
53 pub fn fill_vertical(&mut self, value: f32) {
54 self.top = Length::new(value);
55 self.bottom = Length::new(value);
56 }
57
58 pub fn fill_horizontal(&mut self, value: f32) {
59 self.right = Length::new(value);
60 self.left = Length::new(value);
61 }
62
63 pub fn fill_all(&mut self, value: f32) {
64 self.fill_horizontal(value);
65 self.fill_vertical(value);
66 }
67
68 pub fn horizontal(&self) -> f32 {
69 (self.right + self.left).get()
70 }
71
72 pub fn vertical(&self) -> f32 {
73 (self.top + self.bottom).get()
74 }
75
76 pub fn top(&self) -> f32 {
77 self.top.get()
78 }
79
80 pub fn right(&self) -> f32 {
81 self.right.get()
82 }
83
84 pub fn bottom(&self) -> f32 {
85 self.bottom.get()
86 }
87
88 pub fn left(&self) -> f32 {
89 self.left.get()
90 }
91
92 pub fn pretty(&self) -> String {
93 format!(
94 "({}, {}, {}, {})",
95 self.top(),
96 self.right(),
97 self.bottom(),
98 self.left()
99 )
100 }
101}
102
103impl Scaled for Gaps {
104 fn scale(&mut self, scale: f32) {
105 self.left *= scale;
106 self.right *= scale;
107 self.top *= scale;
108 self.bottom *= scale;
109 }
110}