summaryrefslogtreecommitdiffstats
path: root/core/src
diff options
context:
space:
mode:
authorLibravatar Ben LeFevre <ben@lefev.dev>2020-11-23 17:19:21 +0000
committerLibravatar Héctor Ramón <hector@lich.io>2021-06-01 19:05:39 +0700
commitfe0a27c56d9d75fb521e69352259f1d737402a20 (patch)
treef7f77430b63983717036a81e734276123d139ca6 /core/src
parenta9eb591628017caaf7aa9af505d1206f7a143a9a (diff)
downloadiced-fe0a27c56d9d75fb521e69352259f1d737402a20.tar.gz
iced-fe0a27c56d9d75fb521e69352259f1d737402a20.tar.bz2
iced-fe0a27c56d9d75fb521e69352259f1d737402a20.zip
Add support for asymmetrical padding
Diffstat (limited to 'core/src')
-rw-r--r--core/src/lib.rs2
-rw-r--r--core/src/padding.rs65
-rw-r--r--core/src/size.rs8
3 files changed, 71 insertions, 4 deletions
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<u16> 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,
}
}
}