muri 0.10.6

Menu Utilities for Rust Interfaces — a cross-platform, fully-styleable tray-icon and popup-menu system (a custom-drawn muda/tray-icon replacement).
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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
//! Painting a [`Menu`] through a [`SceneDrawer`]: the single, platform-agnostic
//! layout + draw pass that turns the declarative menu tree into pixels and a
//! hit-test map. It measures text through the drawer, resolves the
//! [`Flex`](crate::Flex)/[`Align`] layout with
//! [`crate::layout::resolve_segments`] (the flush-right promise), and emits
//! fills, separators, icons, and text runs.
//!
//! [`render_menu`] is called once for a snapshot and once per frame by the live
//! popup (cheap — menus are small); it returns a [`LaidMenu`] describing the
//! popup size and the clickable rows for hit-testing.

use std::collections::HashMap;

use crate::geometry::{LogicalPoint, LogicalRect, LogicalSize};
use crate::layout::{resolve_segments, SegmentMetrics};
use crate::menu::{Align, Axis, Content, Icon, Item, MenuId, Row, Segment, Stack};
use crate::render::{SceneDrawer, TextRun};
use crate::style::{Font, FontFamily, Rgba, Weight};
use crate::theme::{MenuOptions, Theme};
use crate::Menu;

/// A scratch memo of `(text, font)` -> measured width, cleared at the start of
/// every [`render_menu`] call. A row's segments are measured once for the width
/// pass (`row_intrinsic`) and again while laying out the draw pass
/// (`draw_row_content`'s `SegmentMetrics` + per-styled-piece widths); without
/// this cache the same `(text, font)` gets re-shaped through the drawer's text
/// engine up to 3× per frame for no behavioral difference.
type MeasureCache = HashMap<MeasureKey, f32>;

#[derive(PartialEq, Eq, Hash)]
struct MeasureKey {
    text: String,
    family_tag: u8,
    family_name: String,
    size_bits: u32,
    weight: u16,
}

impl MeasureKey {
    fn new(text: &str, font: &Font) -> Self {
        let (family_tag, family_name) = match &font.family {
            FontFamily::System => (0u8, String::new()),
            FontFamily::SystemMono => (1u8, String::new()),
            FontFamily::Named(name) => (2u8, name.clone()),
        };
        MeasureKey {
            text: text.to_string(),
            family_tag,
            family_name,
            size_bits: font.size.to_bits(),
            weight: font.weight.ot_weight(),
        }
    }
}

/// Measure `text` in `font` through `drawer`, memoizing in `cache` so an
/// identical `(text, font)` pair within the same frame is only shaped once.
/// Output is identical to calling `drawer.measure_text` directly every time.
fn measure_cached<D: SceneDrawer>(
    drawer: &D,
    cache: &mut MeasureCache,
    text: &str,
    font: &Font,
) -> f32 {
    let key = MeasureKey::new(text, font);
    if let Some(&w) = cache.get(&key) {
        return w;
    }
    let w = drawer.measure_text(text, font);
    cache.insert(key, w);
    w
}

/// A resolved, clickable row in a painted menu (window-relative logical coords).
#[derive(Clone, Debug)]
pub struct LaidRow {
    /// Index of the item within the menu's top-level `items`.
    pub index: usize,
    /// The row's bounding rectangle in logical, window-relative coordinates.
    pub rect: LogicalRect,
    /// The row's click id (may be [`MenuId::none`] for inert rows).
    pub id: MenuId,
    /// Whether the row is interactive (clickable / highlightable).
    pub interactive: bool,
}

/// The result of painting a menu: its logical size and the clickable rows.
#[derive(Clone, Debug, Default)]
pub struct LaidMenu {
    /// Total popup size in logical pixels.
    pub size: LogicalSize,
    /// One entry per top-level item, in order (for hit-testing / highlight).
    pub rows: Vec<LaidRow>,
}

impl LaidMenu {
    /// The top-level item index at a window-relative logical point, if any
    /// interactive row contains it.
    pub fn hit(&self, point: LogicalPoint) -> Option<usize> {
        self.rows
            .iter()
            .find(|r| r.interactive && r.rect.contains(point))
            .map(|r| r.index)
    }

    /// The clickable [`MenuId`] at a point, if it lands on an interactive row.
    pub fn id_at(&self, point: LogicalPoint) -> Option<MenuId> {
        self.rows
            .iter()
            .find(|r| r.interactive && r.rect.contains(point))
            .map(|r| r.id.clone())
    }
}

const SEPARATOR_HEIGHT: f32 = 11.0;
const ICON_SIZE: f32 = 16.0;
const TRAILING_COLUMN: f32 = 14.0;
const DEFAULT_MIN_WIDTH: f32 = 200.0;
const DEFAULT_MAX_WIDTH: f32 = 380.0;
/// Vertical padding above/below an [`Item::Content`] row's measured stack
/// height, on top of the theme's `row_height` floor (issue #44). Chosen to
/// roughly match the breathing room a text row gets from its line-height
/// centering within `theme.row_height`.
const CONTENT_ROW_VPAD: f32 = 4.0;

fn is_submenu(item: &Item) -> bool {
    matches!(item, Item::Submenu { .. })
}

/// The inline leading advance a row consumes for its own icon/checkmark before
/// its text: `ICON_SIZE + gap` when the row carries a leading icon or is checked,
/// else `0`. This is **per-row** and never reserved globally — every row's
/// content starts at the same left x; an icon row simply draws its image first
/// and pushes only *its own* text right (issue #16). A caller wanting a shared
/// checkmark/icon column adds it explicitly.
fn row_leading_width(row: &Row, gap: f32) -> f32 {
    if row.leading.is_some() || row.checked == Some(true) {
        ICON_SIZE + gap
    } else {
        0.0
    }
}

/// Whether the menu reserves a shared leading gutter: true when any row is
/// **checkable** — `checked.is_some()`, i.e. `Some(true)` *or* `Some(false)`, per
/// the `Row::checked` contract that "`Some(true/false)` shows a check column" —
/// so checked *and* currently-unchecked-but-checkable rows align their text past
/// the gutter (the native `NSMenu` look). Testing only `Some(true)` would leave a
/// menu whose checkable rows are all currently unchecked with no reserved column,
/// making every row's text jump right the instant one row is toggled on. A menu
/// with only a section-header icon and no checkable rows reserves nothing and
/// stays per-row inline (#16). This reconciles #16's shared-left-x with native
/// alignment.
fn menu_reserves_gutter(menu: &Menu) -> bool {
    menu.items.iter().any(|it| {
        item_row(it)
            .is_some_and(|r| r.checked.is_some() || matches!(r.leading, Some(Icon::Checkmark)))
    })
}

/// The leading advance a row consumes: the shared gutter width when the menu
/// reserves one (every row, so text aligns), else the row's own inline icon
/// advance (0 for icon-less rows).
fn row_lead(row: &Row, gap: f32, reserve_gutter: bool) -> f32 {
    if reserve_gutter {
        ICON_SIZE + gap
    } else {
        row_leading_width(row, gap)
    }
}

fn row_font(row: &Row, seg: &Segment, base: &Font) -> Font {
    if let Some(f) = &seg.font {
        f.clone()
    } else {
        let mut f = base.clone();
        // A bold-labelled row (usagio's active account) carries weight on the
        // segment font; nothing extra to do here.
        let _ = row;
        f.weight = base.weight;
        f
    }
}

