nsh-core 0.3.0

Platform-independent core: shadow tree, styles, layout and mount instructions.
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
//! The elements a view is made of ([`view`], [`text`], [`image`], other
//! components, slots and children).

use std::{any::Any, fmt, marker::PhantomData, rc::Rc, sync::Arc};

use super::{
    Component, ComponentSpec, Event, IntoReactive, JsValue, PropError, Reactive, SharedString,
    component::AnyInstance,
    events::{self, GestureEvent, PanEvent},
    style::{StyleRefinement, Styled, Variant},
};
use crate::{
    AccessibilityProp, AccessibilityRole, AccessibilityState, AccessibilityValue, Bounds,
    ContextMenuEvent, EdgeInsets, EventKind, EventPayload, HoverEvent, Hsla, ImageSource,
    ListContentStyle, NativeProp, NodeId, PointerEvents, Points, Prop, Size, Tag, TouchEvent,
    geometry::Radians, style::Color,
};

/// A handler of `E` on an element: a closure (`move |e: &E| ..`) that
/// writes the signals it captured. It runs under the owner current when it
/// was made (the component, region or row whose view made it), so what it
/// makes (signals, animated values, element refs, tasks) is that owner's
/// and goes with it; once that owner is disposed it no longer runs.
pub struct Listener<E: ?Sized> {
    f: Rc<dyn Fn(&E)>,
    owner: super::Owner,
}

impl<E: ?Sized + 'static> Listener<E> {
    /// A listener that does nothing (the element still listens).
    pub(crate) fn noop() -> Self {
        Self::new(Rc::new(|_| {}))
    }

    fn new(f: Rc<dyn Fn(&E)>) -> Self {
        Self {
            f,
            owner: super::Owner::current_or_detached(),
        }
    }

    /// Calls it with `event`, under its owner; not once that owner is
    /// disposed: the view it was made for is gone.
    pub(crate) fn call(&self, event: &E) {
        if self.owner == super::Owner::detached() {
            (self.f)(event);
        } else if self.owner.is_live() {
            self.owner.scope(|| (self.f)(event));
        }
    }

    /// A listener of `F` that runs `f` with this one's closure, under its
    /// owner.
    pub(crate) fn wrap<F: ?Sized>(self, f: impl Fn(&dyn Fn(&E), &F) + 'static) -> Listener<F> {
        let inner = self.f;
        Listener {
            f: Rc::new(move |event: &F| f(&*inner, event)),
            owner: self.owner,
        }
    }
}

impl<E: 'static> Listener<E> {
    /// This listener for events of another type, turned into `E` by `f`.
    pub fn map<F: ?Sized + 'static>(self, f: impl Fn(&F) -> E + 'static) -> Listener<F> {
        self.wrap(move |inner, event: &F| inner(&f(event)))
    }
}

/// A closure as a listener.
impl<E: ?Sized + 'static, F: Fn(&E) + 'static> From<F> for Listener<E> {
    fn from(f: F) -> Self {
        Self::new(Rc::new(f))
    }
}

/// What a listener method takes (`on_press`, `on_change`,
/// `on_event`, ...): a closure of the event, `move |text: &str| ..`; for an
/// event without data also one without arguments, `move || ..` (or
/// `move |_: &()| ..`); or a [`Listener`]. `M` tells them apart: it is
/// inferred, never written. (One conversion could not take both closures:
/// a type may implement `Fn()` and `Fn(&())`.)
#[diagnostic::on_unimplemented(
    message = "`{Self}` is not a listener of `{E}`",
    label = "not a listener of `{E}`",
    note = "a listener is a closure `move |event: &{E}| ..` (`move || ..` for an event without data) or a `Listener<{E}>`"
)]
pub trait IntoListener<E: ?Sized, M> {
    /// As the listener.
    fn into_listener(self) -> Listener<E>;
}

/// How a closure of the event is a listener ([`IntoListener`]).
#[doc(hidden)]
pub struct OfEvent(());

/// How a closure without arguments is a listener of an event without data
/// ([`IntoListener`]).
#[doc(hidden)]
pub struct WithoutEvent(());

impl<E: ?Sized> IntoListener<E, ()> for Listener<E> {
    fn into_listener(self) -> Listener<E> {
        self
    }
}

impl<E: ?Sized + 'static, F: Fn(&E) + 'static> IntoListener<E, OfEvent> for F {
    fn into_listener(self) -> Listener<E> {
        Listener::new(Rc::new(self))
    }
}

impl<F: Fn() + 'static> IntoListener<(), WithoutEvent> for F {
    fn into_listener(self) -> Listener<()> {
        Listener::new(Rc::new(move |_: &()| self()))
    }
}

impl<E: ?Sized> Clone for Listener<E> {
    fn clone(&self) -> Self {
        Self {
            f: self.f.clone(),
            owner: self.owner,
        }
    }
}

/// An event a component emitted, with its type erased: what JS hears of
/// it and the event, for Rust listeners.
pub(crate) trait AnyEvent: super::EventToJs {
    fn as_any(&self) -> &dyn Any;
}

impl<E: Event> super::EventToJs for E {
    fn name(&self) -> &'static str {
        Event::name(self)
    }

    fn data_js<'a>(&self, host: &'a dyn super::JsHost) -> Result<super::JsOwned<'a>, PropError> {
        Event::data_js(self, host)
    }

    fn spread(&self) -> bool {
        Event::spread(self)
    }
}

impl<E: Event> AnyEvent for E {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// The props of an element the core diffs field by field (opaque).
#[doc(hidden)]
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Attrs {
    pub(crate) disabled: bool,
    pub(crate) accessible: Option<bool>,
    pub(crate) label: Option<SharedString>,
    pub(crate) hint: Option<SharedString>,
    pub(crate) role: AccessibilityRole,
    pub(crate) state: AccessibilityState,
    pub(crate) value: Option<AccessibilityValue>,
    pub(crate) pointer_events: PointerEvents,
    pub(crate) number_of_lines: u32,
    pub(crate) src: Option<super::ImageSrc>,
    pub(crate) inset_padding: Option<EdgeInsets>,
    pub(crate) inset_margin: Option<EdgeInsets>,
    /// An inline style (a `Style` prop), over the styles.
    pub(crate) inline: Option<Arc<crate::StylePatch>>,
    /// The props of host elements (inputs, scroll views, ...), one per
    /// property ([`super::host`]).
    pub(crate) props: Vec<Prop>,
    /// A canvas's drawing.
    pub(crate) drawing: Option<Arc<crate::canvas::Drawing>>,
    /// What the ref the view put on the element names (not a prop): held
    /// while the element is, however long the ref's owner lives.
    pub(crate) element_ref: Option<super::element_ref::RefTarget>,
    /// Style properties bound to native animation nodes (held while
    /// bound).
    pub(crate) animated: Vec<(crate::StyleDecl, super::LiveNode)>,
    /// Animated nodes host props read (drags, scroll offsets), held.
    pub(crate) bound: Vec<super::LiveNode>,
    /// Tailwind classes the app gave through a prop (a component's part),
    /// after the element's own styles.
    pub(crate) class: Option<SharedString>,
    /// The view is a selectable view ([`crate::selection`]).
    pub(crate) selectable_view: bool,
    /// The states it shows as its control's part
    /// ([`Interactive::project_checked`]).
    pub(crate) projected_states: u8,
    /// Where a JS renderer's content goes (the element is its
    /// placeholder: [`super::Renderer::render`]).
    pub(crate) render: Option<super::renderer::RenderAt>,
}

impl Attrs {
    /// The props of the fields that differ from `old`, in a fixed order.
    pub(crate) fn changes(&self, old: &Attrs, out: &mut Vec<Prop>) {
        use AccessibilityProp as A;
        if self.disabled != old.disabled {
            out.push(Prop::Native(NativeProp::Disabled(self.disabled)));
        }
        if self.accessible != old.accessible {
            out.push(Prop::Accessibility(A::Accessible(self.accessible)));
        }
        if self.label != old.label {
            out.push(Prop::Accessibility(A::Label(
                self.label.as_ref().map(SharedString::to_arc),
            )));
        }
        if self.hint != old.hint {
            out.push(Prop::Accessibility(A::Hint(
                self.hint.as_ref().map(SharedString::to_arc),
            )));
        }
        if self.role != old.role {
            out.push(Prop::Accessibility(A::Role(self.role)));
        }
        if self.state != old.state {
            out.push(Prop::Accessibility(A::State(self.state)));
        }
        if self.value != old.value {
            out.push(Prop::Accessibility(A::Value(self.value.clone())));
        }
        if self.pointer_events != old.pointer_events {
            out.push(Prop::PointerEvents(self.pointer_events));
        }
        if self.number_of_lines != old.number_of_lines {
            out.push(Prop::NumberOfLines(self.number_of_lines));
        }
        if self.src != old.src {
            out.push(Prop::Source(self.src.as_ref().map(|src| ImageSource {
                uri: src.uri.to_arc(),
                size: src.size.map(|(w, h)| crate::geometry::size_pt(w, h)),
                scales: src.scales,
                data: None,
            })));
        }
        if self.inset_padding != old.inset_padding {
            out.push(Prop::InsetPadding(self.inset_padding.unwrap_or_default()));
        }
        if self.inset_margin != old.inset_margin {
            out.push(Prop::InsetMargin(self.inset_margin.unwrap_or_default()));
        }
        if self.selectable_view != old.selectable_view {
            out.push(Prop::SelectableView(self.selectable_view));
        }
        if self.projected_states != old.projected_states {
            out.push(Prop::ProjectedStates(self.projected_states));
        }
        // Host props: the ones no longer set go back to their defaults,
        // then the new and changed ones.
        for prop in &old.props {
            if !self.props.iter().any(|p| super::host::same_kind(p, prop))
                && let Some(reset) = super::host::reset(prop)
            {
                out.push(reset);
            }
        }
        for prop in &self.props {
            if !old.props.contains(prop) {
                out.push(prop.clone());
            }
        }
    }
}

/// A nested component's props, with their type erased.
pub(crate) trait AnyProps {
    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;
    fn clone_box(&self) -> Box<dyn AnyProps>;
}

impl<P: Clone + 'static> AnyProps for P {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn clone_box(&self) -> Box<dyn AnyProps> {
        Box::new(self.clone())
    }
}

