diff options
Diffstat (limited to 'graphics/src/widget/canvas/program.rs')
-rw-r--r-- | graphics/src/widget/canvas/program.rs | 34 |
1 files changed, 22 insertions, 12 deletions
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<Message, Theme = iced_native::Theme> { - /// 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<Message, Theme = iced_native::Theme> { /// /// [`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<Message, Theme = iced_native::Theme> { /// [`Cache`]: crate::widget::canvas::Cache fn draw( &self, + state: &Self::State, theme: &Theme, bounds: Rectangle, cursor: Cursor, @@ -53,6 +57,7 @@ pub trait Program<Message, Theme = iced_native::Theme> { /// [`Canvas`]: crate::widget::Canvas fn mouse_interaction( &self, + _state: &Self::State, _bounds: Rectangle, _cursor: Cursor, ) -> mouse::Interaction { @@ -60,33 +65,38 @@ pub trait Program<Message, Theme = iced_native::Theme> { } } -impl<T, Message, Theme> Program<Message, Theme> for &mut T +impl<Message, Theme, T> Program<Message, Theme> for &T where T: Program<Message, Theme>, { + type State = T::State; + fn update( - &mut self, + &self, + state: &mut Self::State, event: Event, bounds: Rectangle, cursor: Cursor, ) -> (event::Status, Option<Message>) { - 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<Geometry> { - 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) } } |