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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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,
{
}