Skip to content

Commit

Permalink
Add and purify arc example by @ThatsNoMoon
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jul 10, 2022
1 parent 53d93a3 commit 4b8ae71
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ members = [
"examples/tour",
"examples/url_handler",
"examples/websocket",
"examples/pure/arc",
"examples/pure/component",
"examples/pure/counter",
"examples/pure/game_of_life",
Expand Down
9 changes: 9 additions & 0 deletions examples/pure/arc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "arc"
version = "0.1.0"
authors = ["ThatsNoMoon <git@thatsnomoon.dev>"]
edition = "2021"
publish = false

[dependencies]
iced = { path = "../../..", features = ["pure", "canvas", "tokio", "debug"] }
14 changes: 14 additions & 0 deletions examples/pure/arc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## arc_to

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_to
```

[`main`]: src/main.rs
126 changes: 126 additions & 0 deletions examples/pure/arc/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
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::{
Color, 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<Message>) {
(
Arc {
start: Instant::now(),
cache: Default::default(),
},
Command::none(),
)
}

fn title(&self) -> String {
String::from("Arc - Iced")
}

fn update(&mut self, _: Message) -> Command<Message> {
self.cache.clear();

Command::none()
}

fn subscription(&self) -> Subscription<Message> {
iced::time::every(std::time::Duration::from_millis(10))
.map(|_| Message::Tick)
}

fn view(&self) -> Element<Message> {
Canvas::new(self)
.width(Length::Fill)
.height(Length::Fill)
.into()
}

fn theme(&self) -> Theme {
Theme::Dark
}
}

impl<Message> canvas::Program<Message> for Arc {
type State = ();

fn draw(
&self,
_state: &Self::State,
theme: &Theme,
bounds: Rectangle,
_cursor: Cursor,
) -> Vec<Geometry> {
let geometry = self.cache.draw(bounds.size(), |frame| {
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, Color::WHITE);

let path = Path::new(|b| {
b.move_to(start);
b.arc_to(center, end, 50.0);
b.line_to(end);
});

let palette = theme.palette();

frame.stroke(
&path,
Stroke {
color: palette.text,
width: 10.0,
..Stroke::default()
},
);
});

vec![geometry]
}
}
10 changes: 5 additions & 5 deletions style/src/theme/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use palette::{FromColor, Hsl, Mix, RelativeContrast, Srgb};

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Palette {
background: Color,
text: Color,
primary: Color,
success: Color,
danger: Color,
pub background: Color,
pub text: Color,
pub primary: Color,
pub success: Color,
pub danger: Color,
}

impl Palette {
Expand Down

0 comments on commit 4b8ae71

Please sign in to comment.