quickgui 0.1.6

A damage-driven, GPU-accelerated desktop GUI foundation
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
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
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
use std::{
    any::{Any, TypeId},
    borrow::Cow,
    cell::RefCell,
    fmt,
    rc::Rc,
    sync::Arc,
};

use glyphon::{Style as GlyphStyle, Weight};
use taffy::{
    Style,
    geometry::{Line as TaffyLine, Point as TaffyPoint, Rect as TaffyRect, Size as TaffySize},
    prelude::{
        AlignContent, AlignItems, AlignSelf, Dimension, Display, FlexDirection, FlexWrap,
        GridAutoFlow, GridPlacement, GridTemplateComponent, JustifyContent, LengthPercentage,
        LengthPercentageAuto, Position, TrackSizingFunction,
    },
    style::Overflow,
    style_helpers::{
        auto, fit_content, flex, fr, length, line, max_content, min_content, minmax, percent,
        repeat,
    },
};

use crate::{
    AnimatedImage, Background, BorderStyle, BoxShadow, Canvas, Color, ColorStops, Corners,
    CursorStyle, CustomShader, DispatchPhase, Filter, Filters, Font, FontFallbacks, FontFamily,
    FontFeatures, Gradient, GradientAngle, GradientCenter, Image, ImageSource, Insets, KeyContext,
    MAX_VALIDATION_MESSAGE_BYTES, ObjectFit, Path, RadialGradientShape, Rect, ScenePlane,
    ShaderParameters, StyledText, Svg, SvgTransform, TextAlign, TextCheckingOverrides,
    TextHighlight, TextOverflow, TextShaping, TextStyle, TextUnderline, TextWrap, Tooltip,
    Transition, WhiteSpace,
    action::{ActionListenerBinding, MAX_ACTION_LISTENERS_PER_ELEMENT},
    animation::ElementAnimation,
    font::{assert_valid_font_family, normalize_fallbacks},
    spring::ElementSpring,
    virtual_list::{
        ListItemMeasurement, ListState, VirtualList, VirtualScrollHandle, VirtualScrollMount,
    },
};

// Direction, sticky positioning, scroll snapping, and extended text styling.
use crate::{
    Hyphens, OverflowWrap, TextDirection, TextShadow, TextTransform, WordBreak,
    scene::sane_text_spacing,
};

// Compositing layers: transforms, subtree filters, backdrop effects, and blend modes.
use crate::{BlendMode, DropShadow, Point, Transform2D};

#[cfg(target_os = "macos")]
use crate::native_view::MacNativeView;
#[cfg(target_os = "macos")]
use objc2_app_kit::NSView;

const SPACING_UNIT: f32 = 4.0;
const DEFAULT_ANCHOR_GAP: f32 = 8.0;
const DEFAULT_VIEWPORT_MARGIN: f32 = 8.0;

macro_rules! state_cursor_helper {
    ($method:ident, $variant:ident, $css:literal) => {
        #[doc = concat!("Use the native `", $css, "` cursor while this state is active.")]
        pub fn $method(self) -> Self {
            self.cursor(CursorStyle::$variant)
        }
    };
}

macro_rules! spacing_scale_methods {
    ($setter:ident; $($method:ident => $units:expr),+ $(,)?) => {
        $(
            pub fn $method(self) -> Self {
                self.$setter(SPACING_UNIT * $units)
            }
        )+
    };
}

/// Maximum CSS-like box shadows retained by one element or interaction-state override.
pub const MAX_BOX_SHADOWS_PER_ELEMENT: usize = 8;

/// Longest name a group can be declared with or targeted by, in bytes.
pub const MAX_HOVER_GROUP_NAME_BYTES: usize = 256;
/// Most group states — `group_hover` and `group_active` entries together — one element follows.
pub const MAX_GROUP_STYLES_PER_ELEMENT: usize = 8;

/// Largest accepted outline width in logical pixels.
pub const MAX_OUTLINE_WIDTH: f32 = 1_024.0;
/// Largest number of repeated background-image tiles painted for one element.
///
/// A tiling that would exceed this bound falls back to a single tile rather than emitting an
/// unbounded number of per-frame image instances.
pub const MAX_BACKGROUND_IMAGE_TILES: usize = 256;
/// Largest accepted absolute outline offset in logical pixels.
pub const MAX_OUTLINE_OFFSET: f32 = 1_024.0;
/// Largest accepted corner radius in logical pixels.
///
/// Radii are additionally reduced by the CSS uniform-scale rule so two radii sharing one edge
/// can never overlap.
pub const MAX_CORNER_RADIUS: f32 = 4_096.0;

/// Maximum explicit grid tracks accepted on either axis.
///
/// The public grid helpers retain a compact `repeat()` definition, but layout cost still scales
/// with the resolved track count. Keeping this below Taffy's much larger internal safety limit
/// prevents dynamic application data from accidentally creating an expensive desktop layout.
pub const MAX_GRID_TRACKS: u16 = 1_024;

/// Maximum CSS-like container query nodes retained by one window declaration.
pub const MAX_CONTAINER_QUERIES_PER_WINDOW: usize = 1_024;

/// Maximum nested container-query depth resolved in one declaration.
pub const MAX_CONTAINER_QUERY_DEPTH: usize = 16;

/// Maximum targeted desktop mouse declarations attached to one retained element.
///
/// Ordinary elements retain only one optional pointer and allocate nothing for mouse dispatch.
pub const MAX_MOUSE_LISTENERS_PER_ELEMENT: usize = 16;

