From 3a0d34c0240f4421737a6a08761f99d6f8140d02 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 4 Mar 2023 05:37:11 +0100 Subject: Create `iced_widget` subcrate and re-organize the whole codebase --- widget/src/canvas/program.rs | 109 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 widget/src/canvas/program.rs (limited to 'widget/src/canvas/program.rs') diff --git a/widget/src/canvas/program.rs b/widget/src/canvas/program.rs new file mode 100644 index 00000000..efb33c56 --- /dev/null +++ b/widget/src/canvas/program.rs @@ -0,0 +1,109 @@ +use crate::canvas::event::{self, Event}; +use crate::canvas::mouse; +use crate::canvas::Cursor; +use crate::core::Rectangle; +use crate::graphics::geometry; + +/// The state and logic of a [`Canvas`]. +/// +/// A [`Program`] can mutate internal state and produce messages for an +/// application. +/// +/// [`Canvas`]: crate::widget::Canvas +pub trait Program +where + Renderer: geometry::Renderer, +{ + /// The internal state mutated by the [`Program`]. + type State: Default + 'static; + + /// Updates the [`State`](Self::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. + /// + /// [`Canvas`]: crate::widget::Canvas + fn update( + &self, + _state: &mut Self::State, + _event: Event, + _bounds: Rectangle, + _cursor: Cursor, + ) -> (event::Status, Option) { + (event::Status::Ignored, 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`]. + /// + /// [`Frame`]: crate::widget::canvas::Frame + /// [`Cache`]: crate::widget::canvas::Cache + fn draw( + &self, + state: &Self::State, + renderer: &Renderer, + theme: &Renderer::Theme, + bounds: Rectangle, + cursor: Cursor, + ) -> Vec; + + /// Returns the current mouse interaction of the [`Program`]. + /// + /// The interaction returned will be in effect even if the cursor position + /// is out of bounds of the program's [`Canvas`]. + /// + /// [`Canvas`]: crate::widget::Canvas + fn mouse_interaction( + &self, + _state: &Self::State, + _bounds: Rectangle, + _cursor: Cursor, + ) -> mouse::Interaction { + mouse::Interaction::default() + } +} + +impl Program for &T +where + Renderer: geometry::Renderer, + T: Program, +{ + type State = T::State; + + fn update( + &self, + state: &mut Self::State, + event: Event, + bounds: Rectangle, + cursor: Cursor, + ) -> (event::Status, Option) { + T::update(self, state, event, bounds, cursor) + } + + fn draw( + &self, + state: &Self::State, + renderer: &Renderer, + theme: &Renderer::Theme, + bounds: Rectangle, + cursor: Cursor, + ) -> Vec { + T::draw(self, state, renderer, theme, bounds, cursor) + } + + fn mouse_interaction( + &self, + state: &Self::State, + bounds: Rectangle, + cursor: Cursor, + ) -> mouse::Interaction { + T::mouse_interaction(self, state, bounds, cursor) + } +} -- cgit From 8622e998f2701e7f4ca8d2f71c85150f436a9945 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 11 May 2023 15:25:58 +0200 Subject: Write missing documentation in `iced_graphics` --- widget/src/canvas/program.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'widget/src/canvas/program.rs') diff --git a/widget/src/canvas/program.rs b/widget/src/canvas/program.rs index efb33c56..ad0fbb83 100644 --- a/widget/src/canvas/program.rs +++ b/widget/src/canvas/program.rs @@ -2,7 +2,7 @@ use crate::canvas::event::{self, Event}; use crate::canvas::mouse; use crate::canvas::Cursor; use crate::core::Rectangle; -use crate::graphics::geometry; +use crate::graphics::geometry::{self, Geometry}; /// The state and logic of a [`Canvas`]. /// @@ -52,7 +52,7 @@ where theme: &Renderer::Theme, bounds: Rectangle, cursor: Cursor, - ) -> Vec; + ) -> Vec; /// Returns the current mouse interaction of the [`Program`]. /// @@ -94,7 +94,7 @@ where theme: &Renderer::Theme, bounds: Rectangle, cursor: Cursor, - ) -> Vec { + ) -> Vec { T::draw(self, state, renderer, theme, bounds, cursor) } -- cgit