frust-glyph 0.5.2

Glyph design-system catalog for Frust: a terminal-native, dark-first, monospace-led widget set with bundled Space Mono and IBM Plex Mono fonts.
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
//! [`glyph_list`]/[`GlyphListView`] + [`GlyphListItem`]: a bordered, rounded
//! list container whose rows are **data** (not child views) — each a leading
//! glyph box (a 32px raised square holding an accent glyph char) + a title/sub
//! text column + an optional right-aligned meta + an optional chevron (the
//! Glyph design system's `.list`/`.list-item`).
//!
//! Because the rows are the widget's own data, it shapes/paints every text run
//! directly (like [`super::badge`]) and hit-tests presses itself: a press
//! captures the row under the pointer and fires `on_press(index)` on
//! release **inside that same row** (fire-on-up-inside), with a `Cancel`
//! clearing the pressed wash — the controlled, never-self-mutating contract
//! `crate::material::navbar` uses.
//!
//! # Token resolution
//!
//! - **container** = `surface_container` fill + `outline` border,
//!   `shape.medium` corner; rows divided by an `outline_variant` hairline.
//! - **glyph box** = `surface_container_high` (Glyph `bg-raised`) fill; the
//!   glyph char is `primary` (the accent).
//! - **title** = `on_surface`; **sub/meta/chevron** = `on_surface_variant`.
//! - **pressed wash** = `on_surface` at [`PRESS_WASH_ALPHA`].
//! - **families**, read at layout from the live type scale: the glyph char
//!   from `headlineSmall` (Space Mono under Glyph's own scale), the title from
//!   `titleSmall`, the sub and meta from `bodySmall` (all IBM Plex Mono), so
//!   a theme swap reshapes every row (see [`super::badge`]'s Typeface
//!   section).
//!
//! Unthemed, each falls back to the literal Glyph **dark** constant (each
//! family to the matching Glyph stack).

use std::rc::Rc;

use frust::authoring::text::{
    FontFamily, FontWeight, GenericSlot, TextContext, TextLayout, TextStyle,
};
use frust::authoring::{Action, Role};
use frust::authoring::{
    BoxConstraints, BuildCtx, ChangeFlags, EventCtx, EventResult, InputEvent, LayoutCtx, PaintCtx,
    PaintScene, PointerPhase, SemanticsCtx, View, Widget,
};
use frust::{ShapeScale, Theme};
use kurbo::{Point, Rect, RoundedRect, Shape, Size, Vec2};
use peniko::{Brush, Color};

use crate::press::presses;

/// Corner-rounding tolerance for the container border stroke.
const PATH_TOLERANCE: f64 = 0.1;
/// Horizontal padding inside a row, in logical px.
const ROW_PAD_X: f64 = 14.0;
/// Vertical padding inside a row, in logical px.
const ROW_PAD_Y: f64 = 12.0;
/// The leading glyph box edge, in logical px.
const GLYPH_BOX: f64 = 32.0;
/// Gap between the glyph box and the text column, in logical px.
const GLYPH_GAP: f64 = 12.0;
/// Gap between the title and sub rows, in logical px.
const TITLE_SUB_GAP: f64 = 2.0;
/// Gap between the text column / meta and the chevron, in logical px.
const CHEVRON_GAP: f64 = 12.0;
/// The chevron's arm length, in logical px.
const CHEVRON_ARM: f64 = 4.5;
/// The chevron stroke width, in logical px.
const CHEVRON_WIDTH: f64 = 1.5;
/// Container border width, in logical px.
const LIST_BORDER_WIDTH: f64 = 1.0;
/// Row divider (hairline) width, in logical px.
const DIVIDER_WIDTH: f64 = 1.0;
/// The pressed-row wash alpha over `on_surface`.
const PRESS_WASH_ALPHA: f32 = 0.06;

/// Title font size, in logical px.
const TITLE_SIZE: f32 = 13.0;
/// Sub-title font size, in logical px.
const SUB_SIZE: f32 = 11.5;
/// Meta font size, in logical px.
const META_SIZE: f32 = 11.5;
/// The glyph-box glyph font size, in logical px.
const GLYPH_SIZE: f32 = 14.0;

/// Unthemed corner-radius fallback — Glyph `--radius-md` (10px).
const LIST_RADIUS_FALLBACK: f64 = 10.0;
/// Glyph-box corner radius fallback — `--radius-sm` (6px).
const GLYPH_BOX_RADIUS_FALLBACK: f64 = 6.0;

