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 --- widget/src/container.rs | 61 +++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 22 deletions(-) (limited to 'widget/src/container.rs') diff --git a/widget/src/container.rs b/widget/src/container.rs index 08d5cb17..adfe347c 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -94,27 +94,19 @@ where /// Sets the [`Container`] to fill the available space in the horizontal axis. /// - /// This can be useful to quickly position content when chained with - /// alignment functions—like [`center_x`]. - /// /// Calling this method is equivalent to calling [`width`] with a /// [`Length::Fill`]. /// - /// [`center_x`]: Self::center_x /// [`width`]: Self::width pub fn fill_x(self) -> Self { self.width(Length::Fill) } - /// Sets the [`Container`] to fill the available space in the vetical axis. - /// - /// This can be useful to quickly position content when chained with - /// alignment functions—like [`center_y`]. + /// Sets the [`Container`] to fill the available space in the vertical axis. /// /// Calling this method is equivalent to calling [`height`] with a /// [`Length::Fill`]. /// - /// [`center_y`]: Self::center_x /// [`height`]: Self::height pub fn fill_y(self) -> Self { self.height(Length::Fill) @@ -125,7 +117,6 @@ where /// Calling this method is equivalent to chaining [`fill_x`] and /// [`fill_y`]. /// - /// [`center`]: Self::center /// [`fill_x`]: Self::fill_x /// [`fill_y`]: Self::fill_y pub fn fill(self) -> Self { @@ -144,18 +135,6 @@ where self } - /// Sets the content alignment for the horizontal axis of the [`Container`]. - pub fn align_x(mut self, alignment: alignment::Horizontal) -> Self { - self.horizontal_alignment = alignment; - self - } - - /// Sets the content alignment for the vertical axis of the [`Container`]. - pub fn align_y(mut self, alignment: alignment::Vertical) -> Self { - self.vertical_alignment = alignment; - self - } - /// Sets the width of the [`Container`] and centers its contents horizontally. pub fn center_x(self, width: impl Into) -> Self { self.width(width).align_x(alignment::Horizontal::Center) @@ -179,6 +158,44 @@ where self.center_x(length).center_y(length) } + /// Aligns the contents of the [`Container`] to the left. + pub fn align_left(self) -> Self { + self.align_x(alignment::left()) + } + + /// Aligns the contents of the [`Container`] to the right. + pub fn align_right(self) -> Self { + self.align_x(alignment::right()) + } + + /// Aligns the contents of the [`Container`] to the top. + pub fn align_top(self) -> Self { + self.align_y(alignment::top()) + } + + /// Aligns the contents of the [`Container`] to the bottom. + pub fn align_bottom(self) -> Self { + self.align_y(alignment::bottom()) + } + + /// Sets the content alignment for the horizontal axis of the [`Container`]. + pub fn align_x( + mut self, + alignment: impl Into, + ) -> Self { + self.horizontal_alignment = alignment.into(); + self + } + + /// Sets the content alignment for the vertical axis of the [`Container`]. + pub fn align_y( + mut self, + alignment: impl Into, + ) -> Self { + self.vertical_alignment = alignment.into(); + self + } + /// Sets whether the contents of the [`Container`] should be clipped on /// overflow. pub fn clip(mut self, clip: bool) -> 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 --- widget/src/container.rs | 47 ++++++++--------------------------------------- 1 file changed, 8 insertions(+), 39 deletions(-) (limited to 'widget/src/container.rs') diff --git a/widget/src/container.rs b/widget/src/container.rs index adfe347c..cf27bf96 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -92,37 +92,6 @@ where self } - /// Sets the [`Container`] to fill the available space in the horizontal axis. - /// - /// Calling this method is equivalent to calling [`width`] with a - /// [`Length::Fill`]. - /// - /// [`width`]: Self::width - pub fn fill_x(self) -> Self { - self.width(Length::Fill) - } - - /// Sets the [`Container`] to fill the available space in the vertical axis. - /// - /// Calling this method is equivalent to calling [`height`] with a - /// [`Length::Fill`]. - /// - /// [`height`]: Self::height - pub fn fill_y(self) -> Self { - self.height(Length::Fill) - } - - /// Sets the [`Container`] to fill all the available space. - /// - /// Calling this method is equivalent to chaining [`fill_x`] and - /// [`fill_y`]. - /// - /// [`fill_x`]: Self::fill_x - /// [`fill_y`]: Self::fill_y - pub fn fill(self) -> Self { - self.width(Length::Fill).height(Length::Fill) - } - /// Sets the maximum width of the [`Container`]. pub fn max_width(mut self, max_width: impl Into) -> Self { self.max_width = max_width.into().0; @@ -159,23 +128,23 @@ where } /// Aligns the contents of the [`Container`] to the left. - pub fn align_left(self) -> Self { - self.align_x(alignment::left()) + pub fn align_left(self, width: impl Into) -> Self { + self.width(width).align_x(alignment::Horizontal::Left) } /// Aligns the contents of the [`Container`] to the right. - pub fn align_right(self) -> Self { - self.align_x(alignment::right()) + pub fn align_right(self, width: impl Into) -> Self { + self.width(width).align_x(alignment::Horizontal::Right) } /// Aligns the contents of the [`Container`] to the top. - pub fn align_top(self) -> Self { - self.align_y(alignment::top()) + pub fn align_top(self, height: Length) -> Self { + self.height(height).align_y(alignment::Vertical::Top) } /// Aligns the contents of the [`Container`] to the bottom. - pub fn align_bottom(self) -> Self { - self.align_y(alignment::bottom()) + pub fn align_bottom(self, height: Length) -> Self { + self.height(height).align_y(alignment::Vertical::Bottom) } /// Sets the content alignment for the horizontal axis of the [`Container`]. -- cgit From 915c926c28f77ad7b401a17964408d27548543e6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 12 Jul 2024 18:18:04 +0200 Subject: Fix inconsistent `align_*` methods in `Container` --- widget/src/container.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'widget/src/container.rs') diff --git a/widget/src/container.rs b/widget/src/container.rs index cf27bf96..92b782e8 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -138,12 +138,12 @@ where } /// Aligns the contents of the [`Container`] to the top. - pub fn align_top(self, height: Length) -> Self { + pub fn align_top(self, height: impl Into) -> Self { self.height(height).align_y(alignment::Vertical::Top) } /// Aligns the contents of the [`Container`] to the bottom. - pub fn align_bottom(self, height: Length) -> Self { + pub fn align_bottom(self, height: impl Into) -> Self { self.height(height).align_y(alignment::Vertical::Bottom) } -- 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 --- widget/src/container.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'widget/src/container.rs') diff --git a/widget/src/container.rs b/widget/src/container.rs index 92b782e8..5680bc30 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -1,5 +1,6 @@ //! Decorate content and apply alignment. use crate::core::alignment::{self, Alignment}; +use crate::core::border::{self, Border}; use crate::core::event::{self, Event}; use crate::core::gradient::{self, Gradient}; use crate::core::layout; @@ -9,9 +10,8 @@ use crate::core::renderer; use crate::core::widget::tree::{self, Tree}; use crate::core::widget::{self, Operation}; use crate::core::{ - self, Background, Border, Clipboard, Color, Element, Layout, Length, - Padding, Pixels, Point, Rectangle, Shadow, Shell, Size, Theme, Vector, - Widget, + self, Background, Clipboard, Color, Element, Layout, Length, Padding, + Pixels, Point, Rectangle, Shadow, Shell, Size, Theme, Vector, Widget, }; use crate::runtime::task::{self, Task}; @@ -627,7 +627,7 @@ pub fn rounded_box(theme: &Theme) -> Style { Style { background: Some(palette.background.weak.color.into()), - border: Border::rounded(2), + border: border::rounded(2), ..Style::default() } } -- cgit