/// A nested component of a view.
pub(crate) struct ComponentEl {
    pub(crate) name: &'static str,
    /// Makes an instance: a component a view makes need not be registered
    /// (the registry is what JS can make by name).
    pub(crate) make: fn() -> Box<dyn AnyInstance>,
    /// Its spec (made once per type and kept by the core).
    pub(crate) spec: fn() -> ComponentSpec,
    pub(crate) props: Box<dyn AnyProps>,
    /// The handlers of its events in the view that has it, in order.
    pub(crate) on_event: Vec<Listener<dyn Any>>,
}

impl Clone for ComponentEl {
    fn clone(&self) -> Self {
        Self {
            name: self.name,
            make: self.make,
            spec: self.spec,
            props: self.props.clone_box(),
            on_event: self.on_event.clone(),
        }
    }
}

/// Places one piece of the app's content: a JSX child, or a list row.
pub(crate) type PlaceFn = Rc<dyn Fn(Child, Position) -> AnyElement>;

/// Where a JSX child or a list row is among its siblings (what a TS row
/// knows from its index and the data's length). `Copy`: a handle of two
/// signals, so a binding that reads it (`move || at.is_last()`) runs again
/// when a list cell shows another row or the rows come and go, and the
/// cell's content stays.
#[derive(Clone, Copy)]
pub struct Position {
    index: super::reactive::Node,
    count: super::reactive::Node,
}

impl Position {
    /// The position read from signals (or memos) of the index and the
    /// count.
    pub(crate) fn of(index: super::reactive::Node, count: super::reactive::Node) -> Self {
        Self { index, count }
    }

    /// A position that does not change (signals of the current owner).
    pub(crate) fn fixed(index: usize, count: usize) -> Self {
        Self::of(super::signal(index).node(), super::signal(count).node())
    }

    /// Its index, subscribing the running computation.
    #[track_caller]
    pub fn index(&self) -> usize {
        self.index.with("a position", |i: &usize| *i)
    }

    /// How many there are, subscribing the running computation.
    #[track_caller]
    pub fn count(&self) -> usize {
        self.count.with("a position", |n: &usize| *n)
    }

    /// The first one.
    #[track_caller]
    pub fn is_first(&self) -> bool {
        self.index() == 0
    }

    /// The last one.
    #[track_caller]
    pub fn is_last(&self) -> bool {
        self.index() + 1 == self.count()
    }
}

impl fmt::Debug for Position {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (index, count) = super::untracked(|| (self.index(), self.count()));
        write!(f, "Position({index} of {count})")
    }
}

/// How a list is laid out (each set field is sent to the list); set with
/// [`super::ListLayout`]'s methods.
#[doc(hidden)]
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct ListConfig {
    pub(crate) estimated: Option<f32>,
    pub(crate) columns: Option<u32>,
    pub(crate) horizontal: Option<bool>,
    pub(crate) inverted: Option<bool>,
    pub(crate) scrollbar_visible: Option<bool>,
    pub(crate) content: Option<ListContentStyle>,
    pub(crate) follow_end: Option<f32>,
    pub(crate) viewport: Option<crate::ListViewport>,
}

impl ListConfig {
    /// The props of the fields that differ from `old`.
    pub(crate) fn changes(&self, old: &ListConfig, out: &mut Vec<Prop>) {
        if self.estimated != old.estimated {
            out.push(Prop::EstimatedItemSize(Points(
                self.estimated.unwrap_or(50.0),
            )));
        }
        if self.columns != old.columns {
            out.push(Prop::Columns(self.columns.unwrap_or(1)));
        }
        if self.horizontal != old.horizontal {
            out.push(Prop::Horizontal(self.horizontal.unwrap_or(false)));
        }
        if self.inverted != old.inverted {
            out.push(Prop::Inverted(self.inverted.unwrap_or(false)));
        }
        if self.scrollbar_visible != old.scrollbar_visible {
            out.push(Prop::Scrollbar(self.scrollbar_visible.unwrap_or(true)));
        }
        if self.content != old.content {
            out.push(Prop::ListContent(self.content.unwrap_or_default()));
        }
        if self.follow_end != old.follow_end {
            out.push(Prop::FollowEnd(self.follow_end.map(Points)));
        }
        if self.viewport != old.viewport {
            out.push(Prop::ListViewport(self.viewport.unwrap_or_default()));
        }
    }
}

/// A list slot as a view places it.
#[derive(Clone)]
pub(crate) struct ListEl {
    pub(crate) name: &'static str,
    pub(crate) config: ListConfig,
    pub(crate) row: PlaceFn,
}

/// What an element is beyond its tag.
#[derive(Clone)]
pub(crate) enum ElKind {
    Plain,
    /// Where slot `name` goes (a view placing it).
    Slot(&'static str),
    /// What a view fills a nested component's slot `name` with: its
    /// children, placed as they are where the nested view places the slot.
    Fill(&'static str),
    /// Where the nested view places what a view filled slot `name` with
    /// (no node of its own).
    Filled(&'static str),
    /// Where the JSX children go (a list row's content, in a row).
    Children,
    /// One JSX child (in [`ParentElement::children_each`]).
    JsChild(NodeId),
    /// Every JSX child, each placed by the function.
    EachChild(PlaceFn),
    /// The glue's list of an [`Items`] prop.
    List(Box<ListEl>),
    /// A list of the component's own rows ([`super::virtual_list`]).
    VirtualList(Box<super::virtual_list::VirtualListEl>),
    /// A piece of a text's content among its spans (a raw text node, as
    /// JS's strings in a `<text>`).
    Raw,
    Component(ComponentEl),
    /// Elements that change ([`super::dynamic`], [`super::If`],
    /// [`super::Each`]): a region of its parent's children, reconciled on
    /// its own.
    Region(super::flow::Region),
    /// In a region: the element of this key stays as it is (an `Each` row
    /// that was there before).
    Keep,
}

/// An element as the core expands it (opaque).
#[doc(hidden)]
#[derive(Clone)]
pub struct ElNode {
    pub(crate) tag: Tag,
    pub(crate) key: Option<SharedString>,
    /// A text's content.
    pub(crate) text: Option<SharedString>,
    pub(crate) attrs: Attrs,
    pub(crate) style: StyleRefinement,
    pub(crate) handlers: Vec<(EventKind, Listener<EventPayload>)>,
    /// Its part in the touch responder's negotiation.
    pub(crate) responder: Option<Rc<super::responder::ResponderEl>>,
    pub(crate) children: Vec<ElNode>,
    pub(crate) kind: ElKind,
    /// A text's content when it is bound (read in a binding).
    pub(crate) bound_text: Option<Rc<dyn Fn() -> SharedString>>,
    /// Bound props: each sets the fields it binds on the static ones.
    pub(crate) bound_attrs: Vec<BoundAttrs>,
    /// A list's bound layout: each sets the fields it binds.
    pub(crate) bound_list: Vec<BoundListConfig>,
}

/// A bound part of a list's layout.
#[doc(hidden)]
pub type BoundListConfig = Rc<dyn Fn(&mut ListConfig)>;

crate::static_reactive!(AccessibilityState: AccessibilityState);
crate::static_reactive!(EdgeInsets: EdgeInsets);
crate::static_reactive!(Style: Style);
crate::static_reactive!(PointerEvents: PointerEvents);

/// A bound part of an element's props.
pub(crate) type BoundAttrs = Rc<dyn Fn(&mut Attrs)>;

impl ElNode {
    /// Applies `value` to the props with `apply`: now when it is static,
    /// else as a bound part.
    pub(crate) fn bind_attr<T: 'static>(
        &mut self,
        value: impl IntoReactive<T>,
        apply: impl Fn(&mut Attrs, T) + 'static,
    ) {
        match value.into_reactive() {
            Reactive::Static(v) => apply(&mut self.attrs, v),
            Reactive::Bound(f) => self.bound_attrs.push(Rc::new(move |a| apply(a, f()))),
        }
    }

    /// Whether it has bound parts (text, props or styles).
    pub(crate) fn is_bound(&self) -> bool {
        self.bound_text.is_some()
            || !self.bound_attrs.is_empty()
            || self.style.is_bound()
            || !self.bound_list.is_empty()
    }

    /// The props now: the static ones with the bound parts applied (read in
    /// the running computation, which subscribes to them).
    pub(crate) fn resolved_attrs(&self) -> Attrs {
        let mut attrs = self.attrs.clone();
        for part in &self.bound_attrs {
            part(&mut attrs);
        }
        attrs
    }

    /// Turns the bound parts into static ones, read once, untracked: how a
    /// nested component's element and a slot's node take them.
    pub(crate) fn resolve_once(&mut self) {
        if !self.is_bound() {
            return;
        }
        super::untracked(|| {
            if let Some(text) = self.bound_text.take() {
                self.text = Some(text());
            }
            if !self.bound_attrs.is_empty() {
                self.attrs = self.resolved_attrs();
                self.bound_attrs.clear();
            }
            if self.style.is_bound() {
                self.style = self.style.resolved();
            }
            let parts = std::mem::take(&mut self.bound_list);
            if let Some(config) = self.list_config() {
                for part in &parts {
                    part(config);
                }
            }
        });
    }

    /// A list's layout (of a list element or a virtual list).
    pub(crate) fn list_config(&mut self) -> Option<&mut ListConfig> {
        match &mut self.kind {
            ElKind::List(list) => Some(&mut list.config),
            ElKind::VirtualList(list) => Some(&mut list.config),
            _ => None,
        }
    }

    /// What a component that is not registered renders.
    pub(crate) fn empty_view() -> Self {
        Self::new(Tag::View)
    }

    pub(crate) fn new(tag: Tag) -> Self {
        Self {
            tag,
            key: None,
            text: None,
            attrs: Attrs::default(),
            style: StyleRefinement::default(),
            handlers: Vec::new(),
            responder: None,
            children: Vec::new(),
            kind: ElKind::Plain,
            bound_text: None,
            bound_attrs: Vec::new(),
            bound_list: Vec::new(),
        }
    }
}

/// Any element, its type erased.
pub struct AnyElement(pub(crate) ElNode);

/// What can be a child: elements, and strings (as texts).
pub trait IntoElement {
    /// As an element.
    fn into_element(self) -> AnyElement;
}

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

impl IntoElement for SharedString {
    fn into_element(self) -> AnyElement {
        text(self).into_element()
    }
}

impl IntoElement for &'static str {
    fn into_element(self) -> AnyElement {
        text(self).into_element()
    }
}

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

/// What `.child(..)` takes ([`ParentElement::child`]): an element, or
/// what holds elements (an `Option`, a `Vec`, an array or an iterator of
/// them: `rows.iter().map(|r| row(r))`). `M` tells the two apart: it is
/// inferred, never written. (One blanket conversion could not take both: a
/// type may be an element and hold elements.)
#[diagnostic::on_unimplemented(
    message = "`{Self}` is not an element",
    label = "not an element",
    note = "elements are host elements (`view()`, `text(..)`, ...), components' elements, strings, and an `Option`, a `Vec`, an array or an iterator of elements"
)]
pub trait IntoChild<M> {
    /// Adds what it is to `out`.
    #[doc(hidden)]
    fn __push_into(self, out: &mut Vec<ElNode>);
}