/// Measure the intrinsic width a row's segments want (sum of segment widths plus
/// inter-segment gaps), using the drawer's text metrics. Weight-aware and split
/// exactly as the (un-highlighted) draw pass will render the row, so a segment
/// carrying a bolder `StyleRun` is sized at the bold advance — the popup width
/// then never under-fits the text. `base_color` is the row's resolved base color
/// (the highlight overlay is a hover-time repaint that must not resize the
/// popup, so the width pass always measures the un-highlighted split).
fn row_intrinsic<D: SceneDrawer>(
    d: &D,
    cache: &mut MeasureCache,
    row: &Row,
    base: &Font,
    base_color: Rgba,
    theme: &Theme,
    gap: f32,
) -> f32 {
    if row.segments.is_empty() {
        return 0.0;
    }
    let mut w = 0.0;
    for (i, seg) in row.segments.iter().enumerate() {
        let font = row_font(row, seg, base);
        let seg_base = seg_base_color(seg, theme, base_color, false);
        w += measure_segment(d, cache, seg, &font, theme, seg_base, false);
        if i + 1 < row.segments.len() {
            w += gap;
        }
    }
    w
}

fn item_row(item: &Item) -> Option<&Row> {
    match item {
        Item::Row(r) | Item::SectionHeader(r) => Some(r),
        Item::Submenu { label, .. } => Some(label),
        Item::Separator | Item::Content(_) => None,
    }
}

// -----------------------------------------------------------------------------
// Content stacks (issue #44): recursive measure + paint over a `Stack` tree.
// -----------------------------------------------------------------------------

/// The intrinsic (unconstrained) size of one [`Content`] node: text measured
/// through the drawer's shaper at its resolved font, an image at `size`×`size`,
/// a spacer at zero (it only absorbs slack at paint time), and a stack as the
/// recursive sum-along-axis / max-across-axis of its own children.
fn measure_content<D: SceneDrawer>(
    drawer: &D,
    cache: &mut MeasureCache,
    theme: &Theme,
    content: &Content,
) -> LogicalSize {
    match content {
        Content::Text(t) => {
            let font = t.font.clone().unwrap_or_else(|| theme.row_font.clone());
            let w = measure_cached(drawer, cache, &t.text, &font);
            let h = drawer.line_height(&font);
            LogicalSize::new(w, h)
        }
        Content::Image { size, .. } => LogicalSize::new(*size, *size),
        Content::Stack(stack) => measure_stack(drawer, cache, theme, stack),
        Content::Spacer => LogicalSize::new(0.0, 0.0),
    }
}

/// The intrinsic size of a [`Stack`]: children summed (plus inter-child
/// `spacing`) along `axis`, maxed across the cross axis.
fn measure_stack<D: SceneDrawer>(
    drawer: &D,
    cache: &mut MeasureCache,
    theme: &Theme,
    stack: &Stack,
) -> LogicalSize {
    let mut main = 0.0_f32;
    let mut cross = 0.0_f32;
    for (i, child) in stack.children.iter().enumerate() {
        let sz = measure_content(drawer, cache, theme, child);
        let (m, c) = match stack.axis {
            Axis::Horizontal => (sz.width, sz.height),
            Axis::Vertical => (sz.height, sz.width),
        };
        main += m;
        if i + 1 < stack.children.len() {
            main += stack.spacing;
        }
        cross = cross.max(c);
    }
    match stack.axis {
        Axis::Horizontal => LogicalSize::new(main, cross),
        Axis::Vertical => LogicalSize::new(cross, main),
    }
}

/// Paint one [`Content`] node into `rect` (already resolved by the parent
/// stack's layout pass). `base_color` is the row's default text color (used
/// when a [`crate::menu::TextContent`] carries no explicit color override).
fn paint_content<D: SceneDrawer>(
    drawer: &mut D,
    cache: &mut MeasureCache,
    theme: &Theme,
    content: &Content,
    rect: LogicalRect,
    base_color: Rgba,
) {
    match content {
        Content::Text(t) => {
            let font = t.font.clone().unwrap_or_else(|| theme.row_font.clone());
            let color = t.color.map(|c| theme.resolve(c)).unwrap_or(base_color);
            let w = measure_cached(drawer, cache, &t.text, &font);
            let lh = drawer.line_height(&font);
            let x = match t.align {
                Align::Left => rect.origin.x,
                Align::Center => rect.origin.x + (rect.size.width - w) / 2.0,
                Align::Right => rect.origin.x + (rect.size.width - w),
            };
            let y = rect.origin.y + (rect.size.height - lh) / 2.0;
            drawer.draw_text(&TextRun {
                text: &t.text,
                origin: LogicalPoint::new(x, y),
                font: &font,
                color,
                weight: font.weight,
            });
        }
        Content::Image { icon, size } => {
            let x = rect.origin.x + (rect.size.width - size) / 2.0;
            let y = rect.origin.y + (rect.size.height - size) / 2.0;
            let dest = LogicalRect::new(LogicalPoint::new(x, y), LogicalSize::new(*size, *size));
            match icon {
                // Both raster and SVG icon bytes converge on the same decoded-icon
                // blit path (`decode_icon` tries PNG then falls back to the SVG
                // rasterizer; see `crate::render::decode_icon_bytes`).
                Icon::Png(bytes) | Icon::Svg(bytes) => {
                    if let Some(decoded) = drawer.decode_icon(bytes) {
                        let (rgba, w, h) = &*decoded;
                        drawer.draw_image(rgba, *w, *h, dest);
                    }
                }
                Icon::Checkmark => draw_glyph_centered(
                    drawer,
                    cache,
                    "\u{2713}",
                    &theme.row_font,
                    Weight::Bold,
                    base_color,
                    dest,
                ),
                // A named symbol has no bundled fallback glyph in the content-stack
                // path yet (Phase 2, same limit `draw_row_content` documents for
                // its own leading-icon column); skip rather than draw nothing
                // useful.
                Icon::Symbol(_) => {}
            }
        }
        Content::Stack(s) => paint_stack(drawer, cache, theme, s, rect, base_color),
        Content::Spacer => {}
    }
}