// ---- Unthemed fallback constants (Glyph **dark** values) ---------------

const LIST_BG: Color = Color::from_rgb8(0x16, 0x1a, 0x23); // bg-surface
const LIST_BORDER: Color = Color::from_rgb8(0x3e, 0x3f, 0x44); // outline
const LIST_DIVIDER: Color = Color::from_rgb8(0x2a, 0x2d, 0x33); // outline-variant
const GLYPH_BOX_BG: Color = Color::from_rgb8(0x1e, 0x23, 0x30); // bg-raised
const GLYPH_FG: Color = Color::from_rgb8(0xff, 0xb6, 0x27); // accent
const TITLE_FG: Color = Color::from_rgb8(0xf2, 0xea, 0xd9); // fg
const MUTED_FG: Color = Color::from_rgb8(0xa3, 0x9c, 0x88); // fg-muted
const PRESS_WASH_BASE: Color = Color::from_rgb8(0xf2, 0xea, 0xd9); // on_surface

/// Return `color` with its alpha channel replaced by `alpha`.
fn with_alpha(color: Color, alpha: f32) -> Color {
    let c = color.components;
    Color::new([c[0], c[1], c[2], alpha])
}

/// One row of a [`GlyphListView`]: a glyph char, a title, and optional sub /
/// meta text + a trailing chevron. Pure data — see the [module docs](self).
#[derive(Clone)]
pub struct GlyphListItem {
    glyph: String,
    title: String,
    sub: Option<String>,
    meta: Option<String>,
    chevron: bool,
}

/// Create a list row with a leading `glyph` char and a `title`.
pub fn glyph_list_item(glyph: impl Into<String>, title: impl Into<String>) -> GlyphListItem {
    GlyphListItem {
        glyph: glyph.into(),
        title: title.into(),
        sub: None,
        meta: None,
        chevron: false,
    }
}

impl GlyphListItem {
    /// Add a sub-title line under the title.
    #[allow(clippy::should_implement_trait)] // `.sub(..)` is a builder, not arithmetic
    pub fn sub(mut self, sub: impl Into<String>) -> Self {
        self.sub = Some(sub.into());
        self
    }

    /// Add right-aligned meta text.
    pub fn meta(mut self, meta: impl Into<String>) -> Self {
        self.meta = Some(meta.into());
        self
    }

    /// Show (or hide) the trailing chevron.
    pub fn chevron(mut self, chevron: bool) -> Self {
        self.chevron = chevron;
        self
    }
}

/// A view-held, typed row-press callback (erased on build).
type OnPress<State> = Rc<dyn Fn(&mut State, usize)>;

/// A declarative Glyph list. See the [module docs](self).
pub struct GlyphListView<State: 'static> {
    items: Vec<GlyphListItem>,
    on_press: Option<OnPress<State>>,
}

/// Create a Glyph list from `items`. Chain [`GlyphListView::on_press`] to make
/// rows interactive.
pub fn glyph_list<State: 'static>(items: Vec<GlyphListItem>) -> GlyphListView<State> {
    GlyphListView {
        items,
        on_press: None,
    }
}

impl<State: 'static> GlyphListView<State> {
    /// Fire `on_press(state, index)` on a release inside the row at `index`
    /// (fire-on-up-inside). The list never mutates itself; the app owns any
    /// resulting state change.
    pub fn on_press<F: Fn(&mut State, usize) + 'static>(mut self, on_press: F) -> Self {
        self.on_press = Some(Rc::new(on_press));
        self
    }
}

/// A minimal retained text run — see [`super::badge`]'s `GlyphLabel`.
struct GlyphLabel {
    content: String,
    layout: Option<TextLayout>,
    laid_out_style: Option<TextStyle>,
    laid_out_max_width: Option<f32>,
}

impl GlyphLabel {
    fn new(content: impl Into<String>) -> Self {
        Self {
            content: content.into(),
            layout: None,
            laid_out_style: None,
            laid_out_max_width: None,
        }
    }

    fn layout(&mut self, ctx: &mut LayoutCtx, style: &TextStyle, max_width: Option<f32>) -> Size {
        if let Some(cached) = &self.layout
            && self.laid_out_max_width == max_width
            && self.laid_out_style.as_ref() == Some(style)
        {
            return cached.size();
        }
        let text_ctx = ctx.text_context::<TextContext>();
        let laid = text_ctx.layout(&self.content, style, max_width);
        let size = laid.size();
        self.layout = Some(laid);
        self.laid_out_style = Some(style.clone());
        self.laid_out_max_width = max_width;
        size
    }

