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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use euclid::{Rect, Size2D};
use crate::{event::Event, unit::Cell, Printer, View as ViewTrait};
pub enum Alignment {
Start,
Middle,
End,
}
pub struct View<V> {
inner: V,
v: Alignment,
h: Alignment,
}
impl<V> View<V> {
pub fn new(view: V) -> Self {
Self {
inner: view,
v: Alignment::Middle,
h: Alignment::Middle,
}
}
pub fn h_align(mut self, alignment: Alignment) -> Self {
self.h = alignment;
self
}
pub fn v_align(mut self, alignment: Alignment) -> Self {
self.v = alignment;
self
}
}
pub fn new<V>(view: V) -> View<V> {
View::new(view)
}
impl<T, M, V> ViewTrait<T, M> for View<V>
where
V: ViewTrait<T, M>,
{
fn draw(&self, printer: &Printer, focused: bool) {
let p_size = printer.size();
let inner_size =
self.inner.layout((p_size.width, p_size.height).into());
let origin = {
let x = match self.h {
Alignment::Start => 0,
Alignment::Middle => {
(p_size.width / 2).saturating_sub(inner_size.width / 2)
}
Alignment::End => p_size.width.saturating_sub(inner_size.width),
};
let y = match self.v {
Alignment::Start => 0,
Alignment::Middle => {
(p_size.height / 2).saturating_sub(inner_size.height / 2)
}
Alignment::End => {
p_size.height.saturating_sub(inner_size.height)
}
};
(x, y)
};
let sub =
printer.to_sub_area(Rect::new(origin.into(), inner_size)).unwrap();
self.inner.draw(&sub, 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 {
fn align(self) -> View<Self>
where
Self: Sized,
{
View::new(self)
}
}
impl<V> ViewExt for V {}