summaryrefslogtreecommitdiffstats
path: root/core
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
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 '')
-rw-r--r--core/src/alignment.rs20
-rw-r--r--core/src/border.rs228
-rw-r--r--core/src/border_radius.rs22
-rw-r--r--core/src/lib.rs2
-rw-r--r--core/src/padding.rs92
-rw-r--r--core/src/pixels.rs26
-rw-r--r--core/src/widget/text.rs18
7 files changed, 309 insertions, 99 deletions
diff --git a/core/src/alignment.rs b/core/src/alignment.rs
index 51b7fca9..8f01ef71 100644
--- a/core/src/alignment.rs
+++ b/core/src/alignment.rs
@@ -46,6 +46,16 @@ pub enum Horizontal {
Right,
}
+impl From<Alignment> for Horizontal {
+ fn from(alignment: Alignment) -> Self {
+ match alignment {
+ Alignment::Start => Self::Left,
+ Alignment::Center => Self::Center,
+ Alignment::End => Self::Right,
+ }
+ }
+}
+
/// The vertical [`Alignment`] of some resource.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Vertical {
@@ -58,3 +68,13 @@ pub enum Vertical {
/// Align bottom
Bottom,
}
+
+impl From<Alignment> for Vertical {
+ fn from(alignment: Alignment) -> Self {
+ match alignment {
+ Alignment::Start => Self::Top,
+ Alignment::Center => Self::Center,
+ Alignment::End => Self::Bottom,
+ }
+ }
+}
diff --git a/core/src/border.rs b/core/src/border.rs
index 2df24988..da0aaa28 100644
--- a/core/src/border.rs
+++ b/core/src/border.rs
@@ -10,40 +10,64 @@ pub struct Border {
/// The width of the border.
pub width: f32,
- /// The radius of the border.
+ /// The [`Radius`] of the border.
pub radius: Radius,
}
+/// Creates a new [`Border`] with the given [`Radius`].
+///
+/// ```
+/// # use iced_core::border::{self, Border};
+/// #
+/// assert_eq!(border::rounded(10), Border::default().rounded(10));
+/// ```
+pub fn rounded(radius: impl Into<Radius>) -> Border {
+ Border::default().rounded(radius)
+}
+
+/// Creates a new [`Border`] with the given [`Color`].
+///
+/// ```
+/// # use iced_core::border::{self, Border};
+/// # use iced_core::Color;
+/// #
+/// assert_eq!(border::color(Color::BLACK), Border::default().color(Color::BLACK));
+/// ```
+pub fn color(color: impl Into<Color>) -> Border {
+ Border::default().color(color)
+}
+
+/// Creates a new [`Border`] with the given `width`.
+///
+/// ```
+/// # use iced_core::border::{self, Border};
+/// # use iced_core::Color;
+/// #
+/// assert_eq!(border::width(10), Border::default().width(10));
+/// ```
+pub fn width(width: impl Into<Pixels>) -> Border {
+ Border::default().width(width)
+}
+
impl Border {
- /// Creates a new default rounded [`Border`] with the given [`Radius`].
- ///
- /// ```
- /// # use iced_core::Border;
- /// #
- /// assert_eq!(Border::rounded(10), Border::default().with_radius(10));
- /// ```
- pub fn rounded(radius: impl Into<Radius>) -> Self {
- Self::default().with_radius(radius)
- }
-
- /// Updates the [`Color`] of the [`Border`].
- pub fn with_color(self, color: impl Into<Color>) -> Self {
+ /// Sets the [`Color`] of the [`Border`].
+ pub fn color(self, color: impl Into<Color>) -> Self {
Self {
color: color.into(),
..self
}
}
- /// Updates the [`Radius`] of the [`Border`].
- pub fn with_radius(self, radius: impl Into<Radius>) -> Self {
+ /// Sets the [`Radius`] of the [`Border`].
+ pub fn rounded(self, radius: impl Into<Radius>) -> Self {
Self {
radius: radius.into(),
..self
}
}
- /// Updates the width of the [`Border`].
- pub fn with_width(self, width: impl Into<Pixels>) -> Self {
+ /// Sets the width of the [`Border`].
+ pub fn width(self, width: impl Into<Pixels>) -> Self {
Self {
width: width.into().0,
..self
@@ -54,11 +78,160 @@ impl Border {
/// The border radii for the corners of a graphics primitive in the order:
/// top-left, top-right, bottom-right, bottom-left.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
-pub struct Radius([f32; 4]);
+pub struct Radius {
+ /// Top left radius
+ pub top_left: f32,
+ /// Top right radius
+ pub top_right: f32,
+ /// Bottom right radius
+ pub bottom_right: f32,
+ /// Bottom left radius
+ pub bottom_left: f32,
+}
+
+/// Creates a new [`Radius`] with the same value for each corner.
+pub fn radius(value: impl Into<Pixels>) -> Radius {
+ Radius::new(value)
+}
+
+/// Creates a new [`Radius`] with the given top left value.
+pub fn top_left(value: impl Into<Pixels>) -> Radius {
+ Radius::default().top_left(value)
+}
+
+/// Creates a new [`Radius`] with the given top right value.
+pub fn top_right(value: impl Into<Pixels>) -> Radius {
+ Radius::default().top_right(value)
+}
+
+/// Creates a new [`Radius`] with the given bottom right value.
+pub fn bottom_right(value: impl Into<Pixels>) -> Radius {
+ Radius::default().bottom_right(value)
+}
+
+/// Creates a new [`Radius`] with the given bottom left value.
+pub fn bottom_left(value: impl Into<Pixels>) -> Radius {
+ Radius::default().bottom_left(value)
+}
+
+/// Creates a new [`Radius`] with the given value as top left and top right.
+pub fn top(value: impl Into<Pixels>) -> Radius {
+ Radius::default().top(value)
+}
+
+/// Creates a new [`Radius`] with the given value as bottom left and bottom right.
+pub fn bottom(value: impl Into<Pixels>) -> Radius {
+ Radius::default().bottom(value)
+}
+
+/// Creates a new [`Radius`] with the given value as top left and bottom left.
+pub fn left(value: impl Into<Pixels>) -> Radius {
+ Radius::default().left(value)
+}
+
+/// Creates a new [`Radius`] with the given value as top right and bottom right.
+pub fn right(value: impl Into<Pixels>) -> Radius {
+ Radius::default().right(value)
+}
+
+impl Radius {
+ /// Creates a new [`Radius`] with the same value for each corner.
+ pub fn new(value: impl Into<Pixels>) -> Self {
+ let value = value.into().0;
+
+ Self {
+ top_left: value,
+ top_right: value,
+ bottom_right: value,
+ bottom_left: value,
+ }
+ }
+
+ /// Sets the top left value of the [`Radius`].
+ pub fn top_left(self, value: impl Into<Pixels>) -> Self {
+ Self {
+ top_left: value.into().0,
+ ..self
+ }
+ }
+
+ /// Sets the top right value of the [`Radius`].
+ pub fn top_right(self, value: impl Into<Pixels>) -> Self {
+ Self {
+ top_right: value.into().0,
+ ..self
+ }
+ }
+
+ /// Sets the bottom right value of the [`Radius`].
+ pub fn bottom_right(self, value: impl Into<Pixels>) -> Self {
+ Self {
+ bottom_right: value.into().0,
+ ..self
+ }
+ }
+
+ /// Sets the bottom left value of the [`Radius`].
+ pub fn bottom_left(self, value: impl Into<Pixels>) -> Self {
+ Self {
+ bottom_left: value.into().0,
+ ..self
+ }
+ }
+
+ /// Sets the top left and top right values of the [`Radius`].
+ pub fn top(self, value: impl Into<Pixels>) -> Self {
+ let value = value.into().0;
+
+ Self {
+ top_left: value,
+ top_right: value,
+ ..self
+ }
+ }
+
+ /// Sets the bottom left and bottom right values of the [`Radius`].
+ pub fn bottom(self, value: impl Into<Pixels>) -> Self {
+ let value = value.into().0;
+
+ Self {
+ bottom_left: value,
+ bottom_right: value,
+ ..self
+ }
+ }
+
+ /// Sets the top left and bottom left values of the [`Radius`].
+ pub fn left(self, value: impl Into<Pixels>) -> Self {
+ let value = value.into().0;
+
+ Self {
+ top_left: value,
+ bottom_left: value,
+ ..self
+ }
+ }
+
+ /// Sets the top right and bottom right values of the [`Radius`].
+ pub fn right(self, value: impl Into<Pixels>) -> Self {
+ let value = value.into().0;
+
+ Self {
+ top_right: value,
+ bottom_right: value,
+ ..self
+ }
+ }
+}
impl From<f32> for Radius {
- fn from(w: f32) -> Self {
- Self([w; 4])
+ fn from(radius: f32) -> Self {
+ Self {
+ top_left: radius,
+ top_right: radius,
+ bottom_right: radius,
+ bottom_left: radius,
+ }
}
}
@@ -80,14 +253,13 @@ impl From<i32> for Radius {
}
}
-impl From<[f32; 4]> for Radius {
- fn from(radi: [f32; 4]) -> Self {
- Self(radi)
- }
-}
-
impl From<Radius> for [f32; 4] {
fn from(radi: Radius) -> Self {
- radi.0
+ [
+ radi.top_left,
+ radi.top_right,
+ radi.bottom_right,
+ radi.bottom_left,
+ ]
}
}
diff --git a/core/src/border_radius.rs b/core/src/border_radius.rs
deleted file mode 100644
index a444dd74..00000000
--- a/core/src/border_radius.rs
+++ /dev/null
@@ -1,22 +0,0 @@
-/// The border radii for the corners of a graphics primitive in the order:
-/// top-left, top-right, bottom-right, bottom-left.
-#[derive(Debug, Clone, Copy, PartialEq, Default)]
-pub struct BorderRadius([f32; 4]);
-
-impl From<f32> for BorderRadius {
- fn from(w: f32) -> Self {
- Self([w; 4])
- }
-}
-
-impl From<[f32; 4]> for BorderRadius {
- fn from(radi: [f32; 4]) -> Self {
- Self(radi)
- }
-}
-
-impl From<BorderRadius> for [f32; 4] {
- fn from(radi: BorderRadius) -> Self {
- radi.0
- }
-}
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 32156441..40a288e5 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -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;
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())
diff --git a/core/src/pixels.rs b/core/src/pixels.rs
index 425c0028..f5550a10 100644
--- a/core/src/pixels.rs
+++ b/core/src/pixels.rs
@@ -6,7 +6,7 @@
/// (e.g. `impl Into<Pixels>`) and, since `Pixels` implements `From` both for
/// `f32` and `u16`, you should be able to provide both integers and float
/// literals as needed.
-#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
+#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
pub struct Pixels(pub f32);
impl From<f32> for Pixels {
@@ -27,6 +27,30 @@ impl From<Pixels> for f32 {
}
}
+impl std::ops::Add for Pixels {
+ type Output = Pixels;
+
+ fn add(self, rhs: Self) -> Self {
+ Pixels(self.0 + rhs.0)
+ }
+}
+
+impl std::ops::Add<f32> for Pixels {
+ type Output = Pixels;
+
+ fn add(self, rhs: f32) -> Self {
+ Pixels(self.0 + rhs)
+ }
+}
+
+impl std::ops::Mul for Pixels {
+ type Output = Pixels;
+
+ fn mul(self, rhs: Self) -> Self {
+ Pixels(self.0 * rhs.0)
+ }
+}
+
impl std::ops::Mul<f32> for Pixels {
type Output = Pixels;
diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs
index 081407e5..990c5567 100644
--- a/core/src/widget/text.rs
+++ b/core/src/widget/text.rs
@@ -86,21 +86,27 @@ where
self
}
+ /// Centers the [`Text`], both horizontally and vertically.
+ pub fn center(self) -> Self {
+ self.align_x(alignment::Horizontal::Center)
+ .align_y(alignment::Vertical::Center)
+ }
+
/// Sets the [`alignment::Horizontal`] of the [`Text`].
- pub fn horizontal_alignment(
+ pub fn align_x(
mut self,
- alignment: alignment::Horizontal,
+ alignment: impl Into<alignment::Horizontal>,
) -> Self {
- self.horizontal_alignment = alignment;
+ self.horizontal_alignment = alignment.into();
self
}
/// Sets the [`alignment::Vertical`] of the [`Text`].
- pub fn vertical_alignment(
+ pub fn align_y(
mut self,
- alignment: alignment::Vertical,
+ alignment: impl Into<alignment::Vertical>,
) -> Self {
- self.vertical_alignment = alignment;
+ self.vertical_alignment = alignment.into();
self
}