    fn paint(&self, origin: Point, scene: &mut dyn PaintScene) {
        if let Some(layout) = &self.layout {
            for run in layout.to_scene_runs(origin) {
                scene.draw_glyph_run(run);
            }
        }
    }
}

/// A retained row: its text runs plus the laid-out `(y, height)` band used for
/// hit-testing and painting.
struct Row {
    glyph: GlyphLabel,
    glyph_size: Size,
    title: GlyphLabel,
    title_text: String,
    title_size: Size,
    sub: Option<GlyphLabel>,
    sub_size: Size,
    meta: Option<GlyphLabel>,
    meta_size: Size,
    chevron: bool,
    y: f64,
    height: f64,
}

impl Row {
    fn from_item(item: &GlyphListItem) -> Self {
        Row {
            glyph: GlyphLabel::new(item.glyph.clone()),
            glyph_size: Size::ZERO,
            title: GlyphLabel::new(item.title.clone()),
            title_text: item.title.clone(),
            title_size: Size::ZERO,
            sub: item.sub.as_ref().map(GlyphLabel::new),
            sub_size: Size::ZERO,
            meta: item.meta.as_ref().map(GlyphLabel::new),
            meta_size: Size::ZERO,
            chevron: item.chevron,
            y: 0.0,
            height: 0.0,
        }
    }
}

/// The retained widget for a [`GlyphListView`].
pub struct GlyphListWidget {
    rows: Vec<Row>,
    on_press: Option<frust::authoring::ErasedArgCallback<usize>>,
    /// The row currently pressed (armed by a `Down`), if any.
    pressed_row: Option<usize>,
    /// The row that captured the active gesture, if any.
    captured_row: Option<usize>,
}

impl<State: 'static> View<State> for GlyphListView<State> {
    type Element = GlyphListWidget;

    fn build(&self, _ctx: &mut BuildCtx<'_>) -> GlyphListWidget {
        GlyphListWidget {
            rows: self.items.iter().map(Row::from_item).collect(),
            on_press: self
                .on_press
                .as_ref()
                .map(frust::authoring::erase_callback_arg),
            pressed_row: None,
            captured_row: None,
        }
    }

    fn rebuild(
        &self,
        prev: &Self,
        element: &mut GlyphListWidget,
        _ctx: &mut BuildCtx<'_>,
    ) -> ChangeFlags {
        let mut flags = ChangeFlags::NONE;
        // Rows are reconciled positionally (v1: no keyed-reconciliation
        // support here); any structural or content change rebuilds the row
        // set wholesale and drops a mid-gesture capture.
        let changed = prev.items.len() != self.items.len()
            || prev
                .items
                .iter()
                .zip(&self.items)
                .any(|(a, b)| !items_equal(a, b));
        if changed {
            element.rows = self.items.iter().map(Row::from_item).collect();
            element.pressed_row = None;
            element.captured_row = None;
            flags |= ChangeFlags::LAYOUT | ChangeFlags::PAINT;
        }
        // Always refresh the callback (a cheap closure swap, never a structural
        // change) so the newest app state is captured.
        element.on_press = self
            .on_press
            .as_ref()
            .map(frust::authoring::erase_callback_arg);
        flags
    }
}

fn items_equal(a: &GlyphListItem, b: &GlyphListItem) -> bool {
    a.glyph == b.glyph
        && a.title == b.title
        && a.sub == b.sub
        && a.meta == b.meta
        && a.chevron == b.chevron
}

/// Glyph's UI face stack (IBM Plex Mono): the title's, sub's and meta's
/// unthemed family.
fn ui_face() -> FontFamily {
    FontFamily::stack_with_generic(["IBM Plex Mono"], GenericSlot::Monospace)
}

/// Glyph's display face stack (Space Mono): the glyph char's unthemed family.
fn display_face() -> FontFamily {
    FontFamily::stack_with_generic(["Space Mono"], GenericSlot::Monospace)
}

