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
132
133
use embedded_graphics::{
draw_target::DrawTarget, pixelcolor::PixelColor, prelude::Point, primitives::Rectangle,
Drawable,
};
use crate::{
object_chain::{Chain, ChainElement, Link},
prelude::RectExt,
view_group::ViewGroup,
View,
};
impl<'a, C, V, VC> Drawable for Link<V, VC>
where
C: PixelColor,
V: View + Drawable<Color = C>,
VC: View + ChainElement + Drawable<Color = C>,
{
type Color = C;
type Output = ();
#[inline]
fn draw<D>(&self, display: &mut D) -> Result<(), D::Error>
where
D: DrawTarget<Color = Self::Color>,
{
self.object.draw(display)?;
self.parent.draw(display)?;
Ok(())
}
}
impl<V, VC> View for Link<V, VC>
where
V: View,
VC: View + ChainElement,
{
#[inline]
fn bounds(&self) -> Rectangle {
let bounds = self.object.bounds();
bounds.enveloping(&self.parent.bounds())
}
#[inline]
fn translate_impl(&mut self, by: Point) {
self.object.translate_mut(by);
self.parent.translate_mut(by);
}
}
impl<'a, C, V> Drawable for Chain<V>
where
C: PixelColor,
V: View + Drawable<Color = C>,
{
type Color = C;
type Output = ();
#[inline]
fn draw<D>(&self, display: &mut D) -> Result<(), D::Error>
where
D: DrawTarget<Color = Self::Color>,
{
self.object.draw(display)?;
Ok(())
}
}
impl<V> View for Chain<V>
where
V: View,
{
#[inline]
fn bounds(&self) -> Rectangle {
self.object.bounds()
}
#[inline]
fn translate_impl(&mut self, by: Point) {
self.object.translate_mut(by);
}
}
impl<V, VC> ViewGroup for Link<V, VC>
where
V: View,
VC: ViewGroup + View + ChainElement,
{
fn len(&self) -> usize {
ChainElement::len(self) as usize
}
fn at(&self, index: usize) -> &dyn View {
if index == ViewGroup::len(self) - 1 {
return &self.object;
}
return self.parent.at(index);
}
fn at_mut(&mut self, index: usize) -> &mut dyn View {
if index == ViewGroup::len(self) - 1 {
return &mut self.object;
}
return self.parent.at_mut(index);
}
}
impl<V> ViewGroup for Chain<V>
where
V: View,
{
fn len(&self) -> usize {
ChainElement::len(self) as usize
}
fn at(&self, index: usize) -> &dyn View {
assert!(index == 0);
return &self.object;
}
fn at_mut(&mut self, index: usize) -> &mut dyn View {
assert!(index == 0);
return &mut self.object;
}
}