compose-rust 0.0.11

Declarative UI in Rust, rendered by an AOT-compiled Compose Multiplatform engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
package dev.darkpyonix.composerust.ui.node

import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import dev.darkpyonix.composerust.foundation.ChromeInsets
import dev.darkpyonix.composerust.foundation.MessageQueue
import dev.darkpyonix.composerust.foundation.ScrollPort
import dev.darkpyonix.composerust.foundation.ScrollReports
import dev.darkpyonix.composerust.foundation.ScrollRequest
import dev.darkpyonix.composerust.protocol.ScrollWire
import dev.darkpyonix.composerust.protocol.ContentScale
import dev.darkpyonix.composerust.protocol.Mutation
import dev.darkpyonix.composerust.protocol.OverlayTokens
import dev.darkpyonix.composerust.protocol.PropertyKind
import dev.darkpyonix.composerust.protocol.PropertyValue
import dev.darkpyonix.composerust.protocol.TextAlign
import dev.darkpyonix.composerust.protocol.Theme
import dev.darkpyonix.composerust.protocol.Window
import dev.darkpyonix.composerust.protocol.WidgetKind
import dev.darkpyonix.composerust.protocol.Modifier as ProtocolModifier
import dev.darkpyonix.composerust.foundation.code.CodeEditorModel
import dev.darkpyonix.composerust.foundation.code.CodePosition
import dev.darkpyonix.composerust.foundation.codeEvents
import dev.darkpyonix.composerust.foundation.contentPaddingDp
import dev.darkpyonix.composerust.foundation.wordWrapRule
import dev.darkpyonix.composerust.foundation.fontFeatureSettings
import dev.darkpyonix.composerust.foundation.spanFontFeatureSettings
import dev.darkpyonix.composerust.protocol.CodeEditorRecords
import dev.darkpyonix.composerust.protocol.HostEvent
import dev.darkpyonix.composerust.ui.SEMANTICS_STATE_MASK

/**
 * Text the Host pushed into a TextField with `SetText`.
 *
 * The field is uncontrolled: the Renderer owns the edit value and the composition state, and
 * this is the only way the Host changes it. If an IME composition is in progress the change
 * waits until the composition commits, because replacing text mid-composition destroys the
 * syllable being assembled.
 */
data class HostText(
    val text: String,
    val selectionStart: Int,
    val selectionEnd: Int,
    /** Increments on every `SetText`, so re-sending the same text still applies. */
    val revision: Long,
)

/**
 * One interpreted node. Each field is its own snapshot state, so a `SetProp` on one node
 * invalidates only the composables that read that node.
 */
class Node internal constructor(val id: Int, val widget: WidgetKind) {
    internal val props = mutableStateMapOf<PropertyKind, PropertyValue>()
    internal val modifiers = mutableStateListOf<ProtocolModifier>()
    internal val children = mutableStateListOf<Int>()
    internal var parentId: Int = NodeTable.ROOT_ID
    internal var hostText by mutableStateOf<HostText?>(null)
    internal var focusRequestRevision by mutableStateOf(0L)

    /**
     * The node's font features as Compose's `fontFeatureSettings` string, or null for none.
     * Made once when the `FontFeatureSettings` property changes and read as it is on every
     * frame after, so a text whose value changes builds no string for its features.
     */
    internal var fontFeatures by mutableStateOf<String?>(null)

    /**
     * The OpenType features runs of a `Text` name for themselves, as Compose's
     * `fontFeatureSettings` strings by run index, or null where no run names any or the
     * table could not be read. Made once when the `SpanFontFeatures` property changes and
     * read as it is on every composition, so drawing the text parses nothing.
     */
    internal var spanFontFeatures by mutableStateOf<Map<Int, String>?>(null)

    /**
     * A code editor's document and everything over it. Created with the node and kept for
     * its life, because the document is the Renderer's from the moment it opens: the Host
     * feeds it through the node's properties and its edit records, and never replaces it.
     */
    internal val code: CodeEditorModel? =
        if (widget == WidgetKind.CodeEditor) CodeEditorModel() else null

    /**
     * What a scroll container holds for the Host's scroll requests and for its reports.
     * Created with the node, because a request can arrive in the batch that creates it.
     */
    internal val scroll: ScrollPort? = if (widget in NodeTable.SCROLL_CONTAINERS) ScrollPort(id) else null

    /**
     * Set while a scroll request needs to know where this node is drawn. Nothing records
     * that for a node nobody asked about.
     */
    internal var placementWanted by mutableStateOf(false)
    internal var placement: androidx.compose.ui.layout.LayoutCoordinates? = null

    /**
     * A `NativeView`'s platform view: the handle the Host attached, 0 for none. The view is
     * put into the window, and taken out, where the node is drawn.
     */
    internal var nativeHandle by androidx.compose.runtime.mutableLongStateOf(0L)

    /** The last `FocusNativeView` for this node, or null before one came. */
    internal var nativeFocusRequest by mutableStateOf<NativeFocusRequest?>(null)

    fun property(kind: PropertyKind): PropertyValue? = props[kind]

    fun text(kind: PropertyKind, default: String = ""): String =
        (props[kind] as? PropertyValue.Text)?.value ?: default

    fun flag(kind: PropertyKind, default: Boolean): Boolean =
        (props[kind] as? PropertyValue.Bool)?.value ?: default

    /** Handler ids arrive as integer property values. */
    fun handler(kind: PropertyKind): Long? = (props[kind] as? PropertyValue.Integer)?.value

    fun number(kind: PropertyKind): Float? = (props[kind] as? PropertyValue.Float)?.value

    /** The raw bytes of a byte-valued property, such as a Canvas command list. */
    fun bytes(kind: PropertyKind): ByteArray? = (props[kind] as? PropertyValue.Bytes)?.value
}

/**
 * A request to move keyboard focus into a platform view ([focus] true) or out of it. The
 * revision makes a second request of the same kind a new one.
 */
data class NativeFocusRequest(val focus: Boolean, val revision: Long)

/** A protocol violation that must become a `ProtocolError` event, never a crash. */
data class TableError(val code: Int, val message: String) {
    companion object {
        const val UNKNOWN_NODE = 1
        const val DUPLICATE_NODE = 2
        const val UNSUPPORTED_PROPERTY = 3
        const val INVALID_INDEX = 4
        const val UNKNOWN_ASSET = 5
        const val UNSUPPORTED_ASSET = 6
        const val UNREADABLE_ASSET = 7
        const val CYCLIC_INSERT = 8
        const val INVALID_PALETTE = 9
        const val INVALID_CHILDREN = 10
        const val INVALID_ANIMATION = 11
        /** A code editor record or edit whose range, version or kind could not be honoured. */
        const val INVALID_CODE_EDIT = 12
        /** A `Window` node placed anywhere but directly under the root. */
        const val MISPLACED_WINDOW = 13
        /** A node moved from the content of one `Window` into another, or out of its window. */
        const val MOVE_BETWEEN_WINDOWS = 14
        /** A file dialog request whose number is still waiting for its answer. The command is dropped. */
        const val DUPLICATE_FILE_DIALOG = 15
        /** A modifier whose value could not be honoured. The modifier is dropped. */
        const val INVALID_MODIFIER = 16
        /** An input or output request whose number is still waiting for its answer. The command is dropped. */
        const val DUPLICATE_IO_REQUEST = 17
        /** A file operation whose request number is still waiting for its answer. The command is dropped. */
        const val DUPLICATE_FILE_CALL = 18
        /** A scroll request for a node that is not a scroll container. The request is dropped. */
        const val NOT_SCROLLABLE = 19
        /** A scroll request whose values make no sense for the container. The request is dropped. */
        const val INVALID_SCROLL = 20
        /** A platform view command for a node that is not a `NativeView`. The command is dropped. */
        const val NOT_NATIVE_VIEW = 21
        /**
         * A platform view the window would not take: not a view of this platform, or one
         * already attached to another node. The command is dropped.
         */
        const val NATIVE_VIEW_REJECTED = 22
        /** A platform view command where the window has no place for platform views. The command is dropped. */
        const val NATIVE_VIEW_UNSUPPORTED = 23
        /** A font feature list that cannot be read. The text is drawn without features. */
        const val INVALID_FONT_FEATURES = 24
    }
}

/**
 * The interpreted node tree.
 *
 * A whole batch is applied inside one `Snapshot.withMutableSnapshot` transaction by
 * [ComposeRustHost], so no intermediate tree state is ever drawn.
 */
class NodeTable {
    private val nodes = mutableStateMapOf<Int, Node>()

    /** The code editors among them, so that what they report is collected without a walk of the tree. */
    private val codeNodes = LinkedHashMap<Int, Node>()
    private val rootChildren = mutableStateListOf<Int>()
    private val errors = mutableListOf<TableError>()

    /**
     * The theme each `Window` node overrides the application's with. A window with
     * no entry follows [theme]. Dropped with the node, so a closed window leaves nothing here.
     */
    private val windowThemes = mutableStateMapOf<Int, Theme>()

    /** The `FocusWindow` commands of the batch just applied; the window manager drains them. */
    private val focusRequests = mutableListOf<Int>()

    /**
     * What applying a batch gave a code editor to say: the change an edit from the Host
     * made, or the edit it refused. Sent after the batch is applied, never in the middle of
     * it, so the Host is not entered while it is still writing.
     */
    private val outbox = mutableListOf<HostEvent>()

    /**
     * The registered assets. Pictures outlive the batch that carried them, so they are not
     * part of a node: a node names an id and the cache answers with what was registered.
     */
    val assets: AssetCache = AssetCache()

    /**
     * The transient messages the Host has asked to say.
     *
     * Not part of the node tree, because a message is not part of the tree: it has a
     * lifetime rather than a position, and that lifetime is owned here.
     */
    val messages: MessageQueue = MessageQueue()