/// Maximum focused key listeners attached to one retained element.
pub const MAX_KEY_LISTENERS_PER_ELEMENT: usize = 16;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct MouseListenerKey(pub(crate) u64);

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum MouseListenerKind {
    Down,
    Up,
    Move,
    Exit,
    Hover,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct MouseListenerBinding {
    pub(crate) key: MouseListenerKey,
    pub(crate) kind: MouseListenerKind,
    pub(crate) phase: DispatchPhase,
    pub(crate) button: Option<crate::MouseButton>,
    pub(crate) outside: bool,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct KeyListenerKey(pub(crate) u64);

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum KeyListenerKind {
    Down,
    Up,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct KeyListenerBinding {
    pub(crate) key: KeyListenerKey,
    pub(crate) kind: KeyListenerKind,
    pub(crate) phase: DispatchPhase,
}

const DISMISS_ON_ESCAPE: u8 = 1 << 0;
const DISMISS_ON_POINTER_OUTSIDE: u8 = 1 << 1;

/// Independent event boundaries for one dismissible retained surface.
///
/// Keeping this as one byte lets modal components distinguish ordinary dialogs from alert
/// dialogs without adding another pair of booleans to every [`Element`].
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) struct DismissPolicy(u8);

impl DismissPolicy {
    pub(crate) const BOTH: Self = Self(DISMISS_ON_ESCAPE | DISMISS_ON_POINTER_OUTSIDE);

    pub(crate) const fn on_escape(self) -> bool {
        self.0 & DISMISS_ON_ESCAPE != 0
    }

    pub(crate) const fn on_pointer_outside(self) -> bool {
        self.0 & DISMISS_ON_POINTER_OUTSIDE != 0
    }

    pub(crate) const fn with_escape(self) -> Self {
        Self(self.0 | DISMISS_ON_ESCAPE)
    }

    pub(crate) const fn with_pointer_outside(self) -> Self {
        Self(self.0 | DISMISS_ON_POINTER_OUTSIDE)
    }

    pub(crate) const fn is_empty(self) -> bool {
        self.0 == 0
    }
}

const MAX_GRID_LINE: i16 = MAX_GRID_TRACKS as i16 + 1;

/// One explicit CSS-grid track used by [`Element::grid_template_columns`] and
/// [`Element::grid_template_rows`].
///
/// Fractional tracks use `minmax(0, Nfr)`, which matches the web-friendly behavior of GPUI's
/// equal-column helpers and allows content to shrink without forcing overflow.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GridTrack(TrackSizingFunction);

impl GridTrack {
    /// A content-sized `auto` track.
    pub fn auto() -> Self {
        Self(auto())
    }

    /// A track sized to its minimum content contribution.
    pub fn min_content() -> Self {
        Self(min_content())
    }

    /// A track sized to its maximum content contribution.
    pub fn max_content() -> Self {
        Self(max_content())
    }

    /// A fixed logical-pixel track.
    pub fn px(value: f32) -> Self {
        Self(length(finite_nonnegative(value)))
    }

    /// A percentage track expressed as a `0.0..=1.0` fraction of the grid container.
    pub fn percent(fraction: f32) -> Self {
        Self(percent(finite_nonnegative(fraction).min(1.0)))
    }

    /// A flexible `minmax(0, Nfr)` track.
    pub fn fr(fraction: f32) -> Self {
        Self(flex(finite_nonnegative(fraction)))
    }

    /// The common responsive web track `minmax(<minimum px>, <fraction>fr)`.
    pub fn minmax_px_fr(minimum: f32, fraction: f32) -> Self {
        Self(minmax(
            length(finite_nonnegative(minimum)),
            fr(finite_nonnegative(fraction)),
        ))
    }

    /// An `auto` minimum with a fixed fit-content limit in logical pixels.
    pub fn fit_content_px(limit: f32) -> Self {
        Self(fit_content(LengthPercentage::length(finite_nonnegative(
            limit,
        ))))
    }
}

#[derive(Clone, Copy, Debug)]
enum EqualGridTrackSizing {
    Zero,
    MinContent,
    MaxContent,
}

fn finite_nonnegative(value: f32) -> f32 {
    if value.is_finite() {
        value.max(0.0)
    } else {
        0.0
    }
}

fn finite_opacity(value: f32) -> f32 {
    if value.is_finite() {
        value.clamp(0.0, 1.0)
    } else {
        1.0
    }
}

fn finite_length(value: f32) -> f32 {
    if value.is_finite() { value } else { 0.0 }
}

fn equal_grid_tracks(
    count: u16,
    sizing: EqualGridTrackSizing,
) -> Vec<GridTemplateComponent<String>> {
    let count = count.min(MAX_GRID_TRACKS);
    if count == 0 {
        return Vec::new();
    }
    let track: TrackSizingFunction = match sizing {
        EqualGridTrackSizing::Zero => minmax(length(0.0_f32), fr(1.0_f32)),
        EqualGridTrackSizing::MinContent => minmax(min_content(), fr(1.0_f32)),
        EqualGridTrackSizing::MaxContent => minmax(length(0.0_f32), max_content()),
    };
    vec![repeat(count, vec![track])]
}

fn bounded_grid_line(index: i16) -> GridPlacement<String> {
    if index == 0 {
        GridPlacement::Auto
    } else {
        line(index.clamp(-MAX_GRID_LINE, MAX_GRID_LINE))
    }
}

fn bounded_grid_span(span: u16) -> GridPlacement<String> {
    GridPlacement::Span(span.clamp(1, MAX_GRID_TRACKS))
}

/// A stable identifier used for hit testing and retained state.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ElementId(pub(crate) u64);

/// Web-style window drag behavior for a laid-out element.
///
/// A `Drag` region hands primary-button drags to the native window and ignores element-level
/// pointer input. Descendants such as buttons opt back into normal input with `NoDrag`.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum AppRegion {
    Drag,
    NoDrag,
}

impl ElementId {
    pub const fn new(value: u64) -> Self {
        Self(value)
    }

    pub fn named(value: &str) -> Self {
        Self(stable_hash(value.as_bytes()))
    }

    pub const fn as_u64(self) -> u64 {
        self.0
    }

    pub(crate) const fn value(self) -> u64 {
        self.as_u64()
    }
}

impl From<u64> for ElementId {
    fn from(value: u64) -> Self {
        Self::new(value)
    }
}

impl From<usize> for ElementId {
    fn from(value: usize) -> Self {
        Self::new(value as u64)
    }
}

impl From<&str> for ElementId {
    fn from(value: &str) -> Self {
        Self::named(value)
    }
}

impl From<String> for ElementId {
    fn from(value: String) -> Self {
        Self::named(&value)
    }
}

/// A stable handle for programmatic and keyboard focus.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct FocusHandle(ElementId);

impl FocusHandle {
    pub fn new(id: impl Into<ElementId>) -> Self {
        Self(id.into())
    }

    pub const fn id(self) -> ElementId {
        self.0
    }
}

impl From<FocusHandle> for ElementId {
    fn from(value: FocusHandle) -> Self {
        value.id()
    }
}

/// Preferred placement for a floating element relative to its anchor.
///
/// Placement automatically flips to the opposite side when it has more usable space, then shifts
/// inside the window's content viewport.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum AnchorPlacement {
    TopStart,
    Top,
    TopEnd,
    #[default]
    BottomStart,
    Bottom,
    BottomEnd,
    LeftStart,
    Left,
    LeftEnd,
    RightStart,
    Right,
    RightEnd,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum AnchorTarget {
    Element(ElementId),
    Point(crate::Point),
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct AnchorStyle {
    pub target: AnchorTarget,
    pub placement: AnchorPlacement,
    pub gap: f32,
    pub align_offset: f32,
    pub viewport_margin: f32,
    pub sticky: bool,
}

/// Where an anchored element actually landed on the most recently painted frame.
///
/// A declared [`AnchorPlacement`] is only a preference: QuickGUI flips to the opposite side and
/// re-aligns on the cross axis whenever the preferred side does not fit inside the collision
/// viewport. Presentation that has to follow the real placement — a popover arrow, a
/// side-dependent transform origin, a popup sized to the space it was actually given — must read
/// the resolved value instead of guessing from the preference.
///
/// Every rectangle is in window logical coordinates.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ResolvedAnchorPlacement {
    /// The side and cross-axis alignment the element was actually placed with.
    pub placement: AnchorPlacement,
    /// The anchor rectangle the element was placed against.
    pub anchor: crate::Rect,
    /// The placed rectangle of the anchored element itself.
    pub bounds: crate::Rect,
    /// Space left for the element between the anchor and the collision viewport.
    ///
    /// The primary axis measures the gap-adjusted room on [`Self::placement`]'s resolved side; the
    /// cross axis measures the full margin-inset viewport extent. An application sizes a scrolling
    /// popup from this instead of measuring the window itself.
    pub available: crate::Size,
    /// Whether the anchor rectangle left the collision viewport entirely.
    ///
    /// This is the moment Base UI hides a popup whose anchor scrolled out of view; QuickGUI reports
    /// it and leaves the decision to the application.
    pub anchor_hidden: bool,
}

impl ResolvedAnchorPlacement {
    /// The resolved side, discarding the cross-axis alignment.
    pub const fn side(self) -> AnchorSide {
        AnchorSide::of(self.placement)
    }

    /// The resolved cross-axis alignment, discarding the side.
    pub const fn align(self) -> AnchorAlign {
        AnchorAlign::of(self.placement)
    }
}

impl Default for ResolvedAnchorPlacement {
    fn default() -> Self {
        Self {
            placement: AnchorPlacement::BottomStart,
            anchor: crate::Rect::ZERO,
            bounds: crate::Rect::ZERO,
            available: crate::Size::ZERO,
            anchor_hidden: false,
        }
    }
}

#[derive(Debug, Default)]
struct AnchorPlacementInner {
    resolved: Option<ResolvedAnchorPlacement>,
    revision: u64,
}

/// An application-owned receiver for the placement QuickGUI resolved for one anchored element.
///
/// Store the handle on the view next to the state that opens the surface, bind it with
/// [`Element::report_anchor_placement`], and read [`Self::resolved`] while declaring the next
/// frame. The handle retains one small allocation and no task, timer, observer, or idle scheduler
/// source: QuickGUI writes it during the paint it was already performing, and requests exactly one
/// correcting frame when the resolved placement changed, so a settled window stays settled.
///
/// ```
/// use quickgui::{AnchorPlacement, AnchorPlacementHandle, div};
///
/// let placement = AnchorPlacementHandle::new();
/// assert_eq!(placement.resolved(), None);
/// let positioner = div()
///     .anchor_to("trigger", AnchorPlacement::BottomStart)
///     .report_anchor_placement(placement.clone());
/// assert!(positioner.reports_anchor_placement());
/// ```
#[derive(Clone, Default)]
pub struct AnchorPlacementHandle(Rc<RefCell<AnchorPlacementInner>>);

impl AnchorPlacementHandle {
    pub fn new() -> Self {
        Self::default()
    }

    /// The placement resolved on the most recent painted frame, or `None` before the first one.
    pub fn resolved(&self) -> Option<ResolvedAnchorPlacement> {
        self.0.borrow().resolved
    }

    /// The resolved placement, falling back to `preferred` before the first painted frame.
    pub fn placement_or(&self, preferred: AnchorPlacement) -> AnchorPlacement {
        self.resolved()
            .map_or(preferred, |resolved| resolved.placement)
    }

    /// Forget the last resolved placement so a reopened surface cannot read a stale side.
    pub fn clear(&self) {
        let mut inner = self.0.borrow_mut();
        if inner.resolved.is_some() {
            inner.resolved = None;
            inner.revision = inner.revision.wrapping_add(1);
        }
    }

    pub(crate) fn revision(&self) -> u64 {
        self.0.borrow().revision
    }

    pub(crate) fn report(&self, resolved: ResolvedAnchorPlacement) {
        let mut inner = self.0.borrow_mut();
        if inner.resolved == Some(resolved) {
            return;
        }
        inner.resolved = Some(resolved);
        inner.revision = inner.revision.wrapping_add(1);
    }
}

impl fmt::Debug for AnchorPlacementHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AnchorPlacementHandle")
            .field("resolved", &self.resolved())
            .finish_non_exhaustive()
    }
}

impl PartialEq for AnchorPlacementHandle {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }
}

#[derive(Default)]
struct LayoutBoundsInner {
    bounds: Option<Rect>,
    revision: u64,
}

/// An application-owned receiver for the window-relative bounds QuickGUI laid out for one element.
///
/// Store the handle next to the state that depends on the element's real size — a splitter that
/// must match its container, a scroll area that must know its viewport and content extents — bind
/// it with [`Element::report_bounds`], and read [`Self::bounds`] while declaring the next frame.
/// Like [`AnchorPlacementHandle`], it retains one small allocation and no task, timer, observer, or
/// idle scheduler source: QuickGUI writes it during the paint it was already performing and
/// requests exactly one correcting frame when the bounds changed, so a settled window stays
/// settled.
///
/// ```
/// use quickgui::{LayoutBoundsHandle, div};
///
/// let bounds = LayoutBoundsHandle::new();
/// assert_eq!(bounds.bounds(), None);
/// let pane = div().report_bounds(bounds.clone());
/// assert!(pane.reports_bounds());
/// ```
#[derive(Clone, Default)]
pub struct LayoutBoundsHandle(Rc<RefCell<LayoutBoundsInner>>);

impl LayoutBoundsHandle {
    pub fn new() -> Self {
        Self::default()
    }

    /// The bounds painted on the most recent frame, in logical window coordinates, or `None`
    /// before the first painted frame.
    pub fn bounds(&self) -> Option<Rect> {
        self.0.borrow().bounds
    }

    /// Forget the last painted bounds so a remounted element cannot read a stale size.
    pub fn clear(&self) {
        let mut inner = self.0.borrow_mut();
        if inner.bounds.is_some() {
            inner.bounds = None;
            inner.revision = inner.revision.wrapping_add(1);
        }
    }

    pub(crate) fn revision(&self) -> u64 {
        self.0.borrow().revision
    }

    pub(crate) fn report(&self, bounds: Rect) {
        let mut inner = self.0.borrow_mut();
        if inner.bounds == Some(bounds) {
            return;
        }
        inner.bounds = Some(bounds);
        inner.revision = inner.revision.wrapping_add(1);
    }
}

impl fmt::Debug for LayoutBoundsHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LayoutBoundsHandle")
            .field("bounds", &self.bounds())
            .finish_non_exhaustive()
    }
}

impl PartialEq for LayoutBoundsHandle {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }
}

/// The side of its anchor an element was placed on.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum AnchorSide {
    Top,
    #[default]
    Bottom,
    Left,
    Right,
}

