From 2ebc92319711e6fa0dda310939257334625b59c9 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Wed, 7 Jun 2023 15:19:11 -0700 Subject: feat: use lyon for easing --- examples/loading_spinners/src/circular.rs | 444 ++++++++++++++++++++++++++++++ 1 file changed, 444 insertions(+) create mode 100644 examples/loading_spinners/src/circular.rs (limited to 'examples/loading_spinners/src/circular.rs') diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs new file mode 100644 index 00000000..0d01e7a1 --- /dev/null +++ b/examples/loading_spinners/src/circular.rs @@ -0,0 +1,444 @@ +//! Show a circular progress indicator. +use iced::widget::canvas::{self, Cursor, Program, Renderer as CanvasRenderer}; +use iced::window; +use iced_core::event::{self, Event}; +use iced_core::time::Instant; +use iced_core::widget::tree::{self, Tree}; +use iced_core::window::RedrawRequest; +use iced_core::{layout, Size}; +use iced_core::{renderer, Vector}; +use iced_core::{ + Background, Clipboard, Color, Element, Layout, Length, Point, Rectangle, + Renderer, Shell, Widget, +}; + +use super::easing::{self, Easing}; + +use std::f32::consts::PI; +use std::time::Duration; + +type R = iced_widget::renderer::Renderer; + +const MIN_RADIANS: f32 = PI / 8.0; +const WRAP_RADIANS: f32 = 2.0 * PI - PI / 4.0; +const BASE_ROTATION_SPEED: u32 = u32::MAX / 80; + +#[allow(missing_debug_implementations)] +pub struct Circular<'a, Theme> +where + Theme: StyleSheet, +{ + size: f32, + bar_height: f32, + style: ::Style, + easing: &'a Easing, + cycle_duration: Duration, + rotation_speed: u32, +} + +impl<'a, Theme> Circular<'a, Theme> +where + Theme: StyleSheet, +{ + /// Creates a new [`Circular`] with the given content. + pub fn new() -> Self { + Circular { + size: 40.0, + bar_height: 4.0, + style: ::Style::default(), + easing: &easing::STANDARD, + cycle_duration: Duration::from_millis(600), + rotation_speed: BASE_ROTATION_SPEED, + } + } + + /// Sets the size of the [`Circular`]. + pub fn size(mut self, size: f32) -> Self { + self.size = size; + self + } + + /// Sets the bar height of the [`Circular`]. + pub fn bar_height(mut self, bar_height: f32) -> Self { + self.bar_height = bar_height; + self + } + + /// Sets the style variant of this [`Circular`]. + pub fn style(mut self, style: ::Style) -> Self { + self.style = style; + self + } + + /// Sets the easing of this [`Circular`]. + pub fn easing(mut self, easing: &'a Easing) -> Self { + self.easing = easing; + self + } + + /// Sets the cycle duration of this [`Circular`]. + pub fn cycle_duration(mut self, duration: Duration) -> Self { + self.cycle_duration = duration / 2; + self + } + + /// Sets the rotation speed of this [`Circular`]. Must be set to between 0.0 and 10.0. + /// Defaults to 1.0. + pub fn rotation_speed(mut self, speed: f32) -> Self { + let multiplier = speed.min(10.0).max(0.0); + self.rotation_speed = (BASE_ROTATION_SPEED as f32 * multiplier) as u32; + self + } +} + +impl<'a, Theme> Default for Circular<'a, Theme> +where + Theme: StyleSheet, +{ + fn default() -> Self { + Self::new() + } +} + +#[derive(Clone, Copy)] +enum State { + Expanding { + start: Instant, + progress: f32, + procession: u32, + }, + Contracting { + start: Instant, + progress: f32, + procession: u32, + }, +} + +impl Default for State { + fn default() -> Self { + Self::Expanding { + start: Instant::now(), + progress: 0.0, + procession: 0, + } + } +} + +impl State { + fn next(&self, now: Instant) -> Self { + match self { + Self::Expanding { procession, .. } => Self::Contracting { + start: now, + progress: 0.0, + procession: procession.wrapping_add(BASE_ROTATION_SPEED), + }, + Self::Contracting { procession, .. } => Self::Expanding { + start: now, + progress: 0.0, + procession: procession.wrapping_add( + BASE_ROTATION_SPEED.wrapping_add( + ((WRAP_RADIANS / (2.0 * PI)) * u32::MAX as f32) as u32, + ), + ), + }, + } + } + + fn start(&self) -> Instant { + match self { + Self::Expanding { start, .. } | Self::Contracting { start, .. } => { + *start + } + } + } + + fn timed_transition( + &self, + cycle_duration: Duration, + rotation_speed: u32, + now: Instant, + ) -> Self { + let elapsed = now.duration_since(self.start()); + + match elapsed { + elapsed if elapsed > cycle_duration => self.next(now), + _ => self.with_elapsed(cycle_duration, rotation_speed, elapsed), + } + } + + fn with_elapsed( + &self, + cycle_duration: Duration, + rotation_speed: u32, + elapsed: Duration, + ) -> Self { + let progress = elapsed.as_secs_f32() / cycle_duration.as_secs_f32(); + match self { + Self::Expanding { + start, procession, .. + } => Self::Expanding { + start: *start, + progress, + procession: procession.wrapping_add(rotation_speed), + }, + Self::Contracting { + start, procession, .. + } => Self::Contracting { + start: *start, + progress, + procession: procession.wrapping_add(rotation_speed), + }, + } + } + + fn procession(&self) -> f32 { + match self { + Self::Expanding { procession, .. } + | Self::Contracting { procession, .. } => { + *procession as f32 / u32::MAX as f32 + } + } + } +} + +impl<'a, Message, Theme> Widget> for Circular<'a, Theme> +where + Message: 'a + Clone, + Theme: StyleSheet, +{ + fn tag(&self) -> tree::Tag { + tree::Tag::of::() + } + + fn state(&self) -> tree::State { + tree::State::new(State::default()) + } + + fn width(&self) -> Length { + Length::Fixed(self.size) + } + + fn height(&self) -> Length { + Length::Fixed(self.size) + } + + fn layout( + &self, + _renderer: &iced_widget::renderer::Renderer, + limits: &layout::Limits, + ) -> layout::Node { + let limits = limits.width(self.size).height(self.size); + let size = limits.resolve(Size::ZERO); + + layout::Node::new(size) + } + + fn on_event( + &mut self, + tree: &mut Tree, + event: Event, + _layout: Layout<'_>, + _cursor_position: Point, + _renderer: &iced_widget::renderer::Renderer, + _clipboard: &mut dyn Clipboard, + shell: &mut Shell<'_, Message>, + ) -> event::Status { + let state = tree.state.downcast_mut::(); + + if let Event::Window(window::Event::RedrawRequested(now)) = event { + *state = state.timed_transition( + self.cycle_duration, + self.rotation_speed, + now, + ); + + shell.request_redraw(RedrawRequest::NextFrame); + } + + event::Status::Ignored + } + + fn draw( + &self, + tree: &Tree, + renderer: &mut R, + theme: &Theme, + _style: &renderer::Style, + layout: Layout<'_>, + _cursor_position: Point, + _viewport: &Rectangle, + ) { + let bounds = layout.bounds(); + let state = tree.state.downcast_ref::(); + + renderer.with_translation( + Vector::new(bounds.x, bounds.y), + |renderer| { + renderer.draw( as Program< + Message, + R, + >>::draw( + &StateWithStyle { + state, + style: &self.style, + bar_height: self.bar_height, + easing: self.easing, + }, + &(), + renderer, + theme, + bounds, + Cursor::Unavailable, + )); + }, + ); + } +} + +impl<'a, Message, Theme> From> + for Element<'a, Message, R> +where + Message: Clone + 'a, + Theme: StyleSheet + 'a, +{ + fn from(circular: Circular<'a, Theme>) -> Self { + Self::new(circular) + } +} + +#[derive(Debug, Clone, Copy)] +pub struct Appearance { + /// The [`Background`] of the progress indicator. + pub background: Option, + /// The track [`Color`] of the progress indicator. + pub track_color: Color, + /// The bar [`Color`] of the progress indicator. + pub bar_color: Color, +} + +impl std::default::Default for Appearance { + fn default() -> Self { + Self { + background: None, + track_color: Color::TRANSPARENT, + bar_color: Color::BLACK, + } + } +} + +/// A set of rules that dictate the style of an indicator. +pub trait StyleSheet { + /// The supported style of the [`StyleSheet`]. + type Style: Default; + + /// Produces the active [`Appearance`] of a indicator. + fn appearance(&self, style: &Self::Style) -> Appearance; +} + +impl StyleSheet for iced::Theme { + type Style = (); + + fn appearance(&self, _style: &Self::Style) -> Appearance { + let palette = self.extended_palette(); + + Appearance { + background: None, + track_color: palette.background.weak.color, + bar_color: palette.primary.base.color, + } + } +} + +struct StateWithStyle<'a, Theme> +where + Theme: StyleSheet, +{ + state: &'a State, + style: &'a ::Style, + easing: &'a Easing, + bar_height: f32, +} + +impl<'a, Message, Theme> + canvas::Program> + for StateWithStyle<'a, Theme> +where + Theme: StyleSheet, +{ + type State = (); + + fn update( + &self, + _state: &mut Self::State, + _event: canvas::Event, + _bounds: Rectangle, + _cursor: canvas::Cursor, + ) -> (canvas::event::Status, Option) { + (canvas::event::Status::Ignored, None) + } + + fn draw( + &self, + _state: &Self::State, + renderer: &iced_widget::renderer::Renderer, + theme: &Theme, + bounds: Rectangle, + _cursor: canvas::Cursor, + ) -> Vec { + let mut frame = canvas::Frame::new(renderer, bounds.size()); + let custom_style = ::appearance(theme, self.style); + let track_radius = frame.width() / 2.0 - self.bar_height; + + if let Some(Background::Color(color)) = custom_style.background { + let background_path = + canvas::Path::circle(frame.center(), track_radius); + frame.fill(&background_path, color); + } + + let track_path = canvas::Path::circle(frame.center(), track_radius); + + frame.stroke( + &track_path, + canvas::Stroke::default() + .with_color(custom_style.track_color) + .with_width(self.bar_height), + ); + + let mut builder = canvas::path::Builder::new(); + + let start = self.state.procession() * 2.0 * PI; + + match self.state { + State::Expanding { progress, .. } => { + builder.arc(canvas::path::Arc { + center: frame.center(), + radius: track_radius, + start_angle: start, + end_angle: start + + MIN_RADIANS + + WRAP_RADIANS * (self.easing.y_at_x(*progress)), + }); + } + State::Contracting { progress, .. } => { + builder.arc(canvas::path::Arc { + center: frame.center(), + radius: track_radius, + start_angle: start + + WRAP_RADIANS * (self.easing.y_at_x(*progress)), + end_angle: start + MIN_RADIANS + WRAP_RADIANS, + }); + } + } + + let bar_path = builder.build(); + + frame.stroke( + &bar_path, + canvas::Stroke::default() + .with_color(custom_style.bar_color) + .with_width(self.bar_height), + ); + + vec![frame.into_geometry()] + } +} -- cgit From 204c9d6f52dab7b04ecec35220b15768296dfe1e Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Wed, 7 Jun 2023 16:48:11 -0700 Subject: fix: rotation in terms of duration --- examples/loading_spinners/src/circular.rs | 83 ++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 29 deletions(-) (limited to 'examples/loading_spinners/src/circular.rs') diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index 0d01e7a1..4f5b6fe4 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -33,7 +33,7 @@ where style: ::Style, easing: &'a Easing, cycle_duration: Duration, - rotation_speed: u32, + rotaion_duration: Duration, } impl<'a, Theme> Circular<'a, Theme> @@ -48,7 +48,7 @@ where style: ::Style::default(), easing: &easing::STANDARD, cycle_duration: Duration::from_millis(600), - rotation_speed: BASE_ROTATION_SPEED, + rotaion_duration: Duration::from_secs(2), } } @@ -82,11 +82,10 @@ where self } - /// Sets the rotation speed of this [`Circular`]. Must be set to between 0.0 and 10.0. - /// Defaults to 1.0. - pub fn rotation_speed(mut self, speed: f32) -> Self { - let multiplier = speed.min(10.0).max(0.0); - self.rotation_speed = (BASE_ROTATION_SPEED as f32 * multiplier) as u32; + /// Sets the base rotation duration of this [`Circular`]. This is the duration that a full + /// rotation would take if the cycle rotation were set to 0.0 (no expanding or contracting) + pub fn rotation_duration(mut self, duration: Duration) -> Self { + self.rotaion_duration = duration; self } } @@ -105,12 +104,14 @@ enum State { Expanding { start: Instant, progress: f32, - procession: u32, + rotation: u32, + last: Instant, }, Contracting { start: Instant, progress: f32, - procession: u32, + rotation: u32, + last: Instant, }, } @@ -119,27 +120,30 @@ impl Default for State { Self::Expanding { start: Instant::now(), progress: 0.0, - procession: 0, + rotation: 0, + last: Instant::now(), } } } impl State { - fn next(&self, now: Instant) -> Self { + fn next(&self, additional_rotation: u32, now: Instant) -> Self { match self { - Self::Expanding { procession, .. } => Self::Contracting { + Self::Expanding { rotation, .. } => Self::Contracting { start: now, progress: 0.0, - procession: procession.wrapping_add(BASE_ROTATION_SPEED), + rotation: rotation.wrapping_add(additional_rotation), + last: now, }, - Self::Contracting { procession, .. } => Self::Expanding { + Self::Contracting { rotation, .. } => Self::Expanding { start: now, progress: 0.0, - procession: procession.wrapping_add( + rotation: rotation.wrapping_add( BASE_ROTATION_SPEED.wrapping_add( ((WRAP_RADIANS / (2.0 * PI)) * u32::MAX as f32) as u32, ), ), + last: now, }, } } @@ -152,50 +156,71 @@ impl State { } } + fn last(&self) -> Instant { + match self { + Self::Expanding { last, .. } | Self::Contracting { last, .. } => { + *last + } + } + } + fn timed_transition( &self, cycle_duration: Duration, - rotation_speed: u32, + rotation_duration: Duration, now: Instant, ) -> Self { let elapsed = now.duration_since(self.start()); + let additional_rotation = ((now - self.last()).as_secs_f32() + / rotation_duration.as_secs_f32() + * (u32::MAX) as f32) as u32; match elapsed { - elapsed if elapsed > cycle_duration => self.next(now), - _ => self.with_elapsed(cycle_duration, rotation_speed, elapsed), + elapsed if elapsed > cycle_duration => { + self.next(additional_rotation, now) + } + _ => self.with_elapsed( + cycle_duration, + additional_rotation, + elapsed, + now, + ), } } fn with_elapsed( &self, cycle_duration: Duration, - rotation_speed: u32, + additional_rotation: u32, elapsed: Duration, + now: Instant, ) -> Self { let progress = elapsed.as_secs_f32() / cycle_duration.as_secs_f32(); match self { Self::Expanding { - start, procession, .. + start, rotation, .. } => Self::Expanding { start: *start, progress, - procession: procession.wrapping_add(rotation_speed), + rotation: rotation.wrapping_add(additional_rotation), + last: now, }, Self::Contracting { - start, procession, .. + start, rotation, .. } => Self::Contracting { start: *start, progress, - procession: procession.wrapping_add(rotation_speed), + rotation: rotation.wrapping_add(additional_rotation), + last: now, }, } } - fn procession(&self) -> f32 { + fn rotation(&self) -> f32 { match self { - Self::Expanding { procession, .. } - | Self::Contracting { procession, .. } => { - *procession as f32 / u32::MAX as f32 + Self::Expanding { rotation, .. } + | Self::Contracting { rotation, .. } => { + *rotation as f32 / u32::MAX as f32 } } } @@ -248,7 +273,7 @@ where if let Event::Window(window::Event::RedrawRequested(now)) = event { *state = state.timed_transition( self.cycle_duration, - self.rotation_speed, + self.rotaion_duration, now, ); @@ -406,7 +431,7 @@ where let mut builder = canvas::path::Builder::new(); - let start = self.state.procession() * 2.0 * PI; + let start = self.state.rotation() * 2.0 * PI; match self.state { State::Expanding { progress, .. } => { -- cgit From 56eacdb3583e701cde8cca074e151b24c3fd8df3 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Thu, 8 Jun 2023 07:47:57 -0700 Subject: comment: cleanup imports --- examples/loading_spinners/src/circular.rs | 63 +++++++++++++++---------------- 1 file changed, 30 insertions(+), 33 deletions(-) (limited to 'examples/loading_spinners/src/circular.rs') diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index 4f5b6fe4..8639780a 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -1,23 +1,21 @@ //! Show a circular progress indicator. -use iced::widget::canvas::{self, Cursor, Program, Renderer as CanvasRenderer}; -use iced::window; -use iced_core::event::{self, Event}; -use iced_core::time::Instant; -use iced_core::widget::tree::{self, Tree}; -use iced_core::window::RedrawRequest; -use iced_core::{layout, Size}; -use iced_core::{renderer, Vector}; -use iced_core::{ - Background, Clipboard, Color, Element, Layout, Length, Point, Rectangle, - Renderer, Shell, Widget, -}; +use iced::advanced::layout; +use iced::advanced::renderer; +use iced::advanced::widget::tree::{self, Tree}; +use iced::advanced::{Clipboard, Layout, Renderer, Shell, Widget}; +use iced::event; +use iced::time::Instant; +use iced::widget::canvas::{self, Cursor, Program}; +use iced::window::{self, RedrawRequest}; +use iced::{Background, Color, Element, Rectangle}; +use iced::{Event, Length, Point, Size, Vector}; use super::easing::{self, Easing}; use std::f32::consts::PI; use std::time::Duration; -type R = iced_widget::renderer::Renderer; +type R = iced::Renderer; const MIN_RADIANS: f32 = PI / 8.0; const WRAP_RADIANS: f32 = 2.0 * PI - PI / 4.0; @@ -249,7 +247,7 @@ where fn layout( &self, - _renderer: &iced_widget::renderer::Renderer, + _renderer: &iced::Renderer, limits: &layout::Limits, ) -> layout::Node { let limits = limits.width(self.size).height(self.size); @@ -264,7 +262,7 @@ where event: Event, _layout: Layout<'_>, _cursor_position: Point, - _renderer: &iced_widget::renderer::Renderer, + _renderer: &iced::Renderer, _clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, ) -> event::Status { @@ -299,22 +297,22 @@ where renderer.with_translation( Vector::new(bounds.x, bounds.y), |renderer| { - renderer.draw( as Program< - Message, - R, - >>::draw( - &StateWithStyle { - state, - style: &self.style, - bar_height: self.bar_height, - easing: self.easing, - }, - &(), + canvas::Renderer::draw( renderer, - theme, - bounds, - Cursor::Unavailable, - )); + as Program>>::draw( + &StateWithStyle { + state, + style: &self.style, + bar_height: self.bar_height, + easing: self.easing, + }, + &(), + renderer, + theme, + bounds, + Cursor::Unavailable, + ), + ); }, ); } @@ -384,8 +382,7 @@ where bar_height: f32, } -impl<'a, Message, Theme> - canvas::Program> +impl<'a, Message, Theme> Program> for StateWithStyle<'a, Theme> where Theme: StyleSheet, @@ -405,7 +402,7 @@ where fn draw( &self, _state: &Self::State, - renderer: &iced_widget::renderer::Renderer, + renderer: &iced::Renderer, theme: &Theme, bounds: Rectangle, _cursor: canvas::Cursor, -- cgit From 0148cfc82b0076a050e13d72a7c1b144a8c67033 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Thu, 8 Jun 2023 07:51:32 -0700 Subject: fix: RedrawRequest::NextFrame -> RedrawRequest::At() --- examples/loading_spinners/src/circular.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'examples/loading_spinners/src/circular.rs') diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index 8639780a..56ffa6f5 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -266,6 +266,8 @@ where _clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, ) -> event::Status { + const FRAME_RATE: u64 = 60; + let state = tree.state.downcast_mut::(); if let Event::Window(window::Event::RedrawRequested(now)) = event { @@ -275,7 +277,9 @@ where now, ); - shell.request_redraw(RedrawRequest::NextFrame); + shell.request_redraw(RedrawRequest::At( + now + Duration::from_millis(1000 / FRAME_RATE), + )); } event::Status::Ignored -- cgit From 9d69af10ccb4317f01bff32e2e1e58d498b2fc89 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Thu, 8 Jun 2023 20:09:10 -0700 Subject: comment: fix typo --- examples/loading_spinners/src/circular.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'examples/loading_spinners/src/circular.rs') diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index 56ffa6f5..4e7bdc18 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -31,7 +31,7 @@ where style: ::Style, easing: &'a Easing, cycle_duration: Duration, - rotaion_duration: Duration, + rotation_duration: Duration, } impl<'a, Theme> Circular<'a, Theme> @@ -46,7 +46,7 @@ where style: ::Style::default(), easing: &easing::STANDARD, cycle_duration: Duration::from_millis(600), - rotaion_duration: Duration::from_secs(2), + rotation_duration: Duration::from_secs(2), } } @@ -83,7 +83,7 @@ where /// Sets the base rotation duration of this [`Circular`]. This is the duration that a full /// rotation would take if the cycle rotation were set to 0.0 (no expanding or contracting) pub fn rotation_duration(mut self, duration: Duration) -> Self { - self.rotaion_duration = duration; + self.rotation_duration = duration; self } } @@ -273,7 +273,7 @@ where if let Event::Window(window::Event::RedrawRequested(now)) = event { *state = state.timed_transition( self.cycle_duration, - self.rotaion_duration, + self.rotation_duration, now, ); -- cgit From 290b47f312471db74e8b149e0f78df3891456208 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Fri, 9 Jun 2023 11:03:52 -0700 Subject: refactor: remove unnecessary canvas complexity --- examples/loading_spinners/src/circular.rs | 187 +++++++++++------------------- 1 file changed, 67 insertions(+), 120 deletions(-) (limited to 'examples/loading_spinners/src/circular.rs') diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index 4e7bdc18..55363de1 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -5,7 +5,7 @@ use iced::advanced::widget::tree::{self, Tree}; use iced::advanced::{Clipboard, Layout, Renderer, Shell, Widget}; use iced::event; use iced::time::Instant; -use iced::widget::canvas::{self, Cursor, Program}; +use iced::widget::canvas; use iced::window::{self, RedrawRequest}; use iced::{Background, Color, Element, Rectangle}; use iced::{Event, Length, Point, Size, Vector}; @@ -15,8 +15,6 @@ use super::easing::{self, Easing}; use std::f32::consts::PI; use std::time::Duration; -type R = iced::Renderer; - const MIN_RADIANS: f32 = PI / 8.0; const WRAP_RADIANS: f32 = 2.0 * PI - PI / 4.0; const BASE_ROTATION_SPEED: u32 = u32::MAX / 80; @@ -98,7 +96,7 @@ where } #[derive(Clone, Copy)] -enum State { +enum Animation { Expanding { start: Instant, progress: f32, @@ -113,7 +111,7 @@ enum State { }, } -impl Default for State { +impl Default for Animation { fn default() -> Self { Self::Expanding { start: Instant::now(), @@ -124,7 +122,7 @@ impl Default for State { } } -impl State { +impl Animation { fn next(&self, additional_rotation: u32, now: Instant) -> Self { match self { Self::Expanding { rotation, .. } => Self::Contracting { @@ -224,7 +222,14 @@ impl State { } } -impl<'a, Message, Theme> Widget> for Circular<'a, Theme> +#[derive(Default)] +struct State { + animation: Animation, + cache: canvas::Cache, +} + +impl<'a, Message, Theme> Widget> + for Circular<'a, Theme> where Message: 'a + Clone, Theme: StyleSheet, @@ -271,12 +276,13 @@ where let state = tree.state.downcast_mut::(); if let Event::Window(window::Event::RedrawRequested(now)) = event { - *state = state.timed_transition( + state.animation = state.animation.timed_transition( self.cycle_duration, self.rotation_duration, now, ); + state.cache.clear(); shell.request_redraw(RedrawRequest::At( now + Duration::from_millis(1000 / FRAME_RATE), )); @@ -288,42 +294,76 @@ where fn draw( &self, tree: &Tree, - renderer: &mut R, + renderer: &mut iced::Renderer, theme: &Theme, _style: &renderer::Style, layout: Layout<'_>, _cursor_position: Point, _viewport: &Rectangle, ) { - let bounds = layout.bounds(); let state = tree.state.downcast_ref::(); + let bounds = layout.bounds(); + let custom_style = + ::appearance(theme, &self.style); + + let geometry = state.cache.draw(renderer, bounds.size(), |frame| { + let track_radius = frame.width() / 2.0 - self.bar_height; + let track_path = canvas::Path::circle(frame.center(), track_radius); + + frame.stroke( + &track_path, + canvas::Stroke::default() + .with_color(custom_style.track_color) + .with_width(self.bar_height), + ); + + let mut builder = canvas::path::Builder::new(); + + let start = state.animation.rotation() * 2.0 * PI; + + match state.animation { + Animation::Expanding { progress, .. } => { + builder.arc(canvas::path::Arc { + center: frame.center(), + radius: track_radius, + start_angle: start, + end_angle: start + + MIN_RADIANS + + WRAP_RADIANS * (self.easing.y_at_x(progress)), + }); + } + Animation::Contracting { progress, .. } => { + builder.arc(canvas::path::Arc { + center: frame.center(), + radius: track_radius, + start_angle: start + + WRAP_RADIANS * (self.easing.y_at_x(progress)), + end_angle: start + MIN_RADIANS + WRAP_RADIANS, + }); + } + } + + let bar_path = builder.build(); + + frame.stroke( + &bar_path, + canvas::Stroke::default() + .with_color(custom_style.bar_color) + .with_width(self.bar_height), + ); + }); renderer.with_translation( Vector::new(bounds.x, bounds.y), |renderer| { - canvas::Renderer::draw( - renderer, - as Program>>::draw( - &StateWithStyle { - state, - style: &self.style, - bar_height: self.bar_height, - easing: self.easing, - }, - &(), - renderer, - theme, - bounds, - Cursor::Unavailable, - ), - ); + renderer.draw_primitive(geometry.0); }, ); } } impl<'a, Message, Theme> From> - for Element<'a, Message, R> + for Element<'a, Message, iced::Renderer> where Message: Clone + 'a, Theme: StyleSheet + 'a, @@ -375,96 +415,3 @@ impl StyleSheet for iced::Theme { } } } - -struct StateWithStyle<'a, Theme> -where - Theme: StyleSheet, -{ - state: &'a State, - style: &'a ::Style, - easing: &'a Easing, - bar_height: f32, -} - -impl<'a, Message, Theme> Program> - for StateWithStyle<'a, Theme> -where - Theme: StyleSheet, -{ - type State = (); - - fn update( - &self, - _state: &mut Self::State, - _event: canvas::Event, - _bounds: Rectangle, - _cursor: canvas::Cursor, - ) -> (canvas::event::Status, Option) { - (canvas::event::Status::Ignored, None) - } - - fn draw( - &self, - _state: &Self::State, - renderer: &iced::Renderer, - theme: &Theme, - bounds: Rectangle, - _cursor: canvas::Cursor, - ) -> Vec { - let mut frame = canvas::Frame::new(renderer, bounds.size()); - let custom_style = ::appearance(theme, self.style); - let track_radius = frame.width() / 2.0 - self.bar_height; - - if let Some(Background::Color(color)) = custom_style.background { - let background_path = - canvas::Path::circle(frame.center(), track_radius); - frame.fill(&background_path, color); - } - - let track_path = canvas::Path::circle(frame.center(), track_radius); - - frame.stroke( - &track_path, - canvas::Stroke::default() - .with_color(custom_style.track_color) - .with_width(self.bar_height), - ); - - let mut builder = canvas::path::Builder::new(); - - let start = self.state.rotation() * 2.0 * PI; - - match self.state { - State::Expanding { progress, .. } => { - builder.arc(canvas::path::Arc { - center: frame.center(), - radius: track_radius, - start_angle: start, - end_angle: start - + MIN_RADIANS - + WRAP_RADIANS * (self.easing.y_at_x(*progress)), - }); - } - State::Contracting { progress, .. } => { - builder.arc(canvas::path::Arc { - center: frame.center(), - radius: track_radius, - start_angle: start - + WRAP_RADIANS * (self.easing.y_at_x(*progress)), - end_angle: start + MIN_RADIANS + WRAP_RADIANS, - }); - } - } - - let bar_path = builder.build(); - - frame.stroke( - &bar_path, - canvas::Stroke::default() - .with_color(custom_style.bar_color) - .with_width(self.bar_height), - ); - - vec![frame.into_geometry()] - } -} -- cgit From af62ec1c877d0d6d29277fb7dcfd4c681fd499af Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 28 Jun 2023 00:11:41 +0200 Subject: Fix `loading_spinners` example --- examples/loading_spinners/src/circular.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'examples/loading_spinners/src/circular.rs') diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index 55363de1..4cef081e 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -4,11 +4,13 @@ use iced::advanced::renderer; use iced::advanced::widget::tree::{self, Tree}; use iced::advanced::{Clipboard, Layout, Renderer, Shell, Widget}; use iced::event; +use iced::mouse; use iced::time::Instant; use iced::widget::canvas; use iced::window::{self, RedrawRequest}; -use iced::{Background, Color, Element, Rectangle}; -use iced::{Event, Length, Point, Size, Vector}; +use iced::{ + Background, Color, Element, Event, Length, Rectangle, Size, Vector, +}; use super::easing::{self, Easing}; @@ -266,7 +268,7 @@ where tree: &mut Tree, event: Event, _layout: Layout<'_>, - _cursor_position: Point, + _cursor: mouse::Cursor, _renderer: &iced::Renderer, _clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, @@ -298,7 +300,7 @@ where theme: &Theme, _style: &renderer::Style, layout: Layout<'_>, - _cursor_position: Point, + _cursor: mouse::Cursor, _viewport: &Rectangle, ) { let state = tree.state.downcast_ref::(); -- cgit From 0ae1baa37bd7b6607f79b33b8a6d8c5daafde0b2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 22 Jun 2023 00:38:36 +0200 Subject: Introduce custom backend-specific primitives --- examples/loading_spinners/src/circular.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'examples/loading_spinners/src/circular.rs') diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index 4cef081e..3a35e029 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -358,7 +358,9 @@ where renderer.with_translation( Vector::new(bounds.x, bounds.y), |renderer| { - renderer.draw_primitive(geometry.0); + use iced::advanced::graphics::geometry::Renderer as _; + + renderer.draw(vec![geometry]); }, ); } -- cgit