2023-03-04 05:37:11 +01:00
|
|
|
use crate::canvas::event::{self, Event};
|
|
|
|
|
use crate::canvas::mouse;
|
|
|
|
|
use crate::core::Rectangle;
|
2023-06-22 00:38:36 +02:00
|
|
|
use crate::graphics::geometry;
|
2020-04-19 21:55:23 +02:00
|
|
|
|
2020-04-30 07:38:46 +02:00
|
|
|
/// The state and logic of a [`Canvas`].
|
|
|
|
|
///
|
|
|
|
|
/// A [`Program`] can mutate internal state and produce messages for an
|
|
|
|
|
/// application.
|
|
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`Canvas`]: crate::widget::Canvas
|
2023-03-03 04:57:55 +01:00
|
|
|
pub trait Program<Message, Renderer = crate::Renderer>
|
2023-03-01 21:34:26 +01:00
|
|
|
where
|
2023-03-04 05:37:11 +01:00
|
|
|
Renderer: geometry::Renderer,
|
2023-03-01 21:34:26 +01:00
|
|
|
{
|
2022-07-27 06:49:20 +02:00
|
|
|
/// The internal state mutated by the [`Program`].
|
|
|
|
|
type State: Default + 'static;
|
|
|
|
|
|
|
|
|
|
/// Updates the [`State`](Self::State) of the [`Program`].
|
2020-04-30 07:38:46 +02:00
|
|
|
///
|
|
|
|
|
/// 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.
|
|
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`Canvas`]: crate::widget::Canvas
|
2020-04-29 04:25:49 +02:00
|
|
|
fn update(
|
2022-07-27 06:49:20 +02:00
|
|
|
&self,
|
|
|
|
|
_state: &mut Self::State,
|
2020-04-29 04:25:49 +02:00
|
|
|
_event: Event,
|
|
|
|
|
_bounds: Rectangle,
|
2023-06-08 20:11:59 +02:00
|
|
|
_cursor: mouse::Cursor,
|
2020-11-12 01:24:59 +01:00
|
|
|
) -> (event::Status, Option<Message>) {
|
|
|
|
|
(event::Status::Ignored, None)
|
2020-04-28 03:46:03 +02:00
|
|
|
}
|
2020-04-19 21:55:23 +02:00
|
|
|
|
2020-04-30 07:38:46 +02:00
|
|
|
/// Draws the state of the [`Program`], producing a bunch of [`Geometry`].
|
|
|
|
|
///
|
|
|
|
|
/// [`Geometry`] can be easily generated with a [`Frame`] or stored in a
|
|
|
|
|
/// [`Cache`].
|
|
|
|
|
///
|
2021-06-24 00:16:08 +09:00
|
|
|
/// [`Frame`]: crate::widget::canvas::Frame
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`Cache`]: crate::widget::canvas::Cache
|
2022-06-07 05:24:43 +02:00
|
|
|
fn draw(
|
|
|
|
|
&self,
|
2022-07-27 06:49:20 +02:00
|
|
|
state: &Self::State,
|
2023-03-01 21:34:26 +01:00
|
|
|
renderer: &Renderer,
|
|
|
|
|
theme: &Renderer::Theme,
|
2022-06-07 05:24:43 +02:00
|
|
|
bounds: Rectangle,
|
2023-06-08 20:11:59 +02:00
|
|
|
cursor: mouse::Cursor,
|
2023-06-22 00:38:36 +02:00
|
|
|
) -> Vec<Renderer::Geometry>;
|
2020-04-29 03:16:03 +02:00
|
|
|
|
2020-04-30 08:16:38 +02:00
|
|
|
/// Returns the current mouse interaction of the [`Program`].
|
2020-04-30 07:38:46 +02:00
|
|
|
///
|
2020-04-30 08:16:38 +02:00
|
|
|
/// The interaction returned will be in effect even if the cursor position
|
2020-04-30 07:38:46 +02:00
|
|
|
/// is out of bounds of the program's [`Canvas`].
|
|
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`Canvas`]: crate::widget::Canvas
|
2020-04-30 08:16:38 +02:00
|
|
|
fn mouse_interaction(
|
|
|
|
|
&self,
|
2022-07-27 06:49:20 +02:00
|
|
|
_state: &Self::State,
|
2020-04-30 08:16:38 +02:00
|
|
|
_bounds: Rectangle,
|
2023-06-08 20:11:59 +02:00
|
|
|
_cursor: mouse::Cursor,
|
2020-04-30 08:16:38 +02:00
|
|
|
) -> mouse::Interaction {
|
|
|
|
|
mouse::Interaction::default()
|
2020-04-29 03:16:03 +02:00
|
|
|
}
|
2020-04-19 21:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-01 21:34:26 +01:00
|
|
|
impl<Message, Renderer, T> Program<Message, Renderer> for &T
|
2020-04-19 21:55:23 +02:00
|
|
|
where
|
2023-03-04 05:37:11 +01:00
|
|
|
Renderer: geometry::Renderer,
|
2023-03-01 21:34:26 +01:00
|
|
|
T: Program<Message, Renderer>,
|
2020-04-19 21:55:23 +02:00
|
|
|
{
|
2022-07-27 06:49:20 +02:00
|
|
|
type State = T::State;
|
|
|
|
|
|
2020-04-29 04:25:49 +02:00
|
|
|
fn update(
|
2022-07-27 06:49:20 +02:00
|
|
|
&self,
|
|
|
|
|
state: &mut Self::State,
|
2020-04-29 04:25:49 +02:00
|
|
|
event: Event,
|
|
|
|
|
bounds: Rectangle,
|
2023-06-08 20:11:59 +02:00
|
|
|
cursor: mouse::Cursor,
|
2020-11-12 01:24:59 +01:00
|
|
|
) -> (event::Status, Option<Message>) {
|
2022-07-27 06:49:20 +02:00
|
|
|
T::update(self, state, event, bounds, cursor)
|
2020-04-19 21:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 05:24:43 +02:00
|
|
|
fn draw(
|
|
|
|
|
&self,
|
2022-07-27 06:49:20 +02:00
|
|
|
state: &Self::State,
|
2023-03-01 21:34:26 +01:00
|
|
|
renderer: &Renderer,
|
|
|
|
|
theme: &Renderer::Theme,
|
2022-06-07 05:24:43 +02:00
|
|
|
bounds: Rectangle,
|
2023-06-08 20:11:59 +02:00
|
|
|
cursor: mouse::Cursor,
|
2023-06-22 00:38:36 +02:00
|
|
|
) -> Vec<Renderer::Geometry> {
|
2023-03-01 21:34:26 +01:00
|
|
|
T::draw(self, state, renderer, theme, bounds, cursor)
|
2020-04-19 21:55:23 +02:00
|
|
|
}
|
2020-04-29 03:16:03 +02:00
|
|
|
|
2020-04-30 08:16:38 +02:00
|
|
|
fn mouse_interaction(
|
|
|
|
|
&self,
|
2022-07-27 06:49:20 +02:00
|
|
|
state: &Self::State,
|
2020-04-30 08:16:38 +02:00
|
|
|
bounds: Rectangle,
|
2023-06-08 20:11:59 +02:00
|
|
|
cursor: mouse::Cursor,
|
2020-04-30 08:16:38 +02:00
|
|
|
) -> mouse::Interaction {
|
2022-07-27 06:49:20 +02:00
|
|
|
T::mouse_interaction(self, state, bounds, cursor)
|
2020-04-29 03:16:03 +02:00
|
|
|
}
|
2020-04-19 21:55:23 +02:00
|
|
|
}
|