rlvgl-widgets 0.2.5

Built-in widgets for rlvgl.
Documentation
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Specialized [`ButtonMatrix`] with predefined key maps and key-output hook
//! (LPAR-13 §5.D).
//!
//! A [`Keyboard`] owns a [`ButtonMatrix`] internally and switches between
//! predefined button maps via [`set_mode`](Keyboard::set_mode).  Pressing a
//! key resolves to a [`KeyOutput`] value delivered to the caller via:
//!
//! 1. **Hook** — install a `FnMut(KeyOutput) + 'static` closure with
//!    [`set_key_output_hook`](Keyboard::set_key_output_hook).
//! 2. **Poll slot** — call [`last_key_output`](Keyboard::last_key_output) once
//!    per frame; it drains and returns the most-recently-produced output.
//!
//! Before LPAR-14 Textarea v2 the app wires the hook to any text field
//! manually (e.g. `textarea.insert_char(ch)`).
//!
//! # Key navigation (LPAR-12/LPAR-13 §5.J)
//!
//! Wire `ObjectEvent::Key` to the imperative helpers:
//!
//! ```ignore
//! keyboard.navigate_next();        // ArrowRight / Tab
//! keyboard.navigate_prev();        // ArrowLeft  / BackTab
//! keyboard.activate_selected();    // Enter
//! ```

use alloc::boxed::Box;
use alloc::string::String;
use alloc::string::ToString;
use rlvgl_core::event::Event;
use rlvgl_core::renderer::Renderer;
use rlvgl_core::widget::{Rect, Widget};

use crate::button_matrix::{BUTTON_NONE, ButtonId, ButtonMatrix};

// ── Predefined key maps ───────────────────────────────────────────────────────

/// Lowercase alphabetic map (mirrors `lv_kb_def_btnm_map_lc`).
static MAP_LOWER: &[&str] = &[
    "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "\n", "a", "s", "d", "f", "g", "h", "j", "k",
    "l", "\n", "ABC", "z", "x", "c", "v", "b", "n", "m", "", "\n", "123", " ", "",
];

/// Uppercase alphabetic map (mirrors `lv_kb_def_btnm_map_uc`).
static MAP_UPPER: &[&str] = &[
    "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "\n", "A", "S", "D", "F", "G", "H", "J", "K",
    "L", "\n", "abc", "Z", "X", "C", "V", "B", "N", "M", "", "\n", "123", " ", "",
];

/// Numeric / symbol map (mirrors `lv_kb_def_btnm_map_num`).
static MAP_NUMBER: &[&str] = &[
    "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "\n", "+", "-", "/", "*", "=", "%", "!", "?",
    "#", "<", ">", "\n", "\\", "@", "$", "(", ")", "{", "}", "[", "]", ";", ":", "'", "\n", "abc",
    " ", "", "",
];

/// Special / emoji placeholder map.
static MAP_SPECIAL: &[&str] = &[
    ":)", ":(", ";)", ":D", ":O", ":-|", ":P", "\n", "abc", " ", "", "",
];

// ── Public types ──────────────────────────────────────────────────────────────

/// Named key-map configuration for [`Keyboard`].
///
/// Registration policy: Specification Required (LPAR-13 §7).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyboardMode {
    /// Lowercase alphabetic layout.
    TextLower,
    /// Uppercase alphabetic layout.
    TextUpper,
    /// Numeric and symbol layout.
    Number,
    /// Special characters / emoji placeholder layout.
    Special,
    /// User-defined slot 1.
    User1,
    /// User-defined slot 2.
    User2,
    /// User-defined slot 3.
    User3,
    /// User-defined slot 4.
    User4,
}

/// Internal control action produced by a special key in the keyboard map.
///
/// Registration policy: Specification Required (LPAR-13 §7).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KeyboardControl {
    /// Switch to the indicated keyboard mode.
    SwitchMode(KeyboardMode),
}

/// Value produced by a key activation on a [`Keyboard`].
///
/// Registration policy: Specification Required (LPAR-13 §7).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KeyOutput {
    /// A printable character was pressed.
    Char(char),
    /// The backspace key was pressed (delete character before cursor).
    Backspace,
    /// The enter / return key was pressed.
    Enter,
    /// The tab key was pressed.
    Tab,
    /// The escape key was pressed.
    Escape,
    /// An internal control action (mode switch, etc.).
    Control(KeyboardControl),
}

