summaryrefslogtreecommitdiffstats
path: root/core/src/padding.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2024-07-12 21:40:46 +0200
committerLibravatar GitHub <noreply@github.com>2024-07-12 21:40:46 +0200
commit8cadd3b99485c344feb18b774c8e6fd6c1ea9dd7 (patch)
tree826fa9e0acdf3a4e003f882c94ff24a4ac9f50e4 /core/src/padding.rs
parentbe06060117da061ad8cad94ab0830c06def6b147 (diff)
parent3f480d3d18c41188bf40ead0a3dc4497316f11ae (diff)
downloadiced-8cadd3b99485c344feb18b774c8e6fd6c1ea9dd7.tar.gz
iced-8cadd3b99485c344feb18b774c8e6fd6c1ea9dd7.tar.bz2
iced-8cadd3b99485c344feb18b774c8e6fd6c1ea9dd7.zip
Merge pull request #2504 from iced-rs/view-ergonomics
Improved `view` ergonomics
Diffstat (limited to 'core/src/padding.rs')
-rw-r--r--core/src/padding.rs92
1 files changed, 51 insertions, 41 deletions
diff --git a/core/src/padding.rs b/core/src/padding.rs
index b8c941d8..fdaa0236 100644
--- a/core/src/padding.rs
+++ b/core/src/padding.rs
@@ -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,9 +31,8 @@ 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)]
+#[derive(Debug, Copy, Clone, Default)]
pub struct Padding {
/// Top padding
pub top: f32,
@@ -45,6 +44,31 @@ 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::default().top(padding)
+}
+
+/// Create some bottom [`Padding`].
+pub fn bottom(padding: impl Into<Pixels>) -> Padding {
+ Padding::default().bottom(padding)
+}
+
+/// Create some left [`Padding`].
+pub fn left(padding: impl Into<Pixels>) -> Padding {
+ Padding::default().left(padding)
+}
+
+/// Create some right [`Padding`].
+pub fn right(padding: impl Into<Pixels>) -> Padding {
+ Padding::default().right(padding)
+}
+
impl Padding {
/// Padding of zero
pub const ZERO: Padding = Padding {
@@ -64,35 +88,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 +175,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 +197,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())