/// Lay out and paint a [`Stack`]'s children into `rect`: each child gets its
/// intrinsic main-axis size (measured via [`measure_content`]) except a
/// [`Content::Spacer`], which receives an equal share of whatever main-axis
/// space is left over after fixed children and `spacing` are subtracted
/// (clamped to zero — an over-full stack simply overflows `rect`, it is never
/// negative-sized). Cross-axis position honors `stack.align`.
fn paint_stack<D: SceneDrawer>(
    drawer: &mut D,
    cache: &mut MeasureCache,
    theme: &Theme,
    stack: &Stack,
    rect: LogicalRect,
    base_color: Rgba,
) {
    if stack.children.is_empty() {
        return;
    }
    let sizes: Vec<LogicalSize> = stack
        .children
        .iter()
        .map(|c| measure_content(drawer, cache, theme, c))
        .collect();

    let n = stack.children.len();
    let spacing_total = stack.spacing * (n.saturating_sub(1)) as f32;
    let main_avail = match stack.axis {
        Axis::Horizontal => rect.size.width,
        Axis::Vertical => rect.size.height,
    } - spacing_total;
    let cross_avail = match stack.axis {
        Axis::Horizontal => rect.size.height,
        Axis::Vertical => rect.size.width,
    };

    let mut fixed_main_sum = 0.0_f32;
    let mut spacer_count = 0usize;
    for (child, sz) in stack.children.iter().zip(&sizes) {
        if matches!(child, Content::Spacer) {
            spacer_count += 1;
        } else {
            fixed_main_sum += match stack.axis {
                Axis::Horizontal => sz.width,
                Axis::Vertical => sz.height,
            };
        }
    }
    let leftover = (main_avail - fixed_main_sum).max(0.0);
    let spacer_share = if spacer_count > 0 {
        leftover / spacer_count as f32
    } else {
        0.0
    };

    let cross_origin = match stack.axis {
        Axis::Horizontal => rect.origin.y,
        Axis::Vertical => rect.origin.x,
    };
    let mut main_pos = match stack.axis {
        Axis::Horizontal => rect.origin.x,
        Axis::Vertical => rect.origin.y,
    };

    for (child, sz) in stack.children.iter().zip(&sizes) {
        let (m, c) = match stack.axis {
            Axis::Horizontal => (sz.width, sz.height),
            Axis::Vertical => (sz.height, sz.width),
        };
        let this_main = if matches!(child, Content::Spacer) {
            spacer_share
        } else {
            m
        };
        let cross_pos = match stack.align {
            Align::Left => cross_origin,
            Align::Center => cross_origin + (cross_avail - c) / 2.0,
            Align::Right => cross_origin + (cross_avail - c),
        };
        let child_rect = match stack.axis {
            Axis::Horizontal => LogicalRect::new(
                LogicalPoint::new(main_pos, cross_pos),
                LogicalSize::new(this_main, c),
            ),
            Axis::Vertical => LogicalRect::new(
                LogicalPoint::new(cross_pos, main_pos),
                LogicalSize::new(c, this_main),
            ),
        };
        paint_content(drawer, cache, theme, child, child_rect, base_color);
        main_pos += this_main + stack.spacing;
    }
}

/// Split a segment's text into consecutive styled pieces by its UTF-16
/// [`StyleRun`](crate::StyleRun) spans, falling back to `base_color`/`base_weight`.
///
/// `StyleRun` colors resolve against the **live** `theme` so a semantic run color
/// (`Label`/`SecondaryLabel`/`Accent`/`Separator`) is correct in dark mode, not
/// baked against a hard-coded light palette (spec §7.3).
///
/// When `highlighted` (the row is filled with the accent and every other glyph —
/// label, checkmark, chevron — is forced to `base_color`, i.e. white), a run's
/// semantic color is **suppressed** so styled runs invert with the rest of the
/// row instead of rendering, say, saturated red on accent blue. Per-run *weight*
/// overrides still apply in both states.
fn style_pieces(
    seg: &Segment,
    base_color: Rgba,
    base_weight: Weight,
    theme: &Theme,
    highlighted: bool,
) -> Vec<(String, Rgba, Weight)> {
    if seg.runs.is_empty() {
        return vec![(seg.text.clone(), base_color, base_weight)];
    }
    // Map each char to (color, weight) by walking UTF-16 offsets.
    let mut pieces: Vec<(String, Rgba, Weight)> = Vec::new();
    let mut u16_idx = 0usize;
    for ch in seg.text.chars() {
        let mut color = base_color;
        let mut weight = base_weight;
        for run in &seg.runs {
            // `start`/`len` are public `StyleRun` fields set by the caller;
            // saturating add so a pathological `start + len` can't overflow
            // `usize` and panic under debug overflow checks.
            if u16_idx >= run.start && u16_idx < run.start.saturating_add(run.len) {
                if !highlighted {
                    color = theme.resolve(run.color);
                }
                if let Some(w) = run.weight {
                    weight = w;
                }
                break;
            }
        }
        match pieces.last_mut() {
            Some((s, c, w)) if *c == color && *w == weight => s.push(ch),
            _ => pieces.push((ch.to_string(), color, weight)),
        }
        u16_idx += ch.len_utf16();
    }
    pieces
}

/// The effective base color for a segment's un-styled chars, matching what the
/// draw pass uses: the highlight override (white) wins; otherwise an explicit
/// per-segment color; else the row's base color. Shared by the width pass, the
/// draw-pass metrics, and the draw loop so all three split into *identical*
/// pieces (piece boundaries depend on this color, and a different split changes
/// the summed width via cross-piece kerning).
fn seg_base_color(seg: &Segment, theme: &Theme, base_color: Rgba, highlighted: bool) -> Rgba {
    if highlighted {
        Rgba::WHITE
    } else if let Some(c) = seg.color {
        theme.resolve(c)
    } else {
        base_color
    }
}

/// The rendered width of a segment, measured the *same way it is drawn*: each
/// `StyleRun` weight override changes glyph advances, so the width is the sum of
/// the per-piece advances at each piece's weight — not the whole string measured
/// once at the base weight (which under-sizes a segment containing a bolder run
/// and lets a right-aligned / flex run overflow its box). `base_color` and
/// `highlighted` must be the same values the draw pass will use for this segment
/// so the piece split — and hence the summed width — is identical to the drawn
/// advance.
fn measure_segment<D: SceneDrawer>(
    drawer: &D,
    cache: &mut MeasureCache,
    seg: &Segment,
    font: &Font,
    theme: &Theme,
    base_color: Rgba,
    highlighted: bool,
) -> f32 {
    style_pieces(seg, base_color, font.weight, theme, highlighted)
        .iter()
        .map(|(text, _color, weight)| {
            let pf = font.clone().with_weight(*weight);
            measure_cached(drawer, cache, text, &pf)
        })
        .sum()
}

