Allow canvas::State to produce messages

This commit is contained in:
Héctor Ramón Jiménez 2020-04-28 03:46:03 +02:00
parent 2ca73036ab
commit e4eb0553de
4 changed files with 31 additions and 17 deletions

View file

@ -80,7 +80,10 @@ impl Cache {
Geometry::from_primitive(Primitive::Cached { cache: primitive })
}
pub fn with<'a, T>(&'a self, input: T) -> impl crate::canvas::State + 'a
pub fn with<'a, T, Message>(
&'a self,
input: T,
) -> impl crate::canvas::State<Message> + 'a
where
T: Drawable + std::fmt::Debug + 'a,
{
@ -93,7 +96,7 @@ struct Bind<'a, T> {
input: T,
}
impl<'a, T> crate::canvas::State for Bind<'a, T>
impl<'a, T, Message> crate::canvas::State<Message> for Bind<'a, T>
where
T: Drawable + std::fmt::Debug + 'a,
{

View file

@ -1,17 +1,19 @@
use crate::canvas::{Event, Geometry, Size};
pub trait State {
fn update(&mut self, _event: Event, _bounds: Size) {}
pub trait State<Message> {
fn update(&mut self, _event: Event, _bounds: Size) -> Option<Message> {
None
}
fn draw(&self, bounds: Size) -> Vec<Geometry>;
}
impl<T> State for &mut T
impl<T, Message> State<Message> for &mut T
where
T: State,
T: State<Message>,
{
fn update(&mut self, event: Event, bounds: Size) {
T::update(self, event, bounds);
fn update(&mut self, event: Event, bounds: Size) -> Option<Message> {
T::update(self, event, bounds)
}
fn draw(&self, bounds: Size) -> Vec<Geometry> {