lurq 0.65.1

A Rust UI toolkit with typed components, retained state, reactive signals, GPU rendering, and DevTools.
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
//! A picture: an image in its box, with what it shows while it loads and when it cannot be shown, and what lies
//! over it.

use std::sync::Arc;

use super::{
  Menu, MenuItem, Skeleton, SkeletonAnimation, SkeletonPart, Tooltip, icons, names,
  part_style::{Length, PartAnimation, PartTransition},
  recipe::{Case, Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe, Selector},
  shared::{fading, glyph, pressable},
  theme::{Themed, current_theme},
};
use crate::{
  animation::Transition,
  app::{
    component::Component,
    ctx::{Ctx, Placement},
    events::MouseEvent,
    theme::{MotionDuration, MotionEasing, OpacityLevel, PaletteColor, RadiusSize, Theme, TypographyStyle},
  },
  components::{Column, Row, Stack},
  core::{ElementRef, PictureError, Signal},
  layout::{
    Alignment, StackAlignment,
    layout_kind::{Justify, SizeBounds},
    text_style::{FontStyle, FontWeight, TextAlign},
  },
  node::{
    CursorIcon, Element, EventHandler, HitTestBehavior, IntoMouseEventHandler, NodeUpdate, border::Border,
    color::Color, dimension::Dimension, outline::Outline,
  },
};

/// A picture this wide and wider is drawn whole: the large form.
pub const PICTURE_LARGE_FROM: f32 = 240.0;
/// A picture this wide and narrower is a thumbnail: the small form.
pub const PICTURE_SMALL_UP_TO: f32 = 96.0;
/// A picture this wide and narrower is as small as an avatar: the smallest form.
pub const PICTURE_SMALLEST_UP_TO: f32 = 40.0;
/// The ratio of a picture's box that has a width and nothing to tell its height: no height of its own, no ratio,
/// and no pixels yet. The video player's.
const WIDE: f32 = 16.0 / 9.0;

/// The forms of a picture, by the room it has: what it keeps of what lies over it.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum PictureSize {
  /// As small as an avatar, 40 wide and under: the picture, or a glyph where there is none.
  Xs,
  /// A thumbnail, 96 wide and under: the picture, the select mark and the count; what it does is a glyph or
  /// a line.
  Sm,
  /// Compact, under 240 wide: a title on one line and the menu's button alone.
  Md,
  /// From 240 wide: everything.
  #[default]
  Lg,
}

names!(PictureSize {
  Xs => "xs",
  Sm => "sm",
  Md => "md",
  Lg => "lg",
});

impl PictureSize {
  const ALL: [Self; 4] = [Self::Xs, Self::Sm, Self::Md, Self::Lg];

  /// The form that fits `width`.
  pub fn for_width(width: f32) -> Self {
    if width <= PICTURE_SMALLEST_UP_TO {
      Self::Xs
    } else if width <= PICTURE_SMALL_UP_TO {
      Self::Sm
    } else if width < PICTURE_LARGE_FROM {
      Self::Md
    } else {
      Self::Lg
    }
  }

  /// The widths of a picture this form is for: what [`for_width`](Self::for_width) says, as bounds.
  pub fn bounds(self) -> SizeBounds {
    match self {
      Self::Xs => SizeBounds::new().max_width(PICTURE_SMALLEST_UP_TO),
      Self::Sm => SizeBounds::new()
        .above_width(PICTURE_SMALLEST_UP_TO)
        .max_width(PICTURE_SMALL_UP_TO),
      Self::Md => SizeBounds::new()
        .above_width(PICTURE_SMALL_UP_TO)
        .below_width(PICTURE_LARGE_FROM),
      Self::Lg => SizeBounds::new().min_width(PICTURE_LARGE_FROM),
    }
  }

  fn of(name: Name) -> Self {
    Self::ALL
      .into_iter()
      .find(|size| Name::from(*size) == name)
      .unwrap_or_default()
  }
}

/// How a picture's pixels fill its box.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum PictureFit {
  /// As small as fills the box, at its own ratio: what does not fit is cut.
  #[default]
  Cover,
  /// As large as fits the box whole, at its own ratio: the box shows beside it.
  Contain,
  /// Stretched to the box, whatever its own ratio.
  Fill,
  /// At its own size: what does not fit is cut, and the box shows beside what is smaller.
  None,
}

/// The shape of a picture's box.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum PictureShape {
  /// The corners of a surface.
  #[default]
  Rounded,
  /// A circle, where the box is square.
  Circle,
}

/// What a picture's box shows while its pixels are on their way.
#[derive(Clone, Debug, Default)]
pub enum PicturePlaceholder {
  /// The skeleton's sheen over the box.
  #[default]
  Skeleton,
  /// A small version of the picture, blurred: a thumbnail of a few hundred bytes the app has at hand, a few
  /// pixels it made from a hash, or a small file it loads first. It shows once it is there, and the picture
  /// comes in over it. The box alone until then. Made by [`blur`](Self::blur) and
  /// [`blur_pixels`](Self::blur_pixels).
  Blur(PictureSmall),
  /// The box alone.
  None,
}

impl PicturePlaceholder {
  /// [`Blur`](Self::Blur) of what `source` gives, read and decoded as the picture is: bytes, a file, a
  /// resource, an address.
  pub fn blur(source: impl Into<crate::resources::Source>) -> Self {
    Self::Blur(PictureSmall(Source::Encoded {
      source: source.into(),
      cap: None,
    }))
  }

  /// [`Blur`](Self::Blur) of pixels the app has.
  #[cfg(feature = "raster")]
  pub fn blur_pixels(data: crate::images::ImageData) -> Self {
    Self::Blur(PictureSmall(Source::Data(data)))
  }
}

/// The small version of a picture that stands in for it, blurred: see [`PicturePlaceholder::Blur`].
#[derive(Clone)]
pub struct PictureSmall(#[cfg_attr(not(feature = "raster"), allow(dead_code))] Source);

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

/// How far the small version of a picture is blurred while it stands in for it.
#[cfg(feature = "raster")]
const BLUR: f32 = 24.0;
/// How much larger than the box the blurred version is drawn, so that the soft edge the blur gives it lies
/// outside the box and is cut off.
#[cfg(feature = "raster")]
const BLUR_OVERDRAW: f32 = 1.25;

/// The parts of a picture a recipe styles.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PicturePart {
  /// The picture with its caption under it: a figure. Its gap is between the two.
  Figure,
  /// The picture's box: its fill shows while it loads, beside a picture that does not fill it and where there
  /// is none. Rounded, and what it holds is cut to it.
  Root,
  /// The pixels.
  Image,
  /// The line around the box, over the picture.
  Edge,
  /// What stands in for the pixels while they are on their way, as a whole: what it leaves with is how it
  /// goes when they are there, over them. Only a box whose size does not wait for the pixels has it.
  StandIn,
  /// The sheen that crosses the box while it loads.
  Sheen,
  /// The dark layer over the picture under a count.
  Veil,
  /// The app's own layer over the whole picture.
  Overlay,
  /// The middle of the box: what stands for a picture that is not there.
  Center,
  /// The glyph of a box with no picture to show.
  Mark,
  /// What the picture is called, under the glyph of a box with none.
  Label,
  /// The glyph of a picture that did not load.
  ErrorIcon,
  /// What the picture is called, in place of one that did not load.
  ErrorLabel,
  /// The line that says it did not load.
  ErrorDescription,
  /// The button that asks for it again.
  Retry,
  /// How far a load has come, in the middle of the box: the rail and the words under it.
  Progress,
  /// The rail of the load.
  Track,
  /// How much of it is loaded.
  Fill,
  /// The words under the rail.
  ProgressLabel,
  /// The line of the load along the bottom edge of a small picture.
  ProgressLine,
  /// How many more pictures the row has, over the veil.
  Count,
  /// The row along the top: the badge at its start, the select mark at its end.
  Top,
  /// The disc that says the picture is chosen, and the ring of one that can be.
  Select,
  /// The check in the disc.
  SelectIcon,
  /// The bar along the bottom: the caption and the actions, on a gradient. Its background is the color the
  /// gradient ends in at the bottom, from nothing at its top.
  Controls,
  /// The column of the bar's title and description.
  Text,
  /// The title in the bar.
  Title,
  /// The line under the title in the bar.
  Description,
  /// The row of the bar's buttons.
  Actions,
  /// A button of the bar.
  Control,
  /// The glyph of a button of the bar.
  ControlIcon,
  /// The panel of the menu behind More.
  MoreMenu,
  /// The caption under the picture.
  Caption,
}

names!(PicturePart {
  Figure => "figure",
  Root => "root",
  Image => "image",
  Edge => "edge",
  StandIn => "stand_in",
  Sheen => "sheen",
  Veil => "veil",
  Overlay => "overlay",
  Center => "center",
  Mark => "mark",
  Label => "label",
  ErrorIcon => "error_icon",
  ErrorLabel => "error_label",
  ErrorDescription => "error_description",
  Retry => "retry",
  Progress => "progress",
  Track => "track",
  Fill => "fill",
  ProgressLabel => "progress_label",
  ProgressLine => "progress_line",
  Count => "count",
  Top => "top",
  Select => "select",
  SelectIcon => "select_icon",
  Controls => "controls",
  Text => "text",
  Title => "title",
  Description => "description",
  Actions => "actions",
  Control => "control",
  ControlIcon => "control_icon",
  MoreMenu => "more_menu",
  Caption => "caption",
});

/// The forms of a picture as a whole.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub enum PictureVariant {
  /// For a pointer: the bar comes in under it, with its buttons.
  #[default]
  Desktop,
  /// For a finger, on a phone (the board's Mobile/Picture): a large picture runs edge to edge with no corners and
  /// no line, its bar is always there and has no buttons, and a long press brings its actions in an
  /// [`ActionSheet`](super::ActionSheet).
  Phone,
}

names!(PictureVariant {
  Desktop => "desktop",
  Phone => "phone",
});

/// The states of a picture a recipe styles.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PictureState {
  /// It is the box alone: no fill, no corners, no line around it.
  Bare,
  /// Its shape is [`PictureShape::Circle`].
  Circle,
  /// It is pressed as a whole: it opens, or it is chosen.
  Pressable,
  /// It can be chosen: the ring of its mark shows under the pointer and with the focus.
  Selectable,
  /// It is chosen.
  Selected,
  /// It takes nothing.
  Disabled,
  /// Its bar shows whatever the pointer does.
  Shown,
  /// A button of its bar whose menu is open.
  On,
}

names!(PictureState {
  Bare => "bare",
  Circle => "circle",
  Pressable => "pressable",
  Selectable => "selectable",
  Selected => "selected",
  Disabled => "disabled",
  Shown => "shown",
  On => "on",
});

/// What builds a picture's nodes: a theme's in place of the default one.
pub type PictureRenderer = Arc<dyn Fn(PictureView) -> Element + Send + Sync>;

type Action = Arc<dyn Fn() + Send + Sync>;
type Told<T> = Arc<dyn Fn(T) + Send + Sync>;

/// Where a picture's pixels come from.
#[derive(Clone)]
enum Source {
  /// Nowhere: the box says so.
  None,
  #[cfg(feature = "raster")]
  Data(crate::images::ImageData),
  /// Encoded bytes to read and decode: a file, bytes, a resource of the app, an address. `cap` is the most
  /// pixels across and down it is decoded to.
  Encoded {
    source: crate::resources::Source,
    cap: Option<(u32, u32)>,
  },
}

/// Where a picture's pixels are, as its component found them when it rendered.
#[derive(Clone)]
enum Phase {
  /// It has no source.
  Empty,
  #[cfg(feature = "raster")]
  Ready(crate::images::ImageData),
  /// On their way: the bytes read and how many there are, where the app's loader reads them and knows.
  #[cfg_attr(not(feature = "image"), allow(dead_code))]
  Loading(Option<(u64, u64)>),
  /// They did not come, or were no picture: why.
  Failed(PictureError),
}

/// How far the load of a picture has come: what [`Picture::progress_text`] is given.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PictureLoad {
  /// How much of it is there, of 1.
  pub fraction: f64,
  /// The bytes read and how many there are, where the app's loader loads it and knows.
  pub bytes: Option<(u64, u64)>,
}

impl PictureLoad {
  /// "42%", and "42% · 1.4 of 3.4 MB" where the bytes are known.
  fn text(self) -> String {
    let percent = format!("{}%", (self.fraction * 100.0).round());
    match self.bytes {
      Some((loaded, total)) => {
        let locale = super::current_locale();
        let megabytes = |bytes: u64| format!("{:.1}", bytes as f64 / (1024.0 * 1024.0));
        let total = crate::core::fill(&locale.fields.megabytes, &[("size", &megabytes(total))]);
        let said = crate::core::fill(
          &locale.common.place_of,
          &[("at", &megabytes(loaded)), ("total", &total)],
        );
        format!("{percent} · {said}")
      }
      None => percent,
    }
  }
}

/// Why this build shows no picture decoded from encoded bytes.
#[cfg(not(feature = "image"))]
const NO_CODECS: &str = "this build has no `image` feature: it decodes no picture";