impl AnchorSide {
    /// The side half of a full placement.
    pub const fn of(placement: AnchorPlacement) -> Self {
        match placement {
            AnchorPlacement::TopStart | AnchorPlacement::Top | AnchorPlacement::TopEnd => Self::Top,
            AnchorPlacement::BottomStart | AnchorPlacement::Bottom | AnchorPlacement::BottomEnd => {
                Self::Bottom
            }
            AnchorPlacement::LeftStart | AnchorPlacement::Left | AnchorPlacement::LeftEnd => {
                Self::Left
            }
            AnchorPlacement::RightStart | AnchorPlacement::Right | AnchorPlacement::RightEnd => {
                Self::Right
            }
        }
    }

    /// The side a flip would move to.
    pub const fn opposite(self) -> Self {
        match self {
            Self::Top => Self::Bottom,
            Self::Bottom => Self::Top,
            Self::Left => Self::Right,
            Self::Right => Self::Left,
        }
    }

    /// Whether the side runs along the vertical axis, so the cross axis is horizontal.
    pub const fn is_vertical(self) -> bool {
        matches!(self, Self::Top | Self::Bottom)
    }
}

/// The cross-axis alignment an element was placed with.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum AnchorAlign {
    #[default]
    Start,
    Center,
    End,
}

impl AnchorAlign {
    /// The alignment half of a full placement.
    pub const fn of(placement: AnchorPlacement) -> Self {
        match placement {
            AnchorPlacement::TopStart
            | AnchorPlacement::BottomStart
            | AnchorPlacement::LeftStart
            | AnchorPlacement::RightStart => Self::Start,
            AnchorPlacement::Top
            | AnchorPlacement::Bottom
            | AnchorPlacement::Left
            | AnchorPlacement::Right => Self::Center,
            AnchorPlacement::TopEnd
            | AnchorPlacement::BottomEnd
            | AnchorPlacement::LeftEnd
            | AnchorPlacement::RightEnd => Self::End,
        }
    }
}

/// Compose a side and a cross-axis alignment into one [`AnchorPlacement`].
pub const fn anchor_placement(side: AnchorSide, align: AnchorAlign) -> AnchorPlacement {
    match (side, align) {
        (AnchorSide::Top, AnchorAlign::Start) => AnchorPlacement::TopStart,
        (AnchorSide::Top, AnchorAlign::Center) => AnchorPlacement::Top,
        (AnchorSide::Top, AnchorAlign::End) => AnchorPlacement::TopEnd,
        (AnchorSide::Bottom, AnchorAlign::Start) => AnchorPlacement::BottomStart,
        (AnchorSide::Bottom, AnchorAlign::Center) => AnchorPlacement::Bottom,
        (AnchorSide::Bottom, AnchorAlign::End) => AnchorPlacement::BottomEnd,
        (AnchorSide::Left, AnchorAlign::Start) => AnchorPlacement::LeftStart,
        (AnchorSide::Left, AnchorAlign::Center) => AnchorPlacement::Left,
        (AnchorSide::Left, AnchorAlign::End) => AnchorPlacement::LeftEnd,
        (AnchorSide::Right, AnchorAlign::Start) => AnchorPlacement::RightStart,
        (AnchorSide::Right, AnchorAlign::Center) => AnchorPlacement::Right,
        (AnchorSide::Right, AnchorAlign::End) => AnchorPlacement::RightEnd,
    }
}

/// Platform-neutral semantics used to build the native accessibility tree.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum AccessibilityRole {
    #[default]
    GenericContainer,
    Label,
    Button,
    Link,
    Image,
    List,
    ListItem,
    Heading,
    CheckBox,
    RadioButton,
    RadioGroup,
    Switch,
    TextInput,
    PasswordInput,
    MultilineTextInput,
    Dialog,
    AlertDialog,
    Menu,
    MenuItem,
    MenuItemCheckBox,
    MenuItemRadio,
    /// A non-interactive visual or structural divider.
    ///
    /// AccessKit names the platform-neutral role `Splitter`; without value-changing actions it
    /// projects as the native separator/divider role exposed by each platform adapter.
    Separator,
    Group,
    Region,
    ListBox,
    ListBoxOption,
    ComboBox,
    EditableComboBox,
    Table,
    Tree,
    Grid,
    Row,
    ColumnHeader,
    RowHeader,
    GridCell,
    TreeItem,
    Tab,
    TabList,
    TabPanel,
    Tooltip,
    Form,
    /// A continuous or stepped range control whose value the user changes directly.
    Slider,
    /// A numeric text control with paired increment and decrement affordances.
    SpinButton,
    /// A determinate or indeterminate task-completion indicator.
    ProgressIndicator,
    /// A static measurement inside a known range, such as disk usage.
    Meter,
    /// A movable divider between two resizable panes.
    ///
    /// AccessKit names the platform-neutral role `Splitter`. Unlike
    /// [`AccessibilityRole::Separator`], this variant is focusable and carries a numeric value.
    SplitterHandle,
    /// A grouping of controls presented as one compact set of application commands.
    Toolbar,
    /// A button with retained pressed state, distinct from a checkbox's checked state.
    ToggleButton,
    /// An in-window horizontal set of menu triggers.
    MenuBar,
    /// An assertive live region for urgent, time-sensitive messages.
    Alert,
    /// A polite live region for advisory status messages.
    Status,
    /// A collection of navigation links presented as one landmark.
    Navigation,
    /// A clipped scroll viewport whose content is larger than its box.
    ScrollView,
    /// A scrollbar whose numeric value is the current scroll position.
    ScrollBar,
}

/// Axis projected for accessibility roles whose behavior changes with orientation.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AccessibilityOrientation {
    Horizontal,
    Vertical,
}

/// Announcement urgency projected for one live region.
///
/// QuickGUI never polls a live region. The projection is rebuilt only when the application
/// rebuilds the mounted tree, so an unchanged region announces exactly once.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AccessibilityLive {
    /// Announce after the current utterance finishes.
    Polite,
    /// Interrupt the current utterance.
    Assertive,
}

/// Numeric range projected by slider, spin-button, progress, meter, and splitter roles.
///
/// Every field is optional so an indeterminate progress indicator can expose bounds without a
/// current value. Non-finite inputs are dropped rather than projected.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct AccessibilityValueRange {
    pub value: Option<f64>,
    pub min: Option<f64>,
    pub max: Option<f64>,
    pub step: Option<f64>,
}

impl AccessibilityValueRange {
    /// Create a determinate range. Non-finite components are omitted from the native projection.
    pub fn new(value: f64, min: f64, max: f64) -> Self {
        Self {
            value: finite_value(value),
            min: finite_value(min),
            max: finite_value(max),
            step: None,
        }
    }

    /// Create a range with no current value, used by indeterminate progress indicators.
    pub fn indeterminate(min: f64, max: f64) -> Self {
        Self {
            value: None,
            min: finite_value(min),
            max: finite_value(max),
            step: None,
        }
    }

    pub fn step(mut self, step: f64) -> Self {
        self.step = finite_value(step).filter(|step| *step > 0.0);
        self
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.value.is_none() && self.min.is_none() && self.max.is_none() && self.step.is_none()
    }
}

fn finite_value(value: f64) -> Option<f64> {
    value.is_finite().then_some(value)
}

/// Kind of popover exposed by a trigger to assistive technology.
///
/// This mirrors the finite ARIA/AccessKit `has-popup` vocabulary. It describes the controlled
/// surface without imposing a visual component implementation.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AccessibilityPopover {
    Menu,
    ListBox,
    Tree,
    Grid,
    Dialog,
}

/// Sort order exposed by a table or grid column header.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AccessibilitySortDirection {
    Ascending,
    Descending,
    Other,
}

/// Suggestion presentation exposed by an editable combobox or text input.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AccessibilityAutoComplete {
    Inline,
    List,
    Both,
}

/// The controlled checked state exposed by checkbox-like accessibility roles.
///
/// [`ToggleState::Mixed`] corresponds to the web `indeterminate` state and is intended primarily
/// for checkboxes that summarize a partially selected collection.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ToggleState {
    #[default]
    Off,
    On,
    Mixed,
}

impl ToggleState {
    pub const fn is_on(self) -> bool {
        matches!(self, Self::On)
    }

    pub const fn is_mixed(self) -> bool {
        matches!(self, Self::Mixed)
    }
}

impl From<bool> for ToggleState {
    fn from(checked: bool) -> Self {
        if checked { Self::On } else { Self::Off }
    }
}

/// CSS-like policy for selecting immutable text with the pointer.
///
/// [`UserSelect::Auto`] keeps ordinary text selectable while inheriting suppression from controls
/// such as buttons and drag sources. [`UserSelect::Text`] explicitly re-enables selection and
/// [`UserSelect::None`] disables it for the complete subtree.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum UserSelect {
    #[default]
    Auto,
    Text,
    None,
}

/// Whether an element subtree is painted while retaining its layout box.
///
/// This mirrors GPUI's visibility model: [`Visibility::Hidden`] suppresses the element and its
/// descendants from paint, input, focus, and accessibility, but their layout still participates.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum Visibility {
    #[default]
    Visible,
    Hidden,
}

/// Converts common values into an [`Element`] for `.child(...)` and `.children(...)`.
pub trait IntoElement {
    fn into_element(self) -> Element;
}

impl IntoElement for Element {
    fn into_element(self) -> Element {
        self
    }
}

impl IntoElement for String {
    fn into_element(self) -> Element {
        text(self)
    }
}

impl IntoElement for Arc<str> {
    fn into_element(self) -> Element {
        text(self)
    }
}

impl IntoElement for &str {
    fn into_element(self) -> Element {
        text(Arc::<str>::from(self))
    }
}

impl IntoElement for Cow<'_, str> {
    fn into_element(self) -> Element {
        text(Arc::<str>::from(self.as_ref()))
    }
}

impl IntoElement for StyledText {
    fn into_element(self) -> Element {
        Element::styled_text(self)
    }
}

#[derive(Clone, Debug)]
pub(crate) enum ElementKind {
    Container,
    ContainerQuery(ContainerQueryElement),
    Text(Arc<str>),
    StyledText(StyledText),
    Image(ImageElement),
    Svg(SvgElement),
    Path(PathElement),
    Canvas(CanvasElement),
    CustomShader(ShaderElement),
    TextInput(TextInputElement),
    #[cfg(target_os = "macos")]
    NativeView(MacNativeView),
}