/// How an element is a child ([`IntoChild`]).
#[doc(hidden)]
pub struct OneChild(());

/// How what holds elements is children ([`IntoChild`]).
#[doc(hidden)]
pub struct ManyChildren(());

impl<T: IntoElement> IntoChild<OneChild> for T {
    fn __push_into(self, out: &mut Vec<ElNode>) {
        out.push(self.into_element().0);
    }
}

impl<I: IntoIterator> IntoChild<ManyChildren> for I
where
    I::Item: IntoElement,
{
    fn __push_into(self, out: &mut Vec<ElNode>) {
        out.extend(self.into_iter().map(|c| c.into_element().0));
    }
}

/// Conditional building, as GPUI's `FluentBuilder` (`gpui::prelude`): decided
/// once, as the element is made. What must follow a signal is a region
/// ([`super::when`], [`super::dynamic`]).
pub trait FluentBuilder: Sized {
    /// `then(self)` when `condition` holds.
    fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self {
        if condition { then(self) } else { self }
    }

    /// `then(self, value)` when `option` has one.
    fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self {
        match option {
            Some(v) => then(self, v),
            None => self,
        }
    }

    /// `f(self)`.
    fn map(self, f: impl FnOnce(Self) -> Self) -> Self {
        f(self)
    }
}

impl<T: Element> FluentBuilder for T {}

/// An element: what every element can do. Its other abilities are the
/// traits it implements ([`Styled`], [`Accessible`], [`Interactive`],
/// [`ParentElement`]).
pub trait Element: IntoElement + Sized {
    #[doc(hidden)]
    fn node(&mut self) -> &mut ElNode;

    /// Identifies the element among its siblings when they are made again
    /// (a region's, a list cell's row; where their order changes).
    fn key(mut self, key: impl Into<SharedString>) -> Self {
        self.node().key = Some(key.into());
        self
    }

    /// Binds `opacity` to an animated value (it moves natively).
    fn bind_opacity(self, value: &impl super::Animated) -> Self {
        self.bind(crate::animation::AnimationProp::Opacity, value)
    }

    /// Binds the background color to a node of colors
    /// ([`super::AnimatedNode::interpolate_colors`]).
    fn bind_background(self, value: &impl super::Animated) -> Self {
        self.bind(crate::animation::AnimationProp::BackgroundColor, value)
    }

    /// Binds the color of every border side to a node of colors.
    fn bind_border_color(self, value: &impl super::Animated) -> Self {
        self.bind(crate::animation::AnimationProp::BorderColor, value)
    }

    /// Binds a text's color (the whole text) to a node of colors.
    fn bind_color(self, value: &impl super::Animated) -> Self {
        self.bind(crate::animation::AnimationProp::Color, value)
    }

    /// Binds the shadow's color to a node of colors (TS: `shadowColor`).
    fn bind_shadow_color(self, value: &impl super::Animated) -> Self {
        self.bind(crate::animation::AnimationProp::ShadowColor, value)
    }

    /// Binds the shadow's opacity, 0..1 (TS: `shadowOpacity`).
    fn bind_shadow_opacity(self, value: &impl super::Animated) -> Self {
        self.bind(crate::animation::AnimationProp::ShadowOpacity, value)
    }

    /// Binds the shadow's blur radius, in points (TS: `shadowRadius`).
    fn bind_shadow_radius(self, value: &impl super::Animated) -> Self {
        self.bind(crate::animation::AnimationProp::ShadowRadius, value)
    }

    /// Binds `prop` (anything but a transform step or a canvas argument)
    /// to an animated value (nothing once the value's owner is gone).
    fn bind(mut self, prop: crate::animation::AnimationProp, value: &impl super::Animated) -> Self {
        if let Some(node) = value.animated_node().live() {
            let decl = crate::StyleDecl::Animated(prop, node.id());
            self.node().attrs.animated.push((decl, node));
        }
        self
    }

    /// A sticky view ([`super::Styled::sticky`]) sticks this animated value
    /// from the top of the scroll view's viewport, following it (below a
    /// header that moves: `header.add(8.0)`; TS: `top` set to an animated
    /// value).
    fn bind_sticky_top(self, value: &impl super::Animated) -> Self {
        self.bind(crate::animation::AnimationProp::StickyTop, value)
    }

    /// The same as [`Self::bind_sticky_top`] in a horizontal scroll view.
    fn bind_sticky_left(self, value: &impl super::Animated) -> Self {
        self.bind(crate::animation::AnimationProp::StickyLeft, value)
    }

    /// Appends a transform step driven by an animated value, after the styles'
    /// transforms.
    fn bind_transform(mut self, kind: crate::TransformKind, value: &impl super::Animated) -> Self {
        if let Some(node) = value.animated_node().live() {
            let decl = crate::StyleDecl::Transform(kind, kind.identity(), Some(node.id()));
            self.node().attrs.animated.push((decl, node));
        }
        self
    }

    /// Binds a motion's channels: its opacity, and its transform after the
    /// styles' transforms (translations, then rotations, then the scales,
    /// `scale` folded into `scale_x` and `scale_y`: 7 steps of a view's 8).
    fn bind_motion(self, motion: &super::Motion) -> Self {
        use crate::animation::MotionChannel::*;
        let mut this = self.bind_opacity(&motion.value(Opacity));
        for channel in [TranslateX, TranslateY, Rotate, RotateX, RotateY] {
            if let Some(kind) = channel.transform() {
                this = this.bind_transform(kind, &motion.value(channel));
            }
        }
        let scale = motion.value(Scale);
        this.bind_transform(
            crate::TransformKind::ScaleX,
            &scale.mul(&motion.value(ScaleX)),
        )
        .bind_transform(
            crate::TransformKind::ScaleY,
            &scale.mul(&motion.value(ScaleY)),
        )
    }

    /// Moves it right by an animated value (points).
    fn bind_translate_x(self, value: &impl super::Animated) -> Self {
        self.bind_transform(crate::TransformKind::TranslateX, value)
    }

    /// Moves it down by an animated value (points).
    fn bind_translate_y(self, value: &impl super::Animated) -> Self {
        self.bind_transform(crate::TransformKind::TranslateY, value)
    }

    /// Scales it by an animated value.
    fn bind_scale(self, value: &impl super::Animated) -> Self {
        self.bind_transform(crate::TransformKind::Scale, value)
    }

    /// Rotates it by an animated value (degrees, clockwise).
    fn bind_rotate(self, value: &impl super::Animated) -> Self {
        self.bind_transform(crate::TransformKind::Rotate, value)
    }

    /// Puts `handle` on this element: its commands (`focus`, `scroll_to`)
    /// go here (nothing once the ref's owner is gone).
    fn element_ref(mut self, handle: &super::ElementRef) -> Self {
        self.node().attrs.element_ref = handle.target();
        self
    }

    /// Padding added to the element's own (from styles): insets of system
    /// UI, a keyboard.
    fn inset_padding(mut self, insets: impl IntoReactive<EdgeInsets>) -> Self {
        self.node()
            .bind_attr(insets, |a, insets| a.inset_padding = Some(insets));
        self
    }

    /// Margin added to the element's own.
    fn inset_margin(mut self, insets: impl IntoReactive<EdgeInsets>) -> Self {
        self.node()
            .bind_attr(insets, |a, insets| a.inset_margin = Some(insets));
        self
    }

    /// An inline style the app passed (a prop of type `Style`, React
    /// Native's names), over the element's styles.
    fn inline_style(mut self, style: impl IntoReactive<Style>) -> Self {
        Style::apply(self.node(), style, Variant::default(), |a, patch| {
            a.inline = patch
        });
        self
    }

    /// Makes the texts in the view select together, as a browser page's
    /// do (TSX `<View selectable>`): a selection runs from text to text,
    /// across views and `TextView`s; texts that are not selectable stay
    /// out of it ([`crate::selection`]).
    fn selectable(mut self, area: impl IntoReactive<bool>) -> Self {
        self.node()
            .bind_attr(area, |a, area| a.selectable_view = area);
        self
    }

    /// Tailwind classes the app passed (a prop naming the classes of one
    /// of the component's parts: `classes={{ heading: "text-2xl" }}`),
    /// after the element's own styles, as the app's `class` on a
    /// component's root is: at equal rank they win.
    fn app_class(mut self, class: impl IntoReactive<Option<SharedString>>) -> Self {
        self.node().bind_attr(class, |a, class| a.class = class);
        self
    }
}

/// An inline style: a prop typed `StyleProp<ViewStyle>` in TypeScript (an
/// object, an array of them, or nothing), decoded when JS sets it as a host
/// element's `style` is (transforms, shadow offsets, native animated
/// values included), and applied with [`Element::inline_style`]. Rust
/// makes one with the typed styles (`From<StyleRefinement>`: what a style
/// prop's setter does with its closure, `.selected_style(|s| s.bg(..))`),
/// theme colors following the appearance and bound parts their signals,
/// as in `.active(|s| ..)`; or from declarations ([`Style::from_decls`],
/// [`Style::with`]).
/// Its native animated values are node ids, valid while the style is the
/// prop's current value (JS keeps the values alive with it, as a host
/// element's `style` does); a component that keeps an older `Style`
/// beyond that may hold ids of values JS has collected.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Style(
    pub(crate) Option<Arc<crate::StylePatch>>,
    /// The typed styles it was made of (Rust's), applied as the element's
    /// own are.
    pub(crate) StyleRefinement,
);