/// Lay out and paint `menu` through `drawer`, highlighting the top-level item at
/// `highlight` (usually the row under the cursor). Returns the popup size and
/// clickable row rectangles for hit-testing.
pub fn render_menu<D: SceneDrawer>(
    drawer: &mut D,
    menu: &Menu,
    theme: &Theme,
    opts: &MenuOptions,
    highlight: Option<usize>,
) -> LaidMenu {
    let pad = theme.padding;
    let gap = theme.column_gap;
    let base_font = theme.row_font.clone();
    // Reserve a shared leading gutter per the caller's policy (#43): `Auto` (the
    // OEM default) reserves it only when the menu has checkmarks so checked and
    // unchecked rows align (#16 reconciliation); `Always`/`Never` force it.
    let reserve_gutter = match opts.gutter {
        crate::theme::GutterPolicy::Always => true,
        crate::theme::GutterPolicy::Never => false,
        crate::theme::GutterPolicy::Auto => menu_reserves_gutter(menu),
    };

    let trailing_w = if menu.items.iter().any(is_submenu) {
        TRAILING_COLUMN
    } else {
        0.0
    };

    // Scratch text-measurement memo, cleared every call (see `MeasureCache`).
    let mut measure_cache: MeasureCache = HashMap::new();

    // ---- Pass 1: width & height ----
    let mut max_content = 0.0_f32;
    for item in &menu.items {
        if let Some(row) = item_row(item) {
            let is_header = matches!(item, Item::SectionHeader(_));
            let font = if is_header {
                &theme.header_font
            } else {
                &base_font
            };
            // The row's un-highlighted base color, matching draw_row_content so
            // the width pass splits into the same pieces the draw pass advances.
            let base_color = if is_header || !row.enabled {
                theme.resolve(theme.secondary_label)
            } else {
                theme.resolve(theme.label)
            };
            // The row's content width: its leading advance (the shared gutter
            // when reserved, else its own inline icon) plus its segments.
            let content = row_lead(row, gap, reserve_gutter)
                + row_intrinsic(
                    drawer,
                    &mut measure_cache,
                    row,
                    font,
                    base_color,
                    theme,
                    gap,
                );
            max_content = max_content.max(content);
        } else if let Item::Content(stack) = item {
            let stack_size = measure_stack(drawer, &mut measure_cache, theme, stack);
            max_content = max_content.max(stack_size.width);
        }
    }

    let min_w = opts.min_width.unwrap_or(DEFAULT_MIN_WIDTH);
    // `f32::clamp` panics if `min > max`; a consumer can set `min_width >
    // max_width`, so normalize by letting the floor win (#36).
    let max_w = opts.max_width.unwrap_or(DEFAULT_MAX_WIDTH).max(min_w);
    let desired = pad.left + max_content + trailing_w + pad.right;
    let width = desired.clamp(min_w, max_w);

    // Every row's content starts at the same left x (`content_left`); a row with a
    // leading icon draws it here and offsets only its own text. `band_right` is
    // the shared right edge segments right-align to (before the submenu column).
    let content_left = pad.left;
    let band_right = width - pad.right - trailing_w;

    let mut y = pad.top;
    let mut rows: Vec<LaidRow> = Vec::new();
    let mut plan: Vec<(usize, f32, f32)> = Vec::new(); // (item index, y, height)
    for (i, item) in menu.items.iter().enumerate() {
        let h = match item {
            Item::Separator => SEPARATOR_HEIGHT,
            Item::SectionHeader(_) => theme.row_height,
            Item::Row(r) | Item::Submenu { label: r, .. } => {
                theme.row_height.max(r.min_height.unwrap_or(0.0))
            }
            Item::Content(stack) => {
                let stack_size = measure_stack(drawer, &mut measure_cache, theme, stack);
                theme
                    .row_height
                    .max(stack_size.height + CONTENT_ROW_VPAD * 2.0)
            }
        };
        plan.push((i, y, h));
        y += h;
    }
    let height = y + pad.bottom;
    let size = LogicalSize::new(width, height);

    // ---- Pass 2: draw ----
    drawer.begin_frame(size);
    let bg = theme.resolve(theme.background);
    drawer.fill_round_rect(
        LogicalRect::new(LogicalPoint::new(0.0, 0.0), size),
        theme.corner_radius,
        bg,
    );

    for (i, ry, rh) in plan {
        let item = &menu.items[i];
        match item {
            Item::Separator => {
                let sep = LogicalRect::new(
                    LogicalPoint::new(pad.left, ry + rh / 2.0),
                    LogicalSize::new(width - pad.horizontal(), 1.0),
                );
                drawer.draw_separator(sep, theme.resolve(theme.separator));
            }
            Item::SectionHeader(row) => {
                draw_row_content(
                    drawer,
                    &mut measure_cache,
                    row,
                    theme,
                    &theme.header_font,
                    content_left,
                    band_right,
                    reserve_gutter,
                    ry,
                    rh,
                    false,
                    false,
                    theme.resolve(theme.secondary_label),
                );
            }
            Item::Content(stack) => {
                if let Some(bg) = stack.background {
                    let band =
                        LogicalRect::new(LogicalPoint::new(0.0, ry), LogicalSize::new(width, rh));
                    drawer.fill_round_rect(band, 0.0, theme.resolve(bg));
                }
                let content_rect = LogicalRect::new(
                    LogicalPoint::new(content_left, ry + CONTENT_ROW_VPAD),
                    LogicalSize::new(
                        (band_right - content_left).max(1.0),
                        (rh - CONTENT_ROW_VPAD * 2.0).max(1.0),
                    ),
                );
                paint_stack(
                    drawer,
                    &mut measure_cache,
                    theme,
                    stack,
                    content_rect,
                    theme.resolve(theme.label),
                );
            }
            Item::Row(row) | Item::Submenu { label: row, .. } => {
                let submenu = is_submenu(item);
                let interactive = if submenu {
                    row.enabled
                } else {
                    row.enabled && !row.id.is_none()
                };
                let highlighted = interactive && highlight == Some(i);
                let rect =
                    LogicalRect::new(LogicalPoint::new(0.0, ry), LogicalSize::new(width, rh));
                if highlighted {
                    let hl = LogicalRect::new(
                        LogicalPoint::new(pad.left - 2.0, ry + 1.0),
                        LogicalSize::new(width - pad.horizontal() + 4.0, rh - 2.0),
                    );
                    drawer.fill_round_rect(hl, 5.0, theme.resolve(theme.accent));
                }
                let base_color = if highlighted {
                    Rgba::WHITE
                } else if !row.enabled {
                    theme.resolve(theme.secondary_label)
                } else {
                    theme.resolve(theme.label)
                };
                draw_row_content(
                    drawer,
                    &mut measure_cache,
                    row,
                    theme,
                    &base_font,
                    content_left,
                    band_right,
                    reserve_gutter,
                    ry,
                    rh,
                    submenu,
                    highlighted,
                    base_color,
                );
                rows.push(LaidRow {
                    index: i,
                    rect,
                    id: row.id.clone(),
                    interactive,
                });
            }
        }
    }

    LaidMenu { size, rows }
}

