diff options
author | 2024-02-19 09:11:36 +0100 | |
---|---|---|
committer | 2024-02-19 09:11:36 +0100 | |
commit | e77a6add8ae7dcded0bdecc72f3cdf455f30e79f (patch) | |
tree | 1ac56b9f98ab4d020bda0913a8f4e06f91869470 /widget | |
parent | 121d220532c14e6fa85e333ccf4271477298445b (diff) | |
parent | 626604b8afcd649b408d64cd9e767b822bb32b8f (diff) | |
download | iced-e77a6add8ae7dcded0bdecc72f3cdf455f30e79f.tar.gz iced-e77a6add8ae7dcded0bdecc72f3cdf455f30e79f.tar.bz2 iced-e77a6add8ae7dcded0bdecc72f3cdf455f30e79f.zip |
Merge pull request #2264 from iced-rs/column-row-ergonomics
`extend` and `from_vec` methods for `Column` and `Row`
Diffstat (limited to '')
-rw-r--r-- | widget/src/column.rs | 39 | ||||
-rw-r--r-- | widget/src/row.rs | 39 |
2 files changed, 60 insertions, 18 deletions
diff --git a/widget/src/column.rs b/widget/src/column.rs index b9eb5d93..e59a0809 100644 --- a/widget/src/column.rs +++ b/widget/src/column.rs @@ -30,7 +30,27 @@ where { /// Creates an empty [`Column`]. pub fn new() -> Self { - Column { + Self::from_vec(Vec::new()) + } + + /// Creates a [`Column`] with the given elements. + pub fn with_children( + children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>, + ) -> Self { + Self::new().extend(children) + } + + /// Creates a [`Column`] from an already allocated [`Vec`]. + /// + /// Keep in mind that the [`Column`] will not inspect the [`Vec`], which means + /// it won't automatically adapt to the sizing strategy of its contents. + /// + /// If any of the children have a [`Length::Fill`] strategy, you will need to + /// call [`Column::width`] or [`Column::height`] accordingly. + pub fn from_vec( + children: Vec<Element<'a, Message, Theme, Renderer>>, + ) -> Self { + Self { spacing: 0.0, padding: Padding::ZERO, width: Length::Shrink, @@ -38,17 +58,10 @@ where max_width: f32::INFINITY, align_items: Alignment::Start, clip: false, - children: Vec::new(), + children, } } - /// Creates a [`Column`] with the given elements. - pub fn with_children( - children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>, - ) -> Self { - children.into_iter().fold(Self::new(), Self::push) - } - /// Sets the vertical spacing _between_ elements. /// /// Custom margins per element do not exist in iced. You should use this @@ -127,6 +140,14 @@ where self } } + + /// Extends the [`Column`] with the given children. + pub fn extend( + self, + children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>, + ) -> Self { + children.into_iter().fold(self, Self::push) + } } impl<'a, Message, Renderer> Default for Column<'a, Message, Renderer> diff --git a/widget/src/row.rs b/widget/src/row.rs index 20b47a41..b41b5380 100644 --- a/widget/src/row.rs +++ b/widget/src/row.rs @@ -28,24 +28,37 @@ where { /// Creates an empty [`Row`]. pub fn new() -> Self { - Row { + Self::from_vec(Vec::new()) + } + + /// Creates a [`Row`] with the given elements. + pub fn with_children( + children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>, + ) -> Self { + Self::new().extend(children) + } + + /// Creates a [`Row`] from an already allocated [`Vec`]. + /// + /// Keep in mind that the [`Row`] will not inspect the [`Vec`], which means + /// it won't automatically adapt to the sizing strategy of its contents. + /// + /// If any of the children have a [`Length::Fill`] strategy, you will need to + /// call [`Row::width`] or [`Row::height`] accordingly. + pub fn from_vec( + children: Vec<Element<'a, Message, Theme, Renderer>>, + ) -> Self { + Self { spacing: 0.0, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, align_items: Alignment::Start, clip: false, - children: Vec::new(), + children, } } - /// Creates a [`Row`] with the given elements. - pub fn with_children( - children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>, - ) -> Self { - children.into_iter().fold(Self::new(), Self::push) - } - /// Sets the horizontal spacing _between_ elements. /// /// Custom margins per element do not exist in iced. You should use this @@ -118,6 +131,14 @@ where self } } + + /// Extends the [`Row`] with the given children. + pub fn extend( + self, + children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>, + ) -> Self { + children.into_iter().fold(self, Self::push) + } } impl<'a, Message, Renderer> Default for Row<'a, Message, Renderer> |