impl Style {
    /// A style of `decls`, applied in order; one that does not apply (a
    /// value of the wrong type or format) is left out and logged, as on a
    /// host element.
    pub fn from_decls(decls: impl IntoIterator<Item = crate::StyleDecl>) -> Self {
        let mut style = crate::Style::default();
        let mut patch = crate::StylePatch::default();
        for decl in decls {
            let key = match &decl {
                crate::StyleDecl::Set(key, _) => Some(*key),
                _ => None,
            };
            if patch.push(&mut style, decl).is_err()
                && let Some(key) = key
            {
                log::warn!("invalid value for style `{}`", key.name());
            }
        }
        Self(Some(Arc::new(patch)), StyleRefinement::default())
    }

    /// This style with property `key` set to `value` after the rest
    /// (`Style::default().with(StyleKey::Flex, PatchValue::Number(1.0))`).
    pub fn with(self, key: crate::StyleKey, value: crate::PatchValue) -> Self {
        let decls = self.0.iter().flat_map(|p| p.decls().iter().cloned());
        let mut style = Self::from_decls(decls.chain([crate::StyleDecl::Set(key, value)]));
        style.1 = self.1;
        style
    }

    /// Its declarations, in order (not its typed styles').
    pub fn decls(&self) -> &[crate::StyleDecl] {
        self.0.as_deref().map_or(&[], crate::StylePatch::decls)
    }

    /// Applies it to `node` in `variant`: the declarations with `attr`, the
    /// typed styles as part of the node's own. A bound style adds its
    /// typed styles as a bound part only when it has some as the element
    /// is made (they are set with the element; JS's styles have none), so
    /// a style prop left unset costs what a style of declarations does.
    pub(crate) fn apply(
        node: &mut ElNode,
        style: impl IntoReactive<Style>,
        variant: Variant,
        attr: impl Fn(&mut Attrs, Option<Arc<crate::StylePatch>>) + 'static,
    ) {
        match style.into_reactive() {
            Reactive::Static(Style(patch, typed)) => {
                attr(&mut node.attrs, patch);
                if !typed.is_empty() {
                    node.style.extend_as(variant, typed);
                }
            }
            Reactive::Bound(f) => {
                let typed = super::untracked(|| !f().1.is_empty());
                let patch = f.clone();
                node.bind_attr(move || patch().0, attr);
                if typed {
                    // Resolved here: the bound parts of what it adds would
                    // not run in this resolution.
                    node.style
                        .bind(move |s| s.extend_as(variant, f().1.resolved()));
                }
            }
        }
    }
}

impl From<crate::StylePatch> for Style {
    fn from(patch: crate::StylePatch) -> Self {
        Self(Some(Arc::new(patch)), StyleRefinement::default())
    }
}

impl From<StyleRefinement> for Style {
    fn from(style: StyleRefinement) -> Self {
        Self(None, style)
    }
}

impl JsValue for Style {
    fn ts_type(_types: &mut super::TsTypes) -> String {
        "StyleProp<ViewStyle>".into()
    }

    /// Decoded as a host element's `style` is ([`super::JsHost::style`]);
    /// what it does not take is logged, as there.
    fn from_js(value: super::JsIn<'_>) -> Result<Self, PropError> {
        if value.is_nullish() {
            return Ok(Self::default());
        }
        Ok(Self::from(value.host().style(value.raw())?))
    }

    /// A style is JS's to give, not to get back.
    fn to_js<'a>(&self, _host: &'a dyn super::JsHost) -> Result<super::JsOwned<'a>, PropError> {
        Err(PropError("a style does not convert back to JS".to_owned()))
    }
}

impl<T: Element> Styled for T {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.node().style
    }
}

/// Elements with children.
pub trait ParentElement: Element {
    /// Adds a child: an element, or the elements an `Option`, a `Vec`, an
    /// array or an iterator holds.
    fn child<M>(mut self, child: impl IntoChild<M>) -> Self {
        child.__push_into(&mut self.node().children);
        self
    }

    /// Adds children (an `Option` adds one or none).
    fn children(mut self, children: impl IntoIterator<Item = impl IntoElement>) -> Self {
        self.node()
            .children
            .extend(children.into_iter().map(|c| c.into_element().0));
        self
    }

    /// Places slot `slot` here when the app passed it, as `place` styles it
    /// (the closure gets the slot's own node).
    fn slot(mut self, slot: Option<Slot>, place: impl FnOnce(SlotElement) -> SlotElement) -> Self {
        if let Some(slot) = slot {
            let mut node = ElNode::new(Tag::View);
            node.kind = ElKind::Slot(slot.name);
            self.node().children.push(place(SlotElement(node)).0);
        }
        self
    }

    /// Places the app's JSX children here one by one: `place(child, at)`
    /// wraps each (runs again when the children change).
    fn children_each<R: IntoElement>(
        mut self,
        children: impl Into<Children>,
        place: impl Fn(Child, Position) -> R + 'static,
    ) -> Self {
        let _ = children.into();
        let mut node = ElNode::new(Tag::View);
        node.kind = ElKind::EachChild(Rc::new(move |child, at| place(child, at).into_element()));
        self.node().children.push(node);
        self
    }

    /// Places the app's virtualized list of `items` here: `config` lays
    /// the list out (size, columns, padding), `row(content, at)` wraps
    /// each row the app renders, once per cell (`at` follows the row the
    /// cell shows and the number of rows). Only rows near the viewport
    /// exist; the core recycles them.
    fn list<R: IntoElement>(
        mut self,
        items: Items,
        config: impl FnOnce(ListElement) -> ListElement,
        row: impl Fn(Child, Position) -> R + 'static,
    ) -> Self {
        let mut node = ElNode::new(Tag::List);
        node.kind = ElKind::List(Box::new(ListEl {
            name: items.name,
            config: ListConfig::default(),
            row: Rc::new(move |content, at| row(content, at).into_element()),
        }));
        self.node().children.push(config(ListElement(node)).0);
        self
    }
}

/// Elements with an accessibility description.
pub trait Accessible: Element {
    /// Whether assistive technologies see it as one element.
    fn accessible(mut self, accessible: impl IntoReactive<bool>) -> Self {
        self.node()
            .bind_attr(accessible, |a, accessible| a.accessible = Some(accessible));
        self
    }

    /// What screen readers say.
    fn label(mut self, label: impl IntoReactive<SharedString>) -> Self {
        self.node()
            .bind_attr(label, |a, label| a.label = Some(label));
        self
    }

    /// What screen readers say, while it is set (`None`: what its
    /// content says).
    fn optional_label(mut self, label: impl IntoReactive<Option<SharedString>>) -> Self {
        self.node().bind_attr(label, |a, label| a.label = label);
        self
    }

    /// What happens when activated, for screen readers.
    fn hint(mut self, hint: impl IntoReactive<SharedString>) -> Self {
        self.node().bind_attr(hint, |a, hint| a.hint = Some(hint));
        self
    }

    /// What it is (a button, a header, ...).
    fn role(mut self, role: impl IntoReactive<AccessibilityRole>) -> Self {
        self.node().bind_attr(role, |a, role| a.role = role);
        self
    }

    /// Its state for screen readers (`disabled` follows
    /// [`Interactive::disabled`]).
    fn accessibility_state(mut self, state: impl IntoReactive<AccessibilityState>) -> Self {
        self.node().bind_attr(state, |a, state| {
            a.state = AccessibilityState {
                disabled: a.state.disabled,
                ..state
            };
        });
        self
    }

    /// Its value for screen readers, for a range (a progress bar, a
    /// slider: `min`, `max` and `now`) or as a text; `None` has none (an
    /// indeterminate progress bar keeps its range and leaves out `now`).
    fn accessibility_value(mut self, value: impl IntoReactive<Option<AccessibilityValue>>) -> Self {
        self.node().bind_attr(value, |a, value| a.value = value);
        self
    }

    /// The actions assistive technologies offer on it (TS:
    /// `accessibilityActions`): `increment` and `decrement` for an
    /// adjustable element, `activate`, `longpress`, or the app's own with
    /// a label; [`Interactive::on_accessibility_action`] hears them.
    fn accessibility_actions(mut self, actions: Vec<crate::AccessibilityActionDef>) -> Self {
        super::host::set(
            self.node(),
            Prop::Accessibility(AccessibilityProp::Actions(actions)),
        );
        self
    }
}

/// What an element's tooltip can be ([`Interactive::tooltip`]): an
/// element, shown as it is (`nsh_base`'s `Tooltip` is the tooltip itself).
pub trait IntoTooltip {
    #[doc(hidden)]
    fn apply_tooltip(self, node: &mut ElNode);
}

impl IntoTooltip for AnyElement {
    fn apply_tooltip(self, node: &mut ElNode) {
        // A hint overlay inside the element, as the core makes for JS's.
        let mut hint = ElNode::new(Tag::Overlay);
        hint.key = Some("tooltip".into());
        hint.attrs.pointer_events = PointerEvents::None;
        let gap = Points(4.0);
        for prop in [
            crate::OverlayProp::Hint(Some(crate::overlay::HINT_DELAY)),
            crate::OverlayProp::Placement(crate::overlay::Placement::Top),
            crate::OverlayProp::Offset(gap),
            crate::OverlayProp::Margin(gap),
        ] {
            super::host::set(&mut hint, Prop::Overlay(prop));
        }
        hint.children.push(self.0);
        node.children.push(hint);
    }
}

impl<C: Component> IntoTooltip for ComponentElement<C> {
    fn apply_tooltip(self, node: &mut ElNode) {
        self.into_element().apply_tooltip(node);
    }
}

/// Makes `hint`, an element whose root is a hint overlay
/// ([`super::Overlay::hint`]: `nsh_base`'s `Tooltip`), the tooltip of
/// `node` as it is, with no overlay of the core's around it (its
/// [`IntoTooltip`]).
#[doc(hidden)]
pub fn __hint_tooltip(node: &mut ElNode, hint: AnyElement) {
    let mut hint = hint.0;
    hint.key = Some("tooltip".into());
    node.children.push(hint);
}

