From fe0a27c56d9d75fb521e69352259f1d737402a20 Mon Sep 17 00:00:00 2001 From: Ben LeFevre Date: Mon, 23 Nov 2020 17:19:21 +0000 Subject: Add support for asymmetrical padding --- core/src/lib.rs | 2 ++ core/src/padding.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ core/src/size.rs | 8 +++---- 3 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 core/src/padding.rs (limited to 'core/src') diff --git a/core/src/lib.rs b/core/src/lib.rs index f2d21a5f..6453d599 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -22,6 +22,7 @@ mod background; mod color; mod font; mod length; +mod padding; mod point; mod rectangle; mod size; @@ -32,6 +33,7 @@ pub use background::Background; pub use color::Color; pub use font::Font; pub use length::Length; +pub use padding::Padding; pub use point::Point; pub use rectangle::Rectangle; pub use size::Size; diff --git a/core/src/padding.rs b/core/src/padding.rs new file mode 100644 index 00000000..083f7eab --- /dev/null +++ b/core/src/padding.rs @@ -0,0 +1,65 @@ +/// An amount of space to pad for each side of a box +#[derive(Debug, Hash, Copy, Clone)] +pub struct Padding { + /// Top padding + pub top: u16, + /// Right padding + pub right: u16, + /// Bottom padding + pub bottom: u16, + /// Left padding + pub left: u16, +} + +impl Padding { + /// Padding of zero + pub const ZERO: Padding = Padding { + top: 0, + right: 0, + bottom: 0, + left: 0, + }; + + /// Create a Padding that is equal on all sides + pub const fn new(padding: u16) -> Padding { + Padding { + top: padding, + right: padding, + bottom: padding, + left: padding, + } + } +} + +impl std::convert::From for Padding { + fn from(p: u16) -> Self { + Padding { + top: p, + right: p, + bottom: p, + left: p, + } + } +} + +impl std::convert::From<[u16; 2]> for Padding { + fn from(p: [u16; 2]) -> Self { + Padding { + top: p[0], + right: p[1], + bottom: p[0], + left: p[1], + } + } +} + +impl std::convert::From<[u16; 4]> for Padding { + fn from(p: [u16; 4]) -> Self { + Padding { + top: p[0], + right: p[1], + bottom: p[2], + left: p[3], + } + } +} diff --git a/core/src/size.rs b/core/src/size.rs index 9ea9e686..712caee2 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -1,4 +1,4 @@ -use crate::Vector; +use crate::{Padding, Vector}; use std::f32; /// An amount of space in 2 dimensions. @@ -28,10 +28,10 @@ impl Size { pub const INFINITY: Size = Size::new(f32::INFINITY, f32::INFINITY); /// Increments the [`Size`] to account for the given padding. - pub fn pad(&self, padding: f32) -> Self { + pub fn pad(&self, padding: Padding) -> Self { Size { - width: self.width + padding * 2.0, - height: self.height + padding * 2.0, + width: self.width + (padding.left + padding.right) as f32, + height: self.height + (padding.top + padding.bottom) as f32, } } } -- cgit From d83e263abe3458808799e2afca159c2ddab5e10f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Tue, 1 Jun 2021 19:13:11 +0700 Subject: Introduce `vertical` and `horizontal` methods to `Padding` --- core/src/padding.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'core/src') diff --git a/core/src/padding.rs b/core/src/padding.rs index 083f7eab..6eafd44d 100644 --- a/core/src/padding.rs +++ b/core/src/padding.rs @@ -29,6 +29,16 @@ impl Padding { left: padding, } } + + /// Returns the total amount of vertical [`Padding`]. + pub fn vertical(self) -> u16 { + self.top + self.bottom + } + + /// Returns the total amount of horizontal [`Padding`]. + pub fn horizontal(self) -> u16 { + self.left + self.right + } } impl std::convert::From for Padding { -- cgit From b94cd7a2a83d81769d31f6379539089ce68cbdcd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Tue, 1 Jun 2021 19:21:43 +0700 Subject: Use `Padding::horizontal` and `Padding::vertical` helpers --- core/src/size.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'core/src') diff --git a/core/src/size.rs b/core/src/size.rs index 712caee2..6745c6c8 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -30,8 +30,8 @@ impl Size { /// Increments the [`Size`] to account for the given padding. pub fn pad(&self, padding: Padding) -> Self { Size { - width: self.width + (padding.left + padding.right) as f32, - height: self.height + (padding.top + padding.bottom) as f32, + width: self.width + padding.horizontal() as f32, + height: self.height + padding.vertical() as f32, } } } -- cgit From 8a3b71df8b619571ce0a972826cb5a3987b66b3d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Tue, 1 Jun 2021 19:45:47 +0700 Subject: Replace ignored doc-tests with additional documentation for `Padding` --- core/src/padding.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'core/src') diff --git a/core/src/padding.rs b/core/src/padding.rs index 6eafd44d..22467d6b 100644 --- a/core/src/padding.rs +++ b/core/src/padding.rs @@ -1,4 +1,36 @@ /// An amount of space to pad for each side of a box +/// +/// You can leverage the `From` trait to build [`Padding`] conveniently: +/// +/// ``` +/// # use iced_core::Padding; +/// # +/// 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`, +/// so you can easily write: +/// +/// ``` +/// # use iced_core::Padding; +/// # +/// # struct Widget; +/// # +/// impl Widget { +/// # pub fn new() -> Self { Self } +/// # +/// pub fn padding(mut self, padding: impl Into) -> Self { +/// // ... +/// self +/// } +/// } +/// +/// 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, Hash, Copy, Clone)] pub struct Padding { /// Top padding -- cgit