iced_widget/shader/program.rs
1use crate::core::Rectangle;
2use crate::core::mouse;
3use crate::renderer::wgpu::Primitive;
4use crate::shader::{self, Action};
5
6/// The state and logic of a [`Shader`] widget.
7///
8/// A [`Program`] can mutate the internal state of a [`Shader`] widget
9/// and produce messages for an application.
10///
11/// [`Shader`]: crate::Shader
12pub trait Program<Message> {
13 /// The internal state of the [`Program`].
14 type State: Default + 'static;
15
16 /// The type of primitive this [`Program`] can draw.
17 type Primitive: Primitive + 'static;
18
19 /// Update the internal [`State`] of the [`Program`]. This can be used to reflect state changes
20 /// based on mouse & other events. You can return an [`Action`] to publish a message, request a
21 /// redraw, or capture the event.
22 ///
23 /// By default, this method returns `None`.
24 ///
25 /// [`State`]: Self::State
26 fn update(
27 &self,
28 _state: &mut Self::State,
29 _event: &shader::Event,
30 _bounds: Rectangle,
31 _cursor: mouse::Cursor,
32 ) -> Option<Action<Message>> {
33 None
34 }
35
36 /// Draws the [`Primitive`].
37 ///
38 /// [`Primitive`]: Self::Primitive
39 fn draw(
40 &self,
41 state: &Self::State,
42 cursor: mouse::Cursor,
43 bounds: Rectangle,
44 ) -> Self::Primitive;
45
46 /// Returns the current mouse interaction of the [`Program`].
47 ///
48 /// The interaction returned will be in effect even if the cursor position is out of
49 /// bounds of the [`Shader`]'s program.
50 ///
51 /// [`Shader`]: crate::Shader
52 fn mouse_interaction(
53 &self,
54 _state: &Self::State,
55 _bounds: Rectangle,
56 _cursor: mouse::Cursor,
57 ) -> mouse::Interaction {
58 mouse::Interaction::default()
59 }
60}