add initial attempt at adding winit example
This commit is contained in:
parent
06d0158efb
commit
52d44769a3
4 changed files with 90 additions and 0 deletions
|
|
@ -67,6 +67,7 @@ members = [
|
|||
"examples/counter",
|
||||
"examples/custom_widget",
|
||||
"examples/download_progress",
|
||||
"examples/winit",
|
||||
"examples/events",
|
||||
"examples/game_of_life",
|
||||
"examples/geometry",
|
||||
|
|
|
|||
9
examples/winit/Cargo.toml
Normal file
9
examples/winit/Cargo.toml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "counter_winit"
|
||||
version = "0.1.0"
|
||||
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
iced_winit = { path = "../../winit" }
|
||||
18
examples/winit/README.md
Normal file
18
examples/winit/README.md
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
## Counter
|
||||
|
||||
The classic counter example explained in the [`README`](../../README.md).
|
||||
|
||||
The __[`main`]__ file contains all the code of the example.
|
||||
|
||||
<div align="center">
|
||||
<a href="https://gfycat.com/fairdeadcatbird">
|
||||
<img src="https://thumbs.gfycat.com/FairDeadCatbird-small.gif">
|
||||
</a>
|
||||
</div>
|
||||
|
||||
You can run it with `cargo run`:
|
||||
```
|
||||
cargo run --package counter
|
||||
```
|
||||
|
||||
[`main`]: src/main.rs
|
||||
62
examples/winit/src/main.rs
Normal file
62
examples/winit/src/main.rs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
use iced_winit::{button, Align, Button, Column, Element, Application, Settings, Text, Renderer, Program, Command};
|
||||
|
||||
pub fn main() {
|
||||
Counter::run(Settings::default()).unwrap()
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Counter {
|
||||
value: i32,
|
||||
increment_button: button::State,
|
||||
decrement_button: button::State,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum Message {
|
||||
IncrementPressed,
|
||||
DecrementPressed,
|
||||
}
|
||||
|
||||
impl Application for Counter {
|
||||
type Flags = ();
|
||||
|
||||
fn new(flags: Self::Flags) -> (Self, Command<Message>) {
|
||||
(Self::default(), Command::none())
|
||||
}
|
||||
|
||||
fn title(&self) -> String {
|
||||
String::from("Counter with winit - Iced")
|
||||
}
|
||||
}
|
||||
|
||||
impl Program for Counter {
|
||||
type Renderer = Renderer;
|
||||
type Message = Message;
|
||||
|
||||
fn update(&mut self, message: Message) {
|
||||
match message {
|
||||
Message::IncrementPressed => {
|
||||
self.value += 1;
|
||||
}
|
||||
Message::DecrementPressed => {
|
||||
self.value -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn view(&mut self) -> Element<Message, Self::Renderer> {
|
||||
Column::new()
|
||||
.padding(20)
|
||||
.align_items(Align::Center)
|
||||
.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()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue