diff options
| author | 2024-07-12 15:11:30 +0200 | |
|---|---|---|
| committer | 2024-07-12 15:14:43 +0200 | |
| commit | f9dd5cbb099bbe44a57b6369be54a442363b7a8d (patch) | |
| tree | fe16084bc47faadc32d698aa446ea202f7949a4c /core | |
| parent | be06060117da061ad8cad94ab0830c06def6b147 (diff) | |
| download | iced-f9dd5cbb099bbe44a57b6369be54a442363b7a8d.tar.gz iced-f9dd5cbb099bbe44a57b6369be54a442363b7a8d.tar.bz2 iced-f9dd5cbb099bbe44a57b6369be54a442363b7a8d.zip | |
Introduce helper methods for alignment for all widgets
Diffstat (limited to '')
| -rw-r--r-- | core/src/alignment.rs | 45 | ||||
| -rw-r--r-- | core/src/pixels.rs | 26 | ||||
| -rw-r--r-- | core/src/widget/text.rs | 47 | 
3 files changed, 111 insertions, 7 deletions
| 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<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 +93,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/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..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<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      } | 
