embedded_layout/view_group/
object_chain.rs1use embedded_graphics::{
4 draw_target::DrawTarget, pixelcolor::PixelColor, prelude::Point, primitives::Rectangle,
5 Drawable,
6};
7
8use crate::{
9 object_chain::{Chain, ChainElement, Link},
10 prelude::RectExt,
11 view_group::ViewGroup,
12 View,
13};
14
15impl<C, V, VC> Drawable for Link<V, VC>
16where
17 C: PixelColor,
18 V: View + Drawable<Color = C>,
19 VC: View + ChainElement + Drawable<Color = C>,
20{
21 type Color = C;
22 type Output = ();
23
24 #[inline]
25 fn draw<D>(&self, display: &mut D) -> Result<(), D::Error>
26 where
27 D: DrawTarget<Color = Self::Color>,
28 {
29 self.object.draw(display)?;
30 self.parent.draw(display)?;
31
32 Ok(())
33 }
34}
35
36impl<V, VC> View for Link<V, VC>
37where
38 V: View,
39 VC: View + ChainElement,
40{
41 #[inline]
42 fn bounds(&self) -> Rectangle {
43 let bounds = self.object.bounds();
44
45 bounds.enveloping(&self.parent.bounds())
46 }
47
48 #[inline]
49 fn translate_impl(&mut self, by: Point) {
50 self.object.translate_mut(by);
51 self.parent.translate_mut(by);
52 }
53}
54
55impl<C, V> Drawable for Chain<V>
56where
57 C: PixelColor,
58 V: View + Drawable<Color = C>,
59{
60 type Color = C;
61 type Output = ();
62
63 #[inline]
64 fn draw<D>(&self, display: &mut D) -> Result<(), D::Error>
65 where
66 D: DrawTarget<Color = Self::Color>,
67 {
68 self.object.draw(display)?;
69 Ok(())
70 }
71}
72
73impl<V> View for Chain<V>
74where
75 V: View,
76{
77 #[inline]
78 fn bounds(&self) -> Rectangle {
79 self.object.bounds()
80 }
81
82 #[inline]
83 fn translate_impl(&mut self, by: Point) {
84 self.object.translate_mut(by);
85 }
86}
87
88impl<V, VC> ViewGroup for Link<V, VC>
89where
90 V: View,
91 VC: ViewGroup + View + ChainElement,
92{
93 #[inline]
94 fn len(&self) -> usize {
95 ChainElement::len(self)
96 }
97
98 #[inline]
99 fn at(&self, index: usize) -> &dyn View {
100 if index == ViewGroup::len(self) - 1 {
101 return &self.object;
102 }
103
104 self.parent.at(index)
105 }
106
107 #[inline]
108 fn at_mut(&mut self, index: usize) -> &mut dyn View {
109 if index == ViewGroup::len(self) - 1 {
110 return &mut self.object;
111 }
112
113 self.parent.at_mut(index)
114 }
115
116 #[inline]
117 fn bounds_of(&self, index: usize) -> Rectangle {
118 if index == ViewGroup::len(self) - 1 {
119 return self.object.bounds();
120 }
121
122 self.parent.bounds_of(index)
123 }
124
125 #[inline]
126 fn translate_child(&mut self, index: usize, by: Point) {
127 if index == ViewGroup::len(self) - 1 {
128 return self.object.translate_impl(by);
129 }
130
131 self.parent.translate_child(index, by)
132 }
133}
134
135impl<V> ViewGroup for Chain<V>
136where
137 V: View,
138{
139 #[inline]
140 fn len(&self) -> usize {
141 ChainElement::len(self)
142 }
143
144 #[inline]
145 fn at(&self, index: usize) -> &dyn View {
146 assert_eq!(index, 0);
147
148 &self.object
149 }
150
151 #[inline]
152 fn at_mut(&mut self, index: usize) -> &mut dyn View {
153 assert_eq!(index, 0);
154
155 &mut self.object
156 }
157
158 #[inline]
159 fn bounds_of(&self, index: usize) -> Rectangle {
160 assert_eq!(index, 0);
161
162 self.object.bounds()
163 }
164
165 #[inline]
166 fn translate_child(&mut self, index: usize, by: Point) {
167 assert_eq!(index, 0);
168
169 self.object.translate_impl(by)
170 }
171}