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 --- graphics/src/widget/canvas/program.rs | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'graphics/src/widget/canvas/program.rs') diff --git a/graphics/src/widget/canvas/program.rs b/graphics/src/widget/canvas/program.rs index dddc387d..656dbfa6 100644 --- a/graphics/src/widget/canvas/program.rs +++ b/graphics/src/widget/canvas/program.rs @@ -1,8 +1,7 @@ -use crate::canvas::event::{self, Event}; -use crate::canvas::{Cursor, Geometry}; - -use iced_native::mouse; -use iced_native::Rectangle; +use crate::widget::canvas::event::{self, Event}; +use crate::widget::canvas::mouse; +use crate::widget::canvas::{Cursor, Geometry}; +use crate::Rectangle; /// The state and logic of a [`Canvas`]. /// @@ -11,7 +10,10 @@ use iced_native::Rectangle; /// /// [`Canvas`]: crate::widget::Canvas pub trait Program { - /// Updates the state of the [`Program`]. + /// The internal state mutated by the [`Program`]. + type State: Default + 'static; + + /// Updates the [`State`](Self::State) of the [`Program`]. /// /// When a [`Program`] is used in a [`Canvas`], the runtime will call this /// method for each [`Event`]. @@ -23,7 +25,8 @@ pub trait Program { /// /// [`Canvas`]: crate::widget::Canvas fn update( - &mut self, + &self, + _state: &mut Self::State, _event: Event, _bounds: Rectangle, _cursor: Cursor, @@ -40,6 +43,7 @@ pub trait Program { /// [`Cache`]: crate::widget::canvas::Cache fn draw( &self, + state: &Self::State, theme: &Theme, bounds: Rectangle, cursor: Cursor, @@ -53,6 +57,7 @@ pub trait Program { /// [`Canvas`]: crate::widget::Canvas fn mouse_interaction( &self, + _state: &Self::State, _bounds: Rectangle, _cursor: Cursor, ) -> mouse::Interaction { @@ -60,33 +65,38 @@ pub trait Program { } } -impl Program for &mut T +impl Program for &T where T: Program, { + type State = T::State; + fn update( - &mut self, + &self, + state: &mut Self::State, event: Event, bounds: Rectangle, cursor: Cursor, ) -> (event::Status, Option) { - T::update(self, event, bounds, cursor) + T::update(self, state, event, bounds, cursor) } fn draw( &self, + state: &Self::State, theme: &Theme, bounds: Rectangle, cursor: Cursor, ) -> Vec { - T::draw(self, theme, bounds, cursor) + T::draw(self, state, theme, bounds, cursor) } fn mouse_interaction( &self, + state: &Self::State, bounds: Rectangle, cursor: Cursor, ) -> mouse::Interaction { - T::mouse_interaction(self, bounds, cursor) + T::mouse_interaction(self, state, bounds, cursor) } } -- cgit