/// Elements that take touches.
pub trait Interactive: Element {
    /// A hint about it, shown above it on the desktops while the pointer
    /// rests on it (after half a second), until the pointer leaves or
    /// presses it; never on touch screens, and it never takes the pointer.
    /// The element is shown as it is; `nsh_base`'s `Tooltip` is the tooltip
    /// itself, with its own placement, delay and reports.
    fn tooltip(mut self, tooltip: impl IntoTooltip) -> Self {
        tooltip.apply_tooltip(self.node());
        self
    }

    /// A local file drag entered this desktop drop destination.
    fn on_file_drag_enter<M>(self, listener: impl IntoListener<(), M>) -> Self {
        self.on(
            EventKind::FileDragEnter,
            events::typed(listener.into_listener(), events::unit),
        )
    }

    /// A local file drag left this desktop drop destination.
    fn on_file_drag_leave<M>(self, listener: impl IntoListener<(), M>) -> Self {
        self.on(
            EventKind::FileDragLeave,
            events::typed(listener.into_listener(), events::unit),
        )
    }

    /// Local files or directories were dropped on this desktop destination.
    fn on_file_drop<M>(self, listener: impl IntoListener<events::FileDropEvent, M>) -> Self {
        self.on(
            EventKind::FileDrop,
            events::typed(listener.into_listener(), events::file_drop),
        )
    }

    /// The mouse came over it or an element inside it (never on touches).
    fn on_hover_in<M>(self, listener: impl IntoListener<(), M>) -> Self {
        self.on(
            EventKind::HoverIn,
            events::typed(listener.into_listener(), events::unit),
        )
    }

    /// The mouse left it and the elements inside it, or the window.
    fn on_hover_out<M>(self, listener: impl IntoListener<(), M>) -> Self {
        self.on(
            EventKind::HoverOut,
            events::typed(listener.into_listener(), events::unit),
        )
    }

    /// The mouse moved over it (or an element inside it) with no button
    /// down: where, in it and in its root view, at most once per frame.
    fn on_hover_move<M>(self, listener: impl IntoListener<HoverEvent, M>) -> Self {
        self.on(
            EventKind::HoverMove,
            events::typed(listener.into_listener(), events::hover),
        )
    }

    /// The mouse moved inside it, including while dragging: local coordinates
    /// in points, at most once per frame (desktops and web).
    fn on_mouse_move<M>(self, listener: impl IntoListener<crate::Point<crate::Points>, M>) -> Self {
        self.on(
            EventKind::MouseMove,
            events::typed(listener.into_listener(), events::mouse_position),
        )
    }

    /// The secondary button was pressed over it (a right click,
    /// Control-click on Apple platforms): where, to open a menu at
    /// (`page_x`, `page_y`). The nearest element that listens hears it;
    /// a text input or selectable text nearer shows its own menu instead.
    fn on_context_menu<M>(self, listener: impl IntoListener<ContextMenuEvent, M>) -> Self {
        self.on(
            EventKind::ContextMenu,
            events::typed(listener.into_listener(), events::context_menu),
        )
    }

    /// It took the keyboard focus: an input, or a view that takes presses
    /// reached by Tab (or `focus()`) on the platforms that move the focus
    /// between pressables.
    fn on_focus<M>(self, listener: impl IntoListener<(), M>) -> Self {
        self.on(
            EventKind::Focus,
            events::typed(listener.into_listener(), events::unit),
        )
    }

    /// It lost the keyboard focus.
    fn on_blur<M>(self, listener: impl IntoListener<(), M>) -> Self {
        self.on(
            EventKind::Blur,
            events::typed(listener.into_listener(), events::unit),
        )
    }

    /// Handles presses.
    fn on_press<M>(self, listener: impl IntoListener<(), M>) -> Self {
        self.on(
            EventKind::Press,
            events::typed(listener.into_listener(), events::unit),
        )
    }

    /// Handles presses held for about half a second.
    fn on_long_press<M>(self, listener: impl IntoListener<(), M>) -> Self {
        self.on(
            EventKind::LongPress,
            events::typed(listener.into_listener(), events::unit),
        )
    }

    /// A finger went down on the element.
    fn on_press_down<M>(self, listener: impl IntoListener<(), M>) -> Self {
        self.on(
            EventKind::PressDown,
            events::typed(listener.into_listener(), events::unit),
        )
    }

    /// The press ended, inside or outside the element.
    fn on_press_up<M>(self, listener: impl IntoListener<(), M>) -> Self {
        self.on(
            EventKind::PressUp,
            events::typed(listener.into_listener(), events::unit),
        )
    }

    /// Its frame changed (relative to its parent).
    fn on_layout<M>(self, listener: impl IntoListener<Bounds<Points>, M>) -> Self {
        self.on(
            EventKind::Layout,
            events::typed(listener.into_listener(), events::layout),
        )
    }

    /// Touches on it or inside it, every phase (custom gestures): the
    /// elements around the finger hear them from it outwards. To have them
    /// alone, claim them ([`Interactive::claim_touches`]).
    fn on_touch<M>(self, listener: impl IntoListener<TouchEvent, M>) -> Self {
        self.on(
            EventKind::Touch,
            events::typed(listener.into_listener(), events::touch),
        )
    }

    /// Claims the touches of a gesture as `claim` says (TS:
    /// `onStartShouldSetResponder`, `PanResponder`): as the responder it
    /// hears them alone (`on_responder_*`), and native scroll views around
    /// it stop taking them over. Several claims may be given.
    fn claim_touches(mut self, claim: super::TouchClaim) -> Self {
        if let Some(pan) = claim.pan {
            super::host::set(self.node(), Prop::PanActivation(Some(pan)));
        }
        super::responder::of(self.node()).claims.push(claim);
        self
    }

    /// As the responder, refuses to hand the touches over to an element
    /// around it that claims them (TS: `onResponderTerminationRequest`
    /// returning false); a native scroll view that started dragging first
    /// still takes them.
    fn keep_touches(mut self, keep: bool) -> Self {
        super::responder::of(self.node()).keep = keep;
        self
    }

    /// It became the responder (TS: `onResponderGrant`).
    fn on_responder_grant<M>(
        mut self,
        listener: impl IntoListener<super::ResponderEvent, M>,
    ) -> Self {
        super::responder::of(self.node()).grant = Some(listener.into_listener());
        self
    }

    /// It claimed the touches, but the responder kept them (TS:
    /// `onResponderReject`).
    fn on_responder_reject<M>(
        mut self,
        listener: impl IntoListener<super::ResponderEvent, M>,
    ) -> Self {
        super::responder::of(self.node()).reject = Some(listener.into_listener());
        self
    }

    /// As the responder, a finger went down (the first one too, after the
    /// grant; TS: `onResponderStart`).
    fn on_responder_start<M>(
        mut self,
        listener: impl IntoListener<super::ResponderEvent, M>,
    ) -> Self {
        super::responder::of(self.node()).start = Some(listener.into_listener());
        self
    }

    /// As the responder, the fingers moved (TS: `onResponderMove`).
    fn on_responder_move<M>(
        mut self,
        listener: impl IntoListener<super::ResponderEvent, M>,
    ) -> Self {
        super::responder::of(self.node()).moved = Some(listener.into_listener());
        self
    }

    /// As the responder, a finger lifted (TS: `onResponderEnd`).
    fn on_responder_end<M>(
        mut self,
        listener: impl IntoListener<super::ResponderEvent, M>,
    ) -> Self {
        super::responder::of(self.node()).end = Some(listener.into_listener());
        self
    }

    /// The last finger lifted: it is no longer the responder (TS:
    /// `onResponderRelease`).
    fn on_responder_release<M>(
        mut self,
        listener: impl IntoListener<super::ResponderEvent, M>,
    ) -> Self {
        super::responder::of(self.node()).release = Some(listener.into_listener());
        self
    }

    /// Another element or a native gesture took the touches over, or the
    /// system cancelled them (TS: `onResponderTerminate`).
    fn on_responder_terminate<M>(
        mut self,
        listener: impl IntoListener<super::ResponderEvent, M>,
    ) -> Self {
        super::responder::of(self.node()).terminate = Some(listener.into_listener());
        self
    }

    /// Drags move these values natively (by the drag's translation from
    /// where they were), with no engine work per frame; bind them to its
    /// transform to follow the finger. `on_pan_end` reports where they
    /// ended, to spring them on.
    fn pan_values(
        mut self,
        x: Option<&super::AnimatedValue>,
        y: Option<&super::AnimatedValue>,
    ) -> Self {
        let x = x.and_then(|v| v.node().live());
        let y = y.and_then(|v| v.node().live());
        let prop = Prop::PanValues(x.as_ref().map(|n| n.id()), y.as_ref().map(|n| n.id()));
        super::host::set(self.node(), prop);
        let bound = &mut self.node().attrs.bound;
        bound.extend(x);
        bound.extend(y);
        self
    }

    /// Enables native pans without replacing their values or listeners.
    /// A disabled pan leaves presses and scrolling to the content.
    fn pan_enabled(mut self, enabled: impl IntoReactive<bool>) -> Self {
        super::host::bind_prop(self.node(), enabled, Prop::PanEnabled);
        self
    }

    /// On iOS, a vertical pan shares descendant scroll views' touches and
    /// follows only the downward distance left after scrolling to the top.
    /// Other platforms keep their normal pan and scroll arbitration.
    fn pan_scroll_handoff(mut self, enabled: impl IntoReactive<bool>) -> Self {
        super::host::bind_prop(self.node(), enabled, Prop::PanScrollHandoff);
        self
    }

    /// Two-finger pinches scale `scale` natively (multiplying the value it
    /// has when the pinch starts; TS: `pinch={{ scale }}`); bind it to its
    /// transform ([`Element::bind_scale`]). `on_pinch_start` /
    /// `on_pinch_end` report the pinches.
    fn pinch_value(mut self, scale: &super::AnimatedValue) -> Self {
        if let Some(node) = scale.node().live() {
            super::host::set(self.node(), Prop::PinchValue(Some(node.id())));
            self.node().attrs.bound.push(node);
        }
        self
    }

    /// Two-finger rotations turn `angle` natively, in degrees (added to
    /// the value it has when the rotation starts; TS: `rotation={{ angle
    /// }}`); bind it to its transform ([`Element::bind_rotate`]).
    fn rotation_value(mut self, angle: &super::AnimatedValue) -> Self {
        if let Some(node) = angle.node().live() {
            super::host::set(self.node(), Prop::RotationValue(Some(node.id())));
            self.node().attrs.bound.push(node);
        }
        self
    }

    /// A drag started on it (the platform recognizes it).
    fn on_pan_start<M>(self, listener: impl IntoListener<PanEvent, M>) -> Self {
        self.on(
            EventKind::PanStart,
            events::typed(listener.into_listener(), events::pan),
        )
    }

    /// The drag ended, with its velocity.
    fn on_pan_end<M>(self, listener: impl IntoListener<PanEvent, M>) -> Self {
        self.on(
            EventKind::PanEnd,
            events::typed(listener.into_listener(), events::pan),
        )
    }

    /// A pinch started (its scale).
    fn on_pinch_start<M>(self, listener: impl IntoListener<GestureEvent, M>) -> Self {
        self.on(
            EventKind::PinchStart,
            events::typed(listener.into_listener(), events::pinch),
        )
    }

    /// The pinch ended.
    fn on_pinch_end<M>(self, listener: impl IntoListener<GestureEvent, M>) -> Self {
        self.on(
            EventKind::PinchEnd,
            events::typed(listener.into_listener(), events::pinch),
        )
    }

    /// A two-finger rotation started (its angle).
    fn on_rotation_start<M>(self, listener: impl IntoListener<GestureEvent<Radians>, M>) -> Self {
        self.on(
            EventKind::RotationStart,
            events::typed(listener.into_listener(), events::rotation),
        )
    }

    /// The rotation ended.
    fn on_rotation_end<M>(self, listener: impl IntoListener<GestureEvent<Radians>, M>) -> Self {
        self.on(
            EventKind::RotationEnd,
            events::typed(listener.into_listener(), events::rotation),
        )
    }

    /// An assistive technology performed an action (its name).
    fn on_accessibility_action<M>(self, listener: impl IntoListener<str, M>) -> Self {
        self.on(
            EventKind::AccessibilityAction,
            events::typed(listener.into_listener(), events::action),
        )
    }

    /// Handles native event `kind` with its raw payload (the element
    /// listens to it).
    fn on<M>(mut self, kind: EventKind, listener: impl IntoListener<EventPayload, M>) -> Self {
        self.node().handlers.push((kind, listener.into_listener()));
        self
    }

    /// Presses also count this far outside it.
    fn hit_slop(mut self, insets: EdgeInsets) -> Self {
        super::host::set(self.node(), Prop::HitSlop(insets));
        self
    }

    /// How long a press must last to be a long press, in milliseconds.
    fn long_press_delay(mut self, ms: f32) -> Self {
        super::host::set(self.node(), Prop::LongPressDelay(Some(ms)));
        self
    }

    /// The press ripple (Android).
    fn ripple(mut self, ripple: crate::props::Ripple) -> Self {
        super::host::set(self.node(), Prop::Ripple(Some(ripple)));
        self
    }

    /// The elevation shadow (Android).
    fn elevation(mut self, elevation: impl Into<Points>) -> Self {
        super::host::set(self.node(), Prop::Elevation(elevation.into()));
        self
    }

    /// The system material of a backdrop blur (iOS).
    fn backdrop_material(mut self, material: crate::BackdropMaterial) -> Self {
        super::host::set(self.node(), Prop::BackdropMaterial(material));
        self
    }

    /// The id UI tests find it by.
    fn test_id(mut self, id: &str) -> Self {
        super::host::set(self.node(), Prop::TestId(Some(id.into())));
        self
    }

    /// Whether a desktop press drags the window and a double click follows
    /// the system's title-bar behavior. The nearest explicit value wins;
    /// inputs and press/touch handlers inside a drag region stay interactive.
    /// Set false on a child to exclude it from its parent's drag region.
    fn window_drag(mut self, enabled: impl IntoReactive<bool>) -> Self {
        super::host::bind_prop(self.node(), enabled, |enabled| {
            Prop::WindowDrag(Some(enabled))
        });
        self
    }

    /// Whether a view that takes presses is a stop of the keyboard focus
    /// (Tab; default true): false leaves it to the pointer, as a backdrop
    /// that closes a popover is.
    fn focusable(mut self, focusable: impl IntoReactive<bool>) -> Self {
        super::host::bind_prop(self.node(), focusable, |focusable| {
            Prop::Focusable(Some(focusable))
        });
        self
    }

    /// The id inputs name an input accessory by (`nativeID`).
    fn native_id(mut self, id: &str) -> Self {
        super::host::set(self.node(), Prop::NativeId(Some(id.into())));
        self
    }

    /// Styles while pressed (native: nothing runs).
    fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        let s = f(StyleRefinement::default());
        self.style().extend_as(Variant::ACTIVE, s);
        self
    }

    /// Styles while a pointer hovers it (iPad, Mac, mice).
    fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        let s = f(StyleRefinement::default());
        self.style().extend_as(Variant::HOVER, s);
        self
    }

    /// Styles while focused.
    fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        let s = f(StyleRefinement::default());
        self.style().extend_as(Variant::FOCUS, s);
        self
    }

    /// Styles while [`Interactive::disabled`] (Tailwind's `disabled:`).
    fn disabled_style(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        let s = f(StyleRefinement::default());
        self.style().extend_as(Variant::DISABLED, s);
        self
    }

    /// No presses nor press feedback; screen readers say it is disabled.
    fn disabled(mut self, disabled: impl IntoReactive<bool>) -> Self {
        self.node().bind_attr(disabled, |a, disabled| {
            a.disabled = disabled;
            a.state.disabled = disabled;
        });
        self
    }

    /// Which touches it and its children take.
    fn pointer_events(mut self, pointer_events: impl IntoReactive<PointerEvents>) -> Self {
        self.node()
            .bind_attr(pointer_events, |a, p| a.pointer_events = p);
        self
    }

    /// An app's style while selected (`accessibility_state` `selected`;
    /// TS: a `Button`'s `selectedStyle`), as `selected:` classes apply.
    fn selected_style(mut self, style: impl IntoReactive<Style>) -> Self {
        Style::apply(self.node(), style, Variant::SELECTED, |a, patch| {
            super::host::set_in(&mut a.props, Prop::SelectedStyle(patch))
        });
        self
    }

    /// An app's style while checked (`accessibility_state` `checked`, not
    /// mixed: a checkbox's, a switch's, a radio's), as `checked:` classes
    /// apply.
    fn checked_style(mut self, style: impl IntoReactive<Style>) -> Self {
        Style::apply(self.node(), style, Variant::CHECKED, |a, patch| {
            super::host::set_in(&mut a.props, Prop::CheckedStyle(patch))
        });
        self
    }

    /// An app's style while indeterminate (`accessibility_state` `checked`
    /// mixed: a checkbox's `indeterminate`), as `indeterminate:` classes
    /// apply.
    fn indeterminate_style(mut self, style: impl IntoReactive<Style>) -> Self {
        Style::apply(self.node(), style, Variant::INDETERMINATE, |a, patch| {
            super::host::set_in(&mut a.props, Prop::IndeterminateStyle(patch))
        });
        self
    }

    /// Styles it as checked while `checked` holds (its `checked:` classes
    /// and [`Self::checked_style`]), as a part of a control that is: a
    /// switch's track and thumb, a checkbox's mark (`gpui-base`'s
    /// `SwitchThumb::new(checked)`). Screen readers hear it from the
    /// control alone, so the part is no second checkable element.
    fn project_checked(mut self, checked: impl IntoReactive<bool>) -> Self {
        self.node().bind_attr(checked, |a, checked| {
            project(a, crate::tailwind::state::CHECKED, checked)
        });
        self
    }

    /// Styles it as indeterminate while `indeterminate` holds (its
    /// `indeterminate:` classes and [`Self::indeterminate_style`]), as a
    /// part of a control that is: a checkbox's indicator (`gpui-base`'s
    /// `CheckboxIndicator::state(CheckboxState::Indeterminate)`). Screen
    /// readers hear it from the control alone.
    fn project_indeterminate(mut self, indeterminate: impl IntoReactive<bool>) -> Self {
        self.node().bind_attr(indeterminate, |a, indeterminate| {
            project(a, crate::tailwind::state::INDETERMINATE, indeterminate)
        });
        self
    }

    /// Styles it as disabled while `disabled` holds (its `disabled:`
    /// classes and [`Self::disabled_style`]), as a part of a disabled
    /// control (`gpui-base`'s `SwitchThumb::disabled`). The control takes
    /// no presses and tells screen readers; the part keeps its own.
    fn project_disabled(mut self, disabled: impl IntoReactive<bool>) -> Self {
        self.node().bind_attr(disabled, |a, disabled| {
            project(a, crate::tailwind::state::DISABLED, disabled)
        });
        self
    }
}

/// Sets or clears `bit` of the states an element shows as a part.
fn project(a: &mut Attrs, bit: u8, on: bool) {
    if on {
        a.projected_states |= bit;
    } else {
        a.projected_states &= !bit;
    }
}