/// The title's style; family from the theme's `titleSmall` role.
fn title_style(theme: Option<&Theme>, color: Color) -> TextStyle {
    TextStyle {
        family: theme.map_or_else(ui_face, |t| t.type_scale.title_small.family.clone()),
        weight: FontWeight::MEDIUM,
        ..TextStyle::new(TITLE_SIZE, color)
    }
}

/// The sub line's style; family from the theme's `bodySmall` role.
fn sub_style(theme: Option<&Theme>, color: Color) -> TextStyle {
    TextStyle {
        family: theme.map_or_else(ui_face, |t| t.type_scale.body_small.family.clone()),
        weight: FontWeight::REGULAR,
        ..TextStyle::new(SUB_SIZE, color)
    }
}

/// The meta's style; family from the theme's `bodySmall` role.
fn meta_style(theme: Option<&Theme>, color: Color) -> TextStyle {
    TextStyle {
        family: theme.map_or_else(ui_face, |t| t.type_scale.body_small.family.clone()),
        weight: FontWeight::REGULAR,
        ..TextStyle::new(META_SIZE, color)
    }
}

/// The glyph char's style; family from the theme's `headlineSmall` role.
fn glyph_style(theme: Option<&Theme>, color: Color) -> TextStyle {
    TextStyle {
        family: theme.map_or_else(display_face, |t| t.type_scale.headline_small.family.clone()),
        weight: FontWeight::BOLD,
        ..TextStyle::new(GLYPH_SIZE, color)
    }
}

/// Resolve `(bg, border, divider, box_bg, glyph, title, muted)`.
#[allow(clippy::type_complexity)]
fn resolve_colors(theme: Option<&Theme>) -> (Color, Color, Color, Color, Color, Color, Color) {
    match theme {
        Some(theme) => {
            let s = theme.scheme();
            (
                s.surface_container,
                s.outline,
                s.outline_variant,
                s.surface_container_high,
                s.primary,
                s.on_surface,
                s.on_surface_variant,
            )
        }
        None => (
            LIST_BG,
            LIST_BORDER,
            LIST_DIVIDER,
            GLYPH_BOX_BG,
            GLYPH_FG,
            TITLE_FG,
            MUTED_FG,
        ),
    }
}

fn resolve_radius(theme: Option<&Theme>, size: Size) -> f64 {
    match theme {
        Some(theme) => ShapeScale::resolve(theme.shape.medium, size.width, size.height),
        None => LIST_RADIUS_FALLBACK,
    }
}

fn resolve_box_radius(theme: Option<&Theme>) -> f64 {
    match theme {
        Some(theme) => ShapeScale::resolve(theme.shape.small, GLYPH_BOX, GLYPH_BOX),
        None => GLYPH_BOX_RADIUS_FALLBACK,
    }
}

impl GlyphListWidget {
    /// The row index containing local `y`, if any.
    fn row_at(&self, y: f64) -> Option<usize> {
        self.rows
            .iter()
            .position(|r| y >= r.y && y < r.y + r.height)
    }
}

impl Widget for GlyphListWidget {
    fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        // Resolve every style up front so the immutable theme borrow ends
        // before the `&mut ctx` shaping calls below.
        let theme = Theme::from_layout_ctx(ctx);
        let (_, _, _, _, glyph_c, title_c, muted_c) = resolve_colors(theme);
        let glyph_style = glyph_style(theme, glyph_c);
        let title_style = title_style(theme, title_c);
        let sub_style = sub_style(theme, muted_c);
        let meta_style = meta_style(theme, muted_c);

        let width = if bc.max().width.is_finite() {
            bc.max().width
        } else {
            280.0 // a sensible intrinsic width when unconstrained
        };
        let text_col_x = ROW_PAD_X + GLYPH_BOX + GLYPH_GAP;

        let mut y = 0.0f64;
        for row in self.rows.iter_mut() {
            row.glyph_size = row.glyph.layout(ctx, &glyph_style, None);
            let meta_w = if let Some(meta) = row.meta.as_mut() {
                row.meta_size = meta.layout(ctx, &meta_style, None);
                row.meta_size.width + CHEVRON_GAP
            } else {
                0.0
            };
            let chevron_w = if row.chevron {
                CHEVRON_ARM + CHEVRON_GAP
            } else {
                0.0
            };
            let text_col_max =
                ((width - text_col_x - ROW_PAD_X - meta_w - chevron_w).max(20.0)) as f32;
            row.title_size = row.title.layout(ctx, &title_style, Some(text_col_max));
            let text_col_h = if let Some(sub) = row.sub.as_mut() {
                row.sub_size = sub.layout(ctx, &sub_style, Some(text_col_max));
                row.title_size.height + TITLE_SUB_GAP + row.sub_size.height
            } else {
                row.sub_size = Size::ZERO;
                row.title_size.height
            };
            let content_h = text_col_h.max(GLYPH_BOX);
            let height = content_h + ROW_PAD_Y * 2.0;
            row.y = y;
            row.height = height;
            y += height;
        }