/// Resolve a button label string from a predefined map to a [`KeyOutput`].
fn resolve_key(label: &str) -> Option<KeyOutput> {
    match label {
        "" => Some(KeyOutput::Backspace),
        "" => Some(KeyOutput::Enter),
        "ABC" => Some(KeyOutput::Control(KeyboardControl::SwitchMode(
            KeyboardMode::TextUpper,
        ))),
        "abc" => Some(KeyOutput::Control(KeyboardControl::SwitchMode(
            KeyboardMode::TextLower,
        ))),
        "123" => Some(KeyOutput::Control(KeyboardControl::SwitchMode(
            KeyboardMode::Number,
        ))),
        " " => Some(KeyOutput::Char(' ')),
        s if s.chars().count() == 1 => Some(KeyOutput::Char(s.chars().next().unwrap())),
        _ => None,
    }
}

// ── Keyboard ──────────────────────────────────────────────────────────────────

/// Specialized button-grid keyboard with predefined mode maps (LPAR-13 §5.D).
///
/// Owns a [`ButtonMatrix`] internally.  Mode switching replaces the inner map.
/// Key output is delivered via the hook closure and/or the poll slot.
pub struct Keyboard {
    matrix: ButtonMatrix,
    mode: KeyboardMode,
    popovers: bool,
    /// Most-recently produced key output (poll slot).
    last_output: Option<KeyOutput>,
    /// Optional callback invoked on each key activation.
    hook: Option<Box<dyn FnMut(KeyOutput)>>,
}

impl Keyboard {
    /// Create a [`Keyboard`] occupying `bounds` in [`KeyboardMode::TextLower`].
    pub fn new(bounds: Rect) -> Self {
        let mut matrix = ButtonMatrix::new(bounds);
        matrix.set_map(MAP_LOWER);
        Self {
            matrix,
            mode: KeyboardMode::TextLower,
            popovers: false,
            last_output: None,
            hook: None,
        }
    }

    // ── Mode ──────────────────────────────────────────────────────────────

    /// Set the active key map mode and update the inner [`ButtonMatrix`] map.
    pub fn set_mode(&mut self, mode: KeyboardMode) {
        self.mode = mode;
        let map: &[&str] = match mode {
            KeyboardMode::TextLower => MAP_LOWER,
            KeyboardMode::TextUpper => MAP_UPPER,
            KeyboardMode::Number => MAP_NUMBER,
            KeyboardMode::Special => MAP_SPECIAL,
            // User-defined: no built-in map; leave current matrix unchanged.
            KeyboardMode::User1
            | KeyboardMode::User2
            | KeyboardMode::User3
            | KeyboardMode::User4 => return,
        };
        self.matrix.set_map(map);
    }

    /// Return the currently active mode.
    pub fn mode(&self) -> KeyboardMode {
        self.mode
    }

    // ── Popovers ──────────────────────────────────────────────────────────

    /// Store the popover preference (forwarded to the inner matrix; v1 drawing
    /// behavior may be a no-op pending LPAR-12 §13 deferred-Safe).
    pub fn set_popovers(&mut self, enable: bool) {
        self.popovers = enable;
    }

    /// Return whether popovers are requested.
    pub fn popovers(&self) -> bool {
        self.popovers
    }

    // ── Key output hook ───────────────────────────────────────────────────

    /// Install a callback invoked on every key activation.
    ///
    /// The hook is stored as `Box<dyn FnMut(KeyOutput)>` so it is independent
    /// of any specific text-field lifetime (LPAR-13 §5.D, `no_std + alloc`).
    pub fn set_key_output_hook<F>(&mut self, hook: F)
    where
        F: FnMut(KeyOutput) + 'static,
    {
        self.hook = Some(Box::new(hook));
    }

    /// Drain and return the most-recently produced [`KeyOutput`] since the last
    /// call.  Returns `None` if no key was pressed since the last poll.
    pub fn last_key_output(&mut self) -> Option<KeyOutput> {
        self.last_output.take()
    }

    // ── Key navigation helpers (LPAR-13 §5.J) ────────────────────────────

    /// Select the next navigable button (delegates to inner [`ButtonMatrix`]).
    ///
    /// Wire to `ObjectEvent::Key(Key::ArrowRight)` or `Key::Tab`.
    pub fn navigate_next(&mut self) {
        self.matrix.navigate_next();
    }

