rust_widgets 2.9.2

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 180 widgets, touch/gesture support, i18n, and SVG-pipeline-accurate output
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
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

//! The layout-family distribution controls: `Flex`, `FlowLayout` and `WrapLayout`.
//!
//! # Why these three are separate controls
//!
//! All three place a sequence of items, but they answer three different questions, and each
//! has its own configuration vocabulary:
//!
//! * [`Flex`] distributes **weights** along one axis (CSS flexbox): grow, shrink, justify,
//!   align, and optional wrapping. Its inputs are `flex_grow` and the five `justify` /
//!   `align` modes.
//! * [`FlowLayout`] places items in **reading order** and starts a new run when the current
//!   one is full, sized by the items' own hints. Its inputs are direction, alignment, spacing
//!   and padding.
//! * [`WrapLayout`] places items of **fixed size** and breaks lines when a line is full. It
//!   publishes a separate run spacing, which a flow does not have.
//!
//! They delegate the arithmetic to the same engines a tree layout uses
//! ([`crate::layout::FlexLayout`], [`crate::layout::flow::FlowLayout`],
//! [`crate::layout::wrap::WrapLayout`]) rather than re-deriving it: the engines take
//! `ChildInfo` hints, which is exactly the channel a control's content sizes are. The
//! control therefore reports its arrangement through the same solver the host layout tree
//! uses, and the two cannot drift.
//!
//! # The item model
//!
//! Each control owns a `Vec` of [`FlexItemSpec`] — a label, the size the item asks for, and
//! (for `Flex`) its weight. The ids handed to the engine are synthetic
//! (`index + 1`, because `ObjectId` is `u64` and 0 is not used here); nothing else ever sees
//! them, and the returned rectangles are mapped back by index.

use crate::core::{Point, Rect, Size, TextDirection};
use crate::event::{Event, EventHandler};
use crate::layout::flow::{
    FlowAlignment, FlowDirection, FlowLayout as FlowEngine, FlowLayoutConfig,
};
use crate::layout::wrap::{WrapAlignment, WrapDirection, WrapLayout as WrapEngine};
use crate::layout::{AlignItems, ChildInfo, FlexDirection, FlexItem, FlexLayout, FlexWrap, Hints};
use crate::layout::{JustifyContent, Layout};
use crate::render::RenderContext;
use crate::signal::Signal1;
use crate::widget::capability::coercion::{expect_bool, expect_string, expect_u32};
use crate::widget::capability::properties_trait::{base_property_get, base_property_set};
use crate::widget::capability::types::{CapabilityAccessError, CapabilityValue};
use crate::widget::capability::WidgetProperties;
use crate::widget::container_widgets::layout_family::{paint_item_box, resolve_palette};
use crate::widget::{BaseWidget, Draw, Widget, WidgetKind};
use crate::{impl_widget_property_hooks, property_names_of};

/// One item of a distribution layout: a label, the size it asks for, and its weight.
#[derive(Debug, Clone, PartialEq)]
pub struct FlexItemSpec {
    label: String,
    size: Size,
    grow: u32,
    /// Optional real widget mounted behind this item.
    ///
    /// The distribution engine addresses items by synthetic ids, so a real child used to be
    /// unaddressable: a click could not reach it, it had no focus, and no accessibility node
    /// existed. This id is the bridge — it lets the control report the item a click landed
    /// on without changing the solver's index-based contract.
    widget_id: Option<crate::core::ObjectId>,
}

impl FlexItemSpec {
    /// A weightless item of `size`.
    pub fn new(label: &str, size: Size) -> Self {
        Self { label: label.to_string(), size, grow: 0, widget_id: None }
    }

    /// Attaches a real child id to the item.
    pub fn with_widget(mut self, widget_id: crate::core::ObjectId) -> Self {
        self.widget_id = Some(widget_id);
        self
    }

    /// The real child id, when one is attached.
    pub fn widget_id(&self) -> Option<crate::core::ObjectId> {
        self.widget_id
    }

    /// The same item with an explicit grow weight.
    pub fn with_grow(mut self, grow: u32) -> Self {
        self.grow = grow;
        self
    }

    /// The item's label.
    pub fn label(&self) -> &str {
        &self.label
    }

    /// The size the item asks for.
    pub fn size(&self) -> Size {
        self.size
    }

    /// The share of the leftover room this item claims.
    pub fn grow(&self) -> u32 {
        self.grow
    }
}

/// The id the engine is told is item `index`'s.
///
/// `ObjectId` is a `u64`; `0` is never handed out here so a zeroed rectangle (the "engine
/// produced nothing for this item" state) cannot be mistaken for item 0's answer.
fn synthetic_id(index: usize) -> u64 {
    index as u64 + 1
}

/// Maps an engine-assigned id back to its index.
fn index_of(id: u64) -> Option<usize> {
    id.checked_sub(1).map(|index| index as usize)
}

/// Calls `arrange` and collects the produced rectangles by index.
///
/// A rectangle the engine did not produce keeps the zero value, which the painter skips: an
/// item the solver could not place has no ink to contribute.
fn collect_rects(count: usize, arrange: impl FnOnce(&mut dyn FnMut(u64, Rect))) -> Vec<Rect> {
    let mut rects = vec![Rect::new(0, 0, 0, 0); count];
    arrange(&mut |id, rect| {
        if let Some(index) = index_of(id) {
            if let Some(slot) = rects.get_mut(index) {
                *slot = rect;
            }
        }
    });
    rects
}

/// Paints every placed item, clipped to the control's own rectangle.
///
/// The clip is the control's box: nothing clips a widget at this layer, so a distribution
/// that overflows its own geometry would otherwise paint on a neighbour.
fn paint_items(
    ctx: &mut RenderContext,
    base: &BaseWidget,
    role: &str,
    outer: Rect,
    rects: &[Rect],
    items: &[FlexItemSpec],
) {
    let palette = resolve_palette(base, role);
    ctx.push_clip(outer.x, outer.y, outer.width, outer.height);
    let bottom = outer.y.saturating_add(outer.height as i32);
    for (item, rect) in items.iter().zip(rects) {
        // A fully hidden item is skipped rather than clipped: the clip handles the partial
        // case, and this keeps a long list from emitting commands for pixels nobody sees.
        if rect.height == 0 || rect.y >= bottom {
            continue;
        }
        paint_item_box(ctx, *rect, item.label(), &palette, 3);
    }
    ctx.pop_clip();
}

// ── Flex ────────────────────────────────────────────────────────────────────

/// Flex — a CSS-flexbox-style distribution of weighted items along one axis.
///
/// The control owns its items and calls [`FlexLayout`] with them, so `flex`, `justify`,
/// `align` and `wrap` behave exactly as they do for a tree child of that layout. An item's
/// `grow` is the flex weight; an item with `grow == 0` keeps the size it asked for.
pub struct Flex {
    base: BaseWidget,
    items: Vec<FlexItemSpec>,
    direction: FlexDirection,
    wrap: FlexWrap,
    justify: JustifyContent,
    align: AlignItems,
    gap: u32,
    /// Bumped by every input that changes the arrangement.
    rect_version: u64,
    rect_cache: std::cell::RefCell<Option<(u64, std::rc::Rc<Vec<Rect>>)>>,
    /// Emitted with the item index when an item is clicked.
    pub item_clicked: Signal1<usize>,
}

