From 02e60e104934e145dceccee97cf381f52d516bea Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Wed, 16 Oct 2024 15:39:01 -0700 Subject: [PATCH] IntoWidgetList Fixes #182 --- CHANGELOG.md | 10 ++++++++++ examples/file-picker.rs | 2 +- src/widget.rs | 37 ++++++++++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a502e66f..a7e9d218a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 current outer size. - `Graphics::draw_texture` and `Graphics::draw_textured_shape` now both accept an opactiy parameter controlling how opaque the texture should be rendered at. +- `MakeWigetList` has had some of its functions moved to a new trait: + `IntoWidgetList`. A blanket implementation of `MakeWidgetList` is provided + for types that implement `IntoWidgetList`. In general, compilation errors + related to this change can be fixed by importing `IntoWidgetList`. + + - `MakeWidgetList::into_rows` -> `IntoWidgetList::into_rows` + - `MakeWidgetList::into_columns` -> `IntoWidgetList::into_columns` + - `MakeWidgetList::into_layers` -> `IntoWidgetList::into_layers` + - `MakeWidgetList::into_wrap` -> `IntoWidgetList::into_wrap` + - `MakeWidgetList::into_list` -> `IntoWidgetList::into_list` ### Changed diff --git a/examples/file-picker.rs b/examples/file-picker.rs index 87e35cf1a..237935cb6 100644 --- a/examples/file-picker.rs +++ b/examples/file-picker.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use cushy::dialog::{FilePicker, PickFile}; use cushy::value::{Destination, Dynamic, Source}; -use cushy::widget::{MakeWidget, MakeWidgetList}; +use cushy::widget::{IntoWidgetList, MakeWidget}; use cushy::widgets::button::ButtonClick; use cushy::widgets::checkbox::Checkable; use cushy::widgets::layers::Modal; diff --git a/src/widget.rs b/src/widget.rs index 53d8c393a..8668f217a 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -2395,37 +2395,64 @@ pub trait MakeWidgetList: Sized { list.push(widget); list } +} + +/// A type that can be converted to a `Value`. +pub trait IntoWidgetList: Sized { + /// Returns this list of widgets as a `Value`. + fn into_widget_list(self) -> Value; /// Returns `self` as a vertical [`Stack`] of rows. #[must_use] fn into_rows(self) -> Stack { - Stack::rows(self.make_widget_list()) + Stack::rows(self.into_widget_list()) } /// Returns `self` as a horizontal [`Stack`] of columns. #[must_use] fn into_columns(self) -> Stack { - Stack::columns(self.make_widget_list()) + Stack::columns(self.into_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()) + Layers::new(self.into_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()) + Wrap::new(self.into_widget_list()) } /// Returns `self` as an unordered [`List`]. #[must_use] fn into_list(self) -> List { - List::new(self.make_widget_list()) + List::new(self.into_widget_list()) + } +} + +impl IntoWidgetList for T +where + T: MakeWidgetList, +{ + fn into_widget_list(self) -> Value { + Value::Constant(self.make_widget_list()) + } +} + +impl IntoWidgetList for Dynamic { + fn into_widget_list(self) -> Value { + Value::Dynamic(self) + } +} + +impl IntoWidgetList for Value { + fn into_widget_list(self) -> Value { + self } }