From ff2519b1d43d481987351a83b6dd7237524c21f0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 27 Jul 2022 06:49:20 +0200 Subject: Replace stateful widgets with new `iced_pure` API --- examples/arc/Cargo.toml | 9 ++++ examples/arc/README.md | 14 ++++++ examples/arc/src/main.rs | 124 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 examples/arc/Cargo.toml create mode 100644 examples/arc/README.md create mode 100644 examples/arc/src/main.rs (limited to 'examples/arc') diff --git a/examples/arc/Cargo.toml b/examples/arc/Cargo.toml new file mode 100644 index 00000000..299f5a3e --- /dev/null +++ b/examples/arc/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "arc" +version = "0.1.0" +authors = ["ThatsNoMoon "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../../..", features = ["canvas", "tokio", "debug"] } diff --git a/examples/arc/README.md b/examples/arc/README.md new file mode 100644 index 00000000..303253da --- /dev/null +++ b/examples/arc/README.md @@ -0,0 +1,14 @@ +## Arc + +An application that uses the `Canvas` widget to draw a rotating arc. + +This is a simple demo for https://github.com/iced-rs/iced/pull/1358. + +The __[`main`]__ file contains all the code of the example. + +You can run it with `cargo run`: +``` +cargo run --package arc +``` + +[`main`]: src/main.rs diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs new file mode 100644 index 00000000..df0e1e8a --- /dev/null +++ b/examples/arc/src/main.rs @@ -0,0 +1,124 @@ +use std::{f32::consts::PI, time::Instant}; + +use iced::executor; +use iced::pure::widget::canvas::{ + self, Cache, Canvas, Cursor, Geometry, Path, Stroke, +}; +use iced::pure::{Application, Element}; +use iced::{Command, Length, Point, Rectangle, Settings, Subscription, Theme}; + +pub fn main() -> iced::Result { + Arc::run(Settings { + antialiasing: true, + ..Settings::default() + }) +} + +struct Arc { + start: Instant, + cache: Cache, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + Tick, +} + +impl Application for Arc { + type Executor = executor::Default; + type Message = Message; + type Theme = Theme; + type Flags = (); + + fn new(_flags: ()) -> (Self, Command) { + ( + Arc { + start: Instant::now(), + cache: Default::default(), + }, + Command::none(), + ) + } + + fn title(&self) -> String { + String::from("Arc - Iced") + } + + fn update(&mut self, _: Message) -> Command { + self.cache.clear(); + + Command::none() + } + + fn subscription(&self) -> Subscription { + iced::time::every(std::time::Duration::from_millis(10)) + .map(|_| Message::Tick) + } + + fn view(&self) -> Element { + Canvas::new(self) + .width(Length::Fill) + .height(Length::Fill) + .into() + } + + fn theme(&self) -> Theme { + Theme::Dark + } +} + +impl canvas::Program for Arc { + type State = (); + + fn draw( + &self, + _state: &Self::State, + theme: &Theme, + bounds: Rectangle, + _cursor: Cursor, + ) -> Vec { + let geometry = self.cache.draw(bounds.size(), |frame| { + let palette = theme.palette(); + + let center = frame.center(); + let radius = frame.width().min(frame.height()) / 5.0; + + let start = Point::new(center.x, center.y - radius); + + let angle = (self.start.elapsed().as_millis() % 10_000) as f32 + / 10_000.0 + * 2.0 + * PI; + + let end = Point::new( + center.x + radius * angle.cos(), + center.y + radius * angle.sin(), + ); + + let circles = Path::new(|b| { + b.circle(start, 10.0); + b.move_to(end); + b.circle(end, 10.0); + }); + + frame.fill(&circles, palette.text); + + let path = Path::new(|b| { + b.move_to(start); + b.arc_to(center, end, 50.0); + b.line_to(end); + }); + + frame.stroke( + &path, + Stroke { + color: palette.text, + width: 10.0, + ..Stroke::default() + }, + ); + }); + + vec![geometry] + } +} -- cgit From effa6881f725fdfd2087205737484726f3580a43 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 27 Jul 2022 06:57:49 +0200 Subject: Enable `arc` example --- examples/arc/Cargo.toml | 2 +- examples/arc/src/main.rs | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'examples/arc') diff --git a/examples/arc/Cargo.toml b/examples/arc/Cargo.toml index 299f5a3e..e6e74363 100644 --- a/examples/arc/Cargo.toml +++ b/examples/arc/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../../..", features = ["canvas", "tokio", "debug"] } +iced = { path = "../..", features = ["canvas", "tokio", "debug"] } diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index df0e1e8a..0c619dc9 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -1,11 +1,13 @@ use std::{f32::consts::PI, time::Instant}; use iced::executor; -use iced::pure::widget::canvas::{ +use iced::widget::canvas::{ self, Cache, Canvas, Cursor, Geometry, Path, Stroke, }; -use iced::pure::{Application, Element}; -use iced::{Command, Length, Point, Rectangle, Settings, Subscription, Theme}; +use iced::{ + Application, Command, Element, Length, Point, Rectangle, Settings, + Subscription, Theme, +}; pub fn main() -> iced::Result { Arc::run(Settings { -- cgit