Skip to content

Commit

Permalink
IntoWidgetList
Browse files Browse the repository at this point in the history
Fixes #182
  • Loading branch information
ecton committed Oct 16, 2024
1 parent 5b92832 commit 02e60e1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion examples/file-picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 32 additions & 5 deletions src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2395,37 +2395,64 @@ pub trait MakeWidgetList: Sized {
list.push(widget);
list
}
}

/// A type that can be converted to a `Value<WidgetList>`.
pub trait IntoWidgetList: Sized {
/// Returns this list of widgets as a `Value<WidgetList>`.
fn into_widget_list(self) -> Value<WidgetList>;

/// 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<T> IntoWidgetList for T
where
T: MakeWidgetList,
{
fn into_widget_list(self) -> Value<WidgetList> {
Value::Constant(self.make_widget_list())
}
}

impl IntoWidgetList for Dynamic<WidgetList> {
fn into_widget_list(self) -> Value<WidgetList> {
Value::Dynamic(self)
}
}

impl IntoWidgetList for Value<WidgetList> {
fn into_widget_list(self) -> Value<WidgetList> {
self
}
}

Expand Down

0 comments on commit 02e60e1

Please sign in to comment.