From f9dd5cbb099bbe44a57b6369be54a442363b7a8d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 12 Jul 2024 15:11:30 +0200 Subject: Introduce helper methods for alignment for all widgets --- core/src/alignment.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ core/src/pixels.rs | 26 +++++++++++++++++++++++++- core/src/widget/text.rs | 47 +++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 111 insertions(+), 7 deletions(-) (limited to 'core/src') diff --git a/core/src/alignment.rs b/core/src/alignment.rs index 51b7fca9..cacf7ce3 100644 --- a/core/src/alignment.rs +++ b/core/src/alignment.rs @@ -1,5 +1,30 @@ //! Align and position widgets. +/// Returns a value representing center alignment. +pub const fn center() -> Alignment { + Alignment::Center +} + +/// Returns a value representing left alignment. +pub const fn left() -> Horizontal { + Horizontal::Left +} + +/// Returns a value representing right alignment. +pub const fn right() -> Horizontal { + Horizontal::Right +} + +/// Returns a value representing top alignment. +pub const fn top() -> Vertical { + Vertical::Top +} + +/// Returns a value representing bottom alignment. +pub const fn bottom() -> Vertical { + Vertical::Bottom +} + /// Alignment on the axis of a container. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Alignment { @@ -46,6 +71,16 @@ pub enum Horizontal { Right, } +impl From 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 +93,13 @@ pub enum Vertical { /// Align bottom Bottom, } + +impl From 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/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`) 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 for Pixels { @@ -27,6 +27,30 @@ impl From 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 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 for Pixels { type Output = Pixels; diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 081407e5..6ae95c8b 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -86,21 +86,56 @@ where self } + /// Centers the [`Text`], both horizontally and vertically. + pub fn center(self) -> Self { + self.center_x().center_y() + } + + /// Centers the [`Text`] horizontally. + pub fn center_x(self) -> Self { + self.align_x(alignment::center()) + } + + /// Aligns the [`Text`] to the left, the default. + pub fn align_left(self) -> Self { + self.align_x(alignment::left()) + } + + /// Aligns the [`Text`] to the right. + pub fn align_right(self) -> Self { + self.align_x(alignment::right()) + } + + /// Centers the [`Text`] vertically. + pub fn center_y(self) -> Self { + self.align_y(alignment::center()) + } + + /// Aligns the [`Text`] to the top, the default. + pub fn align_top(self) -> Self { + self.align_y(alignment::top()) + } + + /// Aligns the [`Text`] to the bottom. + pub fn align_bottom(self) -> Self { + self.align_y(alignment::bottom()) + } + /// Sets the [`alignment::Horizontal`] of the [`Text`]. - pub fn horizontal_alignment( + pub fn align_x( mut self, - alignment: alignment::Horizontal, + alignment: impl Into, ) -> 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, ) -> Self { - self.vertical_alignment = alignment; + self.vertical_alignment = alignment.into(); self } -- cgit From 76737351ea9e116291112b7d576d9ed4f6bb5c2a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 12 Jul 2024 18:12:34 +0200 Subject: Re-export variants of `Length` and `alignment` types --- core/src/alignment.rs | 25 ------------------------- core/src/widget/text.rs | 33 ++------------------------------- 2 files changed, 2 insertions(+), 56 deletions(-) (limited to 'core/src') diff --git a/core/src/alignment.rs b/core/src/alignment.rs index cacf7ce3..8f01ef71 100644 --- a/core/src/alignment.rs +++ b/core/src/alignment.rs @@ -1,30 +1,5 @@ //! Align and position widgets. -/// Returns a value representing center alignment. -pub const fn center() -> Alignment { - Alignment::Center -} - -/// Returns a value representing left alignment. -pub const fn left() -> Horizontal { - Horizontal::Left -} - -/// Returns a value representing right alignment. -pub const fn right() -> Horizontal { - Horizontal::Right -} - -/// Returns a value representing top alignment. -pub const fn top() -> Vertical { - Vertical::Top -} - -/// Returns a value representing bottom alignment. -pub const fn bottom() -> Vertical { - Vertical::Bottom -} - /// Alignment on the axis of a container. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Alignment { diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 6ae95c8b..990c5567 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -88,37 +88,8 @@ where /// Centers the [`Text`], both horizontally and vertically. pub fn center(self) -> Self { - self.center_x().center_y() - } - - /// Centers the [`Text`] horizontally. - pub fn center_x(self) -> Self { - self.align_x(alignment::center()) - } - - /// Aligns the [`Text`] to the left, the default. - pub fn align_left(self) -> Self { - self.align_x(alignment::left()) - } - - /// Aligns the [`Text`] to the right. - pub fn align_right(self) -> Self { - self.align_x(alignment::right()) - } - - /// Centers the [`Text`] vertically. - pub fn center_y(self) -> Self { - self.align_y(alignment::center()) - } - - /// Aligns the [`Text`] to the top, the default. - pub fn align_top(self) -> Self { - self.align_y(alignment::top()) - } - - /// Aligns the [`Text`] to the bottom. - pub fn align_bottom(self) -> Self { - self.align_y(alignment::bottom()) + self.align_x(alignment::Horizontal::Center) + .align_y(alignment::Vertical::Center) } /// Sets the [`alignment::Horizontal`] of the [`Text`]. -- cgit From 7c3341760de74df2153ef367e502960f20f9c681 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 12 Jul 2024 18:40:54 +0200 Subject: Improve `Padding` ergonomics We expose free functions for creating a `Padding` and methods with the same name to modify its fields. --- core/src/lib.rs | 2 +- core/src/padding.rs | 102 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 63 insertions(+), 41 deletions(-) (limited to 'core/src') 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..a0915fbc 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`, @@ -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) -> Padding { + Padding::new(padding.into().0) +} + +/// Create some top [`Padding`]. +pub fn top(padding: impl Into) -> Padding { + Padding { + top: padding.into().0, + ..Padding::ZERO + } +} + +/// Create some bottom [`Padding`]. +pub fn bottom(padding: impl Into) -> Padding { + Padding { + bottom: padding.into().0, + ..Padding::ZERO + } +} + +/// Create some left [`Padding`]. +pub fn left(padding: impl Into) -> Padding { + Padding { + left: padding.into().0, + ..Padding::ZERO + } +} + +/// Create some right [`Padding`]. +pub fn right(padding: impl Into) -> 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) -> Self { + /// Sets the [`top`] of the [`Padding`]. + /// + /// [`top`]: Self::top + pub fn top(self, top: impl Into) -> Self { Self { - top: padding.into().0, - ..Self::ZERO + top: top.into().0, + ..self } } - /// Create some right [`Padding`]. - pub fn right(padding: impl Into) -> Self { + /// Sets the [`bottom`] of the [`Padding`]. + /// + /// [`bottom`]: Self::bottom + pub fn bottom(self, bottom: impl Into) -> Self { Self { - right: padding.into().0, - ..Self::ZERO + bottom: bottom.into().0, + ..self } } - /// Create some bottom [`Padding`]. - pub fn bottom(padding: impl Into) -> Self { + /// Sets the [`left`] of the [`Padding`]. + /// + /// [`left`]: Self::left + pub fn left(self, left: impl Into) -> Self { Self { - bottom: padding.into().0, - ..Self::ZERO + left: left.into().0, + ..self } } - /// Create some left [`Padding`]. - pub fn left(padding: impl Into) -> Self { + /// Sets the [`right`] of the [`Padding`]. + /// + /// [`right`]: Self::right + pub fn right(self, right: impl Into) -> 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 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 for Size { fn from(padding: Padding) -> Self { Self::new(padding.horizontal(), padding.vertical()) -- cgit From ab392cee947a7207bdd021d5f04945b9d5a16b4b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 12 Jul 2024 19:10:52 +0200 Subject: Improve `Border` ergonomics --- core/src/border.rs | 164 +++++++++++++++++++++++++++++++++++++++++++--------- core/src/padding.rs | 22 ++----- 2 files changed, 141 insertions(+), 45 deletions(-) (limited to 'core/src') diff --git a/core/src/border.rs b/core/src/border.rs index 2df24988..05e74ac6 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, } -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) -> Self { - Self::default().with_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) -> 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) -> 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) -> Border { + Border::default().width(width) +} - /// Updates the [`Color`] of the [`Border`]. - pub fn with_color(self, color: impl Into) -> Self { +impl Border { + /// Sets the [`Color`] of the [`Border`]. + pub fn color(self, color: impl Into) -> Self { Self { color: color.into(), ..self } } - /// Updates the [`Radius`] of the [`Border`]. - pub fn with_radius(self, radius: impl Into) -> Self { + /// Sets the [`Radius`] of the [`Border`]. + pub fn rounded(self, radius: impl Into) -> Self { Self { radius: radius.into(), ..self } } - /// Updates the width of the [`Border`]. - pub fn with_width(self, width: impl Into) -> Self { + /// Sets the width of the [`Border`]. + pub fn width(self, width: impl Into) -> Self { Self { width: width.into().0, ..self @@ -54,11 +78,96 @@ 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) -> Radius { + Radius::new(value) +} + +/// Creates a new [`Radius`] with the given top left value. +pub fn top_left(value: impl Into) -> Radius { + Radius::default().top_left(value) +} + +/// Creates a new [`Radius`] with the given top right value. +pub fn top_right(value: impl Into) -> Radius { + Radius::default().top_right(value) +} + +/// Creates a new [`Radius`] with the given bottom right value. +pub fn bottom_right(value: impl Into) -> Radius { + Radius::default().bottom_right(value) +} + +/// Creates a new [`Radius`] with the given bottom left value. +pub fn bottom_left(value: impl Into) -> Radius { + Radius::default().bottom_left(value) +} + +impl Radius { + /// Creates a new [`Radius`] with the same value for each corner. + pub fn new(value: impl Into) -> 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) -> Self { + Self { + top_left: value.into().0, + ..self + } + } + + /// Sets the top right value of the [`Radius`]. + pub fn top_right(self, value: impl Into) -> Self { + Self { + top_right: value.into().0, + ..self + } + } + + /// Sets the bottom right value of the [`Radius`]. + pub fn bottom_right(self, value: impl Into) -> Self { + Self { + bottom_right: value.into().0, + ..self + } + } + + /// Sets the bottom left value of the [`Radius`]. + pub fn bottom_left(self, value: impl Into) -> Self { + Self { + bottom_left: value.into().0, + ..self + } + } +} impl From 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 +189,13 @@ impl From for Radius { } } -impl From<[f32; 4]> for Radius { - fn from(radi: [f32; 4]) -> Self { - Self(radi) - } -} - impl From 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/padding.rs b/core/src/padding.rs index a0915fbc..fdaa0236 100644 --- a/core/src/padding.rs +++ b/core/src/padding.rs @@ -32,7 +32,7 @@ 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 /// ``` -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, Default)] pub struct Padding { /// Top padding pub top: f32, @@ -51,34 +51,22 @@ pub fn all(padding: impl Into) -> Padding { /// Create some top [`Padding`]. pub fn top(padding: impl Into) -> Padding { - Padding { - top: padding.into().0, - ..Padding::ZERO - } + Padding::default().top(padding) } /// Create some bottom [`Padding`]. pub fn bottom(padding: impl Into) -> Padding { - Padding { - bottom: padding.into().0, - ..Padding::ZERO - } + Padding::default().bottom(padding) } /// Create some left [`Padding`]. pub fn left(padding: impl Into) -> Padding { - Padding { - left: padding.into().0, - ..Padding::ZERO - } + Padding::default().left(padding) } /// Create some right [`Padding`]. pub fn right(padding: impl Into) -> Padding { - Padding { - right: padding.into().0, - ..Padding::ZERO - } + Padding::default().right(padding) } impl Padding { -- cgit From 2513213e8953fbaccb9ece1cabd3697cbca8aa2c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 12 Jul 2024 19:35:01 +0200 Subject: Add directional `border::Radius` helpers --- core/src/border.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++ core/src/border_radius.rs | 22 ---------------- 2 files changed, 64 insertions(+), 22 deletions(-) delete mode 100644 core/src/border_radius.rs (limited to 'core/src') diff --git a/core/src/border.rs b/core/src/border.rs index 05e74ac6..da0aaa28 100644 --- a/core/src/border.rs +++ b/core/src/border.rs @@ -114,6 +114,26 @@ pub fn bottom_left(value: impl Into) -> 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) -> 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) -> 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) -> 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) -> Radius { + Radius::default().right(value) +} + impl Radius { /// Creates a new [`Radius`] with the same value for each corner. pub fn new(value: impl Into) -> Self { @@ -158,6 +178,50 @@ impl Radius { ..self } } + + /// Sets the top left and top right values of the [`Radius`]. + pub fn top(self, value: impl Into) -> 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) -> 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) -> 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) -> Self { + let value = value.into().0; + + Self { + top_right: value, + bottom_right: value, + ..self + } + } } impl From for Radius { 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 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 for [f32; 4] { - fn from(radi: BorderRadius) -> Self { - radi.0 - } -} -- cgit