From f436f20eb86b2324126a54d4164b4cedf2134a45 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 03:47:36 +0100 Subject: Draft `Canvas` types and `clock` example --- examples/clock/src/main.rs | 145 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 examples/clock/src/main.rs (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs new file mode 100644 index 00000000..958846f4 --- /dev/null +++ b/examples/clock/src/main.rs @@ -0,0 +1,145 @@ +use iced::{ + canvas, executor, Application, Canvas, Color, Command, Element, Length, + Point, Settings, +}; + +use std::sync::Arc; + +pub fn main() { + Clock::run(Settings::default()) +} + +struct Clock { + local_time: Arc, + clock: canvas::layer::Cached, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + Tick(chrono::DateTime), +} + +impl Application for Clock { + type Executor = executor::Default; + type Message = Message; + + fn new() -> (Self, Command) { + let now: LocalTime = chrono::Local::now().into(); + + ( + Clock { + local_time: Arc::new(now), + clock: canvas::layer::Cached::new(), + }, + Command::none(), + ) + } + + fn title(&self) -> String { + String::from("Clock - Iced") + } + + fn update(&mut self, message: Message) -> Command { + match message { + Message::Tick(local_time) => {} + } + + Command::none() + } + + fn view(&mut self) -> Element { + Canvas::new() + .width(Length::Fill) + .height(Length::Fill) + .push(self.clock.with(&self.local_time)) + .into() + } +} + +#[derive(Debug)] +struct LocalTime { + hour: u32, + minute: u32, + second: u32, +} + +impl From> for LocalTime { + fn from(date_time: chrono::DateTime) -> LocalTime { + use chrono::Timelike; + + LocalTime { + hour: date_time.hour(), + minute: date_time.minute(), + second: date_time.second(), + } + } +} + +impl canvas::layer::Drawable for LocalTime { + fn draw(&self, frame: &mut canvas::Frame) { + let center = frame.center(); + let radius = frame.width().min(frame.height()) as f32 / 2.0; + + let mut path = canvas::Path::new(); + + path.arc(canvas::path::Arc { + center, + radius, + start_angle: 0.0, + end_angle: 360.0 * 2.0 * std::f32::consts::PI, + }); + + frame.fill( + path, + canvas::Fill::Color(Color::from_rgb8(0x12, 0x93, 0xD8)), + ); + + fn draw_handle( + n: u32, + total: u32, + length: f32, + path: &mut canvas::Path, + ) { + let turns = n as f32 / total as f32; + let t = 2.0 * std::f32::consts::PI * (turns - 0.25); + + let x = length * t.cos(); + let y = length * t.sin(); + + path.line_to(Point::new(x, y)); + } + + let mut path = canvas::Path::new(); + + path.move_to(center); + draw_handle(self.hour, 12, 0.6 * radius, &mut path); + + path.move_to(center); + draw_handle(self.minute, 60, 0.9 * radius, &mut path); + + frame.stroke( + path, + canvas::Stroke { + width: 4.0, + color: Color::WHITE, + line_cap: canvas::LineCap::Round, + ..canvas::Stroke::default() + }, + ); + + let mut path = canvas::Path::new(); + + path.move_to(center); + draw_handle(self.second, 60, 0.9 * radius, &mut path); + + frame.stroke( + path, + canvas::Stroke { + width: 2.0, + color: Color::WHITE, + line_cap: canvas::LineCap::Round, + ..canvas::Stroke::default() + }, + ); + } +} -- cgit From 4777f5d787e262aa9093a3099ae62be2b8c0c82f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 04:00:13 +0100 Subject: Remove unnecessary `Arc` from `clock` example --- examples/clock/src/main.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 958846f4..dc2c06cf 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -3,14 +3,12 @@ use iced::{ Point, Settings, }; -use std::sync::Arc; - pub fn main() { Clock::run(Settings::default()) } struct Clock { - local_time: Arc, + now: LocalTime, clock: canvas::layer::Cached, } @@ -28,7 +26,7 @@ impl Application for Clock { ( Clock { - local_time: Arc::new(now), + now, clock: canvas::layer::Cached::new(), }, Command::none(), @@ -41,7 +39,15 @@ impl Application for Clock { fn update(&mut self, message: Message) -> Command { match message { - Message::Tick(local_time) => {} + Message::Tick(local_time) => { + let now = local_time.into(); + + if now != self.now { + self.now = now; + + self.clock.clear(); + } + } } Command::none() @@ -51,12 +57,12 @@ impl Application for Clock { Canvas::new() .width(Length::Fill) .height(Length::Fill) - .push(self.clock.with(&self.local_time)) + .push(self.clock.with(&self.now)) .into() } } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] struct LocalTime { hour: u32, minute: u32, -- cgit From 74dd79e97f83d3e9e13d87444740edeb353f9be8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 06:41:24 +0100 Subject: Rename current `Path` to `path::Builder` --- examples/clock/src/main.rs | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index dc2c06cf..76752d20 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -86,17 +86,17 @@ impl canvas::layer::Drawable for LocalTime { let center = frame.center(); let radius = frame.width().min(frame.height()) as f32 / 2.0; - let mut path = canvas::Path::new(); - - path.arc(canvas::path::Arc { - center, - radius, - start_angle: 0.0, - end_angle: 360.0 * 2.0 * std::f32::consts::PI, + let path = canvas::Path::new(|path| { + path.arc(canvas::path::Arc { + center, + radius, + start_angle: 0.0, + end_angle: 360.0 * 2.0 * std::f32::consts::PI, + }) }); frame.fill( - path, + &path, canvas::Fill::Color(Color::from_rgb8(0x12, 0x93, 0xD8)), ); @@ -104,7 +104,7 @@ impl canvas::layer::Drawable for LocalTime { n: u32, total: u32, length: f32, - path: &mut canvas::Path, + path: &mut canvas::path::Builder, ) { let turns = n as f32 / total as f32; let t = 2.0 * std::f32::consts::PI * (turns - 0.25); @@ -115,16 +115,16 @@ impl canvas::layer::Drawable for LocalTime { path.line_to(Point::new(x, y)); } - let mut path = canvas::Path::new(); - - path.move_to(center); - draw_handle(self.hour, 12, 0.6 * radius, &mut path); + let path = canvas::Path::new(|path| { + path.move_to(center); + draw_handle(self.hour, 12, 0.6 * radius, path); - path.move_to(center); - draw_handle(self.minute, 60, 0.9 * radius, &mut path); + path.move_to(center); + draw_handle(self.minute, 60, 0.9 * radius, path) + }); frame.stroke( - path, + &path, canvas::Stroke { width: 4.0, color: Color::WHITE, @@ -133,13 +133,13 @@ impl canvas::layer::Drawable for LocalTime { }, ); - let mut path = canvas::Path::new(); - - path.move_to(center); - draw_handle(self.second, 60, 0.9 * radius, &mut path); + let path = canvas::Path::new(|path| { + path.move_to(center); + draw_handle(self.second, 60, 0.9 * radius, path) + }); frame.stroke( - path, + &path, canvas::Stroke { width: 2.0, color: Color::WHITE, -- cgit From 578ea4abb8a2dd0d53d7087322796bf9ad541b56 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 08:49:42 +0100 Subject: Finish `clock` example --- examples/clock/src/main.rs | 65 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 8 deletions(-) (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 76752d20..1b8d1ee6 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,6 +1,6 @@ use iced::{ - canvas, executor, Application, Canvas, Color, Command, Element, Length, - Point, Settings, + canvas, executor, Application, Canvas, Color, Command, Container, Element, + Length, Point, Settings, Subscription, Vector, }; pub fn main() { @@ -53,11 +53,21 @@ impl Application for Clock { Command::none() } + fn subscription(&self) -> Subscription { + time::every(std::time::Duration::from_millis(500)).map(Message::Tick) + } + fn view(&mut self) -> Element { - Canvas::new() + let canvas = Canvas::new() + .width(Length::Units(400)) + .height(Length::Units(400)) + .push(self.clock.with(&self.now)); + + Container::new(canvas) .width(Length::Fill) .height(Length::Fill) - .push(self.clock.with(&self.now)) + .center_x() + .center_y() .into() } } @@ -85,6 +95,7 @@ impl canvas::layer::Drawable for LocalTime { fn draw(&self, frame: &mut canvas::Frame) { let center = frame.center(); let radius = frame.width().min(frame.height()) as f32 / 2.0; + let offset = Vector::new(center.x, center.y); let path = canvas::Path::new(|path| { path.arc(canvas::path::Arc { @@ -104,6 +115,7 @@ impl canvas::layer::Drawable for LocalTime { n: u32, total: u32, length: f32, + offset: Vector, path: &mut canvas::path::Builder, ) { let turns = n as f32 / total as f32; @@ -112,15 +124,15 @@ impl canvas::layer::Drawable for LocalTime { let x = length * t.cos(); let y = length * t.sin(); - path.line_to(Point::new(x, y)); + path.line_to(Point::new(x, y) + offset); } let path = canvas::Path::new(|path| { path.move_to(center); - draw_handle(self.hour, 12, 0.6 * radius, path); + draw_handle(self.hour, 12, 0.5 * radius, offset, path); path.move_to(center); - draw_handle(self.minute, 60, 0.9 * radius, path) + draw_handle(self.minute, 60, 0.8 * radius, offset, path) }); frame.stroke( @@ -135,7 +147,7 @@ impl canvas::layer::Drawable for LocalTime { let path = canvas::Path::new(|path| { path.move_to(center); - draw_handle(self.second, 60, 0.9 * radius, path) + draw_handle(self.second, 60, 0.8 * radius, offset, path) }); frame.stroke( @@ -149,3 +161,40 @@ impl canvas::layer::Drawable for LocalTime { ); } } + +mod time { + use iced::futures; + + pub fn every( + duration: std::time::Duration, + ) -> iced::Subscription> { + iced::Subscription::from_recipe(Every(duration)) + } + + struct Every(std::time::Duration); + + impl iced_native::subscription::Recipe for Every + where + H: std::hash::Hasher, + { + type Output = chrono::DateTime; + + fn hash(&self, state: &mut H) { + use std::hash::Hash; + + std::any::TypeId::of::().hash(state); + self.0.hash(state); + } + + fn stream( + self: Box, + _input: futures::stream::BoxStream<'static, I>, + ) -> futures::stream::BoxStream<'static, Self::Output> { + use futures::stream::StreamExt; + + async_std::stream::interval(self.0) + .map(|_| chrono::Local::now()) + .boxed() + } + } +} -- cgit From 96b36d0f9e4beba2ac9cbe9c873adf1b89d31f99 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 09:00:44 +0100 Subject: Increase line width in `clock` example --- examples/clock/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 1b8d1ee6..e70ce91a 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -54,7 +54,7 @@ impl Application for Clock { } fn subscription(&self) -> Subscription { - time::every(std::time::Duration::from_millis(500)).map(Message::Tick) + time::every(std::time::Duration::from_millis(1000)).map(Message::Tick) } fn view(&mut self) -> Element { @@ -138,7 +138,7 @@ impl canvas::layer::Drawable for LocalTime { frame.stroke( &path, canvas::Stroke { - width: 4.0, + width: 6.0, color: Color::WHITE, line_cap: canvas::LineCap::Round, ..canvas::Stroke::default() @@ -153,7 +153,7 @@ impl canvas::layer::Drawable for LocalTime { frame.stroke( &path, canvas::Stroke { - width: 2.0, + width: 3.0, color: Color::WHITE, line_cap: canvas::LineCap::Round, ..canvas::Stroke::default() -- cgit From 4a24392c9c078c56ea26b49c455c9ef183fa79a6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 09:21:13 +0100 Subject: Simplify `Clock::new` in `clock` example --- examples/clock/src/main.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index e70ce91a..e30158f7 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -22,11 +22,9 @@ impl Application for Clock { type Message = Message; fn new() -> (Self, Command) { - let now: LocalTime = chrono::Local::now().into(); - ( Clock { - now, + now: chrono::Local::now().into(), clock: canvas::layer::Cached::new(), }, Command::none(), -- cgit From 265c08661ca7943473c2ef305246a75bfc7847ff Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 22:08:49 +0100 Subject: Fix circle `end_angle` in `clock` example --- examples/clock/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index e30158f7..25849d1a 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -100,7 +100,7 @@ impl canvas::layer::Drawable for LocalTime { center, radius, start_angle: 0.0, - end_angle: 360.0 * 2.0 * std::f32::consts::PI, + end_angle: 2.0 * std::f32::consts::PI, }) }); -- cgit From 9dc9305d93b9c03d5ea309ecce148857f9bb0745 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 22:10:00 +0100 Subject: Remove redundant conversion in `clock` example --- examples/clock/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 25849d1a..8259e382 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -92,7 +92,7 @@ impl From> for LocalTime { impl canvas::layer::Drawable for LocalTime { fn draw(&self, frame: &mut canvas::Frame) { let center = frame.center(); - let radius = frame.width().min(frame.height()) as f32 / 2.0; + let radius = frame.width().min(frame.height()) / 2.0; let offset = Vector::new(center.x, center.y); let path = canvas::Path::new(|path| { -- cgit From e7c400a0aaa01320e80aeed726e7ba702af9380b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 22:13:59 +0100 Subject: Improve naming in `clock` example --- examples/clock/src/main.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 8259e382..160adffd 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -42,7 +42,6 @@ impl Application for Clock { if now != self.now { self.now = now; - self.clock.clear(); } } @@ -95,7 +94,7 @@ impl canvas::layer::Drawable for LocalTime { let radius = frame.width().min(frame.height()) / 2.0; let offset = Vector::new(center.x, center.y); - let path = canvas::Path::new(|path| { + let clock = canvas::Path::new(|path| { path.arc(canvas::path::Arc { center, radius, @@ -105,11 +104,11 @@ impl canvas::layer::Drawable for LocalTime { }); frame.fill( - &path, + &clock, canvas::Fill::Color(Color::from_rgb8(0x12, 0x93, 0xD8)), ); - fn draw_handle( + fn draw_hand( n: u32, total: u32, length: f32, @@ -125,16 +124,16 @@ impl canvas::layer::Drawable for LocalTime { path.line_to(Point::new(x, y) + offset); } - let path = canvas::Path::new(|path| { + let hour_and_minute_hands = canvas::Path::new(|path| { path.move_to(center); - draw_handle(self.hour, 12, 0.5 * radius, offset, path); + draw_hand(self.hour, 12, 0.5 * radius, offset, path); path.move_to(center); - draw_handle(self.minute, 60, 0.8 * radius, offset, path) + draw_hand(self.minute, 60, 0.8 * radius, offset, path) }); frame.stroke( - &path, + &hour_and_minute_hands, canvas::Stroke { width: 6.0, color: Color::WHITE, @@ -143,13 +142,13 @@ impl canvas::layer::Drawable for LocalTime { }, ); - let path = canvas::Path::new(|path| { + let second_hand = canvas::Path::new(|path| { path.move_to(center); - draw_handle(self.second, 60, 0.8 * radius, offset, path) + draw_hand(self.second, 60, 0.8 * radius, offset, path) }); frame.stroke( - &path, + &second_hand, canvas::Stroke { width: 3.0, color: Color::WHITE, -- cgit From 979edeb213c2ca0dd394a9a6c68a30dfab371263 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 22:38:36 +0100 Subject: Fix `clock` example eventually skipping a second --- examples/clock/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 160adffd..25a213cd 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -51,7 +51,7 @@ impl Application for Clock { } fn subscription(&self) -> Subscription { - time::every(std::time::Duration::from_millis(1000)).map(Message::Tick) + time::every(std::time::Duration::from_millis(500)).map(Message::Tick) } fn view(&mut self) -> Element { -- cgit From 76df374624e5d82dcb2670789a6c4ff228dda9e9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 14 Feb 2020 02:23:41 +0100 Subject: Implement additional methods in `path::Builder` --- examples/clock/src/main.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 25a213cd..0a70709f 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -94,14 +94,7 @@ impl canvas::layer::Drawable for LocalTime { let radius = frame.width().min(frame.height()) / 2.0; let offset = Vector::new(center.x, center.y); - let clock = canvas::Path::new(|path| { - path.arc(canvas::path::Arc { - center, - radius, - start_angle: 0.0, - end_angle: 2.0 * std::f32::consts::PI, - }) - }); + let clock = canvas::Path::new(|path| path.circle(center, radius)); frame.fill( &clock, -- cgit From dadae122533ae0916bebd04d6efab3de145263d4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 15 Feb 2020 10:08:27 +0100 Subject: Implement MSAA for `triangle` pipeline in `iced_wgpu` --- examples/clock/src/main.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 0a70709f..f7fb6f2d 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -4,7 +4,10 @@ use iced::{ }; pub fn main() { - Clock::run(Settings::default()) + Clock::run(Settings { + antialiasing: true, + ..Settings::default() + }) } struct Clock { -- cgit From 570f769744aabce2d9d9618feadb47e4b92f50ca Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 15 Feb 2020 10:50:07 +0100 Subject: Rename `Settings::antialiasing` to `use_antialiasing` --- examples/clock/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index f7fb6f2d..d0995e0c 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -5,7 +5,7 @@ use iced::{ pub fn main() { Clock::run(Settings { - antialiasing: true, + use_antialiasing: true, ..Settings::default() }) } -- cgit From 9c067562fa765cfc49d09cd9b12fbba96d5619fa Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 18 Feb 2020 08:48:54 +0100 Subject: Write documentation for new `canvas` module --- examples/clock/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index d0995e0c..559f0192 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -12,7 +12,7 @@ pub fn main() { struct Clock { now: LocalTime, - clock: canvas::layer::Cached, + clock: canvas::layer::Cache, } #[derive(Debug, Clone, Copy)] @@ -28,7 +28,7 @@ impl Application for Clock { ( Clock { now: chrono::Local::now().into(), - clock: canvas::layer::Cached::new(), + clock: canvas::layer::Cache::new(), }, Command::none(), ) @@ -91,7 +91,7 @@ impl From> for LocalTime { } } -impl canvas::layer::Drawable for LocalTime { +impl canvas::Drawable for LocalTime { fn draw(&self, frame: &mut canvas::Frame) { let center = frame.center(); let radius = frame.width().min(frame.height()) / 2.0; -- cgit From 6f7247ca13181bcdfe1a3065215c1b3204723b84 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 18 Feb 2020 09:54:24 +0100 Subject: Rename `Settings::use_antialiasing` to `antialiasing` --- examples/clock/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/clock/src/main.rs') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 559f0192..d8266f06 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -5,7 +5,7 @@ use iced::{ pub fn main() { Clock::run(Settings { - use_antialiasing: true, + antialiasing: true, ..Settings::default() }) } -- cgit