embedded_menu/
builder.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use crate::{
    collection::{MenuItemCollection, MenuItems},
    interaction::{InputAdapterSource, InputState},
    items::{menu_item::SelectValue, MenuItem, MenuListItem},
    selection_indicator::{style::IndicatorStyle, SelectionIndicatorController},
    theme::Theme,
    Menu, MenuState, MenuStyle, NoItems,
};
use core::marker::PhantomData;
use embedded_layout::{
    layout::linear::LinearLayout,
    object_chain::ChainElement,
    prelude::*,
    view_group::{EmptyViewGroup, ViewGroup},
};

pub struct MenuBuilder<T, IT, LL, R, P, S, C>
where
    T: AsRef<str>,
    IT: InputAdapterSource<R>,
    S: IndicatorStyle,
    P: SelectionIndicatorController,
    C: Theme,
{
    title: T,
    items: LL,
    style: MenuStyle<S, IT, P, R, C>,
}

impl<T, R, S, IT, P, C> MenuBuilder<T, IT, NoItems, R, P, S, C>
where
    T: AsRef<str>,
    S: IndicatorStyle,
    IT: InputAdapterSource<R>,
    P: SelectionIndicatorController,
    C: Theme,
{
    /// Creates a new menu builder with the given title and style.
    pub const fn new(title: T, style: MenuStyle<S, IT, P, R, C>) -> Self {
        Self {
            title,
            items: NoItems,
            style,
        }
    }
}

impl<T, IT, R, P, S, C> MenuBuilder<T, IT, NoItems, R, P, S, C>
where
    T: AsRef<str>,
    IT: InputAdapterSource<R>,
    P: SelectionIndicatorController,
    S: IndicatorStyle,
    C: Theme,
{
    /// Append a non-selectable menu item to the menu with the given title.
    pub fn add_section_title<T2: AsRef<str>>(
        self,
        title: T2,
    ) -> MenuBuilder<T, IT, Chain<MenuItem<T2, R, (), false>>, R, P, S, C> {
        self.add_menu_item(
            MenuItem::new(title, ())
                .with_value_converter(|_| unreachable!())
                .selectable::<false>(),
        )
    }

    /// Append an interactable menu item to the menu.
    ///
    /// The menu item will initially have the given value. Upon interaction, the value will be
    /// updated according to the value type's `next` method. The menu will use the callback
    /// passed in the third argument to convert the value to the global return type.
    pub fn add_item<T2: AsRef<str>, V: SelectValue>(
        self,
        title: T2,
        value: V,
        converter: fn(V) -> R,
    ) -> MenuBuilder<T, IT, Chain<MenuItem<T2, R, V, true>>, R, P, S, C> {
        self.add_menu_item(MenuItem::new(title, value).with_value_converter(converter))
    }

    /// Append an arbitrary [`MenuListItem`] implementation to the menu.
    pub fn add_menu_item<I: MenuListItem<R>>(
        self,
        mut item: I,
    ) -> MenuBuilder<T, IT, Chain<I>, R, P, S, C> {
        item.set_style(&self.style.text_style());

        MenuBuilder {
            title: self.title,
            items: Chain::new(item),
            style: self.style,
        }
    }

    /// Append multiple arbitrary [`MenuListItem`] implementations to the menu.
    pub fn add_menu_items<I, IC>(
        self,
        mut items: IC,
    ) -> MenuBuilder<T, IT, Chain<MenuItems<IC, I, R>>, R, P, S, C>
    where
        I: MenuListItem<R>,
        IC: AsRef<[I]> + AsMut<[I]>,
    {
        items
            .as_mut()
            .iter_mut()
            .for_each(|i| i.set_style(&self.style.text_style()));

        MenuBuilder {
            title: self.title,
            items: Chain::new(MenuItems::new(items)),
            style: self.style,
        }
    }
}

