1use crate::{Pixels, Size};
3
4#[derive(Debug, Copy, Clone, PartialEq, Default)]
36pub struct Padding {
37 pub top: f32,
39 pub right: f32,
41 pub bottom: f32,
43 pub left: f32,
45}
46
47pub fn all(padding: impl Into<Pixels>) -> Padding {
49 Padding::new(padding.into().0)
50}
51
52pub fn top(padding: impl Into<Pixels>) -> Padding {
54 Padding::default().top(padding)
55}
56
57pub fn bottom(padding: impl Into<Pixels>) -> Padding {
59 Padding::default().bottom(padding)
60}
61
62pub fn left(padding: impl Into<Pixels>) -> Padding {
64 Padding::default().left(padding)
65}
66
67pub fn right(padding: impl Into<Pixels>) -> Padding {
69 Padding::default().right(padding)
70}
71
72pub fn horizontal(padding: impl Into<Pixels>) -> Padding {
74 Padding::default().horizontal(padding)
75}
76
77pub fn vertical(padding: impl Into<Pixels>) -> Padding {
79 Padding::default().vertical(padding)
80}
81
82impl Padding {
83 pub const ZERO: Padding = Padding {
85 top: 0.0,
86 right: 0.0,
87 bottom: 0.0,
88 left: 0.0,
89 };
90
91 pub const fn new(padding: f32) -> Padding {
93 Padding {
94 top: padding,
95 right: padding,
96 bottom: padding,
97 left: padding,
98 }
99 }
100
101 pub fn top(self, top: impl Into<Pixels>) -> Self {
105 Self {
106 top: top.into().0,
107 ..self
108 }
109 }
110
111 pub fn bottom(self, bottom: impl Into<Pixels>) -> Self {
115 Self {
116 bottom: bottom.into().0,
117 ..self
118 }
119 }
120
121 pub fn left(self, left: impl Into<Pixels>) -> Self {
125 Self {
126 left: left.into().0,
127 ..self
128 }
129 }
130
131 pub fn right(self, right: impl Into<Pixels>) -> Self {
135 Self {
136 right: right.into().0,
137 ..self
138 }
139 }
140
141 pub fn horizontal(self, horizontal: impl Into<Pixels>) -> Self {
146 let horizontal = horizontal.into();
147
148 Self {
149 left: horizontal.0,
150 right: horizontal.0,
151 ..self
152 }
153 }
154
155 pub fn vertical(self, vertical: impl Into<Pixels>) -> Self {
160 let vertical = vertical.into();
161
162 Self {
163 top: vertical.0,
164 bottom: vertical.0,
165 ..self
166 }
167 }
168
169 pub fn x(self) -> f32 {
171 self.left + self.right
172 }
173
174 pub fn y(self) -> f32 {
176 self.top + self.bottom
177 }
178
179 pub fn fit(self, inner: Size, outer: Size) -> Self {
181 let available = (outer - inner).max(Size::ZERO);
182 let new_top = self.top.min(available.height);
183 let new_left = self.left.min(available.width);
184
185 Padding {
186 top: new_top,
187 bottom: self.bottom.min(available.height - new_top),
188 left: new_left,
189 right: self.right.min(available.width - new_left),
190 }
191 }
192}
193
194impl From<u16> for Padding {
195 fn from(p: u16) -> Self {
196 Padding {
197 top: f32::from(p),
198 right: f32::from(p),
199 bottom: f32::from(p),
200 left: f32::from(p),
201 }
202 }
203}
204
205impl From<[u16; 2]> for Padding {
206 fn from(p: [u16; 2]) -> Self {
207 Padding {
208 top: f32::from(p[0]),
209 right: f32::from(p[1]),
210 bottom: f32::from(p[0]),
211 left: f32::from(p[1]),
212 }
213 }
214}
215
216impl From<f32> for Padding {
217 fn from(p: f32) -> Self {
218 Padding {
219 top: p,
220 right: p,
221 bottom: p,
222 left: p,
223 }
224 }
225}
226
227impl From<[f32; 2]> for Padding {
228 fn from(p: [f32; 2]) -> Self {
229 Padding {
230 top: p[0],
231 right: p[1],
232 bottom: p[0],
233 left: p[1],
234 }
235 }
236}
237
238impl From<Padding> for Size {
239 fn from(padding: Padding) -> Self {
240 Self::new(padding.x(), padding.y())
241 }
242}
243
244impl From<Pixels> for Padding {
245 fn from(pixels: Pixels) -> Self {
246 Self::from(pixels.0)
247 }
248}