Skip to content

Commit

Permalink
Merge pull request #2264 from iced-rs/column-row-ergonomics
Browse files Browse the repository at this point in the history
`extend` and `from_vec` methods for `Column` and `Row`
  • Loading branch information
hecrj authored Feb 19, 2024
2 parents 121d220 + 626604b commit e77a6ad
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 21 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- `extend` and `from_vec` methods for `Column` and `Row`. [#2264](https://github.com/iced-rs/iced/pull/2264)

### Fixed
- Black images when using OpenGL backend in `iced_wgpu`. [#2259](https://github.com/iced-rs/iced/pull/2259)

Many thanks to...

- @PolyMeilex

## [0.12.0] - 2024-02-15
### Added
Expand Down Expand Up @@ -116,8 +125,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Alpha mode misconfiguration in `iced_wgpu`. [#2231](https://github.com/iced-rs/iced/pull/2231)
- Outdated documentation leading to a dead link. [#2232](https://github.com/iced-rs/iced/pull/2232)

## Patched
- Black images when using OpenGL backend in `iced_wgpu`. [#2259](https://github.com/iced-rs/iced/pull/2259)

Many thanks to...

Expand Down Expand Up @@ -159,7 +166,6 @@ Many thanks to...
- @nicksenger
- @Nisatru
- @nyurik
- @PolyMeilex
- @Remmirad
- @ripytide
- @snaggen
Expand Down
39 changes: 30 additions & 9 deletions widget/src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,38 @@ 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,
height: Length::Shrink,
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
Expand Down Expand Up @@ -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>
Expand Down
39 changes: 30 additions & 9 deletions widget/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit e77a6ad

Please sign in to comment.