summaryrefslogtreecommitdiffstats
path: root/graphics/src/widget/canvas
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-03-18 22:13:52 +0700
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-03-18 22:13:52 +0700
commit32fd8dadda6636b11d4a1d9f59b467e57b3706a8 (patch)
tree6acb0c0d0a14c032939412f8d177dc08c5fd75aa /graphics/src/widget/canvas
parentd7100fd2597da82d97eaf196d50573ea64f3f8ff (diff)
downloadiced-32fd8dadda6636b11d4a1d9f59b467e57b3706a8.tar.gz
iced-32fd8dadda6636b11d4a1d9f59b467e57b3706a8.tar.bz2
iced-32fd8dadda6636b11d4a1d9f59b467e57b3706a8.zip
Reintroduce generic `Message` type for `canvas::Program`
As it is useful to make the `Message` completely free in many implementations.
Diffstat (limited to '')
-rw-r--r--graphics/src/widget/canvas.rs41
-rw-r--r--graphics/src/widget/canvas/program.rs15
2 files changed, 27 insertions, 29 deletions
diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs
index ced2ce53..6c526e35 100644
--- a/graphics/src/widget/canvas.rs
+++ b/graphics/src/widget/canvas.rs
@@ -6,13 +6,6 @@
use crate::renderer::{self, Renderer};
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 path;
@@ -36,6 +29,15 @@ pub use program::Program;
pub use stroke::{LineCap, LineDash, LineJoin, Stroke};
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.
///
/// # Examples
@@ -72,9 +74,7 @@ pub use text::Text;
/// }
///
/// // Then, we implement the `Program` trait
-/// impl Program for Circle {
-/// type Message = ();
-///
+/// impl Program<()> for Circle {
/// fn draw(&self, bounds: Rectangle, _cursor: Cursor) -> Vec<Geometry>{
/// // We prepare a new `Frame`
/// let mut frame = Frame::new(bounds.size());
@@ -94,13 +94,14 @@ pub use text::Text;
/// let canvas = Canvas::new(Circle { radius: 50.0 });
/// ```
#[derive(Debug)]
-pub struct Canvas<P: Program> {
+pub struct Canvas<Message, P: Program<Message>> {
width: Length,
height: Length,
program: P,
+ message_: PhantomData<Message>,
}
-impl<P: Program> Canvas<P> {
+impl<Message, P: Program<Message>> Canvas<Message, P> {
const DEFAULT_SIZE: u16 = 100;
/// Creates a new [`Canvas`].
@@ -109,6 +110,7 @@ impl<P: Program> Canvas<P> {
width: Length::Units(Self::DEFAULT_SIZE),
height: Length::Units(Self::DEFAULT_SIZE),
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
- P: Program,
+ P: Program<Message>,
B: Backend,
{
fn width(&self) -> Length {
@@ -156,7 +158,7 @@ where
cursor_position: Point,
_renderer: &Renderer<B>,
_clipboard: &mut dyn Clipboard,
- shell: &mut Shell<'_, P::Message>,
+ shell: &mut Shell<'_, Message>,
) -> event::Status {
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
- P::Message: 'static,
- P: Program + 'a,
+ Message: 'static,
+ P: Program<Message> + 'a,
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)
}
}
diff --git a/graphics/src/widget/canvas/program.rs b/graphics/src/widget/canvas/program.rs
index f8b9ff2b..85a2f67b 100644
--- a/graphics/src/widget/canvas/program.rs
+++ b/graphics/src/widget/canvas/program.rs
@@ -8,10 +8,7 @@ use iced_native::{mouse, Rectangle};
/// application.
///
/// [`Canvas`]: crate::widget::Canvas
-pub trait Program {
- /// The [`Message`] produced by the [`Program`].
- type Message;
-
+pub trait Program<Message> {
/// Updates the state of the [`Program`].
///
/// When a [`Program`] is used in a [`Canvas`], the runtime will call this
@@ -28,7 +25,7 @@ pub trait Program {
_event: Event,
_bounds: Rectangle,
_cursor: Cursor,
- ) -> (event::Status, Option<Self::Message>) {
+ ) -> (event::Status, Option<Message>) {
(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
- T: Program,
+ T: Program<Message>,
{
- type Message = T::Message;
-
fn update(
&mut self,
event: Event,
bounds: Rectangle,
cursor: Cursor,
- ) -> (event::Status, Option<Self::Message>) {
+ ) -> (event::Status, Option<Message>) {
T::update(self, event, bounds, cursor)
}