impl Flex {
    /// Creates an empty row-direction flex with no gap.
    pub fn new(geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(WidgetKind::Flex, geometry, "Flex"),
            items: Vec::new(),
            direction: FlexDirection::default(),
            wrap: FlexWrap::default(),
            justify: JustifyContent::default(),
            align: AlignItems::default(),
            gap: 0,
            rect_version: 1,
            rect_cache: std::cell::RefCell::new(None),
            item_clicked: Signal1::new(),
        }
    }

    /// The items this control distributes.
    pub fn items(&self) -> &[FlexItemSpec] {
        &self.items
    }

    /// Appends an item and requests a redraw.
    ///
    /// An item that carries a real child id (C-11) is linked into this control's child list and
    /// shown, so the tree painter draws the control in the item's rectangle. Without the link the
    /// id was only a click label: the item could not host a real control at all.
    pub fn add_item(&mut self, item: FlexItemSpec) {
        if let Some(widget_id) = item.widget_id() {
            self.base.add_child_linked(widget_id);
            crate::set_widget_visible(widget_id, true);
        }
        self.items.push(item);
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Removes the item at `index` (C-10).
    ///
    /// Returns `false` when `index` is out of range. A removed item takes its mounted child with
    /// it: the link is cleared in both directions and the control is hidden, so the layout cannot
    /// keep painting a control whose cell no longer exists.
    pub fn remove_item(&mut self, index: usize) -> bool {
        if index >= self.items.len() {
            return false;
        }
        let item = self.items.remove(index);
        if let Some(widget_id) = item.widget_id() {
            self.base.remove_child_linked(widget_id);
            crate::set_widget_visible(widget_id, false);
        }
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
        true
    }

    /// Writes each mounted item control's rectangle into the control (C-11).
    ///
    /// The tree painter draws a linked child at the child's own geometry, so the item rectangle is
    /// this control's answer to "where does that child live". Called from `draw` and
    /// `set_geometry`: the arrangement is cached, but the write has to reach the child on the
    /// frames where the cache is served unchanged.
    fn place_item_widgets(&self) {
        for (item, rect) in self.items.iter().zip(self.cached_item_rects().iter()) {
            if let Some(widget_id) = item.widget_id() {
                crate::set_widget_geometry(widget_id, rect.x, rect.y, rect.width, rect.height);
            }
        }
    }

    /// Removes every item.
    pub fn clear_items(&mut self) {
        if self.items.is_empty() {
            return;
        }
        self.items.clear();
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// The main-axis direction.
    pub fn direction(&self) -> FlexDirection {
        self.direction
    }

    /// Sets the main-axis direction.
    pub fn set_direction(&mut self, direction: FlexDirection) {
        if self.direction == direction {
            return;
        }
        self.direction = direction;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Whether the items wrap onto further lines.
    pub fn wrap(&self) -> FlexWrap {
        self.wrap
    }

    /// Sets whether the items wrap onto further lines.
    pub fn set_wrap(&mut self, wrap: FlexWrap) {
        if self.wrap == wrap {
            return;
        }
        self.wrap = wrap;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// How the leftover room is distributed on the main axis.
    pub fn justify(&self) -> JustifyContent {
        self.justify
    }

    /// Sets how the leftover room is distributed on the main axis.
    pub fn set_justify(&mut self, justify: JustifyContent) {
        if self.justify == justify {
            return;
        }
        self.justify = justify;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_redraw();
    }

    /// How items are aligned on the cross axis.
    pub fn align(&self) -> AlignItems {
        self.align
    }

    /// Sets how items are aligned on the cross axis.
    pub fn set_align(&mut self, align: AlignItems) {
        if self.align == align {
            return;
        }
        self.align = align;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_redraw();
    }

    /// The gap between items, in logical pixels.
    pub fn gap(&self) -> u32 {
        self.gap
    }

    /// Sets the gap between items, in logical pixels.
    pub fn set_gap(&mut self, gap: u32) {
        if self.gap == gap {
            return;
        }
        self.gap = gap;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// The rectangle each item occupies, in item order.
    ///
    /// The solver runs only when an input changed; the draw path uses the same cache, so a
    /// repaint of an unchanged distribution is a refcount bump rather than a re-arrange.
    pub fn item_rects(&self) -> Vec<Rect> {
        (*self.cached_item_rects()).clone()
    }

    fn cached_item_rects(&self) -> std::rc::Rc<Vec<Rect>> {
        if let Some((version, rects)) = self.rect_cache.borrow().as_ref() {
            if *version == self.rect_version {
                return std::rc::Rc::clone(rects);
            }
        }
        let rects = std::rc::Rc::new(self.compute_item_rects());
        *self.rect_cache.borrow_mut() = Some((self.rect_version, std::rc::Rc::clone(&rects)));
        rects
    }

    /// The item index under `point`, or `None` when the point is between items.
    ///
    /// C-P16: for the common simple run (one line, start-justified, equally sized items, no grow)
    /// the slot is solved arithmetically and only that candidate is confirmed against the cached
    /// rectangles — an O(1) hit test with no second source of truth, because the confirmation uses
    /// the same rectangles the painter does. Anything else falls back to the linear scan.
    pub fn item_at(&self, point: Point) -> Option<usize> {
        let rects = self.cached_item_rects();
        if let Some(index) = self.uniform_run_candidate(point) {
            if rects.get(index).is_some_and(|rect| rect.contains_point(point)) {
                return Some(index);
            }
        }
        rects.iter().position(|rect| rect.contains_point(point))
    }

    /// The slot a uniformly sized, single-line, start-justified run implies for `point`.
    ///
    /// `None` means "the formula does not describe this layout", and the caller scans instead.
    /// The arithmetic is only a *candidate*: the caller confirms it against the cached rectangle,
    /// so a mismatch degrades to the scan rather than producing a wrong hit.
    fn uniform_run_candidate(&self, point: Point) -> Option<usize> {
        if self.items.is_empty()
            || self.wrap != FlexWrap::NoWrap
            || !matches!(self.justify, JustifyContent::FlexStart)
            || matches!(self.direction, FlexDirection::RowReverse | FlexDirection::ColumnReverse)
        {
            return None;
        }
        let first = self.items.first()?;
        if self.items.iter().any(|item| item.size() != first.size() || item.grow() != 0) {
            return None;
        }
        let outer = self.geometry();
        let gap = self.gap as i32;
        let (start, step, extent, coordinate) = match self.direction {
            FlexDirection::Row => (outer.x, first.size().width as i32 + gap, outer.width, point.x),
            _ => (outer.y, first.size().height as i32 + gap, outer.height, point.y),
        };
        if step <= 0 || coordinate < start || coordinate >= start + extent as i32 {
            return None;
        }
        let slot = ((coordinate - start) / step) as usize;
        (slot < self.items.len()).then_some(slot)
    }

    fn compute_item_rects(&self) -> Vec<Rect> {
        let layout = self.build_layout();
        let children: Vec<ChildInfo> = self
            .items
            .iter()
            .enumerate()
            .map(|(index, item)| {
                ChildInfo::new(
                    synthetic_id(index),
                    Hints::fixed(item.size().width, item.size().height),
                )
            })
            .collect();
        collect_rects(self.items.len(), |out| {
            layout.arrange(self.geometry(), &children, &mut |id, rect| out(id, rect));
        })
    }

    /// Builds the engine from this control's configuration and items.
    fn build_layout(&self) -> FlexLayout {
        let mut layout = FlexLayout::with_params(
            self.direction,
            self.wrap,
            self.justify,
            self.align,
            self.gap as i32,
            0,
        );
        for (index, item) in self.items.iter().enumerate() {
            layout.items_mut().push(FlexItem {
                widget_id: Some(synthetic_id(index)),
                flex_grow: item.grow() as f32,
                ..FlexItem::default()
            });
        }
        layout
    }
}

impl Widget for Flex {
    fn base(&self) -> &BaseWidget {
        &self.base
    }
    fn set_state_theme_hook(&mut self) {
        crate::style::reapply_active_theme_state(self);
    }
    fn base_mut(&mut self) -> &mut BaseWidget {
        &mut self.base
    }

    fn set_geometry(&mut self, geometry: Rect) {
        self.base.set_geometry(geometry);
        self.rect_version = self.rect_version.wrapping_add(1);
        // The arrangement moved with the control, so the controls mounted in it have to move too.
        self.place_item_widgets();
    }

    /// The summed item extent plus the gaps: what this row or column wants to be.
    fn size_hint(&self) -> Size {
        let horizontal = matches!(self.direction, FlexDirection::Row | FlexDirection::RowReverse);
        let gaps = (self.items.len().saturating_sub(1) as u32).saturating_mul(self.gap);
        let mut main: u32 = 0;
        let mut cross: u32 = 0;
        for item in &self.items {
            let size = item.size();
            if horizontal {
                main = main.saturating_add(size.width);
                cross = cross.max(size.height);
            } else {
                main = main.saturating_add(size.height);
                cross = cross.max(size.width);
            }
        }
        let main = main.saturating_add(gaps);
        if horizontal {
            Size::new(main, cross)
        } else {
            Size::new(cross, main)
        }
    }

    /// Resolves a published event name to the signal this layout actually emits.
    ///
    /// The capability publishes `item_clicked` with a `UInt` payload and the
    /// control emits the clicked item's index as a `usize`, so the conversion
    /// keeps the subscriber's number identical to the emitted one.
    fn event_signal_dyn(&self, name: &str) -> Option<crate::signal::EventSignalRef> {
        use crate::signal::EventSignalRef;
        match name {
            "item_clicked" => {
                Some(EventSignalRef::mapped("item_clicked", &self.item_clicked, |v| {
                    CapabilityValue::UInt(*v as u64)
                }))
            }
            _ => None,
        }
    }

    impl_draw_bridge!();
    impl_widget_property_hooks!();
}

impl WidgetProperties for Flex {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "item_count" => Ok(CapabilityValue::UInt(self.items.len() as u64)),
            "direction" => Ok(CapabilityValue::String(flex_direction_token(self.direction).into())),
            "wrap" => Ok(CapabilityValue::String(flex_wrap_token(self.wrap).into())),
            "justify_content" => Ok(CapabilityValue::String(justify_token(self.justify).into())),
            "align_items" => Ok(CapabilityValue::String(align_token(self.align).into())),
            "gap" => Ok(CapabilityValue::UInt(u64::from(self.gap))),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            // Derived from how many items were added, so there is nothing sensible to assign.
            "item_count" => Err(CapabilityAccessError::ReadOnlyProperty),
            "direction" => {
                let token = expect_string(value)?;
                let direction =
                    flex_direction_from_token(&token).ok_or(CapabilityAccessError::OutOfRange)?;
                self.set_direction(direction);
                Ok(())
            }
            "wrap" => {
                let token = expect_string(value)?;
                let wrap = flex_wrap_from_token(&token).ok_or(CapabilityAccessError::OutOfRange)?;
                self.set_wrap(wrap);
                Ok(())
            }
            "justify_content" => {
                let token = expect_string(value)?;
                let justify =
                    justify_from_token(&token).ok_or(CapabilityAccessError::OutOfRange)?;
                self.set_justify(justify);
                Ok(())
            }
            "align_items" => {
                let token = expect_string(value)?;
                let align = align_from_token(&token).ok_or(CapabilityAccessError::OutOfRange)?;
                self.set_align(align);
                Ok(())
            }
            "gap" => {
                self.set_gap(expect_u32(value)?);
                Ok(())
            }
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        property_names_of![
            "item_count",
            "direction",
            "wrap",
            "justify_content",
            "align_items",
            "gap",
            BASE_PROPERTY_NAMES
        ]
    }
}

impl Draw for Flex {
    fn draw(&mut self, ctx: &mut RenderContext) {
        let outer = self.geometry();
        let rects = self.cached_item_rects();
        // C-11: mounted item controls are placed before they are painted.
        self.place_item_widgets();
        paint_items(ctx, &self.base, "flex", outer, &rects, &self.items);
    }
}

impl EventHandler for Flex {
    fn handle_event(&mut self, event: &Event) {
        self.base.handle_event(event);
        if let Event::MousePress { pos, button: 1, .. } = event {
            if let Some(index) = self.item_at(*pos) {
                self.item_clicked.emit(index);
            }
        }
    }
}

// ── FlowLayout ──────────────────────────────────────────────────────────────

/// FlowLayout — places items in reading order, starting a new run when the current is full.
///
/// Delegates to [`crate::layout::flow::FlowLayout`] through [`Layout::arrange`], so the
/// line-breaking arithmetic is the engine's, not a copy: a flow inside a control and a flow
/// inside a tree produce the same rectangles for the same sizes.
pub struct FlowLayout {
    base: BaseWidget,
    items: Vec<FlexItemSpec>,
    direction: FlowDirection,
    alignment: FlowAlignment,
    spacing: u32,
    padding: u32,
    wrap: bool,
    /// Reading direction forwarded to the flow engine; left-to-right by default.
    text_direction: TextDirection,
    /// Bumped by every input that changes the arrangement.
    rect_version: u64,
    rect_cache: std::cell::RefCell<Option<(u64, std::rc::Rc<Vec<Rect>>)>>,
    /// Emitted with the item index when an item is clicked.
    pub item_clicked: Signal1<usize>,
}

impl FlowLayout {
    /// Creates an empty horizontal flow, spacing 8, padding 8, wrapping on.
    ///
    /// Wrapping defaults to **on** here, unlike the engine's `false`: a control named
    /// "flow layout" that overflowed its own box by default would be the surprising choice
    /// for a form.
    pub fn new(geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(WidgetKind::FlowLayout, geometry, "FlowLayout"),
            items: Vec::new(),
            direction: FlowDirection::Horizontal,
            alignment: FlowAlignment::Start,
            spacing: 8,
            padding: 8,
            wrap: true,
            text_direction: TextDirection::default(),
            rect_version: 1,
            rect_cache: std::cell::RefCell::new(None),
            item_clicked: Signal1::new(),
        }
    }

    /// The reading direction items are placed in.
    pub fn text_direction(&self) -> TextDirection {
        self.text_direction
    }

    /// Sets the reading direction; right-to-left mirrors each run inside the control.
    pub fn set_text_direction(&mut self, direction: TextDirection) {
        if self.text_direction == direction {
            return;
        }
        self.text_direction = direction;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// The items this control places.
    pub fn items(&self) -> &[FlexItemSpec] {
        &self.items
    }

    /// Appends an item and requests a redraw.
    pub fn add_item(&mut self, item: FlexItemSpec) {
        self.items.push(item);
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Removes every item.
    pub fn clear_items(&mut self) {
        if self.items.is_empty() {
            return;
        }
        self.items.clear();
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Removes the item at `index` (C-10). Returns `false` when the index is out of range.
    ///
    /// The remaining items re-flow on the next arrangement: removing one changes the run the
    /// following items fall on, which is why the version is bumped rather than only the list.
    pub fn remove_item(&mut self, index: usize) -> bool {
        if index >= self.items.len() {
            return false;
        }
        self.items.remove(index);
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
        true
    }

    /// The flow direction.
    pub fn direction(&self) -> FlowDirection {
        self.direction
    }

    /// Sets the flow direction.
    pub fn set_direction(&mut self, direction: FlowDirection) {
        if self.direction == direction {
            return;
        }
        self.direction = direction;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// How items are positioned within each run.
    pub fn alignment(&self) -> FlowAlignment {
        self.alignment
    }

    /// Sets how items are positioned within each run.
    pub fn set_alignment(&mut self, alignment: FlowAlignment) {
        if self.alignment == alignment {
            return;
        }
        self.alignment = alignment;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_redraw();
    }

    /// The gap between adjacent items and between runs.
    pub fn spacing(&self) -> u32 {
        self.spacing
    }

    /// Sets the gap between adjacent items and between runs.
    pub fn set_spacing(&mut self, spacing: u32) {
        if self.spacing == spacing {
            return;
        }
        self.spacing = spacing;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// The inset applied on all four sides before items are placed.
    pub fn padding(&self) -> u32 {
        self.padding
    }

    /// Sets the inset applied on all four sides before items are placed.
    pub fn set_padding(&mut self, padding: u32) {
        if self.padding == padding {
            return;
        }
        self.padding = padding;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Whether an item that would cross the far edge starts a new run.
    pub fn wraps(&self) -> bool {
        self.wrap
    }

    /// Sets whether an item that would cross the far edge starts a new run.
    pub fn set_wraps(&mut self, wrap: bool) {
        if self.wrap == wrap {
            return;
        }
        self.wrap = wrap;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// The rectangle each item occupies, in item order.
    ///
    /// The engine runs only when an input changed; the draw path and `item_at` share the same
    /// cached vector, so a repaint or a pointer event does not rebuild the flow.
    pub fn item_rects(&self) -> Vec<Rect> {
        (*self.cached_item_rects()).clone()
    }

    fn cached_item_rects(&self) -> std::rc::Rc<Vec<Rect>> {
        if let Some((version, rects)) = self.rect_cache.borrow().as_ref() {
            if *version == self.rect_version {
                return std::rc::Rc::clone(rects);
            }
        }
        let rects = std::rc::Rc::new(self.compute_item_rects());
        *self.rect_cache.borrow_mut() = Some((self.rect_version, std::rc::Rc::clone(&rects)));
        rects
    }

    fn compute_item_rects(&self) -> Vec<Rect> {
        let mut engine = FlowEngine::with_config(FlowLayoutConfig {
            direction: self.direction,
            alignment: self.alignment,
            spacing: self.spacing as i32,
            padding: self.padding as i32,
            wrap: self.wrap,
        });
        engine.set_text_direction(self.text_direction);
        let children: Vec<ChildInfo> = self
            .items
            .iter()
            .enumerate()
            .map(|(index, item)| {
                let id = synthetic_id(index);
                engine.add_widget(id, 0);
                ChildInfo::new(id, Hints::fixed(item.size().width, item.size().height))
            })
            .collect();
        collect_rects(self.items.len(), |out| {
            engine.arrange(self.geometry(), &children, &mut |id, rect| out(id, rect));
        })
    }

    /// The item index under `point`.
    pub fn item_at(&self, point: Point) -> Option<usize> {
        self.cached_item_rects().iter().position(|rect| rect.contains_point(point))
    }
}

impl Widget for FlowLayout {
    fn base(&self) -> &BaseWidget {
        &self.base
    }
    fn set_state_theme_hook(&mut self) {
        crate::style::reapply_active_theme_state(self);
    }
    fn base_mut(&mut self) -> &mut BaseWidget {
        &mut self.base
    }

    fn set_geometry(&mut self, geometry: Rect) {
        self.base.set_geometry(geometry);
        self.rect_version = self.rect_version.wrapping_add(1);
    }

    fn size_hint(&self) -> Size {
        let horizontal = matches!(self.direction, FlowDirection::Horizontal);
        let gaps = (self.items.len().saturating_sub(1) as u32).saturating_mul(self.spacing);
        let mut main: u32 = 0;
        let mut cross: u32 = 0;
        for item in &self.items {
            let size = item.size();
            if horizontal {
                main = main.saturating_add(size.width);
                cross = cross.max(size.height);
            } else {
                main = main.saturating_add(size.height);
                cross = cross.max(size.width);
            }
        }
        let main = main.saturating_add(gaps).saturating_add(self.padding.saturating_mul(2));
        let cross = cross.saturating_add(self.padding.saturating_mul(2));
        if horizontal {
            Size::new(main, cross)
        } else {
            Size::new(cross, main)
        }
    }

    /// Resolves a published event name to the signal this layout actually emits.
    ///
    /// The capability publishes `item_clicked` with a `UInt` payload and the
    /// control emits the clicked item's index as a `usize`, so the conversion
    /// keeps the subscriber's number identical to the emitted one.
    fn event_signal_dyn(&self, name: &str) -> Option<crate::signal::EventSignalRef> {
        use crate::signal::EventSignalRef;
        match name {
            "item_clicked" => {
                Some(EventSignalRef::mapped("item_clicked", &self.item_clicked, |v| {
                    CapabilityValue::UInt(*v as u64)
                }))
            }
            _ => None,
        }
    }

    impl_draw_bridge!();
    impl_widget_property_hooks!();
}

impl WidgetProperties for FlowLayout {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "item_count" => Ok(CapabilityValue::UInt(self.items.len() as u64)),
            "direction" => Ok(CapabilityValue::String(flow_direction_token(self.direction).into())),
            "alignment" => Ok(CapabilityValue::String(flow_alignment_token(self.alignment).into())),
            "spacing" => Ok(CapabilityValue::UInt(u64::from(self.spacing))),
            "padding" => Ok(CapabilityValue::UInt(u64::from(self.padding))),
            "wrap" => Ok(CapabilityValue::Bool(self.wrap)),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "item_count" => Err(CapabilityAccessError::ReadOnlyProperty),
            "direction" => {
                let token = expect_string(value)?;
                let direction =
                    flow_direction_from_token(&token).ok_or(CapabilityAccessError::OutOfRange)?;
                self.set_direction(direction);
                Ok(())
            }
            "alignment" => {
                let token = expect_string(value)?;
                let alignment =
                    flow_alignment_from_token(&token).ok_or(CapabilityAccessError::OutOfRange)?;
                self.set_alignment(alignment);
                Ok(())
            }
            "spacing" => {
                self.set_spacing(expect_u32(value)?);
                Ok(())
            }
            "padding" => {
                self.set_padding(expect_u32(value)?);
                Ok(())
            }
            "wrap" => {
                self.set_wraps(expect_bool(value)?);
                Ok(())
            }
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        property_names_of![
            "item_count",
            "direction",
            "alignment",
            "spacing",
            "padding",
            "wrap",
            BASE_PROPERTY_NAMES
        ]
    }
}

impl Draw for FlowLayout {
    fn draw(&mut self, ctx: &mut RenderContext) {
        let outer = self.geometry();
        let rects = self.cached_item_rects();
        paint_items(ctx, &self.base, "flow_layout", outer, &rects, &self.items);
    }
}

impl EventHandler for FlowLayout {
    fn handle_event(&mut self, event: &Event) {
        self.base.handle_event(event);
        if let Event::MousePress { pos, button: 1, .. } = event {
            if let Some(index) = self.item_at(*pos) {
                self.item_clicked.emit(index);
            }
        }
    }
}

// ── WrapLayout ──────────────────────────────────────────────────────────────

/// WrapLayout — places fixed-size items and breaks lines when a line is full.
///
/// # The difference from `FlowLayout`
///
/// A flow starts a new run when the *hint* of the next item would not fit and publishes one
/// spacing for both axes; a wrap always breaks at the configured cell extent and publishes a
/// separate `run_spacing`. Both delegate line breaking to the same engine functions (the
/// crate's `wrap::break_lines` / `place_lines`), so "where does a line end" has one answer.
pub struct WrapLayout {
    base: BaseWidget,
    items: Vec<FlexItemSpec>,
    direction: WrapDirection,
    alignment: WrapAlignment,
    spacing: u32,
    run_spacing: Option<u32>,
    padding: u32,
    /// Reading direction forwarded to the wrap engine; left-to-right by default.
    text_direction: TextDirection,
    /// Bumped by every input that changes the arrangement.
    rect_version: u64,
    rect_cache: std::cell::RefCell<Option<(u64, std::rc::Rc<Vec<Rect>>)>>,
    /// Emitted with the item index when an item is clicked.
    pub item_clicked: Signal1<usize>,
}

impl WrapLayout {
    /// Creates an empty horizontal wrap, spacing 8, padding 8, run spacing following the item
    /// spacing.
    pub fn new(geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(WidgetKind::WrapLayout, geometry, "WrapLayout"),
            items: Vec::new(),
            direction: WrapDirection::Horizontal,
            alignment: WrapAlignment::Start,
            spacing: 8,
            run_spacing: None,
            padding: 8,
            text_direction: TextDirection::default(),
            rect_version: 1,
            rect_cache: std::cell::RefCell::new(None),
            item_clicked: Signal1::new(),
        }
    }

    /// The items this control places.
    pub fn items(&self) -> &[FlexItemSpec] {
        &self.items
    }

    /// Appends an item and requests a redraw.
    pub fn add_item(&mut self, item: FlexItemSpec) {
        self.items.push(item);
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Removes every item.
    pub fn clear_items(&mut self) {
        if self.items.is_empty() {
            return;
        }
        self.items.clear();
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Removes the item at `index` (C-10). Returns `false` when the index is out of range.
    ///
    /// The remaining items re-wrap on the next arrangement: removing one changes which line the
    /// following items fall on, which is why the version is bumped rather than only the list.
    pub fn remove_item(&mut self, index: usize) -> bool {
        if index >= self.items.len() {
            return false;
        }
        self.items.remove(index);
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
        true
    }

    /// The direction items flow before wrapping.
    pub fn direction(&self) -> WrapDirection {
        self.direction
    }

    /// Sets the direction items flow before wrapping.
    pub fn set_direction(&mut self, direction: WrapDirection) {
        if self.direction == direction {
            return;
        }
        self.direction = direction;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// How items are aligned within each run.
    pub fn alignment(&self) -> WrapAlignment {
        self.alignment
    }

    /// Sets how items are aligned within each run.
    pub fn set_alignment(&mut self, alignment: WrapAlignment) {
        if self.alignment == alignment {
            return;
        }
        self.alignment = alignment;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_redraw();
    }

    /// The gap between adjacent items.
    pub fn spacing(&self) -> u32 {
        self.spacing
    }

    /// Sets the gap between adjacent items.
    pub fn set_spacing(&mut self, spacing: u32) {
        if self.spacing == spacing {
            return;
        }
        self.spacing = spacing;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// The gap between runs, or `None` when it follows the item spacing.
    pub fn run_spacing(&self) -> Option<u32> {
        self.run_spacing
    }

    /// Sets the gap between runs. `None` restores "follow the item spacing".
    pub fn set_run_spacing(&mut self, run_spacing: Option<u32>) {
        if self.run_spacing == run_spacing {
            return;
        }
        self.run_spacing = run_spacing;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// The inset applied on all four sides before items are placed.
    pub fn padding(&self) -> u32 {
        self.padding
    }

    /// Sets the inset applied on all four sides before items are placed.
    pub fn set_padding(&mut self, padding: u32) {
        if self.padding == padding {
            return;
        }
        self.padding = padding;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// The reading direction items are placed in.
    pub fn text_direction(&self) -> TextDirection {
        self.text_direction
    }

    /// Sets the reading direction; right-to-left mirrors each run inside the control.
    pub fn set_text_direction(&mut self, direction: TextDirection) {
        if self.text_direction == direction {
            return;
        }
        self.text_direction = direction;
        self.rect_version = self.rect_version.wrapping_add(1);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// The rectangle each item occupies, in item order.
    ///
    /// The engine runs only when an input changed; the draw path and `item_at` share the same
    /// cached vector, so a repaint or a pointer event does not rebuild the wrap.
    pub fn item_rects(&self) -> Vec<Rect> {
        (*self.cached_item_rects()).clone()
    }

    fn cached_item_rects(&self) -> std::rc::Rc<Vec<Rect>> {
        if let Some((version, rects)) = self.rect_cache.borrow().as_ref() {
            if *version == self.rect_version {
                return std::rc::Rc::clone(rects);
            }
        }
        let rects = std::rc::Rc::new(self.compute_item_rects());
        *self.rect_cache.borrow_mut() = Some((self.rect_version, std::rc::Rc::clone(&rects)));
        rects
    }

    fn compute_item_rects(&self) -> Vec<Rect> {
        let mut engine = WrapEngine::new(
            self.direction,
            self.alignment,
            self.spacing as i32,
            self.padding as i32,
        );
        engine.set_run_spacing(self.run_spacing.map(|spacing| spacing as i32));
        engine.set_text_direction(self.text_direction);
        let children: Vec<ChildInfo> = self
            .items
            .iter()
            .enumerate()
            .map(|(index, item)| {
                let id = synthetic_id(index);
                engine.add_widget(id, 0);
                engine.set_child_size(id, item.size());
                ChildInfo::new(id, Hints::fixed(item.size().width, item.size().height))
            })
            .collect();
        collect_rects(self.items.len(), |out| {
            engine.arrange(self.geometry(), &children, &mut |id, rect| out(id, rect));
        })
    }

    /// The item index under `point`.
    pub fn item_at(&self, point: Point) -> Option<usize> {
        self.cached_item_rects().iter().position(|rect| rect.contains_point(point))
    }
}

impl Widget for WrapLayout {
    fn base(&self) -> &BaseWidget {
        &self.base
    }
    fn set_state_theme_hook(&mut self) {
        crate::style::reapply_active_theme_state(self);
    }
    fn base_mut(&mut self) -> &mut BaseWidget {
        &mut self.base
    }

    fn set_geometry(&mut self, geometry: Rect) {
        self.base.set_geometry(geometry);
        self.rect_version = self.rect_version.wrapping_add(1);
    }

    fn size_hint(&self) -> Size {
        let horizontal = matches!(self.direction, WrapDirection::Horizontal);
        let gap = (self.items.len().saturating_sub(1) as u32).saturating_mul(self.spacing);
        let mut main: u32 = 0;
        let mut cross: u32 = 0;
        for item in &self.items {
            let size = item.size();
            if horizontal {
                main = main.saturating_add(size.width);
                cross = cross.max(size.height);
            } else {
                main = main.saturating_add(size.height);
                cross = cross.max(size.width);
            }
        }
        let main = main.saturating_add(gap).saturating_add(self.padding.saturating_mul(2));
        let cross = cross.saturating_add(self.padding.saturating_mul(2));
        if horizontal {
            Size::new(main, cross)
        } else {
            Size::new(cross, main)
        }
    }

    /// Resolves a published event name to the signal this layout actually emits.
    ///
    /// The capability publishes `item_clicked` with a `UInt` payload and the
    /// control emits the clicked item's index as a `usize`, so the conversion
    /// keeps the subscriber's number identical to the emitted one.
    fn event_signal_dyn(&self, name: &str) -> Option<crate::signal::EventSignalRef> {
        use crate::signal::EventSignalRef;
        match name {
            "item_clicked" => {
                Some(EventSignalRef::mapped("item_clicked", &self.item_clicked, |v| {
                    CapabilityValue::UInt(*v as u64)
                }))
            }
            _ => None,
        }
    }

    impl_draw_bridge!();
    impl_widget_property_hooks!();
}

impl WidgetProperties for WrapLayout {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "item_count" => Ok(CapabilityValue::UInt(self.items.len() as u64)),
            "direction" => Ok(CapabilityValue::String(wrap_direction_token(self.direction).into())),
            "alignment" => Ok(CapabilityValue::String(wrap_alignment_token(self.alignment).into())),
            "spacing" => Ok(CapabilityValue::UInt(u64::from(self.spacing))),
            // `null` is the documented "follow the item spacing" state, so the property is
            // nullable rather than an impossible sentinel such as 0.
            "run_spacing" => Ok(match self.run_spacing {
                Some(spacing) => CapabilityValue::UInt(u64::from(spacing)),
                None => CapabilityValue::Null,
            }),
            "padding" => Ok(CapabilityValue::UInt(u64::from(self.padding))),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "item_count" => Err(CapabilityAccessError::ReadOnlyProperty),
            "direction" => {
                let token = expect_string(value)?;
                let direction =
                    wrap_direction_from_token(&token).ok_or(CapabilityAccessError::OutOfRange)?;
                self.set_direction(direction);
                Ok(())
            }
            "alignment" => {
                let token = expect_string(value)?;
                let alignment =
                    wrap_alignment_from_token(&token).ok_or(CapabilityAccessError::OutOfRange)?;
                self.set_alignment(alignment);
                Ok(())
            }
            "spacing" => {
                self.set_spacing(expect_u32(value)?);
                Ok(())
            }
            "run_spacing" => {
                let spacing = match value {
                    CapabilityValue::Null => None,
                    other => Some(expect_u32(other)?),
                };
                self.set_run_spacing(spacing);
                Ok(())
            }
            "padding" => {
                self.set_padding(expect_u32(value)?);
                Ok(())
            }
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        property_names_of![
            "item_count",
            "direction",
            "alignment",
            "spacing",
            "run_spacing",
            "padding",
            BASE_PROPERTY_NAMES
        ]
    }
}

impl Draw for WrapLayout {
    fn draw(&mut self, ctx: &mut RenderContext) {
        let outer = self.geometry();
        let rects = self.cached_item_rects();
        paint_items(ctx, &self.base, "wrap_layout", outer, &rects, &self.items);
    }
}

impl EventHandler for WrapLayout {
    fn handle_event(&mut self, event: &Event) {
        self.base.handle_event(event);
        if let Event::MousePress { pos, button: 1, .. } = event {
            if let Some(index) = self.item_at(*pos) {
                self.item_clicked.emit(index);
            }
        }
    }
}

// ── Enum tokens ─────────────────────────────────────────────────────────────
//
// One token table per published enum, with the parser beside the writer so a renamed variant
// cannot leave one side behind. Each `from_token` returns `None` for an unpublished spelling
// rather than a default, so a designer that writes a typo is told, not silently ignored.

fn flex_direction_token(direction: FlexDirection) -> &'static str {
    match direction {
        FlexDirection::Row => "row",
        FlexDirection::RowReverse => "row_reverse",
        FlexDirection::Column => "column",
        FlexDirection::ColumnReverse => "column_reverse",
    }
}

fn flex_direction_from_token(token: &str) -> Option<FlexDirection> {
    match token {
        "row" => Some(FlexDirection::Row),
        "row_reverse" => Some(FlexDirection::RowReverse),
        "column" => Some(FlexDirection::Column),
        "column_reverse" => Some(FlexDirection::ColumnReverse),
        _ => None,
    }
}

fn flex_wrap_token(wrap: FlexWrap) -> &'static str {
    match wrap {
        FlexWrap::NoWrap => "no_wrap",
        FlexWrap::Wrap => "wrap",
        FlexWrap::WrapReverse => "wrap_reverse",
    }
}

fn flex_wrap_from_token(token: &str) -> Option<FlexWrap> {
    match token {
        "no_wrap" => Some(FlexWrap::NoWrap),
        "wrap" => Some(FlexWrap::Wrap),
        "wrap_reverse" => Some(FlexWrap::WrapReverse),
        _ => None,
    }
}

fn justify_token(justify: JustifyContent) -> &'static str {
    match justify {
        JustifyContent::FlexStart => "flex_start",
        JustifyContent::FlexEnd => "flex_end",
        JustifyContent::Center => "center",
        JustifyContent::SpaceBetween => "space_between",
        JustifyContent::SpaceAround => "space_around",
        JustifyContent::SpaceEvenly => "space_evenly",
    }
}

fn justify_from_token(token: &str) -> Option<JustifyContent> {
    match token {
        "flex_start" => Some(JustifyContent::FlexStart),
        "flex_end" => Some(JustifyContent::FlexEnd),
        "center" => Some(JustifyContent::Center),
        "space_between" => Some(JustifyContent::SpaceBetween),
        "space_around" => Some(JustifyContent::SpaceAround),
        "space_evenly" => Some(JustifyContent::SpaceEvenly),
        _ => None,
    }
}

fn align_token(align: AlignItems) -> &'static str {
    match align {
        AlignItems::Stretch => "stretch",
        AlignItems::FlexStart => "flex_start",
        AlignItems::FlexEnd => "flex_end",
        AlignItems::Center => "center",
        AlignItems::Baseline => "baseline",
    }
}

fn align_from_token(token: &str) -> Option<AlignItems> {
    match token {
        "stretch" => Some(AlignItems::Stretch),
        "flex_start" => Some(AlignItems::FlexStart),
        "flex_end" => Some(AlignItems::FlexEnd),
        "center" => Some(AlignItems::Center),
        "baseline" => Some(AlignItems::Baseline),
        _ => None,
    }
}

fn flow_direction_token(direction: FlowDirection) -> &'static str {
    match direction {
        FlowDirection::Horizontal => "horizontal",
        FlowDirection::Vertical => "vertical",
    }
}

fn flow_direction_from_token(token: &str) -> Option<FlowDirection> {
    match token {
        "horizontal" => Some(FlowDirection::Horizontal),
        "vertical" => Some(FlowDirection::Vertical),
        _ => None,
    }
}

fn flow_alignment_token(alignment: FlowAlignment) -> &'static str {
    match alignment {
        FlowAlignment::Start => "start",
        FlowAlignment::Center => "center",
        FlowAlignment::End => "end",
        FlowAlignment::SpaceBetween => "space_between",
        FlowAlignment::SpaceAround => "space_around",
    }
}

fn flow_alignment_from_token(token: &str) -> Option<FlowAlignment> {
    match token {
        "start" => Some(FlowAlignment::Start),
        "center" => Some(FlowAlignment::Center),
        "end" => Some(FlowAlignment::End),
        "space_between" => Some(FlowAlignment::SpaceBetween),
        "space_around" => Some(FlowAlignment::SpaceAround),
        _ => None,
    }
}

fn wrap_direction_token(direction: WrapDirection) -> &'static str {
    match direction {
        WrapDirection::Horizontal => "horizontal",
        WrapDirection::Vertical => "vertical",
    }
}

fn wrap_direction_from_token(token: &str) -> Option<WrapDirection> {
    match token {
        "horizontal" => Some(WrapDirection::Horizontal),
        "vertical" => Some(WrapDirection::Vertical),
        _ => None,
    }
}

fn wrap_alignment_token(alignment: WrapAlignment) -> &'static str {
    match alignment {
        WrapAlignment::Start => "start",
        WrapAlignment::Center => "center",
        WrapAlignment::End => "end",
        WrapAlignment::SpaceBetween => "space_between",
        WrapAlignment::SpaceAround => "space_around",
    }
}

fn wrap_alignment_from_token(token: &str) -> Option<WrapAlignment> {
    match token {
        "start" => Some(WrapAlignment::Start),
        "center" => Some(WrapAlignment::Center),
        "end" => Some(WrapAlignment::End),
        "space_between" => Some(WrapAlignment::SpaceBetween),
        "space_around" => Some(WrapAlignment::SpaceAround),
        _ => None,
    }
}

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

    /// All three layouts publish `item_clicked`, so each must resolve it separately.
    #[test]
    fn published_item_clicked_resolves_on_every_layout() {
        let flex = Flex::new(Rect::new(0, 0, 300, 100));
        let flow = FlowLayout::new(Rect::new(0, 0, 300, 100));
        let wrap = WrapLayout::new(Rect::new(0, 0, 300, 100));
        assert!(flex.event_signal_dyn("item_clicked").is_some(), "flex must resolve");
        assert!(flow.event_signal_dyn("item_clicked").is_some(), "flow_layout must resolve");
        assert!(wrap.event_signal_dyn("item_clicked").is_some(), "wrap_layout must resolve");
        assert!(flex.event_signal_dyn("no_such_event").is_none());
        assert!(flow.event_signal_dyn("no_such_event").is_none());
        assert!(wrap.event_signal_dyn("no_such_event").is_none());
    }

    fn spec(label: &str, width: u32, height: u32) -> FlexItemSpec {
        FlexItemSpec::new(label, Size::new(width, height))
    }

    #[test]
    fn flex_distributes_weights_in_item_order() {
        let mut flex = Flex::new(Rect::new(0, 0, 300, 40));
        flex.set_gap(10);
        flex.add_item(spec("a", 40, 40));
        flex.add_item(spec("b", 40, 40).with_grow(1));
        flex.add_item(spec("c", 40, 40).with_grow(1));
        let rects = flex.item_rects();
        assert_eq!(rects.len(), 3);
        // The three items are laid left to right, in creation order.
        assert!(rects[0].x < rects[1].x && rects[1].x < rects[2].x);
        // The two growing items share what the fixed one left, so they end up wider.
        assert!(rects[1].width > rects[0].width, "{rects:?}");
        assert_eq!(rects[1].width, rects[2].width, "{rects:?}");
    }

    #[test]
    fn flex_reports_the_size_its_items_need() {
        let mut flex = Flex::new(Rect::new(0, 0, 300, 40));
        flex.set_gap(5);
        flex.add_item(spec("a", 30, 20));
        flex.add_item(spec("b", 20, 24));
        assert_eq!(flex.size_hint(), Size::new(55, 24));
        flex.set_direction(FlexDirection::Column);
        assert_eq!(flex.size_hint(), Size::new(30, 49));
    }

    #[test]
    fn flex_item_count_is_derived_and_read_only() {
        let mut flex = Flex::new(Rect::new(0, 0, 300, 40));
        flex.add_item(spec("a", 10, 10));
        assert_eq!(flex.get("item_count"), Ok(CapabilityValue::UInt(1)));
        assert_eq!(
            flex.set("item_count", CapabilityValue::UInt(9)),
            Err(CapabilityAccessError::ReadOnlyProperty)
        );
    }

    #[test]
    fn flow_layout_wraps_a_long_run_onto_the_next_line() {
        let mut flow = FlowLayout::new(Rect::new(0, 0, 100, 200));
        flow.set_spacing(0);
        flow.set_padding(0);
        for _ in 0..4 {
            flow.add_item(spec("item", 40, 20));
        }
        let rects = flow.item_rects();
        assert_eq!(rects.len(), 4);
        assert_eq!(rects[0].y, rects[1].y, "the first two fit on one run");
        assert!(rects[2].y > rects[0].y, "the third starts the next run: {rects:?}");
    }

    #[test]
    fn flow_layout_honours_its_padding() {
        let mut flow = FlowLayout::new(Rect::new(0, 0, 200, 200));
        flow.set_padding(12);
        flow.set_spacing(0);
        flow.add_item(spec("item", 20, 10));
        let rects = flow.item_rects();
        assert_eq!((rects[0].x, rects[0].y), (12, 12));
    }

    #[test]
    fn wrap_layout_publishes_a_separate_run_spacing() {
        let mut wrap = WrapLayout::new(Rect::new(0, 0, 100, 200));
        wrap.set_spacing(0);
        wrap.set_padding(0);
        for _ in 0..3 {
            wrap.add_item(spec("item", 40, 20));
        }
        let tight = wrap.item_rects();
        wrap.set_run_spacing(Some(10));
        let loose = wrap.item_rects();
        assert_eq!(tight.len(), 3);
        // Two items fit per run, so the third's y is the run offset: 20 either way, and the
        // difference shows up once a third run exists.
        assert!(loose[2].y > tight[2].y || tight[2].y == loose[2].y);
        // With two runs the answer is exact: run 2 starts after run 1 plus the run spacing.
        assert_eq!(loose[2].y - tight[2].y, 10, "raising the run spacing moves later runs down");
    }

    #[test]
    fn wrap_layout_run_spacing_null_means_follow_the_item_spacing() {
        let mut wrap = WrapLayout::new(Rect::new(0, 0, 100, 200));
        assert_eq!(wrap.get("run_spacing"), Ok(CapabilityValue::Null));
        wrap.set("run_spacing", CapabilityValue::UInt(4)).expect("writable");
        assert_eq!(wrap.run_spacing(), Some(4));
        wrap.set("run_spacing", CapabilityValue::Null).expect("nullable");
        assert_eq!(wrap.run_spacing(), None);
    }

    #[test]
    fn the_enum_tokens_round_trip() {
        for token in ["row", "row_reverse", "column", "column_reverse"] {
            assert_eq!(
                flex_direction_token(
                    flex_direction_from_token(token).expect("a published direction")
                ),
                token
            );
        }
        for token in ["no_wrap", "wrap", "wrap_reverse"] {
            assert_eq!(
                flex_wrap_token(flex_wrap_from_token(token).expect("a published wrap")),
                token
            );
        }
        for token in
            ["flex_start", "flex_end", "center", "space_between", "space_around", "space_evenly"]
        {
            assert_eq!(
                justify_token(justify_from_token(token).expect("a published justify")),
                token
            );
        }
        for token in ["stretch", "flex_start", "flex_end", "center", "baseline"] {
            assert_eq!(align_token(align_from_token(token).expect("a published align")), token);
        }
        for token in ["horizontal", "vertical"] {
            assert_eq!(
                flow_direction_token(flow_direction_from_token(token).expect("a direction")),
                token
            );
            assert_eq!(
                wrap_direction_token(wrap_direction_from_token(token).expect("a direction")),
                token
            );
        }
        for token in ["start", "center", "end", "space_between", "space_around"] {
            assert_eq!(
                flow_alignment_token(flow_alignment_from_token(token).expect("an alignment")),
                token
            );
            assert_eq!(
                wrap_alignment_token(wrap_alignment_from_token(token).expect("an alignment")),
                token
            );
        }
        assert_eq!(flex_direction_from_token("diagonal"), None);
        assert_eq!(justify_from_token("middle"), None);
    }

    #[test]
    fn the_distribution_controls_paint_their_items() {
        // The SVG backend paints a label as glyph geometry, not as a `<text>` element, so the
        // labels are counted through the crate's own ink reader: one `<path>` per drawn run.
        let mut flex = Flex::new(Rect::new(0, 0, 200, 60));
        flex.add_item(spec("one", 60, 40));
        flex.add_item(spec("two", 60, 40));
        let svg = crate::widget::svg::render_to_svg(&mut flex);
        assert_eq!(
            crate::widget::svg::text_ink_boxes(&svg).len(),
            2,
            "both item labels must be painted: {svg}"
        );

        let mut flow = FlowLayout::new(Rect::new(0, 0, 200, 60));
        flow.add_item(spec("alpha", 40, 20));
        let svg = crate::widget::svg::render_to_svg(&mut flow);
        assert_eq!(crate::widget::svg::text_ink_boxes(&svg).len(), 1, "{svg}");

        let mut wrap = WrapLayout::new(Rect::new(0, 0, 200, 60));
        wrap.add_item(spec("beta", 40, 20));
        let svg = crate::widget::svg::render_to_svg(&mut wrap);
        assert_eq!(crate::widget::svg::text_ink_boxes(&svg).len(), 1, "{svg}");
    }

    #[test]
    fn an_item_past_the_bottom_edge_is_not_painted() {
        let mut flex = Flex::new(Rect::new(0, 0, 100, 40));
        flex.set_direction(FlexDirection::Column);
        for _ in 0..6 {
            flex.add_item(spec("row", 40, 30));
        }
        let svg = crate::widget::svg::render_to_svg(&mut flex);
        assert!(svg.contains("<clipPath"), "the items must be clipped to the control: {svg}");
    }

    #[test]
    fn the_distribution_controls_ignore_events_calmly() {
        let mut flex = Flex::new(Rect::new(0, 0, 100, 40));
        flex.handle_event(&Event::MouseMove { pos: crate::core::Point::new(3, 3) });
        let mut flow = FlowLayout::new(Rect::new(0, 0, 100, 40));
        flow.handle_event(&Event::KeyDown((65, 0)));
        let mut wrap = WrapLayout::new(Rect::new(0, 0, 100, 40));
        wrap.handle_event(&Event::KeyUp((65, 0)));
    }

    /// Items with real child ids are addressable, the arrangement is cached, and a click
    /// reports the item it landed on.
    #[test]
    fn items_carry_child_ids_and_clicks_name_them() {
        let mut flex = Flex::new(Rect::new(0, 0, 120, 30));
        flex.add_item(FlexItemSpec::new("a", Size::new(50, 20)).with_widget(7));
        flex.add_item(FlexItemSpec::new("b", Size::new(50, 20)));
        assert_eq!(flex.items()[0].widget_id(), Some(7));
        let rects = flex.item_rects();
        assert_eq!(flex.item_at(crate::core::Point::new(rects[0].x + 1, rects[0].y + 1)), Some(0));
        assert_eq!(flex.item_at(crate::core::Point::new(500, 500)), None);

        let seen = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(usize::MAX));
        let sink = std::sync::Arc::clone(&seen);
        flex.item_clicked.connect(move |index| {
            sink.store(*index, std::sync::atomic::Ordering::SeqCst);
        });
        flex.handle_event(&Event::MousePress {
            pos: crate::core::Point::new(rects[0].x + 1, rects[0].y + 1),
            button: 1,
            modifiers: 0,
        });
        assert_eq!(seen.load(std::sync::atomic::Ordering::SeqCst), 0);
    }

    /// The layout controls expose reading direction and forward it to their engine.
    #[test]
    fn flow_and_wrap_controls_mirror_for_right_to_left() {
        let mut flow = FlowLayout::new(Rect::new(0, 0, 100, 40));
        flow.set_padding(0);
        flow.add_item(FlexItemSpec::new("a", Size::new(30, 10)));
        assert_eq!(flow.item_rects()[0].x, 0);
        flow.set_text_direction(TextDirection::RightToLeft);
        assert!(flow.text_direction().is_right_to_left());
        assert_eq!(flow.item_rects()[0].x, 70, "the run begins at the right edge");

        let mut wrap = WrapLayout::new(Rect::new(0, 0, 100, 40));
        wrap.set_padding(0);
        wrap.add_item(FlexItemSpec::new("a", Size::new(30, 10)));
        assert_eq!(wrap.item_rects()[0].x, 0);
        wrap.set_text_direction(TextDirection::RightToLeft);
        assert!(wrap.text_direction().is_right_to_left());
        assert_eq!(wrap.item_rects()[0].x, 70, "the wrap run mirrors too");
    }
}