/// The words a picture writes itself.
#[derive(Clone, PartialEq)]
struct Words {
  error_description: Arc<str>,
  retry: Arc<str>,
  download: Arc<str>,
  more: Arc<str>,
  open: Arc<str>,
}

impl Default for Words {
  fn default() -> Self {
    let locale = super::current_locale();
    let words = &locale.common;
    Self {
      error_description: words.picture_failed.clone(),
      retry: words.try_again.clone(),
      download: words.download.clone(),
      more: words.more.clone(),
      open: words.open.clone(),
    }
  }
}

/// A picture: an image in its box.
///
/// ```
/// use lurq::ui::{Picture, PictureFit};
///
/// let lake = Picture::from_file("photos/lake.jpg")
///   .label("The lake at dusk, the ridge behind it")
///   .width(320.0)
///   .aspect_ratio(3.0 / 2.0);
/// let logo = Picture::from_resource("brand/logo.png").fit(PictureFit::Contain).size_px(120.0, 48.0);
/// ```
///
/// Its width and its height are a browser's `img`'s. With neither said it is as large as its pixels; with one
/// said the other follows the pixels' ratio; with both said, or with an [`aspect_ratio`](Self::aspect_ratio),
/// it is a box, which the pixels cover unless [`fit`](Self::fit) says otherwise. A picture whose pixels are not
/// there yet has no size of its own: say one, or a ratio, and the page does not move when they come.
///
/// While it decodes the box shows the skeleton's sheen; a picture that did not load shows what it is called
/// ([`label`](Self::label)), that it did not load, and a button that asks for it again; a picture with no source
/// ([`empty`](Self::empty)) shows a glyph and what it would be called.
///
/// What lies over it: a [`badge`](Self::badge) at the top, and a bar along the bottom with a
/// [`title`](Self::title), a [`description`](Self::description) and its buttons, which shows under the pointer
/// and with the focus. A small picture keeps less of that: see [`PictureSize`]. Its form follows its width where
/// [`size`](Self::size) does not say it.
///
/// A picture is a component. Laid in a tree (`Row::new().child(Picture::from_url(..))`) it is mounted by the
/// component that renders the tree, and keeps what is its own for as long as it is rendered: the load of its
/// source and how far it has come, whether the menu behind More is open, its buttons' tooltips. In a list that
/// changes give it a [`key`](Self::key). In a tree no component renders it shows nothing.
#[derive(Clone)]
pub struct Picture {
  source: Source,
  label: Option<Arc<str>>,
  decorative: bool,
  width: Option<Length>,
  height: Option<Length>,
  aspect_ratio: Option<f32>,
  fit: PictureFit,
  anchor: StackAlignment,
  shape: PictureShape,
  bare: bool,
  size: Option<Name>,
  variant: Option<Name>,
  placeholder: PicturePlaceholder,
  loading: bool,
  progress: Option<f64>,
  progress_text: Option<Arc<dyn Fn(PictureLoad) -> String + Send + Sync>>,
  words: Words,
  on_retry: Vec<Action>,
  on_load: Vec<Action>,
  on_error: Vec<Told<PictureError>>,
  fallback: Option<Arc<dyn Fn() -> Element + Send + Sync>>,
  caption: Option<Arc<str>>,
  title: Option<Arc<str>>,
  description: Option<Arc<str>>,
  badge: Option<Element>,
  on_download: Vec<Action>,
  menu_items: Vec<MenuItem>,
  actions: Vec<Element>,
  bar: bool,
  force_bar_shown: Option<bool>,
  children: Vec<Element>,
  count: Option<Arc<str>>,
  on_click: Vec<EventHandler<MouseEvent>>,
  selectable: bool,
  selected: bool,
  on_change: Vec<Told<bool>>,
  lightbox: bool,
  open: Option<Signal<bool>>,
  on_open_change: Vec<Told<bool>>,
  disabled: bool,
  states: Vec<Name>,
  rules: Recipe,
  forced: Option<Interaction>,
  id: Option<Arc<str>>,
  key: Option<Arc<str>>,
}

impl Picture {
  fn of(source: Source) -> Self {
    Self {
      source,
      label: None,
      decorative: false,
      width: None,
      height: None,
      aspect_ratio: None,
      fit: PictureFit::default(),
      anchor: StackAlignment::Center,
      shape: PictureShape::default(),
      bare: false,
      size: None,
      variant: None,
      placeholder: PicturePlaceholder::default(),
      loading: false,
      progress: None,
      progress_text: None,
      words: Words::default(),
      on_retry: Vec::new(),
      on_load: Vec::new(),
      on_error: Vec::new(),
      fallback: None,
      caption: None,
      title: None,
      description: None,
      badge: None,
      on_download: Vec::new(),
      menu_items: Vec::new(),
      actions: Vec::new(),
      bar: true,
      force_bar_shown: None,
      children: Vec::new(),
      count: None,
      on_click: Vec::new(),
      selectable: false,
      selected: false,
      on_change: Vec::new(),
      lightbox: false,
      open: None,
      on_open_change: Vec::new(),
      disabled: false,
      states: Vec::new(),
      rules: Recipe::new(),
      forced: None,
      id: None,
      key: None,
    }
  }

  /// A picture of pixels the app has.
  #[cfg(feature = "raster")]
  pub fn new(data: crate::images::ImageData) -> Self {
    Self::of(Source::Data(data))
  }

  /// A picture with no source: its box, with a glyph and what it would be called in it.
  pub fn empty() -> Self {
    Self::of(Source::None)
  }

  /// A picture read from `source` and decoded, off the UI thread: a file, bytes, a resource of the app or an
  /// address, as [`Source`](crate::resources::Source) says them. A resource and an address are read by the
  /// app's [`Loader`](crate::resources::Loader). The pixels are kept under the source's key.
  ///
  /// A build without the feature `image` has no decoder: the picture shows that it did not load, and why.
  pub fn from_source(source: impl Into<crate::resources::Source>) -> Self {
    Self::of(Source::Encoded {
      source: source.into(),
      cap: None,
    })
  }

  /// A picture read from a file and decoded. Its key is the path with the file's size and the time it was
  /// changed.
  pub fn from_file(path: impl Into<std::path::PathBuf>) -> Self {
    Self::from_source(crate::resources::Source::file(path))
  }

  /// A picture decoded from encoded bytes (PNG, JPEG, the first frame of a GIF or WebP, BMP, TIFF, ICO, and an SVG
  /// with the feature `canvas`, drawn at its own size or as large as [`downscale_to`](Self::downscale_to) says). The
  /// bytes are hashed for the key its pixels are kept under, unless [`cache_key`](Self::cache_key) gives one.
  pub fn from_bytes(bytes: impl Into<Arc<[u8]>>) -> Self {
    Self::from_source(crate::resources::Source::bytes(bytes))
  }

  /// A picture that is a resource of the app: a path under the root the app gave its loader
  /// (`app.loader().root(..)`).
  pub fn from_resource(path: &str) -> Self {
    Self::from_source(crate::resources::Source::resource(path))
  }

  /// A picture at an address on the network. Outside a browser it needs the feature `http`; a build without it
  /// shows that the picture did not load, and why.
  pub fn from_url(url: &str) -> Self {
    Self::from_source(crate::resources::Source::url(url))
  }

  /// The key a decoded picture's pixels are kept under, in place of the one its source makes: the same key is
  /// the same pixels. An app that stores a hash of what it shows gives it.
  pub fn cache_key(mut self, key: u64) -> Self {
    if let Source::Encoded { source, .. } = &mut self.source {
      *source = source.clone().key(key);
    }
    self
  }

  /// Decodes no larger than `width` by `height` pixels, keeping the ratio and never enlarging: a thumbnail of
  /// a large photo does not keep all of it. An SVG, which has no pixels of its own, is drawn as large as fits.
  pub fn downscale_to(mut self, width: u32, height: u32) -> Self {
    if let Source::Encoded { cap, .. } = &mut self.source {
      *cap = Some((width, height));
    }
    self
  }

  /// The key of what it shows, where it is read from a source: what tells one picture another's for a component
  /// that holds it.
  pub(crate) fn source_key(&self) -> Option<u64> {
    match &self.source {
      Source::Encoded { source, .. } => Some(source.cache_key()),
      _ => None,
    }
  }

  /// Its pixels where they are there now: the ones it was given, or the ones of its source that lurq's cache of
  /// decoded pictures holds. Nothing is read or decoded for it here: a picture on its way has none yet, and its
  /// [`on_load`](Self::on_load) tells when they come.
  #[cfg(feature = "raster")]
  pub fn pixels(&self) -> Option<crate::images::ImageData> {
    match &self.source {
      Source::None => None,
      Source::Data(data) => Some(data.clone()),
      #[cfg(feature = "image")]
      Source::Encoded { source, cap } => {
        use crate::images::{ImageKey, ImageSizeCap, decoded_images};
        let cap = cap.map(|(width, height)| ImageSizeCap::new(width, height));
        decoded_images()
          .state(ImageKey::new(source.cache_key()), cap)?
          .ready()
          .cloned()
      }
      #[cfg(not(feature = "image"))]
      Source::Encoded { .. } => None,
    }
  }

  /// What the picture is called: what it shows of itself where there are no pixels, and its name to tooling
  /// that reads the tree. An `alt` text.
  pub fn label(mut self, label: &str) -> Self {
    self.label = Some(Arc::from(label));
    self
  }

  /// Whether the picture says nothing the words around it do not: tooling that reads the tree passes it by.
  pub fn decorative(mut self, decorative: bool) -> Self {
    self.decorative = decorative;
    self
  }

  /// The box's width. Not said, it follows the height by the pixels' ratio, or is the pixels' own.
  pub fn width(mut self, width: impl Into<Length>) -> Self {
    self.width = Some(width.into());
    self
  }

  /// The box's height. Not said, it follows the width by the pixels' ratio, or is the pixels' own.
  pub fn height(mut self, height: impl Into<Length>) -> Self {
    self.height = Some(height.into());
    self
  }

  /// A box of `width` by `height` pixels.
  pub fn size_px(self, width: f32, height: f32) -> Self {
    self.width(width).height(height)
  }

  /// The box's width over its height, which gives the side that is not said from the one that is, and with
  /// neither said makes the box as wide as its room. It holds the box of a picture whose pixels are not there
  /// yet.
  pub fn aspect_ratio(mut self, ratio: f32) -> Self {
    self.aspect_ratio = Some(ratio);
    self
  }

  /// How the pixels fill the box: they cover it unless told.
  pub fn fit(mut self, fit: PictureFit) -> Self {
    self.fit = fit;
    self
  }

  /// Where the pixels are held in the box where they are cut or do not fill it: the middle unless told.
  pub fn anchor(mut self, anchor: StackAlignment) -> Self {
    self.anchor = anchor;
    self
  }

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

  /// The pixels alone: no fill of the box, no corners, no line around it.
  pub fn bare(mut self, bare: bool) -> Self {
    self.bare = bare;
    self
  }

  /// Which form it is drawn in, a [`PictureSize`], in place of the one its width gives it.
  pub fn size(mut self, size: impl Into<Name>) -> Self {
    self.size = Some(size.into());
    self
  }

  /// What the box shows while the pixels are on their way.
  pub fn placeholder(mut self, placeholder: PicturePlaceholder) -> Self {
    self.placeholder = placeholder;
    self
  }

  /// Shows the box as it is while the pixels are on their way, whatever its source: for a picture the app is
  /// still fetching and has no source for yet.
  pub fn loading(mut self, loading: bool) -> Self {
    self.loading = loading;
    self
  }

  /// How far the load has come, of 1, where the app knows: a rail in the middle of the box in place of the
  /// sheen, and along the bottom edge of a small picture. `None` for a load that cannot tell. A picture the
  /// app's loader reads from an address shows it by itself where the server says how much there is.
  pub fn progress(mut self, progress: impl Into<Option<f64>>) -> Self {
    self.progress = progress.into().map(|progress| progress.clamp(0.0, 1.0));
    self
  }

  /// The words under the rail of a load, from how far it has come: "42%" unless told, and "42% · 1.4 of
  /// 3.4 MB" where the app's loader knows the bytes.
  pub fn progress_text(mut self, text: impl Fn(PictureLoad) -> String + Send + Sync + 'static) -> Self {
    self.progress_text = Some(Arc::new(text));
    self
  }

  /// The line that says a picture did not load: "The picture didn't load." unless told.
  pub fn error_description(mut self, description: &str) -> Self {
    self.words.error_description = Arc::from(description);
    self
  }

  /// The button that asks again for a picture that did not load: "Try again" unless told.
  pub fn retry_label(mut self, label: &str) -> Self {
    self.words.retry = Arc::from(label);
    self
  }

  /// What the download button is called: "Download" unless told.
  pub fn download_label(mut self, label: &str) -> Self {
    self.words.download = Arc::from(label);
    self
  }

  /// What the button that opens the picture in its lightbox is called: "Open" unless told.
  pub fn open_label(mut self, label: &str) -> Self {
    self.words.open = Arc::from(label);
    self
  }