#[derive(Clone)]
pub(crate) struct ContainerQueryElement {
    render: Rc<dyn Fn(crate::Size) -> Element>,
    pub(crate) resolved_size: Option<crate::Size>,
    /// Declarative motion mounted directly by this callback. Descendant query callbacks retain
    /// their own IDs, which lets a size change unmount exactly the replaced subtree without
    /// disturbing motion elsewhere in the window.
    pub(crate) resolved_motion_ids: Vec<ElementId>,
    pub(crate) layout_pending: bool,
}

impl ContainerQueryElement {
    pub(crate) fn render(&self, size: crate::Size) -> Element {
        (self.render)(size)
    }
}

impl fmt::Debug for ContainerQueryElement {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("ContainerQueryElement")
            .field("resolved_size", &self.resolved_size)
            .field("resolved_motion_count", &self.resolved_motion_ids.len())
            .field("layout_pending", &self.layout_pending)
            .finish_non_exhaustive()
    }
}

#[derive(Clone, Debug)]
pub(crate) struct ImageElement {
    pub source: ImageSource,
    pub object_fit: ObjectFit,
    pub resolved: ImageResolution,
    pub loading: Option<ImageReplacement>,
    pub fallback: Option<ImageReplacement>,
}

#[derive(Clone, Debug)]
pub(crate) struct SvgElement {
    pub svg: Svg,
    pub object_fit: ObjectFit,
    pub transform: SvgTransform,
}

#[derive(Clone, Debug)]
pub(crate) struct PathElement {
    pub path: Path,
    pub object_fit: ObjectFit,
    pub background: Option<Background>,
}

#[derive(Clone)]
pub(crate) struct CanvasElement {
    pub painter: Rc<CanvasPainter>,
}

#[derive(Clone, Debug)]
pub(crate) struct ShaderElement {
    pub shader: CustomShader,
    pub parameters: ShaderParameters,
}

type CanvasPainter = dyn for<'a> Fn(Rect, &mut Canvas<'a>);

impl fmt::Debug for CanvasElement {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("CanvasElement(..)")
    }
}

#[derive(Clone, Debug)]
pub(crate) enum ImageResolution {
    Ready(Image),
    Animated(AnimatedImage),
    Loading,
    Failed,
}

#[derive(Clone)]
pub(crate) struct ImageReplacement(Rc<dyn Fn() -> Element>);

impl ImageReplacement {
    fn new<E: IntoElement + 'static>(render: impl Fn() -> E + 'static) -> Self {
        Self(Rc::new(move || render().into_element()))
    }

    pub(crate) fn render(&self) -> Element {
        (self.0)()
    }
}

impl fmt::Debug for ImageReplacement {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("ImageReplacement(..)")
    }
}

#[derive(Clone, Debug)]
pub(crate) struct TextInputElement {
    pub value: Arc<str>,
    pub highlights: Arc<[TextHighlight]>,
    pub placeholder: Arc<str>,
    pub multiline: bool,
    pub password: bool,
    pub constraints: InputConstraints,
    #[cfg(feature = "text-input-decorations")]
    pub editor: Option<crate::TextInputGutter>,
}

pub(crate) type InputFilterCallback = Arc<dyn Fn(&str) -> bool>;

#[derive(Clone, Default)]
pub(crate) struct InputConstraints {
    pub max_length: Option<usize>,
    pub filter: Option<InputFilterCallback>,
    /// Per-input text checking overrides layered over the application policy.
    pub text_checking: TextCheckingOverrides,
    #[cfg(feature = "text-input-decorations")]
    pub editor: Option<crate::TextInputIndentation>,
}

impl fmt::Debug for InputConstraints {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut debug = formatter.debug_struct("InputConstraints");
        debug
            .field("max_length", &self.max_length)
            .field("filter", &self.filter.as_ref().map(|_| "InputFilter(..)"))
            .field("text_checking", &self.text_checking);
        #[cfg(feature = "text-input-decorations")]
        debug.field("editor", &self.editor);
        debug.finish()
    }
}

/// How a background image is scaled inside its element box.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum BackgroundSize {
    /// Use the decoded pixel size as the logical tile size.
    #[default]
    Auto,
    /// Scale preserving aspect ratio until the tile covers the whole box.
    Cover,
    /// Scale preserving aspect ratio until the tile fits inside the box.
    Contain,
    /// An explicit logical tile size.
    Fixed(f32, f32),
}

/// Which axes a background image tiles along.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum BackgroundRepeat {
    /// One tile only.
    #[default]
    NoRepeat,
    RepeatX,
    RepeatY,
    Repeat,
}

impl BackgroundRepeat {
    pub(crate) fn repeats_x(self) -> bool {
        matches!(self, Self::RepeatX | Self::Repeat)
    }

    pub(crate) fn repeats_y(self) -> bool {
        matches!(self, Self::RepeatY | Self::Repeat)
    }
}

/// Where a background tile is anchored, as a fraction of the free space in the element box.
///
/// `0.0` aligns with the start edge, `0.5` centers, and `1.0` aligns with the end edge. Values
/// outside `0.0..=1.0` are clamped so a background image can never escape its own tiling grid.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BackgroundPosition {
    pub x: f32,
    pub y: f32,
}

impl Default for BackgroundPosition {
    fn default() -> Self {
        Self::CENTER
    }
}

impl BackgroundPosition {
    pub const TOP_LEFT: Self = Self { x: 0.0, y: 0.0 };
    pub const TOP: Self = Self { x: 0.5, y: 0.0 };
    pub const TOP_RIGHT: Self = Self { x: 1.0, y: 0.0 };
    pub const LEFT: Self = Self { x: 0.0, y: 0.5 };
    pub const CENTER: Self = Self { x: 0.5, y: 0.5 };
    pub const RIGHT: Self = Self { x: 1.0, y: 0.5 };
    pub const BOTTOM_LEFT: Self = Self { x: 0.0, y: 1.0 };
    pub const BOTTOM: Self = Self { x: 0.5, y: 1.0 };
    pub const BOTTOM_RIGHT: Self = Self { x: 1.0, y: 1.0 };

    pub fn new(x: f32, y: f32) -> Self {
        Self {
            x: if x.is_finite() {
                x.clamp(0.0, 1.0)
            } else {
                0.5
            },
            y: if y.is_finite() {
                y.clamp(0.0, 1.0)
            } else {
                0.5
            },
        }
    }
}

/// A raster background painted behind an element's children and inside its rounded corners.
#[derive(Clone, Debug, PartialEq)]
pub struct BackgroundImage {
    pub image: Image,
    pub size: BackgroundSize,
    pub repeat: BackgroundRepeat,
    pub position: BackgroundPosition,
}

impl BackgroundImage {
    pub fn new(image: Image) -> Self {
        Self {
            image,
            size: BackgroundSize::Auto,
            repeat: BackgroundRepeat::NoRepeat,
            position: BackgroundPosition::CENTER,
        }
    }

    /// The logical tile size for `bounds`, or `None` when nothing can be painted.
    pub(crate) fn tile_size(&self, bounds: Rect) -> Option<crate::Size> {
        let intrinsic = self.image.size();
        if intrinsic.width <= 0.0 || intrinsic.height <= 0.0 {
            return None;
        }
        let size = match self.size {
            BackgroundSize::Auto => intrinsic,
            BackgroundSize::Fixed(width, height) => crate::Size {
                width: if width.is_finite() {
                    width.max(0.0)
                } else {
                    0.0
                },
                height: if height.is_finite() {
                    height.max(0.0)
                } else {
                    0.0
                },
            },
            BackgroundSize::Cover | BackgroundSize::Contain => {
                if bounds.width <= 0.0 || bounds.height <= 0.0 {
                    return None;
                }
                let horizontal = bounds.width / intrinsic.width;
                let vertical = bounds.height / intrinsic.height;
                let scale = if matches!(self.size, BackgroundSize::Cover) {
                    horizontal.max(vertical)
                } else {
                    horizontal.min(vertical)
                };
                crate::Size {
                    width: intrinsic.width * scale,
                    height: intrinsic.height * scale,
                }
            }
        };
        (size.width > 0.0 && size.height > 0.0 && size.width.is_finite() && size.height.is_finite())
            .then_some(size)
    }
}

/// A ring painted outside the border box without participating in layout.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Outline {
    pub width: f32,
    pub color: Color,
    /// Gap between the border box and the inner edge of the ring. May be negative.
    pub offset: f32,
    pub style: BorderStyle,
}

impl Outline {
    /// A solid outline with no offset.
    pub fn new(width: f32, color: Color) -> Self {
        Self {
            width: finite_nonnegative(width).min(MAX_OUTLINE_WIDTH),
            color,
            offset: 0.0,
            style: BorderStyle::Solid,
        }
    }

    pub fn offset(mut self, offset: f32) -> Self {
        self.offset = if offset.is_finite() {
            offset.clamp(-MAX_OUTLINE_OFFSET, MAX_OUTLINE_OFFSET)
        } else {
            0.0
        };
        self
    }

    pub fn style(mut self, style: BorderStyle) -> Self {
        self.style = style;
        self
    }

