Remove Drawable and rename State to Program

This commit is contained in:
Héctor Ramón Jiménez 2020-04-28 06:24:12 +02:00
parent 7f1e7aea07
commit 2539042b71
11 changed files with 202 additions and 304 deletions

View file

@ -1,5 +1,5 @@
use crate::{
canvas::{Drawable, Frame, Geometry},
canvas::{Frame, Geometry},
Primitive,
};
@ -48,10 +48,11 @@ impl Cache {
*self.state.borrow_mut() = State::Empty;
}
pub fn draw<T>(&self, new_bounds: Size, input: T) -> Geometry
where
T: Drawable + std::fmt::Debug,
{
pub fn draw(
&self,
new_bounds: Size,
draw_fn: impl Fn(&mut Frame),
) -> Geometry {
use std::ops::Deref;
if let State::Filled { bounds, primitive } = self.state.borrow().deref()
@ -64,7 +65,7 @@ impl Cache {
}
let mut frame = Frame::new(new_bounds);
input.draw(&mut frame);
draw_fn(&mut frame);
let primitive = {
let geometry = frame.into_geometry();
@ -79,30 +80,6 @@ impl Cache {
Geometry::from_primitive(Primitive::Cached { cache: primitive })
}
pub fn with<'a, T, Message>(
&'a self,
input: T,
) -> impl crate::canvas::State<Message> + 'a
where
T: Drawable + std::fmt::Debug + 'a,
{
Bind { cache: self, input }
}
}
struct Bind<'a, T> {
cache: &'a Cache,
input: T,
}
impl<'a, T, Message> crate::canvas::State<Message> for Bind<'a, T>
where
T: Drawable + std::fmt::Debug + 'a,
{
fn draw(&self, bounds: Size) -> Vec<Geometry> {
vec![self.cache.draw(bounds, &self.input)]
}
}
impl std::fmt::Debug for State {

View file

@ -1,36 +0,0 @@
use crate::canvas::Frame;
/// A type that can be drawn on a [`Frame`].
///
/// [`Frame`]: struct.Frame.html
pub trait Drawable {
/// Draws the [`Drawable`] on the given [`Frame`].
///
/// [`Drawable`]: trait.Drawable.html
/// [`Frame`]: struct.Frame.html
fn draw(&self, frame: &mut Frame);
}
impl<'a> Drawable for dyn Fn(&mut Frame) + 'a {
fn draw(&self, frame: &mut Frame) {
self(frame)
}
}
impl<T> Drawable for &T
where
T: Drawable,
{
fn draw(&self, frame: &mut Frame) {
T::draw(self, frame)
}
}
impl<T> Drawable for &[T]
where
T: Drawable,
{
fn draw(&self, frame: &mut Frame) {
self.iter().for_each(|drawable| drawable.draw(frame));
}
}

View file

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