  /// What the menu's button is called: "More" unless told.
  pub fn more_label(mut self, label: &str) -> Self {
    self.words.more = Arc::from(label);
    self
  }

  /// Called on a press of the button that asks again for a picture that did not load, before the picture
  /// asks. A second handler is called after the first.
  pub fn on_retry(mut self, handler: impl Fn() + Send + Sync + 'static) -> Self {
    self.on_retry.push(Arc::new(handler));
    self
  }

  /// What the box shows wherever it has no pixels (while they are on their way, when they did not come, with
  /// no source), in place of what a picture shows there itself: an avatar's initials. It goes out over the
  /// pixels as the picture's own stand-in does.
  pub(crate) fn fallback(mut self, fallback: impl Fn() -> Element + Send + Sync + 'static) -> Self {
    self.fallback = Some(Arc::new(fallback));
    self
  }

  /// Called once the picture's pixels are there: once for a source, and again for one asked for anew.
  pub fn on_load(mut self, handler: impl Fn() + Send + Sync + 'static) -> Self {
    self.on_load.push(Arc::new(handler));
    self
  }

  /// Called once the picture did not come, with why: a kind (not found, not allowed, a server's error, the
  /// network, no picture, too large, …), which the app's locale puts into words
  /// ([`MediaWords::picture_error`](crate::core::MediaWords::picture_error)); what the system or the decoder said is
  /// kept beside it for a log ([`PictureError::detail`]). Once for a source, and again for one asked for anew.
  pub fn on_error(mut self, handler: impl Fn(&PictureError) + Send + Sync + 'static) -> Self {
    self.on_error.push(Arc::new(move |error: PictureError| handler(&error)));
    self
  }

  /// A caption under the picture, which makes it a figure: "Figure 1. The lake at dusk."
  pub fn caption(mut self, caption: &str) -> Self {
    self.caption = Some(Arc::from(caption));
    self
  }

  /// The title in the bar over the picture's bottom edge.
  pub fn title(mut self, title: &str) -> Self {
    self.title = Some(Arc::from(title));
    self
  }

  /// The line under the title in the bar.
  pub fn description(mut self, description: &str) -> Self {
    self.description = Some(Arc::from(description));
    self
  }

  /// What stands at the top of the picture, at its start: a badge.
  pub fn badge(mut self, badge: impl Into<Element>) -> Self {
    self.badge = Some(badge.into());
    self
  }

  /// Called on a press of the download button, which the bar has once this is given. A second handler is
  /// called after the first.
  pub fn on_download(mut self, handler: impl Fn() + Send + Sync + 'static) -> Self {
    self.on_download.push(Arc::new(handler));
    self
  }

  /// An item of the menu behind More, which the bar has once it has an item. The menu opens on a mounted
  /// picture.
  pub fn menu_item(mut self, item: MenuItem) -> Self {
    self.menu_items.push(item);
    self
  }

  /// Items of the menu behind More, in order.
  pub fn menu_items(mut self, items: impl IntoIterator<Item = MenuItem>) -> Self {
    self.menu_items.extend(items);
    self
  }

  /// A button of the app's own in the bar, before the picture's: each call adds one.
  pub fn action(mut self, action: impl Into<Element>) -> Self {
    self.actions.push(action.into());
    self
  }

  /// Buttons of the app's own in the bar, in order.
  pub fn actions(mut self, actions: impl IntoIterator<Item = impl Into<Element>>) -> Self {
    self.actions.extend(actions.into_iter().map(Into::into));
    self
  }

  /// Whether the bar is drawn. Without it the picture is its pixels, its badge and the app's layer.
  pub fn bar(mut self, bar: bool) -> Self {
    self.bar = bar;
    self
  }

  /// Shows the bar, and the ring of the mark of a picture that can be chosen, or hides the bar, whatever the
  /// pointer and the focus do: for a picture that always says what it is, and for a gallery's page of states.
  pub fn force_bar_shown(mut self, shown: Option<bool>) -> Self {
    self.force_bar_shown = shown;
    self
  }

  /// Something of the app's own over the whole picture: a layer the size of the box, under the bar.
  pub fn child(mut self, child: impl Into<Element>) -> Self {
    self.children.push(child.into());
    self
  }

  /// Things of the app's own over the picture, in order.
  pub fn children(mut self, children: impl IntoIterator<Item = impl Into<Element>>) -> Self {
    self.children.extend(children.into_iter().map(Into::into));
    self
  }

  /// A count over a veil: "+5" on the last picture of a row that could not hold them all. A gallery's.
  pub(crate) fn count(mut self, count: &str) -> Self {
    self.count = Some(Arc::from(count));
    self
  }

  /// Called on a press of the picture: a click, or Enter or Space while it has the focus. A picture given one
  /// is pressed as a whole; a press of a button of its bar is that button's.
  pub fn on_click(mut self, handler: impl IntoMouseEventHandler) -> Self {
    self.on_click.push(handler.into_event_handler());
    self
  }

  /// Whether the picture can be chosen: a press chooses it, and the ring of its mark shows under the pointer.
  pub fn selectable(mut self, selectable: bool) -> Self {
    self.selectable = selectable;
    self
  }

  /// Whether the picture is chosen: the mark at its top, and a line around it. The app says so; the picture
  /// tells [`on_change`](Self::on_change) what a press would make it.
  pub fn selected(mut self, selected: bool) -> Self {
    self.selected = selected;
    self
  }

  /// Called on a press of a picture that can be chosen, with what it is to be. A second handler is called
  /// after the first.
  pub fn on_change(mut self, handler: impl Fn(bool) + Send + Sync + 'static) -> Self {
    self.on_change.push(Arc::new(handler));
    self
  }

  /// The form as a whole ([`PictureVariant`]): the phone's where the person points with a finger
  /// ([`Pointer::Coarse`](crate::app::Pointer::Coarse), [`touch_form`](super::touch_form)), whatever the window's
  /// width and the theme's density, unless told.
  pub fn variant(mut self, variant: impl Into<Name>) -> Self {
    self.variant = Some(variant.into());
    self
  }

  /// A press of the picture, or Enter or Space while it has the focus, opens it in a [`Lightbox`] over the
  /// window: the picture alone, whole, to bring closer. The lightbox shows its [`title`](Self::title) and its
  /// [`description`](Self::description). The bar of a large picture has a button that opens it too, the
  /// last one: a picture that can be chosen is chosen by a press, and opened by that button.
  pub fn lightbox(mut self, lightbox: bool) -> Self {
    self.lightbox = lightbox;
    self
  }

  /// Whether its lightbox is open, to read and to set. Without it the picture keeps it.
  pub fn open(mut self, open: Signal<bool>) -> Self {
    self.open = Some(open);
    self
  }

  /// Called when its lightbox opens, and when it is closed.
  pub fn on_open_change(mut self, handler: impl Fn(bool) + Send + Sync + 'static) -> Self {
    self.on_open_change.push(Arc::new(handler));
    self
  }

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

  /// Puts the picture in a state the theme's recipe names, or takes it out.
  pub fn state(mut self, state: impl Into<Name>, on: bool) -> Self {
    let state = state.into();
    self.states.retain(|held| *held != state);
    if on {
      self.states.push(state);
    }
    self
  }

  /// Styles one part of this picture over what the theme gives it.
  pub fn style(mut self, part: impl Into<Name>, style: impl FnOnce(super::PartStyle) -> super::PartStyle) -> Self {
    self.rules = self.rules.base(part, style);
    self
  }

  /// Lays rules over the theme's recipe for this picture.
  pub fn rules(mut self, rules: &Recipe) -> Self {
    self.rules = self.rules.extend(rules);
    self
  }

  /// Draws the picture as under the pointer, pressed or focused from the keyboard, whatever the pointer does.
  pub fn force(mut self, interaction: Option<Interaction>) -> Self {
    self.forced = interaction;
    self
  }

  /// The id of the picture's box; its parts are `{id}-retry`, `{id}-download`, `{id}-more`.
  pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
    self.id = Some(id.into());
    self
  }

  pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
    self.key = Some(key.into());
    self
  }

  /// The picture mounted in `ctx`: what converting it into an element does once the render that returns it
  /// has returned.
  pub fn mount(self, ctx: &mut Ctx) -> Element {
    let key = self.key.clone();
    let props = PictureProps(self);
    match key {
      Some(key) => ctx.mount_keyed::<PictureHost>(&key, props),
      None => ctx.mount::<PictureHost>(props),
    }
  }

  /// The picture as a viewer shows it: the pixels alone, whole, in all the room there is, with nothing over
  /// them and nothing a press does. What it is called and how it loads stay.
  /// Starts bringing the picture's pixels on without showing it: a file and bytes are decoded, a resource and
  /// an address asked of `loader` and decoded once their bytes are there. What a picture shown later then has
  /// at once. The load to hold while it is on its way, where the loader reads it: a load nobody holds is let
  /// go. Called again once it has ended, it decodes what it brought.
  #[cfg(feature = "image")]
  pub(crate) fn fetch(&self, loader: Option<&crate::resources::Loader>) -> Option<crate::resources::Load> {
    use crate::{
      images::{ImageKey, ImageSizeCap, ImageSource, decoded_images},
      resources::{LoadState, SourceKind},
    };
    let Source::Encoded { source, cap } = &self.source else {
      return None;
    };
    let key = ImageKey::new(source.cache_key());
    let cap = cap.map(|(width, height)| ImageSizeCap::new(width, height));
    let decode = |from: ImageSource| {
      decoded_images().load(key, cap, || from, None);
    };
    match &source.kind {
      SourceKind::Bytes(bytes) => decode(ImageSource::Bytes(bytes.clone())),
      SourceKind::File(path) => decode(ImageSource::File(path.clone())),
      SourceKind::Resource(_) | SourceKind::Url(_) => {
        if decoded_images().state(key, cap).is_some() {
          return None;
        }
        let load = loader?.load(source);
        match load.state() {
          LoadState::Loading { .. } => return Some(load),
          LoadState::Ready(bytes) => decode(ImageSource::Bytes(bytes)),
          LoadState::Failed(_) => {}
        }
      }
    }
    None
  }

  pub(crate) fn in_viewer(mut self) -> Self {
    self = self.stripped();
    self.width = Some(Length::Fill);
    self.height = Some(Length::Fill);
    self.aspect_ratio = None;
    self.fit = PictureFit::Contain;
    self.shape = PictureShape::Rounded;
    self.bare = true;
    self.size = Some(PictureSize::Lg.into());
    self
  }

  /// The picture as a small one of `width` by `height` in a strip or a grid of them: the pixels covering the
  /// box, with nothing over them and nothing a press does.
  pub(crate) fn as_thumb(mut self, width: f32, height: f32) -> Self {
    self = self.stripped();
    self.width = Some(Length::Px(width));
    self.height = Some(Length::Px(height));
    self.aspect_ratio = None;
    self.fit = PictureFit::Cover;
    self.bare = true;
    self.size = None;
    // What holds it is what tooling reads.
    self.decorative = true;
    // A small picture decodes small: a strip of a dozen photos does not hold a dozen photos' pixels. Four times
    // its longer side, both ways, covers its box at any scale of a screen and any shape of picture but a strip.
    if let Source::Encoded { cap, .. } = &mut self.source
      && cap.is_none()
    {
      let side = (width.max(height) * 4.0).ceil() as u32;
      *cap = Some((side, side));
    }
    self
  }

  /// The picture as one of a gallery's: the pixels covering all of the room it is given, with nothing over
  /// them and nothing a press does yet. What it is called and how it loads stay, and so do its box and its
  /// corners.
  pub(crate) fn in_grid(mut self) -> Self {
    self = self.stripped();
    self.width = Some(Length::Fill);
    self.height = Some(Length::Fill);
    self.aspect_ratio = None;
    self.fit = PictureFit::Cover;
    self
  }

  /// The picture with what lies over it and what a press of it does taken off, and no name in the tree of its
  /// own: what another component shows of it.
  fn stripped(mut self) -> Self {
    self.caption = None;
    self.badge = None;
    self.bar = false;
    self.force_bar_shown = None;
    self.children.clear();
    self.count = None;
    self.on_click.clear();
    self.selectable = false;
    self.selected = false;
    self.on_change.clear();
    self.lightbox = false;
    self.open = None;
    self.on_open_change.clear();
    self.id = None;
    self.key = None;
    self
  }

  /// The title and the description of its bar.
  pub(crate) fn caption_of(&self) -> (Option<Arc<str>>, Option<Arc<str>>) {
    (self.title.clone(), self.description.clone())
  }

  /// What it is called: its label, or its title.
  pub(crate) fn label_of(&self) -> Option<Arc<str>> {
    self.label.clone().or_else(|| self.title.clone())
  }

  /// Whether a press of the picture as a whole does something.
  fn pressable(&self) -> bool {
    !self.disabled && (self.selectable || self.lightbox || !self.on_click.is_empty())
  }

  /// The forms to build, each with the widths it lives at: the one that is said, or that a width in pixels
  /// gives, alone and at every width; else all of them, for layout to show the one that fits.
  ///
  /// `narrowed` is the form of the width a box of pixels was laid out at where its room made it narrower than
  /// it was told: the form of that width, built alone in its place.
  fn forms(&self, theme: &Theme, narrowed: Option<PictureSize>) -> Vec<(PictureSize, Option<SizeBounds>)> {
    if let Some(size) = self.size {
      return vec![(PictureSize::of(size), None)];
    }
    match self.width.and_then(|width| width.resolve(theme)) {
      Some(Dimension::Px(width)) => vec![(narrowed.unwrap_or(PictureSize::for_width(width)), None)],
      // A box that takes the size of what is in it has no width to choose a form by before it is laid out.
      None if self.aspect_ratio.is_none() => vec![(PictureSize::Lg, None)],
      _ => PictureSize::ALL
        .into_iter()
        .map(|size| (size, Some(size.bounds())))
        .collect(),
    }
  }
}

/// Pixels the app has are a picture of them.
#[cfg(feature = "raster")]
impl From<crate::images::ImageData> for Picture {
  fn from(data: crate::images::ImageData) -> Self {
    Self::new(data)
  }
}

/// A picture in a tree is its component, declared: the component that renders the tree mounts it.
impl From<Picture> for Element {
  fn from(picture: Picture) -> Self {
    let key = picture.key.clone();
    let props = PictureProps(picture);
    match key {
      Some(key) => Element::component_keyed::<PictureHost>(key, props),
      None => Element::component::<PictureHost>(props),
    }
  }
}

/// A picture to mount.
#[derive(Clone)]
struct PictureProps(Picture);

/// A picture is built anew by every render of what holds it, and is the picture it was where all it is made
/// of can be told to be the same: then its component is not rendered again, and what it rendered last stands.
/// A closure and an element are made anew by each render and nothing compares them: a picture with a handler,
/// a badge, a button, a layer, a menu or rules of its own is another picture each time, as it was.
impl PartialEq for PictureProps {
  fn eq(&self, other: &Self) -> bool {
    match (self.0.plain(), other.0.plain()) {
      (Some(this), Some(that)) => this == that,
      _ => false,
    }
  }
}

/// Which pixels a source is: what tells two sources apart without reading them.
#[derive(PartialEq)]
enum SourceId {
  None,
  /// Pixels the app has, by the buffer's id.
  #[cfg(feature = "raster")]
  Data(u64),
  /// What is read and decoded, by the source's key and the size it is decoded to.
  Encoded(u64, Option<(u32, u32)>),
}

impl Source {
  fn id(&self) -> SourceId {
    match self {
      Source::None => SourceId::None,
      #[cfg(feature = "raster")]
      Source::Data(data) => SourceId::Data(data.id()),
      Source::Encoded { source, cap } => SourceId::Encoded(source.cache_key(), *cap),
    }
  }
}

/// All a picture is made of that can be compared, where it is made of nothing else.
#[derive(PartialEq)]
struct Plain<'a> {
  source: SourceId,
  /// What stands in for it: 0 for the sheen, 1 for the box alone, 2 for a small version, and which.
  placeholder: (u8, SourceId),
  texts: [&'a Option<Arc<str>>; 7],
  words: &'a Words,
  lengths: (&'a Option<Length>, &'a Option<Length>, Option<f32>),
  shown: (
    PictureFit,
    StackAlignment,
    PictureShape,
    &'a Option<Name>,
    &'a Option<Name>,
    &'a [Name],
  ),
  flags: [bool; 8],
  /// The signal that says whether its lightbox is open, by its id.
  open: Option<usize>,
  load: (Option<f64>, Option<bool>, Option<Interaction>),
}

impl Picture {
  /// The picture as what can be compared, or `None` where it holds a closure, an element or rules.
  fn plain(&self) -> Option<Plain<'_>> {
    // Every field is named, so that a new one is put on one side or the other here.
    let Picture {
      source,
      label,
      decorative,
      width,
      height,
      aspect_ratio,
      fit,
      anchor,
      shape,
      bare,
      size,
      variant,
      placeholder,
      loading,
      progress,
      progress_text,
      words,
      on_retry,
      on_load,
      on_error,
      fallback,
      caption,
      title,
      description,
      badge,
      on_download,
      menu_items,
      actions,
      bar,
      force_bar_shown,
      children,
      count,
      on_click,
      selectable,
      selected,
      on_change,
      lightbox,
      open,
      on_open_change,
      disabled,
      states,
      rules,
      forced,
      id,
      key,
    } = self;
    let made_anew = progress_text.is_some()
      || fallback.is_some()
      || badge.is_some()
      || !(on_retry.is_empty()
        && on_load.is_empty()
        && on_error.is_empty()
        && on_download.is_empty()
        && menu_items.is_empty()
        && actions.is_empty()
        && children.is_empty()
        && on_click.is_empty()
        && on_change.is_empty()
        && on_open_change.is_empty()
        && rules.is_empty());
    if made_anew {
      return None;
    }
    Some(Plain {
      source: source.id(),
      placeholder: match placeholder {
        PicturePlaceholder::Skeleton => (0, SourceId::None),
        PicturePlaceholder::None => (1, SourceId::None),
        PicturePlaceholder::Blur(PictureSmall(small)) => (2, small.id()),
      },
      texts: [label, caption, title, description, count, id, key],
      words,
      lengths: (width, height, *aspect_ratio),
      shown: (*fit, *anchor, *shape, size, variant, states),
      flags: [
        *decorative,
        *bare,
        *loading,
        *bar,
        *selectable,
        *selected,
        *disabled,
        *lightbox,
      ],
      open: open.as_ref().map(Signal::id),
      load: (*progress, *force_bar_shown, *forced),
    })
  }
}

impl crate::app::component::DevtoolsInspectable for PictureProps {}

/// What a picture keeps between renders.
#[derive(Clone)]
struct Live {
  /// Whether the menu behind More is open.
  menu: Signal<bool>,
  /// The More button, which the menu hangs from.
  more: ElementRef,
  /// Whether its lightbox is open, where the app names no signal for it.
  viewer: Signal<bool>,
  /// Whether the sheet of its actions is open, on a phone.
  sheet: Signal<bool>,
  /// Counts on when a load or a decode of the picture's comes further or ends, and when it is asked for again:
  /// the picture renders again and looks.
  tick: Signal<u64>,
  /// Counts `tick` on: what a load and a decode are given to call, the same one for the picture's life.
  wake: Action,
  /// The load of the picture's bytes while it is on its way, with its source's key: while it is held the app's
  /// loader keeps it.
  held: Arc<parking_lot::Mutex<Option<(u64, crate::resources::Load)>>>,
  /// Whether any of its box shows, in the list it scrolls in or in the window: a picture that shows is loaded
  /// before one that does not. Kept by the observer under it, for as long as the picture lives.
  #[cfg_attr(not(feature = "image"), allow(dead_code))]
  shows: Arc<std::sync::atomic::AtomicBool>,
  _watching: Arc<crate::core::VisibilityObserver>,
  /// The load of the small version that stands in for the picture, as `held` is the picture's.
  held_small: Arc<parking_lot::Mutex<Option<(u64, crate::resources::Load)>>>,
  /// What the app was last told of a source, by its key: that it came, or that it did not.
  told: Arc<parking_lot::Mutex<Option<(u64, bool)>>>,
  /// What to tell the app once this render is laid out.
  news: Arc<parking_lot::Mutex<Vec<Action>>>,
  /// The box as it is laid out: a box of pixels that its room made narrower takes the form of the width it got.
  frame: ElementRef,
  /// The width in pixels the box was told, where it was told one and no form.
  said: Arc<parking_lot::Mutex<Option<f32>>>,
  /// The form of the width the box got, where that is narrower than it was told and has another form.
  narrowed: Signal<Option<PictureSize>>,
}

/// The component of a picture.
struct PictureHost {
  live: Live,
}

impl Component for PictureHost {
  type Props = PictureProps;

  fn create(ctx: &mut Ctx) -> Self {
    let tick = ctx.signal(0u64);
    let wake: Action = {
      let tick = tick.clone();
      Arc::new(move || tick.set(tick.get_untracked().wrapping_add(1)))
    };
    // Measured before paint, and written only where the form changes: the first frame of a box its room made
    // narrower has the form that fits it, and a box at its width builds the one form it always did.
    let (frame, said, narrowed) = (
      ElementRef::new(),
      Arc::new(parking_lot::Mutex::new(None::<f32>)),
      ctx.signal(None::<PictureSize>),
    );
    {
      let (said, narrowed) = (said.clone(), narrowed.clone());
      frame.observe_rect_before_paint(move |rect| {
        let want = match *said.lock() {
          Some(width) if rect.width + 0.5 < width => {
            let form = PictureSize::for_width(rect.width);
            (form != PictureSize::for_width(width)).then_some(form)
          }
          _ => None,
        };
        if narrowed.get_untracked() != want {
          narrowed.set(want);
        }
      });
    }
    // A picture that shows is loaded before those that wait their turn and do not: in a long list scrolled
    // fast, the rows on the screen now and not the ones that went by.
    type Held = Arc<parking_lot::Mutex<Option<(u64, crate::resources::Load)>>>;
    let (held, held_small): (Held, Held) = (Arc::default(), Arc::default());
    let shows = Arc::new(std::sync::atomic::AtomicBool::new(false));
    let watching = frame.observe_visibility(crate::core::VisibilityOptions::new(), {
      let (shows, loads) = (shows.clone(), [held.clone(), held_small.clone()]);
      move |seen| {
        shows.store(seen.visible, std::sync::atomic::Ordering::Relaxed);
        let priority = if seen.visible {
          crate::resources::LoadPriority::High
        } else {
          crate::resources::LoadPriority::Normal
        };
        for held in &loads {
          if let Some((_, load)) = &*held.lock() {
            load.set_priority(priority);
          }
        }
      }
    });
    Self {
      live: Live {
        menu: ctx.signal(false),
        more: ElementRef::new(),
        viewer: ctx.signal(false),
        sheet: ctx.signal(false),
        tick,
        wake,
        held,
        held_small,
        shows,
        _watching: Arc::new(watching),
        told: Arc::default(),
        news: Arc::default(),
        frame,
        said,
        narrowed,
      },
    }
  }

  fn render(&self, ctx: &mut Ctx) -> impl Into<Element> {
    let picture = ctx.props::<PictureProps>().0.clone();
    let theme = current_theme();
    *self.live.said.lock() = match (picture.size, picture.width.and_then(|width| width.resolve(&theme))) {
      (None, Some(Dimension::Px(width))) => Some(width),
      _ => None,
    };
    // Read, so that a load that comes further renders the picture again.
    self.live.tick.get();
    let phase = self.phase(&picture, ctx);
    self.tell(&picture, &phase);
    let loader = ctx.app_handle().map(|app| app.loader().clone());
    #[cfg(feature = "raster")]
    let small = self.small(&picture, &phase, ctx);
    #[cfg_attr(not(feature = "raster"), allow(unused_mut))]
    let mut view = view(picture, &theme, &self.live, phase, loader, Some(ctx));
    #[cfg(feature = "raster")]
    {
      view.small = small;
    }
    view.render_themed()
  }

  /// The app hears of a picture that came or did not once the render that found it is laid out, not in it.
  fn after_layout(&self) {
    let news = std::mem::take(&mut *self.live.news.lock());
    news.iter().for_each(|tell| tell());
  }

  /// A picture that is gone waits for no load: the loader stops one nobody else holds.
  fn on_unmounted(&self) {
    *self.live.held.lock() = None;
    *self.live.held_small.lock() = None;
  }
}

impl PictureHost {
  /// Where the picture's pixels are now. A source to read and decode is brought on from here: asked of the
  /// app's loader and of the pixels' cache, each of which counts `tick` on when it has more.
  fn phase(&self, picture: &Picture, ctx: &Ctx) -> Phase {
    match &picture.source {
      Source::None => Phase::Empty,
      #[cfg(feature = "raster")]
      Source::Data(data) => Phase::Ready(data.clone()),
      #[cfg(not(feature = "image"))]
      Source::Encoded { .. } => {
        let _ = ctx;
        Phase::Failed(PictureError::Unavailable(Arc::from(NO_CODECS)))
      }
      #[cfg(feature = "image")]
      Source::Encoded { source, cap } => self.decoded(source, *cap, ctx, &self.live.held),
    }
  }

  /// The pixels of the small version that stands in for the picture while it is on its way, once they are
  /// there: the ones the app gave, or read and decoded as the picture is.
  #[cfg(feature = "raster")]
  fn small(&self, picture: &Picture, phase: &Phase, ctx: &Ctx) -> Option<crate::images::ImageData> {
    let waits = picture.loading || matches!(phase, Phase::Loading(_));
    let PicturePlaceholder::Blur(PictureSmall(small)) = &picture.placeholder else {
      return None;
    };
    if !waits {
      *self.live.held_small.lock() = None;
      return None;
    }
    match small {
      Source::Data(data) => Some(data.clone()),
      #[cfg(feature = "image")]
      Source::Encoded { source, cap } => match self.decoded(source, *cap, ctx, &self.live.held_small) {
        Phase::Ready(data) => Some(data),
        _ => None,
      },
      _ => {
        let _ = ctx;
        None
      }
    }
  }

  /// Where the pixels of `source` are, brought on from here; `held` keeps its load while it is on its way.
  #[cfg(feature = "image")]
  fn decoded(
    &self,
    source: &crate::resources::Source,
    cap: Option<(u32, u32)>,
    ctx: &Ctx,
    held: &parking_lot::Mutex<Option<(u64, crate::resources::Load)>>,
  ) -> Phase {
    use crate::{
      images::{ImageKey, ImageLoadState, ImageSizeCap, ImageSource, decoded_images},
      resources::{LoadState, SourceKind},
    };
    let key = ImageKey::new(source.cache_key());
    let cap = cap.map(|(width, height)| ImageSizeCap::new(width, height));
    let wake = &self.live.wake;
    let decode = |from: ImageSource| match decoded_images().load(key, cap, || from, Some(wake.clone())) {
      ImageLoadState::Loading => Phase::Loading(None),
      ImageLoadState::Ready(data) => Phase::Ready(data),
      ImageLoadState::Failed(error) => Phase::Failed(error.kind().clone()),
    };
    match &source.kind {
      // A file and bytes are read by what decodes them.
      SourceKind::Bytes(bytes) => decode(ImageSource::Bytes(bytes.clone())),
      SourceKind::File(path) => decode(ImageSource::File(path.clone())),
      // A resource and an address are read by the app's loader first.
      SourceKind::Resource(_) | SourceKind::Url(_) => {
        let mut held = held.lock();
        // Its pixels are there, or known to be no picture: nothing to load.
        match decoded_images().state(key, cap) {
          Some(ImageLoadState::Ready(data)) => {
            *held = None;
            return Phase::Ready(data);
          }
          Some(ImageLoadState::Failed(error)) => {
            *held = None;
            return Phase::Failed(error.kind().clone());
          }
          _ => {}
        }
        let load = match &*held {
          Some((of, load)) if *of == source.cache_key() => load.clone(),
          _ => {
            let Some(app) = ctx.app_handle() else {
              return Phase::Failed(PictureError::Unavailable(Arc::from(
                "the picture is in no app: nothing loads it",
              )));
            };
            let load = app.loader().load(source);
            // One that shows goes before those that wait and do not.
            if self.live.shows.load(std::sync::atomic::Ordering::Relaxed) {
              load.set_priority(crate::resources::LoadPriority::High);
            }
            *held = Some((source.cache_key(), load.clone()));
            load
          }
        };
        // Asked to be told before it is looked at: a load that ends in between is not missed.
        load.wake_with(wake.clone());
        match load.state() {
          LoadState::Loading { loaded, total } => Phase::Loading(total.map(|total| (loaded, total))),
          LoadState::Failed(error) => Phase::Failed(load_failed(error, source.is_remote())),
          LoadState::Ready(bytes) => decode(ImageSource::Bytes(bytes)),
        }
      }
    }
  }

  /// Notes what the app is to be told of this render's find: that the picture came, or that it did not, once
  /// for a source and again for one asked for anew.
  fn tell(&self, picture: &Picture, phase: &Phase) {
    let came = match phase {
      #[cfg(feature = "raster")]
      Phase::Ready(_) => true,
      Phase::Failed(_) => false,
      Phase::Empty | Phase::Loading(_) => return,
    };
    let key = match &picture.source {
      Source::Encoded { source, .. } => source.cache_key(),
      _ => 0,
    };
    let mut told = self.live.told.lock();
    if *told == Some((key, came)) {
      return;
    }
    *told = Some((key, came));
    let mut news = self.live.news.lock();
    match phase {
      Phase::Failed(error) => news.extend(picture.on_error.iter().map(|handler| {
        let (handler, error) = (handler.clone(), error.clone());
        Arc::new(move || handler(error.clone())) as Action
      })),
      _ => news.extend(picture.on_load.iter().cloned()),
    }
  }
}

/// What a load that failed is, as a picture says it: `remote` where it was read from an address, whose reading
/// fails on the network.
#[cfg(feature = "image")]
fn load_failed(error: crate::resources::LoadError, remote: bool) -> PictureError {
  use crate::resources::LoadError;
  match error {
    LoadError::NotFound => PictureError::NotFound,
    LoadError::Denied => PictureError::Denied,
    LoadError::Status(status) => PictureError::Server(status),
    LoadError::TooLarge { .. } => PictureError::TooLarge,
    LoadError::TimedOut => PictureError::TimedOut,
    LoadError::Unsupported(why) => PictureError::Unavailable(Arc::from(why)),
    LoadError::Io(said) if remote => PictureError::Network(Arc::from(said)),
    LoadError::Io(said) => PictureError::Other(Arc::from(said)),
    LoadError::Cancelled => PictureError::Other(Arc::from("the load was stopped")),
  }
}

/// A layer as large as the box it is in.
fn layer() -> Stack {
  Stack::new().size(Dimension::Pct(100.0), Dimension::Pct(100.0))
}

/// Which sides of the picture's box are known before its pixels are: the width, the height.
#[derive(Clone, Copy)]
struct Known {
  width: bool,
  height: bool,
}

impl Known {
  /// A box for what stands in for the pixels: as large as the picture's where that is known, and of the wide
  /// ratio on a side nothing tells.
  fn stand_in(self) -> Stack {
    let full = Dimension::Pct(100.0);
    let stack = Stack::new().stack_align(StackAlignment::Center);
    match (self.width, self.height) {
      (true, true) => stack.size(full, full),
      (true, false) => stack.width(full).aspect_ratio(WIDE),
      (false, true) => stack.height(full).aspect_ratio(WIDE),
      (false, false) => stack,
    }
  }
}

/// One form of a picture as its view holds it: the widths it lives at, and the style of its parts there.
#[derive(Clone)]
pub struct PictureForm {
  pub size: PictureSize,
  /// The widths of the picture at which this form shows; `None` for the one form of a picture, at every width.
  pub bounds: Option<SizeBounds>,
  /// The style of each part in this form.
  pub recipe: ResolvedRecipe,
  /// The style of each part of a button whose menu is open.
  on_recipe: ResolvedRecipe,
}

impl PictureForm {
  fn part(&self, part: PicturePart) -> ResolvedPart {
    self.recipe.part(part)
  }

  /// `layer` as a layer of the picture's box in this form: as large as the box came to be, and laid out only
  /// while the box has a width of this form.
  fn placed(&self, layer: Stack) -> Stack {
    layer.drop_outside(self.bounds.unwrap_or_default())
  }
}

/// What stands in the middle of a box with no picture: its glyph, and what it is called under it.
fn empty_center(form: &PictureForm, label: Option<&Arc<str>>) -> Element {
  let mark = glyph(&form.part(PicturePart::Mark), Name::new(icons::IMAGE), false);
  let mut center = middle(form).child(mark);
  if let (Some(label), PictureSize::Md | PictureSize::Lg) = (label, form.size) {
    center = center.child(wrapped(&form.part(PicturePart::Label), label));
  }
  center.into()
}

/// The column in the middle of the box, in the style of its part.
fn middle(form: &PictureForm) -> Column {
  Column::new()
    .part(&form.part(PicturePart::Center))
    .size(Dimension::Pct(100.0), Dimension::Pct(100.0))
    .align_items(Alignment::Center)
    .justify(Justify::Center)
}

/// `text` in the style of `part`, wrapping in the width it is given and centered in it.
fn wrapped(part: &ResolvedPart, text: &str) -> Element {
  Column::new()
    .width(Dimension::Pct(100.0))
    .align_items(Alignment::Center)
    .child(
      part
        .text(text)
        .text_align(TextAlign::Center)
        .width(Dimension::Pct(100.0))
        .part(part),
    )
    .into()
}

/// The rail of a load that is `progress` of 1 along, in the style of `track` and `fill`.
fn rail(track: &ResolvedPart, fill: &ResolvedPart, progress: f64) -> Row {
  Row::new().part(track).clip().child(
    Row::new()
      .part(fill)
      .size(Dimension::Pct(progress as f32 * 100.0), Dimension::Pct(100.0)),
  )
}

/// Builds the view of `picture` in `theme`, with its bar made, and its tooltips where `ctx` is given.
fn view(
  picture: Picture,
  theme: &Theme,
  live: &Live,
  phase: Phase,
  loader: Option<crate::resources::Loader>,
  mut ctx: Option<&mut Ctx>,
) -> PictureView {
  let pressable = picture.pressable();
  let phone = match picture.variant {
    Some(variant) => variant == Name::from(PictureVariant::Phone),
    // A finger has no hover to bring a bar in by: the person's pointer says which it is.
    None => super::touch_form(),
  };
  // On a phone the bar says what the picture is and holds no buttons: its actions are a long press's.
  let bar_has = picture.title.is_some()
    || picture.description.is_some()
    || (!phone
      && (!picture.actions.is_empty()
        || !picture.on_download.is_empty()
        || !picture.menu_items.is_empty()
        || picture.lightbox));
  let menu_open = live.menu.get();
  let case = |size: PictureSize, on: bool| {
    Case {
      variant: Some(if phone { PictureVariant::Phone } else { PictureVariant::Desktop }.into()),
      size: Some(size.into()),
      density: None,
      breakpoint: super::current_breakpoint(),
      states: picture.states.clone(),
    }
    .state(PictureState::Bare, picture.bare)
    .state(PictureState::Circle, picture.shape == PictureShape::Circle)
    .state(PictureState::Pressable, pressable)
    .state(PictureState::Selectable, picture.selectable)
    .state(PictureState::Selected, picture.selected)
    .state(PictureState::Disabled, picture.disabled)
    // There is no pointer to bring the bar in on a phone: it is there.
    .state(
      PictureState::Shown,
      picture.force_bar_shown.unwrap_or(menu_open || phone) && picture.force_bar_shown != Some(false),
    )
    .state(PictureState::On, on)
  };
  let sizes = picture.forms(theme, live.narrowed.get());
  // Every form names its parts after the picture's id, counted together.
  let owner = sizes.first().map(|(size, _)| {
    theme
      .resolve::<Picture>(&case(*size, false), Some(&picture.rules))
      .owned_by(picture.id.as_ref())
  });
  let forms: Vec<PictureForm> = sizes
    .into_iter()
    .map(|(size, bounds)| {
      let resolve = |on: bool| {
        let recipe = theme.resolve::<Picture>(&case(size, on), Some(&picture.rules));
        match &owner {
          Some(owner) => recipe.owned_like(owner),
          None => recipe,
        }
      };
      PictureForm {
        size,
        bounds,
        recipe: resolve(false),
        on_recipe: resolve(true),
      }
    })
    .collect();
  let id = |suffix: &str| picture.id.as_ref().map(|id| format!("{id}-{suffix}"));

  // The bar's own buttons, once for every form that has them: an element is built for one place.
  let mut bars = Vec::with_capacity(forms.len());
  for form in &forms {
    let shown = picture.bar && bar_has && picture.force_bar_shown != Some(false);
    if !shown || matches!(form.size, PictureSize::Xs | PictureSize::Sm) {
      bars.push(None);
      continue;
    }
    let large = form.size == PictureSize::Lg;
    let mut buttons: Vec<Element> = Vec::new();
    if large && !phone {
      buttons.extend(picture.actions.iter().cloned());
      if !picture.on_download.is_empty() {
        let handlers = picture.on_download.clone();
        let press: Action = Arc::new(move || handlers.iter().for_each(|handler| handler()));
        let button = control(
          form,
          false,
          icons::DOWNLOAD,
          &picture.words.download,
          id("download"),
          press,
        );
        buttons.push(tip(ctx.as_deref_mut(), button, &picture.words.download));
      }
    }
    // A compact picture keeps the menu's button alone: what its other buttons do is in the menu.
    let mut menu = None;
    if !picture.menu_items.is_empty() && !phone {
      let open = live.menu.clone();
      let press: Action = Arc::new(move || open.set(!open.get_untracked()));
      let button = control(
        form,
        menu_open,
        icons::MORE_HORIZONTAL,
        &picture.words.more,
        id("more"),
        press,
      );
      let button = Row::new().ref_element(live.more.clone()).child(button);
      buttons.push(tip(ctx.as_deref_mut(), button.into(), &picture.words.more));
      menu = Some(
        Menu::new(live.menu.clone(), live.more.clone())
          .label(&picture.words.more)
          .placement(Placement::TopEnd)
          .offset(4.0)
          .rules(&form.recipe.rules_for(PicturePart::MoreMenu, super::MenuPart::Panel))
          .items(picture.menu_items.iter().cloned())
          .build(theme),
      );
    }
    // Open larger is the last, where a video has fullscreen.
    if large && picture.lightbox && !phone {
      let open = picture.open.clone().unwrap_or_else(|| live.viewer.clone());
      let told = picture.on_open_change.clone();
      let press: Action = Arc::new(move || {
        if !open.get_untracked() {
          open.set(true);
          told.iter().for_each(|tell| tell(true));
        }
      });
      let button = control(form, false, icons::FULLSCREEN, &picture.words.open, id("open"), press);
      buttons.push(tip(ctx.as_deref_mut(), button, &picture.words.open));
    }
    bars.push(Some(PictureBar {
      title: picture.title.clone(),
      description: picture.description.clone().filter(|_| large),
      buttons,
      menu,
    }));
  }

  // Asked for again: the app is told, the pixels' cache and the loader forget that it failed, the load that
  // was held is let go, and the picture renders again and asks anew.
  let retry: Action = {
    let handlers = picture.on_retry.clone();
    let (source, live) = (picture.source.clone(), live.clone());
    Arc::new(move || {
      handlers.iter().for_each(|handler| handler());
      #[cfg(feature = "image")]
      if let Source::Encoded { source, cap } = &source {
        let cap = cap.map(|(width, height)| crate::images::ImageSizeCap::new(width, height));
        crate::images::decoded_images().forget(crate::images::ImageKey::new(source.cache_key()), cap);
        if let Some(loader) = &loader {
          loader.forget(source);
        }
      }
      #[cfg(not(feature = "image"))]
      let _ = (&source, &loader);
      *live.held.lock() = None;
      *live.told.lock() = None;
      (live.wake)();
    })
  };

  PictureView {
    phase,
    retry,
    viewer: picture.open.clone().unwrap_or_else(|| live.viewer.clone()),
    phone,
    sheet: live.sheet.clone(),
    #[cfg(feature = "raster")]
    small: None,
    known: Known {
      width: picture.width.is_some() || picture.aspect_ratio.is_some(),
      height: picture.height.is_some() || picture.aspect_ratio.is_some(),
    },
    pressable,
    forms,
    bars,
    picture,
    frame: live.frame.clone(),
  }
}

