use std::marker::PhantomData;
use euclid::Size2D;
use crate::{
event::Event, unit::Cell, view::element::ViewExt as _, Printer,
View as ViewTrait,
};
pub struct View<V, O, F> {
inner: V,
inner_m: PhantomData<O>,
map: F,
}
impl<V, O, F> View<V, O, F> {
pub fn new<T>(view: V, map: F) -> Self
where
V: ViewTrait<T, O>,
{
Self {
inner: view,
inner_m: PhantomData,
map,
}
}
}
pub fn new<V, T, O, F>(view: V, map: F) -> View<V, O, F>
where
V: ViewTrait<T, O>,
{
View::new(view, map)
}
impl<T, M, O, V, F> ViewTrait<T, M> for View<V, O, F>
where
V: ViewTrait<T, O>,
F: Fn(O) -> M,
F: Clone + 'static,
M: 'static,
O: 'static,
{
fn draw(&self, printer: &Printer, focused: bool) {
self.inner.draw(printer, focused);
}
fn width(&self) -> Size2D<u16, Cell> {
self.inner.width()
}
fn height(&self) -> Size2D<u16, Cell> {
self.inner.height()
}
fn layout(&self, constraint: Size2D<u16, Cell>) -> Size2D<u16, Cell> {
self.inner.layout(constraint)
}
fn event(
&mut self,
event: &Event<T>,
focused: bool,
) -> Box<dyn Iterator<Item = M>> {
let f = self.map.clone();
let x = Iterator::map(self.inner.event(event, focused), move |m| f(m));
Box::new(x)
}
fn interactive(&self) -> bool {
self.inner.interactive()
}
}
pub trait ViewExt<'a, T, O, M, F>
where
F: Fn(O) -> M,
F: Clone + 'static,
M: 'static,
O: 'static,
{
fn map(self, f: F) -> super::element::View<'a, T, M>
where
Self: ViewTrait<T, O> + Sized + 'a,
{
View::new(self, f).into_element()
}
}
impl<'a, T, O, M, F, V> ViewExt<'a, T, O, M, F> for V
where
F: Fn(O) -> M,
F: Clone + 'static,
M: 'static,
O: 'static,
{
}