Add simple counter example

This commit is contained in:
Héctor Ramón Jiménez 2019-11-26 22:31:46 +01:00
parent 84874ac5dc
commit 811d8b90d7

56
examples/counter.rs Normal file
View File

@ -0,0 +1,56 @@
use iced::{button, Button, Column, Element, Sandbox, Settings, Text};
pub fn main() {
Counter::run(Settings::default())
}
#[derive(Default)]
struct Counter {
value: i32,
increment_button: button::State,
decrement_button: button::State,
}
#[derive(Debug, Clone, Copy)]
enum Message {
IncrementPressed,
DecrementPressed,
}
impl Sandbox for Counter {
type Message = Message;
fn new() -> Self {
Self::default()
}
fn title(&self) -> String {
String::from("A simple counter")
}
fn update(&mut self, message: Message) {
match message {
Message::IncrementPressed => {
self.value += 1;
}
Message::DecrementPressed => {
self.value -= 1;
}
}
}
fn view(&mut self) -> Element<Message> {
Column::new()
.padding(20)
.push(
Button::new(&mut self.increment_button, Text::new("Increment"))
.on_press(Message::IncrementPressed),
)
.push(Text::new(self.value.to_string()).size(50))
.push(
Button::new(&mut self.decrement_button, Text::new("Decrement"))
.on_press(Message::DecrementPressed),
)
.into()
}
}