Use associated type for Message in a canvas::Program

This commit is contained in:
Héctor Ramón Jiménez 2022-03-09 18:27:22 +07:00
parent 12c1a3f829
commit c52fd089f1
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
7 changed files with 37 additions and 24 deletions

View file

@ -104,7 +104,9 @@ mod bezier {
curves: &'a [Curve], curves: &'a [Curve],
} }
impl<'a> canvas::Program<Curve> for Bezier<'a> { impl<'a> canvas::Program for Bezier<'a> {
type Message = Curve;
fn update( fn update(
&mut self, &mut self,
event: Event, event: Event,

View file

@ -76,7 +76,9 @@ impl Application for Clock {
} }
} }
impl canvas::Program<Message> for Clock { impl canvas::Program for Clock {
type Message = Message;
fn draw(&self, bounds: Rectangle, _cursor: Cursor) -> Vec<Geometry> { fn draw(&self, bounds: Rectangle, _cursor: Cursor) -> Vec<Geometry> {
let clock = self.clock.draw(bounds.size(), |frame| { let clock = self.clock.draw(bounds.size(), |frame| {
let center = frame.center(); let center = frame.center();

View file

@ -235,7 +235,9 @@ impl Theme {
} }
} }
impl canvas::Program<Message> for Theme { impl canvas::Program for Theme {
type Message = Message;
fn draw(&self, bounds: Rectangle, _cursor: Cursor) -> Vec<Geometry> { fn draw(&self, bounds: Rectangle, _cursor: Cursor) -> Vec<Geometry> {
let theme = self.canvas_cache.draw(bounds.size(), |frame| { let theme = self.canvas_cache.draw(bounds.size(), |frame| {
self.draw(frame); self.draw(frame);

View file

@ -328,7 +328,9 @@ mod grid {
} }
} }
impl<'a> canvas::Program<Message> for Grid { impl<'a> canvas::Program for Grid {
type Message = Message;
fn update( fn update(
&mut self, &mut self,
event: Event, event: Event,

View file

@ -128,7 +128,9 @@ impl State {
} }
} }
impl<Message> canvas::Program<Message> for State { impl canvas::Program for State {
type Message = Message;
fn draw( fn draw(
&self, &self,
bounds: Rectangle, bounds: Rectangle,

View file

@ -12,7 +12,6 @@ use iced_native::{
Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Size, Vector, Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Size, Vector,
Widget, Widget,
}; };
use std::marker::PhantomData;
pub mod event; pub mod event;
pub mod path; pub mod path;
@ -73,7 +72,9 @@ pub use text::Text;
/// } /// }
/// ///
/// // Then, we implement the `Program` trait /// // Then, we implement the `Program` trait
/// impl Program<()> for Circle { /// impl Program for Circle {
/// type Message = ();
///
/// fn draw(&self, bounds: Rectangle, _cursor: Cursor) -> Vec<Geometry>{ /// fn draw(&self, bounds: Rectangle, _cursor: Cursor) -> Vec<Geometry>{
/// // We prepare a new `Frame` /// // We prepare a new `Frame`
/// let mut frame = Frame::new(bounds.size()); /// let mut frame = Frame::new(bounds.size());
@ -93,14 +94,13 @@ pub use text::Text;
/// let canvas = Canvas::new(Circle { radius: 50.0 }); /// let canvas = Canvas::new(Circle { radius: 50.0 });
/// ``` /// ```
#[derive(Debug)] #[derive(Debug)]
pub struct Canvas<Message, P: Program<Message>> { pub struct Canvas<P: Program> {
width: Length, width: Length,
height: Length, height: Length,
program: P, program: P,
phantom: PhantomData<Message>,
} }
impl<Message, P: Program<Message>> Canvas<Message, P> { impl<P: Program> Canvas<P> {
const DEFAULT_SIZE: u16 = 100; const DEFAULT_SIZE: u16 = 100;
/// Creates a new [`Canvas`]. /// Creates a new [`Canvas`].
@ -109,7 +109,6 @@ impl<Message, P: Program<Message>> Canvas<Message, P> {
width: Length::Units(Self::DEFAULT_SIZE), width: Length::Units(Self::DEFAULT_SIZE),
height: Length::Units(Self::DEFAULT_SIZE), height: Length::Units(Self::DEFAULT_SIZE),
program, program,
phantom: PhantomData,
} }
} }
@ -126,9 +125,9 @@ impl<Message, P: Program<Message>> Canvas<Message, P> {
} }
} }
impl<Message, P, B> Widget<Message, Renderer<B>> for Canvas<Message, P> impl<P, B> Widget<P::Message, Renderer<B>> for Canvas<P>
where where
P: Program<Message>, P: Program,
B: Backend, B: Backend,
{ {
fn width(&self) -> Length { fn width(&self) -> Length {
@ -157,7 +156,7 @@ where
cursor_position: Point, cursor_position: Point,
_renderer: &Renderer<B>, _renderer: &Renderer<B>,
_clipboard: &mut dyn Clipboard, _clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>, shell: &mut Shell<'_, P::Message>,
) -> event::Status { ) -> event::Status {
let bounds = layout.bounds(); let bounds = layout.bounds();
@ -232,14 +231,13 @@ where
} }
} }
impl<'a, Message, P, B> From<Canvas<Message, P>> impl<'a, P, B> From<Canvas<P>> for Element<'a, P::Message, Renderer<B>>
for Element<'a, Message, Renderer<B>>
where where
Message: 'static, P::Message: 'static,
P: Program<Message> + 'a, P: Program + 'a,
B: Backend, B: Backend,
{ {
fn from(canvas: Canvas<Message, P>) -> Element<'a, Message, Renderer<B>> { fn from(canvas: Canvas<P>) -> Element<'a, P::Message, Renderer<B>> {
Element::new(canvas) Element::new(canvas)
} }
} }

View file

@ -8,7 +8,10 @@ use iced_native::{mouse, Rectangle};
/// application. /// application.
/// ///
/// [`Canvas`]: crate::widget::Canvas /// [`Canvas`]: crate::widget::Canvas
pub trait Program<Message> { pub trait Program {
/// The [`Message`] produced by the [`Program`].
type Message;
/// Updates the state of the [`Program`]. /// Updates the state of the [`Program`].
/// ///
/// When a [`Program`] is used in a [`Canvas`], the runtime will call this /// When a [`Program`] is used in a [`Canvas`], the runtime will call this
@ -25,7 +28,7 @@ pub trait Program<Message> {
_event: Event, _event: Event,
_bounds: Rectangle, _bounds: Rectangle,
_cursor: Cursor, _cursor: Cursor,
) -> (event::Status, Option<Message>) { ) -> (event::Status, Option<Self::Message>) {
(event::Status::Ignored, None) (event::Status::Ignored, None)
} }
@ -53,16 +56,18 @@ pub trait Program<Message> {
} }
} }
impl<T, Message> Program<Message> for &mut T impl<T> Program for &mut T
where where
T: Program<Message>, T: Program,
{ {
type Message = T::Message;
fn update( fn update(
&mut self, &mut self,
event: Event, event: Event,
bounds: Rectangle, bounds: Rectangle,
cursor: Cursor, cursor: Cursor,
) -> (event::Status, Option<Message>) { ) -> (event::Status, Option<Self::Message>) {
T::update(self, event, bounds, cursor) T::update(self, event, bounds, cursor)
} }