/// A button of the bar: its glyph and its name, and on where its menu is open.
fn control(
  form: &PictureForm,
  on: bool,
  icon: &'static str,
  label: &str,
  id: Option<String>,
  press: Action,
) -> Element {
  let recipe = if on { &form.on_recipe } else { &form.recipe };
  let mut button = pressable(&recipe.part(PicturePart::Control), false)
    .describe("a11y_role", "button")
    .describe("a11y_name", Arc::<str>::from(label))
    .child(glyph(&recipe.part(PicturePart::ControlIcon), Name::new(icon), false))
    .on_click(move |event: MouseEvent| {
      // The press is the button's, not the picture's it lies over.
      event.stop_propagation();
      press();
    });
  if let Some(id) = id {
    button = button.id(id);
  }
  button.into()
}

/// `element` with a tooltip of `text` over it, where the picture is mounted.
fn tip(ctx: Option<&mut Ctx>, element: Element, text: &str) -> Element {
  match ctx {
    Some(ctx) => Tooltip::new(text)
      .placement(Placement::Top)
      .arrow(true)
      .offset(4.0)
      .mount(ctx, element),
    None => element,
  }
}

/// The bar of a picture in one of its forms: what it says and its buttons, made.
#[derive(Clone)]
pub struct PictureBar {
  pub title: Option<Arc<str>>,
  /// The line under the title; a compact picture has none.
  pub description: Option<Arc<str>>,
  /// The buttons at the bar's end, in order: the app's, download, More, open.
  pub buttons: Vec<Element>,
  /// The menu behind More, which takes no room, where the picture is mounted.
  pub menu: Option<Element>,
}

/// A picture as a renderer sees it: what it was given, and each of its forms with the style of its parts.
pub struct PictureView {
  /// The forms it is built in: one, or all of them for layout to show the one that fits its width.
  pub forms: Vec<PictureForm>,
  /// The bar of each form, where that form has one.
  pub bars: Vec<Option<PictureBar>>,
  /// Whether a press of it as a whole does something.
  pub pressable: bool,
  known: Known,
  picture: Picture,
  /// Where its pixels are.
  phase: Phase,
  /// Whether its lightbox is open: the app's signal, or the picture's own.
  viewer: Signal<bool>,
  /// The pixels of the small version that stands in for it, blurred, while it is on its way.
  #[cfg(feature = "raster")]
  small: Option<crate::images::ImageData>,
  /// Asks for the picture again.
  retry: Action,
  /// Whether it is the phone's form, and whether the sheet of its actions is open there.
  pub phone: bool,
  sheet: Signal<bool>,
  /// The box as it is laid out.
  frame: ElementRef,
}

impl PictureView {
  /// The form whose style the box itself takes: the only one, or the large one.
  fn base(&self) -> &PictureForm {
    self.forms.last().expect("a picture has a form")
  }

