Reintroduce generic Message type for canvas::Program

As it is useful to make the `Message` completely free in many
implementations.
This commit is contained in:
Héctor Ramón Jiménez 2022-03-18 22:13:52 +07:00
parent d7100fd259
commit 32fd8dadda
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
10 changed files with 55 additions and 68 deletions

View file

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

View file

@ -76,9 +76,7 @@ impl Application for Clock {
} }
} }
impl canvas::Program for Clock { impl<Message> canvas::Program<Message> 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,9 +235,7 @@ impl Theme {
} }
} }
impl canvas::Program for Theme { impl<Message> canvas::Program<Message> 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,9 +328,7 @@ mod grid {
} }
} }
impl<'a> canvas::Program for Grid { impl<'a> canvas::Program<Message> for Grid {
type Message = Message;
fn update( fn update(
&mut self, &mut self,
event: Event, event: Event,

View file

@ -394,8 +394,7 @@ mod grid {
} }
} }
impl canvas::Program for Grid { impl canvas::Program<Message> for Grid {
type Message = Message;
type State = Interaction; type State = Interaction;
fn update( fn update(

View file

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

View file

@ -6,13 +6,6 @@
use crate::renderer::{self, Renderer}; use crate::renderer::{self, Renderer};
use crate::{Backend, Primitive}; use crate::{Backend, Primitive};
use iced_native::layout;
use iced_native::mouse;
use iced_native::{
Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Size, Vector,
Widget,
};
pub mod event; pub mod event;
pub mod path; pub mod path;
@ -36,6 +29,15 @@ pub use program::Program;
pub use stroke::{LineCap, LineDash, LineJoin, Stroke}; pub use stroke::{LineCap, LineDash, LineJoin, Stroke};
pub use text::Text; pub use text::Text;
use iced_native::layout;
use iced_native::mouse;
use iced_native::{
Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Size, Vector,
Widget,
};
use std::marker::PhantomData;
/// A widget capable of drawing 2D graphics. /// A widget capable of drawing 2D graphics.
/// ///
/// # Examples /// # Examples
@ -72,9 +74,7 @@ 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());
@ -94,13 +94,14 @@ 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<P: Program> { pub struct Canvas<Message, P: Program<Message>> {
width: Length, width: Length,
height: Length, height: Length,
program: P, program: P,
message_: PhantomData<Message>,
} }
impl<P: Program> Canvas<P> { impl<Message, P: Program<Message>> Canvas<Message, P> {
const DEFAULT_SIZE: u16 = 100; const DEFAULT_SIZE: u16 = 100;
/// Creates a new [`Canvas`]. /// Creates a new [`Canvas`].
@ -109,6 +110,7 @@ impl<P: Program> Canvas<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,
message_: PhantomData,
} }
} }
@ -125,9 +127,9 @@ impl<P: Program> Canvas<P> {
} }
} }
impl<P, B> Widget<P::Message, Renderer<B>> for Canvas<P> impl<Message, P, B> Widget<Message, Renderer<B>> for Canvas<Message, P>
where where
P: Program, P: Program<Message>,
B: Backend, B: Backend,
{ {
fn width(&self) -> Length { fn width(&self) -> Length {
@ -156,7 +158,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<'_, P::Message>, shell: &mut Shell<'_, Message>,
) -> event::Status { ) -> event::Status {
let bounds = layout.bounds(); let bounds = layout.bounds();
@ -231,13 +233,14 @@ where
} }
} }
impl<'a, P, B> From<Canvas<P>> for Element<'a, P::Message, Renderer<B>> impl<'a, Message, P, B> From<Canvas<Message, P>>
for Element<'a, Message, Renderer<B>>
where where
P::Message: 'static, Message: 'static,
P: Program + 'a, P: Program<Message> + 'a,
B: Backend, B: Backend,
{ {
fn from(canvas: Canvas<P>) -> Element<'a, P::Message, Renderer<B>> { fn from(canvas: Canvas<Message, P>) -> Element<'a, Message, Renderer<B>> {
Element::new(canvas) Element::new(canvas)
} }
} }

View file

@ -8,10 +8,7 @@ use iced_native::{mouse, Rectangle};
/// application. /// application.
/// ///
/// [`Canvas`]: crate::widget::Canvas /// [`Canvas`]: crate::widget::Canvas
pub trait Program { pub trait Program<Message> {
/// 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
@ -28,7 +25,7 @@ pub trait Program {
_event: Event, _event: Event,
_bounds: Rectangle, _bounds: Rectangle,
_cursor: Cursor, _cursor: Cursor,
) -> (event::Status, Option<Self::Message>) { ) -> (event::Status, Option<Message>) {
(event::Status::Ignored, None) (event::Status::Ignored, None)
} }
@ -56,18 +53,16 @@ pub trait Program {
} }
} }
impl<T> Program for &mut T impl<T, Message> Program<Message> for &mut T
where where
T: Program, T: Program<Message>,
{ {
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<Self::Message>) { ) -> (event::Status, Option<Message>) {
T::update(self, event, bounds, cursor) T::update(self, event, bounds, cursor)
} }

