summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorLibravatar TimUntersberger <timuntersberger2@gmail.com>2021-06-25 15:38:30 +0200
committerLibravatar TimUntersberger <timuntersberger2@gmail.com>2021-06-25 17:33:40 +0200
commit52d44769a332d1d96a5e9292805a5884073b9185 (patch)
tree86c607e307a120c1f9aa76d1addc5cdd92bb8a34 /examples
parent06d0158efbaadc5ae0a6dea22e7a761a3e1c2a8f (diff)
downloadiced-52d44769a332d1d96a5e9292805a5884073b9185.tar.gz
iced-52d44769a332d1d96a5e9292805a5884073b9185.tar.bz2
iced-52d44769a332d1d96a5e9292805a5884073b9185.zip
add initial attempt at adding winit example
Diffstat (limited to 'examples')
-rw-r--r--examples/winit/Cargo.toml9
-rw-r--r--examples/winit/README.md18
-rw-r--r--examples/winit/src/main.rs62
3 files changed, 89 insertions, 0 deletions
diff --git a/examples/winit/Cargo.toml b/examples/winit/Cargo.toml
new file mode 100644
index 00000000..6d16a7a9
--- /dev/null
+++ b/examples/winit/Cargo.toml
@@ -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" }
diff --git a/examples/winit/README.md b/examples/winit/README.md
new file mode 100644
index 00000000..4d9fc5b9
--- /dev/null
+++ b/examples/winit/README.md
@@ -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
diff --git a/examples/winit/src/main.rs b/examples/winit/src/main.rs
new file mode 100644
index 00000000..890ded79
--- /dev/null
+++ b/examples/winit/src/main.rs
@@ -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()
+ }
+}