    /**
     * What the screen's own chrome is using along the window's edges.
     *
     * Only a message reads it, and only so that it does not cover the destinations it is
     * drawn over. It is not part of the tree and never crosses the boundary.
     */
    val insets: ChromeInsets = ChromeInsets()

    /**
     * The notifications the Host has asked to show, and the platform's answers on their way
     * back.
     *
     * Not cleared with the tree. A resync throws the nodes away, but what is showing in the
     * notification centre and what the Host has been told about permission are still true,
     * and starting them over would tell the Host something it already knows or forget a
     * press that has not been delivered yet.
     */
    /**
     * The animations the Host handed over to play, and what each node shows because of
     * them. The Host is not called while they play unless an animation asked for events.
     */
    val animations: AnimationTable = AnimationTable(this)

    /**
     * Pointer input for the nodes that subscribed to it: who holds a press, and the moves
     * and wheel travel waiting for the next frame.
     */
    internal val pointers: dev.darkpyonix.composerust.foundation.PointerRouter =
        dev.darkpyonix.composerust.foundation.PointerRouter { id -> nodes.containsKey(id) }

    val notifications: dev.darkpyonix.composerust.ui.platform.NotificationCenter =
        dev.darkpyonix.composerust.ui.platform.NotificationCenter()

    /**
     * The file dialogs the Host has asked for, and their answers on the way back.
     *
     * Not cleared with the tree, for the reason the notifications are not: a request the Host
     * is waiting on is still owed its one answer after a resync.
     */
    val fileDialogs: dev.darkpyonix.composerust.ui.platform.FileDialogCenter =
        dev.darkpyonix.composerust.ui.platform.FileDialogCenter()

    /**
     * The operations the Host asked of files this side holds handles for, and their answers.
     * Not cleared with the tree, for the reason the dialogs are not: every request is owed its
     * one answer after a resync too.
     */
    val fileCalls: dev.darkpyonix.composerust.ui.platform.FileCallCenter =
        dev.darkpyonix.composerust.ui.platform.FileCallCenter()

    /**
     * Where the text the Host asks to copy goes. Not part of the tree and not cleared with
     * it: the clipboard is the platform's.
     */
    val clipboard: dev.darkpyonix.composerust.ui.platform.ClipboardWriter =
        dev.darkpyonix.composerust.ui.platform.ClipboardWriter()

    /**
     * The clipboard reads the Host asked for, each answered once with what the clipboard
     * held. Not part of the tree either.
     */
    val clipboardReads: dev.darkpyonix.composerust.ui.platform.ClipboardReader =
        dev.darkpyonix.composerust.ui.platform.ClipboardReader()

    /**
     * The storage operations the Host asked this side to carry out, each answered once.
     * Not cleared with the tree: a request the Host waits on is owed its answer after a
     * resync too.
     */
    val storage: dev.darkpyonix.composerust.ui.platform.StorageCenter =
        dev.darkpyonix.composerust.ui.platform.StorageCenter()

    /**
     * Counts the batches applied, so a scroll container can tell whether its own stated
     * move and a scroll request came in the same one.
     */
    private var batchSerial = 0L

    /** The scroll reports waiting for the next frame, from the containers the Host listens to. */
    internal val scrolls: ScrollReports = ScrollReports()

    private var revision = 0L

    /**
     * The `NativeView` node each attached platform view handle belongs to. A handle is in one
     * place at a time, so a second node asking for it is refused.
     */
    private val nativeHandles = HashMap<Long, Int>()

    /**
     * Increments on each `RequestFocus` for a node that is not a platform view, so a
     * platform view holding keyboard focus gives it back to the scene first.
     */
    var sceneFocusRevision by mutableStateOf(0L)
        private set

    /** Increments on each `RequestFocus(0)` so the root composition clears active focus. */
    var focusClearRevision by mutableStateOf(0L)
        private set

    /** Null until the Host sends its first `SetTheme` record. */
    var theme: Theme? by mutableStateOf(null)
        private set

    /** Null until the Host sends an overlay tokens record. */
    var overlayTokens: OverlayTokens? by mutableStateOf(null)
        private set

    /** Null until the Host sends its first `SetWindow` record. */
    var window: Window? by mutableStateOf(null)
        private set

    /** The theme the window drawn for the `Window` node [windowId] uses; node 0 is the application's. */
    fun themeFor(windowId: Int): Theme? =
        if (windowId == ROOT_ID) theme else windowThemes[windowId] ?: theme

    /** True when [windowId] has a theme of its own over the application's. */
    internal fun overridesTheme(windowId: Int): Boolean = windowThemes.containsKey(windowId)

    /** How many windows hold a theme override; zero once they are all gone. */
    internal val windowThemeCount: Int get() = windowThemes.size

    /**
     * The zoom level the application asked for the window [windowId], or null where it left
     * the level to the renderer. Node 0 is the single window of `SetWindow`.
     */
    fun zoomLevelFor(windowId: Int): Int? {
        if (windowId == ROOT_ID) return window?.zoomLevel
        val level = (nodes[windowId]?.props?.get(PropertyKind.WindowZoomLevel) as? PropertyValue.Integer)?.value
        return level?.toInt()?.coerceIn(-8, 8)
    }

    /**
     * Whether the window [windowId] asked for every whole-dp change of its size. Node 0 is
     * the single window of `SetWindow`, which asks through its settings; a `Window` node
     * asks through its `report_size` property.
     */
    fun reportSizeFor(windowId: Int): Boolean {
        if (windowId == ROOT_ID) return window?.reportSize == true
        return node(windowId)?.flag(PropertyKind.ReportSize, false) == true
    }

    /**
     * The `Window` node the node [nodeId] is drawn in, itself included, or null for a node
     * that sits in none (every node of a single window application).
     */
    fun windowOf(nodeId: Int): Int? {
        var current = nodeId
        var steps = nodes.size + 1
        while (current != ROOT_ID && steps-- > 0) {
            val node = nodes[current] ?: return null
            if (node.widget == WidgetKind.Window) return current
            current = node.parentId
        }
        return null
    }

    /** The `FocusWindow` commands applied since the last call, in order. */
    internal fun drainFocusRequests(): List<Int> {
        if (focusRequests.isEmpty()) return emptyList()
        val drained = focusRequests.toList()
        focusRequests.clear()
        return drained
    }

    /** Top-level nodes, in creation order until the Host parents them. */
    val roots: List<Int> get() = rootChildren

    fun node(id: Int): Node? = nodes[id]

    /** Events code editors queued while a batch was applied; drained after it commits. */
    internal fun drainEvents(): List<HostEvent> {
        // What is reported as where it ended up, the selection and the lines on screen, is
        // said once for the whole batch: several moves in it are one report.
        if (codeNodes.isNotEmpty()) for (node in codeNodes.values.toList()) collectCode(node, includeReports = true)
        if (outbox.isEmpty()) return emptyList()
        val drained = outbox.toList()
        outbox.clear()
        return drained
    }

    /** Errors collected while applying a batch; drained after the transaction commits. */
    internal fun drainErrors(): List<TableError> {
        // Asked once the whole batch is in, because a batch may start an animation before
        // the record that gives it something to play on.
        animations.afterBatch { code, message -> fail(code, message) }
        batchSerial++
        if (errors.isEmpty()) return emptyList()
        val drained = errors.toList()
        errors.clear()
        return drained
    }

    /**
     * Throws the whole interpreted tree away.
     *
     * For a Renderer that is about to ask the Host to send all of it again: the Host
     * answers a resync by building the application from nothing, so the ids in that batch
     * start over and anything kept from before would collide with them.
     */
    fun clear() {
        // The windows go with the nodes, and a dialog open on one of them ends with a cancel.
        nodes.filterValues { it.widget == WidgetKind.Window }.keys.toList()
            .forEach(fileDialogs::windowRemoved)
        animations.clear()
        scrolls.clear()
        nodes.clear()
        codeNodes.clear()
        rootChildren.clear()
        errors.clear()
        outbox.clear()
        windowThemes.clear()
        focusRequests.clear()
        nativeHandles.clear()
        assets.clear()
        messages.clear()
        theme = null
        window = null
        materials = 0
        revision = 0
        focusClearRevision = 0
    }

    fun apply(mutation: Mutation) {
        when (mutation) {
            is Mutation.Create -> create(mutation)
            is Mutation.SetProp -> setProp(mutation)
            is Mutation.SetModifier -> setModifier(mutation)
            is Mutation.Insert -> insert(mutation.parentId, mutation.nodeId, mutation.index)
            is Mutation.Move -> insert(mutation.parentId, mutation.nodeId, mutation.index)
            is Mutation.Remove -> remove(mutation.nodeId)
            is Mutation.SetText -> setText(mutation)
            is Mutation.AppendText -> appendText(mutation)
            // One record changes the whole tree's appearance. `ComposeRustContent`
            // resolves it into tokens and rules, and Compose invalidates the readers.
            is Mutation.SetTheme -> {
                theme = mutation.theme
                // The entries that were wrong are already out of the palette and the rest
                // of it applies. Each one is still said, so an application whose brand
                // colour did not arrive can find out why.
                mutation.theme.paletteProblems.forEach { problem ->
                    fail(TableError.INVALID_PALETTE, "theme palette: $problem")
                }
            }
            is Mutation.SetOverlayTokens -> overlayTokens = mutation.overlay
            // Read before there is a window to apply it to. The platform layer asks for
            // this out of the first batch and builds the window from it, so by the time
            // the batch is applied the window already looks the way it says. Keeping it
            // here as well is what lets a later rebuild change it.
            is Mutation.SetWindow -> window = mutation.window
            // A theme for one window, and bringing one forward. The theme is kept with the
            // window's node and read by the scene drawn for it; the focus request waits for
            // the window manager, which owns the platform windows, and is acted on after
            // the batch.
            is Mutation.SetWindowTheme -> setWindowTheme(mutation)
            is Mutation.FocusWindow -> focusWindow(mutation)
            is Mutation.RequestFocus -> requestFocus(mutation)
            // The bytes are read here, once, and never again: what a frame carries is the
            // id. A kind this Renderer cannot read, or a release of something that was
            // never registered, is reported and the rest of the batch still applies.
            is Mutation.RegisterAsset ->
                assets.register(mutation.assetId, mutation.kind, mutation.bytes)?.let(errors::add)

            is Mutation.ReleaseAsset -> assets.release(mutation.assetId)?.let(errors::add)

            // A sentence to say, not a node to draw. It joins the line; how long it stays
            // and what happens to the one behind it is decided on this side.
            is Mutation.ShowMessage -> messages.post(
                mutation.handlerId,
                mutation.text,
                mutation.action,
                mutation.duration,
                mutation.vertical,
                mutation.horizontal,
                mutation.offsetDp,
            )

            // Outside the window altogether. The centre decides what the window's own state
            // says about it (whether it is active, whether permission has been asked) and
            // the platform shows it.
            is Mutation.PostNotification -> notifications.apply(mutation)
            is Mutation.WithdrawNotification -> notifications.withdraw(mutation.key)
            is Mutation.RequestNotificationPermission -> notifications.requestPermission()

            // Not a node either. A request on a window that is not there is answered as
            // unavailable, so the Host's one answer per request holds, and is reported too.
            is Mutation.ShowFileDialog -> showFileDialog(mutation)

            // Plain text for the platform clipboard. Nothing answers it, and where there is
            // no clipboard it does nothing.
            is Mutation.SetClipboardText -> clipboard.write(mutation.text)

            // A read is answered later, outside the batch, by one event with its number.
            is Mutation.ReadClipboard -> clipboardReads.request(mutation.request)

            // An operation on a file this side holds a handle for, answered later, outside the
            // batch, by one event with its number. Where nothing holds handles it is answered
            // as unavailable, which is still its one answer.
            is Mutation.FileCall -> fileCall(mutation)
            // Nothing answers a release, and a handle nobody holds is no error.
            is Mutation.ReleaseFileHandle -> fileCalls.release(mutation.handle)
            // Started now, while the value to write can still be read where the Host put it,
            // and answered later, outside the batch, by one event with its number.
            is Mutation.StorageCall -> storageCall(mutation)

            // Played on this side's frame clock until it ends. Whether the node has
            // somewhere to play it is asked once the whole batch is in.
            is Mutation.StartAnimation -> animations.start(mutation.animation)
            is Mutation.ControlAnimation -> animations.control(mutation)
            // A request against the editor's document, which the editor may refuse. What
            // it did is reported once the batch is applied.
            is Mutation.EditCode -> editCode(mutation)
            // Several edits as one, all of them or none, answered once whatever became of it.
            is Mutation.EditCodeGroup -> editCodeGroup(mutation)

            // The editor's selection and scroll are this side's, so these are requests
            // too: each is checked against the document and refused, with a protocol
            // error, where it names a place that is not in it.
            is Mutation.SetSelection -> codeNode(mutation.nodeId, "SetSelection")?.let { (node, code) ->
                code.hostSetSelection(
                    mutation.baseVersion,
                    mutation.anchorLine,
                    mutation.anchorColumn,
                    mutation.activeLine,
                    mutation.activeColumn,
                )
                collectCode(node)
            }
            is Mutation.RevealRange -> codeNode(mutation.nodeId, "RevealRange")?.let { (node, code) ->
                code.hostReveal(
                    mutation.baseVersion,
                    CodePosition(mutation.startLine, mutation.startColumn),
                    CodePosition(mutation.endLine, mutation.endColumn),
                    mutation.align,
                )
                collectCode(node)
            }
            // Every selection the Host asked for, the first the primary one. A list that is
            // not whole selections is reported here; the editor refuses one that names a
            // place the document does not have.
            is Mutation.SetSelections -> codeNode(mutation.nodeId, "SetSelections")?.let { (node, code) ->
                val bytes = mutation.selections
                if (bytes.isEmpty() || bytes.size % 16 != 0) {
                    fail(
                        TableError.INVALID_CODE_EDIT,
                        "SetSelections on node ${node.id}: ${bytes.size} bytes are not whole selections of 16",
                    )
                } else {
                    val values = IntArray(bytes.size / 4) { index ->
                        (bytes[index * 4].toInt() and 0xff) or
                            ((bytes[index * 4 + 1].toInt() and 0xff) shl 8) or
                            ((bytes[index * 4 + 2].toInt() and 0xff) shl 16) or
                            ((bytes[index * 4 + 3].toInt() and 0xff) shl 24)
                    }
                    code.hostSetSelections(mutation.baseVersion, values)
                    collectCode(node)
                }
            }
            // A snippet goes in as one edit and opens its tab stops; one that cannot be read,
            // or whose range the reader has typed over, is reported and dropped.
            is Mutation.InsertSnippet -> codeNode(mutation.nodeId, "InsertSnippet")?.let { (node, code) ->
                code.hostInsertSnippet(
                    mutation.baseVersion,
                    mutation.startLine,
                    mutation.startColumn,
                    mutation.endLine,
                    mutation.endColumn,
                    mutation.snippet,
                )
                collectCode(node)
            }
            is Mutation.SetFolded -> codeNode(mutation.nodeId, "SetFolded")?.let { (node, code) ->
                code.hostSetFolded(mutation.startLine, mutation.collapsed)
                collectCode(node)
            }
            // The offset waits in the model for the surface, which owns the scroll position
            // and knows how far the document lets it go.
            is Mutation.SetScrollOffset -> codeNode(mutation.nodeId, "SetScrollOffset")?.let { (node, code) ->
                code.hostScrollTo(mutation.offsetPx)
                collectCode(node)
            }
            // A question, answered exactly once: by the editor where it can find the
            // position, and here, with nothing on screen, where there is no editor to ask.
            is Mutation.QueryPositionRect -> {
                val found = codeNode(mutation.nodeId, "QueryPositionRect")
                if (found == null) {
                    outbox += HostEvent.PositionRect(mutation.nodeId, 0L, mutation.request, false, 0f, 0f, 0f, 0f)
                } else {
                    val (node, code) = found
                    code.hostQueryPosition(mutation.request, mutation.line, mutation.column)
                    collectCode(node)
                }
            }

            // Requests too: the scroll position is this side's. Each is checked here and
            // waits on the node for the container, which keeps it in range, may animate it,
            // and drops it while the user is scrolling.
            is Mutation.ScrollTo -> scrollTo(mutation)
            is Mutation.ScrollIntoView -> scrollIntoView(mutation)

            // A platform view the Host made, and focus moving into it or out. Checked here;
            // the view itself is put into the window where the node is drawn.
            is Mutation.AttachNativeView -> attachNativeView(mutation)
            is Mutation.FocusNativeView -> focusNativeView(mutation)
        }
    }

    /** The `NativeView` a command names, or null after reporting that it names none. */
    private fun nativeViewNode(nodeId: Int, command: String): Node? {
        val node = nodes[nodeId] ?: run {
            fail(TableError.UNKNOWN_NODE, "$command for unknown node $nodeId")
            return null
        }
        if (node.widget != WidgetKind.NativeView) {
            fail(TableError.NOT_NATIVE_VIEW, "$command on ${node.widget}, which holds no platform view")
            return null
        }
        if (!dev.darkpyonix.composerust.foundation.NativeViewPlatform.available) {
            fail(
                TableError.NATIVE_VIEW_UNSUPPORTED,
                "$command on node $nodeId: this renderer's windows have no place for a platform view",
            )
            return null
        }
        return node
    }

    private fun attachNativeView(mutation: Mutation.AttachNativeView) {
        val node = nativeViewNode(mutation.nodeId, "AttachNativeView") ?: return
        val handle = mutation.handle
        if (handle != 0L) {
            val holder = nativeHandles[handle]
            if (holder != null && holder != node.id) {
                fail(
                    TableError.NATIVE_VIEW_REJECTED,
                    "AttachNativeView on node ${node.id}: handle 0x${handle.toULong().toString(16)} is already " +
                        "attached to node $holder",
                )
                return
            }
        }
        if (node.nativeHandle != 0L) nativeHandles.remove(node.nativeHandle)
        node.nativeHandle = handle
        if (handle != 0L) nativeHandles[handle] = node.id
    }

    private fun focusNativeView(mutation: Mutation.FocusNativeView) {
        val node = nativeViewNode(mutation.nodeId, "FocusNativeView") ?: return
        revision += 1
        node.nativeFocusRequest = NativeFocusRequest(mutation.focus, revision)
    }

    /** The window would not take the node's view: the node holds none. */
    internal fun forgetNativeHandle(nodeId: Int) {
        val node = nodes[nodeId] ?: return
        if (node.nativeHandle != 0L) nativeHandles.remove(node.nativeHandle)
        node.nativeHandle = 0L
    }

    private fun windowNode(nodeId: Int, command: String): Node? {
        val node = nodes[nodeId] ?: run {
            fail(TableError.UNKNOWN_NODE, "$command for unknown node $nodeId")
            return null
        }
        if (node.widget != WidgetKind.Window) {
            fail(TableError.UNSUPPORTED_PROPERTY, "$command on ${node.widget}")
            return null
        }
        return node
    }

    private fun setWindowTheme(mutation: Mutation.SetWindowTheme) {
        windowNode(mutation.nodeId, "SetWindowTheme") ?: return
        windowThemes[mutation.nodeId] = mutation.theme
        mutation.theme.paletteProblems.forEach { problem ->
            fail(TableError.INVALID_PALETTE, "window ${mutation.nodeId} theme palette: $problem")
        }
    }

    private fun focusWindow(mutation: Mutation.FocusWindow) {
        windowNode(mutation.nodeId, "FocusWindow") ?: return
        focusRequests += mutation.nodeId
    }