macro_rules! element {
    ($(#[$doc:meta])* $name:ident) => {
        $(#[$doc])*
        #[derive(Clone)]
        pub struct $name(ElNode);

        impl Element for $name {
            fn node(&mut self) -> &mut ElNode {
                &mut self.0
            }
        }

        impl IntoElement for $name {
            fn into_element(self) -> AnyElement {
                AnyElement(self.0)
            }
        }

        impl Accessible for $name {}

        impl IntoTooltip for $name {
            fn apply_tooltip(self, node: &mut ElNode) {
                self.into_element().apply_tooltip(node);
            }
        }
    };
}

element! {
    /// A view (`<view>`), made by [`view`].
    View
}

element! {
    /// A text block (`<text>`), made by [`text`].
    Text
}

element! {
    /// An image (`<image>`), made by [`image`].
    Image
}

element! {
    /// A slot's node as a view places it ([`ParentElement::slot`]):
    /// style it like a view; its children are the app's.
    SlotElement
}

element! {
    /// A list as a view lays it out ([`ParentElement::list`]).
    ListElement
}

impl Interactive for View {}
impl ParentElement for View {}
impl Interactive for SlotElement {}
impl Interactive for Text {}
impl Interactive for Image {}

/// A text's children are its spans, as nested `<text>`s in JS: texts with
/// styles and presses of their own (a link inside a sentence), strings,
/// and views and images placed inline, wrapping with the words. The core
/// merges them into one paragraph.
impl ParentElement for Text {
    fn child<M>(mut self, child: impl IntoChild<M>) -> Self {
        let at = self.0.children.len();
        child.__push_into(&mut self.0.children);
        for el in &mut self.0.children[at..] {
            *el = span(std::mem::replace(el, ElNode::new(Tag::View)));
        }
        self
    }

    fn children(mut self, children: impl IntoIterator<Item = impl IntoElement>) -> Self {
        self.0
            .children
            .extend(children.into_iter().map(|c| span(c.into_element().0)));
        self
    }
}

/// A child of a text: a bare string (a text with nothing of its own) is a
/// piece of its content, anything else a span or an inline element.
fn span(mut el: ElNode) -> ElNode {
    if el.tag == Tag::Text
        && matches!(el.kind, ElKind::Plain)
        && el.key.is_none()
        && el.children.is_empty()
        && el.handlers.is_empty()
        && el.style.is_empty()
        && el.attrs == Attrs::default()
        && !el.is_bound()
    {
        el.kind = ElKind::Raw;
    }
    el
}

impl Text {
    /// At most `lines` lines (0: no limit), the last one truncated.
    pub fn number_of_lines(mut self, lines: impl IntoReactive<u32>) -> Self {
        self.0
            .bind_attr(lines, |a, lines| a.number_of_lines = lines);
        self
    }

    /// Where a truncated line is cut.
    pub fn ellipsize_mode(mut self, mode: impl IntoReactive<crate::EllipsizeMode>) -> Self {
        super::host::bind_prop(&mut self.0, mode, Prop::EllipsizeMode);
        self
    }

    /// The user can select and copy it.
    pub fn selectable(mut self, selectable: impl IntoReactive<bool>) -> Self {
        super::host::bind_prop(&mut self.0, selectable, |s| Prop::Selectable(Some(s)));
        self
    }

    /// The highlight of its selection, when it is selectable.
    pub fn selection_color(mut self, color: impl IntoReactive<Hsla>) -> Self {
        super::host::bind_prop(&mut self.0, color, |c: Hsla| {
            Prop::SelectionColor(Some(Color::from(c)))
        });
        self
    }

    /// Shrinks the font, down to `min_scale` of it, to fit its lines.
    pub fn shrink_to_fit(mut self, min_scale: impl IntoReactive<f32>) -> Self {
        super::host::set(&mut self.0, Prop::AdjustsFontSizeToFit(true));
        super::host::bind_prop(&mut self.0, min_scale, Prop::MinimumFontScale);
        self
    }

    /// Whether its fonts follow the system's text size (true by default;
    /// TS: `allowFontScaling`).
    pub fn allow_font_scaling(mut self, allow: impl IntoReactive<bool>) -> Self {
        super::host::bind_prop(&mut self.0, allow, Prop::AllowFontScaling);
        self
    }

    /// At most this multiple of its fonts' sizes with the system's text
    /// size (TS: `maxFontSizeMultiplier`; 0: no limit).
    pub fn max_font_size_multiplier(mut self, multiplier: impl IntoReactive<f32>) -> Self {
        super::host::bind_prop(&mut self.0, multiplier, Prop::MaxFontSizeMultiplier);
        self
    }
}

impl Image {
    /// How it fits its box (default: cover).
    pub fn resize_mode(mut self, mode: impl IntoReactive<crate::ResizeMode>) -> Self {
        super::host::bind_prop(&mut self.0, mode, Prop::ResizeMode);
        self
    }

    /// Shown until it loads (a URL, an `asset:/` URI or an
    /// [`super::ImageAsset`]).
    pub fn placeholder(mut self, src: impl IntoReactive<super::ImageSrc>) -> Self {
        super::host::bind_prop(&mut self.0, src, |src: super::ImageSrc| {
            Prop::Image(crate::ImageProp::Placeholder(Some(ImageSource {
                uri: src.uri.to_arc(),
                size: src.size.map(|(w, h)| crate::geometry::size_pt(w, h)),
                scales: src.scales,
                data: None,
            })))
        });
        self
    }

    /// Draws its opaque pixels in `color` (a template image, an icon).
    pub fn tint_color(self, color: impl IntoReactive<super::AdaptiveColor>) -> Self {
        Styled::tint_color(self, color)
    }

    /// Blurs it.
    pub fn blur_radius(mut self, radius: impl Into<Points>) -> Self {
        super::host::set(
            &mut self.0,
            Prop::Image(crate::ImageProp::BlurRadius(radius.into())),
        );
        self
    }

    /// Fades it in over `ms` milliseconds once loaded.
    pub fn fade_duration(mut self, ms: impl IntoReactive<f32>) -> Self {
        super::host::bind_prop(&mut self.0, ms, |ms| {
            Prop::Image(crate::ImageProp::FadeDuration(Some(ms)))
        });
        self
    }

    /// It loaded: its size in pixels.
    pub fn on_load<M>(self, listener: impl IntoListener<Size<Points>, M>) -> Self {
        self.on(
            EventKind::Load,
            events::typed(listener.into_listener(), events::size),
        )
    }

    /// It failed to load (the message).
    pub fn on_error<M>(self, listener: impl IntoListener<str, M>) -> Self {
        self.on(
            EventKind::Error,
            events::typed(listener.into_listener(), events::message),
        )
    }

    /// It started loading.
    pub fn on_load_start<M>(self, listener: impl IntoListener<(), M>) -> Self {
        self.on(
            EventKind::LoadStart,
            events::typed(listener.into_listener(), events::unit),
        )
    }

    /// It finished loading, or failed.
    pub fn on_load_end<M>(self, listener: impl IntoListener<(), M>) -> Self {
        self.on(
            EventKind::LoadEnd,
            events::typed(listener.into_listener(), events::unit),
        )
    }

    /// Its download went on: bytes so far and in all (at most every 100
    /// ms; none for images already in memory).
    pub fn on_progress<M>(self, listener: impl IntoListener<events::ImageProgress, M>) -> Self {
        self.on(
            EventKind::Progress,
            events::typed(listener.into_listener(), events::image_progress),
        )
    }
}

impl super::Scrollable for ListElement {}

impl super::ListLayout for ListElement {
    fn config(&mut self) -> &mut ListConfig {
        match &mut self.0.kind {
            ElKind::List(list) => &mut list.config,
            _ => unreachable!("a ListElement is a list"),
        }
    }

    fn bound_config(&mut self) -> &mut Vec<BoundListConfig> {
        &mut self.0.bound_list
    }
}

/// A named slot (an `Option<Slot>` prop): `Some` while the app passes
/// content for it. Placed with [`ParentElement::slot`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Slot {
    name: &'static str,
}

impl Slot {
    pub(crate) fn new(name: &'static str) -> Self {
        Self { name }
    }

    /// Its name (its prop's, in JS).
    pub fn name(&self) -> &'static str {
        self.name
    }

    /// The slot's node, to style and place (its children are the app's).
    pub fn element(self) -> SlotElement {
        let mut node = ElNode::new(Tag::View);
        node.kind = ElKind::Slot(self.name);
        SlotElement(node)
    }
}

/// The app's virtualized rows (an `Items` prop): in JS the component takes
/// `each` and a row renderer like `List`. Placed with
/// [`ParentElement::list`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Items {
    name: &'static str,
}

impl Items {
    #[doc(hidden)]
    pub const fn __new(name: &'static str) -> Self {
        Self { name }
    }
}

/// A piece of the app's content to place: one JSX child
/// ([`ParentElement::children_each`]) or a list row's content
/// ([`ParentElement::list`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Child(ChildRef);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ChildRef {
    Js(NodeId),
    Row,
}

impl Child {
    pub(crate) fn js(id: NodeId) -> Self {
        Self(ChildRef::Js(id))
    }

    pub(crate) fn row() -> Self {
        Self(ChildRef::Row)
    }
}

impl IntoElement for Child {
    fn into_element(self) -> AnyElement {
        let mut node = ElNode::new(Tag::View);
        node.kind = match self.0 {
            ChildRef::Js(id) => ElKind::JsChild(id),
            ChildRef::Row => ElKind::Children,
        };
        AnyElement(node)
    }
}

/// A view.
pub fn view() -> View {
    View(ElNode::new(Tag::View))
}

/// A text block showing `content`, then its children (spans:
/// [`ParentElement`] for [`Text`]).
pub fn text(content: impl IntoReactive<SharedString>) -> Text {
    let mut node = ElNode::new(Tag::Text);
    match content.into_reactive() {
        Reactive::Static(content) => node.text = Some(content),
        Reactive::Bound(content) => {
            node.text = Some(SharedString::default());
            node.bound_text = Some(content);
        }
    }
    Text(node)
}

/// An image loading `src`: a URL, a file path or `asset:/` URI, or an
/// [`super::ImageAsset`] (`asset!("logo@2x.png")`), which lays out at its
/// natural size before it loads.
pub fn image(src: impl IntoReactive<super::ImageSrc>) -> Image {
    let mut node = ElNode::new(Tag::Image);
    node.bind_attr(src, |a, src| a.src = Some(src));
    Image(node)
}

/// The JSX children of a component (a `Children` prop): placed where the
/// view puts it, among the view's own children.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Children {
    present: bool,
}

impl Children {
    pub(crate) fn new(present: bool) -> Self {
        Self { present }
    }

    /// The app passed none.
    pub fn is_empty(&self) -> bool {
        !self.present
    }
}

impl IntoElement for Children {
    fn into_element(self) -> AnyElement {
        let mut node = ElNode::new(Tag::View);
        node.kind = ElKind::Children;
        AnyElement(node)
    }
}