    /// The ring rectangle for a border box, or `None` when nothing is painted.
    pub(crate) fn ring(self, bounds: Rect) -> Option<Rect> {
        if self.width <= 0.0 || self.color.a <= 0.0 {
            return None;
        }
        let grow = self.offset + self.width;
        let left = bounds.x - grow;
        let top = bounds.y - grow;
        let width = bounds.width + grow * 2.0;
        let height = bounds.height + grow * 2.0;
        if width <= 0.0 || height <= 0.0 {
            return None;
        }
        Some(Rect::new(left, top, width, height))
    }
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct VisualStyle {
    pub background: Option<Color>,
    /// A bounded multi-stop gradient painted instead of `background`.
    pub background_gradient: Option<Gradient>,
    /// A raster background painted above the background color and behind children.
    pub background_image: Option<Box<BackgroundImage>>,
    /// A bounded color-filter chain applied to this element's own raster content.
    pub filters: Filters,
    pub border_color: Option<Color>,
    pub border_widths: Insets,
    pub border_style: BorderStyle,
    pub radius: f32,
    /// Per-corner radii. When present they replace `radius` and are not transitioned.
    pub corner_radii: Option<Corners>,
    /// Explicit radii for a solid background anchored to the nearest scroll viewport.
    /// Used by virtual rows whose visible edge moves through the viewport's corners.
    pub scroll_background_corners: Option<Corners>,
    pub scroll_clip_insets: Option<Insets>,
    pub scroll_clip_corners: Option<Corners>,
    pub outline: Option<Outline>,
    pub shadows: Option<Arc<[BoxShadow]>>,
    pub opacity: f32,
    /// A subtree transform. Anything but a whole-pixel translation opens a compositing group.
    pub transform: Transform2D,
    /// Where the transform acts, as a fraction of the element's border box.
    pub transform_origin: Point,
    /// Color filters applied to what is already painted behind this element.
    pub backdrop: Filters,
    /// How this element's subtree combines with what is already painted behind it.
    pub blend: BlendMode,
}

impl VisualStyle {
    /// The resolved corner radii, preferring explicit per-corner values.
    pub(crate) fn corners(&self, radius: f32) -> Corners {
        self.corner_radii.unwrap_or(Corners::all(radius))
    }
}

impl Default for VisualStyle {
    fn default() -> Self {
        Self {
            background: None,
            background_gradient: None,
            background_image: None,
            filters: Filters::none(),
            border_color: None,
            border_widths: Insets::default(),
            border_style: BorderStyle::Solid,
            radius: 0.0,
            corner_radii: None,
            scroll_background_corners: None,
            scroll_clip_insets: None,
            scroll_clip_corners: None,
            outline: None,
            shadows: None,
            opacity: 1.0,
            transform: Transform2D::IDENTITY,
            transform_origin: Point::new(0.5, 0.5),
            backdrop: Filters::none(),
            blend: BlendMode::Normal,
        }
    }
}

/// Non-layout overrides for hover, pressed, focus, validation, and drag states.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ElementStateStyle {
    pub(crate) background: Option<Color>,
    pub(crate) background_gradient: Option<Gradient>,
    pub(crate) outline: Option<Outline>,
    pub(crate) border_color: Option<Color>,
    pub(crate) border_width: Option<f32>,
    pub(crate) radius: Option<f32>,
    pub(crate) text_color: Option<Color>,
    pub(crate) shadows: Option<Arc<[BoxShadow]>>,
    pub(crate) opacity: Option<f32>,
    pub(crate) cursor_style: Option<CursorStyle>,
    pub(crate) transform: Option<Transform2D>,
    pub(crate) transform_origin: Option<Point>,
}

impl ElementStateStyle {
    /// Set the native cursor while this state is active.
    pub fn cursor(mut self, cursor: CursorStyle) -> Self {
        self.cursor_style = Some(cursor);
        self
    }

    state_cursor_helper!(cursor_default, Arrow, "default");
    state_cursor_helper!(cursor_pointer, PointingHand, "pointer");
    state_cursor_helper!(cursor_text, IBeam, "text");
    state_cursor_helper!(cursor_move, ClosedHand, "move");
    state_cursor_helper!(cursor_not_allowed, OperationNotAllowed, "not-allowed");
    state_cursor_helper!(cursor_context_menu, ContextualMenu, "context-menu");
    state_cursor_helper!(cursor_crosshair, Crosshair, "crosshair");
    state_cursor_helper!(
        cursor_vertical_text,
        IBeamCursorForVerticalLayout,
        "vertical-text"
    );
    state_cursor_helper!(cursor_alias, DragLink, "alias");
    state_cursor_helper!(cursor_copy, DragCopy, "copy");
    state_cursor_helper!(cursor_no_drop, OperationNotAllowed, "no-drop");
    state_cursor_helper!(cursor_grab, OpenHand, "grab");
    state_cursor_helper!(cursor_grabbing, ClosedHand, "grabbing");
    state_cursor_helper!(cursor_ew_resize, ResizeLeftRight, "ew-resize");
    state_cursor_helper!(cursor_ns_resize, ResizeUpDown, "ns-resize");
    state_cursor_helper!(cursor_nesw_resize, ResizeUpRightDownLeft, "nesw-resize");
    state_cursor_helper!(cursor_nwse_resize, ResizeUpLeftDownRight, "nwse-resize");
    state_cursor_helper!(cursor_col_resize, ResizeColumn, "col-resize");
    state_cursor_helper!(cursor_row_resize, ResizeRow, "row-resize");
    state_cursor_helper!(cursor_n_resize, ResizeUp, "n-resize");
    state_cursor_helper!(cursor_e_resize, ResizeRight, "e-resize");
    state_cursor_helper!(cursor_s_resize, ResizeDown, "s-resize");
    state_cursor_helper!(cursor_w_resize, ResizeLeft, "w-resize");

    pub fn bg(mut self, color: Color) -> Self {
        self.background = Some(color);
        self.background_gradient = None;
        self
    }

    /// Transform the subtree while this state is active.
    ///
    /// Layout never moves; only paint and hit testing do, so a hover lift or press shrink costs
    /// no relayout. A transform in a state overrides the element's own
    /// [`Element::transform`](crate::Element::transform).
    pub fn transform(mut self, transform: Transform2D) -> Self {
        self.transform = Some(transform);
        self
    }

    /// Move the point this state's transform acts around, as a fraction of the border box.
    pub fn transform_origin(mut self, x: f32, y: f32) -> Self {
        self.transform_origin = Some(Point::new(x, y));
        self
    }

    /// Translate the subtree by logical pixels while this state is active.
    pub fn translate(self, x: f32, y: f32) -> Self {
        self.transform(Transform2D::translate(x, y))
    }

    /// Rotate the subtree around its transform origin while this state is active.
    pub fn rotate_degrees(self, degrees: f32) -> Self {
        self.transform(Transform2D::rotate_degrees(degrees))
    }

    /// Scale the subtree around its transform origin while this state is active.
    pub fn scale(self, x: f32, y: f32) -> Self {
        self.transform(Transform2D::scale(x, y))
    }

    /// Scale the subtree uniformly around its transform origin while this state is active.
    pub fn scale_uniform(self, scale: f32) -> Self {
        self.transform(Transform2D::scale_uniform(scale))
    }

    /// Replace the background with a bounded multi-stop gradient while this state is active.
    ///
    /// Gradients are swapped, not interpolated: only the transitionable solid background,
    /// border, radius, shadow, opacity, and text color values animate.
    pub fn bg_gradient(mut self, gradient: impl Into<Background>) -> Self {
        match gradient.into() {
            Background::Solid(color) => {
                self.background = Some(color);
                self.background_gradient = None;
            }
            other => {
                self.background_gradient = other.as_gradient();
            }
        }
        self
    }

    /// Paint an outline ring outside the border box while this state is active.
    pub fn outline(mut self, width: f32, color: Color) -> Self {
        self.outline = Some(Outline::new(width, color));
        self
    }

    /// Paint an offset outline ring outside the border box while this state is active.
    pub fn outline_offset(mut self, width: f32, color: Color, offset: f32) -> Self {
        self.outline = Some(Outline::new(width, color).offset(offset));
        self
    }

    /// Remove any inherited outline while this state is active.
    pub fn outline_none(mut self) -> Self {
        self.outline = Some(Outline::new(0.0, Color::TRANSPARENT));
        self
    }

    /// Paint this state's outline as evenly spaced dashes.
    pub fn outline_dashed(self) -> Self {
        self.outline_style(BorderStyle::Dashed)
    }

    /// Paint this state's outline as evenly distributed dots.
    pub fn outline_dotted(self) -> Self {
        self.outline_style(BorderStyle::Dotted)
    }

    fn outline_style(mut self, style: BorderStyle) -> Self {
        let outline = self
            .outline
            .unwrap_or_else(|| Outline::new(0.0, Color::TRANSPARENT));
        self.outline = Some(outline.style(style));
        self
    }

    pub fn border_color(mut self, color: Color) -> Self {
        self.border_color = Some(color);
        self
    }

    /// Override the paint-only border width while this state is active, keeping the border color.
    pub fn border_width(mut self, width: f32) -> Self {
        self.border_width = Some(finite_nonnegative(width));
        self
    }

    /// Paint an inside border without changing layout.
    pub fn border(mut self, width: f32, color: Color) -> Self {
        self.border_width = Some(width.max(0.0));
        self.border_color = Some(color);
        self
    }

    /// Override the paint-only corner radius while this state is active.
    pub fn rounded(mut self, radius: f32) -> Self {
        self.radius = Some(finite_nonnegative(radius));
        self
    }

    pub fn text_color(mut self, color: Color) -> Self {
        self.text_color = Some(color);
        self
    }

    /// Set the opacity of this element and its descendants while this state is active.
    pub fn opacity(mut self, opacity: f32) -> Self {
        self.opacity = Some(finite_opacity(opacity));
        self
    }

    /// Replace the element's shadows while this state is active.
    pub fn shadow(mut self, shadow: BoxShadow) -> Self {
        self.shadows = Some(Arc::from([shadow]));
        self
    }

    /// Replace the element's shadows while this state is active.
    pub fn shadows(mut self, shadows: impl IntoIterator<Item = BoxShadow>) -> Self {
        self.shadows = Some(collect_box_shadows(shadows));
        self
    }

    /// Remove all shadows while this state is active.
    pub fn shadow_none(mut self) -> Self {
        self.shadows = Some(Arc::from([]));
        self
    }

    pub fn shadow_sm(self) -> Self {
        self.shadow(shadow_sm_preset())
    }

    pub fn shadow_md(self) -> Self {
        self.shadows(shadow_md_preset())
    }

    pub fn shadow_lg(self) -> Self {
        self.shadows(shadow_lg_preset())
    }

    pub fn shadow_xl(self) -> Self {
        self.shadows(shadow_xl_preset())
    }

    pub fn shadow_2xl(self) -> Self {
        self.shadow(shadow_2xl_preset())
    }

