diff options
| author | 2022-03-23 17:11:14 +0700 | |
|---|---|---|
| committer | 2022-03-23 17:11:14 +0700 | |
| commit | 0eef527fa5b04be74141c75b076677473320e321 (patch) | |
| tree | 5062a9ce2c370632de87a01471526da1176e0a60 /graphics/src/widget/pure/canvas | |
| parent | 4aece6b77617f4a37af8208d8ddb1618bf9052d3 (diff) | |
| parent | ef4c79ea23e86fec9a8ad0fb27463296c14400e5 (diff) | |
| download | iced-0eef527fa5b04be74141c75b076677473320e321.tar.gz iced-0eef527fa5b04be74141c75b076677473320e321.tar.bz2 iced-0eef527fa5b04be74141c75b076677473320e321.zip | |
Merge pull request #1284 from iced-rs/virtual-widgets
Stateless widgets
Diffstat (limited to 'graphics/src/widget/pure/canvas')
| -rw-r--r-- | graphics/src/widget/pure/canvas/program.rs | 100 | 
1 files changed, 100 insertions, 0 deletions
| diff --git a/graphics/src/widget/pure/canvas/program.rs b/graphics/src/widget/pure/canvas/program.rs new file mode 100644 index 00000000..ee74c27f --- /dev/null +++ b/graphics/src/widget/pure/canvas/program.rs @@ -0,0 +1,100 @@ +use crate::widget::pure::canvas::event::{self, Event}; +use crate::widget::pure::canvas::mouse; +use crate::widget::pure::canvas::{Cursor, Geometry}; +use crate::Rectangle; + +/// 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<Message> { +    /// The internal [`State`] mutated by the [`Program`]. +    type State: Default + 'static; + +    /// 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. +    /// +    /// [`Canvas`]: crate::widget::Canvas +    fn update( +        &self, +        _state: &mut Self::State, +        _event: Event, +        _bounds: Rectangle, +        _cursor: Cursor, +    ) -> (event::Status, Option<Message>) { +        (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, +        bounds: Rectangle, +        cursor: Cursor, +    ) -> Vec<Geometry>; + +    /// 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<Message, T> Program<Message> for &T +where +    T: Program<Message>, +{ +    type State = T::State; + +    fn update( +        &self, +        state: &mut Self::State, +        event: Event, +        bounds: Rectangle, +        cursor: Cursor, +    ) -> (event::Status, Option<Message>) { +        T::update(self, state, event, bounds, cursor) +    } + +    fn draw( +        &self, +        state: &Self::State, +        bounds: Rectangle, +        cursor: Cursor, +    ) -> Vec<Geometry> { +        T::draw(self, state, bounds, cursor) +    } + +    fn mouse_interaction( +        &self, +        state: &Self::State, +        bounds: Rectangle, +        cursor: Cursor, +    ) -> mouse::Interaction { +        T::mouse_interaction(self, state, bounds, cursor) +    } +} | 
