From 43a7ad72ef070929278e6d03d98077ac267fe2a6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 11 Feb 2022 18:42:15 +0700 Subject: Expose function helpers to build widgets in `pure::widget` `button("Hello")` is easier to write and read than `Button::new("Hello")`. --- examples/pure/counter/src/main.rs | 11 ++++++----- pure/src/widget.rs | 17 +++++++++++++++++ pure/src/widget/text.rs | 4 ++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/examples/pure/counter/src/main.rs b/examples/pure/counter/src/main.rs index 00cb3fc7..4cb79a0e 100644 --- a/examples/pure/counter/src/main.rs +++ b/examples/pure/counter/src/main.rs @@ -1,4 +1,5 @@ -use iced::pure::{Button, Column, Element, Sandbox, Text}; +use iced::pure::widget::{button, column, text}; +use iced::pure::{Element, Sandbox}; use iced::{Alignment, Settings}; pub fn main() -> iced::Result { @@ -38,12 +39,12 @@ impl Sandbox for Counter { } fn view(&self) -> Element { - Column::new() + column() .padding(20) .align_items(Alignment::Center) - .push(Button::new("Increment").on_press(Message::IncrementPressed)) - .push(Text::new(self.value.to_string()).size(50)) - .push(Button::new("Decrement").on_press(Message::DecrementPressed)) + .push(button("Increment").on_press(Message::IncrementPressed)) + .push(text(self.value).size(50)) + .push(button("Decrement").on_press(Message::DecrementPressed)) .into() } } diff --git a/pure/src/widget.rs b/pure/src/widget.rs index bf63b999..7215e99e 100644 --- a/pure/src/widget.rs +++ b/pure/src/widget.rs @@ -71,3 +71,20 @@ pub trait Widget { event::Status::Ignored } } + +pub fn column() -> Column { + Column::new() +} + +pub fn button( + content: impl Into>, +) -> Button { + Button::new(content) +} + +pub fn text(text: impl ToString) -> Text +where + Renderer: iced_native::text::Renderer, +{ + Text::new(text) +} diff --git a/pure/src/widget/text.rs b/pure/src/widget/text.rs index e3a7d299..73ff71e2 100644 --- a/pure/src/widget/text.rs +++ b/pure/src/widget/text.rs @@ -24,9 +24,9 @@ where impl Text { /// Create a new fragment of [`Text`] with the given contents. - pub fn new>(label: T) -> Self { + pub fn new(label: T) -> Self { Text { - content: label.into(), + content: label.to_string(), size: None, color: None, font: Default::default(), -- cgit