    /// Lay `other` over this style: every value it declares replaces the one here.
    pub(crate) fn overlay(&mut self, other: &Self) {
        if other.background.is_some() {
            self.background = other.background;
        }
        if other.background_gradient.is_some() {
            self.background_gradient = other.background_gradient;
        }
        if other.outline.is_some() {
            self.outline = other.outline;
        }
        if other.border_color.is_some() {
            self.border_color = other.border_color;
        }
        if other.border_width.is_some() {
            self.border_width = other.border_width;
        }
        if other.radius.is_some() {
            self.radius = other.radius;
        }
        if other.text_color.is_some() {
            self.text_color = other.text_color;
        }
        if other.shadows.is_some() {
            self.shadows = other.shadows.clone();
        }
        if other.opacity.is_some() {
            self.opacity = other.opacity;
        }
        if other.cursor_style.is_some() {
            self.cursor_style = other.cursor_style;
        }
        if other.transform.is_some() {
            self.transform = other.transform;
        }
        if other.transform_origin.is_some() {
            self.transform_origin = other.transform_origin;
        }
    }

    fn has_paint_overrides(&self) -> bool {
        self.background.is_some()
            || self.background_gradient.is_some()
            || self.outline.is_some()
            || self.border_color.is_some()
            || self.border_width.is_some()
            || self.radius.is_some()
            || self.text_color.is_some()
            || self.shadows.is_some()
            || self.opacity.is_some()
    }
}

/// Which state of an ancestor group a member's style follows.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum GroupState {
    /// The group is hovered.
    Hover,
    /// A press inside the group is held.
    Active,
}

/// One paint-only style a member paints while an ancestor group is in one state.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct GroupStateStyle {
    pub(crate) state: GroupState,
    /// The named group to follow; `None` follows the nearest group.
    pub(crate) target: Option<Arc<str>>,
    pub(crate) style: ElementStateStyle,
}

fn shadow_sm_preset() -> BoxShadow {
    BoxShadow::new(0.0, 1.0, Color::rgba8(0, 0, 0, 13)).blur_radius(2.0)
}

fn shadow_md_preset() -> [BoxShadow; 2] {
    [
        BoxShadow::new(0.0, 4.0, Color::rgba8(0, 0, 0, 26))
            .blur_radius(6.0)
            .spread_radius(-1.0),
        BoxShadow::new(0.0, 2.0, Color::rgba8(0, 0, 0, 26))
            .blur_radius(4.0)
            .spread_radius(-2.0),
    ]
}

fn shadow_lg_preset() -> [BoxShadow; 2] {
    [
        BoxShadow::new(0.0, 10.0, Color::rgba8(0, 0, 0, 26))
            .blur_radius(15.0)
            .spread_radius(-3.0),
        BoxShadow::new(0.0, 4.0, Color::rgba8(0, 0, 0, 26))
            .blur_radius(6.0)
            .spread_radius(-4.0),
    ]
}

fn shadow_xl_preset() -> [BoxShadow; 2] {
    [
        BoxShadow::new(0.0, 20.0, Color::rgba8(0, 0, 0, 26))
            .blur_radius(25.0)
            .spread_radius(-5.0),
        BoxShadow::new(0.0, 8.0, Color::rgba8(0, 0, 0, 26))
            .blur_radius(10.0)
            .spread_radius(-6.0),
    ]
}

fn shadow_2xl_preset() -> BoxShadow {
    BoxShadow::new(0.0, 25.0, Color::rgba8(0, 0, 0, 64))
        .blur_radius(50.0)
        .spread_radius(-12.0)
}

fn collect_box_shadows(shadows: impl IntoIterator<Item = BoxShadow>) -> Arc<[BoxShadow]> {
    let mut retained = Vec::with_capacity(2);
    for shadow in shadows {
        assert!(
            retained.len() < MAX_BOX_SHADOWS_PER_ELEMENT,
            "one element retains at most {MAX_BOX_SHADOWS_PER_ELEMENT} box shadows"
        );
        retained.push(shadow);
    }
    Arc::from(retained)
}

#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct AccessibilityStyle {
    pub hidden: bool,
    pub role: AccessibilityRole,
    pub label: Option<Arc<str>>,
    pub value: Option<Arc<str>>,
    pub disabled: bool,
    pub selected: bool,
    pub toggled: Option<ToggleState>,
    pub expanded: Option<bool>,
    pub relations: AccessibilityRelationsStyle,
    pub has_popover: Option<AccessibilityPopover>,
    pub auto_complete: Option<AccessibilityAutoComplete>,
    pub collection: AccessibilityCollectionStyle,
    pub modal: bool,
    pub required: bool,
    pub read_only: bool,
    pub invalid: bool,
    pub validation_message: Option<Arc<str>>,
    pub validation_message_truncated: bool,
    pub description: Option<Arc<str>>,
    pub orientation: Option<AccessibilityOrientation>,
    pub live: Option<AccessibilityLive>,
    pub value_range: Option<Box<AccessibilityValueRange>>,
    pub multiselectable: bool,
}

/// Retained keyboard policy for an unstyled tab list.
///
/// This is present only on [`AccessibilityRole::TabList`] elements. It is scanned directly from
/// the mounted tree on explicit keyboard input and retains no item registry or scheduler source.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct TabListBehavior {
    pub(crate) vertical: bool,
    pub(crate) activate_on_focus: bool,
    pub(crate) loop_focus: bool,
}

/// Exact opt-in relationships stored out of line.
///
/// Most retained elements declare no relationship, so they pay one nullable pointer instead of
/// four discriminated `ElementId` words. Relationship-bearing controls allocate one fixed record
/// during declaration and can still represent every `u64` identity without sentinels.
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct AccessibilityRelationsStyle(Option<Box<AccessibilityRelations>>);

#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct AccessibilityRelations {
    controls: Option<ElementId>,
    active_descendant: Option<ElementId>,
    labelled_by: Option<ElementId>,
    described_by: Option<ElementId>,
    described_by_secondary: Option<ElementId>,
}

impl AccessibilityRelationsStyle {
    fn values_mut(&mut self) -> &mut AccessibilityRelations {
        self.0
            .get_or_insert_with(|| Box::new(AccessibilityRelations::default()))
    }

    pub(crate) fn controls(&self) -> Option<ElementId> {
        self.0.as_deref().and_then(|values| values.controls)
    }

    pub(crate) fn set_controls(&mut self, value: ElementId) {
        self.values_mut().controls = Some(value);
    }

    pub(crate) fn active_descendant(&self) -> Option<ElementId> {
        self.0
            .as_deref()
            .and_then(|values| values.active_descendant)
    }

    pub(crate) fn set_active_descendant(&mut self, value: ElementId) {
        self.values_mut().active_descendant = Some(value);
    }

    pub(crate) fn labelled_by(&self) -> Option<ElementId> {
        self.0.as_deref().and_then(|values| values.labelled_by)
    }

    pub(crate) fn set_labelled_by(&mut self, value: ElementId) {
        self.values_mut().labelled_by = Some(value);
    }

    pub(crate) fn described_by(&self) -> Option<ElementId> {
        self.0.as_deref().and_then(|values| values.described_by)
    }

    pub(crate) fn set_described_by(&mut self, value: ElementId) {
        let values = self.values_mut();
        values.described_by = Some(value);
        values.described_by_secondary = None;
    }

    pub(crate) fn described_by_secondary(&self) -> Option<ElementId> {
        self.0
            .as_deref()
            .and_then(|values| values.described_by_secondary)
    }

    pub(crate) fn set_described_by_pair(&mut self, first: ElementId, second: ElementId) {
        let values = self.values_mut();
        values.described_by = Some(first);
        values.described_by_secondary = (second != first).then_some(second);
    }
}

const ACCESSIBILITY_COLLECTION_UNSET: u32 = u32::MAX;

/// Compact inline storage for collection metadata.
///
/// A separate `Option<usize>` for every property would add more than one hundred bytes to every
/// retained element, including ordinary text. The reserved sentinel keeps the complete table/tree
/// vocabulary allocation-free in 32 bytes while still covering QuickGUI's much smaller hard
/// collection bounds.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct AccessibilityCollectionStyle {
    pub row_count: u32,
    pub column_count: u32,
    pub row_index: u32,
    pub column_index: u32,
    pub level: u32,
    pub size_of_set: u32,
    pub position_in_set: u32,
    pub sort_direction: Option<AccessibilitySortDirection>,
}

impl Default for AccessibilityCollectionStyle {
    fn default() -> Self {
        Self {
            row_count: ACCESSIBILITY_COLLECTION_UNSET,
            column_count: ACCESSIBILITY_COLLECTION_UNSET,
            row_index: ACCESSIBILITY_COLLECTION_UNSET,
            column_index: ACCESSIBILITY_COLLECTION_UNSET,
            level: ACCESSIBILITY_COLLECTION_UNSET,
            size_of_set: ACCESSIBILITY_COLLECTION_UNSET,
            position_in_set: ACCESSIBILITY_COLLECTION_UNSET,
            sort_direction: None,
        }
    }
}

impl AccessibilityCollectionStyle {
    pub(crate) fn row_count(self) -> Option<usize> {
        accessibility_collection_value(self.row_count)
    }

    pub(crate) fn column_count(self) -> Option<usize> {
        accessibility_collection_value(self.column_count)
    }

    pub(crate) fn row_index(self) -> Option<usize> {
        accessibility_collection_value(self.row_index)
    }

    pub(crate) fn column_index(self) -> Option<usize> {
        accessibility_collection_value(self.column_index)
    }

    pub(crate) fn level(self) -> Option<usize> {
        accessibility_collection_value(self.level)
    }

    pub(crate) fn size_of_set(self) -> Option<usize> {
        accessibility_collection_value(self.size_of_set)
    }

    pub(crate) fn position_in_set(self) -> Option<usize> {
        accessibility_collection_value(self.position_in_set)
    }
}

fn accessibility_collection_storage(value: usize) -> u32 {
    assert!(
        value < ACCESSIBILITY_COLLECTION_UNSET as usize,
        "accessibility collection values must fit below u32::MAX"
    );
    value as u32
}

fn accessibility_collection_value(value: u32) -> Option<usize> {
    (value != ACCESSIBILITY_COLLECTION_UNSET).then_some(value as usize)
}