        bc.constrain(Size::new(width, y))
    }

    fn paint(&mut self, ctx: &mut PaintCtx, scene: &mut dyn PaintScene) {
        let theme = Theme::from_paint_ctx(ctx);
        let (bg, border, divider, box_bg, _glyph_c, _title_c, muted_c) = resolve_colors(theme);
        let press_base = theme
            .map(|t| t.scheme().on_surface)
            .unwrap_or(PRESS_WASH_BASE);
        let radius = resolve_radius(theme, ctx.size());
        let box_radius = resolve_box_radius(theme);
        let o = ctx.origin();
        let size = ctx.size();

        scene.fill_rounded_rect(o, size, radius, bg);

        let text_col_x = ROW_PAD_X + GLYPH_BOX + GLYPH_GAP;
        for (i, row) in self.rows.iter().enumerate() {
            let row_origin = o + Vec2::new(0.0, row.y);

            // Pressed wash.
            if self.pressed_row == Some(i) {
                scene.fill_rect(
                    row_origin,
                    Size::new(size.width, row.height),
                    with_alpha(press_base, PRESS_WASH_ALPHA),
                );
            }

            // Divider above every row but the first.
            if i > 0 {
                scene.stroke_line(
                    Point::new(o.x, row_origin.y),
                    Point::new(o.x + size.width, row_origin.y),
                    DIVIDER_WIDTH,
                    divider,
                );
            }

            // Glyph box + centered glyph char.
            let box_y = row.y + (row.height - GLYPH_BOX) / 2.0;
            scene.fill_rounded_rect(
                o + Vec2::new(ROW_PAD_X, box_y),
                Size::new(GLYPH_BOX, GLYPH_BOX),
                box_radius,
                box_bg,
            );
            let gx = ROW_PAD_X + (GLYPH_BOX - row.glyph_size.width) / 2.0;
            let gy = box_y + (GLYPH_BOX - row.glyph_size.height) / 2.0;
            row.glyph.paint(o + Vec2::new(gx, gy), scene);

            // Text column (title over optional sub), vertically centered.
            let text_col_h = if row.sub.is_some() {
                row.title_size.height + TITLE_SUB_GAP + row.sub_size.height
            } else {
                row.title_size.height
            };
            let text_top = row.y + (row.height - text_col_h) / 2.0;
            row.title.paint(o + Vec2::new(text_col_x, text_top), scene);
            if let Some(sub) = &row.sub {
                sub.paint(
                    o + Vec2::new(text_col_x, text_top + row.title_size.height + TITLE_SUB_GAP),
                    scene,
                );
            }

            // Chevron (far right), then meta to its left.
            let mut right_x = size.width - ROW_PAD_X;
            if row.chevron {
                let cx = o.x + right_x - CHEVRON_ARM;
                let cy = row_origin.y + row.height / 2.0;
                draw_chevron(scene, Point::new(cx, cy), muted_c);
                right_x -= CHEVRON_ARM + CHEVRON_GAP;
            }
            if let Some(meta) = &row.meta {
                let mx = right_x - row.meta_size.width;
                let my = row.y + (row.height - row.meta_size.height) / 2.0;
                meta.paint(o + Vec2::new(mx, my), scene);
            }
        }

        // Container border on top of the row content.
        let rr = RoundedRect::from_rect(Rect::from_origin_size(Point::ORIGIN, size), radius);
        let path = rr.to_path(PATH_TOLERANCE);
        scene.stroke_path(o, &path, LIST_BORDER_WIDTH, &Brush::Solid(border));
    }

    fn event(&mut self, ctx: &mut EventCtx, event: &InputEvent) -> EventResult {
        let InputEvent::Pointer(p) = event else {
            return EventResult::Ignored;
        };
        if self.on_press.is_none() {
            return EventResult::Ignored;
        }
        match p.phase {
            PointerPhase::Down => {
                if !presses(p) {
                    return EventResult::Ignored;
                }
                if let Some(idx) = self.row_at(p.position.y) {
                    self.pressed_row = Some(idx);
                    self.captured_row = Some(idx);
                    ctx.capture_pointer();
                    ctx.request_redraw();
                    return EventResult::Handled;
                }
                EventResult::Ignored
            }
            PointerPhase::Move => {
                let Some(captured) = self.captured_row else {
                    return EventResult::Ignored;
                };
                // The wash follows the pointer in/out of the captured row.
                let inside_row = self.row_at(p.position.y) == Some(captured);
                let new_pressed = if inside_row { Some(captured) } else { None };
                if new_pressed != self.pressed_row {
                    self.pressed_row = new_pressed;
                    ctx.request_redraw();
                }
                EventResult::Handled
            }
            PointerPhase::Up => {
                let Some(captured) = self.captured_row.take() else {
                    return EventResult::Ignored;
                };
                let fired = self.row_at(p.position.y) == Some(captured);
                self.pressed_row = None;
                if fired && let Some(cb) = self.on_press.as_mut() {
                    cb(ctx, captured);
                }
                ctx.request_redraw();
                EventResult::Handled
            }
            PointerPhase::Cancel => {
                if self.captured_row.take().is_some() {
                    self.pressed_row = None;
                    ctx.request_redraw();
                    return EventResult::Handled;
                }
                EventResult::Ignored
            }
        }
    }

    fn semantics(&self, ctx: &mut SemanticsCtx) {
        ctx.push_container(
            Role::List,
            |_| {},
            |ctx| {
                for row in &self.rows {
                    ctx.push_node(Role::ListItem, |node| {
                        node.set_label(row.title_text.as_str());
                        node.add_action(Action::Click);
                    });
                }
            },
        );
    }
}

