From c52fd089f102be4e2ac07952106e2b6924e72e68 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 9 Mar 2022 18:27:22 +0700 Subject: Use associated type for `Message` in a `canvas::Program` --- examples/bezier_tool/src/main.rs | 4 +++- examples/clock/src/main.rs | 4 +++- examples/color_palette/src/main.rs | 4 +++- examples/game_of_life/src/main.rs | 4 +++- examples/solar_system/src/main.rs | 4 +++- graphics/src/widget/canvas.rs | 26 ++++++++++++-------------- graphics/src/widget/canvas/program.rs | 15 ++++++++++----- 7 files changed, 37 insertions(+), 24 deletions(-) diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index 35b5182c..fc7ef0c9 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -104,7 +104,9 @@ mod bezier { curves: &'a [Curve], } - impl<'a> canvas::Program for Bezier<'a> { + impl<'a> canvas::Program for Bezier<'a> { + type Message = Curve; + fn update( &mut self, event: Event, diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 325ccc1a..41d160c1 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -76,7 +76,9 @@ impl Application for Clock { } } -impl canvas::Program for Clock { +impl canvas::Program for Clock { + type Message = Message; + fn draw(&self, bounds: Rectangle, _cursor: Cursor) -> Vec { let clock = self.clock.draw(bounds.size(), |frame| { let center = frame.center(); diff --git a/examples/color_palette/src/main.rs b/examples/color_palette/src/main.rs index ad3004b0..c9c0637e 100644 --- a/examples/color_palette/src/main.rs +++ b/examples/color_palette/src/main.rs @@ -235,7 +235,9 @@ impl Theme { } } -impl canvas::Program for Theme { +impl canvas::Program for Theme { + type Message = Message; + fn draw(&self, bounds: Rectangle, _cursor: Cursor) -> Vec { let theme = self.canvas_cache.draw(bounds.size(), |frame| { self.draw(frame); diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index ab8b80e4..43ef8ffd 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -328,7 +328,9 @@ mod grid { } } - impl<'a> canvas::Program for Grid { + impl<'a> canvas::Program for Grid { + type Message = Message; + fn update( &mut self, event: Event, diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 12184dd1..bf96fee0 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -128,7 +128,9 @@ impl State { } } -impl canvas::Program for State { +impl canvas::Program for State { + type Message = Message; + fn draw( &self, bounds: Rectangle, diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index 65d7e37e..ced2ce53 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -12,7 +12,6 @@ use iced_native::{ Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Size, Vector, Widget, }; -use std::marker::PhantomData; pub mod event; pub mod path; @@ -73,7 +72,9 @@ pub use text::Text; /// } /// /// // Then, we implement the `Program` trait -/// impl Program<()> for Circle { +/// impl Program for Circle { +/// type Message = (); +/// /// fn draw(&self, bounds: Rectangle, _cursor: Cursor) -> Vec{ /// // We prepare a new `Frame` /// let mut frame = Frame::new(bounds.size()); @@ -93,14 +94,13 @@ pub use text::Text; /// let canvas = Canvas::new(Circle { radius: 50.0 }); /// ``` #[derive(Debug)] -pub struct Canvas> { +pub struct Canvas { width: Length, height: Length, program: P, - phantom: PhantomData, } -impl> Canvas { +impl Canvas

{ const DEFAULT_SIZE: u16 = 100; /// Creates a new [`Canvas`]. @@ -109,7 +109,6 @@ impl> Canvas { width: Length::Units(Self::DEFAULT_SIZE), height: Length::Units(Self::DEFAULT_SIZE), program, - phantom: PhantomData, } } @@ -126,9 +125,9 @@ impl> Canvas { } } -impl Widget> for Canvas +impl Widget> for Canvas

where - P: Program, + P: Program, B: Backend, { fn width(&self) -> Length { @@ -157,7 +156,7 @@ where cursor_position: Point, _renderer: &Renderer, _clipboard: &mut dyn Clipboard, - shell: &mut Shell<'_, Message>, + shell: &mut Shell<'_, P::Message>, ) -> event::Status { let bounds = layout.bounds(); @@ -232,14 +231,13 @@ where } } -impl<'a, Message, P, B> From> - for Element<'a, Message, Renderer> +impl<'a, P, B> From> for Element<'a, P::Message, Renderer> where - Message: 'static, - P: Program + 'a, + P::Message: 'static, + P: Program + 'a, B: Backend, { - fn from(canvas: Canvas) -> Element<'a, Message, Renderer> { + fn from(canvas: Canvas

) -> Element<'a, P::Message, Renderer> { Element::new(canvas) } } diff --git a/graphics/src/widget/canvas/program.rs b/graphics/src/widget/canvas/program.rs index 85a2f67b..f8b9ff2b 100644 --- a/graphics/src/widget/canvas/program.rs +++ b/graphics/src/widget/canvas/program.rs @@ -8,7 +8,10 @@ use iced_native::{mouse, Rectangle}; /// application. /// /// [`Canvas`]: crate::widget::Canvas -pub trait Program { +pub trait Program { + /// The [`Message`] produced by the [`Program`]. + type Message; + /// Updates the state of the [`Program`]. /// /// When a [`Program`] is used in a [`Canvas`], the runtime will call this @@ -25,7 +28,7 @@ pub trait Program { _event: Event, _bounds: Rectangle, _cursor: Cursor, - ) -> (event::Status, Option) { + ) -> (event::Status, Option) { (event::Status::Ignored, None) } @@ -53,16 +56,18 @@ pub trait Program { } } -impl Program for &mut T +impl Program for &mut T where - T: Program, + T: Program, { + type Message = T::Message; + fn update( &mut self, event: Event, bounds: Rectangle, cursor: Cursor, - ) -> (event::Status, Option) { + ) -> (event::Status, Option) { T::update(self, event, bounds, cursor) } -- cgit