From 0b5028b1ab47707a469176e9bf20cacdd3a19861 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 19 Apr 2020 14:39:30 +0200 Subject: Draft `Program` interactivity for `Canvas` --- wgpu/src/widget/canvas/program.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 wgpu/src/widget/canvas/program.rs (limited to 'wgpu/src/widget/canvas/program.rs') diff --git a/wgpu/src/widget/canvas/program.rs b/wgpu/src/widget/canvas/program.rs new file mode 100644 index 00000000..c65a078b --- /dev/null +++ b/wgpu/src/widget/canvas/program.rs @@ -0,0 +1,16 @@ +use crate::canvas::{Event, Layer, Size}; + +pub trait Program { + type Input; + + fn layers<'a>(&'a self, input: &'a Self::Input) + -> Vec>; + + fn update<'a>( + &'a mut self, + _event: Event, + _bounds: Size, + _input: &'a Self::Input, + ) { + } +} -- cgit From 592cc685067c36cbba87e4db14f4ebc71d65b951 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 19 Apr 2020 21:55:23 +0200 Subject: Remove `Layer` trait and simplify `Canvas` --- wgpu/src/widget/canvas/program.rs | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 wgpu/src/widget/canvas/program.rs (limited to 'wgpu/src/widget/canvas/program.rs') diff --git a/wgpu/src/widget/canvas/program.rs b/wgpu/src/widget/canvas/program.rs deleted file mode 100644 index c65a078b..00000000 --- a/wgpu/src/widget/canvas/program.rs +++ /dev/null @@ -1,16 +0,0 @@ -use crate::canvas::{Event, Layer, Size}; - -pub trait Program { - type Input; - - fn layers<'a>(&'a self, input: &'a Self::Input) - -> Vec>; - - fn update<'a>( - &'a mut self, - _event: Event, - _bounds: Size, - _input: &'a Self::Input, - ) { - } -} -- cgit From 2539042b71d70afd4d8f262783d441e768811ee9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 06:24:12 +0200 Subject: Remove `Drawable` and rename `State` to `Program` --- wgpu/src/widget/canvas/program.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 wgpu/src/widget/canvas/program.rs (limited to 'wgpu/src/widget/canvas/program.rs') diff --git a/wgpu/src/widget/canvas/program.rs b/wgpu/src/widget/canvas/program.rs new file mode 100644 index 00000000..8e35fdfb --- /dev/null +++ b/wgpu/src/widget/canvas/program.rs @@ -0,0 +1,22 @@ +use crate::canvas::{Event, Geometry, Size}; + +pub trait Program { + fn update(&mut self, _event: Event, _bounds: Size) -> Option { + None + } + + fn draw(&self, bounds: Size) -> Vec; +} + +impl Program for &mut T +where + T: Program, +{ + fn update(&mut self, event: Event, bounds: Size) -> Option { + T::update(self, event, bounds) + } + + fn draw(&self, bounds: Size) -> Vec { + T::draw(self, bounds) + } +} -- cgit From 52719c7076cafb7b01967edf4df11ea72ae45aff Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 29 Apr 2020 03:16:03 +0200 Subject: Let a `canvas::Program` control the mouse cursor --- wgpu/src/widget/canvas/program.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'wgpu/src/widget/canvas/program.rs') diff --git a/wgpu/src/widget/canvas/program.rs b/wgpu/src/widget/canvas/program.rs index 8e35fdfb..9e4aca89 100644 --- a/wgpu/src/widget/canvas/program.rs +++ b/wgpu/src/widget/canvas/program.rs @@ -1,4 +1,5 @@ use crate::canvas::{Event, Geometry, Size}; +use iced_native::MouseCursor; pub trait Program { fn update(&mut self, _event: Event, _bounds: Size) -> Option { @@ -6,6 +7,10 @@ pub trait Program { } fn draw(&self, bounds: Size) -> Vec; + + fn mouse_cursor(&self, _bounds: Size) -> MouseCursor { + MouseCursor::default() + } } impl Program for &mut T @@ -19,4 +24,8 @@ where fn draw(&self, bounds: Size) -> Vec { T::draw(self, bounds) } + + fn mouse_cursor(&self, bounds: Size) -> MouseCursor { + T::mouse_cursor(self, bounds) + } } -- cgit From dc51080328caa12d2b1fc02febc72cab70bb9f50 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 29 Apr 2020 04:25:49 +0200 Subject: Introduce `Cursor` type in `canvas` --- wgpu/src/widget/canvas/program.rs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'wgpu/src/widget/canvas/program.rs') diff --git a/wgpu/src/widget/canvas/program.rs b/wgpu/src/widget/canvas/program.rs index 9e4aca89..f8e54514 100644 --- a/wgpu/src/widget/canvas/program.rs +++ b/wgpu/src/widget/canvas/program.rs @@ -1,14 +1,19 @@ -use crate::canvas::{Event, Geometry, Size}; -use iced_native::MouseCursor; +use crate::canvas::{Cursor, Event, Geometry}; +use iced_native::{MouseCursor, Rectangle}; pub trait Program { - fn update(&mut self, _event: Event, _bounds: Size) -> Option { + fn update( + &mut self, + _event: Event, + _bounds: Rectangle, + _cursor: Cursor, + ) -> Option { None } - fn draw(&self, bounds: Size) -> Vec; + fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Vec; - fn mouse_cursor(&self, _bounds: Size) -> MouseCursor { + fn mouse_cursor(&self, _bounds: Rectangle, _cursor: Cursor) -> MouseCursor { MouseCursor::default() } } @@ -17,15 +22,20 @@ impl Program for &mut T where T: Program, { - fn update(&mut self, event: Event, bounds: Size) -> Option { - T::update(self, event, bounds) + fn update( + &mut self, + event: Event, + bounds: Rectangle, + cursor: Cursor, + ) -> Option { + T::update(self, event, bounds, cursor) } - fn draw(&self, bounds: Size) -> Vec { - T::draw(self, bounds) + fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Vec { + T::draw(self, bounds, cursor) } - fn mouse_cursor(&self, bounds: Size) -> MouseCursor { - T::mouse_cursor(self, bounds) + fn mouse_cursor(&self, bounds: Rectangle, cursor: Cursor) -> MouseCursor { + T::mouse_cursor(self, bounds, cursor) } } -- cgit From d4c4198f7242f168de65146e0ca339e0c1cbfe9b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 07:38:46 +0200 Subject: Write documentation for the new `canvas` API --- wgpu/src/widget/canvas/program.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'wgpu/src/widget/canvas/program.rs') diff --git a/wgpu/src/widget/canvas/program.rs b/wgpu/src/widget/canvas/program.rs index f8e54514..5b995bfa 100644 --- a/wgpu/src/widget/canvas/program.rs +++ b/wgpu/src/widget/canvas/program.rs @@ -1,7 +1,27 @@ use crate::canvas::{Cursor, Event, Geometry}; use iced_native::{MouseCursor, Rectangle}; +/// The state and logic of a [`Canvas`]. +/// +/// A [`Program`] can mutate internal state and produce messages for an +/// application. +/// +/// [`Canvas`]: struct.Canvas.html +/// [`Program`]: trait.Program.html pub trait Program { + /// Updates the state of the [`Program`]. + /// + /// When a [`Program`] is used in a [`Canvas`], the runtime will call this + /// method for each [`Event`]. + /// + /// This method can optionally return a `Message` to notify an application + /// of any meaningful interactions. + /// + /// By default, this method does and returns nothing. + /// + /// [`Program`]: trait.Program.html + /// [`Canvas`]: struct.Canvas.html + /// [`Event`]: enum.Event.html fn update( &mut self, _event: Event, @@ -11,8 +31,24 @@ pub trait Program { None } + /// Draws the state of the [`Program`], producing a bunch of [`Geometry`]. + /// + /// [`Geometry`] can be easily generated with a [`Frame`] or stored in a + /// [`Cache`]. + /// + /// [`Program`]: trait.Program.html + /// [`Geometry`]: struct.Geometry.html + /// [`Frame`]: struct.Frame.html + /// [`Cache`]: struct.Cache.html fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Vec; + /// Returns the mouse cursor state of the [`Program`]. + /// + /// The mouse cursor returned will be in effect even if the cursor position + /// is out of bounds of the program's [`Canvas`]. + /// + /// [`Program`]: trait.Program.html + /// [`Canvas`]: struct.Canvas.html fn mouse_cursor(&self, _bounds: Rectangle, _cursor: Cursor) -> MouseCursor { MouseCursor::default() } -- cgit From 98bc8cf2a7c4944d762a0148ca9f615d6ccc0d6e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 08:16:38 +0200 Subject: Rename `MouseCursor` to `mouse::Interaction` --- wgpu/src/widget/canvas/program.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'wgpu/src/widget/canvas/program.rs') diff --git a/wgpu/src/widget/canvas/program.rs b/wgpu/src/widget/canvas/program.rs index 5b995bfa..725d9d72 100644 --- a/wgpu/src/widget/canvas/program.rs +++ b/wgpu/src/widget/canvas/program.rs @@ -1,5 +1,5 @@ use crate::canvas::{Cursor, Event, Geometry}; -use iced_native::{MouseCursor, Rectangle}; +use iced_native::{mouse, Rectangle}; /// The state and logic of a [`Canvas`]. /// @@ -42,15 +42,19 @@ pub trait Program { /// [`Cache`]: struct.Cache.html fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Vec; - /// Returns the mouse cursor state of the [`Program`]. + /// Returns the current mouse interaction of the [`Program`]. /// - /// The mouse cursor returned will be in effect even if the cursor position + /// The interaction returned will be in effect even if the cursor position /// is out of bounds of the program's [`Canvas`]. /// /// [`Program`]: trait.Program.html /// [`Canvas`]: struct.Canvas.html - fn mouse_cursor(&self, _bounds: Rectangle, _cursor: Cursor) -> MouseCursor { - MouseCursor::default() + fn mouse_interaction( + &self, + _bounds: Rectangle, + _cursor: Cursor, + ) -> mouse::Interaction { + mouse::Interaction::default() } } @@ -71,7 +75,11 @@ where T::draw(self, bounds, cursor) } - fn mouse_cursor(&self, bounds: Rectangle, cursor: Cursor) -> MouseCursor { - T::mouse_cursor(self, bounds, cursor) + fn mouse_interaction( + &self, + bounds: Rectangle, + cursor: Cursor, + ) -> mouse::Interaction { + T::mouse_interaction(self, bounds, cursor) } } -- cgit