Expose function helpers to build widgets in pure::widget
`button("Hello")` is easier to write and read than
`Button::new("Hello")`.
This commit is contained in:
parent
66d69b5c9a
commit
43a7ad72ef
3 changed files with 25 additions and 7 deletions
|
|
@ -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};
|
use iced::{Alignment, Settings};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
|
|
@ -38,12 +39,12 @@ impl Sandbox for Counter {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) -> Element<Message> {
|
fn view(&self) -> Element<Message> {
|
||||||
Column::new()
|
column()
|
||||||
.padding(20)
|
.padding(20)
|
||||||
.align_items(Alignment::Center)
|
.align_items(Alignment::Center)
|
||||||
.push(Button::new("Increment").on_press(Message::IncrementPressed))
|
.push(button("Increment").on_press(Message::IncrementPressed))
|
||||||
.push(Text::new(self.value.to_string()).size(50))
|
.push(text(self.value).size(50))
|
||||||
.push(Button::new("Decrement").on_press(Message::DecrementPressed))
|
.push(button("Decrement").on_press(Message::DecrementPressed))
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,3 +71,20 @@ pub trait Widget<Message, Renderer> {
|
||||||
event::Status::Ignored
|
event::Status::Ignored
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn column<Message, Renderer>() -> Column<Message, Renderer> {
|
||||||
|
Column::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn button<Message, Renderer>(
|
||||||
|
content: impl Into<Element<Message, Renderer>>,
|
||||||
|
) -> Button<Message, Renderer> {
|
||||||
|
Button::new(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn text<Renderer>(text: impl ToString) -> Text<Renderer>
|
||||||
|
where
|
||||||
|
Renderer: iced_native::text::Renderer,
|
||||||
|
{
|
||||||
|
Text::new(text)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,9 @@ where
|
||||||
|
|
||||||
impl<Renderer: text::Renderer> Text<Renderer> {
|
impl<Renderer: text::Renderer> Text<Renderer> {
|
||||||
/// Create a new fragment of [`Text`] with the given contents.
|
/// Create a new fragment of [`Text`] with the given contents.
|
||||||
pub fn new<T: Into<String>>(label: T) -> Self {
|
pub fn new<T: ToString>(label: T) -> Self {
|
||||||
Text {
|
Text {
|
||||||
content: label.into(),
|
content: label.to_string(),
|
||||||
size: None,
|
size: None,
|
||||||
color: None,
|
color: None,
|
||||||
font: Default::default(),
|
font: Default::default(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue