compose-rust 0.0.0

Declarative UI in Rust, rendered by an AOT-compiled Compose Multiplatform engine.
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
#![allow(non_snake_case)]

use dioxus_core::{Callback, Element, EventHandler};
use dioxus_core_macro::{Props, component, rsx};
use dioxus_hooks::use_signal;
use dioxus_signals::WritableExt as _;

use crate as dioxus_elements;
use crate::Key;
use crate::drawing::DrawList;
use crate::schema::{
    Alignment, Arrangement, ButtonVariant, ColorRole, IconRole, MaterialRole, MotionRole, Paint,
    ShapeRole, SlotRole, SpaceRole, TextAlign, TextOverflow, TypeRole,
};
use std::cell::Cell;
use std::rc::Rc;

/// A key-down event whose consumption state is shared with the Host boundary.
#[derive(Clone, Debug)]
pub struct KeyEvent {
    key: Key,
    shift_key: bool,
    ctrl_key: bool,
    alt_key: bool,
    meta_key: bool,
    consumed: Rc<Cell<bool>>,
}

impl KeyEvent {
    pub(crate) fn new(
        key: Key,
        shift_key: bool,
        ctrl_key: bool,
        alt_key: bool,
        meta_key: bool,
    ) -> Self {
        Self {
            key,
            shift_key,
            ctrl_key,
            alt_key,
            meta_key,
            consumed: Rc::new(Cell::new(false)),
        }
    }

    pub fn key(&self) -> Key {
        self.key
    }

    pub fn shift_key(&self) -> bool {
        self.shift_key
    }

    pub fn ctrl_key(&self) -> bool {
        self.ctrl_key
    }

    pub fn alt_key(&self) -> bool {
        self.alt_key
    }

    pub fn meta_key(&self) -> bool {
        self.meta_key
    }

    pub fn consume(&self) {
        self.consumed.set(true);
    }

    pub fn consumed(&self) -> bool {
        self.consumed.get()
    }
}

/// A role that was not set is tag 0, which means "not sent". The Renderer
/// never sees a zero role, so it never has to guess what an unset role meant.
fn role(value: Option<impl Into<u16>>) -> i64 {
    value.map_or(0, |value| i64::from(value.into()))
}

/// A Modifier value that was not set sends nothing at all, rather than a zero. No padding
/// and a padding of zero look the same on screen, but only one of them should cost a
/// mutation on every frame the widget appears in.
fn opt_dp(value: Option<f32>) -> Option<f64> {
    value.map(f64::from)
}

fn opt_role(value: Option<impl Into<u16>>) -> Option<i64> {
    value.map(|value| i64::from(value.into()))
}

fn opt_paint(value: Option<Paint>) -> Option<i64> {
    value.map(|paint| paint.to_bits() as i64)
}

fn dp(value: Option<f32>) -> f64 {
    f64::from(value.unwrap_or(0.0))
}

#[component]
pub fn Column(
    /// Asks the Renderer to report this node's measured size, under the name a
    /// screen gave it with `use_node_size`. Nothing is measured without it.
    #[props(default)]
    observe_size: Option<i64>,
    /// How important this node's changes are. The curve and the length are the design
    /// system's answer to that, and are never sent from here.
    #[props(default)]
    motion: Option<MotionRole>,
    /// What this node's surface is made of. Blur on the systems that blur, a lifted tone
    /// on the ones that lift, a flat fill on the rest.
    #[props(default)]
    material: Option<MaterialRole>,
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] arrangement: Option<Arrangement>,
    #[props(default)] spacing: Option<f32>,
    #[props(default)] space_role: Option<SpaceRole>,
    #[props(default)] alignment: Option<Alignment>,
    children: Element,
) -> Element {
    rsx! {
        column {
            observe_size,
            motion: opt_role(motion),
            material: opt_role(material),
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            arrangement: role(arrangement),
            spacing: dp(spacing),
            space_role: role(space_role),
            alignment: role(alignment),
            {children}
        }
    }
}

#[component]
pub fn Row(
    /// Asks the Renderer to report this node's measured size, under the name a
    /// screen gave it with `use_node_size`. Nothing is measured without it.
    #[props(default)]
    observe_size: Option<i64>,
    /// How important this node's changes are. The curve and the length are the design
    /// system's answer to that, and are never sent from here.
    #[props(default)]
    motion: Option<MotionRole>,
    /// What this node's surface is made of. Blur on the systems that blur, a lifted tone
    /// on the ones that lift, a flat fill on the rest.
    #[props(default)]
    material: Option<MaterialRole>,
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] arrangement: Option<Arrangement>,
    #[props(default)] spacing: Option<f32>,
    #[props(default)] space_role: Option<SpaceRole>,
    #[props(default)] alignment: Option<Alignment>,
    children: Element,
) -> Element {
    rsx! {
        row {
            observe_size,
            motion: opt_role(motion),
            material: opt_role(material),
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            arrangement: role(arrangement),
            spacing: dp(spacing),
            space_role: role(space_role),
            alignment: role(alignment),
            {children}
        }
    }
}