    /// Select the previous navigable button (delegates to inner [`ButtonMatrix`]).
    ///
    /// Wire to `ObjectEvent::Key(Key::ArrowLeft)` or `Key::BackTab`.
    pub fn navigate_prev(&mut self) {
        self.matrix.navigate_prev();
    }

    /// Activate the currently selected button (delegates to inner
    /// [`ButtonMatrix`]) and emit any resulting [`KeyOutput`].
    ///
    /// Wire to `ObjectEvent::Key(Key::Enter)`.
    pub fn activate_selected(&mut self) {
        let id = self.matrix.selected_button();
        if id == BUTTON_NONE {
            return;
        }
        // Snapshot label before mutable call
        let label_owned: String = self.matrix.button_text(id).unwrap_or_default().to_string();
        self.matrix.activate_selected();
        self.emit_for_label(&label_owned);
    }

    // ── Internal ──────────────────────────────────────────────────────────

    /// Emit a [`KeyOutput`] for the given label string, handling mode-switch
    /// side-effects inline.
    fn emit_for_label(&mut self, label: &str) {
        let output = match resolve_key(label) {
            Some(out) => out,
            None => return,
        };

        // Apply mode-switch side-effect before delivering to caller
        if let KeyOutput::Control(KeyboardControl::SwitchMode(new_mode)) = &output {
            let m = *new_mode;
            self.set_mode(m);
        }

        // Deliver via poll slot
        self.last_output = Some(output.clone());

        // Deliver via hook (takes ownership of output)
        if let Some(hook) = &mut self.hook {
            hook(output);
        }
    }

    /// Return a reference to the inner [`ButtonMatrix`] for style customization.
    pub fn matrix(&self) -> &ButtonMatrix {
        &self.matrix
    }

    /// Return a mutable reference to the inner [`ButtonMatrix`].
    pub fn matrix_mut(&mut self) -> &mut ButtonMatrix {
        &mut self.matrix
    }

    /// Return the currently selected button id in the inner matrix.
    pub fn selected_button(&self) -> ButtonId {
        self.matrix.selected_button()
    }
}

impl Widget for Keyboard {
    fn bounds(&self) -> Rect {
        self.matrix.bounds()
    }

    fn set_bounds(&mut self, bounds: Rect) {
        self.matrix.set_bounds(bounds);
    }

    fn draw(&self, renderer: &mut dyn Renderer) {
        self.matrix.draw(renderer);
    }

