iced_core/
padding.rs

1//! Space stuff around the perimeter.
2use crate::{Pixels, Size};
3
4/// An amount of space to pad for each side of a box
5///
6/// You can leverage the `From` trait to build [`Padding`] conveniently:
7///
8/// ```
9/// # use iced_core::Padding;
10/// #
11/// let padding = Padding::from(20);              // 20px on all sides
12/// let padding = Padding::from([10, 20]);        // top/bottom, left/right
13/// ```
14///
15/// Normally, the `padding` method of a widget will ask for an `Into<Padding>`,
16/// so you can easily write:
17///
18/// ```
19/// # use iced_core::Padding;
20/// #
21/// # struct Widget;
22/// #
23/// impl Widget {
24///     # pub fn new() -> Self { Self }
25///     #
26///     pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
27///         // ...
28///         self
29///     }
30/// }
31///
32/// let widget = Widget::new().padding(20);              // 20px on all sides
33/// let widget = Widget::new().padding([10, 20]);        // top/bottom, left/right
34/// ```
35#[derive(Debug, Copy, Clone, PartialEq, Default)]
36pub struct Padding {
37    /// Top padding
38    pub top: f32,
39    /// Right padding
40    pub right: f32,
41    /// Bottom padding
42    pub bottom: f32,
43    /// Left padding
44    pub left: f32,
45}
46
47/// Create a [`Padding`] that is equal on all sides.
48pub fn all(padding: impl Into<Pixels>) -> Padding {
49    Padding::new(padding.into().0)
50}
51
52/// Create some top [`Padding`].
53pub fn top(padding: impl Into<Pixels>) -> Padding {
54    Padding::default().top(padding)
55}
56
57/// Create some bottom [`Padding`].
58pub fn bottom(padding: impl Into<Pixels>) -> Padding {
59    Padding::default().bottom(padding)
60}
61
62/// Create some left [`Padding`].
63pub fn left(padding: impl Into<Pixels>) -> Padding {
64    Padding::default().left(padding)
65}
66
67/// Create some right [`Padding`].
68pub fn right(padding: impl Into<Pixels>) -> Padding {
69    Padding::default().right(padding)
70}
71
72impl Padding {
73    /// Padding of zero
74    pub const ZERO: Padding = Padding {
75        top: 0.0,
76        right: 0.0,
77        bottom: 0.0,
78        left: 0.0,
79    };
80
81    /// Create a [`Padding`] that is equal on all sides.
82    pub const fn new(padding: f32) -> Padding {
83        Padding {
84            top: padding,
85            right: padding,
86            bottom: padding,
87            left: padding,
88        }
89    }
90
91    /// Sets the [`top`] of the [`Padding`].
92    ///
93    /// [`top`]: Self::top
94    pub fn top(self, top: impl Into<Pixels>) -> Self {
95        Self {
96            top: top.into().0,
97            ..self
98        }
99    }
100
101    /// Sets the [`bottom`] of the [`Padding`].
102    ///
103    /// [`bottom`]: Self::bottom
104    pub fn bottom(self, bottom: impl Into<Pixels>) -> Self {
105        Self {
106            bottom: bottom.into().0,
107            ..self
108        }
109    }
110
111    /// Sets the [`left`] of the [`Padding`].
112    ///
113    /// [`left`]: Self::left
114    pub fn left(self, left: impl Into<Pixels>) -> Self {
115        Self {
116            left: left.into().0,
117            ..self
118        }
119    }
120
121    /// Sets the [`right`] of the [`Padding`].
122    ///
123    /// [`right`]: Self::right
124    pub fn right(self, right: impl Into<Pixels>) -> Self {
125        Self {
126            right: right.into().0,
127            ..self
128        }
129    }
130
131    /// Returns the total amount of vertical [`Padding`].
132    pub fn vertical(self) -> f32 {
133        self.top + self.bottom
134    }
135
136    /// Returns the total amount of horizontal [`Padding`].
137    pub fn horizontal(self) -> f32 {
138        self.left + self.right
139    }
140
141    /// Fits the [`Padding`] between the provided `inner` and `outer` [`Size`].
142    pub fn fit(self, inner: Size, outer: Size) -> Self {
143        let available = (outer - inner).max(Size::ZERO);
144        let new_top = self.top.min(available.height);
145        let new_left = self.left.min(available.width);
146
147        Padding {
148            top: new_top,
149            bottom: self.bottom.min(available.height - new_top),
150            left: new_left,
151            right: self.right.min(available.width - new_left),
152        }
153    }
154}
155
156impl From<u16> for Padding {
157    fn from(p: u16) -> Self {
158        Padding {
159            top: f32::from(p),
160            right: f32::from(p),
161            bottom: f32::from(p),
162            left: f32::from(p),
163        }
164    }
165}
166
167impl From<[u16; 2]> for Padding {
168    fn from(p: [u16; 2]) -> Self {
169        Padding {
170            top: f32::from(p[0]),
171            right: f32::from(p[1]),
172            bottom: f32::from(p[0]),
173            left: f32::from(p[1]),
174        }
175    }
176}
177
178impl From<f32> for Padding {
179    fn from(p: f32) -> Self {
180        Padding {
181            top: p,
182            right: p,
183            bottom: p,
184            left: p,
185        }
186    }
187}
188
189impl From<[f32; 2]> for Padding {
190    fn from(p: [f32; 2]) -> Self {
191        Padding {
192            top: p[0],
193            right: p[1],
194            bottom: p[0],
195            left: p[1],
196        }
197    }
198}
199
200impl From<Padding> for Size {
201    fn from(padding: Padding) -> Self {
202        Self::new(padding.horizontal(), padding.vertical())
203    }
204}
205
206impl From<Pixels> for Padding {
207    fn from(pixels: Pixels) -> Self {
208        Self::from(pixels.0)
209    }
210}