Improve consistency of Padding API

This commit is contained in:
Héctor Ramón Jiménez 2025-11-18 21:33:13 +01:00
parent 53110f0ede
commit 5b029ae61c
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
6 changed files with 48 additions and 29 deletions

View file

@ -69,20 +69,14 @@ pub fn right(padding: impl Into<Pixels>) -> Padding {
Padding::default().right(padding)
}
/// Create a [`Padding`] with equal left and right sides.
/// Create some [`Padding`] with equal left and right sides.
pub fn horizontal(padding: impl Into<Pixels>) -> Padding {
let padding: Pixels = padding.into();
Padding::default()
.left(padding.clone())
.right(padding)
Padding::default().horizontal(padding)
}
/// Create a [`Padding`] with equal top and bottom sides.
/// Create some [`Padding`] with equal top and bottom sides.
pub fn vertical(padding: impl Into<Pixels>) -> Padding {
let padding: Pixels = padding.into();
Padding::default()
.top(padding.clone())
.bottom(padding)
Padding::default().vertical(padding)
}
impl Padding {
@ -144,16 +138,44 @@ impl Padding {
}
}
/// Returns the total amount of vertical [`Padding`].
pub fn vertical(self) -> f32 {
self.top + self.bottom
/// Sets the [`left`] and [`right`] of the [`Padding`].
///
/// [`left`]: Self::left
/// [`right`]: Self::right
pub fn horizontal(self, horizontal: impl Into<Pixels>) -> Self {
let horizontal = horizontal.into();
Self {
left: horizontal.0,
right: horizontal.0,
..self
}
}
/// Sets the [`top`] and [`bottom`] of the [`Padding`].
///
/// [`top`]: Self::top
/// [`bottom`]: Self::bottom
pub fn vertical(self, vertical: impl Into<Pixels>) -> Self {
let vertical = vertical.into();
Self {
top: vertical.0,
bottom: vertical.0,
..self
}
}
/// Returns the total amount of horizontal [`Padding`].
pub fn horizontal(self) -> f32 {
pub fn x(self) -> f32 {
self.left + self.right
}
/// Returns the total amount of vertical [`Padding`].
pub fn y(self) -> f32 {
self.top + self.bottom
}
/// Fits the [`Padding`] between the provided `inner` and `outer` [`Size`].
pub fn fit(self, inner: Size, outer: Size) -> Self {
let available = (outer - inner).max(Size::ZERO);
@ -215,7 +237,7 @@ impl From<[f32; 2]> for Padding {
impl From<Padding> for Size {
fn from(padding: Padding) -> Self {
Self::new(padding.horizontal(), padding.vertical())
Self::new(padding.x(), padding.y())
}
}