#[component]
pub fn ComposeBox(
    /// Asks the Renderer to report this node's measured size, under the name a
    /// screen gave it with `use_node_size`. Nothing is measured without it.
    #[props(default)]
    observe_size: Option<i64>,
    /// How important this node's changes are. The curve and the length are the design
    /// system's answer to that, and are never sent from here.
    #[props(default)]
    motion: Option<MotionRole>,
    /// What this node's surface is made of. Blur on the systems that blur, a lifted tone
    /// on the ones that lift, a flat fill on the rest.
    #[props(default)]
    material: Option<MaterialRole>,
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] alignment: Option<Alignment>,
    children: Element,
) -> Element {
    rsx! {
        composebox {
            observe_size,
            motion: opt_role(motion),
            material: opt_role(material),
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            alignment: role(alignment),
            {children}
        }
    }
}

/// The whole content with a vertical scroll attached. The scroll position is the
/// Renderer's, like focus and animation state, so scrolling never reaches the Host.
#[component]
pub fn ScrollColumn(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    children: Element,
) -> Element {
    rsx! {
        scrollcolumn {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            {children}
        }
    }
}

/// `type_role` alone takes the design system's size, weight, line height and letter
/// spacing. Each override replaces one axis and costs one `SetProp`, so changing the font
/// size does not resend the rest of the text's styling.
#[component]
pub fn Text(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    /// Runs of different treatment inside [text]: a bold phrase, a link, a piece of
    /// code. Left out, the widget travels exactly as it did before runs existed.
    #[props(default)]
    spans: crate::spans::TextSpans,
    #[props(into)] text: String,
    #[props(default)] type_role: Option<TypeRole>,
    #[props(default)] font_size: Option<f32>,
    #[props(default)] font_weight: Option<u16>,
    #[props(default)] line_height: Option<f32>,
    #[props(default)] letter_spacing: Option<f32>,
    #[props(default)] color: Option<Paint>,
    #[props(default)] text_align: Option<TextAlign>,
    #[props(default)] max_lines: Option<u32>,
    #[props(default)] overflow: Option<TextOverflow>,
) -> Element {
    rsx! {
        text {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            // Left out entirely when there are none, so a Text that says nothing about
            // runs travels exactly as it did before runs existed.
            spans: (!spans.is_empty()).then_some(spans),
            text,
            type_role: role(type_role),
            font_size: dp(font_size),
            font_weight: i64::from(font_weight.unwrap_or(0)),
            line_height: dp(line_height),
            letter_spacing: dp(letter_spacing),
            color: color.map_or(0, |paint| paint.to_bits() as i64),
            text_align: role(text_align),
            max_lines: i64::from(max_lines.unwrap_or(0)),
            overflow: role(overflow),
        }
    }
}

#[component]
pub fn TextField(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(into, default)] placeholder: String,
    #[props(default = true)] enabled: bool,
    #[props(default)] multiline: bool,
    /// The rung of the type ladder the field's own text is set in. A field holding a file
    /// path or a snippet of code wants the monospace rung; prose does not.
    #[props(default)]
    type_role: Option<TypeRole>,
    #[props(default)] on_value_change: EventHandler<String>,
    #[props(default)] on_submit: EventHandler<String>,
    #[props(default)] on_focus_lost: EventHandler<()>,
    #[props(default)] on_key_down: EventHandler<KeyEvent>,
) -> Element {
    rsx! {
        textfield {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            placeholder,
            enabled,
            multiline,
            type_role: role(type_role),
            onvaluechange: move |event| on_value_change.call((*event.data()).clone()),
            onsubmit: move |event| on_submit.call((*event.data()).clone()),
            onfocuslost: move |_| on_focus_lost.call(()),
            onkeydown: move |event| on_key_down.call((*event.data()).clone()),
        }
    }
}

/// The variant is the seam the design system's component rule attaches to.
/// The same rsx draws differently per system, and that is correct behaviour.
#[component]
pub fn Button(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(into)] text: String,
    /// The meaning of the glyph on it, never a picture: `IconRole::Search` comes out as
    /// this design system's search icon.
    ///
    /// With a `text` beside it the button shows both. With `text` empty it is the glyph
    /// alone, which is what every one of the reference toolbars is made of, and the role
    /// is what names it for assistive technology.
    #[props(default)]
    icon: Option<IconRole>,
    #[props(default = true)] enabled: bool,
    #[props(default)] variant: Option<ButtonVariant>,
    /// The label's colour, for the rare button whose meaning is not the variant's.
    /// A destructive action is the case that needs it: it is a plain button in every
    /// design system, and what marks it is that its label is the error colour.
    #[props(default)]
    color: Option<Paint>,
    #[props(default)] on_click: EventHandler<()>,
) -> Element {
    rsx! {
        button {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            text,
            icon: opt_role(icon),
            enabled,
            variant: role(variant),
            color: opt_paint(color),
            onclick: move |_| on_click.call(()),
        }
    }
}

/// A hairline between two rows of a grouped list.
///
/// One [`Divider`] under the name application code already uses. Nothing about the weight,
/// the colour or the inset is decided here: a Material rule, a Fluent layer stroke and an
/// Apple grouped-list separator are three different lines, and which one gets drawn is the
/// active design system's answer rather than a constant this library holds.
///
/// `color` overrides that answer for the rare case where a list rules itself in something
/// other than the quiet edge. Leaving it unset is the usual thing to do.
#[component]
pub fn Separator(#[props(default)] color: Option<Paint>) -> Element {
    rsx! {
        Divider { background: color }
    }
}

#[component]
pub fn Spacer(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
) -> Element {
    rsx! {
        spacer {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
        }
    }
}

/// The paths of the files a reader let go over a node.
///
/// Separated on the wire by a NUL, which is the one byte no path on any of the three
/// desktops may contain. A newline would have been shorter to read and wrong: a file
/// called "notes\nfor tuesday" is legal on two of them, and splitting on newlines would
/// have turned one file into two.
#[derive(Clone, Debug, PartialEq)]
pub struct FileDrop {
    paths: Vec<String>,
}

impl FileDrop {
    /// Takes apart the one string the paths travelled in.
    ///
    /// Public because the separation is part of what this type promises, and a test that
    /// could not build one could only check it through a window.
    pub fn new(joined: &str) -> Self {
        Self {
            paths: joined
                .split('\0')
                .filter(|path| !path.is_empty())
                .map(str::to_owned)
                .collect(),
        }
    }

    /// Every path that arrived, in the order the platform gave them.
    pub fn paths(&self) -> &[String] {
        &self.paths
    }
}

/// The visible item range the Renderer asks the Host to materialise.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RangeRequest {
    start: u32,
    count: u32,
}

impl RangeRequest {
    pub(crate) fn new(start: u32, count: u32) -> Self {
        Self { start, count }
    }

    pub fn start(&self) -> usize {
        self.start as usize
    }

    pub fn count(&self) -> usize {
        self.count as usize
    }
}

/// A windowed list. The Host declares `item_count` and a stable key per item, and
/// materialises **exactly** the range the Renderer last requested.
///
/// The read-ahead buffer belongs to the Renderer, which owns the scroll position and so
/// knows how far ahead to ask. Widening the range here would break the Renderer's placement:
/// `start` is the global index of the first child it receives, and that is what lets it draw
/// a real Compose `LazyColumn` of `item_count` items. The data stays in the Host, so
/// scrolling back re-materialises an identical subtree.
#[component]
pub fn LazyColumn(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    item_count: usize,
    #[props(default)] key_of: Option<Callback<usize, String>>,
    item: Callback<usize, Element>,
) -> Element {
    let mut range = use_signal(|| (0_usize, 0_usize));
    let (start, count) = range();
    let first = start.min(item_count);
    let last = first.saturating_add(count).min(item_count);
    rsx! {
        lazycolumn {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            item_count: item_count as i64,
            onrangerequest: move |event: dioxus_core::Event<RangeRequest>| {
                let requested = event.data();
                range.set((requested.start(), requested.count()));
            },
            for index in first..last {
                {
                    let item_key = key_of
                        .map_or_else(|| index.to_string(), |key_of| key_of.call(index));
                    rsx! {
                        composebox { key: "{item_key}", item_key, {item.call(index)} }
                    }
                }
            }
        }
    }
}

/// A grouped container. What a card looks like, its background, corner and resting
/// elevation, is the design system's decision, so the widget carries no appearance of its
/// own. `Modifier::Elevation` overrides the resting height when the Host has a reason to.
#[component]
pub fn Card(
    /// Asks the Renderer to report this node's measured size, under the name a
    /// screen gave it with `use_node_size`. Nothing is measured without it.
    #[props(default)]
    observe_size: Option<i64>,
    /// How important this node's changes are. The curve and the length are the design
    /// system's answer to that, and are never sent from here.
    #[props(default)]
    motion: Option<MotionRole>,
    /// What this node's surface is made of. Blur on the systems that blur, a lifted tone
    /// on the ones that lift, a flat fill on the rest.
    #[props(default)]
    material: Option<MaterialRole>,
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    children: Element,
) -> Element {
    rsx! {
        card {
            observe_size,
            motion: opt_role(motion),
            material: opt_role(material),
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            {children}
        }
    }
}

/// A plain background-and-elevation container. Use it where a `Card`'s grouping meaning
/// would be wrong and only the surface is wanted.
#[component]
pub fn Surface(
    /// Asks the Renderer to report this node's measured size, under the name a
    /// screen gave it with `use_node_size`. Nothing is measured without it.
    #[props(default)]
    observe_size: Option<i64>,
    /// How important this node's changes are. The curve and the length are the design
    /// system's answer to that, and are never sent from here.
    #[props(default)]
    motion: Option<MotionRole>,
    /// What this node's surface is made of. Blur on the systems that blur, a lifted tone
    /// on the ones that lift, a flat fill on the rest.
    #[props(default)]
    material: Option<MaterialRole>,
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    children: Element,
) -> Element {
    rsx! {
        surface {
            observe_size,
            motion: opt_role(motion),
            material: opt_role(material),
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            {children}
        }
    }
}

/// A modal. `open` seeds the Renderer's own open state and carries changes that came from
/// somewhere other than the Renderer; the Renderer runs the enter and exit itself so the
/// animation never round trips through the Host. `on_dismiss` fires when the user asks to
/// close it, and the Host decides whether to honour that by setting `open` to false.
#[component]
pub fn Dialog(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] open: bool,
    #[props(default)] on_dismiss: EventHandler<()>,
    children: Element,
) -> Element {
    rsx! {
        dialog {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            open,
            ondismiss: move |_| on_dismiss.call(()),
            {children}
        }
    }
}

/// A popup anchored to `anchor`, which is the widget the menu hangs off. The anchor is the
/// first child on the wire and the entries follow it, so the Renderer can place the popup
/// without the Host knowing any screen coordinates.
///
/// Each entry supplies its own `on_click`, which is what tells the Host which one was
/// chosen. The Renderer closes the popup itself.
#[component]
pub fn Menu(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] expanded: bool,
    #[props(default)] on_dismiss: EventHandler<()>,
    anchor: Element,
    children: Element,
) -> Element {
    rsx! {
        menu {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            open: expanded,
            ondismiss: move |_| on_dismiss.call(()),
            {anchor}
            {children}
        }
    }
}

/// A row of tabs. Each child is one tab.
///
/// `selected_index` seeds the Renderer's selection and moves it when the Host changes it.
/// Tapping a tab changes the selection in the Renderer and reports it by firing that tab's
/// own `on_click`, so switching tabs costs one event and no re-render of the tab strip.
#[component]
pub fn Tabs(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    /// The colour of the mark that shows which tab is selected.
    ///
    /// The design system decides where this is unset, which is what an adaptive
    /// application wants. A unified one names it, because its reference may have no
    /// accent at all and a system asked the question answers with its own.
    #[props(default)]
    color: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] selected_index: usize,
    children: Element,
) -> Element {
    rsx! {
        tabs {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            color: opt_paint(color),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            selected_index: selected_index as i64,
            {children}
        }
    }
}

/// The bar across the top of a screen. Its children are its content, left to right. How the
/// bar is sized, spaced and separated from what is below it is the design system's rule.
#[component]
pub fn TopAppBar(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    /// The window's title, where this bar is the window's own caption.
    ///
    /// A property rather than a child, because the Renderer has to be able to find it.
    /// Three design systems centre the window title and put everything else at the
    /// leading edge, and a bar whose children are an arbitrary tree gives the Renderer no
    /// way to tell which of them is the title. Where the bar is not the caption, or the
    /// design system puts its title at the start, this draws in the same place a first
    /// child would.
    #[props(default)]
    title: String,
    children: Element,
) -> Element {
    rsx! {
        topappbar {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            text: title,
            {children}
        }
    }
}

/// The horizontal axis of the same windowing protocol `LazyColumn` uses, with the same
/// contract: the Host materialises exactly the range the Renderer last asked for, and the
/// read-ahead buffer belongs to the Renderer because the scroll position does.
#[component]
pub fn LazyRow(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    item_count: usize,
    #[props(default)] key_of: Option<Callback<usize, String>>,
    item: Callback<usize, Element>,
) -> Element {
    let mut range = use_signal(|| (0_usize, 0_usize));
    let (start, count) = range();
    let first = start.min(item_count);
    let last = first.saturating_add(count).min(item_count);
    rsx! {
        lazyrow {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            item_count: item_count as i64,
            onrangerequest: move |event: dioxus_core::Event<RangeRequest>| {
                let requested = event.data();
                range.set((requested.start(), requested.count()));
            },
            for index in first..last {
                {
                    let item_key = key_of
                        .map_or_else(|| index.to_string(), |key_of| key_of.call(index));
                    rsx! {
                        composebox { key: "{item_key}", item_key, {item.call(index)} }
                    }
                }
            }
        }
    }
}

/// An explanation attached to its child. It is a description as much as a hover popup: the
/// Renderer also exposes the text to the accessibility tree, so a pointer is not the only
/// way to reach it.
#[component]
pub fn Tooltip(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(into)] text: String,
    children: Element,
) -> Element {
    rsx! {
        tooltip {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            text,
            {children}
        }
    }
}

/// Pixels the widget vocabulary cannot produce: charts, sparklines, signature pads, rings.
///
/// The list is a value, not a callback, because drawing code cannot cross the boundary into
/// an ahead-of-time compiled Renderer. Two equal lists are one attribute comparison, so a
/// frame that redraws the same chart sends nothing. Size comes from the Modifier chain, and
/// coordinates are dp from the widget's top-left corner.
///
/// Colour goes through `Paint`, so `Paint::Role(ColorRole::Primary)` draws in whatever the
/// active design system calls primary.
#[component]
pub fn Canvas(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    commands: DrawList,
) -> Element {
    rsx! {
        canvas {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            commands,
        }
    }
}

/// A registered asset drawn as a picture. The bytes reached the Renderer once, through
/// `Host::register_asset`, and what crosses per frame is the id.
#[component]
pub fn Image(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    asset_id: u32,
) -> Element {
    rsx! {
        image {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            asset: i64::from(asset_id),
        }
    }
}

/// A registered icon, tinted by a role. `Host::register_icon` registers the meaning rather
/// than a picture, so the same declaration comes out as the icon each design system draws.
#[component]
pub fn Icon(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    asset_id: u32,
    #[props(default)] color: Option<Paint>,
) -> Element {
    rsx! {
        icon {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            asset: i64::from(asset_id),
            color: color.map_or(0, |paint| paint.to_bits() as i64),
        }
    }
}

/// A date, as whole days since 1970-01-01.
///
/// The widget says what the value is and what range is allowed. How it is picked, a
/// calendar grid, a wheel or a flyout, belongs to the design system, and there is no
/// property that lets the Host ask for one of them.
///
/// Time zone, locale, the first day of the week and the display format stay in the
/// Renderer, which is the only side that can read the platform's settings. A format string
/// crossing here would turn "follows the platform" into a claim the Renderer cannot keep.
#[component]
pub fn DatePicker(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    /// Days since 1970-01-01.
    value: i64,
    #[props(default)] min: Option<i64>,
    #[props(default)] max: Option<i64>,
    #[props(default = true)] enabled: bool,
    #[props(default)] on_change: EventHandler<i64>,
) -> Element {
    rsx! {
        datepicker {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            value,
            min: min.unwrap_or(i64::MIN),
            max: max.unwrap_or(i64::MAX),
            enabled,
            onchange: move |event: dioxus_core::Event<f64>| on_change.call(*event.data() as i64),
        }
    }
}

/// A time of day, as minutes since midnight.
///
/// Whether it is picked on a dial, a wheel or a list is the design system's decision, and
/// whether it reads as 12 or 24 hour is the platform's.
#[component]
pub fn TimePicker(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    /// Minutes since midnight.
    value: u32,
    #[props(default)] min: Option<u32>,
    #[props(default)] max: Option<u32>,
    #[props(default = true)] enabled: bool,
    #[props(default)] on_change: EventHandler<u32>,
) -> Element {
    rsx! {
        timepicker {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            value: i64::from(value),
            min: i64::from(min.unwrap_or(0)),
            max: i64::from(max.unwrap_or(MINUTES_IN_A_DAY - 1)),
            enabled,
            onchange: move |event: dioxus_core::Event<f64>| {
                on_change.call((*event.data() as i64).clamp(0, i64::from(MINUTES_IN_A_DAY - 1)) as u32)
            },
        }
    }
}

const MINUTES_IN_A_DAY: u32 = 24 * 60;

/// One choice out of a list. Each child is one option, and the Renderer reports the
/// position the user landed on.
#[component]
pub fn Dropdown(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] selected_index: usize,
    #[props(default = true)] enabled: bool,
    #[props(default)] on_change: EventHandler<usize>,
    children: Element,
) -> Element {
    rsx! {
        dropdown {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            selected_index: selected_index as i64,
            enabled,
            onchange: move |event: dioxus_core::Event<f64>| {
                on_change.call((*event.data() as i64).max(0) as usize)
            },
            {children}
        }
    }
}

/// A two-state box.
///
/// The widget is controlled: it draws exactly what `checked` says and reports what the
/// user asked for, so the value the Host holds and the box on screen cannot disagree.
/// What the mark looks like, and whether pressing it ripples or dims, is the design
/// system's rule.
#[component]
pub fn Checkbox(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] checked: bool,
    #[props(default = true)] enabled: bool,
    #[props(default)] on_change: EventHandler<bool>,
) -> Element {
    rsx! {
        checkbox {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            checked,
            enabled,
            onchange: move |event: dioxus_core::Event<f64>| on_change.call(is_on(*event.data())),
        }
    }
}

/// One choice out of several. Which siblings it excludes is the Host's business, so the
/// widget carries only whether this one is the chosen one.
///
/// `selected` is the Compose name for that state and is what the rsx attribute is called.
/// On the wire it is the same "is this on" boolean the other two toggles send, because a
/// selected radio button and a switch that is on are one fact.
#[component]
pub fn RadioButton(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] selected: bool,
    #[props(default = true)] enabled: bool,
    #[props(default)] on_change: EventHandler<bool>,
) -> Element {
    rsx! {
        radiobutton {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            checked: selected,
            enabled,
            onchange: move |event: dioxus_core::Event<f64>| on_change.call(is_on(*event.data())),
        }
    }
}

/// An on/off control. The same contract as `Checkbox`: the Host owns the value, and the
/// track, the thumb and the way the change is animated belong to the design system.
#[component]
pub fn Switch(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] checked: bool,
    #[props(default = true)] enabled: bool,
    #[props(default)] on_change: EventHandler<bool>,
) -> Element {
    rsx! {
        switch {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            checked,
            enabled,
            onchange: move |event: dioxus_core::Event<f64>| on_change.call(is_on(*event.data())),
        }
    }
}

/// A value picked from a range.
///
/// The position a drag is passing through stays in the Renderer, so following a finger
/// costs no boundary call, and `value` seeds that position and moves it when the change
/// came from elsewhere. `steps` is the number of stops between the two ends; zero leaves
/// the slider continuous.
#[component]
pub fn Slider(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    /// The colour of the part of the track that has been filled in, and of the thumb.
    ///
    /// The design system decides where this is unset, which is what an adaptive
    /// application wants. A unified one names it, because its reference may have no
    /// accent at all and a system asked the question answers with its own.
    #[props(default)]
    color: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] value: f32,
    #[props(default = 0.0)] min: f32,
    #[props(default = 1.0)] max: f32,
    #[props(default)] steps: u32,
    #[props(default = true)] enabled: bool,
    #[props(default)] on_change: EventHandler<f32>,
) -> Element {
    rsx! {
        slider {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            color: opt_paint(color),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            value: f64::from(value),
            min: f64::from(min),
            max: f64::from(max),
            steps: i64::from(steps),
            enabled,
            onchange: move |event: dioxus_core::Event<f64>| on_change.call(*event.data() as f32),
        }
    }
}

/// Work in progress. `determinate` says whether `value` means anything, and `circular`
/// picks between the two forms every supported design system has.
///
/// There is no speed, no easing and no track colour here: how an indeterminate indicator
/// travels is motion, and motion is the design system's rule.
#[component]
pub fn ProgressIndicator(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] value: f32,
    #[props(default = true)] determinate: bool,
    #[props(default)] circular: bool,
) -> Element {
    rsx! {
        progressindicator {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            value: f64::from(value),
            determinate,
            circular,
        }
    }
}

/// A line between two things. The axis is the only decision the Host makes: the
/// thickness, the colour and the inset all come from the design system.
#[component]
pub fn Divider(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] vertical: bool,
) -> Element {
    rsx! {
        divider {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            vertical,
        }
    }
}

/// A toggle's new state, as it arrives on the wire: off is 0.0 and on is 1.0.
///
/// The comparison is against zero rather than against 1.0 so a Renderer that reports a
/// half-finished transition still reads as on.
fn is_on(value: f64) -> bool {
    value != 0.0
}

/// A set of destinations with one of them selected, plus the screen they lead to.
///
/// The children that are `NavigationItem` are the destinations; every other child is the
/// content of the selected screen. Splitting by kind rather than by index means a
/// destination that only exists under some condition does not force the Host to keep two
/// lists in step by hand.
///
/// **Nothing here says bar, rail or drawer.** The Renderer has already measured the window,
/// so it chooses: a bar across the bottom of a phone-shaped window, a rail down the side of
/// a tablet-shaped one, a drawer standing open on a desktop. Asking the Host to choose
/// would mean the width travelling up, a whole VirtualDom pass, and a tree of destinations
/// being destroyed and rebuilt every time the window crossed a boundary, to arrive at the
/// same three layouts the Renderer can reach by moving the nodes it already has.
///
/// `selected_index` counts destinations, not children. It seeds the Renderer's selection
/// and moves it when the change came from somewhere other than a tap; a tap moves the
/// selection in the Renderer and reports it by firing that destination's own `on_click`.
#[component]
pub fn Navigation(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] selected_index: usize,
    /// What sits above the destinations in a strip: a mark, a name, a control.
    ///
    /// Drawn only where the destinations are a strip down the side. A bar along the bottom
    /// is five destinations standing side by side and has nowhere to put this.
    #[props(default)]
    head: Option<Element>,
    /// What sits below them. An account row, usually.
    #[props(default)]
    foot: Option<Element>,
    children: Element,
) -> Element {
    rsx! {
        navigation {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            selected_index: selected_index as i64,
            // The same slot widget a Scaffold's bars arrive in, because it says the same
            // thing: where inside my parent this content goes.
            if let Some(head) = head {
                scaffoldslot { slot: i64::from(u16::from(SlotRole::TopBar)), {head} }
            }
            if let Some(foot) = foot {
                scaffoldslot { slot: i64::from(u16::from(SlotRole::BottomBar)), {foot} }
            }
            {children}
        }
    }
}

/// One destination inside a [`Navigation`].
///
/// The label and the icon are properties rather than a child tree, because the three
/// presentations disagree about how they go together: a bottom bar stacks a small label
/// under the icon, a rail may drop the label entirely, a drawer sets the label beside the
/// icon and left-aligns the row. A `Column { Icon, Text }` handed over as children would
/// have settled that question in the Host, where the window's width is not known.
///
/// The icon is a meaning, never a picture: `IconRole::Inbox` comes out as this design
/// system's inbox.
#[component]
pub fn NavigationItem(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] text: String,
    #[props(default)] icon: Option<IconRole>,
    /// The colour of this destination's icon and label, in both states.
    ///
    /// The design system decides what selected looks like where this is not set, which is
    /// what an adaptive application wants. A unified one names it, because its reference
    /// may have no accent in the bar at all and a system asked the question answers with
    /// its own.
    #[props(default)]
    color: Option<Paint>,
    #[props(default = true)] enabled: bool,
    /// The named group of the strip this destination belongs to.
    ///
    /// Neighbouring destinations carrying the same name are one group and the name is its
    /// heading. A destination that names none belongs to no group and is drawn where it
    /// always was. What a heading looks like is the design system's.
    #[props(default)]
    section: Option<String>,
    #[props(default)] on_click: EventHandler<()>,
) -> Element {
    rsx! {
        navigationitem {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            text,
            icon: opt_role(icon),
            color: opt_paint(color),
            enabled,
            section,
            onclick: move |_| on_click.call(()),
        }
    }
}

/// A temporary surface that slides in from an edge, holding whatever its children are.
///
/// It is a `Dialog` in every way that crosses the boundary: `open` seeds the Renderer's own
/// open state and carries a change that came from elsewhere, and `on_dismiss` says once
/// that the user asked to close it. The drag itself does not cross: while the sheet is
/// being pulled about, Rust hears nothing, and only a drag that ends in a dismissal is
/// reported, once.
///
/// Which edge it comes from is the Renderer's decision, made from the width it measured:
/// up from the bottom in a narrow window, in from the side in a wide one. There is no
/// property that could ask for one of them, for the same reason there is none that can ask
/// a date picker for a wheel.
#[component]
pub fn Sheet(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] open: bool,
    #[props(default)] on_dismiss: EventHandler<()>,
    children: Element,
) -> Element {
    rsx! {
        sheet {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            open,
            ondismiss: move |_| on_dismiss.call(()),
            {children}
        }
    }
}

/// The screen's frame: a top bar, a bottom bar, a floating action and the page.
///
/// Shaped after Material's `Scaffold`, because that is the vocabulary people already
/// write, and for the same reason: a screen's frame is the same few parts every time, and
/// assembling it by hand in every application is how every application ends up with a
/// different one.
///
/// What it buys here is more than the layout. **The application says which slot it filled
/// and nothing about what the slot becomes.** A top bar is the window's caption on a
/// desktop that hands its caption to the application and an ordinary bar where it does
/// not; a bottom bar is a bar on a phone, a rail on a tablet and a permanent drawer on a
/// desktop; a floating action floats, or sits in the toolbar, or folds into a menu. Those
/// are decisions about the running platform and the measured window, and both of those
/// are known in the Renderer and not here.
///
/// The page is the children, and it is laid out clear of whatever the other slots took.
/// Nothing about that arithmetic reaches the application, which is the point: a screen
/// that computed its own inset would be wrong the moment the Renderer chose differently.
///
/// ```ignore
/// rsx! {
///     Scaffold {
///         top_bar: rsx! { TopAppBar { title: "Standard" } },
///         bottom_bar: rsx! { Navigation { /* destinations */ } },
///         Column { /* the page */ }
///     }
/// }
/// ```
#[component]
pub fn Scaffold(
    /// The window's own colour, where this screen holds its own palette.
    ///
    /// Read off the root, so a frame that is the root is where it has to be said. A
    /// screen that says nothing gets the theme's page colour.
    #[props(default)]
    background: Option<Paint>,
    /// What the window itself is made of, where the screen wants it to be a material.
    ///
    /// Read off the root like the colour above, and asking for it is what stands the
    /// window up with something behind it to show. `MaterialRole::Chrome` is the window's
    /// own: on a platform that can put the system's material behind a window, the desktop
    /// shows through wherever the design system draws chrome; everywhere else the design
    /// system answers the role the way it answers it on any other node.
    #[props(default)]
    material: Option<MaterialRole>,
    /// The bar across the top, where this screen wants one.
    #[props(default)]
    top_bar: Option<Element>,
    /// The destinations, or the actions that belong at the bottom of a phone screen.
    #[props(default)]
    bottom_bar: Option<Element>,
    /// The one action a screen is mostly about, where it has one.
    #[props(default)]
    floating_action: Option<Element>,
    /// The page.
    children: Element,
) -> Element {
    rsx! {
        scaffold {
            background: opt_paint(background),
            material: opt_role(material),
            if let Some(bar) = top_bar {
                scaffoldslot { slot: i64::from(u16::from(SlotRole::TopBar)), {bar} }
            }
            if let Some(bar) = bottom_bar {
                scaffoldslot { slot: i64::from(u16::from(SlotRole::BottomBar)), {bar} }
            }
            if let Some(action) = floating_action {
                scaffoldslot {
                    slot: i64::from(u16::from(SlotRole::FloatingAction)),
                    {action}
                }
            }
            scaffoldslot { slot: i64::from(u16::from(SlotRole::Content)), {children} }
        }
    }
}

/// A grid that only builds the rows it can show.
///
/// The windowing protocol is `LazyColumn`'s, unchanged: the Renderer asks for a range of
/// items and this materialises exactly that range. What a grid adds is that the range
/// arrives rounded to whole rows, because a row is what the Renderer lays out.
///
/// How wide a column is can be said two ways, and they are different questions. `columns`
/// is a count the screen insists on. `min_column_width` is a width below which a column
/// may not fall, and the Renderer divides its own width by it: that is the same judgement
/// as a size class, made where the width is known, so the Host does not make it.
///
/// Saying both is saying the count, because a count leaves nothing for the width to
/// decide.
#[component]
pub fn LazyGrid(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    /// A fixed number of columns.
    #[props(default)]
    columns: Option<u32>,
    /// The narrowest a column may be, where the count is the Renderer's to work out.
    #[props(default)]
    min_column_width: Option<f32>,
    item_count: usize,
    #[props(default)] key_of: Option<Callback<usize, String>>,
    item: Callback<usize, Element>,
) -> Element {
    let mut range = use_signal(|| (0_usize, 0_usize));
    let (start, count) = range();
    let first = start.min(item_count);
    let last = first.saturating_add(count).min(item_count);
    rsx! {
        lazygrid {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            item_count: item_count as i64,
            columns: columns.map(i64::from),
            min_column_width: min_column_width.map(f64::from),
            onrangerequest: move |event: dioxus_core::Event<RangeRequest>| {
                let requested = event.data();
                range.set((requested.start(), requested.count()));
            },
            for index in first..last {
                {
                    let item_key = key_of
                        .map_or_else(|| index.to_string(), |key_of| key_of.call(index));
                    rsx! {
                        composebox { key: "{item_key}", item_key, {item.call(index)} }
                    }
                }
            }
        }
    }
}

