iced_widget/canvas/
program.rs

1use crate::canvas::event::{self, Event};
2use crate::canvas::mouse;
3use crate::canvas::Geometry;
4use crate::core::Rectangle;
5use crate::graphics::geometry;
6
7/// The state and logic of a [`Canvas`].
8///
9/// A [`Program`] can mutate internal state and produce messages for an
10/// application.
11///
12/// [`Canvas`]: crate::Canvas
13pub trait Program<Message, Theme = crate::Theme, Renderer = crate::Renderer>
14where
15    Renderer: geometry::Renderer,
16{
17    /// The internal state mutated by the [`Program`].
18    type State: Default + 'static;
19
20    /// Updates the [`State`](Self::State) of the [`Program`].
21    ///
22    /// When a [`Program`] is used in a [`Canvas`], the runtime will call this
23    /// method for each [`Event`].
24    ///
25    /// This method can optionally return a `Message` to notify an application
26    /// of any meaningful interactions.
27    ///
28    /// By default, this method does and returns nothing.
29    ///
30    /// [`Canvas`]: crate::Canvas
31    fn update(
32        &self,
33        _state: &mut Self::State,
34        _event: Event,
35        _bounds: Rectangle,
36        _cursor: mouse::Cursor,
37    ) -> (event::Status, Option<Message>) {
38        (event::Status::Ignored, None)
39    }
40
41    /// Draws the state of the [`Program`], producing a bunch of [`Geometry`].
42    ///
43    /// [`Geometry`] can be easily generated with a [`Frame`] or stored in a
44    /// [`Cache`].
45    ///
46    /// [`Geometry`]: crate::canvas::Geometry
47    /// [`Frame`]: crate::canvas::Frame
48    /// [`Cache`]: crate::canvas::Cache
49    fn draw(
50        &self,
51        state: &Self::State,
52        renderer: &Renderer,
53        theme: &Theme,
54        bounds: Rectangle,
55        cursor: mouse::Cursor,
56    ) -> Vec<Geometry<Renderer>>;
57
58    /// Returns the current mouse interaction of the [`Program`].
59    ///
60    /// The interaction returned will be in effect even if the cursor position
61    /// is out of bounds of the program's [`Canvas`].
62    ///
63    /// [`Canvas`]: crate::Canvas
64    fn mouse_interaction(
65        &self,
66        _state: &Self::State,
67        _bounds: Rectangle,
68        _cursor: mouse::Cursor,
69    ) -> mouse::Interaction {
70        mouse::Interaction::default()
71    }
72}
73
74impl<Message, Theme, Renderer, T> Program<Message, Theme, Renderer> for &T
75where
76    Renderer: geometry::Renderer,
77    T: Program<Message, Theme, Renderer>,
78{
79    type State = T::State;
80
81    fn update(
82        &self,
83        state: &mut Self::State,
84        event: Event,
85        bounds: Rectangle,
86        cursor: mouse::Cursor,
87    ) -> (event::Status, Option<Message>) {
88        T::update(self, state, event, bounds, cursor)
89    }
90
91    fn draw(
92        &self,
93        state: &Self::State,
94        renderer: &Renderer,
95        theme: &Theme,
96        bounds: Rectangle,
97        cursor: mouse::Cursor,
98    ) -> Vec<Geometry<Renderer>> {
99        T::draw(self, state, renderer, theme, bounds, cursor)
100    }
101
102    fn mouse_interaction(
103        &self,
104        state: &Self::State,
105        bounds: Rectangle,
106        cursor: mouse::Cursor,
107    ) -> mouse::Interaction {
108        T::mouse_interaction(self, state, bounds, cursor)
109    }
110}