agg-gui 0.4.0

Immediate-mode Rust GUI library with AGG rendering, Y-up layout, widgets, text, SVG, and native/WASM adapters
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! Keyboard layouts — declarative tables of rows / keys per layer.
//!
//! The layer set mirrors Google's Gboard (see `docs/Android Keyboard/`
//! screenshots): a letters page with a dedicated number row, a `?123`
//! symbols page, a `=\<` extended-symbols page, and a phone-style
//! number pad for numeric fields. Adding a new layer (e.g. a French
//! AZERTY, an emoji picker) is a data change here: define a new
//! [`Layer`] variant and return a `Layout` from [`Layout::for_layer`].
//! The painter ([`super::paint`]) and hit-tester don't change.

use crate::draw_ctx::DrawCtx;
use crate::geometry::Rect;

use super::key::{KeyAction, KeyCap, KeyGlyph, PaintedKey};
use super::paint::paint_key;
use super::style::Style;

/// Which layer of the keyboard is currently visible.
///
/// "Shifted" is a one-shot upper-case mode (single tap of Shift;
/// double-tap engages caps lock). The other layers follow Gboard's
/// paging: `?123` opens [`Layer::Symbols`], its `=\<` key opens
/// [`Layer::SymbolsShift`], and the `1234` key (or a numeric input
/// field) opens [`Layer::NumPad`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Layer {
    Letters,
    Shifted,
    /// Gboard's `?123` page — digits plus common symbols.
    Symbols,
    /// Gboard's `=\<` page — math / currency / bracket symbols.
    SymbolsShift,
    /// Phone-style digit grid with an operator strip; the layer numeric
    /// text fields open on.
    NumPad,
}

/// Description of one key in a row — width is expressed in
/// "letter-widths". A standard letter is 1.0; Shift / Backspace are
/// 1.5; the spacebar is wide (4.0 on Gboard).
#[derive(Debug, Clone)]
pub(super) struct KeySpec {
    pub(super) width_units: f64,
    pub(super) cap: KeyCap,
    pub(super) action: KeyAction,
    pub(super) kind: KeyKind,
    /// Fully-rounded ends (Gboard draws the corner keys — `?123`,
    /// `ABC`, enter — as pills / circles).
    pub(super) pill: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum KeyKind {
    /// Letter / digit / symbol — uses `key_face_*` style tokens.
    Letter,
    /// Shift / mode-switch / backspace / punctuation accents — uses
    /// `util_*` tokens (the light-blue keys on Gboard).
    Utility,
    /// Enter / search key — uses `return_*` tokens.
    Return,
    /// Invisible filler used to center short rows (Gboard's home row).
    /// Never painted, never hit-testable.
    Spacer,
}

/// A laid-out layer, ready to paint. Captured so the paint and the
/// hit-test layer-aware logic share a single source of truth.
pub struct Layout {
    rows: Vec<Vec<KeySpec>>,
}

impl Layout {
    pub fn for_layer(layer: Layer) -> Self {
        match layer {
            Layer::Letters => letters_layer(false),
            Layer::Shifted => letters_layer(true),
            Layer::Symbols => symbols_layer(),
            Layer::SymbolsShift => symbols_shift_layer(),
            Layer::NumPad => numpad_layer(),
        }
    }

    /// Compute the panel height required to render this layout at the
    /// given viewport width, accounting for vertical padding and row
    /// gaps.
    pub fn compute_panel_height(&self, _viewport_width: f64, style: &Style) -> f64 {
        let rows = self.rows.len() as f64;
        style.panel_padding_top
            + style.panel_padding_bottom
            + rows * style.row_height
            + (rows - 1.0).max(0.0) * style.key_v_gap
    }

