Skip to content

Commit

Permalink
Merge pull request #2 from cryptowatch/pure-qol
Browse files Browse the repository at this point in the history
Add `column!` and `row!` macros in `iced_pure`
  • Loading branch information
hecrj authored Oct 19, 2022
2 parents d4cd792 + fd26202 commit 55b0518
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 70 deletions.
13 changes: 6 additions & 7 deletions examples/pure/game_of_life/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,12 @@ impl Application for GameOfLife {
self.grid.preset(),
);

let content = column()
.push(
self.grid
.view()
.map(move |message| Message::Grid(message, version)),
)
.push(controls);
let content = column![
self.grid
.view()
.map(move |message| Message::Grid(message, version)),
controls
];

container(content)
.width(Length::Fill)
Expand Down
26 changes: 26 additions & 0 deletions pure/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@ use iced_native::Length;
use std::borrow::Cow;
use std::ops::RangeInclusive;

/// Creates a [`Column`] with the given children.
///
/// [`Column`]: widget::Column
#[macro_export]
macro_rules! column {
() => (
$crate::widget::Column::new()
);
($($x:expr),+ $(,)?) => (
$crate::widget::Column::with_children(vec![$($crate::Element::from($x)),+])
);
}

/// Creates a [`Row`] with the given children.
///
/// [`Row`]: widget::Row
#[macro_export]
macro_rules! row {
() => (
$crate::widget::Row::new()
);
($($x:expr),+ $(,)?) => (
$crate::widget::Row::with_children(vec![$($crate::Element::from($x)),+])
);
}

/// Creates a new [`Container`] with the provided content.
///
/// [`Container`]: widget::Container
Expand Down
8 changes: 4 additions & 4 deletions pure/src/widget/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,14 @@ where
}
}

impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>>
for Button<'a, Message, Renderer>
impl<'a, Message, Renderer> From<Button<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Message: Clone + 'a,
Renderer: iced_native::Renderer + 'a,
Renderer::Theme: StyleSheet,
{
fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self)
fn from(button: Button<'a, Message, Renderer>) -> Self {
Self::new(button)
}
}
12 changes: 7 additions & 5 deletions pure/src/widget/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,16 @@ where
}
}

impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>>
for Checkbox<'a, Message, Renderer>
impl<'a, Message, Renderer> From<Checkbox<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Message: 'a,
Renderer: text::Renderer + 'a,
Renderer: 'a + text::Renderer,
Renderer::Theme: StyleSheet + widget::text::StyleSheet,
{
fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self)
fn from(
checkbox: Checkbox<'a, Message, Renderer>,
) -> Element<'a, Message, Renderer> {
Element::new(checkbox)
}
}
8 changes: 4 additions & 4 deletions pure/src/widget/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,13 @@ where
}
}

impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>>
for Column<'a, Message, Renderer>
impl<'a, Message, Renderer> From<Column<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Message: 'a,
Renderer: iced_native::Renderer + 'a,
{
fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self)
fn from(column: Column<'a, Message, Renderer>) -> Self {
Self::new(column)
}
}
11 changes: 5 additions & 6 deletions pure/src/widget/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,13 @@ where
}
}

impl<'a, Message, Renderer, Handle> Into<Element<'a, Message, Renderer>>
for Image<Handle>
impl<'a, Message, Renderer, Handle> From<Image<Handle>>
for Element<'a, Message, Renderer>
where
Message: Clone + 'a,
Renderer: iced_native::image::Renderer<Handle = Handle> + 'a,
Renderer: iced_native::image::Renderer<Handle = Handle>,
Handle: Clone + Hash + 'a,
{
fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self)
fn from(image: Image<Handle>) -> Element<'a, Message, Renderer> {
Element::new(image)
}
}
8 changes: 4 additions & 4 deletions pure/src/widget/pick_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,16 @@ where
}
}

impl<'a, T: 'a, Message, Renderer> Into<Element<'a, Message, Renderer>>
for PickList<'a, T, Message, Renderer>
impl<'a, T: 'a, Message, Renderer> From<PickList<'a, T, Message, Renderer>>
for Element<'a, Message, Renderer>
where
T: Clone + ToString + Eq + 'static,
[T]: ToOwned<Owned = Vec<T>>,
Message: 'a,
Renderer: text::Renderer + 'a,
Renderer::Theme: StyleSheet,
{
fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self)
fn from(pick_list: PickList<'a, T, Message, Renderer>) -> Self {
Self::new(pick_list)
}
}
13 changes: 8 additions & 5 deletions pure/src/widget/progress_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,16 @@ where
}
}

impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>>
for ProgressBar<Renderer>
impl<'a, Message, Renderer> From<ProgressBar<Renderer>>
for Element<'a, Message, Renderer>
where
Renderer: iced_native::Renderer + 'a,
Message: 'a,
Renderer: 'a + iced_native::Renderer,
Renderer::Theme: StyleSheet,
{
fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self)
fn from(
progress_bar: ProgressBar<Renderer>,
) -> Element<'a, Message, Renderer> {
Element::new(progress_bar)
}
}
10 changes: 5 additions & 5 deletions pure/src/widget/radio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ where
}
}

impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>>
for Radio<Message, Renderer>
impl<'a, Message, Renderer> From<Radio<Message, Renderer>>
for Element<'a, Message, Renderer>
where
Message: 'a + Clone,
Renderer: text::Renderer + 'a,
Renderer: 'a + text::Renderer,
Renderer::Theme: StyleSheet + widget::text::StyleSheet,
{
fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self)
fn from(radio: Radio<Message, Renderer>) -> Element<'a, Message, Renderer> {
Element::new(radio)
}
}
8 changes: 4 additions & 4 deletions pure/src/widget/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ where
}
}

impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>>
for Row<'a, Message, Renderer>
impl<'a, Message, Renderer> From<Row<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Message: 'a,
Renderer: iced_native::Renderer + 'a,
{
fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self)
fn from(row: Row<'a, Message, Renderer>) -> Self {
Self::new(row)
}
}
11 changes: 6 additions & 5 deletions pure/src/widget/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,14 @@ where
}
}

impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>>
for Rule<Renderer>
impl<'a, Message, Renderer> From<Rule<Renderer>>
for Element<'a, Message, Renderer>
where
Renderer: iced_native::Renderer + 'a,
Message: 'a,
Renderer: 'a + iced_native::Renderer,
Renderer::Theme: StyleSheet,
{
fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self)
fn from(rule: Rule<Renderer>) -> Element<'a, Message, Renderer> {
Element::new(rule)
}
}
9 changes: 5 additions & 4 deletions pure/src/widget/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ where
}
}

impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>> for Space
impl<'a, Message, Renderer> From<Space> for Element<'a, Message, Renderer>
where
Renderer: iced_native::Renderer + 'a,
Renderer: iced_native::Renderer,
Message: 'a,
{
fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self)
fn from(space: Space) -> Element<'a, Message, Renderer> {
Element::new(space)
}
}
9 changes: 4 additions & 5 deletions pure/src/widget/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ where
}
}

impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>> for Svg
impl<'a, Message, Renderer> From<Svg> for Element<'a, Message, Renderer>
where
Message: Clone + 'a,
Renderer: iced_native::svg::Renderer + 'a,
Renderer: iced_native::svg::Renderer,
{
fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self)
fn from(icon: Svg) -> Element<'a, Message, Renderer> {
Element::new(icon)
}
}
14 changes: 7 additions & 7 deletions pure/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ where
}
}

impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>>
for Text<Renderer>
impl<'a, Message, Renderer> From<Text<Renderer>>
for Element<'a, Message, Renderer>
where
Renderer: text::Renderer + 'a,
Renderer::Theme: widget::text::StyleSheet,
{
fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self)
fn from(text: Text<Renderer>) -> Self {
Self::new(text)
}
}

impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>> for &'a str
impl<'a, Message, Renderer> From<&'a str> for Element<'a, Message, Renderer>
where
Renderer: text::Renderer + 'a,
Renderer::Theme: widget::text::StyleSheet,
{
fn into(self) -> Element<'a, Message, Renderer> {
Text::new(self).into()
fn from(content: &'a str) -> Self {
Element::from(Text::new(content))
}
}
12 changes: 7 additions & 5 deletions pure/src/widget/toggler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,16 @@ where
}
}

impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>>
for Toggler<'a, Message, Renderer>
impl<'a, Message, Renderer> From<Toggler<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Message: 'a,
Renderer: text::Renderer + 'a,
Renderer: 'a + text::Renderer,
Renderer::Theme: StyleSheet + widget::text::StyleSheet,
{
fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self)
fn from(
toggler: Toggler<'a, Message, Renderer>,
) -> Element<'a, Message, Renderer> {
Element::new(toggler)
}
}
1 change: 1 addition & 0 deletions src/pure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub use sandbox::Sandbox;

pub use iced_pure::helpers::*;
pub use iced_pure::Widget;
pub use iced_pure::{column, row};
pub use iced_pure::{Pure, State};

/// A generic, pure [`Widget`].
Expand Down

0 comments on commit 55b0518

Please sign in to comment.