From 641f4bf52d185dc326ce3304cdbe3e748f4ba04c Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Mon, 5 Feb 2024 21:44:52 +0100 Subject: [PATCH] Add MakeWidgetList Adds functions to convert `impl IntoIterator` directly into `Stack`, `Layers`, etc. --- src/widget.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/widget.rs b/src/widget.rs index f771c08..3b9f362 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -2190,6 +2190,74 @@ impl<'a> IntoIterator for &'a WidgetList { } } +impl MakeWidgetList for I +where + I::Item: MakeWidget, +{ + fn make_widget_list(self) -> WidgetList { + self.into_iter().collect() + } +} + +/// Allows to convert collections or iterators directly into [`Stack`], [`Layers`], etc. +/// +/// ``` +/// use cushy::widget::{MakeWidget, MakeWidgetList}; +/// +/// vec!["hello", "label"].into_rows(); +/// +/// vec!["hello", "button"] +/// .into_iter() +/// .map(|l| l.into_button()) +/// .into_columns(); +/// ``` +pub trait MakeWidgetList: Sized { + /// Returns self as a `WidgetList`. + fn make_widget_list(self) -> WidgetList; + + /// Adds `widget` to self and returns the updated list. + fn and(self, widget: W) -> WidgetList + where + W: MakeWidget, + { + let mut list = self.make_widget_list(); + list.push(widget); + list + } + + /// Returns `self` as a vertical [`Stack`] of rows. + #[must_use] + fn into_rows(self) -> Stack { + Stack::rows(self.make_widget_list()) + } + + /// Returns `self` as a horizontal [`Stack`] of columns. + #[must_use] + fn into_columns(self) -> Stack { + Stack::columns(self.make_widget_list()) + } + + /// Returns `self` as [`Layers`], with the widgets being stacked in the Z + /// direction. + #[must_use] + fn into_layers(self) -> Layers { + Layers::new(self.make_widget_list()) + } + + /// Returns a [`Wrap`] that lays the children out horizontally, wrapping + /// into additional rows as needed. + #[must_use] + fn into_wrap(self) -> Wrap { + Wrap::new(self.make_widget_list()) + } + + /// Returns `self` as an unordered [`List`]. + #[must_use] + fn into_list(self) -> List { + List::new(self.make_widget_list()) + } +} + /// A change to perform during [`WidgetList::synchronize_with`]. pub enum ChildrenSyncChange { /// Insert a new widget at the given index.