    /// Paint every key and return the on-screen hit rects (used by the
    /// tap dispatcher).
    pub fn paint(
        &self,
        ctx: &mut dyn DrawCtx,
        panel: Rect,
        style: &Style,
        active_layer: Layer,
    ) -> Vec<PaintedKey> {
        let mut painted = Vec::with_capacity(self.rows.iter().map(|r| r.len()).sum());

        let inner_x = panel.x + style.panel_padding_horizontal;
        let inner_w = panel.width - 2.0 * style.panel_padding_horizontal;

        // Rows paint top-to-bottom visually. Y-up means the top of the
        // panel is at panel.y + panel.height; descend by row_height +
        // gap per row.
        let mut row_top_y = panel.y + panel.height - style.panel_padding_top;

        for (row_index, row) in self.rows.iter().enumerate() {
            let row_bottom_y = row_top_y - style.row_height;
            let total_units: f64 = row.iter().map(|k| k.width_units).sum();
            let total_gaps = (row.len() as f64 - 1.0).max(0.0) * style.key_h_gap;
            let key_unit_width = (inner_w - total_gaps) / total_units.max(0.001);

            let mut cursor_x = inner_x;
            for spec in row.iter() {
                let kw = spec.width_units * key_unit_width;
                if spec.kind == KeyKind::Spacer {
                    cursor_x += kw + style.key_h_gap;
                    continue;
                }
                let rect = Rect::new(cursor_x, row_bottom_y, kw, style.row_height);

                let pressed = false; // pressed visuals come from hover state, painted later
                paint_key(ctx, rect, spec, pressed, style, active_layer);

                painted.push(PaintedKey {
                    rect,
                    action: spec.action,
                    cap: spec.cap.clone(),
                });
                cursor_x += kw + style.key_h_gap;
            }

            if row_index + 1 < self.rows.len() {
                row_top_y = row_bottom_y - style.key_v_gap;
            }
        }

        painted
    }
}

// ---------------------------------------------------------------------------
// Key constructors
// ---------------------------------------------------------------------------

fn key(c: char) -> KeySpec {
    KeySpec {
        width_units: 1.0,
        cap: KeyCap::Text(c.to_string()),
        action: KeyAction::Char(c),
        kind: KeyKind::Letter,
        pill: false,
    }
}

fn wide_key(c: char, width_units: f64) -> KeySpec {
    KeySpec {
        width_units,
        ..key(c)
    }
}

/// A character key with the light-blue utility face (Gboard paints the
/// `,` / `.` keys and the numpad operator strip this way).
fn util_char(c: char, width_units: f64) -> KeySpec {
    KeySpec {
        width_units,
        kind: KeyKind::Utility,
        ..key(c)
    }
}

fn util_text(label: &str, action: KeyAction, width_units: f64) -> KeySpec {
    KeySpec {
        width_units,
        cap: KeyCap::Text(label.to_string()),
        action,
        kind: KeyKind::Utility,
        pill: false,
    }
}

fn util_glyph(glyph: KeyGlyph, action: KeyAction, width_units: f64) -> KeySpec {
    KeySpec {
        width_units,
        cap: KeyCap::Glyph(glyph),
        action,
        kind: KeyKind::Utility,
        pill: false,
    }
}

fn spacer(width_units: f64) -> KeySpec {
    KeySpec {
        width_units,
        cap: KeyCap::Text(String::new()),
        action: KeyAction::Dismiss, // never dispatched — spacers aren't hit-testable
        kind: KeyKind::Spacer,
        pill: false,
    }
}

/// Bottom-corner mode key (`?123` / `ABC`) — pill-shaped like Gboard's.
fn mode_pill(label: &str, target: Layer) -> KeySpec {
    KeySpec {
        pill: true,
        ..util_text(label, KeyAction::Switch(target), 1.5)
    }
}

fn space_bar(width_units: f64) -> KeySpec {
    KeySpec {
        width_units,
        // Gboard's spacebar is blank (no "space" label).
        cap: KeyCap::Text(String::new()),
        action: KeyAction::Space,
        kind: KeyKind::Letter,
        pill: false,
    }
}

fn enter_key() -> KeySpec {
    KeySpec {
        width_units: 1.5,
        cap: KeyCap::Glyph(KeyGlyph::Return),
        action: KeyAction::Enter,
        kind: KeyKind::Return,
        pill: true,
    }
}

fn backspace(width_units: f64) -> KeySpec {
    util_glyph(KeyGlyph::Backspace, KeyAction::Backspace, width_units)
}

// ---------------------------------------------------------------------------
// Layer definitions
// ---------------------------------------------------------------------------

fn letters_layer(shifted: bool) -> Layout {
    let case = |lower: char, upper: char| if shifted { upper } else { lower };

    let row_keys = |letters: &[(char, char)]| -> Vec<KeySpec> {
        letters.iter().map(|(lo, up)| key(case(*lo, *up))).collect()
    };

    let mut rows: Vec<Vec<KeySpec>> = Vec::with_capacity(5);

    // Gboard shows a dedicated number row above the letters.
    rows.push("1234567890".chars().map(key).collect());

    rows.push(row_keys(&[
        ('q', 'Q'),
        ('w', 'W'),
        ('e', 'E'),
        ('r', 'R'),
        ('t', 'T'),
        ('y', 'Y'),
        ('u', 'U'),
        ('i', 'I'),
        ('o', 'O'),
        ('p', 'P'),
    ]));

    // Home row: 9 letters centered with half-key spacers so the keys
    // stay the same width as row 1 (Gboard behavior) instead of
    // stretching to fill.
    let mut row_home: Vec<KeySpec> = Vec::with_capacity(11);
    row_home.push(spacer(0.5));
    row_home.extend(row_keys(&[
        ('a', 'A'),
        ('s', 'S'),
        ('d', 'D'),
        ('f', 'F'),
        ('g', 'G'),
        ('h', 'H'),
        ('j', 'J'),
        ('k', 'K'),
        ('l', 'L'),
    ]));
    row_home.push(spacer(0.5));
    rows.push(row_home);

    let mut row_shift: Vec<KeySpec> = Vec::with_capacity(9);
    row_shift.push(util_glyph(
        KeyGlyph::Shift,
        KeyAction::Switch(if shifted {
            Layer::Letters
        } else {
            Layer::Shifted
        }),
        1.5,
    ));
    row_shift.extend(row_keys(&[
        ('z', 'Z'),
        ('x', 'X'),
        ('c', 'C'),
        ('v', 'V'),
        ('b', 'B'),
        ('n', 'N'),
        ('m', 'M'),
    ]));
    row_shift.push(backspace(1.5));
    rows.push(row_shift);

    // Bottom row: ?123 · , · dismiss · space · . · enter. Gboard puts
    // its emoji key where we place dismiss — we have no emoji layer,
    // and the panel needs an explicit close affordance (no system nav
    // bar underneath us).
    rows.push(vec![
        mode_pill("?123", Layer::Symbols),
        util_char(',', 1.0),
        KeySpec {
            cap: KeyCap::Glyph(KeyGlyph::DismissDown),
            action: KeyAction::Dismiss,
            ..key(' ')
        },
        space_bar(4.0),
        util_char('.', 1.0),
        enter_key(),
    ]);

    Layout { rows }
}

/// Gboard's `?123` page.
fn symbols_layer() -> Layout {
    let mut rows: Vec<Vec<KeySpec>> = Vec::with_capacity(4);

    rows.push("1234567890".chars().map(key).collect());
    rows.push("@#$_&-+()/".chars().map(key).collect());

    let mut row3: Vec<KeySpec> = Vec::with_capacity(9);
    row3.push(util_text(
        "=\\<",
        KeyAction::Switch(Layer::SymbolsShift),
        1.5,
    ));
    row3.extend("*\"':;!?".chars().map(key));
    row3.push(backspace(1.5));
    rows.push(row3);

    rows.push(vec![
        mode_pill("ABC", Layer::Letters),
        util_char(',', 1.0),
        KeySpec {
            cap: KeyCap::Text("1234".to_string()),
            action: KeyAction::Switch(Layer::NumPad),
            ..key(' ')
        },
        space_bar(4.0),
        util_char('.', 1.0),
        enter_key(),
    ]);

    Layout { rows }
}

/// Gboard's `=\<` page.
fn symbols_shift_layer() -> Layout {
    let mut rows: Vec<Vec<KeySpec>> = Vec::with_capacity(4);

    rows.push("~`|•√π÷×§Δ".chars().map(key).collect());
    rows.push("£¢€¥^°={}\\".chars().map(key).collect());

    let mut row3: Vec<KeySpec> = Vec::with_capacity(9);
    row3.push(util_text("?123", KeyAction::Switch(Layer::Symbols), 1.5));
    row3.extend("%©®™✓[]".chars().map(key));
    row3.push(backspace(1.5));
    rows.push(row3);

    rows.push(vec![
        mode_pill("ABC", Layer::Letters),
        util_char('<', 1.0),
        KeySpec {
            cap: KeyCap::Text("1234".to_string()),
            action: KeyAction::Switch(Layer::NumPad),
            ..key(' ')
        },
        space_bar(4.0),
        util_char('>', 1.0),
        enter_key(),
    ]);

    Layout { rows }
}

/// Phone-style number pad: operator strip on the left, digit grid in
/// the middle, `%` / space / backspace column on the right. All rows
/// sum to 9 units so the columns align.
///
/// Deviation from Gboard: its operator strip squeezes `+ - * /` into
/// three rows' height; ours is one operator per row, so `/` is dropped
/// (still reachable on the `?123` page).
fn numpad_layer() -> Layout {
    let digit = |c: char| wide_key(c, 2.0);

    let mut rows: Vec<Vec<KeySpec>> = Vec::with_capacity(4);
    rows.push(vec![
        util_char('+', 1.5),
        digit('1'),
        digit('2'),
        digit('3'),
        util_char('%', 1.5),
    ]);
    rows.push(vec![
        util_char('-', 1.5),
        digit('4'),
        digit('5'),
        digit('6'),
        KeySpec {
            width_units: 1.5,
            cap: KeyCap::Glyph(KeyGlyph::Space),
            action: KeyAction::Space,
            kind: KeyKind::Utility,
            pill: false,
        },
    ]);
    rows.push(vec![
        util_char('*', 1.5),
        digit('7'),
        digit('8'),
        digit('9'),
        backspace(1.5),
    ]);
    rows.push(vec![
        mode_pill("ABC", Layer::Letters),
        util_char(',', 1.0),
        KeySpec {
            cap: KeyCap::Text("!?#".to_string()),
            action: KeyAction::Switch(Layer::Symbols),
            ..key(' ')
        },
        wide_key('0', 2.0),
        key('='),
        util_char('.', 1.0),
        enter_key(),
    ]);

    Layout { rows }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn letters_layer_has_five_rows_with_number_row() {
        let l = Layout::for_layer(Layer::Letters);
        assert_eq!(l.rows.len(), 5, "number row + 3 letter rows + action row");
        let digits: Vec<char> = l.rows[0]
            .iter()
            .filter_map(|k| match k.action {
                KeyAction::Char(c) => Some(c),
                _ => None,
            })
            .collect();
        assert_eq!(digits, "1234567890".chars().collect::<Vec<_>>());
    }

    #[test]
    fn home_row_is_centered_with_spacers() {
        let l = Layout::for_layer(Layer::Letters);
        let home = &l.rows[2];
        assert_eq!(home.first().unwrap().kind, KeyKind::Spacer);
        assert_eq!(home.last().unwrap().kind, KeyKind::Spacer);
        // Spacers + 9 letters keep the row at the same 10-unit width as
        // the rows above, so letter keys don't stretch.
        let total: f64 = home.iter().map(|k| k.width_units).sum();
        assert!((total - 10.0).abs() < 1e-9);
    }

    #[test]
    fn shift_key_switches_layer() {
        let l = Layout::for_layer(Layer::Letters);
        let shift = &l.rows[3][0];
        match shift.action {
            KeyAction::Switch(Layer::Shifted) => {}
            other => panic!("expected Switch(Shifted) on shift row, got {other:?}"),
        }
    }

    #[test]
    fn shifted_layer_emits_uppercase_chars() {
        let l = Layout::for_layer(Layer::Shifted);
        // Second row (after the number row), first key: should be 'Q'.
        let q = &l.rows[1][0];
        match q.action {
            KeyAction::Char('Q') => {}
            other => panic!("expected Char('Q'), got {other:?}"),
        }
    }

    #[test]
    fn symbols_layer_matches_gboard_page() {
        let l = Layout::for_layer(Layer::Symbols);
        let row2: Vec<char> = l.rows[1]
            .iter()
            .filter_map(|k| match k.action {
                KeyAction::Char(c) => Some(c),
                _ => None,
            })
            .collect();
        assert_eq!(row2, "@#$_&-+()/".chars().collect::<Vec<_>>());
        // First key of row 3 pages to the extended symbols layer.
        match l.rows[2][0].action {
            KeyAction::Switch(Layer::SymbolsShift) => {}
            other => panic!("expected Switch(SymbolsShift), got {other:?}"),
        }
    }

    #[test]
    fn symbols_shift_layer_pages_back_to_symbols() {
        let l = Layout::for_layer(Layer::SymbolsShift);
        match l.rows[2][0].action {
            KeyAction::Switch(Layer::Symbols) => {}
            other => panic!("expected Switch(Symbols), got {other:?}"),
        }
    }

    #[test]
    fn numpad_has_digit_grid_and_operators() {
        let l = Layout::for_layer(Layer::NumPad);
        let chars_of = |row: &Vec<KeySpec>| -> Vec<char> {
            row.iter()
                .filter_map(|k| match k.action {
                    KeyAction::Char(c) => Some(c),
                    _ => None,
                })
                .collect()
        };
        assert_eq!(chars_of(&l.rows[0]), vec!['+', '1', '2', '3', '%']);
        assert_eq!(chars_of(&l.rows[2]), vec!['*', '7', '8', '9']);
        assert!(chars_of(&l.rows[3]).contains(&'0'));
        // Every row spans the same 9 units so the columns align.
        for row in &l.rows {
            let total: f64 = row.iter().map(|k| k.width_units).sum();
            assert!((total - 9.0).abs() < 1e-9, "row width {total} != 9");
        }
    }

    #[test]
    fn bottom_row_mode_keys_page_between_layers() {
        let letters = Layout::for_layer(Layer::Letters);
        match letters.rows[4][0].action {
            KeyAction::Switch(Layer::Symbols) => {}
            other => panic!("?123 should open Symbols, got {other:?}"),
        }
        let symbols = Layout::for_layer(Layer::Symbols);
        match symbols.rows[3][0].action {
            KeyAction::Switch(Layer::Letters) => {}
            other => panic!("ABC should open Letters, got {other:?}"),
        }
    }
}