compose-rust 0.0.10

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
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
//! One composable per widget in the schema.
//!
//! Each is a function with the widget's Compose name that takes what the widget cannot do
//! without, and returns a description whose methods set the rest. The description is
//! composed when the statement it is written in ends, or at once by `content` for a
//! widget that has children.
//!
//! Every attribute list below is the `rsx!` widget's, in its order, with its defaults. A
//! difference would be a widget that draws differently depending on which API wrote it.

#![allow(non_snake_case)]

use super::{
    Attrs, Modifier, Mods, dp_or_zero, emit, optional_text, paint_or_zero, push_modifiers,
    role_or_zero, widget_key,
};
use crate::drawing::DrawList;
use crate::font_features::FontFeature;
use crate::fonts::FontRefs;
use crate::runtime::composer::{EventCallback, OwnedAttr};
use crate::schema::{
    Alignment, Arrangement, ButtonKind, ButtonVariant, ColorRole, CompactNavigation, ContentScale,
    HorizontalAlignment, IconRole, OverflowWrap, Paint, PopoverAlign, PopoverPlacement, SlotRole,
    Snap, SpaceRole, TextAlign, TextOverflow, TypeRole, VerticalAlignment, WidgetKind, WordBreak,
};
use crate::spans::TextSpans;
use crate::{FileDrop, KeyEvent, MotionFinished, PointerEvent, PointerKinds};
use std::cell::RefCell;
use std::rc::Rc;

/// The empty handler every listener has until one is given. The `rsx!` widgets write
/// their listeners whether or not a handler was supplied, so these do too.
fn unit(handler: Option<Box<dyn FnMut()>>) -> EventCallback {
    match handler {
        Some(handler) => EventCallback::Unit(Rc::new(RefCell::new(handler))),
        None => EventCallback::Unit(Rc::new(RefCell::new(|| {}))),
    }
}

fn text_handler(handler: Option<Box<dyn FnMut(String)>>) -> EventCallback {
    match handler {
        Some(handler) => EventCallback::Text(Rc::new(RefCell::new(handler))),
        None => EventCallback::Text(Rc::new(RefCell::new(|_: String| {}))),
    }
}

fn value_handler(handler: Option<Box<dyn FnMut(f64)>>) -> EventCallback {
    match handler {
        Some(handler) => EventCallback::Value(Rc::new(RefCell::new(handler))),
        None => EventCallback::Value(Rc::new(RefCell::new(|_: f64| {}))),
    }
}

/// A node's subscription to pointer input: the kinds it wants and the handler that gets
/// them. A node without one has no pointer input at all.
struct PointerSubscription {
    kinds: PointerKinds,
    handler: Box<dyn FnMut(PointerEvent)>,
}

/// Writes a subscription as the two things the wire needs: the handler, and the mask. A
/// subscription of no kinds writes neither, so it is not a subscription.
fn pointer_input(
    subscription: Option<PointerSubscription>,
    attrs: &mut Attrs,
    listeners: &mut Vec<(&'static str, EventCallback)>,
) {
    let Some(subscription) = subscription else {
        return;
    };
    if subscription.kinds == PointerKinds::NONE {
        return;
    }
    attrs.push((
        "pointer_kinds",
        OwnedAttr::Int(i64::from(subscription.kinds.bits())),
    ));
    listeners.push((
        "onpointer",
        EventCallback::Pointer(Rc::new(RefCell::new(subscription.handler))),
    ));
}

/// What a node says about windows that is not a window's own: the key it is torn
/// off under, and the handler for a dragged window passing over it.
struct TearOff {
    key: Option<String>,
    drag_over: Option<Box<dyn FnMut(crate::windows::WindowDragOver)>>,
}

/// Writes a node's tear-off key and drag-over handler as what the wire needs. A node with
/// neither writes nothing.
fn tear_off_input(
    tear_off: Option<TearOff>,
    attrs: &mut Attrs,
    listeners: &mut Vec<(&'static str, EventCallback)>,
) {
    let Some(tear_off) = tear_off else {
        return;
    };
    if let Some(key) = tear_off.key {
        attrs.push(("tear_off_key", OwnedAttr::Text(key)));
    }
    if let Some(mut handler) = tear_off.drag_over {
        listeners.push((
            "onwindowdragover",
            EventCallback::Window(Rc::new(RefCell::new(
                move |event: crate::windows::WindowEvent| {
                    if let crate::windows::WindowEvent::DragOver { kind, window, x, y } = event {
                        handler(crate::windows::WindowDragOver { kind, window, x, y });
                    }
                },
            ))),
        ));
    }
}

/// Adds `tear_off_key` and `on_window_drag_over` to a builder that has a `tear_off` field.
macro_rules! tear_off {
    ($name:ident) => {
        impl $name {
            /// Lets the user pull this node out into a window of its own, named by `key`:
            /// the `RestoreKey` of the window that results.
            pub fn tear_off_key(mut self, key: impl Into<String>) -> Self {
                self.tear_off
                    .get_or_insert_with(|| TearOff {
                        key: None,
                        drag_over: None,
                    })
                    .key = Some(key.into());
                self
            }

            /// Hears a dragged window pass over this node: where a tab row shows the place
            /// a dropped tab would go.
            pub fn on_window_drag_over(
                mut self,
                handler: impl FnMut(crate::windows::WindowDragOver) + 'static,
            ) -> Self {
                self.tear_off
                    .get_or_insert_with(|| TearOff {
                        key: None,
                        drag_over: None,
                    })
                    .drag_over = Some(std::boxed::Box::new(handler));
                self
            }
        }
    };
}

/// Adds `on_pointer` to a builder that has a `pointer` field.
macro_rules! pointer_subscription {
    ($name:ident) => {
        impl $name {
            /// Subscribes the node to the kinds of pointer input in `kinds`. The handler runs
            /// on the same call as the input; calling `consume` on the event keeps it from
            /// the nearest subscribed ancestor and from the Renderer's own handling.
            ///
            /// A node that never calls this has no pointer input at all, and costs nothing.
            pub fn on_pointer(
                mut self,
                kinds: PointerKinds,
                handler: impl FnMut(PointerEvent) + 'static,
            ) -> Self {
                self.pointer = Some(PointerSubscription {
                    kinds,
                    handler: std::boxed::Box::new(handler),
                });
                self
            }
        }
    };
}

/// Writes a container's click handler as the listener the wire needs. A container that was
/// given none has no `OnClick` at all, so it is not a control: it takes no focus, no key and
/// no pointer press, and says nothing to assistive technology.
fn click_input(
    on_click: Option<Box<dyn FnMut()>>,
    listeners: &mut Vec<(&'static str, EventCallback)>,
) {
    if let Some(handler) = on_click {
        listeners.push(("onclick", unit(Some(handler))));
    }
}

/// Adds `on_click` to a builder that has an `on_click` field.
macro_rules! click_handler {
    ($name:ident) => {
        impl $name {
            /// Makes the container a button. The Renderer gives it keyboard focus, runs
            /// the handler on a click and on Enter or Space, announces it to assistive
            /// technology as a button, and shows hover, pressed and focus state.
            ///
            /// A container that never calls this is not a control and costs nothing.
            pub fn on_click(mut self, handler: impl FnMut() + 'static) -> Self {
                self.on_click = Some(std::boxed::Box::new(handler));
                self
            }
        }
    };
}

/// Optional focus and key handlers on a node. A node that registers none of these
/// emits no focus or key listener property on the wire.
#[derive(Default)]
struct FocusAndKeyHandlers {
    on_focus_gained: Option<Box<dyn FnMut()>>,
    on_focus_lost: Option<Box<dyn FnMut()>>,
    on_key_down: Option<Box<dyn FnMut(KeyEvent)>>,
    on_key_up: Option<Box<dyn FnMut(KeyEvent)>>,
    on_motion_finished: Option<Box<dyn FnMut(MotionFinished)>>,
    focus_handle: Option<super::FocusHandle>,
}

/// Writes a motion handler as the listener the wire needs. A node that was given none has
/// no `OnMotionFinished` at all, and the Renderer attaches nothing to its size motion.
fn motion_input(
    handler: Option<Box<dyn FnMut(MotionFinished)>>,
    listeners: &mut Vec<(&'static str, EventCallback)>,
) {
    if let Some(handler) = handler {
        listeners.push((
            "onmotionfinished",
            EventCallback::Motion(Rc::new(RefCell::new(handler))),
        ));
    }
}

/// Turns the handlers into listeners and binds the focus handle, if there is one, to the
/// node the widget is about to emit, so it is called right before that `emit`.
fn focus_and_key_input(
    mut handlers: FocusAndKeyHandlers,
    listeners: &mut Vec<(&'static str, EventCallback)>,
) {
    super::focus::bind_focus_handle(handlers.focus_handle.take());
    if let Some(handler) = handlers.on_focus_gained.take() {
        listeners.push(("onfocusgained", unit(Some(handler))));
    }
    if let Some(handler) = handlers.on_focus_lost.take() {
        listeners.push(("onfocuslost", unit(Some(handler))));
    }
    if let Some(handler) = handlers.on_key_down.take() {
        listeners.push((
            "onkeydown",
            EventCallback::Key(Rc::new(RefCell::new(handler))),
        ));
    }
    if let Some(handler) = handlers.on_key_up.take() {
        listeners.push((
            "onkeyup",
            EventCallback::Key(Rc::new(RefCell::new(handler))),
        ));
    }
    motion_input(handlers.on_motion_finished.take(), listeners);
}

macro_rules! focus_and_key_handlers {
    ($name:ident) => {
        impl $name {
            /// Runs when this node or a descendant inside it gains keyboard focus.
            pub fn on_focus_gained(mut self, handler: impl FnMut() + 'static) -> Self {
                self.focus_and_keys.on_focus_gained = Some(std::boxed::Box::new(handler));
                self
            }

            /// Runs when this node and its descendants lose keyboard focus.
            pub fn on_focus_lost(mut self, handler: impl FnMut() + 'static) -> Self {
                self.focus_and_keys.on_focus_lost = Some(std::boxed::Box::new(handler));
                self
            }

            /// Sees each key pressed while this node or a descendant inside it holds focus;
            /// calling `consume` on the event keeps the Renderer from acting on it.
            pub fn on_key_down(mut self, handler: impl FnMut(KeyEvent) + 'static) -> Self {
                self.focus_and_keys.on_key_down = Some(std::boxed::Box::new(handler));
                self
            }

            /// Sees each key released while this node or a descendant inside it holds focus.
            pub fn on_key_up(mut self, handler: impl FnMut(KeyEvent) + 'static) -> Self {
                self.focus_and_keys.on_key_up = Some(std::boxed::Box::new(handler));
                self
            }

            /// Binds `handle` to this node, so `handle.request()` puts keyboard focus here.
            pub fn focus_handle(mut self, handle: &super::FocusHandle) -> Self {
                self.focus_and_keys.focus_handle = Some(handle.clone());
                self
            }

            /// Runs once when this node's size motion comes to rest, with the size it settled
            /// at in dp. The node needs `Modifier::motion` for its size to move; without it
            /// nothing is reported. A run interrupted by a newer size is not reported, only
            /// the one that settles, and under reduced motion it arrives in the frame the
            /// size changes in.
            pub fn on_motion_finished(
                mut self,
                handler: impl FnMut(MotionFinished) + 'static,
            ) -> Self {
                self.focus_and_keys.on_motion_finished = Some(std::boxed::Box::new(handler));
                self
            }
        }
    };
}

/// Composes the description when the statement it was written in ends.
macro_rules! composed_on_drop {
    ($name:ident $(<$lifetime:lifetime>)?) => {
        impl$(<$lifetime>)? Drop for $name$(<$lifetime>)? {
            fn drop(&mut self) {
                // A panic is already unwinding the composition, and composing on the way
                // out would only risk a second one.
                if !self.done && !std::thread::panicking() {
                    self.done = true;
                    self.compose(|| {});
                }
            }
        }

        impl$(<$lifetime>)? $name$(<$lifetime>)? {
            /// What sits around it. Compose's `modifier` parameter.
            pub fn modifier(mut self, modifier: Modifier) -> Self {
                self.modifier = modifier;
                self
            }
        }
    };
}

/// A container's `content`: composes it now, with these children.
macro_rules! container {
    ($name:ident $(<$lifetime:lifetime>)?) => {
        composed_on_drop!($name $(<$lifetime>)?);

        impl$(<$lifetime>)? $name$(<$lifetime>)? {
            /// Composes this widget with `content` as its children.
            pub fn content(mut self, content: impl FnOnce()) {
                self.done = true;
                self.compose(content);
            }
        }
    };
}

// ----- layout ---------------------------------------------------------------------------

/// The properties a linear layout takes.
#[derive(Default)]
struct Linear {
    arrangement: Option<Arrangement>,
    spacing: Option<f32>,
    space_role: Option<SpaceRole>,
    alignment: Option<Alignment>,
    intrinsic_size: Option<bool>,
    radio_group: bool,
}

impl Linear {
    fn push(&self, attrs: &mut Attrs) {
        attrs.push(("arrangement", role_or_zero(self.arrangement)));
        attrs.push(("spacing", dp_or_zero(self.spacing)));
        attrs.push(("space_role", role_or_zero(self.space_role)));
        attrs.push(("alignment", role_or_zero(self.alignment)));
        if let Some(intrinsic) = self.intrinsic_size {
            attrs.push(("collapsible", OwnedAttr::Bool(intrinsic)));
        }
        // Written only for a group, so a plain layout sends what it always did.
        if self.radio_group {
            attrs.push(("radio_group", OwnedAttr::Bool(true)));
        }
    }
}

macro_rules! linear {
    ($name:ident, $widget:ident, $key:literal) => {
        #[doc = concat!("A `", stringify!($widget), "` and its builder.")]
        pub struct $name {
            modifier: Modifier,
            linear: Linear,
            pointer: Option<PointerSubscription>,
            on_click: Option<Box<dyn FnMut()>>,
            focus_and_keys: FocusAndKeyHandlers,
            tear_off: Option<TearOff>,
            done: bool,
        }

        #[doc = concat!("Lays its children out one after another. Compose's `", stringify!($widget), "`.")]
        pub fn $widget() -> $name {
            $name {
                modifier: Modifier::new(),
                linear: Linear::default(),
                pointer: None,
                on_click: None,
                focus_and_keys: FocusAndKeyHandlers::default(),
                tear_off: None,
                done: false,
            }
        }

        impl $name {
            /// How the children share the main axis.
            pub fn arrangement(mut self, arrangement: Arrangement) -> Self {
                self.linear.arrangement = Some(arrangement);
                self
            }

            /// A literal gap between children, in dp.
            pub fn spacing(mut self, dp: f32) -> Self {
                self.linear.spacing = Some(dp);
                self
            }

            /// A gap from the design system's spacing ladder.
            pub fn space_role(mut self, role: SpaceRole) -> Self {
                self.linear.space_role = Some(role);
                self
            }

            /// Where the children sit on the cross axis.
            pub fn alignment(mut self, alignment: Alignment) -> Self {
                self.linear.alignment = Some(alignment);
                self
            }

            /// Sizes the cross axis to the tallest child in a `Row` or the widest child in
            /// a `Column`, so children with `fill_max_height` or `fill_max_width` match the
            /// largest sibling without an explicit container size.
            pub fn intrinsic_size(mut self, enabled: bool) -> Self {
                self.linear.intrinsic_size = Some(enabled);
                self
            }

            /// Makes this layout a group of the `RadioButton`s inside it.
            ///
            /// The group is one stop for Tab: the selected button, or the first when none
            /// is. The arrow keys move focus to the next or the previous button, wrapping
            /// at the ends, and each move sends that button's `on_change(true)` once, the
            /// same report a click sends. Which button is selected is still the
            /// application's to decide. Assistive technology hears the layout as a radio
            /// group.
            pub fn radio_group(mut self, enabled: bool) -> Self {
                self.linear.radio_group = enabled;
                self
            }

            fn compose(&mut self, content: impl FnOnce()) {
                let modifier = self.modifier;
                let linear = std::mem::take(&mut self.linear);
                let pointer = self.pointer.take();
                let tear_off = self.tear_off.take();
                let mut listeners = Vec::new();
                let mut subscription = Attrs::new();
                pointer_input(pointer, &mut subscription, &mut listeners);
                click_input(self.on_click.take(), &mut listeners);
                focus_and_key_input(std::mem::take(&mut self.focus_and_keys), &mut listeners);
                tear_off_input(tear_off, &mut subscription, &mut listeners);
                emit(
                    widget_key($key),
                    WidgetKind::$widget,
                    |attrs| {
                        push_modifiers(&modifier, Mods::Container, attrs);
                        linear.push(attrs);
                        attrs.append(&mut subscription);
                    },
                    listeners,
                    content,
                );
            }
        }

        container!($name);
        pointer_subscription!($name);
        click_handler!($name);
        focus_and_key_handlers!($name);
        tear_off!($name);
    };
}

linear!(ColumnScope, Column, "compose_rust::ui::Column");
linear!(RowScope, Row, "compose_rust::ui::Row");

impl RowScope {
    /// Sizes the row to its tallest child so children with `fill_max_height` match their
    /// siblings' height.
    pub fn equal_height(self, enabled: bool) -> Self {
        self.intrinsic_size(enabled)
    }
}

impl ColumnScope {
    /// Sizes the column to its widest child so children with `fill_max_width` match their
    /// siblings' width.
    pub fn equal_width(self, enabled: bool) -> Self {
        self.intrinsic_size(enabled)
    }
}

/// A `FlowRow` and its builder.
pub struct FlowRowScope {
    modifier: Modifier,
    horizontal_spacing: Option<f32>,
    vertical_spacing: Option<f32>,
    horizontal_alignment: Option<HorizontalAlignment>,
    vertical_alignment: Option<VerticalAlignment>,
    max_items_in_each_row: Option<u32>,
    done: bool,
}

/// Lays its children out on a line and starts a new line when the next child does not fit
/// in what is left of it. Each child is measured at its own width, so a line breaks where
/// the text really ends rather than where an estimate put it. A child wider than the whole
/// line has the line to itself and is measured at the line's width. Compose's `FlowRow`.
pub fn FlowRow() -> FlowRowScope {
    FlowRowScope {
        modifier: Modifier::new(),
        horizontal_spacing: None,
        vertical_spacing: None,
        horizontal_alignment: None,
        vertical_alignment: None,
        max_items_in_each_row: None,
        done: false,
    }
}

impl FlowRowScope {
    /// The gap between two children on the same line, in dp.
    pub fn horizontal_spacing(mut self, dp: f32) -> Self {
        self.horizontal_spacing = Some(dp);
        self
    }

    /// The gap between two lines, in dp.
    pub fn vertical_spacing(mut self, dp: f32) -> Self {
        self.vertical_spacing = Some(dp);
        self
    }

    /// Where the children of a line sit along it when they do not fill it. `Start` when
    /// not set.
    pub fn horizontal_alignment(mut self, alignment: HorizontalAlignment) -> Self {
        self.horizontal_alignment = Some(alignment);
        self
    }

    /// Where a child sits inside the height of its line. `Top` when not set.
    pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self {
        self.vertical_alignment = Some(alignment);
        self
    }

    /// The most children one line holds, even when more would fit. No limit when not set.
    pub fn max_items_in_each_row(mut self, count: u32) -> Self {
        self.max_items_in_each_row = Some(count);
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let (horizontal_spacing, vertical_spacing) =
            (self.horizontal_spacing, self.vertical_spacing);
        let (horizontal_alignment, vertical_alignment) =
            (self.horizontal_alignment, self.vertical_alignment);
        let max_items = self.max_items_in_each_row;
        emit(
            widget_key("compose_rust::ui::FlowRow"),
            WidgetKind::FlowRow,
            |attrs| {
                push_modifiers(&modifier, Mods::Container, attrs);
                // Only what was set is written: an absent property is the Renderer's
                // default, and a zero gap or tag would say nothing an absent one does not.
                if let Some(dp) = horizontal_spacing {
                    attrs.push(("horizontal_spacing", dp_or_zero(Some(dp))));
                }
                if let Some(dp) = vertical_spacing {
                    attrs.push(("vertical_spacing", dp_or_zero(Some(dp))));
                }
                if let Some(alignment) = horizontal_alignment {
                    attrs.push(("horizontal_alignment", role_or_zero(Some(alignment))));
                }
                if let Some(alignment) = vertical_alignment {
                    attrs.push(("vertical_alignment", role_or_zero(Some(alignment))));
                }
                if let Some(count) = max_items {
                    attrs.push(("max_items_in_each_row", OwnedAttr::Int(i64::from(count))));
                }
            },
            Vec::new(),
            content,
        );
    }
}

container!(FlowRowScope);

/// A `Box` and its builder. Named apart from `std::boxed::Box`, which a glob import of
/// this module would otherwise hide.
pub struct ComposeBox {
    modifier: Modifier,
    alignment: Option<Alignment>,
    pointer: Option<PointerSubscription>,
    on_click: Option<Box<dyn FnMut()>>,
    focus_and_keys: FocusAndKeyHandlers,
    tear_off: Option<TearOff>,
    done: bool,
}

/// Stacks its children on top of each other. Compose's `Box`.
pub fn Box() -> ComposeBox {
    ComposeBox {
        modifier: Modifier::new(),
        alignment: None,
        pointer: None,
        on_click: None,
        focus_and_keys: FocusAndKeyHandlers::default(),
        tear_off: None,
        done: false,
    }
}

impl ComposeBox {
    /// Where the children sit inside it.
    pub fn alignment(mut self, alignment: Alignment) -> Self {
        self.alignment = Some(alignment);
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let (modifier, alignment) = (self.modifier, self.alignment);
        let pointer = self.pointer.take();
        let tear_off = self.tear_off.take();
        let mut listeners = Vec::new();
        let mut subscription = Attrs::new();
        pointer_input(pointer, &mut subscription, &mut listeners);
        click_input(self.on_click.take(), &mut listeners);
        focus_and_key_input(std::mem::take(&mut self.focus_and_keys), &mut listeners);
        tear_off_input(tear_off, &mut subscription, &mut listeners);
        emit(
            widget_key("compose_rust::ui::Box"),
            WidgetKind::Box,
            |attrs| {
                push_modifiers(&modifier, Mods::Container, attrs);
                attrs.push(("alignment", role_or_zero(alignment)));
                attrs.append(&mut subscription);
            },
            listeners,
            content,
        );
    }
}

container!(ComposeBox);
pointer_subscription!(ComposeBox);
click_handler!(ComposeBox);
focus_and_key_handlers!(ComposeBox);
tear_off!(ComposeBox);

/// A plain container with the standard Modifiers and nothing of its own.
macro_rules! plain_container {
    ($name:ident, $widget:ident, $mods:expr, $key:literal, $doc:literal) => {
        #[doc = concat!("A `", stringify!($widget), "` and its builder.")]
        pub struct $name {
            modifier: Modifier,
            pointer: Option<PointerSubscription>,
            on_click: Option<Box<dyn FnMut()>>,
            focus_and_keys: FocusAndKeyHandlers,
            done: bool,
        }

        #[doc = $doc]
        pub fn $widget() -> $name {
            $name {
                modifier: Modifier::new(),
                pointer: None,
                on_click: None,
                focus_and_keys: FocusAndKeyHandlers::default(),
                done: false,
            }
        }

        impl $name {
            fn compose(&mut self, content: impl FnOnce()) {
                let modifier = self.modifier;
                let pointer = self.pointer.take();
                let mut listeners = Vec::new();
                let mut subscription = Attrs::new();
                pointer_input(pointer, &mut subscription, &mut listeners);
                click_input(self.on_click.take(), &mut listeners);
                focus_and_key_input(std::mem::take(&mut self.focus_and_keys), &mut listeners);
                emit(
                    widget_key($key),
                    WidgetKind::$widget,
                    |attrs| {
                        push_modifiers(&modifier, $mods, attrs);
                        attrs.append(&mut subscription);
                    },
                    listeners,
                    content,
                );
            }
        }

        container!($name);
        pointer_subscription!($name);
        click_handler!($name);
        focus_and_key_handlers!($name);
    };
}

/// The scroll position of a [`ScrollColumn`] or [`ScrollRow`], and a handle to move it.
///
/// The Renderer owns the scroll physics and the scrollbar; this handle reads the current
/// offset and maximum offset in dp as observable states and sends target scroll requests.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ScrollState {
    offset: crate::runtime::MutableState<f32>,
    max_offset: crate::runtime::MutableState<f32>,
    command: crate::runtime::MutableState<(i64, f32, bool)>,
}

impl Default for ScrollState {
    fn default() -> Self {
        Self::new()
    }
}

impl ScrollState {
    /// Creates a new scroll state starting at 0 dp.
    pub fn new() -> Self {
        Self {
            offset: crate::runtime::mutable_state_of(0.0),
            max_offset: crate::runtime::mutable_state_of(0.0),
            command: crate::runtime::mutable_state_of((0, 0.0, false)),
        }
    }

    /// The current scroll offset in dp. A composable that reads this runs again when the
    /// user or a programmatic scroll moves the container.
    pub fn offset(&self) -> f32 {
        self.offset.get()
    }

    /// The furthest offset in dp the container can scroll to (`0.0` when the content fits
    /// inside the viewport). A composable that reads this runs again when the content or
    /// viewport size changes the scrollable distance.
    pub fn max_offset(&self) -> f32 {
        self.max_offset.get()
    }

    /// Jumps to `dp` (clamped by the Renderer to `0.0..=max_offset`).
    pub fn scroll_to(&self, dp: f32) {
        let (revision, _, _) = self.command.get_untracked();
        self.command
            .set((revision.wrapping_add(1).max(1), dp.max(0.0), false));
    }

    /// Smoothly scrolls to `dp` (clamped by the Renderer to `0.0..=max_offset`).
    pub fn animate_scroll_to(&self, dp: f32) {
        let (revision, _, _) = self.command.get_untracked();
        self.command
            .set((revision.wrapping_add(1).max(1), dp.max(0.0), true));
    }
}

/// Remembers a [`ScrollState`] across recompositions. Compose's `rememberScrollState`.
pub fn remember_scroll_state() -> ScrollState {
    crate::runtime::remember(ScrollState::new)
}

fn scroll_state_input(
    state: Option<ScrollState>,
    attrs: &mut Attrs,
    listeners: &mut Vec<(&'static str, EventCallback)>,
) {
    let Some(state) = state else {
        return;
    };
    let (revision, target, animate) = state.command.get();
    if revision > 0 {
        attrs.push(("value", OwnedAttr::Float(f64::from(target))));
        attrs.push(("determinate", OwnedAttr::Bool(animate)));
        attrs.push(("count", OwnedAttr::Int(revision)));
    }
    let offset = state.offset;
    let max_offset = state.max_offset;
    listeners.push((
        "onrangerequest",
        EventCallback::Range(Rc::new(RefCell::new(move |range: crate::RangeRequest| {
            let next_offset = f32::from_bits(range.start() as u32);
            let next_max = f32::from_bits(range.count() as u32);
            if next_offset.is_finite() {
                offset.set(next_offset);
            }
            if next_max.is_finite() {
                max_offset.set(next_max);
            }
        }))),
    ));
}

macro_rules! scroll_container {
    ($name:ident, $widget:ident, $key:literal, $doc:literal) => {
        #[doc = concat!("A `", stringify!($widget), "` and its builder.")]
        pub struct $name {
            modifier: Modifier,
            scroll_state: Option<ScrollState>,
            snap: Snap,
            pointer: Option<PointerSubscription>,
            on_click: Option<Box<dyn FnMut()>>,
            focus_and_keys: FocusAndKeyHandlers,
            done: bool,
        }

        #[doc = $doc]
        pub fn $widget() -> $name {
            $name {
                modifier: Modifier::new(),
                scroll_state: None,
                snap: Snap::None,
                pointer: None,
                on_click: None,
                focus_and_keys: FocusAndKeyHandlers::default(),
                done: false,
            }
        }

        impl $name {
            /// Binds a [`ScrollState`] so the application can read `offset()` and
            /// `max_offset()` or call `scroll_to` and `animate_scroll_to`.
            pub fn scroll_state(mut self, state: &ScrollState) -> Self {
                self.scroll_state = Some(state.clone());
                self
            }

            /// Where the content comes to rest once a drag or a fling ends.
            ///
            /// `Snap::None`, the default, stops wherever the user left it.
            /// `Snap::ItemStart` stops with the start edge of the nearest direct child on
            /// the start of the visible area, like CSS `scroll-snap-align: start`. The
            /// Renderer runs the snap, so it follows the platform's fling physics.
            pub fn snap(mut self, snap: Snap) -> Self {
                self.snap = snap;
                self
            }

            fn compose(&mut self, content: impl FnOnce()) {
                let modifier = self.modifier;
                let snap = self.snap;
                let scroll_state = self.scroll_state.take();
                let pointer = self.pointer.take();
                let mut listeners = Vec::new();
                let mut subscription = Attrs::new();
                scroll_state_input(scroll_state, &mut subscription, &mut listeners);
                pointer_input(pointer, &mut subscription, &mut listeners);
                click_input(self.on_click.take(), &mut listeners);
                focus_and_key_input(std::mem::take(&mut self.focus_and_keys), &mut listeners);
                emit(
                    widget_key($key),
                    WidgetKind::$widget,
                    |attrs| {
                        push_modifiers(&modifier, Mods::Standard, attrs);
                        // Free scrolling is the default, and the default is not sent: a
                        // container that says nothing travels as it did before snapping.
                        if snap != Snap::None {
                            attrs.push(("snap", OwnedAttr::Int(i64::from(u16::from(snap)))));
                        }
                        attrs.append(&mut subscription);
                    },
                    listeners,
                    content,
                );
            }
        }

        container!($name);
        pointer_subscription!($name);
        click_handler!($name);
        focus_and_key_handlers!($name);
    };
}

scroll_container!(
    ScrollColumnScope,
    ScrollColumn,
    "compose_rust::ui::ScrollColumn",
    "All of its content, with a vertical scroll the Renderer owns."
);
scroll_container!(
    ScrollRowScope,
    ScrollRow,
    "compose_rust::ui::ScrollRow",
    "All of its content, with a horizontal scroll the Renderer owns."
);
plain_container!(
    CardScope,
    Card,
    Mods::Container,
    "compose_rust::ui::Card",
    "A grouped container. What it looks like is the design system's."
);
plain_container!(
    SurfaceScope,
    Surface,
    Mods::Container,
    "compose_rust::ui::Surface",
    "A plain background and elevation, without a card's meaning."
);
plain_container!(
    SelectionContainerScope,
    SelectionContainer,
    Mods::Standard,
    "compose_rust::ui::SelectionContainer",
    "Every `Text` inside can be selected and copied as one selection."
);
plain_container!(
    SpacerScope,
    Spacer,
    Mods::Standard,
    "compose_rust::ui::Spacer",
    "Empty space, sized by its Modifier."
);

// ----- text -----------------------------------------------------------------------------

/// A font list as the property blob, or nothing when it is empty.
fn font_bytes(font: &FontRefs) -> OwnedAttr {
    if font.is_empty() {
        OwnedAttr::None
    } else {
        OwnedAttr::Bytes(font.as_bytes().to_vec())
    }
}

/// A `Text` and its builder.
pub struct TextScope {
    modifier: Modifier,
    text: String,
    spans: TextSpans,
    type_role: Option<TypeRole>,
    font_size: Option<f32>,
    font_weight: Option<u16>,
    line_height: Option<f32>,
    letter_spacing: Option<f32>,
    color: Option<Paint>,
    text_align: Option<TextAlign>,
    max_lines: Option<u32>,
    overflow: Option<TextOverflow>,
    font: FontRefs,
    font_features: OwnedAttr,
    word_break: Option<WordBreak>,
    overflow_wrap: Option<OverflowWrap>,
    pointer: Option<PointerSubscription>,
    done: bool,
}

/// Text. Compose's `Text`.
///
/// `type_role` alone takes the design system's size, weight, line height and letter
/// spacing; each other style method overrides one axis of it.
pub fn Text(text: impl Into<String>) -> TextScope {
    TextScope {
        modifier: Modifier::new(),
        text: text.into(),
        spans: TextSpans::default(),
        type_role: None,
        font_size: None,
        font_weight: None,
        line_height: None,
        letter_spacing: None,
        color: None,
        text_align: None,
        max_lines: None,
        overflow: None,
        font: FontRefs::default(),
        font_features: OwnedAttr::None,
        word_break: None,
        overflow_wrap: None,
        pointer: None,
        done: false,
    }
}

impl TextScope {
    /// Runs of different treatment inside the text: a bold phrase, a link, some code.
    pub fn spans(mut self, spans: TextSpans) -> Self {
        self.spans = spans;
        self
    }

    pub fn type_role(mut self, role: TypeRole) -> Self {
        self.type_role = Some(role);
        self
    }

    pub fn font_size(mut self, sp: f32) -> Self {
        self.font_size = Some(sp);
        self
    }

    pub fn font_weight(mut self, weight: u16) -> Self {
        self.font_weight = Some(weight);
        self
    }

    pub fn line_height(mut self, sp: f32) -> Self {
        self.line_height = Some(sp);
        self
    }

    /// Extra space between letters, in sp. 0 draws no extra spacing; NaN, like never
    /// calling this, takes the type role's spacing.
    pub fn letter_spacing(mut self, sp: f32) -> Self {
        self.letter_spacing = Some(sp);
        self
    }

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

    pub fn text_align(mut self, align: TextAlign) -> Self {
        self.text_align = Some(align);
        self
    }

    pub fn max_lines(mut self, lines: u32) -> Self {
        self.max_lines = Some(lines);
        self
    }

    pub fn overflow(mut self, overflow: TextOverflow) -> Self {
        self.overflow = Some(overflow);
        self
    }

    /// The fonts to try, in order. Only text with `TypeRole::None` takes its own fonts;
    /// text with a role is set in the theme's font for that role.
    pub fn font(mut self, font: FontRefs) -> Self {
        self.font = font;
        self
    }

    /// OpenType features of the font the text is set in, such as
    /// `FontFeature::TABULAR_NUMS` for digits of one width. Compose's
    /// `TextStyle.fontFeatureSettings`, CSS's `font-feature-settings`.
    ///
    /// Taken with or without a type role: a feature picks one of the forms the font already
    /// has and does not change the font. A tag given twice takes the later value, an empty
    /// list is no features, and at most `MAX_FONT_FEATURES` (16) are taken. A font without
    /// the feature draws as it would have.
    pub fn font_feature_settings(mut self, features: &[FontFeature]) -> Self {
        self.font_features = crate::font_features::font_feature_attr(features);
        self
    }

    /// Where a line may break inside a word. CSS `word-break`.
    pub fn word_break(mut self, word_break: WordBreak) -> Self {
        self.word_break = Some(word_break);
        self
    }

    /// Whether a token too long for its line, such as a path or a URL, may break inside
    /// itself. CSS `overflow-wrap`. With `Anywhere` such a token breaks between its letters
    /// even under `WordBreak::KeepAll`; tokens that fit keep the breaking rule they had.
    pub fn overflow_wrap(mut self, overflow_wrap: OverflowWrap) -> Self {
        self.overflow_wrap = Some(overflow_wrap);
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let text = std::mem::take(&mut self.text);
        let spans = std::mem::take(&mut self.spans);
        let font = std::mem::take(&mut self.font);
        let font_features = std::mem::replace(&mut self.font_features, OwnedAttr::None);
        let (word_break, overflow_wrap) = (self.word_break, self.overflow_wrap);
        let (type_role, font_size, font_weight, line_height, letter_spacing) = (
            self.type_role,
            self.font_size,
            self.font_weight,
            self.line_height,
            self.letter_spacing,
        );
        let (color, text_align, max_lines, overflow) =
            (self.color, self.text_align, self.max_lines, self.overflow);
        let pointer = self.pointer.take();
        let mut listeners = Vec::new();
        let mut subscription = Attrs::new();
        pointer_input(pointer, &mut subscription, &mut listeners);
        emit(
            widget_key("compose_rust::ui::Text"),
            WidgetKind::Text,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                // Left out entirely when there are none, as the rsx widget leaves it out.
                let span_fonts = spans.font_table().to_vec();
                attrs.push((
                    "spans",
                    if spans.is_empty() {
                        OwnedAttr::None
                    } else {
                        OwnedAttr::Spans(spans)
                    },
                ));
                // The fonts each styled run names travel beside the runs, and are left out
                // when no run names any.
                attrs.push((
                    "span_fonts",
                    if span_fonts.is_empty() {
                        OwnedAttr::None
                    } else {
                        OwnedAttr::Bytes(span_fonts)
                    },
                ));
                attrs.push(("font", font_bytes(&font)));
                // Left out when no feature was asked for, so such a text sends what it
                // always did. A first composition writes every attribute in the list, an
                // empty one as well, so leaving it out is what keeps it off the wire.
                if font_features != OwnedAttr::None {
                    attrs.push(("font_feature_settings", font_features));
                }
                attrs.push(("word_break", super::role(word_break)));
                attrs.push(("overflow_wrap", super::role(overflow_wrap)));
                attrs.push(("text", OwnedAttr::Text(text)));
                // `TypeRole::None` is tag 0, so it travels as an explicit 0 and a role that
                // was never set travels as nothing. Sent the other way, the text that names
                // its own fonts could not say it has no role, and the Renderer set it in
                // the default role's font with its fonts ignored.
                attrs.push(("type_role", super::role(type_role)));
                attrs.push(("font_size", dp_or_zero(font_size)));
                attrs.push((
                    "font_weight",
                    OwnedAttr::Int(i64::from(font_weight.unwrap_or(0))),
                ));
                attrs.push(("line_height", dp_or_zero(line_height)));
                // Unset is NaN, so an explicit 0 stays a 0 and draws no extra spacing.
                attrs.push((
                    "letter_spacing",
                    OwnedAttr::Float(letter_spacing.map_or(f64::NAN, f64::from)),
                ));
                attrs.push(("color", paint_or_zero(color)));
                attrs.push(("text_align", role_or_zero(text_align)));
                attrs.push((
                    "max_lines",
                    OwnedAttr::Int(i64::from(max_lines.unwrap_or(0))),
                ));
                attrs.push(("overflow", role_or_zero(overflow)));
                attrs.append(&mut subscription);
            },
            listeners,
            content,
        );
    }
}

composed_on_drop!(TextScope);
pointer_subscription!(TextScope);

/// A `TextField` and its builder.
pub struct TextFieldScope {
    modifier: Modifier,
    text: String,
    placeholder: String,
    enabled: bool,
    multiline: bool,
    type_role: Option<TypeRole>,
    font_size: Option<f32>,
    font_weight: Option<u16>,
    line_height: Option<f32>,
    letter_spacing: Option<f32>,
    color: Option<Paint>,
    font: FontRefs,
    font_features: OwnedAttr,
    word_break: Option<WordBreak>,
    overflow_wrap: Option<OverflowWrap>,
    on_value_change: Option<Box<dyn FnMut(String)>>,
    on_submit: Option<Box<dyn FnMut(String)>>,
    on_focus_gained: Option<Box<dyn FnMut()>>,
    on_focus_lost: Option<Box<dyn FnMut()>>,
    on_key_down: Option<Box<dyn FnMut(KeyEvent)>>,
    on_key_up: Option<Box<dyn FnMut(KeyEvent)>>,
    on_composition_change: Option<Box<dyn FnMut(bool)>>,
    on_motion_finished: Option<Box<dyn FnMut(MotionFinished)>>,
    focus_handle: Option<super::FocusHandle>,
    bare: bool,
    done: bool,
}

/// An uncontrolled text field. Compose's `TextField`, with the value kept where the
/// platform's text input keeps it: the Renderer reports each change and the Host never
/// writes the value back while the user types.
pub fn TextField() -> TextFieldScope {
    TextFieldScope {
        modifier: Modifier::new(),
        text: String::new(),
        placeholder: String::new(),
        enabled: true,
        multiline: false,
        type_role: None,
        font_size: None,
        font_weight: None,
        line_height: None,
        letter_spacing: None,
        color: None,
        font: FontRefs::default(),
        font_features: OwnedAttr::None,
        word_break: None,
        overflow_wrap: None,
        on_value_change: None,
        on_submit: None,
        on_focus_gained: None,
        on_focus_lost: None,
        on_key_down: None,
        on_key_up: None,
        on_composition_change: None,
        on_motion_finished: None,
        focus_handle: None,
        bare: false,
        done: false,
    }
}

impl TextFieldScope {
    /// The text the field starts with. Only the start: the field stays uncontrolled, so
    /// what the user types is never written back, and a later value here changes nothing
    /// on the field that is already there. To show other text, such as another document,
    /// give the field a new identity with `key`, and the new field starts with this text.
    pub fn text(mut self, text: impl Into<String>) -> Self {
        self.text = text.into();
        self
    }

    pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
        self.placeholder = placeholder.into();
        self
    }

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

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

    /// The rung of the type ladder the field's own text is set in.
    pub fn type_role(mut self, role: TypeRole) -> Self {
        self.type_role = Some(role);
        self
    }

    /// Explicit font size in sp. Overrides the type role's size.
    pub fn font_size(mut self, sp: f32) -> Self {
        self.font_size = Some(sp);
        self
    }

    /// Explicit font weight (e.g. 400, 500, 600, 700). Overrides the type role's weight.
    pub fn font_weight(mut self, weight: u16) -> Self {
        self.font_weight = Some(weight);
        self
    }

    /// Explicit line height in sp.
    pub fn line_height(mut self, sp: f32) -> Self {
        self.line_height = Some(sp);
        self
    }

    /// Explicit letter spacing in sp.
    pub fn letter_spacing(mut self, sp: f32) -> Self {
        self.letter_spacing = Some(sp);
        self
    }

    /// Explicit text colour.
    pub fn color(mut self, paint: Paint) -> Self {
        self.color = Some(paint);
        self
    }

    /// The fonts to try for the field's text, in order. Taken only with `TypeRole::None`.
    pub fn font(mut self, font: FontRefs) -> Self {
        self.font = font;
        self
    }

    /// OpenType features for the typed text and the placeholder, as
    /// `Text::font_feature_settings` takes them. Taken with or without a type role.
    pub fn font_feature_settings(mut self, features: &[FontFeature]) -> Self {
        self.font_features = crate::font_features::font_feature_attr(features);
        self
    }

    /// Where a line of the typed text and of the placeholder may break inside a word, as
    /// `Text::word_break` does. CSS `word-break`.
    pub fn word_break(mut self, word_break: WordBreak) -> Self {
        self.word_break = Some(word_break);
        self
    }

    /// Whether a token too long for its line may break inside itself, in the typed text
    /// and the placeholder, as `Text::overflow_wrap` does. CSS `overflow-wrap`.
    pub fn overflow_wrap(mut self, overflow_wrap: OverflowWrap) -> Self {
        self.overflow_wrap = Some(overflow_wrap);
        self
    }

    pub fn on_value_change(mut self, handler: impl FnMut(String) + 'static) -> Self {
        self.on_value_change = Some(std::boxed::Box::new(handler));
        self
    }

    pub fn on_submit(mut self, handler: impl FnMut(String) + 'static) -> Self {
        self.on_submit = Some(std::boxed::Box::new(handler));
        self
    }

    pub fn on_focus_gained(mut self, handler: impl FnMut() + 'static) -> Self {
        self.on_focus_gained = Some(std::boxed::Box::new(handler));
        self
    }

    pub fn on_focus_lost(mut self, handler: impl FnMut() + 'static) -> Self {
        self.on_focus_lost = Some(std::boxed::Box::new(handler));
        self
    }

    /// Sees each key the field is pressed with; calling `consume` on the event keeps the
    /// field from acting on it.
    pub fn on_key_down(mut self, handler: impl FnMut(KeyEvent) + 'static) -> Self {
        self.on_key_down = Some(std::boxed::Box::new(handler));
        self
    }

    /// Sees each key released while the field holds focus.
    pub fn on_key_up(mut self, handler: impl FnMut(KeyEvent) + 'static) -> Self {
        self.on_key_up = Some(std::boxed::Box::new(handler));
        self
    }

    /// Reports when an input method begins or ends an active composition in the field.
    pub fn on_composition_change(mut self, handler: impl FnMut(bool) + 'static) -> Self {
        self.on_composition_change = Some(std::boxed::Box::new(handler));
        self
    }

    /// Binds `handle` to this field, so `handle.request()` puts keyboard focus here.
    pub fn focus_handle(mut self, handle: &super::FocusHandle) -> Self {
        self.focus_handle = Some(handle.clone());
        self
    }

    /// Runs once when the field's size motion comes to rest, with the size it settled at
    /// in dp. The field needs `Modifier::motion` for its size to move; without it nothing
    /// is reported. A run interrupted by a newer size is not reported, only the one that
    /// settles.
    pub fn on_motion_finished(mut self, handler: impl FnMut(MotionFinished) + 'static) -> Self {
        self.on_motion_finished = Some(std::boxed::Box::new(handler));
        self
    }

    /// Draws the field without the design system's frame: no fill, line, underline,
    /// corner clip, inner padding, minimum height or focus line. Text, placeholder, caret,
    /// selection and input methods stay as they are. For a field that sits inside a box
    /// which already draws its own frame, so the frame is not drawn twice. A `background`
    /// or `border` given on the node is still drawn.
    pub fn bare(mut self) -> Self {
        self.bare = true;
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let text = std::mem::take(&mut self.text);
        let placeholder = std::mem::take(&mut self.placeholder);
        let (enabled, multiline, type_role) = (self.enabled, self.multiline, self.type_role);
        let bare = self.bare;
        let (font_size, font_weight, line_height) =
            (self.font_size, self.font_weight, self.line_height);
        let (letter_spacing, color) = (self.letter_spacing, self.color);
        let font = std::mem::take(&mut self.font);
        let font_features = std::mem::replace(&mut self.font_features, OwnedAttr::None);
        let (word_break, overflow_wrap) = (self.word_break, self.overflow_wrap);
        let key_down: EventCallback = match self.on_key_down.take() {
            Some(handler) => EventCallback::Key(Rc::new(RefCell::new(handler))),
            None => EventCallback::Key(Rc::new(RefCell::new(|_: KeyEvent| {}))),
        };
        let mut listeners = vec![
            ("onvaluechange", text_handler(self.on_value_change.take())),
            ("onsubmit", text_handler(self.on_submit.take())),
            ("onfocuslost", unit(self.on_focus_lost.take())),
            ("onkeydown", key_down),
        ];
        if let Some(handler) = self.on_focus_gained.take() {
            listeners.push(("onfocusgained", unit(Some(handler))));
        }
        if let Some(handler) = self.on_key_up.take() {
            listeners.push((
                "onkeyup",
                EventCallback::Key(Rc::new(RefCell::new(handler))),
            ));
        }
        if let Some(mut handler) = self.on_composition_change.take() {
            listeners.push((
                "oncompositionchange",
                EventCallback::Value(Rc::new(RefCell::new(move |value: f64| {
                    handler(value != 0.0);
                }))),
            ));
        }
        motion_input(self.on_motion_finished.take(), &mut listeners);
        super::focus::bind_focus_handle(self.focus_handle.take());
        emit(
            widget_key("compose_rust::ui::TextField"),
            WidgetKind::TextField,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                // Written only when there is a starting text, so a field without one sends
                // the same records it always did.
                if !text.is_empty() {
                    attrs.push(("text", OwnedAttr::Text(text)));
                }
                attrs.push(("placeholder", OwnedAttr::Text(placeholder)));
                attrs.push(("enabled", OwnedAttr::Bool(enabled)));
                attrs.push(("multiline", OwnedAttr::Bool(multiline)));
                // An explicit `TypeRole::None` is a 0 on the wire, an unset role nothing,
                // as for `Text`.
                attrs.push(("type_role", super::role(type_role)));
                if let Some(font_size) = font_size {
                    attrs.push(("font_size", OwnedAttr::Float(font_size as f64)));
                }
                if let Some(font_weight) = font_weight {
                    attrs.push(("font_weight", OwnedAttr::Int(font_weight as i64)));
                }
                if let Some(line_height) = line_height {
                    attrs.push(("line_height", OwnedAttr::Float(line_height as f64)));
                }
                if let Some(letter_spacing) = letter_spacing {
                    attrs.push(("letter_spacing", OwnedAttr::Float(letter_spacing as f64)));
                }
                if let Some(color) = color {
                    attrs.push(("color", super::paint(Some(color))));
                }
                attrs.push(("font", font_bytes(&font)));
                // Left out when no feature was asked for, as `Text` leaves it out.
                if font_features != OwnedAttr::None {
                    attrs.push(("font_feature_settings", font_features));
                }
                attrs.push(("word_break", super::role(word_break)));
                attrs.push(("overflow_wrap", super::role(overflow_wrap)));
                // Written only for a bare field, so a framed one sends what it always did.
                if bare {
                    attrs.push(("bare", OwnedAttr::Bool(true)));
                }
            },
            listeners,
            content,
        );
    }
}

composed_on_drop!(TextFieldScope);

// ----- buttons and controls ---------------------------------------------------------------

/// The label's line limit and overflow, written only when they were set, so a widget that
/// never asks for them sends what it always sent.
fn lines_and_overflow(max_lines: Option<u32>, overflow: Option<TextOverflow>, attrs: &mut Attrs) {
    if let Some(lines) = max_lines {
        attrs.push(("max_lines", OwnedAttr::Int(i64::from(lines))));
    }
    if overflow.is_some() {
        attrs.push(("overflow", role_or_zero(overflow)));
    }
}

/// The colours and size a Switch, Slider, Checkbox or RadioButton was asked to draw with. Each is sent
/// only when it was set, so a control that sets none sends the properties it always has
/// and the design system draws it.
#[derive(Clone, Copy, Default)]
struct ControlLook {
    track: Option<Paint>,
    thumb: Option<Paint>,
    active_track: Option<Paint>,
    container: Option<Paint>,
    check: Option<Paint>,
    size: Option<(f32, f32)>,
}

impl ControlLook {
    fn push(&self, attrs: &mut Attrs) {
        let paints = [
            ("track_color", self.track),
            ("thumb_color", self.thumb),
            ("active_track_color", self.active_track),
            ("container_color", self.container),
            ("check_color", self.check),
        ];
        for (name, paint) in paints {
            if let Some(paint) = paint {
                attrs.push((name, super::paint(Some(paint))));
            }
        }
        if let Some((width, height)) = self.size {
            let mut bytes = Vec::with_capacity(8);
            bytes.extend_from_slice(&width.to_bits().to_le_bytes());
            bytes.extend_from_slice(&height.to_bits().to_le_bytes());
            attrs.push(("control_size", OwnedAttr::Bytes(bytes)));
        }
    }
}

/// Adds a colour setter that fills one slot of `look`.
macro_rules! control_colour {
    ($name:ident, $slot:ident, $setter:ident, $doc:literal) => {
        impl $name {
            #[doc = $doc]
            pub fn $setter(mut self, paint: Paint) -> Self {
                self.look.$slot = Some(paint);
                self
            }
        }
    };
}

/// Writes a widget's own accessible name. It replaces one the Modifier gave, so the name
/// said on the widget is the one that reaches assistive technology.
fn push_accessible_name(name: Option<String>, attrs: &mut Attrs) {
    if let Some(name) = name.filter(|name| !name.is_empty()) {
        attrs.retain(|(key, _)| *key != "accessible_name");
        attrs.push(("accessible_name", OwnedAttr::Text(name)));
    }
}

/// Adds `accessible_name` to a builder that has an `accessible_name` field.
macro_rules! accessible_name {
    ($name:ident) => {
        impl $name {
            /// The name assistive technology reads. With no visible label it is the
            /// control's name; with one it replaces the label, and on an icon-only button
            /// it is read instead of the icon's meaning.
            pub fn accessible_name(mut self, name: impl Into<String>) -> Self {
                self.accessible_name = Some(name.into());
                self
            }
        }
    };
}

/// A `Button` and its builder.
pub struct ButtonScope {
    modifier: Modifier,
    text: String,
    icon: Option<IconRole>,
    enabled: bool,
    variant: Option<ButtonVariant>,
    kind: Option<ButtonKind>,
    color: Option<Paint>,
    max_lines: Option<u32>,
    overflow: Option<TextOverflow>,
    accessible_name: Option<String>,
    text_align: Option<TextAlign>,
    font_size: Option<f32>,
    font_weight: Option<u16>,
    type_role: Option<TypeRole>,
    on_click: Option<Box<dyn FnMut()>>,
    focus_handle: Option<super::FocusHandle>,
    done: bool,
}

/// A button with a label. Compose's `Button`; the variant is the design system's seam,
/// so `ButtonVariant::Text` is Compose's `TextButton` and `Outlined` its `OutlinedButton`.
///
/// An empty label with an icon is an icon button, named for assistive technology by its
/// icon's meaning.
pub fn Button(text: impl Into<String>) -> ButtonScope {
    ButtonScope {
        modifier: Modifier::new(),
        text: text.into(),
        icon: None,
        enabled: true,
        variant: None,
        kind: None,
        color: None,
        max_lines: None,
        overflow: None,
        accessible_name: None,
        text_align: None,
        font_size: None,
        font_weight: None,
        type_role: None,
        on_click: None,
        focus_handle: None,
        done: false,
    }
}

accessible_name!(ButtonScope);

impl ButtonScope {
    /// Where the label sits in the button. A button that never calls this sends nothing
    /// and keeps its label centred.
    pub fn text_align(mut self, align: TextAlign) -> Self {
        self.text_align = Some(align);
        self
    }

    /// The label's size in sp. A button that never calls this sends nothing and takes the
    /// design system's size for its variant.
    pub fn font_size(mut self, sp: f32) -> Self {
        self.font_size = Some(sp);
        self
    }

    /// The label's weight (e.g. 400, 500, 600, 700). Overrides the type role's weight. A
    /// button that never calls this sends nothing.
    pub fn font_weight(mut self, weight: u16) -> Self {
        self.font_weight = Some(weight);
        self
    }

    /// The rung of the type ladder the label is set in: its size, weight and line height.
    /// `font_size` and `font_weight` win over the role's values. A button that never calls
    /// this sends nothing and keeps the design system's label style for its variant.
    pub fn type_role(mut self, role: TypeRole) -> Self {
        self.type_role = Some(role);
        self
    }

    /// The meaning of the glyph on it, never a picture.
    pub fn icon(mut self, icon: IconRole) -> Self {
        self.icon = Some(icon);
        self
    }

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

    pub fn variant(mut self, variant: ButtonVariant) -> Self {
        self.variant = Some(variant);
        self
    }

    /// What the button is, apart from how loud it is: `ButtonKind::ActionKey` is one key of
    /// a dense grid, drawn in the design system's own calculator-key shape.
    pub fn kind(mut self, kind: ButtonKind) -> Self {
        self.kind = Some(kind);
        self
    }

    /// How many lines the label may take. A label is one line unless this says more, so a
    /// Korean label is never broken between syllables by a squeezed parent.
    pub fn max_lines(mut self, lines: u32) -> Self {
        self.max_lines = Some(lines);
        self
    }

    /// What happens to a label longer than its lines.
    pub fn overflow(mut self, overflow: TextOverflow) -> Self {
        self.overflow = Some(overflow);
        self
    }

    /// The label's colour, for the rare button whose meaning is not its variant's.
    pub fn color(mut self, paint: Paint) -> Self {
        self.color = Some(paint);
        self
    }

    /// The same, when there may be no colour of its own.
    pub fn color_if(mut self, paint: Option<Paint>) -> Self {
        self.color = paint;
        self
    }

    pub fn on_click(mut self, handler: impl FnMut() + 'static) -> Self {
        self.on_click = Some(std::boxed::Box::new(handler));
        self
    }

    /// Binds `handle` to this button, so `handle.request()` puts keyboard focus here.
    pub fn focus_handle(mut self, handle: &super::FocusHandle) -> Self {
        self.focus_handle = Some(handle.clone());
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let text = std::mem::take(&mut self.text);
        let (icon, enabled, variant, kind, color) =
            (self.icon, self.enabled, self.variant, self.kind, self.color);
        let (max_lines, overflow) = (self.max_lines, self.overflow);
        let accessible_name = self.accessible_name.take();
        let (text_align, font_size) = (self.text_align, self.font_size);
        let (font_weight, type_role) = (self.font_weight, self.type_role);
        let listeners = vec![("onclick", unit(self.on_click.take()))];
        super::focus::bind_focus_handle(self.focus_handle.take());
        emit(
            widget_key("compose_rust::ui::Button"),
            WidgetKind::Button,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("text", OwnedAttr::Text(text)));
                attrs.push(("icon", super::role(icon)));
                attrs.push(("enabled", OwnedAttr::Bool(enabled)));
                attrs.push(("variant", role_or_zero(variant)));
                attrs.push(("button_kind", role_or_zero(kind)));
                attrs.push(("color", super::paint(color)));
                lines_and_overflow(max_lines, overflow, attrs);
                push_accessible_name(accessible_name, attrs);
                if text_align.is_some() {
                    attrs.push(("text_align", role_or_zero(text_align)));
                }
                if font_size.is_some() {
                    attrs.push(("font_size", dp_or_zero(font_size)));
                }
                if let Some(font_weight) = font_weight {
                    attrs.push(("font_weight", OwnedAttr::Int(i64::from(font_weight))));
                }
                if type_role.is_some() {
                    attrs.push(("type_role", super::role(type_role)));
                }
            },
            listeners,
            content,
        );
    }
}

container!(ButtonScope);

/// A two-state control, for Checkbox, RadioButton and Switch alike: the Host owns the
/// value, the control draws exactly what it is told and reports what the user asked for.
macro_rules! toggle {
    ($name:ident, $widget:ident, $setter:ident, $key:literal, $doc:literal) => {
        #[doc = concat!("A `", stringify!($widget), "` and its builder.")]
        pub struct $name {
            modifier: Modifier,
            checked: bool,
            enabled: bool,
            accessible_name: Option<String>,
            look: ControlLook,
            on_change: Option<Box<dyn FnMut(bool)>>,
            done: bool,
        }

        #[doc = $doc]
        pub fn $widget($setter: bool) -> $name {
            $name {
                modifier: Modifier::new(),
                checked: $setter,
                enabled: true,
                accessible_name: None,
                look: ControlLook::default(),
                on_change: None,
                done: false,
            }
        }

        accessible_name!($name);

        impl $name {
            pub fn enabled(mut self, enabled: bool) -> Self {
                self.enabled = enabled;
                self
            }

            /// Called with the state the user asked for. The control changes only when the
            /// value passed in does.
            pub fn on_change(mut self, handler: impl FnMut(bool) + 'static) -> Self {
                self.on_change = Some(std::boxed::Box::new(handler));
                self
            }

            fn compose(&mut self, content: impl FnOnce()) {
                let modifier = self.modifier;
                let (checked, enabled) = (self.checked, self.enabled);
                let accessible_name = self.accessible_name.take();
                let look = self.look;
                let handler: EventCallback = match self.on_change.take() {
                    Some(mut handler) => EventCallback::Value(Rc::new(RefCell::new(
                        // Off is 0.0 and on is anything else, so a Renderer that reports a
                        // half-finished transition still reads as on.
                        move |value: f64| handler(value != 0.0),
                    ))),
                    None => value_handler(None),
                };
                emit(
                    widget_key($key),
                    WidgetKind::$widget,
                    |attrs| {
                        push_modifiers(&modifier, Mods::Standard, attrs);
                        attrs.push(("checked", OwnedAttr::Bool(checked)));
                        attrs.push(("enabled", OwnedAttr::Bool(enabled)));
                        push_accessible_name(accessible_name, attrs);
                        look.push(attrs);
                    },
                    vec![("onchange", handler)],
                    content,
                );
            }
        }

        composed_on_drop!($name);
    };
}

control_colour!(
    SwitchScope,
    track,
    track_color,
    "The track, whichever state it is in. The Host owns the state, so it passes the colour \
     that goes with it."
);
control_colour!(
    SwitchScope,
    thumb,
    thumb_color,
    "The thumb that travels along the track."
);

impl SwitchScope {
    /// The track's width and height in dp. The thumb scales with the height.
    pub fn size(mut self, width: f32, height: f32) -> Self {
        self.look.size = Some((width, height));
        self
    }
}

control_colour!(
    CheckboxScope,
    container,
    container_color,
    "The box when it is checked."
);
control_colour!(CheckboxScope, check, check_color, "The check mark.");

impl CheckboxScope {
    /// The length of the box's side in dp.
    pub fn size(mut self, side: f32) -> Self {
        self.look.size = Some((side, side));
        self
    }
}

control_colour!(
    RadioButtonScope,
    container,
    ring_color,
    "The ring around the button, selected or not. Where the design system fills a selected \
     button, the fill takes this colour as well."
);
control_colour!(
    RadioButtonScope,
    check,
    dot_color,
    "The dot inside a selected button."
);

impl RadioButtonScope {
    /// The diameter of the ring in dp. The dot scales with it.
    pub fn size(mut self, side: f32) -> Self {
        self.look.size = Some((side, side));
        self
    }
}

toggle!(
    CheckboxScope,
    Checkbox,
    checked,
    "compose_rust::ui::Checkbox",
    "A box that is ticked or not. Compose's `Checkbox(checked, onCheckedChange)`."
);
toggle!(
    RadioButtonScope,
    RadioButton,
    selected,
    "compose_rust::ui::RadioButton",
    "One choice out of several. Compose's `RadioButton(selected, onClick)`."
);
toggle!(
    SwitchScope,
    Switch,
    checked,
    "compose_rust::ui::Switch",
    "An on and off control. Compose's `Switch(checked, onCheckedChange)`."
);

/// A `Slider` and its builder.
pub struct SliderScope {
    modifier: Modifier,
    color: Option<Paint>,
    value: f32,
    min: f32,
    max: f32,
    steps: u32,
    enabled: bool,
    accessible_name: Option<String>,
    look: ControlLook,
    thumb_size: Option<f32>,
    on_change: Option<Box<dyn FnMut(f32)>>,
    done: bool,
}

/// A value picked from a range. Compose's `Slider`. The drag is the Renderer's; `value`
/// seeds it and moves it when the change came from elsewhere.
pub fn Slider(value: f32) -> SliderScope {
    SliderScope {
        modifier: Modifier::new(),
        color: None,
        value,
        min: 0.0,
        max: 1.0,
        steps: 0,
        enabled: true,
        accessible_name: None,
        look: ControlLook::default(),
        thumb_size: None,
        on_change: None,
        done: false,
    }
}

accessible_name!(SliderScope);
control_colour!(
    SliderScope,
    track,
    track_color,
    "The part of the track that is not filled."
);
control_colour!(SliderScope, thumb, thumb_color, "The thumb.");
control_colour!(
    SliderScope,
    active_track,
    active_track_color,
    "The part of the track up to the thumb."
);

impl SliderScope {
    /// The two ends. Compose's `valueRange`.
    pub fn range(mut self, min: f32, max: f32) -> Self {
        self.min = min;
        self.max = max;
        self
    }

    /// Stops between the ends; zero is continuous.
    pub fn steps(mut self, steps: u32) -> Self {
        self.steps = steps;
        self
    }

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

    /// The filled part of the track and the thumb.
    pub fn color(mut self, paint: Paint) -> Self {
        self.color = Some(paint);
        self
    }

    /// The thumb's diameter in dp. Without it the design system picks the size. The track
    /// and the area that takes a press do not change with it, so a small thumb is still
    /// easy to grab.
    pub fn thumb_size(mut self, dp: f32) -> Self {
        self.thumb_size = Some(dp);
        self
    }

    pub fn on_change(mut self, handler: impl FnMut(f32) + 'static) -> Self {
        self.on_change = Some(std::boxed::Box::new(handler));
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let thumb_size = self.thumb_size;
        let (color, value, min, max, steps, enabled) = (
            self.color,
            self.value,
            self.min,
            self.max,
            self.steps,
            self.enabled,
        );
        let accessible_name = self.accessible_name.take();
        let look = self.look;
        let handler = match self.on_change.take() {
            Some(mut handler) => EventCallback::Value(Rc::new(RefCell::new(move |value: f64| {
                handler(value as f32)
            }))),
            None => value_handler(None),
        };
        emit(
            widget_key("compose_rust::ui::Slider"),
            WidgetKind::Slider,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("color", super::paint(color)));
                attrs.push(("value", OwnedAttr::Float(f64::from(value))));
                attrs.push(("min", OwnedAttr::Float(f64::from(min))));
                attrs.push(("max", OwnedAttr::Float(f64::from(max))));
                attrs.push(("steps", OwnedAttr::Int(i64::from(steps))));
                attrs.push(("enabled", OwnedAttr::Bool(enabled)));
                push_accessible_name(accessible_name, attrs);
                look.push(attrs);
                if let Some(dp) = thumb_size {
                    attrs.push(("thumb_size", OwnedAttr::Float(f64::from(dp))));
                }
            },
            vec![("onchange", handler)],
            content,
        );
    }
}

composed_on_drop!(SliderScope);

/// A `ProgressIndicator` and its builder.
pub struct ProgressIndicatorScope {
    modifier: Modifier,
    value: f32,
    determinate: bool,
    circular: bool,
    done: bool,
}

/// How far along something is, as a fraction. Compose's `LinearProgressIndicator(progress)`.
pub fn ProgressIndicator(value: f32) -> ProgressIndicatorScope {
    ProgressIndicatorScope {
        modifier: Modifier::new(),
        value,
        determinate: true,
        circular: false,
        done: false,
    }
}

/// Work in progress of no known length.
pub fn IndeterminateProgressIndicator() -> ProgressIndicatorScope {
    ProgressIndicator(0.0).determinate(false)
}

impl ProgressIndicatorScope {
    pub fn determinate(mut self, determinate: bool) -> Self {
        self.determinate = determinate;
        self
    }

    /// The circular form rather than the linear one.
    pub fn circular(mut self, circular: bool) -> Self {
        self.circular = circular;
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let (value, determinate, circular) = (self.value, self.determinate, self.circular);
        emit(
            widget_key("compose_rust::ui::ProgressIndicator"),
            WidgetKind::ProgressIndicator,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("value", OwnedAttr::Float(f64::from(value))));
                attrs.push(("determinate", OwnedAttr::Bool(determinate)));
                attrs.push(("circular", OwnedAttr::Bool(circular)));
            },
            Vec::new(),
            content,
        );
    }
}

composed_on_drop!(ProgressIndicatorScope);

/// A `Divider` and its builder.
pub struct DividerScope {
    modifier: Modifier,
    vertical: bool,
    done: bool,
}

/// A line between two things. Compose's `HorizontalDivider`; `vertical` makes it the other.
pub fn Divider() -> DividerScope {
    DividerScope {
        modifier: Modifier::new(),
        vertical: false,
        done: false,
    }
}

/// A hairline between two rows of a grouped list, in the design system's quiet edge or
/// the given colour. The same node as `Divider().modifier(Modifier.background(color))`.
pub fn Separator(color: Option<Paint>) -> DividerScope {
    Divider().modifier(Modifier::new().background_if(color))
}

impl DividerScope {
    pub fn vertical(mut self, vertical: bool) -> Self {
        self.vertical = vertical;
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let vertical = self.vertical;
        emit(
            widget_key("compose_rust::ui::Divider"),
            WidgetKind::Divider,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("vertical", OwnedAttr::Bool(vertical)));
            },
            Vec::new(),
            content,
        );
    }
}

composed_on_drop!(DividerScope);

// ----- pictures -------------------------------------------------------------------------

/// An `Image` or an `Icon` and its builder.
pub struct AssetScope {
    modifier: Modifier,
    widget: WidgetKind,
    asset_id: u32,
    color: Option<Paint>,
    content_scale: Option<ContentScale>,
    done: bool,
}

/// A registered asset drawn as a picture. Compose's `Image`.
pub fn Image(asset_id: u32) -> AssetScope {
    AssetScope {
        modifier: Modifier::new(),
        widget: WidgetKind::Image,
        asset_id,
        color: None,
        content_scale: None,
        done: false,
    }
}

/// A registered icon, tinted. Compose's `Icon`.
pub fn Icon(asset_id: u32) -> AssetScope {
    AssetScope {
        modifier: Modifier::new(),
        widget: WidgetKind::Icon,
        asset_id,
        color: None,
        content_scale: None,
        done: false,
    }
}

impl AssetScope {
    /// The tint. An icon only; an image ignores it, as the rsx widget has no such property.
    pub fn color(mut self, paint: Paint) -> Self {
        self.color = Some(paint);
        self
    }

    /// How the picture is laid into its box. An image only; an icon takes its design
    /// system's optical size instead.
    ///
    /// Not calling it draws `ContentScale::Fit`, the whole picture in its own proportions.
    /// `ContentScale::FillBounds` stretches it to the box, as CSS `object-fit: fill` does.
    pub fn content_scale(mut self, scale: ContentScale) -> Self {
        self.content_scale = Some(scale);
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let (widget, asset_id, color) = (self.widget, self.asset_id, self.color);
        let content_scale = self.content_scale;
        let key = if widget == WidgetKind::Icon {
            widget_key("compose_rust::ui::Icon")
        } else {
            widget_key("compose_rust::ui::Image")
        };
        emit(
            key,
            widget,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("asset", OwnedAttr::Int(i64::from(asset_id))));
                if widget == WidgetKind::Icon {
                    attrs.push(("color", paint_or_zero(color)));
                }
                // A picture that never chose is not sent a choice, so it reaches the
                // Renderer exactly as it did before there was one.
                if let (WidgetKind::Image, Some(scale)) = (widget, content_scale) {
                    attrs.push(("content_scale", OwnedAttr::Int(i64::from(u16::from(scale)))));
                }
            },
            Vec::new(),
            content,
        );
    }
}

composed_on_drop!(AssetScope);

/// A `Canvas` and its builder.
pub struct CanvasScope {
    modifier: Modifier,
    commands: DrawList,
    view_box: Option<(f32, f32)>,
    done: bool,
}

/// Pixels the widget vocabulary cannot produce. Compose's `Canvas`, with the drawing as a
/// value rather than a callback: drawing code cannot cross into the Renderer, and an equal
/// list costs no record.
pub fn Canvas(commands: DrawList) -> CanvasScope {
    let view_box = commands.view_box();
    CanvasScope {
        modifier: Modifier::new(),
        commands,
        view_box,
        done: false,
    }
}

impl CanvasScope {
    /// Scales the command list's coordinates from `(width, height)` to the `Canvas`'s
    /// measured width and height. Stroke widths stay in dp.
    pub fn view_box(mut self, width: f32, height: f32) -> Self {
        self.view_box = Some((width, height));
        self
    }

    /// Scales the command list's coordinates as `0.0..=1.0` fractions of the `Canvas`'s
    /// measured width and height. Stroke widths stay in dp.
    pub fn fractional(self) -> Self {
        self.view_box(1.0, 1.0)
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let commands = std::mem::take(&mut self.commands);
        let view_box = self.view_box;
        emit(
            widget_key("compose_rust::ui::Canvas"),
            WidgetKind::Canvas,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("commands", OwnedAttr::Draw(commands)));
                if let Some((width, height)) = view_box {
                    attrs.push(("min", OwnedAttr::Float(f64::from(width))));
                    attrs.push(("max", OwnedAttr::Float(f64::from(height))));
                }
            },
            Vec::new(),
            content,
        );
    }
}

composed_on_drop!(CanvasScope);

// ----- pickers ----------------------------------------------------------------------------

/// A `DatePicker` and its builder.
pub struct DatePickerScope {
    modifier: Modifier,
    value: i64,
    min: Option<i64>,
    max: Option<i64>,
    enabled: bool,
    on_change: Option<Box<dyn FnMut(i64)>>,
    done: bool,
}

/// A date, as whole days since 1970-01-01. How it is picked is the design system's.
pub fn DatePicker(value: i64) -> DatePickerScope {
    DatePickerScope {
        modifier: Modifier::new(),
        value,
        min: None,
        max: None,
        enabled: true,
        on_change: None,
        done: false,
    }
}

impl DatePickerScope {
    pub fn min(mut self, days: i64) -> Self {
        self.min = Some(days);
        self
    }

    pub fn max(mut self, days: i64) -> Self {
        self.max = Some(days);
        self
    }

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

    pub fn on_change(mut self, handler: impl FnMut(i64) + 'static) -> Self {
        self.on_change = Some(std::boxed::Box::new(handler));
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let (value, min, max, enabled) = (self.value, self.min, self.max, self.enabled);
        let handler = match self.on_change.take() {
            Some(mut handler) => EventCallback::Value(Rc::new(RefCell::new(move |value: f64| {
                handler(value as i64)
            }))),
            None => value_handler(None),
        };
        emit(
            widget_key("compose_rust::ui::DatePicker"),
            WidgetKind::DatePicker,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("value", OwnedAttr::Int(value)));
                attrs.push(("min", OwnedAttr::Int(min.unwrap_or(i64::MIN))));
                attrs.push(("max", OwnedAttr::Int(max.unwrap_or(i64::MAX))));
                attrs.push(("enabled", OwnedAttr::Bool(enabled)));
            },
            vec![("onchange", handler)],
            content,
        );
    }
}

composed_on_drop!(DatePickerScope);

const MINUTES_IN_A_DAY: u32 = 24 * 60;

/// A `TimePicker` and its builder.
pub struct TimePickerScope {
    modifier: Modifier,
    value: u32,
    min: Option<u32>,
    max: Option<u32>,
    enabled: bool,
    on_change: Option<Box<dyn FnMut(u32)>>,
    done: bool,
}

/// A time of day, as minutes since midnight.
pub fn TimePicker(value: u32) -> TimePickerScope {
    TimePickerScope {
        modifier: Modifier::new(),
        value,
        min: None,
        max: None,
        enabled: true,
        on_change: None,
        done: false,
    }
}

impl TimePickerScope {
    pub fn min(mut self, minutes: u32) -> Self {
        self.min = Some(minutes);
        self
    }

    pub fn max(mut self, minutes: u32) -> Self {
        self.max = Some(minutes);
        self
    }

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

    pub fn on_change(mut self, handler: impl FnMut(u32) + 'static) -> Self {
        self.on_change = Some(std::boxed::Box::new(handler));
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let (value, min, max, enabled) = (self.value, self.min, self.max, self.enabled);
        let handler = match self.on_change.take() {
            Some(mut handler) => EventCallback::Value(Rc::new(RefCell::new(move |value: f64| {
                handler((value as i64).clamp(0, i64::from(MINUTES_IN_A_DAY - 1)) as u32)
            }))),
            None => value_handler(None),
        };
        emit(
            widget_key("compose_rust::ui::TimePicker"),
            WidgetKind::TimePicker,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("value", OwnedAttr::Int(i64::from(value))));
                attrs.push(("min", OwnedAttr::Int(i64::from(min.unwrap_or(0)))));
                attrs.push((
                    "max",
                    OwnedAttr::Int(i64::from(max.unwrap_or(MINUTES_IN_A_DAY - 1))),
                ));
                attrs.push(("enabled", OwnedAttr::Bool(enabled)));
            },
            vec![("onchange", handler)],
            content,
        );
    }
}

composed_on_drop!(TimePickerScope);

/// A `Dropdown` and its builder.
pub struct DropdownScope {
    modifier: Modifier,
    selected_index: usize,
    enabled: bool,
    closed: bool,
    bare: bool,
    on_change: Option<Box<dyn FnMut(usize)>>,
    done: bool,
}

/// One choice out of a list; each child is one option.
pub fn Dropdown(selected_index: usize) -> DropdownScope {
    DropdownScope {
        modifier: Modifier::new(),
        selected_index,
        enabled: true,
        closed: false,
        bare: false,
        on_change: None,
        done: false,
    }
}

/// A compact closed-field selector that always rests as a single line and opens its
/// options in a popup below the field on every design system.
pub fn Select(selected_index: usize) -> DropdownScope {
    Dropdown(selected_index).closed(true)
}

impl DropdownScope {
    pub fn enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }

    /// Keeps the control as a closed single-line field that opens a popup menu on click,
    /// even on design systems whose default picker presentation is a wheel.
    pub fn closed(mut self, closed: bool) -> Self {
        self.closed = closed;
        self
    }

    pub fn on_change(mut self, handler: impl FnMut(usize) + 'static) -> Self {
        self.on_change = Some(std::boxed::Box::new(handler));
        self
    }

    /// Draws the closed field without the design system's frame: no fill, line, corner
    /// clip, inner padding, minimum height or focus line. For a select that sits inside a
    /// box which already draws its own frame. A `background` or `border` given on the node
    /// is still drawn.
    pub fn bare(mut self) -> Self {
        self.bare = true;
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let (selected_index, enabled, closed) = (self.selected_index, self.enabled, self.closed);
        let bare = self.bare;
        let handler = match self.on_change.take() {
            Some(mut handler) => EventCallback::Value(Rc::new(RefCell::new(move |value: f64| {
                handler((value as i64).max(0) as usize)
            }))),
            None => value_handler(None),
        };
        emit(
            widget_key("compose_rust::ui::Dropdown"),
            WidgetKind::Dropdown,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("selected_index", OwnedAttr::Int(selected_index as i64)));
                attrs.push(("enabled", OwnedAttr::Bool(enabled)));
                if closed {
                    attrs.push(("collapsible", OwnedAttr::Bool(true)));
                }
                if bare {
                    attrs.push(("bare", OwnedAttr::Bool(true)));
                }
            },
            vec![("onchange", handler)],
            content,
        );
    }
}

container!(DropdownScope);

/// A `MenuItem` and its builder.
pub struct MenuItemScope {
    modifier: Modifier,
    text: String,
    icon: Option<IconRole>,
    selected: bool,
    enabled: bool,
    color: Option<Paint>,
    on_click: Option<Box<dyn FnMut()>>,
    done: bool,
}

/// A single row in a `Menu` or action list: left-aligned label, optional leading icon,
/// and optional selected state.
pub fn MenuItem(text: impl Into<String>) -> MenuItemScope {
    MenuItemScope {
        modifier: Modifier::new(),
        text: text.into(),
        icon: None,
        selected: false,
        enabled: true,
        color: None,
        on_click: None,
        done: false,
    }
}

impl MenuItemScope {
    /// Leading semantic icon for this menu row.
    pub fn icon(mut self, icon: IconRole) -> Self {
        self.icon = Some(icon);
        self
    }

    /// Marks this row as the currently selected option.
    pub fn selected(mut self, selected: bool) -> Self {
        self.selected = selected;
        self
    }

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

    /// Overrides the row's label and icon colour (for example, a destructive action).
    pub fn color(mut self, paint: Paint) -> Self {
        self.color = Some(paint);
        self
    }

    /// Conditional colour override.
    pub fn color_if(mut self, paint: Option<Paint>) -> Self {
        self.color = paint;
        self
    }

    pub fn on_click(mut self, handler: impl FnMut() + 'static) -> Self {
        self.on_click = Some(std::boxed::Box::new(handler));
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let text = std::mem::take(&mut self.text);
        let (icon, selected, enabled, color) = (self.icon, self.selected, self.enabled, self.color);
        let listeners = vec![("onclick", unit(self.on_click.take()))];
        emit(
            widget_key("compose_rust::ui::MenuItem"),
            WidgetKind::Button,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("text", OwnedAttr::Text(text)));
                attrs.push(("icon", super::role(icon)));
                attrs.push(("enabled", OwnedAttr::Bool(enabled)));
                attrs.push(("variant", role_or_zero(Some(ButtonVariant::Text))));
                attrs.push(("text_align", role_or_zero(Some(TextAlign::Start))));
                attrs.push(("checked", OwnedAttr::Bool(selected)));
                attrs.push(("color", super::paint(color)));
            },
            listeners,
            content,
        );
    }
}

composed_on_drop!(MenuItemScope);

// ----- surfaces that come and go ----------------------------------------------------------

/// A `Dialog` or a `Sheet` and its builder: `open` seeds the Renderer's own open state,
/// and `on_dismiss` says once that the user asked to close it.
pub struct ModalScope {
    modifier: Modifier,
    widget: WidgetKind,
    open: bool,
    scrim: Option<Paint>,
    max_height_fraction: Option<f32>,
    on_dismiss: Option<Box<dyn FnMut()>>,
    done: bool,
}

/// A modal. Compose's `Dialog`, kept in the tree and opened by `open`, so its enter and
/// exit belong to the Renderer.
pub fn Dialog(open: bool) -> ModalScope {
    ModalScope {
        modifier: Modifier::new(),
        widget: WidgetKind::Dialog,
        open,
        scrim: None,
        max_height_fraction: None,
        on_dismiss: None,
        done: false,
    }
}

/// A surface that slides in from the edge the Renderer chooses. Compose's
/// `ModalBottomSheet`, without a promise about which edge.
pub fn Sheet(open: bool) -> ModalScope {
    ModalScope {
        modifier: Modifier::new(),
        widget: WidgetKind::Sheet,
        open,
        scrim: None,
        max_height_fraction: None,
        on_dismiss: None,
        done: false,
    }
}

impl ModalScope {
    pub fn on_dismiss(mut self, handler: impl FnMut() + 'static) -> Self {
        self.on_dismiss = Some(std::boxed::Box::new(handler));
        self
    }

    /// Sets the modal scrim colour, overriding the design system default.
    pub fn scrim(mut self, paint: impl Into<Paint>) -> Self {
        self.scrim = Some(paint.into());
        self
    }

    /// The tallest the sheet grows, as a fraction of the window's height, above 0 and at
    /// most 1.
    ///
    /// Below it the sheet is as tall as its content; content taller than that stops the
    /// sheet at the fraction and scrolls inside it. A fraction above 1 is taken as 1, and
    /// one that is not above 0 is not sent, which leaves the height to the design system
    /// as it is without this call. A `Dialog` sizes itself to its content and does not
    /// take it.
    pub fn max_height_fraction(mut self, fraction: f32) -> Self {
        self.max_height_fraction = (fraction > 0.0).then(|| fraction.min(1.0));
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let (widget, open) = (self.widget, self.open);
        let max_height_fraction = self
            .max_height_fraction
            .filter(|_| widget == WidgetKind::Sheet);
        let key = if widget == WidgetKind::Dialog {
            widget_key("compose_rust::ui::Dialog")
        } else {
            widget_key("compose_rust::ui::Sheet")
        };
        let scrim = self.scrim;
        emit(
            key,
            widget,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("open", OwnedAttr::Bool(open)));
                if let Some(scrim) = scrim {
                    attrs.push(("scrim", super::paint(Some(scrim))));
                }
                if let Some(fraction) = max_height_fraction {
                    attrs.push(("max_height_fraction", OwnedAttr::Float(f64::from(fraction))));
                }
            },
            vec![("ondismiss", unit(self.on_dismiss.take()))],
            content,
        );
    }
}

container!(ModalScope);

/// A `Menu` and its builder.
pub struct MenuScope<'a> {
    modifier: Modifier,
    expanded: bool,
    scrim: Option<Paint>,
    on_dismiss: Option<Box<dyn FnMut()>>,
    anchor: Option<Box<dyn FnOnce() + 'a>>,
    done: bool,
}

/// A popup hanging off its anchor. Compose's `DropdownMenu`, with the anchor said rather
/// than positioned: the Renderer places the popup.
pub fn Menu<'a>(expanded: bool) -> MenuScope<'a> {
    MenuScope {
        modifier: Modifier::new(),
        expanded,
        scrim: None,
        on_dismiss: None,
        anchor: None,
        done: false,
    }
}

impl<'a> MenuScope<'a> {
    pub fn on_dismiss(mut self, handler: impl FnMut() + 'static) -> Self {
        self.on_dismiss = Some(std::boxed::Box::new(handler));
        self
    }

    /// Sets the popup scrim colour, overriding the design system default.
    pub fn scrim(mut self, paint: impl Into<Paint>) -> Self {
        self.scrim = Some(paint.into());
        self
    }

    /// The widget the menu hangs off. It is the first child on the wire.
    pub fn anchor(mut self, anchor: impl FnOnce() + 'a) -> Self {
        self.anchor = Some(std::boxed::Box::new(anchor));
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let expanded = self.expanded;
        let anchor = self.anchor.take();
        let scrim = self.scrim;
        emit(
            widget_key("compose_rust::ui::Menu"),
            WidgetKind::Menu,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("open", OwnedAttr::Bool(expanded)));
                if let Some(scrim) = scrim {
                    attrs.push(("scrim", super::paint(Some(scrim))));
                }
            },
            vec![("ondismiss", unit(self.on_dismiss.take()))],
            || {
                if let Some(anchor) = anchor {
                    let _anchor =
                        crate::runtime::group_guard(widget_key("compose_rust::ui::Menu::anchor"));
                    anchor();
                }
                let _entries =
                    crate::runtime::group_guard(widget_key("compose_rust::ui::Menu::entries"));
                content();
            },
        );
    }
}

container!(MenuScope<'a>);

/// A `Popover` and its builder.
pub struct PopoverScope<'a> {
    modifier: Modifier,
    open: bool,
    placement: Option<PopoverPlacement>,
    align: Option<PopoverAlign>,
    offset: Option<f32>,
    scrim: bool,
    scrim_color: Option<Paint>,
    on_dismiss: Option<Box<dyn FnMut()>>,
    anchor: Option<Box<dyn FnOnce() + 'a>>,
    done: bool,
}

/// A floating box of any content attached to its anchor.
///
/// The anchor is always drawn. The content is drawn only while `open` is true, and `open`
/// is the application's: pressing outside the box or pressing Escape calls `on_dismiss`
/// once and changes nothing, so the handler is where the application closes it or keeps
/// it open to ask about unsaved work.
///
/// The Renderer places the box. It opens on the side `placement` names, lined up with the
/// edge `align` names, and it moves to the opposite side or slides along the edge when it
/// would otherwise leave the window.
///
/// `modifier` belongs to the floating box, not to the anchor: a shape, a border, a shadow
/// or a material given there replaces the design system's for this box.
pub fn Popover<'a>(open: bool) -> PopoverScope<'a> {
    PopoverScope {
        modifier: Modifier::new(),
        open,
        placement: None,
        align: None,
        offset: None,
        scrim: false,
        scrim_color: None,
        on_dismiss: None,
        anchor: None,
        done: false,
    }
}

impl<'a> PopoverScope<'a> {
    /// Called once when the user presses outside the box or presses Escape.
    pub fn on_dismiss(mut self, handler: impl FnMut() + 'static) -> Self {
        self.on_dismiss = Some(std::boxed::Box::new(handler));
        self
    }

    /// The side of the anchor the box opens on. `Bottom` when it is not said.
    pub fn placement(mut self, placement: PopoverPlacement) -> Self {
        self.placement = Some(placement);
        self
    }

    /// The edge of the anchor the box lines up with. `Start` when it is not said.
    pub fn align(mut self, align: PopoverAlign) -> Self {
        self.align = Some(align);
        self
    }

    /// The gap between the anchor and the box, in dp. The design system's gap when it is
    /// not said.
    pub fn offset(mut self, dp: f32) -> Self {
        self.offset = Some(dp);
        self
    }

    /// Whether the window behind the box is dimmed while it is open. It is not, unless
    /// asked.
    pub fn scrim(mut self, scrim: bool) -> Self {
        self.scrim = scrim;
        self
    }

    /// Dims the window behind the box with this colour instead of the design system's.
    pub fn scrim_color(mut self, paint: impl Into<Paint>) -> Self {
        self.scrim_color = Some(paint.into());
        self
    }

    /// The widget the box is attached to. It is the first child on the wire.
    pub fn anchor(mut self, anchor: impl FnOnce() + 'a) -> Self {
        self.anchor = Some(std::boxed::Box::new(anchor));
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let open = self.open;
        let (placement, align, offset) = (self.placement, self.align, self.offset);
        let (scrim, scrim_color) = (self.scrim, self.scrim_color);
        let anchor = self.anchor.take();
        emit(
            widget_key("compose_rust::ui::Popover"),
            WidgetKind::Popover,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("open", OwnedAttr::Bool(open)));
                if let Some(placement) = placement {
                    attrs.push(("placement", role_or_zero(Some(placement))));
                }
                if let Some(align) = align {
                    attrs.push(("align", role_or_zero(Some(align))));
                }
                if let Some(offset) = offset {
                    attrs.push(("offset_dp", OwnedAttr::Float(f64::from(offset))));
                }
                if let Some(color) = scrim_color {
                    attrs.push(("scrim", super::paint(Some(color))));
                } else if scrim {
                    attrs.push(("scrim", OwnedAttr::Bool(true)));
                }
            },
            vec![("ondismiss", unit(self.on_dismiss.take()))],
            || {
                if let Some(anchor) = anchor {
                    let _anchor = crate::runtime::group_guard(widget_key(
                        "compose_rust::ui::Popover::anchor",
                    ));
                    anchor();
                }
                let _content =
                    crate::runtime::group_guard(widget_key("compose_rust::ui::Popover::content"));
                content();
            },
        );
    }
}

container!(PopoverScope<'a>);

/// A `Tabs` and its builder.
pub struct TabsScope {
    modifier: Modifier,
    color: Option<Paint>,
    selected_index: usize,
    done: bool,
}

/// A row of tabs; each child is one tab, and a tap is that child's own click. Compose's
/// `TabRow`.
pub fn Tabs(selected_index: usize) -> TabsScope {
    TabsScope {
        modifier: Modifier::new(),
        color: None,
        selected_index,
        done: false,
    }
}

impl TabsScope {
    /// The mark that shows which tab is selected.
    pub fn color(mut self, paint: Paint) -> Self {
        self.color = Some(paint);
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let (color, selected_index) = (self.color, self.selected_index);
        emit(
            widget_key("compose_rust::ui::Tabs"),
            WidgetKind::Tabs,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("color", super::paint(color)));
                attrs.push(("selected_index", OwnedAttr::Int(selected_index as i64)));
            },
            Vec::new(),
            content,
        );
    }
}

container!(TabsScope);

/// A `TopAppBar` and its builder.
pub struct TopAppBarScope {
    modifier: Modifier,
    title: String,
    done: bool,
}

/// The bar across the top of a screen; its children are its content. Compose's
/// `TopAppBar`, with the title a property so the Renderer can find it.
pub fn TopAppBar() -> TopAppBarScope {
    TopAppBarScope {
        modifier: Modifier::new(),
        title: String::new(),
        done: false,
    }
}

impl TopAppBarScope {
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = title.into();
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let title = std::mem::take(&mut self.title);
        emit(
            widget_key("compose_rust::ui::TopAppBar"),
            WidgetKind::TopAppBar,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("text", OwnedAttr::Text(title)));
            },
            Vec::new(),
            content,
        );
    }
}

container!(TopAppBarScope);

/// A `Tooltip` and its builder.
pub struct TooltipScope {
    modifier: Modifier,
    text: String,
    done: bool,
}

/// An explanation attached to its child, which assistive technology reads too. Compose's
/// `TooltipBox`.
pub fn Tooltip(text: impl Into<String>) -> TooltipScope {
    TooltipScope {
        modifier: Modifier::new(),
        text: text.into(),
        done: false,
    }
}

impl TooltipScope {
    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let text = std::mem::take(&mut self.text);
        emit(
            widget_key("compose_rust::ui::Tooltip"),
            WidgetKind::Tooltip,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("text", OwnedAttr::Text(text)));
            },
            Vec::new(),
            content,
        );
    }
}

container!(TooltipScope);

// ----- frames and navigation --------------------------------------------------------------

/// Composes one slot of a frame: a ScaffoldSlot node holding `content`, in a group of its
/// own so that a slot coming and going leaves the others where they were.
fn slot(role: SlotRole, key: u64, content: impl FnOnce()) {
    emit(
        key,
        WidgetKind::ScaffoldSlot,
        |attrs| attrs.push(("slot", OwnedAttr::Int(i64::from(u16::from(role))))),
        Vec::new(),
        content,
    );
}

type Slot<'a> = Option<Box<dyn FnOnce() + 'a>>;

/// A `Scaffold` and its builder.
pub struct ScaffoldScope<'a> {
    modifier: Modifier,
    top_bar: Slot<'a>,
    bottom_bar: Slot<'a>,
    floating_action: Slot<'a>,
    done: bool,
}

/// The screen's frame: which slots it has filled, and the page. Compose's `Scaffold`.
/// What each slot becomes on this platform at this width is the Renderer's decision.
///
/// The Modifier carries the two things a frame takes: `background` and `material`.
pub fn Scaffold<'a>() -> ScaffoldScope<'a> {
    ScaffoldScope {
        modifier: Modifier::new(),
        top_bar: None,
        bottom_bar: None,
        floating_action: None,
        done: false,
    }
}

impl<'a> ScaffoldScope<'a> {
    pub fn top_bar(mut self, bar: impl FnOnce() + 'a) -> Self {
        self.top_bar = Some(std::boxed::Box::new(bar));
        self
    }

    pub fn bottom_bar(mut self, bar: impl FnOnce() + 'a) -> Self {
        self.bottom_bar = Some(std::boxed::Box::new(bar));
        self
    }

    pub fn floating_action(mut self, action: impl FnOnce() + 'a) -> Self {
        self.floating_action = Some(std::boxed::Box::new(action));
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let (top_bar, bottom_bar, floating_action) = (
            self.top_bar.take(),
            self.bottom_bar.take(),
            self.floating_action.take(),
        );
        emit(
            widget_key("compose_rust::ui::Scaffold"),
            WidgetKind::Scaffold,
            |attrs| push_modifiers(&modifier, Mods::Scaffold, attrs),
            Vec::new(),
            || {
                if let Some(bar) = top_bar {
                    slot(
                        SlotRole::TopBar,
                        widget_key("compose_rust::ui::Scaffold::top_bar"),
                        bar,
                    );
                }
                if let Some(bar) = bottom_bar {
                    slot(
                        SlotRole::BottomBar,
                        widget_key("compose_rust::ui::Scaffold::bottom_bar"),
                        bar,
                    );
                }
                if let Some(action) = floating_action {
                    slot(
                        SlotRole::FloatingAction,
                        widget_key("compose_rust::ui::Scaffold::floating_action"),
                        action,
                    );
                }
                slot(
                    SlotRole::Content,
                    widget_key("compose_rust::ui::Scaffold::content"),
                    content,
                );
            },
        );
    }
}

container!(ScaffoldScope<'a>);

/// A `Navigation` and its builder.
pub struct NavigationScope<'a> {
    modifier: Modifier,
    selected_index: usize,
    compact: CompactNavigation,
    head: Slot<'a>,
    foot: Slot<'a>,
    done: bool,
}

/// A set of destinations and the screen they lead to. The children that are
/// `NavigationItem`s are the destinations; whether they are a bar, a rail or a drawer is
/// the Renderer's choice. Compose's `NavigationSuiteScaffold`.
pub fn Navigation<'a>(selected_index: usize) -> NavigationScope<'a> {
    NavigationScope {
        modifier: Modifier::new(),
        selected_index,
        compact: CompactNavigation::Default,
        head: None,
        foot: None,
        done: false,
    }
}

impl<'a> NavigationScope<'a> {
    /// What the destinations become in a compact window.
    ///
    /// Without it the design system answers, which is a bottom bar on most of them. An
    /// application whose own reference draws a compact window differently says so here:
    /// `Sidebar` puts the sidebar away behind a toggle on every design system, and `None`
    /// leaves the destinations off a compact window so the screen can reach them its own
    /// way. Medium and Expanded windows keep the design system's rail and drawer whatever
    /// this says, and the screen still never asks how wide the window is.
    pub fn compact(mut self, compact: CompactNavigation) -> Self {
        self.compact = compact;
        self
    }

    /// What sits above the destinations in a strip.
    pub fn head(mut self, head: impl FnOnce() + 'a) -> Self {
        self.head = Some(std::boxed::Box::new(head));
        self
    }

    /// What sits below them.
    pub fn foot(mut self, foot: impl FnOnce() + 'a) -> Self {
        self.foot = Some(std::boxed::Box::new(foot));
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let selected_index = self.selected_index;
        let compact = self.compact;
        let (head, foot) = (self.head.take(), self.foot.take());
        emit(
            widget_key("compose_rust::ui::Navigation"),
            WidgetKind::Navigation,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("selected_index", OwnedAttr::Int(selected_index as i64)));
                // No choice is the default, and the default is not sent: a navigation that
                // says nothing travels exactly as it did before there was a choice.
                if compact != CompactNavigation::Default {
                    attrs.push((
                        "compact_navigation",
                        OwnedAttr::Int(i64::from(u16::from(compact))),
                    ));
                }
            },
            Vec::new(),
            || {
                if let Some(head) = head {
                    slot(
                        SlotRole::TopBar,
                        widget_key("compose_rust::ui::Navigation::head"),
                        head,
                    );
                }
                if let Some(foot) = foot {
                    slot(
                        SlotRole::BottomBar,
                        widget_key("compose_rust::ui::Navigation::foot"),
                        foot,
                    );
                }
                let _destinations = crate::runtime::group_guard(widget_key(
                    "compose_rust::ui::Navigation::content",
                ));
                content();
            },
        );
    }
}

container!(NavigationScope<'a>);

/// A `NavigationItem` and its builder.
pub struct NavigationItemScope {
    modifier: Modifier,
    text: String,
    icon: Option<IconRole>,
    color: Option<Paint>,
    enabled: bool,
    section: Option<String>,
    on_click: Option<Box<dyn FnMut()>>,
    done: bool,
}

/// One destination inside a `Navigation`. Compose's `NavigationSuiteItem`.
pub fn NavigationItem(text: impl Into<String>) -> NavigationItemScope {
    NavigationItemScope {
        modifier: Modifier::new(),
        text: text.into(),
        icon: None,
        color: None,
        enabled: true,
        section: None,
        on_click: None,
        done: false,
    }
}

impl NavigationItemScope {
    pub fn icon(mut self, icon: IconRole) -> Self {
        self.icon = Some(icon);
        self
    }

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

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

    /// The named group of the strip this destination belongs to.
    pub fn section(mut self, section: impl Into<String>) -> Self {
        self.section = Some(section.into());
        self
    }

    pub fn on_click(mut self, handler: impl FnMut() + 'static) -> Self {
        self.on_click = Some(std::boxed::Box::new(handler));
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let text = std::mem::take(&mut self.text);
        let section = self.section.take();
        let (icon, color, enabled) = (self.icon, self.color, self.enabled);
        emit(
            widget_key("compose_rust::ui::NavigationItem"),
            WidgetKind::NavigationItem,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("text", OwnedAttr::Text(text)));
                attrs.push(("icon", super::role(icon)));
                attrs.push(("color", super::paint(color)));
                attrs.push(("enabled", OwnedAttr::Bool(enabled)));
                attrs.push(("section", optional_text(section)));
            },
            vec![("onclick", unit(self.on_click.take()))],
            content,
        );
    }
}

composed_on_drop!(NavigationItemScope);

// ----- small things ---------------------------------------------------------------------

/// A `FileDropTarget` and its builder.
pub struct FileDropTargetScope {
    modifier: Modifier,
    alignment: Option<Alignment>,
    on_files_entered: Option<Box<dyn FnMut()>>,
    on_files_dropped: Option<Box<dyn FnMut(FileDrop)>>,
    done: bool,
}

/// A place files may be dropped. Desktop only in effect.
pub fn FileDropTarget() -> FileDropTargetScope {
    FileDropTargetScope {
        modifier: Modifier::new(),
        alignment: None,
        on_files_entered: None,
        on_files_dropped: None,
        done: false,
    }
}

impl FileDropTargetScope {
    pub fn alignment(mut self, alignment: Alignment) -> Self {
        self.alignment = Some(alignment);
        self
    }

    pub fn on_files_entered(mut self, handler: impl FnMut() + 'static) -> Self {
        self.on_files_entered = Some(std::boxed::Box::new(handler));
        self
    }

    pub fn on_files_dropped(mut self, handler: impl FnMut(FileDrop) + 'static) -> Self {
        self.on_files_dropped = Some(std::boxed::Box::new(handler));
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let alignment = self.alignment;
        let dropped: EventCallback = match self.on_files_dropped.take() {
            Some(handler) => EventCallback::Files(Rc::new(RefCell::new(handler))),
            None => EventCallback::Files(Rc::new(RefCell::new(|_: FileDrop| {}))),
        };
        emit(
            widget_key("compose_rust::ui::FileDropTarget"),
            WidgetKind::FileDropTarget,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("alignment", super::role(alignment)));
            },
            vec![
                ("onfilesentered", unit(self.on_files_entered.take())),
                ("onfilesdropped", dropped),
            ],
            content,
        );
    }
}

container!(FileDropTargetScope);

/// A `Chip` and its builder.
pub struct ChipScope {
    modifier: Modifier,
    text: String,
    icon: Option<IconRole>,
    selected: bool,
    enabled: bool,
    max_lines: Option<u32>,
    overflow: Option<TextOverflow>,
    on_click: Option<Box<dyn FnMut()>>,
    done: bool,
}

/// A small token that is chosen or filters. Compose's `FilterChip`.
pub fn Chip(text: impl Into<String>) -> ChipScope {
    ChipScope {
        modifier: Modifier::new(),
        text: text.into(),
        icon: None,
        selected: false,
        enabled: true,
        max_lines: None,
        overflow: None,
        on_click: None,
        done: false,
    }
}

impl ChipScope {
    pub fn icon(mut self, icon: IconRole) -> Self {
        self.icon = Some(icon);
        self
    }

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

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

    /// How many lines the label may take. A label is one line unless this says more, so a
    /// chip label is one line unless this says more.
    pub fn max_lines(mut self, lines: u32) -> Self {
        self.max_lines = Some(lines);
        self
    }

    /// What happens to a label longer than its lines.
    pub fn overflow(mut self, overflow: TextOverflow) -> Self {
        self.overflow = Some(overflow);
        self
    }

    pub fn on_click(mut self, handler: impl FnMut() + 'static) -> Self {
        self.on_click = Some(std::boxed::Box::new(handler));
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let text = std::mem::take(&mut self.text);
        let (max_lines, overflow) = (self.max_lines, self.overflow);
        let (icon, selected, enabled) = (self.icon, self.selected, self.enabled);
        emit(
            widget_key("compose_rust::ui::Chip"),
            WidgetKind::Chip,
            |attrs| {
                push_modifiers(&modifier, Mods::Chip, attrs);
                attrs.push(("text", OwnedAttr::Text(text)));
                attrs.push(("icon", super::role(icon)));
                attrs.push(("checked", OwnedAttr::Bool(selected)));
                attrs.push(("enabled", OwnedAttr::Bool(enabled)));
                lines_and_overflow(max_lines, overflow, attrs);
            },
            vec![("onclick", unit(self.on_click.take()))],
            content,
        );
    }
}

composed_on_drop!(ChipScope);

/// A `FloatingAction` and its builder.
pub struct FloatingActionScope {
    modifier: Modifier,
    text: String,
    icon: Option<IconRole>,
    on_click: Option<Box<dyn FnMut()>>,
    done: bool,
}

/// The one action a screen is about. Compose's `ExtendedFloatingActionButton`; where it
/// goes is the design system's answer.
pub fn FloatingAction(text: impl Into<String>) -> FloatingActionScope {
    FloatingActionScope {
        modifier: Modifier::new(),
        text: text.into(),
        icon: None,
        on_click: None,
        done: false,
    }
}

impl FloatingActionScope {
    pub fn icon(mut self, icon: IconRole) -> Self {
        self.icon = Some(icon);
        self
    }

    pub fn on_click(mut self, handler: impl FnMut() + 'static) -> Self {
        self.on_click = Some(std::boxed::Box::new(handler));
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let text = std::mem::take(&mut self.text);
        let icon = self.icon;
        emit(
            widget_key("compose_rust::ui::FloatingAction"),
            WidgetKind::FloatingAction,
            |attrs| {
                push_modifiers(&modifier, Mods::None, attrs);
                attrs.push(("text", OwnedAttr::Text(text)));
                attrs.push(("icon", super::role(icon)));
            },
            vec![("onclick", unit(self.on_click.take()))],
            content,
        );
    }
}

composed_on_drop!(FloatingActionScope);

/// A `Badge` and its builder.
pub struct BadgeScope {
    modifier: Modifier,
    value: Option<u32>,
    text: Option<String>,
    color: Option<ColorRole>,
    done: bool,
}

/// A count, a word or a dot, on its child or on its own. Compose's `BadgedBox` and `Badge`.
pub fn Badge() -> BadgeScope {
    BadgeScope {
        modifier: Modifier::new(),
        value: None,
        text: None,
        color: None,
        done: false,
    }
}

impl BadgeScope {
    /// How many.
    pub fn value(mut self, value: u32) -> Self {
        self.value = Some(value);
        self
    }

    /// One short word, shown where a count would be.
    pub fn text(mut self, text: impl Into<String>) -> Self {
        self.text = Some(text.into());
        self
    }

    /// The role the mark is filled with; the error role where it is not given.
    pub fn color(mut self, role: ColorRole) -> Self {
        self.color = Some(role);
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let text = self.text.take();
        let (value, color) = (self.value, self.color);
        emit(
            widget_key("compose_rust::ui::Badge"),
            WidgetKind::Badge,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push((
                    "count",
                    value.map_or(OwnedAttr::None, |value| OwnedAttr::Int(i64::from(value))),
                ));
                attrs.push(("text", optional_text(text)));
                attrs.push(("color", super::paint(color.map(Paint::Role))));
            },
            Vec::new(),
            content,
        );
    }
}

container!(BadgeScope);

/// A `SplitPane` and its builder.
pub struct SplitPaneScope {
    modifier: Modifier,
    value: Option<f32>,
    min: Option<f32>,
    max: Option<f32>,
    collapsible: bool,
    collapse_below: Option<f32>,
    selected_index: usize,
    label: Option<String>,
    on_change: Option<Box<dyn FnMut(f32)>>,
    on_dismiss: Option<Box<dyn FnMut()>>,
    done: bool,
}

