Skip to main content

embedded_list/
list.rs

1use core::marker::PhantomData;
2
3use embedded_graphics::{
4    prelude::{DrawTarget, PixelColor, Point},
5    primitives::Rectangle,
6    Drawable,
7};
8
9use crate::ListSource;
10
11pub enum ListError {
12    OutOfBounds,
13}
14
15pub struct List<'a, C: PixelColor, S: ListSource> {
16    rect: Rectangle,
17    current: u8,
18    scroll_offset: i32,
19    source: &'a S,
20    _phantom: PhantomData<C>,
21}
22
23impl<'a, C: PixelColor, S: ListSource> List<'a, C, S> {
24    pub fn new(rect: Rectangle, source: &'a S) -> Self {
25        Self {
26            rect,
27            current: 0,
28            scroll_offset: 0,
29            source,
30            _phantom: PhantomData,
31        }
32    }
33}
34
35impl<'a, C: PixelColor, S: ListSource> Drawable for List<'a, C, S> {
36    type Color = S::C;
37    type Output = ();
38
39    fn draw<D: DrawTarget<Color = S::C>>(&self, display: &mut D) -> Result<(), D::Error> {
40        let mut y_offset = self.rect.top_left.y - self.scroll_offset;
41        let bottom_edge = self.rect.bottom_right().unwrap_or(self.rect.top_left).y;
42
43        for i in 0..self.source.total_count() {
44            let item_height = self.source.height_for_index(i);
45            let item_top = y_offset;
46            let item_bottom = y_offset + item_height;
47
48            // Check if the item is within the visible area
49            if item_bottom > self.rect.top_left.y && item_top < bottom_edge {
50                self.source.draw(
51                    i,
52                    self.current == i,
53                    display,
54                    Point::new(self.rect.top_left.x, y_offset),
55                )?;
56            }
57
58            y_offset += item_height;
59        }
60
61        Ok(())
62    }
63}
64
65impl<'a, C: PixelColor, S: ListSource> List<'a, C, S> {
66    pub fn set_current(&mut self, index: u8) {
67        if index >= self.source.total_count() {
68            self.current = 0;
69        } else {
70            self.current = index;
71        }
72
73        // Update scroll_offset to keep current item visible
74        self.update_scroll_offset();
75    }
76
77    fn update_scroll_offset(&mut self) {
78        let mut total_height = 0;
79        let mut current_item_top = 0;
80        let mut current_item_bottom = 0;
81
82        for i in 0..self.source.total_count() {
83            let item_height = self.source.height_for_index(i);
84
85            if i == self.current {
86                current_item_top = total_height;
87                current_item_bottom = total_height + item_height;
88                break;
89            }
90
91            total_height += item_height;
92        }
93
94        let viewport_height = self.rect.size.height as i32;
95        let viewport_top = self.scroll_offset;
96        let viewport_bottom = self.scroll_offset + viewport_height;
97
98        // Adjust scroll to bring the current item into view
99        if current_item_top < viewport_top {
100            self.scroll_offset = current_item_top;
101        } else if current_item_bottom > viewport_bottom {
102            self.scroll_offset = current_item_bottom - viewport_height;
103        }
104    }
105
106    pub fn current(&self) -> u8 {
107        self.current
108    }
109}