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
use embedded_graphics::{
    pixelcolor::Rgb888,
    prelude::{DrawTarget, PixelColor, Point},
    primitives::{Rectangle, StyledDrawable},
};
use embedded_layout::View;

use crate::{
    interaction::InteractionController,
    items::MenuLine,
    selection_indicator::{style::IndicatorStyle, SelectionIndicatorController},
    Marker, MenuItem, MenuStyle,
};

pub struct NavigationItem<'a, R: Copy> {
    title_text: &'a str,
    details: &'a str,
    return_value: R,
    marker: &'a str,
    line: MenuLine,
}

impl<R: Copy> Marker for NavigationItem<'_, R> {}

impl<R> MenuItem<R> for NavigationItem<'_, R>
where
    R: Copy,
{
    fn interact(&mut self) -> R {
        self.return_value
    }

    fn title(&self) -> &str {
        self.title_text
    }

    fn details(&self) -> &str {
        self.details
    }

    fn value(&self) -> &str {
        self.marker
    }

    fn set_style<C, S, IT, P>(&mut self, style: &MenuStyle<C, S, IT, P>)
    where
        C: PixelColor,
        S: IndicatorStyle,
        IT: InteractionController,
        P: SelectionIndicatorController,
    {
        self.line = MenuLine::new(self.marker, style);
    }
}

impl<'a, R: Copy> NavigationItem<'a, R> {
    pub fn new(title: &'a str, value: R) -> Self {
        Self {
            title_text: title,
            return_value: value,
            details: "",
            marker: "",
            line: MenuLine::empty(),
        }
    }

    pub fn with_marker(self, marker: &'a str) -> Self {
        Self { marker, ..self }
    }

    pub fn with_detail_text(self, details: &'a str) -> Self {
        Self { details, ..self }
    }
}

impl<R: Copy> View for NavigationItem<'_, R> {
    fn translate_impl(&mut self, by: Point) {
        self.line.translate_mut(by);
    }

    fn bounds(&self) -> Rectangle {
        self.line.bounds()
    }
}

impl<C, S, IT, P, R> StyledDrawable<MenuStyle<C, S, IT, P>> for NavigationItem<'_, R>
where
    C: PixelColor + From<Rgb888>,
    S: IndicatorStyle,
    IT: InteractionController,
    P: SelectionIndicatorController,
    R: Copy,
{
    type Color = C;
    type Output = ();

    fn draw_styled<D>(
        &self,
        style: &MenuStyle<C, S, IT, P>,
        display: &mut D,
    ) -> Result<Self::Output, D::Error>
    where
        D: DrawTarget<Color = Self::Color>,
    {
        self.line
            .draw_styled(self.title_text, self.marker, style, display)
    }
}