embedded_menu/items/
menu_item.rs

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use embedded_graphics::{
    mono_font::MonoTextStyle,
    pixelcolor::BinaryColor,
    prelude::{DrawTarget, Point},
    primitives::Rectangle,
};
use embedded_layout::View;

use crate::items::{Marker, MenuLine, MenuListItem};

pub trait SelectValue: Sized + Clone + PartialEq {
    /// Transforms the value on interaction
    fn next(&mut self) {}

    /// Returns a displayable marker for the value
    fn marker(&self) -> &str;
}

impl SelectValue for bool {
    fn next(&mut self) {
        *self = !*self;
    }

    fn marker(&self) -> &str {
        match *self {
            // true => "O",
            // false => "O\r+\r#", // this only works for certain small fonts, unfortunately
            false => "[ ]",
            true => "[X]",
        }
    }
}

impl SelectValue for &str {
    fn marker(&self) -> &str {
        self
    }
}

impl SelectValue for () {
    fn marker(&self) -> &str {
        ""
    }
}

pub struct MenuItem<T, R, S, const SELECTABLE: bool>
where
    T: AsRef<str>,
    S: SelectValue,
{
    title_text: T,
    convert: fn(S) -> R,
    value: S,
    line: MenuLine,
}

impl<T, S> MenuItem<T, (), S, true>
where
    T: AsRef<str>,
    S: SelectValue,
{
    pub fn new(title_text: T, value: S) -> Self {
        Self {
            title_text,
            value,
            convert: |_| (),
            line: MenuLine::empty(),
        }
    }
}

impl<T, R, S, const SELECTABLE: bool> MenuItem<T, R, S, SELECTABLE>
where
    T: AsRef<str>,
    S: SelectValue,
{
    pub fn with_value_converter<R2>(self, convert: fn(S) -> R2) -> MenuItem<T, R2, S, SELECTABLE> {
        MenuItem {
            convert,
            title_text: self.title_text,
            value: self.value,
            line: self.line,
        }
    }

    /// Make the item selectable or not
    pub fn selectable<const SELECTABLE2: bool>(self) -> MenuItem<T, R, S, SELECTABLE2> {
        MenuItem {
            convert: self.convert,
            title_text: self.title_text,
            value: self.value,
            line: self.line,
        }
    }
}

impl<T, R, S, const SELECTABLE: bool> Marker for MenuItem<T, R, S, SELECTABLE>
where
    T: AsRef<str>,
    S: SelectValue,
{
}

impl<T, R, S, const SELECTABLE: bool> MenuListItem<R> for MenuItem<T, R, S, SELECTABLE>
where
    T: AsRef<str>,
    S: SelectValue,
{
    fn value_of(&self) -> R {
        (self.convert)(self.value.clone())
    }

    fn interact(&mut self) -> R {
        self.value.next();
        self.value_of()
    }

    fn selectable(&self) -> bool {
        SELECTABLE
    }

    fn set_style(&mut self, text_style: &MonoTextStyle<'_, BinaryColor>) {
        let mut current = self.value.clone();
        let mut longest = self.value.clone();

        let mut longest_len = longest.marker().len();

        loop {
            current.next();
            if current == self.value {
                break;
            }

            if current.marker().len() > longest_len {
                longest = current.clone();
                longest_len = longest.marker().len();
            }
        }

        self.line = MenuLine::new(longest.marker(), text_style);
    }

    fn draw_styled<D>(
        &self,
        text_style: &MonoTextStyle<'static, BinaryColor>,
        display: &mut D,
    ) -> Result<(), D::Error>
    where
        D: DrawTarget<Color = BinaryColor>,
    {
        self.line.draw_styled(
            self.title_text.as_ref(),
            self.value.marker(),
            text_style,
            display,
        )
    }
}

impl<T, R, S, const SELECTABLE: bool> View for MenuItem<T, R, S, SELECTABLE>
where
    T: AsRef<str>,
    S: SelectValue,
{
    fn translate_impl(&mut self, by: Point) {
        self.line.translate_mut(by);
    }

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

#[cfg(test)]
mod test {
    #[test]
    fn interaction_selects_next_value_and_returns_converted() {
        use super::*;
        use crate::items::MenuListItem;

        let mut item = MenuItem::new("title", false).with_value_converter(|b| b as u8);

        assert_eq!(item.value_of(), 0);

        assert_eq!(item.interact(), 1);
        assert_eq!(item.value_of(), 1);

        assert_eq!(item.interact(), 0);
        assert_eq!(item.value_of(), 0);
    }
}