use euclid::Size2D;
use crate::{event::Event, unit::Cell, Printer, View as ViewTrait};
pub struct View<'a, T, M> {
inner: Box<dyn ViewTrait<T, M> + 'a>,
}
impl<'a, T, M> View<'a, T, M> {
pub fn new<V>(other: V) -> Self
where
V: ViewTrait<T, M> + 'a,
{
Self {
inner: Box::new(other),
}
}
}
pub fn new<'a, T, M, V>(other: V) -> View<'a, T, M>
where
V: ViewTrait<T, M> + 'a,
{
View::new(other)
}
impl<T, M> ViewTrait<T, M> for View<'_, T, M> {
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>> {
self.inner.event(event, focused)
}
fn interactive(&self) -> bool {
self.inner.interactive()
}
}
pub trait ViewExt<'a, T, M> {
fn into_element(self) -> View<'a, T, M>
where
Self: ViewTrait<T, M> + Sized + 'a,
{
View::new(self)
}
}
impl<'a, T, M, V> ViewExt<'a, T, M> for V {}