2022-07-27 06:49:20 +02:00
|
|
|
use crate::widget::canvas::event::{self, Event};
|
|
|
|
|
use crate::widget::canvas::mouse;
|
|
|
|
|
use crate::widget::canvas::{Cursor, Geometry};
|
|
|
|
|
use crate::Rectangle;
|
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
|
2022-06-07 05:24:43 +02:00
|
|
|
pub trait Program<Message, Theme = iced_native::Theme> {
|
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,
|
|
|
|
|
_cursor: 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,
|
2022-06-07 05:24:43 +02:00
|
|
|
theme: &Theme,
|
|
|
|
|
bounds: Rectangle,
|
|
|
|
|
cursor: Cursor,
|
|
|
|
|
) -> Vec<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,
|
|
|
|
|
_cursor: Cursor,
|
|
|
|
|
) -> mouse::Interaction {
|
|
|
|
|
mouse::Interaction::default()
|
2020-04-29 03:16:03 +02:00
|
|
|
}
|
2020-04-19 21:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-27 06:49:20 +02:00
|
|
|
impl<Message, Theme, T> Program<Message, Theme> for &T
|
2020-04-19 21:55:23 +02:00
|
|
|
where
|
2022-06-07 05:24:43 +02:00
|
|
|
T: Program<Message, Theme>,
|
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,
|
|
|
|
|
cursor: 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,
|
2022-06-07 05:24:43 +02:00
|
|
|
theme: &Theme,
|
|
|
|
|
bounds: Rectangle,
|
|
|
|
|
cursor: Cursor,
|
|
|
|
|
) -> Vec<Geometry> {
|
2022-07-27 06:49:20 +02:00
|
|
|
T::draw(self, state, 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,
|
|
|
|
|
cursor: Cursor,
|
|
|
|
|
) -> 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
|
|
|
}
|