#[derive(Clone, Debug)]
pub(crate) struct VirtualScrollStyle {
    pub handle: VirtualScrollHandle,
    pub max_offset_y: f32,
    pub measurement_revision: u64,
    pub mount: VirtualScrollMount,
    pub report_viewport: bool,
}

pub(crate) type DropPredicateCallback = Arc<dyn Fn(&dyn Any) -> bool>;

#[derive(Clone)]
pub(crate) struct DropPredicate {
    pub type_id: TypeId,
    pub callback: DropPredicateCallback,
}

impl fmt::Debug for DropPredicate {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("DropPredicate")
            .field("type_id", &self.type_id)
            .finish_non_exhaustive()
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct TypographyStyle {
    pub color: Option<Color>,
    pub font_size: Option<f32>,
    pub line_height: Option<f32>,
    pub monospace_width: Option<f32>,
    pub family: Option<FontFamily>,
    pub features: Option<FontFeatures>,
    pub fallbacks: Option<Option<FontFallbacks>>,
    pub weight: Option<Weight>,
    pub font_style: Option<GlyphStyle>,
    pub font_thicken: Option<bool>,
    pub underline: Option<TextUnderline>,
    pub underline_color: Option<Option<Color>>,
    pub underline_wavy: Option<bool>,
    pub underline_thickness: Option<f32>,
    pub strikethrough: Option<bool>,
    pub strikethrough_color: Option<Option<Color>>,
    pub overline: Option<bool>,
    pub overline_color: Option<Option<Color>>,
    pub direction: Option<TextDirection>,
    pub letter_spacing: Option<f32>,
    pub word_spacing: Option<f32>,
    pub transform: Option<Option<TextTransform>>,
    pub word_break: Option<WordBreak>,
    pub overflow_wrap: Option<OverflowWrap>,
    pub hyphens: Option<Hyphens>,
    pub shadow: Option<Option<TextShadow>>,
    pub align: Option<TextAlign>,
    pub wrap: Option<TextWrap>,
    pub text_overflow: Option<TextOverflow>,
    pub line_clamp: Option<usize>,
    pub shaping: Option<TextShaping>,
}

impl TypographyStyle {
    pub(crate) fn resolve(&self, inherited: &TextStyle) -> TextStyle {
        let font_size = self.font_size.unwrap_or(inherited.font_size);
        TextStyle {
            font_size,
            line_height: self.line_height.unwrap_or_else(|| {
                self.font_size
                    .map(|_| font_size * 1.35)
                    .unwrap_or(inherited.line_height)
            }),
            monospace_width: self.monospace_width.or(inherited.monospace_width),
            family: self
                .family
                .clone()
                .unwrap_or_else(|| inherited.family.clone()),
            features: self
                .features
                .clone()
                .unwrap_or_else(|| inherited.features.clone()),
            fallbacks: normalize_fallbacks(
                self.fallbacks
                    .clone()
                    .unwrap_or_else(|| inherited.fallbacks.clone()),
            ),
            weight: self.weight.unwrap_or(inherited.weight),
            font_style: self.font_style.unwrap_or(inherited.font_style),
            font_thicken: self.font_thicken.unwrap_or(inherited.font_thicken),
            underline: self.underline.unwrap_or(inherited.underline),
            underline_color: self.underline_color.unwrap_or(inherited.underline_color),
            underline_wavy: self.underline_wavy.unwrap_or(inherited.underline_wavy),
            underline_thickness: self
                .underline_thickness
                .unwrap_or(inherited.underline_thickness),
            strikethrough: self.strikethrough.unwrap_or(inherited.strikethrough),
            strikethrough_color: self
                .strikethrough_color
                .unwrap_or(inherited.strikethrough_color),
            overline: self.overline.unwrap_or(inherited.overline),
            overline_color: self.overline_color.unwrap_or(inherited.overline_color),
            direction: self.direction.unwrap_or(inherited.direction),
            letter_spacing: self.letter_spacing.unwrap_or(inherited.letter_spacing),
            word_spacing: self.word_spacing.unwrap_or(inherited.word_spacing),
            transform: self.transform.unwrap_or(inherited.transform),
            word_break: self.word_break.unwrap_or(inherited.word_break),
            overflow_wrap: self.overflow_wrap.unwrap_or(inherited.overflow_wrap),
            hyphens: self.hyphens.unwrap_or(inherited.hyphens),
            shadow: self.shadow.unwrap_or(inherited.shadow),
            align: self.align.unwrap_or(inherited.align),
            wrap: self.wrap.unwrap_or(inherited.wrap),
            text_overflow: self
                .text_overflow
                .clone()
                .or_else(|| inherited.text_overflow.clone()),
            line_clamp: self.line_clamp.or(inherited.line_clamp),
            shaping: self.shaping.unwrap_or(inherited.shaping),
            color: self.color.unwrap_or(inherited.color),
        }
    }
}

/// A declarative UI node with Tailwind-like fluent styling.
///
/// See [`ElementUpdate`] for targeted updates from an embedding runtime that already owns a
/// retained declaration.
#[derive(Clone, Debug)]
pub struct Element {
    pub(crate) explicit_id: Option<ElementId>,
    pub(crate) runtime_id: ElementId,
    pub(crate) kind: ElementKind,
    pub(crate) layout: Style,
    pub(crate) visibility: Visibility,
    pub(crate) visual: VisualStyle,
    pub(crate) typography: TypographyStyle,
    pub(crate) resolved_typography: TextStyle,
    pub(crate) hover: ElementStateStyle,
    pub(crate) active: ElementStateStyle,
    pub(crate) focus: ElementStateStyle,
    pub(crate) disabled_style: ElementStateStyle,
    pub(crate) invalid_style: ElementStateStyle,
    /// Paint while this element is marked selected, like a chosen row of a native list.
    pub(crate) selected_style: ElementStateStyle,
    pub(crate) dragging: ElementStateStyle,
    pub(crate) drag_over: ElementStateStyle,
    /// Paint while this element or a descendant owns keyboard focus, like CSS `:focus-within`.
    pub(crate) focus_within: ElementStateStyle,
    /// Styles following the state of an ancestor group, in declaration order; later ones win.
    pub(crate) group_styles: Vec<GroupStateStyle>,
    /// Whether descendants' `group_hover` and `group_active` styles follow this element.
    pub(crate) group: bool,
    /// The name descendants can target this group by.
    pub(crate) group_name: Option<Arc<str>>,
    pub(crate) clickable: bool,
    pub(crate) pointer_listener: bool,
    pub(crate) scroll_wheel_listener: bool,
    pub(crate) touch_listener: bool,
    pub(crate) context_menu_listener: bool,
    // This extra indirection is intentional: listeners are uncommon, and keeping the
    // non-interactive Element representation compact matters more than one opt-in allocation.
    #[allow(clippy::box_collection)]
    pub(crate) mouse_listeners: Option<Box<Vec<MouseListenerBinding>>>,
    #[allow(clippy::box_collection)]
    pub(crate) key_listeners: Option<Box<Vec<KeyListenerBinding>>>,
    #[allow(clippy::box_collection)]
    pub(crate) action_listeners: Option<Box<Vec<ActionListenerBinding>>>,
    pub(crate) mouse_pressure_listener: bool,
    pub(crate) pinch_listener: bool,
    pub(crate) rotation_listener: bool,
    pub(crate) smart_magnify_listener: bool,
    pub(crate) drag_source: bool,
    pub(crate) drop_target: bool,
    pub(crate) drop_predicates: Vec<DropPredicate>,
    pub(crate) cursor_style: Option<CursorStyle>,
    pub(crate) cursor_style_explicit: bool,
    pub(crate) user_select: UserSelect,
    pub(crate) resolved_user_select: bool,
    pub(crate) focusable: bool,
    pub(crate) focusable_when_disabled: bool,
    pub(crate) focus_on_pointer: bool,
    pub(crate) hit_slop: Insets,
    pub(crate) focus_trap: bool,
    pub(crate) restore_previous_focus: bool,
    pub(crate) key_context: Option<KeyContext>,
    pub(crate) tab_index: i16,
    pub(crate) auto_focus: bool,
    pub(crate) form: bool,
    pub(crate) form_submitter: bool,
    pub(crate) activation_target: Option<ElementId>,
    pub(crate) accessibility: AccessibilityStyle,
    pub(crate) tab_list_behavior: Option<TabListBehavior>,
    pub(crate) plane: Option<ScenePlane>,
    pub(crate) z_index: Option<i16>,
    pub(crate) portal: bool,
    pub(crate) anchor: Option<AnchorStyle>,
    pub(crate) anchor_placement: Option<AnchorPlacementHandle>,
    pub(crate) layout_bounds: Option<LayoutBoundsHandle>,
    pub(crate) tooltip: Option<Tooltip>,
    pub(crate) app_region: Option<AppRegion>,
    pub(crate) virtual_scroll: Option<VirtualScrollStyle>,
    /// Keep wheel/trackpad scrolling active while suppressing the built-in vertical overlay thumb.
    pub(crate) vertical_scrollbar_hidden: bool,
    /// Reserve a fixed left strip outside the horizontal overlay scrollbar track.
    pub(crate) horizontal_scrollbar_left_inset: f32,
    pub(crate) scroll_to_end_revision: Option<u64>,
    pub(crate) layout_rounding: bool,
    pub(crate) list_item_measurement: Option<ListItemMeasurement>,
    pub(crate) animation: Option<ElementAnimation>,
    pub(crate) spring: Option<ElementSpring>,
    /// The declaration callback was consumed, but still owns this resolved subtree's values.
    pub(crate) resolved_motion: bool,
    pub(crate) transition: Option<Transition>,
    pub(crate) blocks_pointer: bool,
    pub(crate) dismiss_policy: DismissPolicy,
    pub(crate) restore_focus: Option<FocusHandle>,
    pub(crate) children: Vec<Element>,
    pub(crate) taffy_node: Option<taffy::NodeId>,
    /// Declared inline layout direction; inherited when absent.
    pub(crate) direction: Option<Direction>,
    /// Direction resolved against the ancestor chain during layout build.
    pub(crate) resolved_direction: Direction,
    /// Direction-relative padding and border overrides resolved during layout build.
    pub(crate) logical_insets: Option<Box<LogicalInsets>>,
    /// CSS-style sticky offsets relative to the nearest scroll container.
    pub(crate) sticky: Option<StickyInsets>,
    /// Scroll-snap strictness declared by this scroll container.
    pub(crate) scroll_snap: Option<ScrollSnapStyle>,
    /// Snap alignment declared by a scroll-snap child.
    pub(crate) snap_align: Option<SnapAlign>,
    /// Whether a scroll gesture may never skip past this snap child.
    pub(crate) snap_stop_always: bool,
}

/// A change to a mounted element, addressed by stable identity.
///
/// Embedding runtimes can submit a batch with [`crate::AppRunner::update_elements`] after
/// updating their source declaration. Text changes invalidate intrinsic layout along the
/// affected ancestor path. Colors and opacity require paint only. Unsupported or unmounted
/// targets reject the entire batch, allowing the caller to request an ordinary view rebuild.
#[derive(Clone, Debug)]
pub enum ElementUpdate {
    Text {
        id: ElementId,
        content: Arc<str>,
    },
    BackgroundColor {
        id: ElementId,
        color: Color,
    },
    TextColor {
        id: ElementId,
        color: Color,
    },
    Opacity {
        id: ElementId,
        opacity: f32,
    },
    /// Change placement without measuring or laying out the subtree again.
    Transform {
        id: ElementId,
        transform: Transform2D,
    },
    /// Replace a declaration at an existing identity, retaining keyed layout and input state.
    /// The replacement root must have the same explicit ID. Register replacement listeners
    /// through a component scope; this operation itself does not register callbacks.
    Replace {
        id: ElementId,
        element: Box<Element>,
    },
}

impl ElementUpdate {
    pub(crate) fn apply_to_declaration(&self, element: &mut Element) -> bool {
        if element.explicit_id != Some(self.id()) {
            return false;
        }
        match self {
            Self::Text { content, .. } => {
                let ElementKind::Text(value) = &mut element.kind else {
                    return false;
                };
                *value = content.clone();
            }
            Self::BackgroundColor { color, .. } => element.visual.background = Some(*color),
            Self::TextColor { color, .. } => element.typography.color = Some(*color),
            Self::Opacity { opacity, .. } => {
                element.visual.opacity = if opacity.is_finite() {
                    opacity.clamp(0.0, 1.0)
                } else {
                    1.0
                }
            }
            Self::Transform { transform, .. } => element.visual.transform = *transform,
            Self::Replace { .. } => return false,
        }
        true
    }