  /// The style of a part of the box, as this picture is to be drawn.
  pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
    self.base().recipe.part(part)
  }

  pub fn theme(&self) -> &Theme {
    self.base().recipe.theme()
  }

  /// What the picture is called to tooling that reads the tree, if it is anything to it.
  pub fn label(&self) -> Option<Arc<str>> {
    (!self.picture.decorative)
      .then(|| self.picture.label.clone().or_else(|| self.picture.title.clone()))
      .flatten()
  }

  fn id(&self, suffix: &str) -> Option<String> {
    self.picture.id.as_ref().map(|id| format!("{id}-{suffix}"))
  }

  fn render_themed(self) -> Element {
    match self.theme().renderer::<Picture>() {
      Some(renderer) => renderer(self),
      None => self.render(),
    }
  }

  /// What stands in for the pixels while they are on their way, for each time it is shown: the sheen, or how
  /// far the load has come where the app says.
  fn loading_maker(&self) -> impl Fn(Option<(u64, u64)>) -> Element + Send + Sync + 'static {
    let (forms, known) = (self.forms.clone(), self.known);
    let said = self.picture.progress;
    let sheen = matches!(self.picture.placeholder, PicturePlaceholder::Skeleton);
    let words = self.picture.progress_text.clone();
    // The small version, blurred, in the place the pixels will have: over the whole box, under no rail.
    #[cfg(feature = "raster")]
    let blurred = self.small.clone().map(|data| {
      let (fit, (x, y)) = (fit_of(self.picture.fit), anchor_shares(self.picture.anchor));
      let part = self.part(PicturePart::Image);
      move || -> Element {
        #[allow(deprecated)]
        let image = crate::components::Image::new(data.clone())
          .fit(fit)
          .image_anchor(x, y)
          .part(&part)
          .size(Dimension::Pct(100.0), Dimension::Pct(100.0))
          .blur(BLUR)
          .transform(crate::node::transform::Transform2D::scale_uniform(BLUR_OVERDRAW));
        layer().child(image).into()
      }
    });
    move |bytes: Option<(u64, u64)>| {
      // What the app says of the load comes before what the loader knows.
      let load = match (said, bytes) {
        (Some(fraction), _) => Some(PictureLoad { fraction, bytes }),
        (None, Some((loaded, total))) if total > 0 => Some(PictureLoad {
          fraction: (loaded as f64 / total as f64).clamp(0.0, 1.0),
          bytes,
        }),
        _ => None,
      };
      let progress = load.map(|load| load.fraction);
      let text = match (load, &words) {
        (Some(load), Some(words)) => words(load),
        (Some(load), None) => load.text(),
        (None, _) => String::new(),
      };
      let mut stand_in = known.stand_in();
      #[cfg(feature = "raster")]
      if let Some(blurred) = &blurred {
        return stand_in.child(blurred()).into();
      }
      for form in &forms {
        let content: Option<Element> = match (progress, form.size) {
          (Some(progress), PictureSize::Lg | PictureSize::Md) => {
            let mut block = Column::new()
              .part(&form.part(PicturePart::Progress))
              .align_items(Alignment::Center)
              .child(rail(
                &form.part(PicturePart::Track),
                &form.part(PicturePart::Fill),
                progress,
              ));
            if form.size == PictureSize::Lg {
              block = block.child(form.part(PicturePart::ProgressLabel).text(&text).nowrap());
            }
            Some(middle(form).child(block).into())
          }
          (Some(progress), PictureSize::Sm) => Some(
            Column::new()
              .size(Dimension::Pct(100.0), Dimension::Pct(100.0))
              .justify(Justify::End)
              .child(rail(
                &form.part(PicturePart::ProgressLine),
                &form.part(PicturePart::Fill),
                progress,
              ))
              .into(),
          ),
          // As small as an avatar, the box alone says it loads.
          (_, PictureSize::Xs) => None,
          (None, _) if sheen => Some(
            Skeleton::block(Length::Fill, Length::Fill)
              .animation(SkeletonAnimation::Shimmer)
              .rules(&form.recipe.rules_for(PicturePart::Sheen, SkeletonPart::Bone))
              .build(form.recipe.theme()),
          ),
          (None, _) => None,
        };
        if let Some(content) = content {
          stand_in = stand_in.child(form.placed(layer().child(content)));
        }
      }
      stand_in.into()
    }
  }

  /// What stands in for a picture that did not load: its glyph, what it is called, that it did not load, and
  /// the button that asks for it again. A thumbnail is the glyph alone, and a press of it asks again.
  fn error_maker(&self) -> impl Fn() -> Element + Send + Sync + 'static {
    let (forms, known) = (self.forms.clone(), self.known);
    let (label, words) = (self.picture.label.clone(), self.picture.words.clone());
    // A build with no decoder shows what it has to decode as failed, and says why: here and once in the log.
    #[cfg(not(feature = "image"))]
    let words = {
      static SAID: std::sync::Once = std::sync::Once::new();
      SAID.call_once(|| tracing::warn!("{NO_CODECS}"));
      Words {
        error_description: Arc::from(NO_CODECS),
        ..words
      }
    };
    let retry_id = self.id("retry");
    let retry = self.retry.clone();
    move || {
      let mut stand_in = known.stand_in();
      for form in &forms {
        let icon = glyph(
          &form.part(PicturePart::ErrorIcon),
          Name::new(icons::ALERT_CIRCLE),
          false,
        );
        let content: Element = match form.size {
          PictureSize::Lg | PictureSize::Md => {
            let mut text = Column::new()
              .width(Dimension::Pct(100.0))
              .align_items(Alignment::Center);
            if let Some(label) = &label {
              text = text.child(wrapped(&form.part(PicturePart::ErrorLabel), label));
            }
            if form.size == PictureSize::Lg {
              text = text.child(wrapped(
                &form.part(PicturePart::ErrorDescription),
                &words.error_description,
              ));
            }
            let part = form.part(PicturePart::Retry);
            let press = retry.clone();
            let mut button = pressable(&part, false)
              .describe("a11y_role", "button")
              .describe("a11y_name", words.retry.clone())
              .child(part.text(&words.retry).nowrap())
              .on_click(move |event: MouseEvent| {
                event.stop_propagation();
                press();
              });
            if let Some(id) = &retry_id {
              button = button.id(id.clone());
            }
            middle(form).child(icon).child(text).child(button).into()
          }
          // A thumbnail is too small for words: the glyph, pressed to ask again, and its name says why.
          PictureSize::Sm | PictureSize::Xs => {
            let press = retry.clone();
            let name = match &label {
              Some(label) => format!("{label}. {} {}", words.error_description, words.retry),
              None => format!("{} {}", words.error_description, words.retry),
            };
            middle(form)
              .describe("a11y_role", "button")
              .describe("a11y_name", name)
              .cursor(CursorIcon::Pointer)
              .on_click(move |event: MouseEvent| {
                event.stop_propagation();
                press();
              })
              .child(icon)
              .into()
          }
        };
        stand_in = stand_in.child(form.placed(layer().child(content)));
      }
      stand_in.into()
    }
  }

  /// The pixels, placed in the box as the fit and the anchor say. Nothing while they are not there.
  #[allow(deprecated)]
  pub fn image(&self) -> Option<Element> {
    #[cfg(not(feature = "raster"))]
    return None;
    #[cfg(feature = "raster")]
    {
      use crate::components::Image;
      let Phase::Ready(data) = &self.phase else {
        return None;
      };
      let (x, y) = anchor_shares(self.picture.anchor);
      let image = Image::new(data.clone())
        .fit(fit_of(self.picture.fit))
        .image_anchor(x, y)
        .part(&self.part(PicturePart::Image));
      let full = Dimension::Pct(100.0);
      // As large as the box on the sides the box knows; on the others the pixels say.
      Some(match (self.known.width, self.known.height) {
        (true, true) => image.size(full, full).into(),
        (true, false) => image.width(full).into(),
        (false, true) => image.height(full).into(),
        (false, false) => image.into(),
      })
    }
  }

  /// What a box with no source shows: in each form, the glyph and what the picture would be called.
  pub fn empty(&self) -> Element {
    let mut stand_in = self.known.stand_in();
    for form in &self.forms {
      let content = empty_center(form, self.picture.label.as_ref());
      stand_in = stand_in.child(form.placed(layer().child(content)));
    }
    stand_in.into()
  }

  /// The veil and the count over it, where the picture stands for more of them.
  pub fn count(&self) -> Vec<Element> {
    let Some(count) = &self.picture.count else {
      return Vec::new();
    };
    let mut layers = vec![
      layer()
        .part(&self.part(PicturePart::Veil))
        .drop_outside(SizeBounds::new())
        .pointer_events_none()
        .into(),
    ];
    for form in self.forms.iter().filter(|form| form.size != PictureSize::Xs) {
      let text = form.part(PicturePart::Count).text(count).nowrap();
      layers.push(
        form
          .placed(layer().pointer_events_none().child(middle(form).child(text)))
          .into(),
      );
    }
    layers
  }

  /// The app's layer over the picture.
  pub fn overlay(&self) -> Option<Element> {
    if self.picture.children.is_empty() {
      return None;
    }
    let mut overlay = layer()
      .part(&self.part(PicturePart::Overlay))
      .drop_outside(SizeBounds::new())
      .hit_test(HitTestBehavior::ContentOnly)
      .with_children(self.picture.children.iter().cloned());
    if let Some(id) = self.id("overlay") {
      overlay = overlay.id(id);
    }
    Some(overlay.into())
  }

  /// The row along the top of each form that has one: the badge at its start, the select mark at its end.
  pub fn top(&self) -> Vec<Element> {
    let picture = &self.picture;
    let marked = picture.selectable || picture.selected;
    let mut rows = Vec::new();
    for form in &self.forms {
      let badge = picture
        .badge
        .clone()
        .filter(|_| matches!(form.size, PictureSize::Lg | PictureSize::Md));
      let mark = (marked && form.size != PictureSize::Xs).then(|| {
        let mut mark = Row::new()
          .part(&form.part(PicturePart::Select))
          .align_items(Alignment::Center)
          .justify(Justify::Center);
        if picture.selected {
          mark = mark.child(glyph(
            &form.part(PicturePart::SelectIcon),
            Name::new(icons::CHECK),
            false,
          ));
        }
        mark
      });
      if badge.is_none() && mark.is_none() {
        continue;
      }
      let mut row = Row::new()
        .part(&form.part(PicturePart::Top))
        .width(Dimension::Pct(100.0))
        .align_items(Alignment::Start);
      if let Some(badge) = badge {
        row = row.child(badge);
      }
      row = row.child(Row::new().flex(1.0));
      if let Some(mark) = mark {
        row = row.child(mark);
      }
      rows.push(
        form
          .placed(
            layer()
              .stack_align(StackAlignment::TopStart)
              .pointer_events_none()
              .child(row),
          )
          .into(),
      );
    }
    rows
  }

  /// The bar along the bottom of each form that has one: the title and the description at its start, the
  /// buttons at its end, on a gradient.
  pub fn controls(&self) -> Vec<Element> {
    let mut bars = Vec::new();
    for (form, bar) in self.forms.iter().zip(&self.bars) {
      let Some(bar) = bar else {
        continue;
      };
      let part = form.part(PicturePart::Controls);
      let end = part
        .rest()
        .background
        .clone()
        .unwrap_or(crate::node::BackgroundColor::Color(Color::new(0, 0, 0, 0xB3)));
      let clear = crate::node::BackgroundColor::Color(Color::new(0, 0, 0, 0));
      let mut text = Column::new()
        .part(&form.part(PicturePart::Text))
        .flex(1.0)
        .min_width(0.0);
      if let Some(title) = &bar.title {
        let part = form.part(PicturePart::Title);
        text = text.child(part.text(title).width(Dimension::Pct(100.0)).part(&part));
      }
      if let Some(description) = &bar.description {
        let part = form.part(PicturePart::Description);
        text = text.child(part.text(description).width(Dimension::Pct(100.0)).part(&part));
      }
      let buttons = Row::new()
        .part(&form.part(PicturePart::Actions))
        .align_items(Alignment::Center)
        .overflow_visible()
        .with_children(bar.buttons.iter().cloned());
      let mut controls = Row::new()
        .part(&part)
        .width(Dimension::Pct(100.0))
        .align_items(Alignment::End)
        .overflow_visible()
        .background_gradient(crate::node::Gradient::linear(
          180.0,
          [(clear, 0.0), (end, 1.0)].map(|(color, at)| crate::node::gradient::GradientStop::at(color, at)),
        ))
        .child(text)
        .child(buttons);
      if let Some(menu) = &bar.menu {
        controls = controls.child(menu.clone());
      }
      if let Some(id) = self.id("controls") {
        controls = controls.id(id);
      }
      bars.push(
        form
          .placed(
            layer()
              .stack_align(StackAlignment::BottomStart)
              .hit_test(HitTestBehavior::ContentOnly)
              .child(controls),
          )
          .into(),
      );
    }
    bars
  }

  /// On a phone, what a long press of the picture offers in a sheet: Download, where it is given, and the app's
  /// items of the menu. Nothing elsewhere: a pointer has the bar's buttons.
  pub fn actions(&self) -> Vec<MenuItem> {
    let picture = &self.picture;
    if !self.phone || picture.disabled {
      return Vec::new();
    }
    let mut items = Vec::new();
    if !picture.on_download.is_empty() {
      let handlers = picture.on_download.clone();
      items.push(
        MenuItem::new(&picture.words.download)
          .icon(icons::DOWNLOAD)
          .on_select(move || handlers.iter().for_each(|handler| handler())),
      );
    }
    items.extend(picture.menu_items.iter().cloned());
    items
  }

  /// The line around the box, over what is in it. A bare picture has none.
  pub fn edge(&self) -> Option<Element> {
    // A large picture on a phone runs edge to edge: nothing marks where it ends.
    let edge_to_edge = self.phone && self.base().size == PictureSize::Lg;
    (!self.picture.bare && !edge_to_edge).then(|| {
      layer()
        .part(&self.part(PicturePart::Edge))
        .drop_outside(SizeBounds::new())
        .pointer_events_none()
        .into()
    })
  }

  /// What a press of the picture as a whole does: tells the app's handlers, and what it is to be where it can
  /// be chosen.
  fn press(&self) -> impl Fn(MouseEvent) + Send + Sync + 'static {
    let (clicks, changes) = (self.picture.on_click.clone(), self.picture.on_change.clone());
    let (selectable, next) = (self.picture.selectable, !self.picture.selected);
    // A picture that is chosen by a press opens nothing by it.
    let opens =
      (self.picture.lightbox && !selectable).then(|| (self.viewer.clone(), self.picture.on_open_change.clone()));
    move |event: MouseEvent| {
      for handler in &clicks {
        handler.call(&event);
      }
      if selectable {
        changes.iter().for_each(|handler| handler(next));
      }
      if let Some((open, told)) = &opens
        && !open.get_untracked()
      {
        open.set(true);
        told.iter().for_each(|tell| tell(true));
      }
    }
  }

  /// The picture's box, with everything in it: the pixels or what stands in for them, the app's layer, the
  /// veil and the count, the top row, the bar and the line around it.
  pub fn frame(&self) -> Element {
    let picture = &self.picture;
    let theme = self.theme();
    let full = Dimension::Pct(100.0);
    let mut inner = Stack::new().stack_align(StackAlignment::Center);
    // The stack is as large as the box where the box says a size, and as the pixels where it does not.
    if self.known.width {
      inner = inner.width(full);
    }
    if self.known.height {
      inner = inner.height(full);
    }
    // A box whose size does not wait for the pixels lets what stood in for them go out over them: it is the
    // second child, kept while it leaves, and the pixels are the first, under it. A box the pixels size has
    // one child, which is gone when they come.
    let fades = self.known.width && self.known.height;
    let waiting = |stand_in: Element| -> Element {
      if !fades {
        return stand_in;
      }
      Stack::new()
        .size(full, full)
        .key("stand-in")
        .part(&self.part(PicturePart::StandIn))
        .child(stand_in)
        .into()
    };
    let (pixels, over) = match (&self.phase, self.image()) {
      (_, Some(image)) if !picture.loading => (Some(image), None),
      // What stands there in place of the picture's own stand-ins, whatever the reason there are no pixels.
      _ if picture.fallback.is_some() => (
        None,
        picture.fallback.as_ref().map(|fallback| {
          // In the middle of the box, as what it stands in for would be.
          let centered = Stack::new()
            .stack_align(StackAlignment::Center)
            .size(full, full)
            .child(fallback());
          waiting(centered.into())
        }),
      ),
      // What the app is still fetching itself: the stand-in of a load, with no pixels to come by themselves.
      _ if picture.loading => (None, Some(waiting(self.loading_maker()(None)))),
      (_, Some(image)) => (Some(image), None),
      (Phase::Loading(bytes), _) => (None, Some(waiting(self.loading_maker()(*bytes)))),
      (Phase::Failed(_), _) => (None, Some(self.error_maker()())),
      _ => (None, Some(self.empty())),
    };
    inner = if fades {
      inner.presence().child(pixels.unwrap_or_default()).with_children(over)
    } else {
      inner.with_children(pixels.or(over))
    };
    if let Some(overlay) = self.overlay() {
      inner = inner.child(overlay);
    }
    inner = inner
      .with_children(self.count())
      .with_children(self.top())
      .with_children(self.controls());
    if let Some(edge) = self.edge() {
      inner = inner.child(edge);
    }
    // On a phone, its actions, which a long press brings.
    let actions = self.actions();
    let actions_empty = actions.is_empty();
    if !actions.is_empty() {
      let mut sheet = super::ActionSheet::new(self.sheet.clone(), actions).key("actions");
      if let Some(title) = picture.title.clone().or_else(|| self.label()) {
        sheet = sheet.title(&title);
      }
      if let Some(id) = self.id("actions") {
        sheet = sheet.id(id);
      }
      inner = inner.child(sheet);
    }
    // Its lightbox, which takes no room: the picture as the app gave it, alone.
    if picture.lightbox {
      let told = picture.on_open_change.clone();
      let mut viewer = super::Lightbox::new(self.viewer.clone(), [picture.clone()])
        .on_dismiss(move || told.iter().for_each(|tell| tell(false)))
        .key("viewer");
      if let Some(label) = self.label() {
        viewer = viewer.label(&label);
      }
      if let Some(id) = self.id("viewer") {
        viewer = viewer.id(id);
      }
      inner = inner.child(viewer);
    }

    let part = self.part(PicturePart::Root).forced(picture.forced);
    let role = match (picture.selectable, self.pressable) {
      (true, _) => "checkbox",
      (false, true) => "button",
      (false, false) => "img",
    };
    // The box is what the pointer and the focus are in: the bar and the ring of the mark show by it.
    let mut root: Element = if self.pressable {
      pressable(&part, false)
        .clip()
        .interaction_group(true)
        .on_click(self.press())
        .child(inner)
        .into()
    } else {
      Row::new()
        .part(&part)
        .clip()
        .interaction_group(true)
        .disabled(picture.disabled)
        .child(inner)
        .into()
    };
    NodeUpdate::ref_element(&mut *root.node, self.frame.clone());
    if !actions_empty {
      let sheet = self.sheet.clone();
      NodeUpdate::on_long_press(&mut *root.node, move |_: crate::app::events::LongPressEvent| {
        sheet.set(true)
      });
    }
    let node = &mut *root.node;
    let width = picture.width.and_then(|width| width.resolve(theme));
    let height = picture.height.and_then(|height| height.resolve(theme));
    if let Some(width) = width {
      NodeUpdate::width(node, width);
      // The width said is the most the box takes: in a room narrower than it the box is as wide as the room.
      NodeUpdate::max_width(node, Dimension::Pct(100.0));
    }
    match (width, height, picture.aspect_ratio) {
      // A box of so many pixels by so many keeps its shape where the room makes it narrower: its height follows
      // its width. The ratio is a hair under the box's own and the height said is the most, so that at its
      // full width the box is as high as said, and not a rounding of it.
      (Some(Dimension::Px(wide)), Some(Dimension::Px(high)), None) if wide > 0.0 && high > 0.0 => {
        NodeUpdate::aspect_ratio(node, wide / high * (1.0 - 4.0 * f32::EPSILON));
        NodeUpdate::max_height(node, high);
      }
      (width, height, ratio) => {
        if let Some(height) = height {
          NodeUpdate::height(node, height);
        }
        if let Some(ratio) = ratio {
          NodeUpdate::aspect_ratio(node, ratio);
          // A ratio with no side said is as wide as its room. A column stretches its child to that by itself; a
          // row does not, and the box there was as wide as its pixels: nothing, while they are not loaded.
          if width.is_none() && height.is_none() {
            NodeUpdate::width(node, Dimension::Pct(100.0));
          }
        }
      }
    }
    match self.label() {
      Some(label) => {
        NodeUpdate::describe(node, Arc::from("a11y_role"), Arc::from(role));
        NodeUpdate::describe(node, Arc::from("a11y_name"), label);
      }
      None if role != "img" => NodeUpdate::describe(node, Arc::from("a11y_role"), Arc::from(role)),
      None => {}
    }
    if picture.selectable {
      let checked = if picture.selected { "true" } else { "false" };
      NodeUpdate::describe(node, Arc::from("a11y_checked"), Arc::from(checked));
    }
    if let Some(id) = &picture.id {
      NodeUpdate::id(node, id.clone());
    }
    // A figure is keyed as a whole: its box is then one child of it among two.
    if let (Some(key), None) = (&picture.key, &picture.caption) {
      node.set_component_key(Some(key));
    }
    root
  }

  /// How the default renderer builds a picture: its box, and its caption under it where it has one.
  pub fn render(self) -> Element {
    let frame = self.frame();
    let Some(caption) = &self.picture.caption else {
      return frame;
    };
    let part = self.part(PicturePart::Caption);
    let mut figure = Column::new()
      .part(&self.part(PicturePart::Figure))
      .align_items(Alignment::Center)
      .describe("a11y_role", "figure")
      .child(frame)
      .child(wrapped(&part, caption));
    if let Some(width) = self.picture.width.and_then(|width| width.resolve(self.theme())) {
      figure = figure.width(width).max_width(Dimension::Pct(100.0));
    }
    if let Some(key) = &self.picture.key {
      figure = figure.key(key.clone());
    }
    figure.into()
  }
}