/// A place files may be dropped.
///
/// A widget rather than a property on every container, because willingness has to be
/// absent by default and a handler cannot be: a listener is attached whether or not a
/// screen supplied one, so four containers each grew three records for saying nothing.
/// Being this widget is the willingness, and nothing else in the tree carries any of it.
///
/// Desktop only in effect. A phone has no notion of letting a file go over a window, so
/// the events never arrive there and the same screen compiles and runs unchanged.
#[component]
pub fn FileDropTarget(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    #[props(default)] alignment: Option<Alignment>,
    /// Files have come over this node. Nothing about what they are: the platforms
    /// disagree about what is knowable before a drop, and a screen that only lights up
    /// does not need to know.
    #[props(default)]
    on_files_entered: EventHandler<()>,
    /// Files were let go here.
    #[props(default)]
    on_files_dropped: EventHandler<FileDrop>,
    children: Element,
) -> Element {
    rsx! {
        filedroptarget {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            alignment: opt_role(alignment),
            onfilesentered: move |_: dioxus_core::Event<()>| on_files_entered.call(()),
            onfilesdropped: move |event: dioxus_core::Event<FileDrop>| {
                on_files_dropped.call(event.data().as_ref().clone());
            },
            {children}
        }
    }
}

/// A small mark on something else that says how many, that there is something new, or one
/// short word.
///
/// With one child it is attached to that child: an icon, an avatar, a destination. With no
/// child it stands on its own, the count at the end of a list row. Compose calls the first
/// a `BadgedBox` and the second a `Badge`; here they are one widget, because whether there
/// is something to attach to is already said by the children.
///
/// `value` is a count and `text` is a short word. With neither it is a dot. Where it sits
/// on its child, what shape it is, and whether 120 is written out or shortened to `99+`
/// are the design system's, so there is no property for a corner or a ceiling.
///
/// The colour is a role and never a literal, and it defaults to the error role the way
/// every one of these systems draws an unread count. The content is read out as part of
/// the child it is attached to, and never on its own.
#[component]
pub fn Badge(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    /// How many. Leave it out for a dot or a word: no count is not the same as a count of
    /// nothing, and only the screen knows which one it means.
    #[props(default)]
    value: Option<u32>,
    /// One short word, shown where a count would be. A count wins where both are given.
    #[props(default)]
    text: Option<String>,
    /// The role the mark is filled with. The error role where it is not given.
    #[props(default)]
    color: Option<ColorRole>,
    #[props(default)] children: Element,
) -> Element {
    rsx! {
        badge {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            count: value.map(i64::from),
            text,
            color: opt_paint(color.map(Paint::Role)),
            {children}
        }
    }
}

/// A region whose text can be dragged over and copied as one selection.
///
/// Every `Text` inside it, however deep and however many, is part of the same selection,
/// so a whole message of paragraphs, list items and code can be picked up in one drag and
/// copied with a line break between each block. Text outside one cannot be selected at all,
/// which is what keeps button labels and destinations from being dragged over as though
/// the screen were a document.
///
/// Buttons, checkboxes and the other controls inside it are left out of the selection and
/// keep working as they always do.
///
/// Nothing about selecting or copying reaches this side. What is selected, the copy
/// shortcut, the context menu and the clipboard all belong to the Renderer, so there is no
/// event to handle and no way to read or set the selection from here.
#[component]
pub fn SelectionContainer(
    #[props(default)] weight: Option<f32>,
    #[props(default)] width: Option<f32>,
    #[props(default)] height: Option<f32>,
    #[props(default)] padding: Option<f32>,
    #[props(default)] padding_role: Option<SpaceRole>,
    #[props(default)] background: Option<Paint>,
    #[props(default)] shape_role: Option<ShapeRole>,
    #[props(default)] corner_radius: Option<f32>,
    #[props(default)] border_width: Option<f32>,
    #[props(default)] border_color: Option<Paint>,
    #[props(default)] elevation: Option<f32>,
    #[props(default)] fill_max_width: bool,
    #[props(default)] fill_max_height: bool,
    children: Element,
) -> Element {
    rsx! {
        selectioncontainer {
            weight: opt_dp(weight),
            width: opt_dp(width),
            height: opt_dp(height),
            padding: opt_dp(padding),
            padding_role: opt_role(padding_role),
            background: opt_paint(background),
            shape_role: opt_role(shape_role),
            corner_radius: opt_dp(corner_radius),
            border_width: opt_dp(border_width),
            border_color: opt_paint(border_color),
            elevation: opt_dp(elevation),
            fill_max_width,
            fill_max_height,
            {children}
        }
    }
}