    fn handle_event(&mut self, event: &Event) -> bool {
        let consumed = self.matrix.handle_event(event);
        if consumed && matches!(event, Event::PressRelease { .. }) {
            // The ButtonMatrix activated a button on PressRelease.
            // Emit the corresponding KeyOutput for the now-selected button.
            let activated_id = self.matrix.selected_button();
            if activated_id != BUTTON_NONE {
                let label_owned: String = self
                    .matrix
                    .button_text(activated_id)
                    .unwrap_or_default()
                    .to_string();
                self.emit_for_label(&label_owned);
            }
        }
        consumed
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rlvgl_core::widget::{Color, Rect};

    fn rect(x: i32, y: i32, w: i32, h: i32) -> Rect {
        Rect {
            x,
            y,
            width: w,
            height: h,
        }
    }

    struct NullRenderer;
    impl rlvgl_core::renderer::Renderer for NullRenderer {
        fn fill_rect(&mut self, _: Rect, _: Color) {}
        fn draw_text(&mut self, _: (i32, i32), _: &str, _: Color) {}
    }

    #[test]
    fn new_starts_in_lower_mode() {
        let kb = Keyboard::new(rect(0, 0, 320, 160));
        assert_eq!(kb.mode(), KeyboardMode::TextLower);
    }

    #[test]
    fn set_mode_upper_changes_map() {
        let mut kb = Keyboard::new(rect(0, 0, 320, 160));
        kb.set_mode(KeyboardMode::TextUpper);
        assert_eq!(kb.mode(), KeyboardMode::TextUpper);
        // First button in upper map should be 'Q'
        assert_eq!(kb.matrix().button_text(ButtonId(0)), Some("Q"));
    }

    #[test]
    fn set_mode_number_changes_map() {
        let mut kb = Keyboard::new(rect(0, 0, 320, 160));
        kb.set_mode(KeyboardMode::Number);
        assert_eq!(kb.mode(), KeyboardMode::Number);
        assert_eq!(kb.matrix().button_text(ButtonId(0)), Some("1"));
    }

    #[test]
    fn set_mode_special_changes_map() {
        let mut kb = Keyboard::new(rect(0, 0, 320, 160));
        kb.set_mode(KeyboardMode::Special);
        assert_eq!(kb.mode(), KeyboardMode::Special);
    }

    #[test]
    fn activate_char_key_emits_char_output() {
        let mut kb = Keyboard::new(rect(0, 0, 320, 160));
        // Navigate to the 'q' key (button 0 in lower mode)
        kb.matrix_mut().set_selected_button(ButtonId(0));
        kb.activate_selected();
        assert_eq!(kb.last_key_output(), Some(KeyOutput::Char('q')));
        // Poll slot is drained
        assert_eq!(kb.last_key_output(), None);
    }

    #[test]
    fn activate_backspace_emits_backspace() {
        let mut kb = Keyboard::new(rect(0, 0, 320, 160));
        // Find backspace button id by scanning
        let count = kb.matrix().button_count();
        let mut bs_id = None;
        for i in 0..count {
            if kb.matrix().button_text(ButtonId(i as u16)) == Some("") {
                bs_id = Some(ButtonId(i as u16));
                break;
            }
        }
        let bs_id = bs_id.expect("backspace button not found");
        kb.matrix_mut().set_selected_button(bs_id);
        kb.activate_selected();
        assert_eq!(kb.last_key_output(), Some(KeyOutput::Backspace));
    }

    #[test]
    fn activate_enter_emits_enter() {
        let mut kb = Keyboard::new(rect(0, 0, 320, 160));
        let count = kb.matrix().button_count();
        let mut enter_id = None;
        for i in 0..count {
            if kb.matrix().button_text(ButtonId(i as u16)) == Some("") {
                enter_id = Some(ButtonId(i as u16));
                break;
            }
        }
        let enter_id = enter_id.expect("enter button not found");
        kb.matrix_mut().set_selected_button(enter_id);
        kb.activate_selected();
        assert_eq!(kb.last_key_output(), Some(KeyOutput::Enter));
    }

    #[test]
    fn mode_switch_key_changes_mode() {
        let mut kb = Keyboard::new(rect(0, 0, 320, 160));
        // Find the ABC (switch to upper) button in lower mode
        let count = kb.matrix().button_count();
        let mut abc_id = None;
        for i in 0..count {
            if kb.matrix().button_text(ButtonId(i as u16)) == Some("ABC") {
                abc_id = Some(ButtonId(i as u16));
                break;
            }
        }
        let abc_id = abc_id.expect("ABC mode-switch button not found");
        kb.matrix_mut().set_selected_button(abc_id);
        kb.activate_selected();
        assert_eq!(kb.mode(), KeyboardMode::TextUpper);
    }

    #[test]
    fn hook_is_called_on_key_activation() {
        use alloc::rc::Rc;
        use core::cell::Cell;

        let called = Rc::new(Cell::new(false));
        let called2 = called.clone();

        let mut kb = Keyboard::new(rect(0, 0, 320, 160));
        kb.set_key_output_hook(move |_out| {
            called2.set(true);
        });
        kb.matrix_mut().set_selected_button(ButtonId(0));
        kb.activate_selected();
        assert!(called.get());
    }

    #[test]
    fn navigate_next_and_prev_work() {
        let mut kb = Keyboard::new(rect(0, 0, 320, 160));
        kb.navigate_next();
        assert_eq!(kb.selected_button(), ButtonId(0));
        kb.navigate_next();
        assert_eq!(kb.selected_button(), ButtonId(1));
        kb.navigate_prev();
        assert_eq!(kb.selected_button(), ButtonId(0));
    }

    #[test]
    fn set_bounds_propagates_to_matrix() {
        let mut kb = Keyboard::new(rect(0, 0, 320, 160));
        kb.set_bounds(rect(10, 20, 400, 200));
        assert_eq!(kb.bounds(), rect(10, 20, 400, 200));
    }

    #[test]
    fn draw_does_not_panic() {
        let kb = Keyboard::new(rect(0, 0, 320, 160));
        let mut r = NullRenderer;
        kb.draw(&mut r);
    }

    #[test]
    fn popovers_stored() {
        let mut kb = Keyboard::new(rect(0, 0, 320, 160));
        kb.set_popovers(true);
        assert!(kb.popovers());
        kb.set_popovers(false);
        assert!(!kb.popovers());
    }
}