/// Draw a right-pointing chevron (`>`) centered at `center` in `color`.
fn draw_chevron(scene: &mut dyn PaintScene, center: Point, color: Color) {
    let top = Point::new(center.x - CHEVRON_ARM, center.y - CHEVRON_ARM);
    let tip = Point::new(center.x + CHEVRON_ARM, center.y);
    let bottom = Point::new(center.x - CHEVRON_ARM, center.y + CHEVRON_ARM);
    scene.stroke_line(top, tip, CHEVRON_WIDTH, color);
    scene.stroke_line(tip, bottom, CHEVRON_WIDTH, color);
}

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

    #[derive(Default)]
    struct Recorder {
        rects: Vec<(Point, Size, Color)>,
        rrects: Vec<(Point, Size, f64, Color)>,
        lines: usize,
        glyph_colors: Vec<Color>,
    }

    impl PaintScene for Recorder {
        fn fill_rect(&mut self, o: Point, s: Size, c: Color) {
            self.rects.push((o, s, c));
        }
        fn draw_text(&mut self, _o: Point, _t: &str) {}
        fn fill_rounded_rect(&mut self, o: Point, s: Size, radius: f64, color: Color) {
            self.rrects.push((o, s, radius, color));
        }
        fn stroke_line(&mut self, _p0: Point, _p1: Point, _w: f64, _c: Color) {
            self.lines += 1;
        }
        fn stroke_path(&mut self, _o: Point, _p: &kurbo::BezPath, _w: f64, _b: &Brush) {}
        fn draw_glyph_run(&mut self, run: frust::authoring::scene::GlyphRun) {
            if let Brush::Solid(c) = run.brush {
                self.glyph_colors.push(c);
            }
        }
    }

    fn sample_items() -> Vec<GlyphListItem> {
        vec![
            glyph_list_item("$", "deploy")
                .sub("staged")
                .meta("2m")
                .chevron(true),
            glyph_list_item("#", "rollback").sub("ready").chevron(true),
        ]
    }

    fn build<S: 'static>(view: &GlyphListView<S>) -> GlyphListWidget {
        let mut counter = 0u64;
        View::<S>::build(view, &mut BuildCtx::new(&mut counter))
    }

    fn layout<S: 'static>(w: &mut GlyphListWidget, theme: Option<&Theme>) -> Size {
        let mut tcx = TextContext::new();
        let mut lctx = match theme {
            Some(t) => {
                LayoutCtx::with_resources(Some(&mut tcx as &mut dyn Any), Some(t as &dyn Any))
            }
            None => LayoutCtx::with_text_context(&mut tcx as &mut dyn Any),
        };
        let _ = std::marker::PhantomData::<S>;
        w.layout(
            &mut lctx,
            &BoxConstraints::tight(Size::new(320.0, f64::INFINITY)),
        )
    }

    fn paint(w: &mut GlyphListWidget, size: Size, theme: Option<&Theme>) -> Recorder {
        let mut rec = Recorder::default();
        let mut ctx = match theme {
            Some(t) => PaintCtx::new(Point::ZERO, size).with_theme(t),
            None => PaintCtx::new(Point::ZERO, size),
        };
        w.paint(&mut ctx, &mut rec);
        rec
    }

    #[test]
    fn unthemed_container_and_glyph_use_fallback_constants() {
        let view: GlyphListView<()> = glyph_list(sample_items());
        let mut w = build(&view);
        let size = layout::<()>(&mut w, None);
        let rec = paint(&mut w, size, None);
        // First rounded rect is the container fill.
        assert_eq!(rec.rrects[0].3, LIST_BG);
        // A glyph-box fill (raised bg) appears among the rounded rects.
        assert!(rec.rrects.iter().any(|(_, _, _, c)| *c == GLYPH_BOX_BG));
        // Accent glyph chars are drawn.
        assert!(rec.glyph_colors.contains(&GLYPH_FG));
    }

    #[test]
    fn glyph_dark_and_light_container_differ() {
        let dark = crate::baseline();
        let light = dark.clone().with_brightness(frust::Brightness::Light);
        let view: GlyphListView<()> = glyph_list(sample_items());
        let mut wd = build(&view);
        let sd = layout::<()>(&mut wd, Some(&dark));
        let rec_d = paint(&mut wd, sd, Some(&dark));
        let mut wl = build(&view);
        let sl = layout::<()>(&mut wl, Some(&light));
        let rec_l = paint(&mut wl, sl, Some(&light));
        assert_eq!(rec_d.rrects[0].3, dark.scheme().surface_container);
        assert_eq!(rec_l.rrects[0].3, light.scheme().surface_container);
        assert_ne!(rec_d.rrects[0].3, rec_l.rrects[0].3);
    }

    fn ev(phase: PointerPhase, y: f64) -> InputEvent {
        InputEvent::Pointer(frust_core::PointerEvent {
            phase,
            position: Point::new(20.0, y),
            button: frust_core::PointerButton::Primary,
        })
    }

    // The concrete state type must stay `Vec<usize>` for the erased callback's
    // `Any` downcast — a slice would not round-trip.
    #[allow(clippy::ptr_arg)]
    fn dispatch(w: &mut GlyphListWidget, state: &mut Vec<usize>, e: &InputEvent) -> EventResult {
        let sa: &mut dyn Any = state;
        let mut ctx = EventCtx::new(sa, Point::ZERO, Size::new(320.0, 500.0));
        w.event(&mut ctx, e)
    }

    #[test]
    fn press_fires_on_up_inside_with_correct_index() {
        let view: GlyphListView<Vec<usize>> =
            glyph_list(sample_items()).on_press(|s: &mut Vec<usize>, i| s.push(i));
        let mut w = build(&view);
        layout::<Vec<usize>>(&mut w, None);
        let row1_mid = w.rows[1].y + w.rows[1].height / 2.0;

        let mut fired = Vec::new();
        dispatch(&mut w, &mut fired, &ev(PointerPhase::Down, row1_mid));
        assert_eq!(w.pressed_row, Some(1));
        dispatch(&mut w, &mut fired, &ev(PointerPhase::Up, row1_mid));
        assert_eq!(fired, vec![1]);
        assert_eq!(w.pressed_row, None);
        assert_eq!(w.captured_row, None);
    }

    #[test]
    fn up_outside_the_pressed_row_does_not_fire() {
        let view: GlyphListView<Vec<usize>> =
            glyph_list(sample_items()).on_press(|s: &mut Vec<usize>, i| s.push(i));
        let mut w = build(&view);
        layout::<Vec<usize>>(&mut w, None);
        let row0_mid = w.rows[0].y + w.rows[0].height / 2.0;
        let row1_mid = w.rows[1].y + w.rows[1].height / 2.0;

        let mut fired = Vec::new();
        dispatch(&mut w, &mut fired, &ev(PointerPhase::Down, row0_mid));
        dispatch(&mut w, &mut fired, &ev(PointerPhase::Move, row1_mid));
        assert_eq!(w.pressed_row, None, "wash clears when leaving the row");
        dispatch(&mut w, &mut fired, &ev(PointerPhase::Up, row1_mid));
        assert!(fired.is_empty());
    }

    #[test]
    fn cancel_clears_pressed_and_captured() {
        let view: GlyphListView<Vec<usize>> =
            glyph_list(sample_items()).on_press(|s: &mut Vec<usize>, i| s.push(i));
        let mut w = build(&view);
        layout::<Vec<usize>>(&mut w, None);
        let row0_mid = w.rows[0].y + w.rows[0].height / 2.0;

        let mut fired = Vec::new();
        dispatch(&mut w, &mut fired, &ev(PointerPhase::Down, row0_mid));
        assert_eq!(w.captured_row, Some(0));
        dispatch(&mut w, &mut fired, &ev(PointerPhase::Cancel, row0_mid));
        assert_eq!(w.pressed_row, None);
        assert_eq!(w.captured_row, None);
        dispatch(&mut w, &mut fired, &ev(PointerPhase::Up, row0_mid));
        assert!(fired.is_empty());
    }

    #[test]
    fn non_interactive_list_ignores_presses() {
        let view: GlyphListView<Vec<usize>> = glyph_list(sample_items());
        let mut w = build(&view);
        layout::<Vec<usize>>(&mut w, None);
        let mut fired = Vec::new();
        let y = w.rows[0].y + 4.0;
        let r = dispatch(&mut w, &mut fired, &ev(PointerPhase::Down, y));
        assert_eq!(r, EventResult::Ignored);
        assert_eq!(w.pressed_row, None);
    }

    #[test]
    fn semantics_is_a_list_with_a_node_per_row() {
        fn logic(_s: &mut ()) -> GlyphListView<()> {
            glyph_list(vec![
                glyph_list_item("$", "deploy"),
                glyph_list_item("#", "rollback"),
            ])
        }
        let mut root: frust_core::RenderRoot<(), GlyphListView<()>> = frust_core::RenderRoot::new();
        let mut state = ();
        root.rebuild(&mut logic, &mut state);
        let mut tcx = TextContext::new();
        root.layout_with_text(Size::new(320.0, 400.0), &mut tcx as &mut dyn Any);
        let update = root.semantics();
        let (_, list) = update
            .nodes
            .iter()
            .find(|(_, n)| n.role() == Role::List)
            .expect("list contributes a Role::List node");
        assert_eq!(list.children().len(), 2);
        let labels: Vec<_> = update
            .nodes
            .iter()
            .filter(|(_, n)| n.role() == Role::ListItem)
            .filter_map(|(_, n)| n.label())
            .collect();
        assert!(labels.contains(&"deploy"));
        assert!(labels.contains(&"rollback"));
    }

    // ---- Typeface: every run's family follows its type-scale role ---------

    /// One row carrying every text run: glyph char, title, sub, meta.
    #[cfg(feature = "bundled-fonts")]
    fn full_row(_: &mut ()) -> GlyphListView<()> {
        glyph_list(vec![
            glyph_list_item("$", "deploy").sub("staging").meta("2m"),
        ])
    }

    #[cfg(feature = "bundled-fonts")]
    #[test]
    fn runs_paint_in_their_role_faces_under_the_glyph_theme() {
        use crate::badge::typeface_probe::{Face, painted_faces};
        let faces = painted_faces(full_row, crate::baseline(), Size::new(320.0, 400.0));
        // Glyph char, title, sub, meta.
        assert_eq!(
            faces,
            [
                Face::SpaceMono,
                Face::PlexMono,
                Face::PlexMono,
                Face::PlexMono
            ]
        );
    }

    #[cfg(feature = "bundled-fonts")]
    #[test]
    fn runs_follow_a_live_theme_family_swap() {
        use crate::badge::typeface_probe::{Face, faces_across_a_live_swap};
        let (before, after) = faces_across_a_live_swap(full_row, Size::new(320.0, 400.0));
        assert_eq!(
            before,
            [
                Face::SpaceMono,
                Face::PlexMono,
                Face::PlexMono,
                Face::PlexMono
            ]
        );
        assert_eq!(
            after,
            [
                Face::PlexMono,
                Face::SpaceMono,
                Face::SpaceMono,
                Face::SpaceMono
            ]
        );
    }
}