Improve Padding ergonomics

We expose free functions for creating a `Padding`
and methods with the same name to modify its fields.
This commit is contained in:
Héctor Ramón Jiménez 2024-07-12 18:40:54 +02:00
parent 915c926c28
commit 7c3341760d
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
5 changed files with 70 additions and 47 deletions

View file

@ -20,6 +20,7 @@ pub mod keyboard;
pub mod layout;
pub mod mouse;
pub mod overlay;
pub mod padding;
pub mod renderer;
pub mod svg;
pub mod text;
@ -35,7 +36,6 @@ mod color;
mod content_fit;
mod element;
mod length;
mod padding;
mod pixels;
mod point;
mod rectangle;

View file

@ -1,3 +1,4 @@
//! Space stuff around the perimeter.
use crate::{Pixels, Size};
/// An amount of space to pad for each side of a box
@ -9,7 +10,6 @@ use crate::{Pixels, Size};
/// #
/// let padding = Padding::from(20); // 20px on all sides
/// let padding = Padding::from([10, 20]); // top/bottom, left/right
/// let padding = Padding::from([5, 10, 15, 20]); // top, right, bottom, left
/// ```
///
/// Normally, the `padding` method of a widget will ask for an `Into<Padding>`,
@ -31,7 +31,6 @@ use crate::{Pixels, Size};
///
/// let widget = Widget::new().padding(20); // 20px on all sides
/// let widget = Widget::new().padding([10, 20]); // top/bottom, left/right
/// let widget = Widget::new().padding([5, 10, 15, 20]); // top, right, bottom, left
/// ```
#[derive(Debug, Copy, Clone)]
pub struct Padding {
@ -45,6 +44,43 @@ pub struct Padding {
pub left: f32,
}
/// Create a [`Padding`] that is equal on all sides.
pub fn all(padding: impl Into<Pixels>) -> Padding {
Padding::new(padding.into().0)
}
/// Create some top [`Padding`].
pub fn top(padding: impl Into<Pixels>) -> Padding {
Padding {
top: padding.into().0,
..Padding::ZERO
}
}
/// Create some bottom [`Padding`].
pub fn bottom(padding: impl Into<Pixels>) -> Padding {
Padding {
bottom: padding.into().0,
..Padding::ZERO
}
}
/// Create some left [`Padding`].
pub fn left(padding: impl Into<Pixels>) -> Padding {
Padding {
left: padding.into().0,
..Padding::ZERO
}
}
/// Create some right [`Padding`].
pub fn right(padding: impl Into<Pixels>) -> Padding {
Padding {
right: padding.into().0,
..Padding::ZERO
}
}
impl Padding {
/// Padding of zero
pub const ZERO: Padding = Padding {
@ -64,35 +100,43 @@ impl Padding {
}
}
/// Create some top [`Padding`].
pub fn top(padding: impl Into<Pixels>) -> Self {
/// Sets the [`top`] of the [`Padding`].
///
/// [`top`]: Self::top
pub fn top(self, top: impl Into<Pixels>) -> Self {
Self {
top: padding.into().0,
..Self::ZERO
top: top.into().0,
..self
}
}
/// Create some right [`Padding`].
pub fn right(padding: impl Into<Pixels>) -> Self {
/// Sets the [`bottom`] of the [`Padding`].
///
/// [`bottom`]: Self::bottom
pub fn bottom(self, bottom: impl Into<Pixels>) -> Self {
Self {
right: padding.into().0,
..Self::ZERO
bottom: bottom.into().0,
..self
}
}
/// Create some bottom [`Padding`].
pub fn bottom(padding: impl Into<Pixels>) -> Self {
/// Sets the [`left`] of the [`Padding`].
///
/// [`left`]: Self::left
pub fn left(self, left: impl Into<Pixels>) -> Self {
Self {
bottom: padding.into().0,
..Self::ZERO
left: left.into().0,
..self
}
}
/// Create some left [`Padding`].
pub fn left(padding: impl Into<Pixels>) -> Self {
/// Sets the [`right`] of the [`Padding`].
///
/// [`right`]: Self::right
pub fn right(self, right: impl Into<Pixels>) -> Self {
Self {
left: padding.into().0,
..Self::ZERO
right: right.into().0,
..self
}
}
@ -143,17 +187,6 @@ impl From<[u16; 2]> for Padding {
}
}
impl From<[u16; 4]> for Padding {
fn from(p: [u16; 4]) -> Self {
Padding {
top: f32::from(p[0]),
right: f32::from(p[1]),
bottom: f32::from(p[2]),
left: f32::from(p[3]),
}
}
}
impl From<f32> for Padding {
fn from(p: f32) -> Self {
Padding {
@ -176,17 +209,6 @@ impl From<[f32; 2]> for Padding {
}
}
impl From<[f32; 4]> for Padding {
fn from(p: [f32; 4]) -> Self {
Padding {
top: p[0],
right: p[1],
bottom: p[2],
left: p[3],
}
}
}
impl From<Padding> for Size {
fn from(padding: Padding) -> Self {
Self::new(padding.horizontal(), padding.vertical())