summaryrefslogtreecommitdiffstats
path: root/graphics
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-03-09 18:27:22 +0700
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-03-09 18:29:10 +0700
commitc52fd089f102be4e2ac07952106e2b6924e72e68 (patch)
tree99326a479b6dd5c3c5ac50fd1dabe73193267c04 /graphics
parent12c1a3f829c801022d45f1a294d8fc7fa10606e5 (diff)
downloadiced-c52fd089f102be4e2ac07952106e2b6924e72e68.tar.gz
iced-c52fd089f102be4e2ac07952106e2b6924e72e68.tar.bz2
iced-c52fd089f102be4e2ac07952106e2b6924e72e68.zip
Use associated type for `Message` in a `canvas::Program`
Diffstat (limited to 'graphics')
-rw-r--r--graphics/src/widget/canvas.rs26
-rw-r--r--graphics/src/widget/canvas/program.rs15
2 files changed, 22 insertions, 19 deletions
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<Geometry>{
/// // 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<Message, P: Program<Message>> {
+pub struct Canvas<P: Program> {
width: Length,
height: Length,
program: P,
- phantom: PhantomData<Message>,
}
-impl<Message, P: Program<Message>> Canvas<Message, P> {
+impl<P: Program> Canvas<P> {
const DEFAULT_SIZE: u16 = 100;
/// Creates a new [`Canvas`].
@@ -109,7 +109,6 @@ impl<Message, P: Program<Message>> Canvas<Message, P> {
width: Length::Units(Self::DEFAULT_SIZE),
height: Length::Units(Self::DEFAULT_SIZE),
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
- P: Program<Message>,
+ P: Program,
B: Backend,
{
fn width(&self) -> Length {
@@ -157,7 +156,7 @@ where
cursor_position: Point,
_renderer: &Renderer<B>,
_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<Canvas<Message, P>>
- for Element<'a, Message, Renderer<B>>
+impl<'a, P, B> From<Canvas<P>> for Element<'a, P::Message, Renderer<B>>
where
- Message: 'static,
- P: Program<Message> + 'a,
+ P::Message: 'static,
+ P: Program + 'a,
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)
}
}
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<Message> {
+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<Message> {
_event: Event,
_bounds: Rectangle,
_cursor: Cursor,
- ) -> (event::Status, Option<Message>) {
+ ) -> (event::Status, Option<Self::Message>) {
(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
- T: Program<Message>,
+ T: Program,
{
+ type Message = T::Message;
+
fn update(
&mut self,
event: Event,
bounds: Rectangle,
cursor: Cursor,
- ) -> (event::Status, Option<Message>) {
+ ) -> (event::Status, Option<Self::Message>) {
T::update(self, event, bounds, cursor)
}