#[allow(clippy::too_many_arguments)]
fn draw_row_content<D: SceneDrawer>(
    drawer: &mut D,
    cache: &mut MeasureCache,
    row: &Row,
    theme: &Theme,
    base_font: &Font,
    content_left: f32,
    band_right: f32,
    reserve_gutter: bool,
    ry: f32,
    rh: f32,
    submenu: bool,
    highlighted: bool,
    base_color: Rgba,
) {
    // Leading advance: the shared gutter width when the menu reserves one (so
    // checked + unchecked rows align, native look), else this row's own inline
    // icon advance (#16). The icon/checkmark still draws at `content_left`.
    let lead = row_lead(row, theme.column_gap, reserve_gutter);
    let band_x = content_left + lead;
    let band_w = (band_right - band_x).max(1.0);
    if lead > 0.0 {
        let icon_rect = LogicalRect::new(
            LogicalPoint::new(content_left, ry + (rh - ICON_SIZE) / 2.0),
            LogicalSize::new(ICON_SIZE, ICON_SIZE),
        );
        match &row.leading {
            Some(Icon::Png(bytes)) => {
                if let Some(decoded) = drawer.decode_icon(bytes) {
                    let (rgba, w, h) = &*decoded;
                    drawer.draw_image(rgba, *w, *h, icon_rect);
                }
            }
            Some(Icon::Checkmark) => draw_glyph_centered(
                drawer,
                cache,
                "\u{2713}",
                base_font,
                Weight::Bold,
                if highlighted {
                    Rgba::WHITE
                } else {
                    theme.resolve(theme.accent)
                },
                icon_rect,
            ),
            // SVG / Symbol icons are Phase 2 (rasterize per-DPI); skip for now.
            _ => {
                if row.checked == Some(true) {
                    draw_glyph_centered(
                        drawer,
                        cache,
                        "\u{2713}",
                        base_font,
                        Weight::Bold,
                        if highlighted {
                            Rgba::WHITE
                        } else {
                            theme.resolve(theme.accent)
                        },
                        icon_rect,
                    );
                }
            }
        }
    }

    // Segment band.
    if !row.segments.is_empty() {
        let metrics: Vec<SegmentMetrics> = row
            .segments
            .iter()
            .map(|seg| {
                let font = row_font(row, seg, base_font);
                let seg_base = seg_base_color(seg, theme, base_color, highlighted);
                SegmentMetrics::new(
                    measure_segment(drawer, cache, seg, &font, theme, seg_base, highlighted),
                    seg.flex,
                    seg.align,
                )
            })
            .collect();
        let boxes = resolve_segments(&metrics, band_w);
        for (seg, bx) in row.segments.iter().zip(boxes.iter()) {
            let font = row_font(row, seg, base_font);
            let lh = drawer.line_height(&font);
            let text_top = ry + (rh - lh) / 2.0;
            let seg_base = seg_base_color(seg, theme, base_color, highlighted);
            let pieces = style_pieces(seg, seg_base, font.weight, theme, highlighted);
            let mut px = band_x + bx.text_x;
            for (text, color, weight) in pieces {
                let pf = font.clone().with_weight(weight);
                let w = measure_cached(drawer, cache, &text, &pf);
                drawer.draw_text(&TextRun {
                    text: &text,
                    origin: LogicalPoint::new(px, text_top),
                    font: &pf,
                    color,
                    weight,
                });
                px += w;
            }
        }
    }

    // Trailing submenu chevron.
    if submenu {
        let chev_rect = LogicalRect::new(
            LogicalPoint::new(band_x + band_w, ry),
            LogicalSize::new(TRAILING_COLUMN, rh),
        );
        draw_glyph_centered(
            drawer,
            cache,
            "\u{203A}", //            base_font,
            Weight::Regular,
            if highlighted {
                Rgba::WHITE
            } else {
                theme.resolve(theme.secondary_label)
            },
            chev_rect,
        );
    }
}

