iced_widget/canvas/
program.rs

1use crate::canvas::mouse;
2use crate::canvas::{Event, Geometry};
3use crate::core::Rectangle;
4use crate::graphics::geometry;
5use crate::Action;
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 an [`Action`] to either notify an
26    /// application of any meaningful interactions, capture the event, or
27    /// request a redraw.
28    ///
29    /// By default, this method does and returns nothing.
30    ///
31    /// [`Canvas`]: crate::Canvas
32    fn update(
33        &self,
34        _state: &mut Self::State,
35        _event: &Event,
36        _bounds: Rectangle,
37        _cursor: mouse::Cursor,
38    ) -> Option<Action<Message>> {
39        None
40    }
41
42    /// Draws the state of the [`Program`], producing a bunch of [`Geometry`].
43    ///
44    /// [`Geometry`] can be easily generated with a [`Frame`] or stored in a
45    /// [`Cache`].
46    ///
47    /// [`Geometry`]: crate::canvas::Geometry
48    /// [`Frame`]: crate::canvas::Frame
49    /// [`Cache`]: crate::canvas::Cache
50    fn draw(
51        &self,
52        state: &Self::State,
53        renderer: &Renderer,
54        theme: &Theme,
55        bounds: Rectangle,
56        cursor: mouse::Cursor,
57    ) -> Vec<Geometry<Renderer>>;
58
59    /// Returns the current mouse interaction of the [`Program`].
60    ///
61    /// The interaction returned will be in effect even if the cursor position
62    /// is out of bounds of the program's [`Canvas`].
63    ///
64    /// [`Canvas`]: crate::Canvas
65    fn mouse_interaction(
66        &self,
67        _state: &Self::State,
68        _bounds: Rectangle,
69        _cursor: mouse::Cursor,
70    ) -> mouse::Interaction {
71        mouse::Interaction::default()
72    }
73}
74
75impl<Message, Theme, Renderer, T> Program<Message, Theme, Renderer> for &T
76where
77    Renderer: geometry::Renderer,
78    T: Program<Message, Theme, Renderer>,
79{
80    type State = T::State;
81
82    fn update(
83        &self,
84        state: &mut Self::State,
85        event: &Event,
86        bounds: Rectangle,
87        cursor: mouse::Cursor,
88    ) -> Option<Action<Message>> {
89        T::update(self, state, event, bounds, cursor)
90    }
91
92    fn draw(
93        &self,
94        state: &Self::State,
95        renderer: &Renderer,
96        theme: &Theme,
97        bounds: Rectangle,
98        cursor: mouse::Cursor,
99    ) -> Vec<Geometry<Renderer>> {
100        T::draw(self, state, renderer, theme, bounds, cursor)
101    }
102
103    fn mouse_interaction(
104        &self,
105        state: &Self::State,
106        bounds: Rectangle,
107        cursor: mouse::Cursor,
108    ) -> mouse::Interaction {
109        T::mouse_interaction(self, state, bounds, cursor)
110    }
111}