/// How the engine fills a box with pixels for `fit`.
#[cfg(feature = "raster")]
fn fit_of(fit: PictureFit) -> crate::node::BackgroundSize {
  use crate::node::BackgroundSize;
  match fit {
    PictureFit::Cover => BackgroundSize::Cover,
    PictureFit::Contain => BackgroundSize::Contain,
    PictureFit::Fill => BackgroundSize::Stretch,
    PictureFit::None => BackgroundSize::Natural,
  }
}

/// Where `anchor` holds a picture in its box, as shares of the room across and down.
#[cfg(feature = "raster")]
fn anchor_shares(anchor: StackAlignment) -> (f32, f32) {
  use StackAlignment::*;
  match anchor {
    TopStart => (0.0, 0.0),
    TopCenter => (0.5, 0.0),
    TopEnd => (1.0, 0.0),
    CenterStart => (0.0, 0.5),
    Center => (0.5, 0.5),
    CenterEnd => (1.0, 0.5),
    BottomStart => (0.0, 1.0),
    BottomCenter => (0.5, 1.0),
    BottomEnd => (1.0, 1.0),
  }
}

impl crate::ui::Styled for Picture {
  type Component = Self;

  fn rules(self, rules: &crate::ui::Recipe) -> Self {
    Self::rules(self, rules)
  }

  fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
    Self::state(self, state, on)
  }
}

impl Themed for Picture {
  const NAME: &'static str = "picture";
  type Renderer = PictureRenderer;

  fn parts() -> crate::ui::Declared {
    PicturePart::declared()
  }

  fn states() -> crate::ui::Declared {
    PictureState::declared()
  }

  /// The design's picture (`pencil/lurq-design-system.pen`, Picture): a box in the muted fill with the corners
  /// of a surface and a line of the border's color around it, over the pixels. Over the picture everything is
  /// white on black in both themes, as on the video: a bar along the bottom on a gradient to 70% black, 24 above
  /// its row, 12 beside it and 8 under it, with a title of 14 medium, a line of 13 at 60% white, and buttons of
  /// 36 with glyphs of 20; a compact one has 20, 8 and 6 around a title of 13 and a button of 28 with a glyph of
  /// 16. The box with no picture, the error, the rail of a load and the mark follow the theme: the error is a
  /// glyph of 24, the picture's name at 14 medium, a line of 13 and an outlined button of 32; the rail is 160 by
  /// 4 in the input's color with the primary on it; the mark is a disc of 20 in the primary color, 12 from the
  /// top and the end, and 16 at 6 on a thumbnail. A chosen picture has a line of 2 in the primary color outside
  /// it; the focus draws the ring.
  fn recipe() -> Recipe {
    use PaletteColor::{Background, Foreground, Input, Muted, MutedForeground, Primary, PrimaryForeground, StateHover};
    use PicturePart::*;
    use PictureSize::{Md, Sm, Xs};
    use PictureState::{Bare, Circle, Disabled, On, Pressable, Selectable, Selected, Shown};
    let white = Color::new(255, 255, 255, 255);
    let white_60 = Color::new(255, 255, 255, 0x99);
    let white_20 = Color::new(255, 255, 255, 0x33);
    let white_12 = Color::new(255, 255, 255, 0x1F);
    let black_40 = Color::new(0, 0, 0, 0x66);
    let black_60 = Color::new(0, 0, 0, 0x99);
    let black_70 = Color::new(0, 0, 0, 0xB3);
    let appear = |s: super::PartStyle| {
      s.transition(PartTransition::new(Transition::opacity(), MotionDuration::Base).easing(MotionEasing::Enter))
    };
    Recipe::new()
      .base(Figure, |s| s.gap(8.0))
      .base(Root, |s| s.radius(RadiusSize::Lg).background(Muted))
      .size(Sm, Root, |s| s.radius(RadiusSize::Md))
      .size(Xs, Root, |s| s.radius(RadiusSize::Md))
      .state(Circle, Root, |s| s.radius(RadiusSize::Full))
      .state(Bare, Root, |s| s.radius(0.0).background(Color::new(0, 0, 0, 0)))
      .state(Pressable, Root, |s| s.cursor(CursorIcon::Pointer))
      .state(Selected, Root, |s| s.outline(Outline::new(2.0, Primary)))
      .state(Disabled, Root, |s| {
        s.opacity(OpacityLevel::Disabled).cursor(CursorIcon::Default)
      })
      .when(Selector::new().state(Pressable).focus_visible(), Root, |s| {
        s.focus_ring()
      })
      .base(Edge, |s| {
        s.radius(RadiusSize::Lg)
          .border(Border::inside(1.0, PaletteColor::Border))
      })
      .size(Sm, Edge, |s| s.radius(RadiusSize::Md))
      .size(Xs, Edge, |s| s.radius(RadiusSize::Md))
      .state(Circle, Edge, |s| s.radius(RadiusSize::Full))
      .base(StandIn, |s| {
        s.exit(PartAnimation::new(
          super::FADE_OUT,
          MotionDuration::Base,
          MotionEasing::Exit,
        ))
      })
      .base(Sheen, |s| s.radius(0.0))
      .base(Veil, |s| s.background(black_60))
      // What stands in the middle is centered, so the room over and under it is the least it keeps: a box only
      // just higher than what it says shows all of it.
      .base(Center, |s| s.padding_x(24.0).padding_y(8.0).gap(12.0))
      .size(Md, Center, |s| s.padding_x(12.0).padding_y(12.0).gap(8.0))
      .size(Sm, Center, |s| s.padding_x(8.0).padding_y(8.0))
      .size(Xs, Center, |s| s.padding_x(0.0).padding_y(0.0))
      .base(Mark, |s| s.square(28.0).foreground(MutedForeground))
      .size(Md, Mark, |s| s.square(24.0))
      .size(Sm, Mark, |s| s.square(20.0))
      .size(Xs, Mark, |s| s.square(16.0))
      .base(Label, |s| {
        s.typography(TypographyStyle::Body)
          .font_size(13.0)
          .line_height(1.4)
          .foreground(MutedForeground)
      })
      .base(ErrorIcon, |s| s.square(24.0).foreground(MutedForeground))
      .size(Sm, ErrorIcon, |s| s.square(20.0))
      .size(Xs, ErrorIcon, |s| s.square(16.0))
      .base(ErrorLabel, |s| {
        s.typography(TypographyStyle::Body)
          .font_size(14.0)
          .weight(FontWeight::Medium)
          .line_height(1.4)
          .foreground(Foreground)
          .padding_bottom(4.0)
      })
      .size(Md, ErrorLabel, |s| s.font_size(13.0).padding_bottom(0.0))
      .base(ErrorDescription, |s| {
        s.typography(TypographyStyle::Body)
          .font_size(13.0)
          .foreground(MutedForeground)
      })
      .base(Retry, |s| {
        fading(
          s.height(32.0)
            .padding_x(16.0)
            .radius(RadiusSize::Md)
            .background(Background)
            .border(Border::inside(1.0, Input))
            .foreground(Foreground)
            .typography(TypographyStyle::Body)
            .font_size(13.0)
            .weight(FontWeight::Medium)
            .cursor(CursorIcon::Pointer),
        )
      })
      .size(Md, Retry, |s| s.height(28.0).padding_x(12.0).font_size(12.0))
      .hovered(Retry, |s| s.veil(StateHover))
      .focus_visible(Retry, |s| s.focus_ring())
      .base(Progress, |s| s.gap(8.0))
      .base(Track, |s| {
        s.width(160.0).height(4.0).radius(RadiusSize::Full).background(Input)
      })
      .size(Md, Track, |s| s.width(96.0))
      .base(Fill, |s| s.background(Primary))
      .base(ProgressLabel, |s| {
        s.typography(TypographyStyle::Mono)
          .font_size(11.0)
          .foreground(MutedForeground)
      })
      .base(ProgressLine, |s| s.width(Length::Fill).height(2.0).background(Input))
      .base(Count, |s| {
        s.typography(TypographyStyle::Body)
          .font_size(20.0)
          .weight(FontWeight::SemiBold)
          .foreground(white)
      })
      .size(Sm, Count, |s| s.font_size(13.0))
      .base(Top, |s| s.padding_x(12.0).padding_y(12.0))
      .size(Sm, Top, |s| s.padding_x(6.0).padding_y(6.0))
      .base(Select, |s| {
        appear(
          s.square(20.0)
            .radius(RadiusSize::Full)
            .background(black_40)
            .border(Border::inside(1.5, white))
            .opacity(0.0),
        )
      })
      .size(Sm, Select, |s| s.square(16.0))
      .when(Selector::new().state(Selectable).within(), Select, |s| s.opacity(1.0))
      .when(Selector::new().state(Selectable).state(Shown), Select, |s| {
        s.opacity(1.0)
      })
      .state(Selected, Select, |s| {
        s.background(Primary)
          .border(Border::inside(1.5, PrimaryForeground))
          .opacity(1.0)
      })
      .base(SelectIcon, |s| s.square(12.0).foreground(PrimaryForeground))
      .size(Sm, SelectIcon, |s| s.square(10.0))
      .base(Controls, |s| {
        appear(
          s.padding_top(24.0)
            .padding_x(12.0)
            .padding_bottom(8.0)
            .gap(12.0)
            .background(black_70)
            .opacity(0.0),
        )
      })
      .size(Md, Controls, |s| {
        s.padding_top(20.0).padding_x(8.0).padding_bottom(6.0).gap(8.0)
      })
      .within(Controls, |s| s.opacity(1.0))
      .state(Shown, Controls, |s| s.opacity(1.0))
      .base(Text, |s| s.gap(2.0).padding_start(4.0).padding_bottom(4.0))
      .size(Md, Text, |s| s.padding_bottom(6.0))
      .base(Title, |s| {
        s.typography(TypographyStyle::Body)
          .font_size(14.0)
          .weight(FontWeight::Medium)
          .foreground(white)
      })
      .size(Md, Title, |s| s.font_size(13.0))
      .base(Description, |s| {
        s.typography(TypographyStyle::Body).font_size(13.0).foreground(white_60)
      })
      .base(Actions, |s| s.gap(4.0))
      .base(Control, |s| {
        fading(
          s.square(36.0)
            .radius(RadiusSize::Md)
            .foreground(white)
            .cursor(CursorIcon::Pointer),
        )
      })
      .size(Md, Control, |s| s.square(28.0))
      .hovered(Control, |s| s.background(white_12))
      .focus_visible(Control, |s| s.outline(Outline::new(2.0, white)))
      .state(On, Control, |s| s.background(white_20))
      .base(ControlIcon, |s| s.square(20.0))
      // The phone's picture (`pencil/lurq-design-system.pen`, Mobile/Picture): a large one runs edge to edge with
      // no corners and no line; its badge 16 in; its bar 32 over, 16 beside and 12 under its title of 15.
      .when(Selector::new().variant(PictureVariant::Phone).size(PictureSize::Lg), Root, |s| s.radius(0.0))
      .when(Selector::new().variant(PictureVariant::Phone).size(PictureSize::Lg), Top, |s| {
        s.padding_x(16.0).padding_y(16.0)
      })
      .when(Selector::new().variant(PictureVariant::Phone).size(PictureSize::Lg), Controls, |s| {
        s.padding_top(32.0).padding_x(16.0).padding_bottom(12.0)
      })
      .when(Selector::new().variant(PictureVariant::Phone).size(PictureSize::Lg), Text, |s| {
        s.padding_start(0.0).padding_bottom(0.0)
      })
      .when(Selector::new().variant(PictureVariant::Phone).size(PictureSize::Lg), Title, |s| s.font_size(15.0))
      .size(Md, ControlIcon, |s| s.square(16.0))
      .base(MoreMenu, |s| s.width(216.0))
      .base(Caption, |s| {
        s.typography(TypographyStyle::Body)
          .font_size(13.0)
          .font_style(FontStyle::Italic)
          .line_height(1.4)
          .foreground(MutedForeground)
      })
  }
}