    private fun showFileDialog(mutation: Mutation.ShowFileDialog) {
        val windowKnown = mutation.window == ROOT_ID ||
            nodes[mutation.window]?.widget == WidgetKind.Window
        when (fileDialogs.request(mutation, windowKnown)) {
            dev.darkpyonix.composerust.ui.platform.FileDialogCenter.Accepted.Accepted -> Unit
            dev.darkpyonix.composerust.ui.platform.FileDialogCenter.Accepted.Duplicate -> fail(
                TableError.DUPLICATE_FILE_DIALOG,
                "ShowFileDialog request ${mutation.request} is already waiting for its answer",
            )
            dev.darkpyonix.composerust.ui.platform.FileDialogCenter.Accepted.UnknownWindow -> fail(
                TableError.UNKNOWN_NODE,
                "ShowFileDialog for unknown window ${mutation.window}",
            )
        }
    }

    private fun fileCall(mutation: Mutation.FileCall) {
        if (fileCalls.request(mutation) == dev.darkpyonix.composerust.ui.platform.FileCallCenter.Accepted.Duplicate) {
            fail(
                TableError.DUPLICATE_FILE_CALL,
                "FileCall request ${mutation.request} is already waiting for its answer",
            )
        }
    }

    private fun storageCall(mutation: Mutation.StorageCall) {
        when (storage.request(mutation)) {
            dev.darkpyonix.composerust.ui.platform.StorageCenter.Accepted.Accepted -> Unit
            dev.darkpyonix.composerust.ui.platform.StorageCenter.Accepted.Duplicate -> fail(
                TableError.DUPLICATE_IO_REQUEST,
                "StorageCall request ${mutation.request} is already waiting for its answer",
            )
        }
    }

    private fun requestFocus(mutation: Mutation.RequestFocus) {
        if (mutation.nodeId == ROOT_ID) {
            revision += 1
            focusClearRevision = revision
            return
        }
        if (!requestNodeFocus(mutation.nodeId)) {
            fail(TableError.UNKNOWN_NODE, "RequestFocus for unknown node ${mutation.nodeId}")
        }
    }

    /**
     * Asks for keyboard focus to move to the node [nodeId] names, and says whether there is
     * such a node.
     *
     * A `RequestFocus` from the Host and the focus action of the accessibility tree both end
     * here, so a screen reader moves focus the way the application does: the node's own
     * composable sees the new revision and calls its focus requester.
     */
    internal fun requestNodeFocus(nodeId: Int): Boolean {
        val node = nodes[nodeId] ?: return false
        revision += 1
        node.focusRequestRevision = revision
        if (node.widget != WidgetKind.NativeView) sceneFocusRevision = revision
        return true
    }

    /** The scroll container a command names, or null after reporting that it names none. */
    private fun scrollNode(nodeId: Int, command: String): Pair<Node, ScrollPort>? {
        val node = nodes[nodeId] ?: run {
            fail(TableError.UNKNOWN_NODE, "$command for unknown node $nodeId")
            return null
        }
        val port = node.scroll ?: run {
            fail(TableError.NOT_SCROLLABLE, "$command on ${node.widget}, which does not scroll")
            return null
        }
        return node to port
    }

    private fun scrollTo(mutation: Mutation.ScrollTo) {
        val (node, port) = scrollNode(mutation.nodeId, "ScrollTo") ?: return
        val invalid = { why: String ->
            fail(TableError.INVALID_SCROLL, "ScrollTo on node ${node.id}: $why")
        }
        if (mutation.flags and ScrollWire.TO_KNOWN.inv() != 0) {
            return invalid("flag bits this renderer does not know: 0x${(mutation.flags and ScrollWire.TO_KNOWN.inv()).toString(16)}")
        }
        val hasX = mutation.flags and ScrollWire.TO_X != 0
        val hasY = mutation.flags and ScrollWire.TO_Y != 0
        if (!hasX && !hasY) return invalid("it names neither axis")
        if (!mutation.xPx.isFinite() || !mutation.yPx.isFinite()) {
            return invalid("an offset of (${mutation.xPx}, ${mutation.yPx}) is not a distance")
        }
        if (mutation.item != 0 && node.widget !in LAZY_CONTAINERS) {
            return invalid("item ${mutation.item.toUInt()} on ${node.widget}, which has no items")
        }
        port.beforeRequest(batchSerial)
        port.request(
            ScrollRequest.To(
                // Unsigned on the wire. An index past what an Int holds is past any list.
                item = if (mutation.item < 0) Int.MAX_VALUE else mutation.item,
                x = if (hasX) mutation.xPx else null,
                y = if (hasY) mutation.yPx else null,
                animate = mutation.flags and ScrollWire.TO_ANIMATE != 0,
            ),
        )
    }

    private fun scrollIntoView(mutation: Mutation.ScrollIntoView) {
        val (node, port) = scrollNode(mutation.nodeId, "ScrollIntoView") ?: return
        val invalid = { why: String ->
            fail(TableError.INVALID_SCROLL, "ScrollIntoView on node ${node.id}: $why")
        }
        if (mutation.flags and ScrollWire.INTO_VIEW_ANIMATE.inv() != 0) {
            return invalid("flag bits this renderer does not know: 0x${(mutation.flags and ScrollWire.INTO_VIEW_ANIMATE.inv()).toString(16)}")
        }
        for (align in intArrayOf(mutation.alignX, mutation.alignY)) {
            if (align !in ScrollWire.ALIGN_KEEP..ScrollWire.ALIGN_NEAREST) return invalid("unknown alignment $align")
        }
        val numbers = doubleArrayOf(mutation.rectXPx, mutation.rectYPx, mutation.rectWidthPx, mutation.rectHeightPx)
        if (numbers.any { !it.isFinite() }) return invalid("a rectangle of ${numbers.joinToString()} is not a place")
        if (mutation.rectWidthPx < 0.0 || mutation.rectHeightPx < 0.0) {
            return invalid("a rectangle ${mutation.rectWidthPx} by ${mutation.rectHeightPx} has a negative size")
        }
        val lazy = node.widget in LAZY_CONTAINERS
        var target = mutation.target
        when (mutation.targetKind) {
            ScrollWire.TARGET_CONTENT -> {
                if (lazy) return invalid("${node.widget} has no content coordinates; name an item")
                if (target != 0) return invalid("a content rectangle names no target, but $target was sent")
            }
            ScrollWire.TARGET_NODE -> {
                if (target == node.id || !nodes.containsKey(target) || !isDescendant(target, node.id)) {
                    return invalid("node $target is not inside the container")
                }
            }
            ScrollWire.TARGET_ITEM -> {
                if (!lazy) return invalid("item ${target.toUInt()} on ${node.widget}, which has no items")
                if (target < 0) target = Int.MAX_VALUE
            }
            else -> return invalid("unknown target kind ${mutation.targetKind}")
        }
        port.beforeRequest(batchSerial)
        port.request(
            ScrollRequest.IntoView(
                targetKind = mutation.targetKind,
                target = target,
                alignX = mutation.alignX,
                alignY = mutation.alignY,
                animate = mutation.flags and ScrollWire.INTO_VIEW_ANIMATE != 0,
                x = mutation.rectXPx,
                y = mutation.rectYPx,
                width = mutation.rectWidthPx,
                height = mutation.rectHeightPx,
            ),
        )
    }

    /** The code editor a command names, or null after reporting that it names none. */
    private fun codeNode(nodeId: Int, command: String): Pair<Node, CodeEditorModel>? {
        val node = nodes[nodeId] ?: run {
            fail(TableError.UNKNOWN_NODE, "$command for unknown node $nodeId")
            return null
        }
        val code = node.code ?: run {
            fail(TableError.UNSUPPORTED_PROPERTY, "$command on ${node.widget}")
            return null
        }
        return node to code
    }

    private fun editCode(mutation: Mutation.EditCode) {
        val node = nodes[mutation.nodeId] ?: return fail(
            TableError.UNKNOWN_NODE,
            "EditCode for unknown node ${mutation.nodeId}",
        )
        val code = node.code ?: return fail(
            TableError.UNSUPPORTED_PROPERTY,
            "EditCode on ${node.widget}",
        )
        code.hostEdit(
            mutation.requestId,
            mutation.baseVersion,
            mutation.startLine,
            mutation.startColumn,
            mutation.endLine,
            mutation.endColumn,
            mutation.text,
        )
        collectCode(node)
    }

    /**
     * Hands a grouped edit to its editor, which answers it exactly once.
     *
     * A record the decoder could not read arrives marked as such, ahead of the protocol
     * error the decoder raises for it, so that the editor still answers it; nothing more is
     * reported for it here. Edits that cannot be read are reported, and answered the same
     * way. Edits that can be read but make no sense are the editor's to answer, and are no
     * protocol error.
     */
    private fun editCodeGroup(mutation: Mutation.EditCodeGroup) {
        if (mutation.malformed != null) {
            val node = nodes[mutation.nodeId] ?: return
            val code = node.code ?: return
            code.hostEditGroupUnreadable(mutation.requestId, mutation.baseVersion, null)
            collectCode(node)
            return
        }
        val (node, code) = codeNode(mutation.nodeId, "EditCodeGroup") ?: return
        var unreadable: String? = null
        val edits = CodeEditorRecords.decodeEditGroup(mutation.edits) { message -> unreadable = message }
        if (edits == null) {
            code.hostEditGroupUnreadable(
                mutation.requestId,
                mutation.baseVersion,
                unreadable ?: "a grouped edit could not be read",
            )
        } else {
            code.hostEditGroup(mutation.requestId, mutation.baseVersion, edits)
        }
        collectCode(node)
    }

    /**
     * Hands a code editor what the Host just set on it. The text is compared with the
     * document rather than applied, and the two lists are read record by record, so a bad
     * record costs that record and nothing else.
     */
    private fun feedCode(node: Node, kind: PropertyKind, value: PropertyValue) {
        val code = node.code ?: return
        when (kind) {
            PropertyKind.Text -> code.setHostText((value as? PropertyValue.Text)?.value ?: "")
            PropertyKind.Decorations -> code.setDecorations(
                (value as? PropertyValue.Bytes)?.value?.let { bytes ->
                    CodeEditorRecords.decodeDecorations(bytes) { message ->
                        fail(TableError.INVALID_CODE_EDIT, "decorations on node ${node.id}: $message")
                    }
                } ?: emptyList(),
            )
            PropertyKind.SyntaxSpans -> code.setSyntaxSpans(
                (value as? PropertyValue.Bytes)?.value?.let { bytes ->
                    CodeEditorRecords.decodeSyntaxSpans(bytes) { message ->
                        fail(TableError.INVALID_CODE_EDIT, "syntax spans on node ${node.id}: $message")
                    }
                } ?: emptyList(),
            )
            PropertyKind.Folds -> code.setFolds(
                (value as? PropertyValue.Bytes)?.value?.let { bytes ->
                    CodeEditorRecords.decodeFolds(bytes) { message ->
                        fail(TableError.INVALID_CODE_EDIT, "folds on node ${node.id}: $message")
                    }
                } ?: emptyList(),
            )
            PropertyKind.GutterDecorations -> code.setGutterDecorations(
                (value as? PropertyValue.Bytes)?.value?.let { bytes ->
                    CodeEditorRecords.decodeGutterDecorations(bytes) { message ->
                        fail(TableError.INVALID_CODE_EDIT, "gutter decorations on node ${node.id}: $message")
                    }
                } ?: emptyList(),
            )
            // Read where the editor is drawn, which knows how wide it is. Here a rule that is
            // not one is said once, when it arrives.
            PropertyKind.WordWrap -> {
                val raw = (value as? PropertyValue.Integer)?.value
                if (raw != null && raw != 0L && wordWrapRule(raw) == null) {
                    fail(TableError.INVALID_CODE_EDIT, "word wrap on node ${node.id}: $raw is not a rule")
                }
                return
            }
            PropertyKind.ViewZones -> code.setViewZones(
                (value as? PropertyValue.Bytes)?.value?.let { bytes ->
                    CodeEditorRecords.decodeViewZones(bytes) { message ->
                        fail(TableError.INVALID_CODE_EDIT, "view zones on node ${node.id}: $message")
                    }
                } ?: emptyList(),
            )
            // Read where the editor is drawn. Here it is only checked, so that a record
            // that cannot be read is said once, when it arrives.
            PropertyKind.CodeEditorColors -> {
                (value as? PropertyValue.Bytes)?.value?.let { bytes ->
                    val records = CodeEditorRecords.decodeCodeEditorColors(bytes) { message ->
                        fail(TableError.INVALID_CODE_EDIT, "colours on node ${node.id}: $message")
                    }
                    if (records.size > 1) {
                        fail(
                            TableError.INVALID_CODE_EDIT,
                            "colours on node ${node.id}: ${records.size} records where an editor has one",
                        )
                    }
                }
                return
            }
            // Read where the editor is drawn. Here padding that is not two lengths of zero or
            // more is said once, when it arrives, and the editor is drawn without it.
            PropertyKind.ContentPaddingDp -> {
                (value as? PropertyValue.Bytes)?.value?.let { bytes ->
                    if (contentPaddingDp(bytes) == null) {
                        fail(
                            TableError.INVALID_CODE_EDIT,
                            "content padding on node ${node.id}: ${bytes.size} bytes that are not " +
                                "two lengths of zero or more, top then bottom",
                        )
                    }
                }
                return
            }
            // Whether there is anyone to tell. The model reports only what is heard, and
            // the next frame's drain carries what a new listener has yet to hear.
            PropertyKind.OnCodeMetrics -> {
                code.hearsLineHeight = value is PropertyValue.Integer
                return
            }
            PropertyKind.OnCodeScrolled -> {
                code.hearsScroll = value is PropertyValue.Integer
                return
            }
            PropertyKind.OnCaretRect -> {
                code.hearsCaretRect = value is PropertyValue.Integer
                return
            }
            else -> return
        }
        collectCode(node)
    }

    private fun collectCode(node: Node, includeReports: Boolean = false) {
        val code = node.code ?: return
        outbox += node.codeEvents(code.drainOutput(includeReports)) { message ->
            fail(TableError.INVALID_CODE_EDIT, "code editor ${node.id}: $message")
        }
    }

    private fun create(mutation: Mutation.Create) {
        if (nodes.containsKey(mutation.nodeId)) {
            fail(TableError.DUPLICATE_NODE, "node ${mutation.nodeId} already exists")
            return
        }
        val created = Node(mutation.nodeId, mutation.widget)
        nodes[mutation.nodeId] = created
        if (created.code != null) codeNodes[mutation.nodeId] = created
        // A node the Host never inserts stays a root. The Host has no mutation that attaches
        // the application root, so the first created node is what the Renderer draws.
        // There is no mutation that attaches a root, so roots are inferred here.
        rootChildren.add(mutation.nodeId)
    }

    private fun setProp(mutation: Mutation.SetProp) {
        val node = nodes[mutation.nodeId] ?: return fail(
            TableError.UNKNOWN_NODE,
            "SetProp for unknown node ${mutation.nodeId}",
        )
        if (!supportsProperty(node.widget, mutation.property)) {
            // An out-of-schema property is reported and skipped, never applied.
            fail(
                TableError.UNSUPPORTED_PROPERTY,
                "${node.widget} does not support ${mutation.property}",
            )
            return
        }
        if (mutation.property == PropertyKind.CollapseBelow) {
            val width = (mutation.value as? PropertyValue.Float)?.value
            if (width != null && (!width.isFinite() || width < 0f)) {
                // Not a width at all. The split pane is drawn as if it had none, so a bad
                // value cannot leave it stacked or unstacked by accident.
                fail(
                    TableError.UNSUPPORTED_PROPERTY,
                    "a split pane stacks below a width of zero or more dp, and node " +
                        "${node.id} was given $width; it keeps its size class's own line",
                )
                node.props.remove(mutation.property)
                return
            }
        }
        if (mutation.property == PropertyKind.ContentScale) {
            val tag = (mutation.value as? PropertyValue.Integer)?.value
            if (tag != null && tag != 0L && tag !in 1L..ContentScale.entries.size.toLong()) {
                // A scale this renderer does not know is a newer or a broken Host. The
                // picture is drawn the way one that never chose is, whole and in its own
                // proportions, rather than guessed at.
                fail(
                    TableError.UNSUPPORTED_PROPERTY,
                    "image ${node.id} was given content scale $tag, which is not Fit (1), " +
                        "Crop (2) or FillBounds (3); it is drawn as Fit",
                )
                node.props.remove(mutation.property)
                return
            }
        }
        if (mutation.property == PropertyKind.TextAlign) {
            val tag = (mutation.value as? PropertyValue.Integer)?.value
            if (tag != null && tag != 0L && tag !in 1L..TextAlign.entries.size.toLong()) {
                // A tag this side has no alignment for. Guessing one would put the text on
                // a side nobody asked for, so it is dropped and the text takes its default.
                fail(
                    TableError.UNSUPPORTED_PROPERTY,
                    "node ${node.id} was given text alignment tag $tag, which is not one of " +
                        "${TextAlign.entries.joinToString()} (1 to ${TextAlign.entries.size}); " +
                        "the text takes its default alignment",
                )
                node.props.remove(mutation.property)
                return
            }
        }
        if (mutation.value is PropertyValue.None) {
            node.props.remove(mutation.property)
        } else {
            node.props[mutation.property] = mutation.value
        }
        if (node.code != null) feedCode(node, mutation.property, mutation.value)
        if (mutation.property == PropertyKind.FontFeatureSettings) {
            node.fontFeatures = (mutation.value as? PropertyValue.Bytes)?.value?.let { bytes ->
                fontFeatureSettings(bytes) { why ->
                    fail(
                        TableError.INVALID_FONT_FEATURES,
                        "font features on node ${node.id}: $why; the text is drawn without them",
                    )
                }
            }
        }
        if (mutation.property == PropertyKind.SpanFontFeatures) {
            node.spanFontFeatures = (mutation.value as? PropertyValue.Bytes)?.value?.let { bytes ->
                spanFontFeatureSettings(bytes) { why ->
                    fail(
                        TableError.INVALID_FONT_FEATURES,
                        "span font features on node ${node.id}: $why; the text is drawn without span font features",
                    )
                }
            }
        }
        // The revision of a scroll container's own stated move. Where a scroll request is
        // already waiting from this batch, its place among them is kept, so the two are
        // applied in the order the batch wrote them.
        if (mutation.property == PropertyKind.Count && node.scroll != null) {
            node.scroll.stated(batchSerial, (mutation.value as? PropertyValue.Integer)?.value ?: 0L)
        }
    }

    private fun setModifier(mutation: Mutation.SetModifier) {
        val node = nodes[mutation.nodeId] ?: return fail(
            TableError.UNKNOWN_NODE,
            "SetModifier for unknown node ${mutation.nodeId}",
        )
        if (mutation.index < 0 || mutation.index > MAX_MODIFIERS) {
            fail(TableError.INVALID_INDEX, "modifier index ${mutation.index} out of range")
            return
        }
        val modifier = mutation.modifier
        if (modifier is ProtocolModifier.BackgroundBrush && !(modifier.tile_width > 0f && modifier.tile_height > 0f)) {
            // A step that is not a size has no copies to place: a repeat would never end and
            // a single copy has no tile to be. The modifier is refused and the slot keeps
            // what it held.
            fail(
                TableError.INVALID_MODIFIER,
                "BackgroundBrush modifier on node ${mutation.nodeId} repeats every " +
                    "${modifier.tile_width} by ${modifier.tile_height} dp; both must be more than zero",
            )
            return
        }
        if (modifier is ProtocolModifier.Semantics && modifier.state and SEMANTICS_STATE_MASK.inv() != 0) {
            // A bit this renderer has no meaning for is a newer or a broken Host. Guessing
            // would announce a control as something it is not, so the modifier is refused
            // and the slot keeps what it held.
            val unknown = modifier.state and SEMANTICS_STATE_MASK.inv()
            fail(
                TableError.INVALID_MODIFIER,
                "Semantics modifier on node ${mutation.nodeId} sets state or action bits this renderer " +
                    "does not know: 0x${unknown.toString(16)}",
            )
            return
        }
        while (node.modifiers.size <= mutation.index) {
            node.modifiers.add(ProtocolModifier.Empty)
        }
        val replaced = node.modifiers[mutation.index]
        node.modifiers[mutation.index] = mutation.modifier
        // Counted rather than searched for. Whether any node in the tree is made of a
        // material decides whether the window is stood up in a form that can show what is
        // behind it, and that question is asked once per frame: walking the tree to answer
        // it would put the size of the tree into every frame for a screen that mostly says
        // no.
        if (replaced is ProtocolModifier.Material) materials--
        if (mutation.modifier is ProtocolModifier.Material) materials++
    }

    /**
     * How many nodes are made of a material.
     *
     * Zero for almost every screen. One is enough for the window to need a backdrop, and
     * which part of it actually shows through is decided where the material is drawn: a
     * sidebar made of glass is glass, and a page that paints itself covers what is behind
     * it exactly as an ordinary page does.
     */
    private var materials: Int by mutableStateOf(0)

    /** True where some node asked to be made of a material. */
    internal val asksForMaterial: Boolean get() = materials > 0

    private fun insert(parentId: Int, nodeId: Int, index: Int) {
        // Node id 0 is the "no node" sentinel. The Host uses it for a Dioxus
        // placeholder: an empty `for` body still has a position in the parent, but nothing
        // to draw. It occupies no slot here, and the Host's later Insert for the real
        // children carries the position the placeholder stood at, so indices still line up.
        if (nodeId == ROOT_ID) return
        if (!nodes.containsKey(nodeId)) {
            fail(TableError.UNKNOWN_NODE, "Insert of unknown node $nodeId")
            return
        }
        if (parentId != ROOT_ID && !nodes.containsKey(parentId)) {
            fail(TableError.UNKNOWN_NODE, "Insert into unknown parent $parentId")
            return
        }
        // A platform view covers its node, so nothing drawn under it would be seen.
        if (nodes[parentId]?.widget == WidgetKind.NativeView) {
            fail(TableError.INVALID_CHILDREN, "Insert of node $nodeId into NativeView $parentId, which takes no children")
            return
        }
        // A window is a child of the root and nothing else, and what is drawn in one
        // window cannot be moved into another: a composition is per window and Compose has
        // no way to hand a node from one scene to the next. Both are the Host's
        // mistake; the tree is left as it was and the process goes on.
        if (nodes[nodeId]?.widget == WidgetKind.Window && parentId != ROOT_ID) {
            fail(TableError.MISPLACED_WINDOW, "Window $nodeId must be a child of the root, not of $parentId")
            return
        }
        val from = windowOf(nodes[nodeId]?.parentId ?: ROOT_ID)
        if (from != null && from != windowOf(parentId)) {
            fail(
                TableError.MOVE_BETWEEN_WINDOWS,
                "node $nodeId cannot move from window $from to $parentId",
            )
            return
        }
        // A node cannot be placed under itself or under one of its own descendants: the
        // parent chain would then have no root, and anything that follows it, drawing the
        // tree or walking up from a node, recurses until the stack runs out. The tree the
        // Renderer already has is left exactly as it was.
        if (nodeId == parentId || isDescendant(parentId, nodeId)) {
            fail(
                TableError.CYCLIC_INSERT,
                "Insert of node $nodeId under $parentId would make $nodeId its own ancestor",
            )
            return
        }
        detach(nodeId)
        val siblings = childrenOf(parentId) ?: return
        // `index` is unsigned on the wire; u32::MAX means "append".
        val position = if (index.toLong() and 0xFFFF_FFFFL > siblings.size.toLong()) {
            siblings.size
        } else {
            index
        }
        siblings.add(position, nodeId)
        nodes[nodeId]?.parentId = parentId
    }

    private fun remove(nodeId: Int) {
        // The placeholder sentinel was never materialised, so removing it is a no-op.
        if (nodeId == ROOT_ID) return
        if (!nodes.containsKey(nodeId)) {
            fail(TableError.UNKNOWN_NODE, "Remove of unknown node $nodeId")
            return
        }
        detach(nodeId)
        removeSubtree(nodeId)
    }

    private fun removeSubtree(nodeId: Int) {
        val node = nodes.remove(nodeId) ?: return
        if (codeNodes.remove(nodeId) != null) {
            // A query the editor had not answered yet is answered now, with nothing on
            // screen: it never will be otherwise.
            for (request in node.code?.abandonQueries().orEmpty()) {
                outbox += HostEvent.PositionRect(nodeId, 0L, request, false, 0f, 0f, 0f, 0f)
            }
        }
        node.scroll?.let(scrolls::forget)
        if (node.nativeHandle != 0L) nativeHandles.remove(node.nativeHandle)
        if (node.widget == WidgetKind.Window) {
            windowThemes.remove(nodeId)
            fileDialogs.windowRemoved(nodeId)
        }
        animations.removeNode(nodeId)
        materials -= node.modifiers.count { it is ProtocolModifier.Material }
        node.children.toList().forEach(::removeSubtree)
    }

    /**
     * Appends to a Text node's content. Streaming sends only the new tail, so the
     * batch does not grow with the text already on screen.
     */
    private fun appendText(mutation: Mutation.AppendText) {
        val node = nodes[mutation.nodeId] ?: return fail(
            TableError.UNKNOWN_NODE,
            "AppendText for unknown node ${mutation.nodeId}",
        )
        if (!supportsProperty(node.widget, PropertyKind.Text)) {
            fail(TableError.UNSUPPORTED_PROPERTY, "AppendText on ${node.widget}")
            return
        }
        revision += 1
        node.props[PropertyKind.Text] = PropertyValue.Text(node.text(PropertyKind.Text) + mutation.text)
    }

    private fun setText(mutation: Mutation.SetText) {
        val node = nodes[mutation.nodeId] ?: return fail(
            TableError.UNKNOWN_NODE,
            "SetText for unknown node ${mutation.nodeId}",
        )
        if (node.widget != WidgetKind.TextField) {
            fail(TableError.UNSUPPORTED_PROPERTY, "SetText on ${node.widget}")
            return
        }
        revision += 1
        node.hostText = HostText(
            text = mutation.text,
            selectionStart = mutation.selectionStart,
            selectionEnd = mutation.selectionEnd,
            revision = revision,
        )
    }

    /**
     * True when [nodeId] is [candidate] itself or sits below it, found by walking up from
     * [nodeId] rather than down, so the cost is the depth of the tree and not its size.
     *
     * The walk is bounded by the number of nodes, so even a chain that already loops ends
     * the walk instead of running forever.
     */
    private fun isDescendant(nodeId: Int, candidate: Int): Boolean {
        var current = nodeId
        var steps = nodes.size
        while (current != ROOT_ID && steps-- > 0) {
            if (current == candidate) return true
            current = nodes[current]?.parentId ?: return false
        }
        return false
    }

    private fun detach(nodeId: Int) {
        val parent = nodes[nodeId]?.parentId ?: ROOT_ID
        childrenOf(parent)?.remove(nodeId)
        rootChildren.remove(nodeId)
        nodes[nodeId]?.parentId = ROOT_ID
    }

    private fun childrenOf(parentId: Int): MutableList<Int>? =
        if (parentId == ROOT_ID) rootChildren else nodes[parentId]?.children

    private fun fail(code: Int, message: String) {
        errors += TableError(code, message)
    }

    /**
     * Records a protocol error found outside the batch itself, such as a measure request
     * naming a font that was never registered. It is reported with the batch's own errors,
     * after the call it was found in has returned, so the Host is never re-entered.
     */
    internal fun report(code: Int, message: String) = fail(code, message)

    /**
     * [report], unless the same error is already waiting to be reported. For a mistake a
     * Host makes in every request of a kind, which is one thing to fix however many
     * requests it was made in.
     */
    internal fun reportOnce(code: Int, message: String) {
        if (errors.none { it.code == code && it.message == message }) fail(code, message)
    }

    companion object {
        /** Node id 0 is the Host's "no node" sentinel, so it names the virtual root. */
        const val ROOT_ID: Int = 0
        private const val MAX_MODIFIERS = 64

        /** The lazy containers, which scroll by item and have no content coordinates. */
        internal val LAZY_CONTAINERS: Set<WidgetKind> =
            setOf(WidgetKind.LazyColumn, WidgetKind.LazyRow, WidgetKind.LazyGrid)

        /** Every widget that takes a scroll request and can report its position. */
        internal val SCROLL_CONTAINERS: Set<WidgetKind> =
            LAZY_CONTAINERS + setOf(WidgetKind.ScrollColumn, WidgetKind.ScrollRow, WidgetKind.CodeEditor)

        internal fun supportsProperty(widget: WidgetKind, property: PropertyKind): Boolean =
            when (property) {
                // The name assistive technology reads can be given to any node.
                PropertyKind.AccessibleName -> true

                // Colours and size of the selection controls.
                PropertyKind.TrackColor -> widget == WidgetKind.Switch || widget == WidgetKind.Slider || widget == WidgetKind.ProgressIndicator
                PropertyKind.ThumbColor -> widget == WidgetKind.Switch || widget == WidgetKind.Slider

                PropertyKind.ActiveTrackColor -> widget == WidgetKind.Slider
                PropertyKind.ThumbSize -> widget == WidgetKind.Slider

                // A radio button's ring is its container and its dot its check.
                PropertyKind.ContainerColor,
                PropertyKind.CheckColor,
                -> widget == WidgetKind.Checkbox || widget == WidgetKind.RadioButton

                PropertyKind.ControlSize -> widget == WidgetKind.Switch ||
                    widget == WidgetKind.Checkbox ||
                    widget == WidgetKind.RadioButton

                // A linear layout gathers the radio buttons inside it into one group.
                PropertyKind.RadioGroup -> widget == WidgetKind.Column || widget == WidgetKind.Row

                // Event properties carry handler ids and are valid on any widget.
                PropertyKind.OnClick,
                PropertyKind.OnValueChange,
                PropertyKind.OnSubmit,
                PropertyKind.OnFocusLost,
                PropertyKind.OnFocusGained,
                PropertyKind.OnKeyDown,
                PropertyKind.OnKeyUp,
                PropertyKind.OnRangeRequested,
                PropertyKind.OnDismiss,
                // Any node's size can move under a motion role, so any node may hear it
                // settle. One without the role hears nothing, which is not an error: the
                // role can come and go with the screen's state.
                PropertyKind.OnMotionFinished,
                -> true

                // The scroll report belongs to the widgets that scroll.
                PropertyKind.OnScrollChanged -> widget in SCROLL_CONTAINERS

                // Whether a platform view shows, and who hears its focus move.
                PropertyKind.NativeVisible,
                PropertyKind.OnNativeViewFocus,
                -> widget == WidgetKind.NativeView

                // Pointer subscription, cursor and selectability belong to the nodes a
                // pointer can sit over as a region: the containers, text and the HTML
                // region root. A picker takes only its value, range and events.
                PropertyKind.OnPointer,
                PropertyKind.PointerKinds,
                PropertyKind.Cursor,
                PropertyKind.UserSelect,
                // A dragged window passing over a node, and the key a node is pulled out
                // into a window under, belong to the same containers a pointer can sit over.
                PropertyKind.OnWindowDragOver,
                PropertyKind.TearOffKey,
                -> widget == WidgetKind.Text ||
                    widget == WidgetKind.Column ||
                    widget == WidgetKind.Row ||
                    widget == WidgetKind.Box ||
                    widget == WidgetKind.Card ||
                    widget == WidgetKind.Surface ||
                    widget == WidgetKind.ScrollColumn ||
                    widget == WidgetKind.ScrollRow ||
                    widget == WidgetKind.SelectionContainer ||
                    widget == WidgetKind.Spacer ||
                    widget == WidgetKind.AbsoluteBox ||
                    // An editor's own pointer events take the same mask of kinds.
                    (widget == WidgetKind.CodeEditor && property == PropertyKind.PointerKinds)

                // A Tooltip's text is the explanation it shows, and its description in the
                // accessibility tree. A destination's is its label.
                PropertyKind.Text ->
                    widget == WidgetKind.Text ||
                        widget == WidgetKind.Button ||
                        widget == WidgetKind.TextField ||
                        widget == WidgetKind.Tooltip ||
                        widget == WidgetKind.NavigationItem ||
                        // A bar's is the window's title, where that bar is the window's
                        // caption. It is a property rather than a child because three
                        // design systems centre it, and a bar whose children are an
                        // arbitrary tree gives no way to tell which of them to centre.
                        widget == WidgetKind.TopAppBar ||
                        // A chip's label, and the name of the one action a screen is about,
                        // which is its accessible name where the system draws the glyph
                        // alone.
                        widget == WidgetKind.Chip ||
                        widget == WidgetKind.FloatingAction ||
                        // A badge's short word, shown where a count would be.
                        widget == WidgetKind.Badge ||
                        // A split pane's is its divider's name for a screen reader.
                        widget == WidgetKind.SplitPane ||
                        // A code editor's document as the Host opened it. A text equal to
                        // what the editor holds changes nothing; a different one opens a
                        // new document.
                        widget == WidgetKind.CodeEditor

                // Note: SpacerProps has width and height in the Rust schema, but there are
                // no matching PropertyKind variants, so a Spacer can only be sized with
                // modifiers.
                PropertyKind.Placeholder -> widget == WidgetKind.TextField
                PropertyKind.Multiline -> widget == WidgetKind.TextField
                PropertyKind.PlaceholderColor,
                PropertyKind.CaretColor,
                -> widget == WidgetKind.TextField
                PropertyKind.Enabled -> widget != WidgetKind.Spacer

                // Windowing properties belong to the lazy container alone.
                PropertyKind.ItemCount ->
                    widget == WidgetKind.LazyColumn ||
                        widget == WidgetKind.LazyRow ||
                        widget == WidgetKind.LazyGrid
                PropertyKind.ItemKey -> true

                // Design primitives, resolved against the design system's token table when
                // the node is drawn.
                PropertyKind.TypeRole,
                PropertyKind.FontSize,
                PropertyKind.FontWeight,
                PropertyKind.LineHeight,
                PropertyKind.LetterSpacing,
                PropertyKind.Color,
                PropertyKind.TextAlign,
                PropertyKind.MaxLines,
                PropertyKind.Overflow,
                -> widget == WidgetKind.Text ||
                    widget == WidgetKind.Button ||
                    widget == WidgetKind.TextField ||
                    // An editor takes the rung of the type ladder, the size and the row
                    // height. Its ink is the design system's or a colour run's, and it
                    // neither aligns nor cuts off its lines.
                    (
                        widget == WidgetKind.CodeEditor &&
                            (
                                property == PropertyKind.TypeRole ||
                                    property == PropertyKind.FontSize ||
                                    property == PropertyKind.LineHeight
                                )
                        ) ||
                    // A chip's label takes a line limit and an overflow, as a button's does.
                    (
                        widget == WidgetKind.Chip &&
                            (property == PropertyKind.MaxLines || property == PropertyKind.Overflow)
                        ) ||
                    // An icon takes a tint through the same Paint attribute text does, and
                    // so does a destination: its icon and label are properties rather than
                    // a child tree, so there is no Text node underneath to colour instead.
                    (
                        property == PropertyKind.Color &&
                            (
                                widget == WidgetKind.Icon ||
                                    widget == WidgetKind.NavigationItem ||
                                    // A tab strip's selection mark, and a slider's
                                    // filled track and thumb. Both are drawn rather
                                    // than written, so there is no Text underneath to
                                    // colour instead.
                                    widget == WidgetKind.Tabs ||
                                    widget == WidgetKind.Slider ||
                                    widget == WidgetKind.ProgressIndicator ||
                                    // A badge's fill. Only its role is read: a badge
                                    // has no literal colour.
                                    widget == WidgetKind.Badge ||
                                    // A layout container's colour is the one the text
                                    // inside it inherits, as CSS `color` is.
                                    widget in INHERITS_TEXT_COLOR
                                )
                        )

                PropertyKind.Arrangement,
                PropertyKind.Spacing,
                PropertyKind.SpaceRole,
                PropertyKind.Alignment,
                -> widget == WidgetKind.Column ||
                    widget == WidgetKind.Row ||
                    widget == WidgetKind.Box ||
                    widget == WidgetKind.LazyColumn ||
                    widget == WidgetKind.LazyRow ||
                    widget == WidgetKind.ScrollColumn ||
                    widget == WidgetKind.ScrollRow ||
                    widget == WidgetKind.FileDropTarget

                PropertyKind.Variant -> widget == WidgetKind.Button

                // An asset id is the whole of what a picture carries. The bytes were read
                // at registration and the id is what crosses per frame.
                PropertyKind.Asset ->
                    widget == WidgetKind.Image || widget == WidgetKind.Icon

                // A picture drawn as an icon, in the icon's colour.
                PropertyKind.IconAsset -> widget == WidgetKind.Icon

                // The pickers carry a value and the ends of the range it may take, in the
                // widget's own unit; a slider carries a position between the same two
                // ends. Nothing here says how the value should be reached.
                PropertyKind.Value,
                PropertyKind.Min,
                PropertyKind.Max,
                -> widget == WidgetKind.DatePicker ||
                    widget == WidgetKind.TimePicker ||
                    widget == WidgetKind.Slider ||
                    // A split pane's side pane width in dp, and the range it may be dragged
                    // over.
                    widget == WidgetKind.SplitPane ||
                    // An indicator's value is how far along it is, and it has no range.
                    (property == PropertyKind.Value && widget == WidgetKind.ProgressIndicator) ||
                    // A scroll container's target scroll offset in dp when the Host asks
                    // to jump or animate to a position.
                    (
                        property == PropertyKind.Value &&
                            (widget == WidgetKind.ScrollColumn || widget == WidgetKind.ScrollRow)
                        ) ||
                    // A Canvas's reference coordinate space (width in Min, height in Max)
                    // when its commands are written in fractional or view-box coordinates.
                    (
                        (property == PropertyKind.Min || property == PropertyKind.Max) &&
                            widget == WidgetKind.Canvas
                        )

                // One boolean for the three toggles: a selected radio button and a switch
                // that is on are the same fact.
                PropertyKind.Checked ->
                    widget == WidgetKind.Checkbox ||
                        widget == WidgetKind.RadioButton ||
                        widget == WidgetKind.Switch ||
                        // A chosen chip or a selected menu item is the same fact again.
                        widget == WidgetKind.Chip ||
                        widget == WidgetKind.Button

                PropertyKind.Steps -> widget == WidgetKind.Slider

                PropertyKind.Determinate,
                PropertyKind.Circular,
                -> widget == WidgetKind.ProgressIndicator ||
                    // On a scroll container, Determinate says whether a requested scroll
                    // animates or jumps immediately.
                    (
                        property == PropertyKind.Determinate &&
                            (widget == WidgetKind.ScrollColumn || widget == WidgetKind.ScrollRow)
                        )

                PropertyKind.Vertical -> widget == WidgetKind.Divider

                // The overlays seed the Renderer's own open state; the tab strip seeds its
                // own selection. Neither is read back every frame.
                PropertyKind.Open -> widget == WidgetKind.Dialog ||
                    widget == WidgetKind.Menu ||
                    // A sheet is an overlay on the same terms as a dialog.
                    widget == WidgetKind.Sheet ||
                    // A popover's is read every time: the Host owns it.
                    widget == WidgetKind.Popover

                // A custom scrim colour for modal and popup overlays.
                PropertyKind.Scrim -> widget == WidgetKind.Dialog ||
                    widget == WidgetKind.Menu ||
                    widget == WidgetKind.Sheet ||
                    // On a popover it is also a boolean: whether to dim the window at all.
                    widget == WidgetKind.Popover

                // The side of its anchor a popover opens on, the edge it lines up with,
                // and the gap between the two.
                PropertyKind.Placement,
                PropertyKind.Align,
                PropertyKind.OffsetDp,
                -> widget == WidgetKind.Popover
                // A field drawn without the design system's frame.
                PropertyKind.Bare -> widget == WidgetKind.TextField ||
                    widget == WidgetKind.Dropdown
                // A wrapping row's gaps, alignments and per-line limit. Only it lays out by
                // lines, so no other widget has anything to read them for.
                PropertyKind.HorizontalSpacing,
                PropertyKind.VerticalSpacing,
                PropertyKind.HorizontalAlignment,
                PropertyKind.VerticalAlignment,
                PropertyKind.MaxItemsInEachRow,
                -> widget == WidgetKind.FlowRow
                // A Dropdown's selection is a position in its own children, the same thing
                // a tab strip's selection is.
                PropertyKind.SelectedIndex ->
                    widget == WidgetKind.Tabs ||
                        widget == WidgetKind.Dropdown ||
                        // Which destination of a set is the current one, counted over the
                        // destinations rather than over all the children.
                        widget == WidgetKind.Navigation ||
                        // Which pane shows when a split pane shows one at a time.
                        widget == WidgetKind.SplitPane
                // Drawing commands belong to the Canvas alone: no other widget draws
                // anything the Host described command by command.
                PropertyKind.Commands -> widget == WidgetKind.Canvas

                // The meaning of an icon. A meaning, never a picture: the artwork is the
                // design system's. A destination carries one, and so does a button,
                // because every toolbar worth copying is a row of icon buttons and
                // nothing else in the vocabulary can place one.
                PropertyKind.Icon ->
                    widget == WidgetKind.NavigationItem ||
                        widget == WidgetKind.Button ||
                        // A glyph before a chip's label, and what the one action a screen
                        // is about means.
                        widget == WidgetKind.Chip ||
                        widget == WidgetKind.FloatingAction

                // Which part of a screen's frame a subtree fills. Only the wrapper a
                // Scaffold puts around a slot carries it.
                PropertyKind.Slot -> widget == WidgetKind.ScaffoldSlot

                // Which named group of a strip a destination is in. Only a destination
                // has one: a group is a run of them, and nothing else in a strip is in a
                // run of anything.
                PropertyKind.Section -> widget == WidgetKind.NavigationItem

                // Runs of different treatment inside one string.
                PropertyKind.Spans -> widget == WidgetKind.Text

                // What text laid out by CSS needs: its own fonts, where its lines may
                // break, its tab stops, sizes the system's font scale leaves alone, and
                // whether it wraps at all. A Text, and its own list of fonts on a TextField
                // A control's label is set by the design system.
                PropertyKind.Font ->
                    widget == WidgetKind.Text ||
                        widget == WidgetKind.TextField ||
                        // An editor set in the application's own monospace list.
                        widget == WidgetKind.CodeEditor

                // Where lines break, in a Text and in a field's typed text and placeholder.
                PropertyKind.WordBreak,
                PropertyKind.OverflowWrap,
                -> widget == WidgetKind.Text || widget == WidgetKind.TextField

                // OpenType features, such as digits of one width. They pick a form the font
                // has rather than another font, so text with a role takes them too.
                PropertyKind.FontFeatureSettings ->
                    widget == WidgetKind.Text || widget == WidgetKind.TextField

                PropertyKind.SpanFonts,
                PropertyKind.SpanFontFeatures,
                PropertyKind.TabSize,
                PropertyKind.AbsoluteSize,
                PropertyKind.SoftWrap,
                -> widget == WidgetKind.Text

                // How large an HTML island is drawn. Only the island's root has one.
                PropertyKind.IslandZoom -> widget == WidgetKind.AbsoluteBox

                // How many a badge counts, or the revision of a programmatic scroll request.
                PropertyKind.Count ->
                    widget == WidgetKind.Badge ||
                        widget == WidgetKind.ScrollColumn ||
                        widget == WidgetKind.ScrollRow

                // Whether a split pane's side pane may be folded away, a dropdown rests as
                // a closed field on every design system, or a linear layout sizes its cross
                // axis to its largest child.
                PropertyKind.Collapsible ->
                    widget == WidgetKind.SplitPane ||
                        widget == WidgetKind.Dropdown ||
                        widget == WidgetKind.Row ||
                        widget == WidgetKind.Column

                // What a button is apart from its emphasis: a plain one, or a key of a
                // dense grid. Only a button has a kind; a chip or a floating action is
                // already its own component.
                PropertyKind.ButtonKind -> widget == WidgetKind.Button

                // A code editor's lists, its tab width and the handlers only it reports to.
                PropertyKind.Decorations,
                PropertyKind.SyntaxSpans,
                PropertyKind.TabWidth,
                PropertyKind.OnEditRejected,
                PropertyKind.OnHover,
                PropertyKind.OnSave,
                PropertyKind.OnDecorationClick,
                PropertyKind.Folds,
                PropertyKind.GutterDecorations,
                PropertyKind.OnSelectionChange,
                PropertyKind.OnVisibleRangeChange,
                PropertyKind.OnEditorFocusChange,
                PropertyKind.OnFoldChange,
                // Whether it draws line numbers, how its gutter is spaced, the colours of
                // its own parts, and the handlers of its row height and scroll offset.
                PropertyKind.LineNumbers,
                PropertyKind.GutterMinWidthDp,
                PropertyKind.GutterPaddingDp,
                PropertyKind.TextInsetDp,
                PropertyKind.CodeEditorColors,
                PropertyKind.OnCodeMetrics,
                PropertyKind.OnCodeScrolled,
                // The handler told where the caret is drawn, and the one offered keys
                // before the editor handles them.
                PropertyKind.OnCaretRect,
                PropertyKind.OnKeyPreview,
                // The handler told what became of each grouped edit.
                PropertyKind.OnEditGroupResult,
                // The handler of its pointer events with the text under them. The kinds it
                // hears are the node's `PointerKinds`, below.
                PropertyKind.OnCodePointer,
                // Whether it draws a minimap, and on which side, and its view zones.
                PropertyKind.Minimap,
                PropertyKind.ViewZones,
                PropertyKind.WordWrap,
                // Room that scrolls with the text, and a gutter whose columns stay put.
                PropertyKind.ContentPaddingDp,
                PropertyKind.FixedGutterColumns,
                -> widget == WidgetKind.CodeEditor

                PropertyKind.OnCompositionChange ->
                    widget == WidgetKind.CodeEditor || widget == WidgetKind.TextField

                // Files over a node and files let go on it. Only the widget that exists
                // to receive them, because a handler is attached whether or not a screen
                // supplied one: on any container these would cost every container in the
                // tree three records for saying nothing.
                PropertyKind.OnFilesEntered,
                PropertyKind.OnFilesDropped,
                -> widget == WidgetKind.FileDropTarget

                // How wide a grid's columns are, said as a count or as a minimum.
                PropertyKind.Columns,
                PropertyKind.MinColumnWidth,
                -> widget == WidgetKind.LazyGrid

                // A window's own properties and handlers. The platform
                // layer reads them; the table only keeps them on the node.
                PropertyKind.WindowTitle,
                PropertyKind.WindowSize,
                PropertyKind.WindowMinSize,
                PropertyKind.WindowResizable,
                PropertyKind.ReportSize,
                PropertyKind.WindowChrome,
                PropertyKind.WindowTitleBar,
                PropertyKind.WindowIcon,
                PropertyKind.WindowPosition,
                PropertyKind.WindowRestoreKey,
                PropertyKind.WindowZoomLevel,
                PropertyKind.DragFromPointer,
                PropertyKind.OnWindowCloseRequested,
                PropertyKind.OnWindowFocusChanged,
                PropertyKind.OnWindowMoved,
                PropertyKind.OnWindowDrag,
                PropertyKind.OnWindowUnavailable,
                PropertyKind.OnWindowClosed,
                -> widget == WidgetKind.Window

                // What a set of destinations becomes in a compact window, where the
                // application chose. Only a navigation has destinations to place.
                PropertyKind.CompactNavigation -> widget == WidgetKind.Navigation

                // The width below which a split pane shows one pane at a time.
                PropertyKind.CollapseBelow -> widget == WidgetKind.SplitPane

                // Where a plain scroll container comes to rest. The lazy containers scroll
                // by item through their own state and do not take it.
                PropertyKind.Snap -> widget == WidgetKind.ScrollRow || widget == WidgetKind.ScrollColumn

                // The tallest a sheet grows. A dialog sizes itself to its content.
                PropertyKind.MaxHeightFraction -> widget == WidgetKind.Sheet

                // The presentation of a sheet: edge drawer or centered modal, and its max width.
                PropertyKind.Presentation,
                PropertyKind.MaxWidth,
                -> widget == WidgetKind.Sheet

                // How a picture is laid into its box. An icon takes its design system's
                // optical size, so only an image has a box to lay it into.
                PropertyKind.ContentScale -> widget == WidgetKind.Image

                // Whether a bar across the top of a screen draws the design system's
                // hairline separator underneath.
                PropertyKind.Separator -> widget == WidgetKind.TopAppBar

                // A property declared by an extension package belongs to the widget
                // that package declared it for.
                PropertyKind.Progress -> widget == WidgetKind.LinearProgressIndicator
            }
    }
}