impl<T, IT, CE, R, P, S, C> MenuBuilder<T, IT, CE, R, P, S, C>
where
    T: AsRef<str>,
    IT: InputAdapterSource<R>,
    CE: MenuItemCollection<R> + ChainElement,
    P: SelectionIndicatorController,
    S: IndicatorStyle,
    C: Theme,
{
    /// Append a non-selectable menu item to the menu with the given title.
    pub fn add_section_title<T2: AsRef<str>>(
        self,
        title: T2,
    ) -> MenuBuilder<T, IT, Link<MenuItem<T2, R, (), false>, CE>, R, P, S, C> {
        self.add_menu_item(
            MenuItem::new(title, ())
                .with_value_converter(|_| unreachable!())
                .selectable::<false>(),
        )
    }

    /// Append an interactable menu item to the menu.
    ///
    /// The menu item will initially have the given value. Upon interaction, the value will be
    /// updated according to the value type's `next` method. The menu will use the callback
    /// passed in the third argument to convert the value to the global return type.
    pub fn add_item<T2: AsRef<str>, V: SelectValue>(
        self,
        title: T2,
        value: V,
        converter: fn(V) -> R,
    ) -> MenuBuilder<T, IT, Link<MenuItem<T2, R, V, true>, CE>, R, P, S, C> {
        self.add_menu_item(MenuItem::new(title, value).with_value_converter(converter))
    }

    /// Append an arbitrary [`MenuListItem`] implementation to the menu.
    pub fn add_menu_item<I: MenuListItem<R>>(
        self,
        mut item: I,
    ) -> MenuBuilder<T, IT, Link<I, CE>, R, P, S, C> {
        item.set_style(&self.style.text_style());

        MenuBuilder {
            title: self.title,
            items: Link {
                parent: self.items,
                object: item,
            },
            style: self.style,
        }
    }

    /// Append multiple arbitrary [`MenuListItem`] implementations to the menu.
    pub fn add_menu_items<I, IC>(
        self,
        mut items: IC,
    ) -> MenuBuilder<T, IT, Link<MenuItems<IC, I, R>, CE>, R, P, S, C>
    where
        I: MenuListItem<R>,
        IC: AsRef<[I]> + AsMut<[I]>,
    {
        items
            .as_mut()
            .iter_mut()
            .for_each(|i| i.set_style(&self.style.text_style()));

        MenuBuilder {
            title: self.title,
            items: Link {
                parent: self.items,
                object: MenuItems::new(items),
            },
            style: self.style,
        }
    }
}

impl<T, IT, VG, R, P, S, C> MenuBuilder<T, IT, VG, R, P, S, C>
where
    T: AsRef<str>,
    IT: InputAdapterSource<R>,
    VG: ViewGroup + MenuItemCollection<R>,
    P: SelectionIndicatorController,
    S: IndicatorStyle,
    C: Theme,
{
    /// Builds the menu and initializes it to a default state.
    pub fn build(self) -> Menu<T, IT, VG, R, P, S, C> {
        self.build_with_state(MenuState {
            selected: 0,
            list_offset: 0,
            interaction_state: Default::default(),
            indicator_state: Default::default(),
            last_input_state: InputState::Idle,
        })
    }

    /// Builds the menu, assigning to it the given state.
    pub fn build_with_state(
        mut self,
        mut state: MenuState<IT::InputAdapter, P, S>,
    ) -> Menu<T, IT, VG, R, P, S, C> {
        // We have less menu items than before. Avoid crashing.
        let max_idx = self.items.count().saturating_sub(1);

        LinearLayout::vertical(EmptyViewGroup).arrange_view_group(&mut self.items);

        state.set_selected_item(state.selected, &self.items, &self.style);
        if max_idx < state.selected {
            self.style
                .indicator
                .jump_to_target(&mut state.indicator_state);
        }

        Menu {
            state,
            _return_type: PhantomData,
            title: self.title,
            items: self.items,
            style: self.style,
        }
    }
}