#[allow(clippy::too_many_arguments)]
fn draw_glyph_centered<D: SceneDrawer>(
    drawer: &mut D,
    cache: &mut MeasureCache,
    glyph: &str,
    base_font: &Font,
    weight: Weight,
    color: Rgba,
    rect: LogicalRect,
) {
    let font = base_font.clone().with_weight(weight);
    let w = measure_cached(drawer, cache, glyph, &font);
    let lh = drawer.line_height(&font);
    let origin = LogicalPoint::new(
        rect.origin.x + (rect.size.width - w) / 2.0,
        rect.origin.y + (rect.size.height - lh) / 2.0,
    );
    drawer.draw_text(&TextRun {
        text: glyph,
        origin,
        font: &font,
        color,
        weight,
    });
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::menu::{Align, Flex, StyleRun, TextContent};
    use crate::style::Color;
    use std::sync::Arc;

    fn demo_menu() -> Menu {
        Menu::new()
            .section_header(Row::info().label("Claude"))
            .row(
                Row::new("switch:claude:me")
                    .leading(Icon::Checkmark)
                    .checked(true)
                    .segments(vec![
                        Segment::new("me@example.com")
                            .flex(Flex::Grow)
                            .font(Font::system(13.0, Weight::Bold)),
                        Segment::new("47% / 89%")
                            .align(Align::Right)
                            .runs(vec![StyleRun::new(6, 3, Color::SystemRed)]),
                    ]),
            )
            .separator()
            .submenu(Row::new("settings").label("Settings"), Menu::new())
            .row(Row::new("quit").segments(vec![
                Segment::new("Quit").flex(Flex::Grow),
                Segment::new("usagio v1")
                    .align(Align::Right)
                    .color(Color::SecondaryLabel),
            ]))
    }

    #[test]
    fn repeated_render_reuses_shaped_runs_no_reshaping() {
        // A hover-highlight repaints unchanged text; after the first render, a
        // second identical render must re-shape nothing (all runs served from the
        // shaped cache) — the fix for the ~0.5s hover latency. Instruments the
        // real shape() miss counter rather than wall-clock (non-flaky).
        let mut d = crate::render::RasterDrawer::new(2.0);
        let menu = demo_menu();
        let _ = render_menu(&mut d, &menu, &Theme::dark(), &MenuOptions::default(), None);
        let after_first = d.shape_miss_count();
        assert!(after_first > 0, "the first render shapes its runs");
        // Re-render the same menu with a different highlight (a hover change).
        let _ = render_menu(
            &mut d,
            &menu,
            &Theme::dark(),
            &MenuOptions::default(),
            Some(0),
        );
        assert_eq!(
            d.shape_miss_count(),
            after_first,
            "a repaint of unchanged text must not re-shape any run"
        );
    }

    #[test]
    fn render_produces_nonempty_size_and_hits() {
        let mut d = crate::render::RasterDrawer::new(2.0);
        let laid = render_menu(
            &mut d,
            &demo_menu(),
            &Theme::dark(),
            &MenuOptions::default(),
            None,
        );
        assert!(laid.size.width > 100.0 && laid.size.height > 60.0);
        // Interactive rows: the account, the submenu, and quit (not header/sep).
        assert_eq!(laid.rows.iter().filter(|r| r.interactive).count(), 3);
        // Device pixmap matches logical size * scale, and is non-blank.
        let (dw, dh) = d.device_size();
        assert_eq!(dw, (laid.size.width * 2.0).round() as u32);
        assert_eq!(dh, (laid.size.height * 2.0).round() as u32);
        let any_opaque = d.framebuffer().pixels().chunks_exact(4).any(|p| p[3] > 0);
        assert!(
            any_opaque,
            "rendered pixmap should not be fully transparent"
        );
    }

    /// Regression for spec §7.3: a `StyleRun` carrying a **semantic** color must
    /// resolve against the *active* theme, not a hard-coded `Theme::light()`, so
    /// a themed run reads correctly in dark mode. `Color::Label` is white on dark
    /// and black on light; the old seam returned black in both.
    #[test]
    fn style_run_semantic_color_follows_active_theme() {
        // A two-char segment whose second char is a `Color::Label` style run.
        let seg = Segment::new("ab").runs(vec![StyleRun::new(1, 1, Color::Label)]);
        let base = Rgba::opaque(1, 2, 3);

        let dark = style_pieces(&seg, base, Weight::Regular, &Theme::dark(), false);
        let light = style_pieces(&seg, base, Weight::Regular, &Theme::light(), false);

        // The styled piece ("b") resolves Label against each theme.
        let dark_b = dark.iter().find(|(s, ..)| s == "b").expect("styled piece");
        let light_b = light.iter().find(|(s, ..)| s == "b").expect("styled piece");
        assert_eq!(dark_b.1, Theme::dark().resolve(Color::Label));
        assert_eq!(light_b.1, Theme::light().resolve(Color::Label));
        // And they actually differ (white vs black), proving the theme is live.
        assert_ne!(dark_b.1, light_b.1);
    }

    /// On a highlighted (accent-filled) row every glyph inverts to the base
    /// color; a `StyleRun`'s semantic color must be suppressed so it doesn't
    /// render, e.g., saturated red on accent blue — but weight overrides stay.
    #[test]
    fn style_run_color_is_suppressed_when_highlighted_but_weight_is_kept() {
        let seg = Segment::new("ab").runs(vec![
            StyleRun::new(1, 1, Color::SystemRed).weight(Weight::Bold)
        ]);
        let white = Rgba::WHITE;

        let hot = style_pieces(&seg, white, Weight::Regular, &Theme::light(), true);
        for (_s, c, _w) in &hot {
            assert_eq!(
                *c, white,
                "highlighted row keeps every piece at the base color"
            );
        }
        let hot_b = hot.iter().find(|(s, ..)| s == "b").expect("styled piece");
        assert_eq!(hot_b.2, Weight::Bold, "weight override survives highlight");

        // Un-highlighted, the semantic color resolves as before.
        let cold = style_pieces(&seg, white, Weight::Regular, &Theme::light(), false);
        let cold_b = cold.iter().find(|(s, ..)| s == "b").expect("styled piece");
        assert_eq!(cold_b.1, Theme::light().resolve(Color::SystemRed));
    }

    /// A segment carrying a bolder `StyleRun` must be measured at the run's
    /// weight, not the base weight, so its box matches what `draw_row_content`
    /// actually advances (else a right-aligned run overflows). The measured
    /// width equals the sum of the per-piece advances the draw path uses.
    #[test]
    fn measure_segment_accounts_for_per_run_weight() {
        let d = crate::render::RasterDrawer::new_headless(1.0);
        let mut cache = MeasureCache::new();
        let font = Font::default();
        let theme = Theme::light();

        let seg =
            Segment::new("Quit").runs(vec![StyleRun::new(0, 4, Color::Label).weight(Weight::Bold)]);
        let measured = measure_segment(&d, &mut cache, &seg, &font, &theme, Rgba::WHITE, false);

        // Same computation the draw loop performs, piece by piece.
        let drawn: f32 = style_pieces(&seg, Rgba::WHITE, font.weight, &theme, false)
            .iter()
            .map(|(t, _c, w)| measure_cached(&d, &mut cache, t, &font.clone().with_weight(*w)))
            .sum();
        assert_eq!(measured, drawn);

        // And it is never narrower than the naive base-weight measure the old
        // code used (bold advances are >= regular for the vendored face).
        let naive = measure_cached(&d, &mut cache, &seg.text, &font);
        assert!(measured >= naive);
    }

    #[test]
    fn hit_testing_maps_points_to_rows() {
        let mut d = crate::render::RasterDrawer::new(1.0);
        let laid = render_menu(
            &mut d,
            &demo_menu(),
            &Theme::light(),
            &MenuOptions::default(),
            None,
        );
        let account = &laid.rows[0];
        let center = LogicalPoint::new(
            account.rect.origin.x + account.rect.size.width / 2.0,
            account.rect.origin.y + account.rect.size.height / 2.0,
        );
        assert_eq!(laid.hit(center), Some(account.index));
        assert_eq!(laid.id_at(center).unwrap().as_str(), "switch:claude:me");
    }

    /// `measure_cached`'s doc promise ("output is identical to calling
    /// `drawer.measure_text` directly every time") is what makes the
    /// per-frame memo safe: prove both the cold path (first call, a miss)
    /// and the warm path (second call, a hit) return exactly what a direct,
    /// uncached `measure_text` call would — i.e. the memoization can't drift
    /// from the ground truth it's short-circuiting.
    #[test]
    fn measure_cached_matches_a_direct_measure_text_call() {
        let d = crate::render::RasterDrawer::new_headless(1.0);
        let font = Font::system(13.0, Weight::Regular);
        let text = "Settings";

        let direct = d.measure_text(text, &font);

        let mut cache: MeasureCache = HashMap::new();
        let cold = measure_cached(&d, &mut cache, text, &font);
        assert_eq!(
            cold, direct,
            "first (cache-miss) call must match measure_text"
        );
        assert_eq!(cache.len(), 1);

        let warm = measure_cached(&d, &mut cache, text, &font);
        assert_eq!(
            warm, direct,
            "second (cache-hit) call must still match measure_text"
        );
        assert_eq!(
            cache.len(),
            1,
            "a repeat (text, font) must not grow the cache"
        );
    }

    /// Distinct `(text, font)` keys must not collide in the cache — a
    /// different font size for the same text is a different measurement and
    /// must get its own entry and its own (independently correct) value.
    #[test]
    fn measure_cached_distinguishes_different_fonts_for_the_same_text() {
        let d = crate::render::RasterDrawer::new_headless(1.0);
        let text = "Settings";
        let small = Font::system(11.0, Weight::Regular);
        let large = Font::system(22.0, Weight::Regular);

        let mut cache: MeasureCache = HashMap::new();
        let w_small = measure_cached(&d, &mut cache, text, &small);
        let w_large = measure_cached(&d, &mut cache, text, &large);

        assert_eq!(w_small, d.measure_text(text, &small));
        assert_eq!(w_large, d.measure_text(text, &large));
        assert!(w_large > w_small, "a larger font must measure wider");
        assert_eq!(cache.len(), 2);
    }

    /// A drawer that records the geometry of every draw op, with deterministic
    /// monospace metrics (7px/char, 14px line height) so layout is exactly
    /// reproducible in a test. Icons decode to a 2x2 stub without touching the
    /// PNG codec.
    #[derive(Default)]
    struct RecordingDrawer {
        texts: Vec<(String, f32, f32)>, // (text, origin.x, origin.y)
        images: Vec<LogicalRect>,       // dest rects of draw_image
    }
    impl SceneDrawer for RecordingDrawer {
        fn begin_frame(&mut self, _size: LogicalSize) {}
        fn fill_round_rect(&mut self, _r: LogicalRect, _cr: f32, _c: Rgba) {}
        fn draw_separator(&mut self, _r: LogicalRect, _c: Rgba) {}
        fn measure_text(&self, text: &str, _font: &Font) -> f32 {
            text.chars().count() as f32 * 7.0
        }
        fn line_height(&self, _font: &Font) -> f32 {
            14.0
        }
        fn draw_text(&mut self, run: &TextRun<'_>) {
            self.texts
                .push((run.text.to_string(), run.origin.x, run.origin.y));
        }
        fn draw_image(&mut self, _rgba: &[u8], _w: u32, _h: u32, dest: LogicalRect) {
            self.images.push(dest);
        }
        fn decode_icon(&self, _bytes: &Arc<[u8]>) -> Option<crate::render::DecodedIcon> {
            Some(std::rc::Rc::new((vec![0u8; 16], 2, 2)))
        }
    }

    /// Right edge (`origin.x + measured width`) of the draw_text op whose text
    /// matches `needle`, using the same 7px/char metric the drawer reports.
    fn text_right_edge(d: &RecordingDrawer, needle: &str) -> f32 {
        let (t, x, _) = d
            .texts
            .iter()
            .find(|(t, ..)| t == needle)
            .unwrap_or_else(|| panic!("no draw_text for {needle:?}; got {:?}", d.texts));
        x + t.chars().count() as f32 * 7.0
    }

    /// Regression for #16: no global leading gutter. A menu mixing an icon row
    /// with plain text rows must start every row's content at the SAME left x —
    /// the icon sits at that x (inline) and plain rows are NOT indented past it.
    #[test]
    fn issue16_no_global_leading_gutter_shared_left_x() {
        use std::sync::Arc;
        let logo: Arc<[u8]> = Arc::from(vec![0u8; 8]);
        let menu = Menu::new()
            .row(
                Row::new("hdr")
                    .leading(Icon::Png(logo.clone()))
                    .label("Claude")
                    .enabled(false),
            )
            .row(Row::new("acct").label("matthew@example.com"))
            .row(Row::new("quit").label("Quit"));

        let mut d = RecordingDrawer::default();
        let _ = render_menu(&mut d, &menu, &Theme::dark(), &MenuOptions::default(), None);

        // The icon is drawn at the shared left x.
        assert_eq!(d.images.len(), 1, "one inline icon");
        let icon_x = d.images[0].origin.x;

        // Plain rows' text starts at the same left x as the icon — NOT indented
        // past a reserved gutter.
        let acct_x = d
            .texts
            .iter()
            .find(|(t, ..)| t == "matthew@example.com")
            .map(|(_, x, _)| *x)
            .expect("account row text");
        let quit_x = d
            .texts
            .iter()
            .find(|(t, ..)| t == "Quit")
            .map(|(_, x, _)| *x)
            .expect("quit row text");

        assert!(
            (acct_x - icon_x).abs() < 0.5,
            "plain row must start at the icon's left x, not indented: icon={icon_x} acct={acct_x}"
        );
        assert!(
            (quit_x - icon_x).abs() < 0.5,
            "every plain row shares the same left x: icon={icon_x} quit={quit_x}"
        );

        // The icon row's OWN label is offset past its icon (inline content).
        let hdr_x = d
            .texts
            .iter()
            .find(|(t, ..)| t == "Claude")
            .map(|(_, x, _)| *x)
            .expect("header text");
        assert!(
            hdr_x > icon_x + 8.0,
            "the icon row's text follows its icon inline: icon={icon_x} hdr={hdr_x}"
        );
    }

    /// OEM alignment: when a menu has a checked row, it reserves a shared gutter
    /// so the checked row's text aligns with the *unchecked* rows' text (native
    /// `NSMenu` look), instead of the checkmark pushing only its own row right.
    #[test]
    fn checkable_menu_reserves_gutter_so_rows_align() {
        let menu = Menu::new()
            .row(Row::new("a").checked(true).segments(vec![
                Segment::new("me@example.com").flex(Flex::Grow),
                Segment::new("47%").align(Align::Right),
            ]))
            .row(Row::new("b").segments(vec![
                Segment::new("you@example.com").flex(Flex::Grow),
                Segment::new("20%").align(Align::Right),
            ]));

        let mut d = RecordingDrawer::default();
        let _ = render_menu(&mut d, &menu, &Theme::dark(), &MenuOptions::default(), None);

        let x = |needle: &str| {
            d.texts
                .iter()
                .find(|(t, ..)| t == needle)
                .map(|(_, x, _)| *x)
                .unwrap_or_else(|| panic!("no text {needle:?}"))
        };
        // Checked row's email aligns with the unchecked row's email (shared
        // gutter), not indented by the checkmark.
        assert!(
            (x("me@example.com") - x("you@example.com")).abs() < 0.5,
            "checked and unchecked rows must share a text left x: {} vs {}",
            x("me@example.com"),
            x("you@example.com")
        );
        // And that text starts past the gutter (a checkmark glyph was drawn at
        // the left).
        assert!(x("you@example.com") > 6.0);
    }

    /// Regression: a menu whose checkable rows are ALL currently *unchecked*
    /// (`checked(false)`) must still reserve the shared gutter — `Row::checked`'s
    /// contract is "`Some(true/false)` shows a check column". Testing only
    /// `Some(true)` (the pre-fix bug) reserved nothing here, so the rows' text
    /// sat flush-left and every row jumped right the instant one was toggled on.
    #[test]
    fn all_unchecked_checkable_menu_still_reserves_gutter() {
        let text_x = |checked: bool| {
            let menu = Menu::new()
                .row(Row::new("a").checked(checked).label("Alpha"))
                .row(Row::new("b").checked(checked).label("Beta"));
            let mut d = RecordingDrawer::default();
            let _ = render_menu(&mut d, &menu, &Theme::dark(), &MenuOptions::default(), None);
            d.texts
                .iter()
                .find(|(t, ..)| t == "Alpha")
                .map(|(_, x, _)| *x)
                .expect("Alpha text")
        };
        // The gutter is reserved regardless of the current on/off state, so text
        // starts at the same x whether the checkable rows are on or off — no jump
        // on toggle. (With the bug, the all-unchecked menu reserved nothing and
        // its text x was smaller.)
        let off = text_x(false);
        let on = text_x(true);
        assert!(
            (off - on).abs() < 0.5,
            "checkable rows must reserve the gutter whether checked or not: off={off} on={on}"
        );
        assert!(
            off > 6.0,
            "an all-unchecked checkable menu must still reserve the check column, text x={off}"
        );
    }

    /// Regression for #15: a menu mixing icon-bearing section headers with
    /// `label\tvalue` rows must (1) draw every leading icon at the same left
    /// gutter x (never trailing), and (2) right-align each `\t` value to one
    /// shared column, for both `Row` and `Submenu` items.
    #[test]
    fn issue15_leading_icons_and_tab_values_align_to_shared_columns() {
        let logo: Arc<[u8]> = Arc::from(vec![0u8; 8]);
        let menu = Menu::new()
            .row(
                Row::new("hdr:claude")
                    .leading(Icon::Png(logo.clone()))
                    .label("Claude")
                    .enabled(false),
            )
            .submenu(
                Row::new("acct:short").segments(vec![
                    Segment::new("a@x.com").flex(Flex::Grow),
                    Segment::new("20% / 38%").align(Align::Right),
                ]),
                Menu::new().row(Row::new("d").label("detail")),
            )
            .submenu(
                Row::new("acct:longemail").segments(vec![
                    Segment::new("demo1@example.com").flex(Flex::Grow),
                    Segment::new("47% / 52%").align(Align::Right),
                ]),
                Menu::new().row(Row::new("d2").label("detail")),
            );

        let mut d = RecordingDrawer::default();
        let _ = render_menu(&mut d, &menu, &Theme::dark(), &MenuOptions::default(), None);

        // (1) The header's leading icon is drawn in the left gutter, near x≈0,
        //     never at the right edge of the row.
        assert_eq!(d.images.len(), 1, "one leading icon drawn");
        let icon_x = d.images[0].origin.x;
        assert!(
            icon_x < 12.0,
            "leading icon must sit in the left gutter, got x={icon_x}"
        );

        // (2) Both `\t` values right-align to the same column: their right edges
        //     match despite different value/label widths.
        let r_short = text_right_edge(&d, "20% / 38%");
        let r_long = text_right_edge(&d, "47% / 52%");
        assert!(
            (r_short - r_long).abs() < 0.5,
            "tab-stop values must share a right column: short={r_short} long={r_long}"
        );
    }

    // -------------------------------------------------------------------------
    // Content stacks (issue #44)
    // -------------------------------------------------------------------------

    /// A vertical stack of 3 texts measures to: width = the widest text (the
    /// cross axis, maxed), height = the summed line heights plus inter-child
    /// spacing (the main axis) — using `RecordingDrawer`'s deterministic
    /// 7px/char, 14px-line-height metrics.
    #[test]
    fn measure_vertical_stack_of_three_texts() {
        let d = RecordingDrawer::default();
        let mut cache = MeasureCache::new();
        let theme = Theme::light();
        let stack = Stack::vertical(2.0)
            .child(Content::Text(TextContent::new("a"))) // 1 char -> 7px wide
            .child(Content::Text(TextContent::new("bb"))) // 2 chars -> 14px wide
            .child(Content::Text(TextContent::new("ccc"))); // 3 chars -> 21px wide

        let size = measure_stack(&d, &mut cache, &theme, &stack);

        // Cross axis (width) = widest child = "ccc" at 21px.
        assert_eq!(size.width, 21.0);
        // Main axis (height) = 3 * 14 (line height) + 2 * 2.0 (spacing between
        // the 3 children).
        assert_eq!(size.height, 3.0 * 14.0 + 2.0 * 2.0);
    }

    /// A horizontal strip measures to: width = summed child widths + spacing
    /// (main axis), height = the tallest child (cross axis).
    #[test]
    fn measure_horizontal_strip() {
        let d = RecordingDrawer::default();
        let mut cache = MeasureCache::new();
        let theme = Theme::light();
        let stack = Stack::horizontal(5.0)
            .child(Content::Text(TextContent::new("ab"))) // 14px wide, 14px tall
            .child(Content::Image {
                icon: Icon::Checkmark,
                size: 20.0,
            }) // 20x20
            .child(Content::Text(TextContent::new("c"))); // 7px wide, 14px tall

        let size = measure_stack(&d, &mut cache, &theme, &stack);

        // Main axis (width) = 14 + 20 + 7 + 2 * 5.0 (spacing between 3 children).
        assert_eq!(size.width, 14.0 + 20.0 + 7.0 + 2.0 * 5.0);
        // Cross axis (height) = tallest child = the 20px image.
        assert_eq!(size.height, 20.0);
    }

    /// A `Spacer` has zero intrinsic size and absorbs the leftover main-axis
    /// space at paint time, splitting it equally among sibling spacers — the
    /// same "grow" promise `Flex::Grow` makes for `Segment`s, generalized to
    /// the `Content` tree.
    #[test]
    fn spacer_distributes_leftover_space_equally() {
        let mut d = RecordingDrawer::default();
        let mut cache = MeasureCache::new();
        let theme = Theme::light();
        // "a" (7px) + spacer + "bb" (14px) + spacer, in a 100px-wide row: the
        // two spacers must split (100 - 7 - 14) = 79px evenly (39.5px each).
        let stack = Stack::horizontal(0.0)
            .child(Content::Text(TextContent::new("a")))
            .child(Content::Spacer)
            .child(Content::Text(TextContent::new("bb")))
            .child(Content::Spacer);

        let rect = LogicalRect::new(LogicalPoint::new(0.0, 0.0), LogicalSize::new(100.0, 14.0));
        paint_stack(&mut d, &mut cache, &theme, &stack, rect, Rgba::BLACK);

        let a_x = d
            .texts
            .iter()
            .find(|(t, ..)| t == "a")
            .map(|(_, x, _)| *x)
            .expect("'a' drawn");
        let bb_x = d
            .texts
            .iter()
            .find(|(t, ..)| t == "bb")
            .map(|(_, x, _)| *x)
            .expect("'bb' drawn");

        assert_eq!(a_x, 0.0, "'a' sits flush at the stack's leading edge");
        // "bb" starts after "a" (7px) plus one spacer's share (39.5px).
        assert_eq!(bb_x, 7.0 + 39.5);
        // The trailing spacer pushes the stack's total content to fill the
        // full 100px width: "bb" ends at 100 - 39.5 (its own trailing spacer).
        assert_eq!(bb_x + 2.0 * 7.0, 100.0 - 39.5);
    }

    /// A nested stack's size is the recursive sum: a horizontal strip of
    /// vertical "cells" (time/icon/temp, the Apple-Weather-extra use case)
    /// measures as wide as its cells summed, and as tall as its tallest cell.
    #[test]
    fn nested_stack_size_recurses() {
        let d = RecordingDrawer::default();
        let mut cache = MeasureCache::new();
        let theme = Theme::light();

        let cell = |time: &str, temp: &str| {
            Content::Stack(
                Stack::vertical(1.0)
                    .child(Content::Text(TextContent::new(time)))
                    .child(Content::Image {
                        icon: Icon::Checkmark,
                        size: 16.0,
                    })
                    .child(Content::Text(TextContent::new(temp))),
            )
        };
        // Each cell: width = max(time, 16, temp) widths; height = time_h + 16 +
        // temp_h + 2 * 1.0 spacing = 14 + 16 + 14 + 2 = 46.
        let strip = Stack::horizontal(3.0)
            .child(cell("1PM", "72°"))
            .child(cell("2PM", "70°"));

        let size = measure_stack(&d, &mut cache, &theme, &strip);

        let cell_height = 14.0 + 16.0 + 14.0 + 2.0 * 1.0;
        assert_eq!(size.height, cell_height, "strip height = tallest cell");
        // Each cell's width = widest of its 3 children; "1PM"/"2PM" are 3
        // chars (21px), wider than the 16px icon, so each cell is 21px wide.
        // Strip width = 2 cells + 1 gap of 3.0.
        assert_eq!(size.width, 21.0 * 2.0 + 3.0);
    }

    /// `Item::Content` integrates into `render_menu`: the row's height is
    /// derived from the stack's measured content (plus padding), it paints
    /// through the ordinary `SceneDrawer` text/image path, and it never
    /// appears in `LaidMenu::rows` (non-interactive, like `SectionHeader`).
    #[test]
    fn item_content_sizes_and_paints_through_render_menu() {
        let logo: std::sync::Arc<[u8]> = std::sync::Arc::from(vec![0u8; 8]);
        let mut d = RecordingDrawer::default();
        let menu = Menu::new().content(
            Stack::vertical(2.0)
                .child(Content::Text(TextContent::new("Weather")))
                .child(Content::Image {
                    icon: Icon::Png(logo),
                    size: 16.0,
                }),
        );
        let laid = render_menu(
            &mut d,
            &menu,
            &Theme::light(),
            &MenuOptions::default(),
            None,
        );

        // Non-interactive: no clickable row recorded for a content item.
        assert!(laid.rows.is_empty());
        // The text was actually painted.
        assert!(d.texts.iter().any(|(t, ..)| t == "Weather"));
        assert_eq!(d.images.len(), 1, "the icon image was blitted");
        // The row is tall enough to fit "Weather" (14px) + gap (2px) + the
        // 16px icon, plus the row's own vertical padding.
        let min_expected = 14.0 + 2.0 + 16.0;
        assert!(laid.size.height > min_expected);
    }
}