1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Erase composition details of a UI element

use euclid::Size2D;

use crate::{event::Event, unit::Cell, Printer, View as ViewTrait};

/// The view itself
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),
        }
    }
}

/// Shorthand for [`View::new()`]
///
/// [`View::new()`]: View::new
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 {}