From 5225e0e304bf5b407977e549c48ce9dea26b8c40 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 10 Feb 2022 21:54:13 +0700 Subject: Draft virtual `Button`, `Column`, and `Text` ... as well as a very naive diffing strategy! --- examples/virtual_counter/Cargo.toml | 10 ++++++ examples/virtual_counter/README.md | 18 +++++++++++ examples/virtual_counter/index.html | 12 +++++++ examples/virtual_counter/src/main.rs | 61 ++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 examples/virtual_counter/Cargo.toml create mode 100644 examples/virtual_counter/README.md create mode 100644 examples/virtual_counter/index.html create mode 100644 examples/virtual_counter/src/main.rs (limited to 'examples/virtual_counter') diff --git a/examples/virtual_counter/Cargo.toml b/examples/virtual_counter/Cargo.toml new file mode 100644 index 00000000..537d4174 --- /dev/null +++ b/examples/virtual_counter/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "virtual_counter" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../.." } +iced_virtual = { path = "../../virtual" } diff --git a/examples/virtual_counter/README.md b/examples/virtual_counter/README.md new file mode 100644 index 00000000..4d9fc5b9 --- /dev/null +++ b/examples/virtual_counter/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. + +
+ + + +
+ +You can run it with `cargo run`: +``` +cargo run --package counter +``` + +[`main`]: src/main.rs diff --git a/examples/virtual_counter/index.html b/examples/virtual_counter/index.html new file mode 100644 index 00000000..d2e368e4 --- /dev/null +++ b/examples/virtual_counter/index.html @@ -0,0 +1,12 @@ + + + + + + Counter - Iced + + + + + + diff --git a/examples/virtual_counter/src/main.rs b/examples/virtual_counter/src/main.rs new file mode 100644 index 00000000..46e8320e --- /dev/null +++ b/examples/virtual_counter/src/main.rs @@ -0,0 +1,61 @@ +use iced::{Alignment, Element, Sandbox, Settings}; +use iced_virtual::{Button, Column, Text, Virtual}; + +pub fn main() -> iced::Result { + Counter::run(Settings::default()) +} + +struct Counter { + value: i32, + state: iced_virtual::State, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + IncrementPressed, + DecrementPressed, +} + +impl Sandbox for Counter { + type Message = Message; + + fn new() -> Self { + Self { + value: 0, + state: iced_virtual::State::new(), + } + } + + fn title(&self) -> String { + String::from("Counter - Iced") + } + + fn update(&mut self, message: Message) { + match message { + Message::IncrementPressed => { + self.value += 1; + } + Message::DecrementPressed => { + self.value -= 1; + } + } + } + + fn view(&mut self) -> Element { + let content = Column::new() + .padding(20) + .align_items(Alignment::Center) + .push( + Button::new(Text::new("Increment")) + .on_press(Message::IncrementPressed), + ) + .push(Text::new(self.value.to_string()).size(50)) + .push( + Button::new(Text::new("Decrement")) + .on_press(Message::DecrementPressed), + ); + + Virtual::new(&mut self.state, iced_virtual::Element::new(content)) + .into() + } +} -- cgit