Skip to content

Commit

Permalink
Add simple counter example
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Nov 26, 2019
1 parent 84874ac commit 811d8b9
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions examples/counter.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 811d8b9

Please sign in to comment.