    pub fn id(&self) -> ElementId {
        match self {
            Self::Text { id, .. }
            | Self::BackgroundColor { id, .. }
            | Self::TextColor { id, .. }
            | Self::Opacity { id, .. }
            | Self::Transform { id, .. }
            | Self::Replace { id, .. } => *id,
        }
    }
}

impl Element {
    /// Whether keyboard focus may land on this element.
    ///
    /// A disabled control leaves the Tab sequence, which is the web default. Toolbars are the
    /// documented exception: an item that opts into [`Element::focusable_when_disabled`] stays
    /// reachable so a keyboard user can discover why it is unavailable.
    pub(crate) const fn is_keyboard_focusable(&self) -> bool {
        self.focusable && (!self.accessibility.disabled || self.focusable_when_disabled)
    }
}

/// Create a container element.
pub fn div() -> Element {
    Element::container()
}

/// Create a CSS-like container query whose contents are declared from its assigned size.
///
/// The query fills its parent by default. Its callback is evaluated after the query's own layout,
/// and the returned subtree is laid out independently inside that fixed box, so contents cannot
/// affect the query's intrinsic size.
///
/// An unchanged assigned size retains the existing subtree. A size change evaluates the callback
/// again, while stable declarative-animation IDs keep their playback state. Query contents must be
/// returned from this callback rather than attached with [`Element::child`] or
/// [`Element::children`].
///
/// # Example
///
/// ```
/// use quickgui::{container_query, div, text};
///
/// let responsive = container_query(|size| {
///     if size.width < 480.0 {
///         div().flex_col().child(text("Compact"))
///     } else {
///         div().grid().grid_cols(3).child(text("Wide"))
///     }
/// });
/// ```
pub fn container_query<E>(render: impl Fn(crate::Size) -> E + 'static) -> Element
where
    E: IntoElement,
{
    let mut element = Element::container();
    element.kind = ElementKind::ContainerQuery(ContainerQueryElement {
        render: Rc::new(move |size| render(size).into_element()),
        resolved_size: None,
        resolved_motion_ids: Vec::new(),
        layout_pending: false,
    });
    element.layout.size = TaffySize {
        width: Dimension::percent(1.0),
        height: Dimension::percent(1.0),
    };
    element
}

/// Create a semantic form container.
///
/// Attach callbacks with [`Element::on_form_submit`] and [`Element::on_form_invalid`]. Return in a
/// descendant single-line input and [`submit_button`] both validate the nearest form.
pub fn form() -> Element {
    let mut element = div().accessibility_role(AccessibilityRole::Form);
    element.form = true;
    element
}

/// Create a viewport-level element painted on the overlay plane.
///
/// Overlay elements are removed from normal flow, escape ancestor clipping, and block pointer
/// events inside their bounds. Use [`Element::anchor_to`] for popovers and menus.
pub fn overlay() -> Element {
    div().overlay()
}

/// Create a semantic, keyboard-focusable button container.
pub fn button() -> Element {
    let mut element = div()
        .accessibility_role(AccessibilityRole::Button)
        .focusable();
    element.set_implicit_cursor(CursorStyle::PointingHand);
    element
}

/// Create a button that validates and submits its nearest ancestor [`form`].
pub fn submit_button() -> Element {
    button().form_submitter()
}

/// Create a text element. Strings are owned through `Arc<str>` and cheap to retain.
pub fn text(content: impl Into<Arc<str>>) -> Element {
    Element::text(content.into())
}

/// Create an image element with intrinsic sizing and `object-fit: contain` behavior.
pub fn img(source: impl Into<ImageSource>) -> Element {
    Element::image(source.into())
}

/// Create a monochrome SVG element with intrinsic sizing and inherited text color.
pub fn svg(source: impl Into<Svg>) -> Element {
    Element::svg(source.into())
}

/// Create a retained vector path element with intrinsic sizing and inherited text color.
pub fn path(source: impl Into<Path>) -> Element {
    Element::path(source.into())
}

/// Create a web-like custom paint surface. The callback runs only when the view repaints.
///
/// Callback coordinates are local to the element and are clipped to its layout box.
pub fn canvas(painter: impl for<'a> Fn(Rect, &mut Canvas<'a>) + 'static) -> Element {
    Element::canvas(painter)
}

/// Create a retained rectangle painted by validated application WGSL.
///
/// Like the web canvas default, its initial size is 300 by 150 logical pixels. The shader remains
/// still until application state invalidates the view or explicitly requests another frame.
pub fn custom_shader(shader: impl Into<CustomShader>) -> Element {
    Element::custom_shader(shader.into())
}

/// Create a controlled, single-line text input.
///
/// Attach a stable [`crate::InputListener`] with [`Element::on_input`].
pub fn text_input(value: impl Into<Arc<str>>) -> Element {
    Element::text_input(value.into(), Arc::from([]), false)
}

/// Create a controlled, multiline text area with web-style soft wrapping.
///
/// Attach a stable [`crate::InputListener`] with [`Element::on_input`]. Use [`Element::no_wrap`]
/// for code-editor-style horizontal scrolling.
pub fn text_area(value: impl Into<Arc<str>>) -> Element {
    Element::text_input(value.into(), Arc::from([]), true)
}

/// Create a controlled, single-line input with bounded byte-range text styles.
///
/// Attach a stable [`crate::InputListener`] with [`Element::on_input`] and rebuild the
/// [`StyledText`] from the controlled value when it changes. Editing, selection, IME, hit testing,
/// and painting all reuse the same retained shaped buffer.
pub fn styled_text_input(value: StyledText) -> Element {
    let (value, highlights) = value.into_parts();
    Element::text_input(value, highlights, false)
}

/// Create a controlled, multiline text area with bounded byte-range text styles.
///
/// This is the attributed-text counterpart to [`text_area`]. It preserves wrapping and both-axis
/// scrolling while using the supplied styles for shaping, caret geometry, selection, and paint.
pub fn styled_text_area(value: StyledText) -> Element {
    let (value, highlights) = value.into_parts();
    Element::text_input(value, highlights, true)
}

/// Embed an AppKit view as a declarative leaf on macOS.
///
/// QuickGUI synchronizes layout, clipping, visibility, and sibling order while AppKit keeps
/// ownership of the view's rendering and input behavior.
#[cfg(target_os = "macos")]
pub fn native_view(view: &NSView) -> Element {
    Element::native_view(MacNativeView::new(view))
}

/// Embed an AppKit view whose frame extends `outset` points past its layout box on every side.
///
/// Layout, hit regions, and siblings see the element's own box; only the native frame and its clip
/// grow, so a control can draw an effect that spills past its bounds, such as Liquid Glass, without
/// that headroom pushing its neighbours apart. See [`MacNativeView::with_outset`].
#[cfg(target_os = "macos")]
pub fn native_view_with_outset(view: &NSView, outset: f32) -> Element {
    Element::native_view(MacNativeView::new(view).with_outset(outset))
}

mod construction_layout;
pub use construction_layout::{Direction, SnapAlign, SnapStrictness, StickyInsets};
pub(crate) use construction_layout::{LogicalInsets, ScrollSnapStyle};
mod interaction;
mod state;
mod style;

fn stable_hash(bytes: &[u8]) -> u64 {
    let mut hash = 0xcbf2_9ce4_8422_2325_u64;
    for byte in bytes {
        hash ^= u64::from(*byte);
        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
    }
    hash
}

fn bounded_validation_message(message: Arc<str>) -> (Option<Arc<str>>, bool) {
    if message.is_empty() {
        return (None, false);
    }
    if message.len() <= MAX_VALIDATION_MESSAGE_BYTES {
        return (Some(message), false);
    }
    let mut end = MAX_VALIDATION_MESSAGE_BYTES;
    while !message.is_char_boundary(end) {
        end -= 1;
    }
    (Some(Arc::from(&message[..end])), true)
}

#[cfg(test)]
mod tests;