Replace stateful widgets with new iced_pure API

This commit is contained in:
Héctor Ramón Jiménez 2022-07-27 06:49:20 +02:00
parent c44267b85f
commit ff2519b1d4
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
142 changed files with 3631 additions and 14494 deletions

View file

@ -1,8 +1,7 @@
use crate::canvas::event::{self, Event};
use crate::canvas::{Cursor, Geometry};
use iced_native::mouse;
use iced_native::Rectangle;
use crate::widget::canvas::event::{self, Event};
use crate::widget::canvas::mouse;
use crate::widget::canvas::{Cursor, Geometry};
use crate::Rectangle;
/// The state and logic of a [`Canvas`].
///
@ -11,7 +10,10 @@ use iced_native::Rectangle;
///
/// [`Canvas`]: crate::widget::Canvas
pub trait Program<Message, Theme = iced_native::Theme> {
/// Updates the state of the [`Program`].
/// The internal state mutated by the [`Program`].
type State: Default + 'static;
/// Updates the [`State`](Self::State) of the [`Program`].
///
/// When a [`Program`] is used in a [`Canvas`], the runtime will call this
/// method for each [`Event`].
@ -23,7 +25,8 @@ pub trait Program<Message, Theme = iced_native::Theme> {
///
/// [`Canvas`]: crate::widget::Canvas
fn update(
&mut self,
&self,
_state: &mut Self::State,
_event: Event,
_bounds: Rectangle,
_cursor: Cursor,
@ -40,6 +43,7 @@ pub trait Program<Message, Theme = iced_native::Theme> {
/// [`Cache`]: crate::widget::canvas::Cache
fn draw(
&self,
state: &Self::State,
theme: &Theme,
bounds: Rectangle,
cursor: Cursor,
@ -53,6 +57,7 @@ pub trait Program<Message, Theme = iced_native::Theme> {
/// [`Canvas`]: crate::widget::Canvas
fn mouse_interaction(
&self,
_state: &Self::State,
_bounds: Rectangle,
_cursor: Cursor,
) -> mouse::Interaction {
@ -60,33 +65,38 @@ pub trait Program<Message, Theme = iced_native::Theme> {
}
}
impl<T, Message, Theme> Program<Message, Theme> for &mut T
impl<Message, Theme, T> Program<Message, Theme> for &T
where
T: Program<Message, Theme>,
{
type State = T::State;
fn update(
&mut self,
&self,
state: &mut Self::State,
event: Event,
bounds: Rectangle,
cursor: Cursor,
) -> (event::Status, Option<Message>) {
T::update(self, event, bounds, cursor)
T::update(self, state, event, bounds, cursor)
}
fn draw(
&self,
state: &Self::State,
theme: &Theme,
bounds: Rectangle,
cursor: Cursor,
) -> Vec<Geometry> {
T::draw(self, theme, bounds, cursor)
T::draw(self, state, theme, bounds, cursor)
}
fn mouse_interaction(
&self,
state: &Self::State,
bounds: Rectangle,
cursor: Cursor,
) -> mouse::Interaction {
T::mouse_interaction(self, bounds, cursor)
T::mouse_interaction(self, state, bounds, cursor)
}
}