/// Another component in a view ([`Component::element`]). Its styles go
/// after the component's own, like an app's classes, and so do its inline
/// style, animated values and insets ([`Element`]'s), on the component's
/// root; its state styles ([`Self::active`], [`Self::hover`],
/// [`Self::focus`], [`Self::disabled_style`], [`Styled::selected`],
/// [`Styled::checked`]) follow the states of that root, as an app's
/// `active:` / `hover:` classes on a TSX component do. It hears and is to
/// assistive technologies what the component says (its events and props):
/// it is not [`Interactive`] or [`Accessible`]. The element a
/// `#[component]`'s constructor makes (`button()`) wraps one
/// ([`ComponentBuilder`]).
pub struct ComponentElement<C: Component> {
    node: ElNode,
    _component: PhantomData<fn() -> C>,
}

impl<C: Component> ComponentElement<C> {
    /// The element of `C` given `props`: what a component's constructor
    /// (`button()`) and [`Component::element`] start from.
    #[doc(hidden)]
    pub fn __new(props: C::Props) -> Self {
        Self::new(props)
    }

    /// Its props, for the setters a `#[component]` makes on its element.
    #[doc(hidden)]
    pub fn __props_mut(&mut self) -> &mut C::Props {
        match &mut self.node.kind {
            ElKind::Component(c) => c
                .props
                .as_any_mut()
                .downcast_mut::<C::Props>()
                .expect("a component element holds its component's props"),
            _ => unreachable!("a component element is a component's"),
        }
    }

    pub(crate) fn new(props: C::Props) -> Self {
        let mut node = ElNode::new(Tag::View);
        node.kind = ElKind::Component(ComponentEl {
            name: super::component::name_of::<C>(),
            make: super::component::make_instance::<C>,
            spec: ComponentSpec::of::<C>,
            props: Box::new(props),
            on_event: Vec::new(),
        });
        Self {
            node,
            _component: PhantomData,
        }
    }

    /// Fills its slot `slot` (its prop's JS name: `header`) with `content`
    /// (in place of what filled it): the component's view places it as it
    /// is where it places the slot, with no node around it (a node of its
    /// own only where the view styles the slot).
    pub fn fill(mut self, slot: &'static str, content: impl IntoElement) -> Self {
        self.node
            .children
            .retain(|c| !matches!(c.kind, ElKind::Fill(s) if s == slot));
        let mut fill = ElNode::new(Tag::View);
        fill.kind = ElKind::Fill(slot);
        fill.children.push(content.into_element().0);
        self.node.children.push(fill);
        self
    }

    /// Fills its `Items` prop with rows of this view (TS: `each` and the
    /// row renderer, `keyExtractor`, `header`, ...): the rows,
    /// their keys, header, footer, empty view, separators, sticky rows,
    /// viewability, the refresh and the end reached are `list`'s, rendered
    /// and handled by this component; the layout and the styles are the
    /// placing component's ([`ParentElement::list`]), so `list`'s own are
    /// left out.
    pub fn items(mut self, list: super::VirtualList) -> Self {
        match <C::Props as super::Props>::ITEMS {
            Some(name) => self.node.children.push(list.into_items(name)),
            None => {
                log::error!(target: "js", "{} has no `Items` prop to fill", super::component::name_of::<C>())
            }
        }
        self
    }

    /// Styles while its root is pressed (Tailwind's `active:`; TS: an
    /// `active:` class or `activeStyle` given the component), applied
    /// natively. The root follows presses when it handles them (a
    /// [`Interactive::on_press`] of the component's view).
    pub fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        let s = f(StyleRefinement::default());
        self.node.style.extend_as(Variant::ACTIVE, s);
        self
    }

    /// Styles while a pointer hovers its root (`hover:`).
    pub fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        let s = f(StyleRefinement::default());
        self.node.style.extend_as(Variant::HOVER, s);
        self
    }

    /// Styles while its root has focus (`focus:`).
    pub fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        let s = f(StyleRefinement::default());
        self.node.style.extend_as(Variant::FOCUS, s);
        self
    }

    /// Styles while its root is disabled (`disabled:`): the component's
    /// view disables it ([`Interactive::disabled`], a `disabled` prop).
    pub fn disabled_style(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        let s = f(StyleRefinement::default());
        self.node.style.extend_as(Variant::DISABLED, s);
        self
    }

    /// Puts `handle` on this component: its updates go to it.
    pub fn component_ref(mut self, handle: &super::ComponentRef<C>) -> Self {
        self.node.attrs.element_ref = handle.element().target();
        self
    }

    /// Handles its events (they then do not reach the app). Given several
    /// times, every listener hears them, in order. The element a
    /// `#[component]`'s constructor makes has a listener method per event
    /// instead (`.on_press(..)`: [`ComponentBuilder`]); this stays for
    /// components implemented by hand.
    pub fn on_event<M>(mut self, listener: impl IntoListener<C::Event, M>) -> Self {
        let listener = listener.into_listener();
        if let ElKind::Component(c) = &mut self.node.kind {
            c.on_event.push(listener.wrap(|inner, event: &dyn Any| {
                if let Some(event) = event.downcast_ref::<C::Event>() {
                    inner(event);
                }
            }));
        }
        self
    }

    /// Handles the events `pick` picks, with the data it gives: the listener
    /// method of one event (`on_press`) that `#[derive(Event)]` generates
    /// calls it.
    #[doc(hidden)]
    pub fn __on_variant<T: ?Sized + 'static, M>(
        self,
        pick: fn(&C::Event) -> Option<&T>,
        listener: impl IntoListener<T, M>,
    ) -> Self {
        let listener = listener.into_listener();
        self.on_event(listener.wrap(move |inner, event: &C::Event| {
            if let Some(data) = pick(event) {
                inner(data);
            }
        }))
    }
}

/// The element of a `#[component]`, as its constructor makes it: a
/// component is made and set up in Rust as a primitive is (`view()`),
/// as JSX does in TSX. `#[component] fn Picker` makes `picker()` (the
/// function's name in snake case), which returns a `PickerElement` with
/// each prop at its default and
///
/// - a setter per prop, named as its field (`.disabled(..)`): a value, a
///   signal, a memo or a prop of the field's type, or a closure returning
///   one (bound); an `Option<T>` prop takes a `T` (`.label("Pick one")`),
///   a slot the content to fill it with (`.header(text(..))`), the
///   children are its [`ParentElement::child`]ren;
/// - a listener method per event, named as its TSX handler in snake case
///   (`onSelect` is `.on_select(..)`): a unit variant's takes
///   `move || ..`, a one-field variant's `move |value: &T| ..`, a
///   named-field variant's the event itself;
/// - styles ([`Styled`]: they go on the component's root), with this
///   trait's for the root's states, and a
///   [`ComponentRef`](super::ComponentRef).
///
/// A prop's setter comes before an element method of the same name (a
/// component's `selected` prop before [`Styled::selected`], its `size`
/// before [`Styled::size`]; call those as `Styled::selected(element, ..)`).
///
/// ```
/// use nsh_core::prelude::*;
///
/// /// The props of [`Picker`].
/// #[derive(Clone, PartialEq, Props)]
/// pub struct PickerProps {
///     /// The options.
///     pub options: Prop<Vec<SharedString>>,
///     /// No presses.
///     #[prop(default)]
///     pub disabled: Prop<bool>,
///     /// What screen readers say.
///     pub label: Prop<Option<SharedString>>,
/// }
///
/// /// What a [`Picker`] reports.
/// #[derive(Event)]
/// pub enum PickerEvent {
///     /// Pressed.
///     Press,
///     /// An option was picked: its index.
///     Select(u32),
/// }
///
/// /// Picks an option.
/// #[component]
/// pub fn Picker(props: PickerProps, cx: Scope<PickerEvent>) -> impl IntoElement {
///     view()
///         .disabled(props.disabled)
///         .on_press(move || cx.emit(PickerEvent::Select(0)))
/// }
///
/// #[component]
/// pub fn Form() -> impl IntoElement {
///     let busy = signal(false);
///     let picked = signal(None::<u32>);
///     view().child(
///         picker()
///             .options(vec![SharedString::from("Tea"), "Coffee".into()])
///             .disabled(move || busy.get())
///             .label("Drink")
///             .on_press(move || log::info!("pressed"))
///             .on_select(move |&index: &u32| picked.set(Some(index)))
///             .px_4()
///             .active(|s| s.opacity(0.6)),
///     )
/// }
/// ```
pub trait ComponentBuilder: Element {
    /// Its component.
    type Component: Component;

    /// The element it builds.
    #[doc(hidden)]
    fn __element(&mut self) -> &mut ComponentElement<Self::Component>;

    /// Styles while its root is pressed (Tailwind's `active:`; TS: an
    /// `active:` class or `activeStyle` given the component), applied
    /// natively. The root follows presses when it handles them (a
    /// [`Interactive::on_press`] of the component's view).
    fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        let s = f(StyleRefinement::default());
        self.node().style.extend_as(Variant::ACTIVE, s);
        self
    }

    /// Styles while a pointer hovers its root (`hover:`).
    fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        let s = f(StyleRefinement::default());
        self.node().style.extend_as(Variant::HOVER, s);
        self
    }

    /// Styles while its root has focus (`focus:`).
    fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        let s = f(StyleRefinement::default());
        self.node().style.extend_as(Variant::FOCUS, s);
        self
    }

    /// Styles while its root is disabled (`disabled:`): the component's
    /// view disables it ([`Interactive::disabled`], a `disabled` prop).
    fn disabled_style(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        let s = f(StyleRefinement::default());
        self.node().style.extend_as(Variant::DISABLED, s);
        self
    }

    /// Puts `handle` on this component: its updates go to it.
    fn component_ref(mut self, handle: &super::ComponentRef<Self::Component>) -> Self {
        self.__element().node.attrs.element_ref = handle.element().target();
        self
    }
}

impl<C: Component> Element for ComponentElement<C> {
    fn node(&mut self) -> &mut ElNode {
        &mut self.node
    }
}

/// The content of a nested component: `.child(..)` / `.children(..)` are
/// its `Children`, [`ComponentElement::fill`] its slots, placed where its
/// view places the app's.
impl<C: Component> ParentElement for ComponentElement<C> {}

impl<C: Component> IntoElement for ComponentElement<C> {
    fn into_element(self) -> AnyElement {
        AnyElement(self.node)
    }
}