View file

@ -18,6 +18,8 @@ use iced_native::{Clipboard, Length, Point, Rectangle, Shell, Size, Vector};
use iced_pure::widget::tree::{self, Tree}; use iced_pure::widget::tree::{self, Tree};
use iced_pure::{Element, Widget}; use iced_pure::{Element, Widget};
use std::marker::PhantomData;
/// A widget capable of drawing 2D graphics. /// A widget capable of drawing 2D graphics.
/// ///
/// ## Drawing a simple circle /// ## Drawing a simple circle
@ -40,8 +42,7 @@ use iced_pure::{Element, Widget};
/// } /// }
/// ///
/// // Then, we implement the `Program` trait /// // Then, we implement the `Program` trait
/// impl Program for Circle { /// impl Program<()> for Circle {
/// type Message = ();
/// type State = (); /// type State = ();
/// ///
/// fn draw(&self, _state: &(), bounds: Rectangle, _cursor: Cursor) -> Vec<Geometry>{ /// fn draw(&self, _state: &(), bounds: Rectangle, _cursor: Cursor) -> Vec<Geometry>{
@ -63,18 +64,19 @@ use iced_pure::{Element, Widget};
/// let canvas = Canvas::new(Circle { radius: 50.0 }); /// let canvas = Canvas::new(Circle { radius: 50.0 });
/// ``` /// ```
#[derive(Debug)] #[derive(Debug)]
pub struct Canvas<P> pub struct Canvas<Message, P>
where where
P: Program, P: Program<Message>,
{ {
width: Length, width: Length,
height: Length, height: Length,
program: P, program: P,
message_: PhantomData<Message>,
} }
impl<P> Canvas<P> impl<Message, P> Canvas<Message, P>
where where
P: Program, P: Program<Message>,
{ {
const DEFAULT_SIZE: u16 = 100; const DEFAULT_SIZE: u16 = 100;
@ -84,6 +86,7 @@ where
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,
message_: PhantomData,
} }
} }
@ -100,9 +103,9 @@ where
} }
} }
impl<P, B> Widget<P::Message, Renderer<B>> for Canvas<P> impl<Message, P, B> Widget<Message, Renderer<B>> for Canvas<Message, P>
where where
P: Program, P: Program<Message>,
B: Backend, B: Backend,
{ {
fn tag(&self) -> tree::Tag { fn tag(&self) -> tree::Tag {
@ -140,7 +143,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<'_, P::Message>, shell: &mut Shell<'_, Message>,
) -> event::Status { ) -> event::Status {
let bounds = layout.bounds(); let bounds = layout.bounds();
@ -221,13 +224,14 @@ where
} }
} }
impl<'a, P, B> From<Canvas<P>> for Element<'a, P::Message, Renderer<B>> impl<'a, Message, P, B> From<Canvas<Message, P>>
for Element<'a, Message, Renderer<B>>
where where
P::Message: 'static, Message: 'a,
P: Program + 'a, P: Program<Message> + 'a,
B: Backend, B: Backend,
{ {
fn from(canvas: Canvas<P>) -> Element<'a, P::Message, Renderer<B>> { fn from(canvas: Canvas<Message, P>) -> Element<'a, Message, Renderer<B>> {
Element::new(canvas) Element::new(canvas)
} }
} }

View file

@ -9,10 +9,7 @@ use crate::Rectangle;
/// application. /// application.
/// ///
/// [`Canvas`]: crate::widget::Canvas /// [`Canvas`]: crate::widget::Canvas
pub trait Program { pub trait Program<Message> {
/// The [`Message`] produced by the [`Program`].
type Message;
/// The internal [`State`] mutated by the [`Program`]. /// The internal [`State`] mutated by the [`Program`].
type State: Default + 'static; type State: Default + 'static;
@ -33,7 +30,7 @@ pub trait Program {
_event: Event, _event: Event,
_bounds: Rectangle, _bounds: Rectangle,
_cursor: Cursor, _cursor: Cursor,
) -> (event::Status, Option<Self::Message>) { ) -> (event::Status, Option<Message>) {
(event::Status::Ignored, None) (event::Status::Ignored, None)
} }
@ -67,11 +64,10 @@ pub trait Program {
} }
} }
impl<T> Program for &T impl<Message, T> Program<Message> for &T
where where
T: Program, T: Program<Message>,
{ {
type Message = T::Message;
type State = T::State; type State = T::State;
fn update( fn update(
@ -80,7 +76,7 @@ where
event: Event, event: Event,
bounds: Rectangle, bounds: Rectangle,
cursor: Cursor, cursor: Cursor,
) -> (event::Status, Option<Self::Message>) { ) -> (event::Status, Option<Message>) {
T::update(self, state, event, bounds, cursor) T::update(self, state, event, bounds, cursor)
} }