/// A side pane and a body with a divider the user drags. Exactly two children, side pane
/// first. The drag is reported once, when it ends.
pub fn SplitPane() -> SplitPaneScope {
    SplitPaneScope {
        modifier: Modifier::new(),
        value: None,
        min: None,
        max: None,
        collapsible: false,
        collapse_below: None,
        selected_index: 0,
        label: None,
        on_change: None,
        on_dismiss: None,
        done: false,
    }
}

impl SplitPaneScope {
    /// The side pane's width in dp, or 0 to open it folded.
    pub fn value(mut self, dp: f32) -> Self {
        self.value = Some(dp);
        self
    }

    pub fn min(mut self, dp: f32) -> Self {
        self.min = Some(dp);
        self
    }

    pub fn max(mut self, dp: f32) -> Self {
        self.max = Some(dp);
        self
    }

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

    /// The split pane's own width in dp below which it shows one pane at a time, the one
    /// `selected_index` names. At or above it the panes are never stacked, and the design
    /// system still decides between two columns and a side pane laid over the body.
    ///
    /// Without it the line is the size class's: stacked below 600 dp. 0 is the same as not
    /// calling this. The width is measured on the Renderer, so crossing the line sends the
    /// Host nothing. A negative or non-finite width is reported as a protocol error and
    /// the split pane is drawn as if it had none.
    pub fn collapse_below(mut self, dp: f32) -> Self {
        self.collapse_below = Some(dp);
        self
    }

    /// Which pane shows when only one fits: 0 the side pane, 1 the body.
    pub fn selected_index(mut self, index: usize) -> Self {
        self.selected_index = index;
        self
    }

    /// What a screen reader calls the divider.
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    pub fn on_change(mut self, handler: impl FnMut(f32) + 'static) -> Self {
        self.on_change = Some(std::boxed::Box::new(handler));
        self
    }

    pub fn on_dismiss(mut self, handler: impl FnMut() + 'static) -> Self {
        self.on_dismiss = Some(std::boxed::Box::new(handler));
        self
    }

    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let label = self.label.take();
        let (value, min, max, collapsible, collapse_below, selected_index) = (
            self.value,
            self.min,
            self.max,
            self.collapsible,
            self.collapse_below,
            self.selected_index,
        );
        let change = match self.on_change.take() {
            Some(mut handler) => EventCallback::Value(Rc::new(RefCell::new(move |value: f64| {
                handler(value as f32)
            }))),
            None => value_handler(None),
        };
        let as_float = |value: Option<f32>| {
            value.map_or(OwnedAttr::None, |value| OwnedAttr::Float(f64::from(value)))
        };
        emit(
            widget_key("compose_rust::ui::SplitPane"),
            WidgetKind::SplitPane,
            |attrs| {
                push_modifiers(&modifier, Mods::Standard, attrs);
                attrs.push(("value", as_float(value)));
                attrs.push(("min", as_float(min)));
                attrs.push(("max", as_float(max)));
                attrs.push(("collapsible", OwnedAttr::Bool(collapsible)));
                attrs.push(("collapse_below", as_float(collapse_below)));
                attrs.push(("selected_index", OwnedAttr::Int(selected_index as i64)));
                attrs.push(("text", optional_text(label)));
            },
            vec![
                ("onchange", change),
                ("ondismiss", unit(self.on_dismiss.take())),
            ],
            content,
        );
    }
}

container!(SplitPaneScope);

/// The worked extension widget: Material 3's determinate linear progress indicator.
pub struct LinearProgressIndicatorScope {
    modifier: Modifier,
    progress: f32,
    done: bool,
}

/// The extension widget the schema carries as its example.
pub fn LinearProgressIndicator(progress: f32) -> LinearProgressIndicatorScope {
    LinearProgressIndicatorScope {
        modifier: Modifier::new(),
        progress,
        done: false,
    }
}

impl LinearProgressIndicatorScope {
    fn compose(&mut self, content: impl FnOnce()) {
        let modifier = self.modifier;
        let progress = self.progress;
        emit(
            widget_key("compose_rust::ui::LinearProgressIndicator"),
            WidgetKind::LinearProgressIndicator,
            |attrs| {
                push_modifiers(&modifier, Mods::None, attrs);
                attrs.push(("progress", OwnedAttr::Float(f64::from(progress))));
            },
            Vec::new(),
            content,
        );
    }
}

composed_on_drop!(LinearProgressIndicatorScope);

// ----- windows --------------------------------------------------------------------

/// A `Window` and its builder.
pub struct WindowScope {
    on_close_request: Option<Box<dyn FnMut()>>,
    title: Option<String>,
    size: Option<(f32, f32)>,
    min_size: Option<(f32, f32)>,
    resizable: Option<bool>,
    report_size: Option<bool>,
    chrome: Option<crate::schema::Chrome>,
    title_bar: Option<crate::schema::TitleBar>,
    icon: Option<u32>,
    position: Option<(f32, f32)>,
    restore_key: Option<String>,
    zoom_level: Option<i8>,
    drag_from_pointer: Option<crate::windows::DragFromPointer>,
    theme: Option<crate::schema::Theme>,
    on_focus_changed: Option<Box<dyn FnMut(bool)>>,
    on_moved: Option<Box<dyn FnMut(crate::windows::WindowFrame)>>,
    on_drag: Option<Box<dyn FnMut(crate::windows::WindowDragEvent)>>,
    on_unavailable: Option<Box<dyn FnMut()>>,
    on_closed: Option<Box<dyn FnMut()>>,
    done: bool,
}

/// A platform window, a child of the root, whose children are the window's content. Compose
/// Desktop's `Window`. The application has to be launched with
/// [`LaunchBuilder::windows`](crate::LaunchBuilder::windows).
///
/// `on_close_request` is called when the user asks to close the window. It does not close
/// it: the window closes when the application stops composing this `Window`, which is
/// where a dialog that asks about unsaved work goes.
///
/// ```ignore
/// Window(move || open.set(false)).title("Notes").size(900.0, 600.0).content(|| {
///     Text("Hello");
/// });
/// ```
pub fn Window(on_close_request: impl FnMut() + 'static) -> WindowScope {
    WindowScope {
        on_close_request: Some(std::boxed::Box::new(on_close_request)),
        title: None,
        size: None,
        min_size: None,
        resizable: None,
        report_size: None,
        chrome: None,
        title_bar: None,
        icon: None,
        position: None,
        restore_key: None,
        zoom_level: None,
        drag_from_pointer: None,
        theme: None,
        on_focus_changed: None,
        on_moved: None,
        on_drag: None,
        on_unavailable: None,
        on_closed: None,
        done: false,
    }
}

impl Drop for WindowScope {
    fn drop(&mut self) {
        // A panic is already unwinding the composition, and composing on the way out would
        // only risk a second one.
        if !self.done && !std::thread::panicking() {
            self.done = true;
            self.compose(|| {});
        }
    }
}

/// A window handler as the callback the runtime delivers events to.
fn window_callback(handler: impl FnMut(crate::windows::WindowEvent) + 'static) -> EventCallback {
    EventCallback::Window(Rc::new(RefCell::new(handler)))
}

impl WindowScope {
    /// What the window calls itself.
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// The size the window opens at, in dp. A later change resizes it.
    pub fn size(mut self, width: f32, height: f32) -> Self {
        self.size = Some((width, height));
        self
    }

    /// The smallest size the user can make it, in dp.
    pub fn min_size(mut self, width: f32, height: f32) -> Self {
        self.min_size = Some((width, height));
        self
    }

    pub fn resizable(mut self, resizable: bool) -> Self {
        self.resizable = Some(resizable);
        self
    }

    /// Asks to be told every time the window's width or height, rounded to whole dp,
    /// changes. Without it the size is reported only when its class or the safe area
    /// changes. Turn it on only for a window that lays out by its exact size: each
    /// whole-dp change while the user drags an edge costs one call into the Host.
    pub fn report_size(mut self, report: bool) -> Self {
        self.report_size = Some(report);
        self
    }

    /// Who draws the caption.
    pub fn chrome(mut self, chrome: crate::schema::Chrome) -> Self {
        self.chrome = Some(chrome);
        self
    }

    /// Which of the two title bars the window has.
    pub fn title_bar(mut self, title_bar: crate::schema::TitleBar) -> Self {
        self.title_bar = Some(title_bar);
        self
    }

    /// The window's icon, as the id of a registered asset. Without one it wears the
    /// application's.
    pub fn icon(mut self, asset_id: u32) -> Self {
        self.icon = Some(asset_id);
        self
    }

    /// Where the window sits, in screen dp from the top left of the main screen. Changing
    /// it moves the window; the position the user chose is not given back. Without it the
    /// platform places the window.
    pub fn position(mut self, x: f32, y: f32) -> Self {
        self.position = Some((x, y));
        self
    }

    /// The application's stable name for the window, which matches a window the system
    /// brings back to the node that describes it.
    pub fn restore_key(mut self, key: impl Into<String>) -> Self {
        self.restore_key = Some(key.into());
        self
    }

    /// The window's own application zoom level, kept inside its range.
    pub fn zoom_level(mut self, level: i8) -> Self {
        self.zoom_level = Some(crate::zoom::clamp_level(level));
        self
    }

    /// Opens the window under a pointer that is held down and follows it until it is let
    /// go. Read when the window opens and not again.
    pub fn drag_from_pointer(mut self, drag: crate::windows::DragFromPointer) -> Self {
        self.drag_from_pointer = Some(drag);
        self
    }

    /// A theme for this window over the application's.
    pub fn theme(mut self, theme: crate::schema::Theme) -> Self {
        self.theme = Some(theme);
        self
    }

    /// The window became the key window or stopped being it.
    pub fn on_focus_changed(mut self, handler: impl FnMut(bool) + 'static) -> Self {
        self.on_focus_changed = Some(std::boxed::Box::new(handler));
        self
    }

    /// The window's outer frame moved or changed size.
    pub fn on_moved(mut self, handler: impl FnMut(crate::windows::WindowFrame) + 'static) -> Self {
        self.on_moved = Some(std::boxed::Box::new(handler));
        self
    }

    /// The window is being dragged out of a tab, and how that ended.
    pub fn on_drag(
        mut self,
        handler: impl FnMut(crate::windows::WindowDragEvent) + 'static,
    ) -> Self {
        self.on_drag = Some(std::boxed::Box::new(handler));
        self
    }

    /// The platform could not open this window. Tell the user, or put the tab back.
    pub fn on_unavailable(mut self, handler: impl FnMut() + 'static) -> Self {
        self.on_unavailable = Some(std::boxed::Box::new(handler));
        self
    }

    /// The system closed the window without asking. Stop composing it.
    pub fn on_closed(mut self, handler: impl FnMut() + 'static) -> Self {
        self.on_closed = Some(std::boxed::Box::new(handler));
        self
    }

    /// Composes this window with `content` as its children.
    pub fn content(mut self, content: impl FnOnce()) {
        self.done = true;
        self.compose(content);
    }

    fn compose(&mut self, content: impl FnOnce()) {
        use crate::windows::WindowEvent;
        let pair = |value: (f32, f32)| {
            let mut bytes = Vec::with_capacity(8);
            bytes.extend_from_slice(&value.0.to_bits().to_le_bytes());
            bytes.extend_from_slice(&value.1.to_bits().to_le_bytes());
            OwnedAttr::Bytes(bytes)
        };
        // Only what was said is written. A property left out is not a property sent as
        // nothing, and one that was said before and is not now is taken away.
        let mut properties: Attrs = Vec::new();
        if let Some(title) = self.title.take() {
            properties.push(("window_title", OwnedAttr::Text(title)));
        }
        if let Some(size) = self.size {
            properties.push(("window_size", pair(size)));
        }
        if let Some(size) = self.min_size {
            properties.push(("window_min_size", pair(size)));
        }
        if let Some(resizable) = self.resizable {
            properties.push(("window_resizable", OwnedAttr::Bool(resizable)));
        }
        if let Some(report) = self.report_size {
            properties.push(("report_size", OwnedAttr::Bool(report)));
        }
        if let Some(chrome) = self.chrome {
            properties.push(("window_chrome", OwnedAttr::Int(i64::from(chrome as u16))));
        }
        if let Some(title_bar) = self.title_bar {
            properties.push((
                "window_title_bar",
                OwnedAttr::Int(i64::from(title_bar as u16)),
            ));
        }
        if let Some(icon) = self.icon {
            properties.push(("window_icon", OwnedAttr::Int(i64::from(icon))));
        }
        if let Some(position) = self.position {
            properties.push(("window_position", pair(position)));
        }
        if let Some(key) = self.restore_key.take() {
            properties.push(("window_restore_key", OwnedAttr::Text(key)));
        }
        if let Some(level) = self.zoom_level {
            properties.push(("window_zoom_level", OwnedAttr::Int(i64::from(level))));
        }
        if let Some(drag) = self.drag_from_pointer {
            properties.push(("drag_from_pointer", OwnedAttr::Bytes(drag.to_bytes())));
        }

        let mut listeners: Vec<(&'static str, EventCallback)> = Vec::new();
        let mut close = self.on_close_request.take();
        listeners.push((
            "onwindowclose",
            window_callback(move |event| {
                if let (WindowEvent::CloseRequested, Some(handler)) = (event, close.as_mut()) {
                    handler();
                }
            }),
        ));
        if let Some(mut handler) = self.on_focus_changed.take() {
            listeners.push((
                "onwindowfocus",
                window_callback(move |event| {
                    if let WindowEvent::FocusChanged { focused } = event {
                        handler(focused);
                    }
                }),
            ));
        }
        if let Some(mut handler) = self.on_moved.take() {
            listeners.push((
                "onwindowmoved",
                window_callback(move |event| {
                    if let WindowEvent::Moved {
                        x,
                        y,
                        width,
                        height,
                    } = event
                    {
                        handler(crate::windows::WindowFrame {
                            x,
                            y,
                            width,
                            height,
                        });
                    }
                }),
            ));
        }
        if let Some(mut handler) = self.on_drag.take() {
            listeners.push((
                "onwindowdrag",
                window_callback(move |event| {
                    if let WindowEvent::Drag {
                        phase,
                        screen_x,
                        screen_y,
                        over_window,
                    } = event
                    {
                        handler(crate::windows::WindowDragEvent {
                            phase,
                            screen_x,
                            screen_y,
                            over_window,
                        });
                    }
                }),
            ));
        }
        if let Some(mut handler) = self.on_unavailable.take() {
            listeners.push((
                "onwindowunavailable",
                window_callback(move |event| {
                    if event == WindowEvent::Unavailable {
                        handler();
                    }
                }),
            ));
        }
        if let Some(mut handler) = self.on_closed.take() {
            listeners.push((
                "onwindowclosed",
                window_callback(move |event| {
                    if event == WindowEvent::Closed {
                        handler();
                    }
                }),
            ));
        }

        let theme = self.theme.take();
        emit(
            widget_key("compose_rust::ui::Window"),
            WidgetKind::Window,
            |attrs| attrs.append(&mut properties),
            listeners,
            move || {
                // The group has its node by now, so the window is known.
                if let (Some(theme), Some(window)) = (theme, crate::windows::current_window()) {
                    window.set_theme(theme);
                }
                content();
            },
        );
    }
}