quickgui 0.1.6

A damage-driven, GPU-accelerated desktop GUI foundation
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
use std::{fmt, sync::Arc};

use thiserror::Error;

use crate::{
    AccessibilityRole, Element, ElementId, EventContext, FocusHandle, IntoElement, KeyBinding,
    ListOffset, ListState, StateAccessor, ViewContext, div,
};

/// Maximum nodes retained by one tree state.
pub const MAX_TREE_NODES: usize = 1_000_000;
/// Maximum hierarchical depth accepted by one tree.
pub const MAX_TREE_DEPTH: usize = 256;
/// Maximum UTF-8 bytes retained for one tree node label.
pub const MAX_TREE_LABEL_BYTES: usize = 64 * 1024;
/// Maximum UTF-8 label bytes retained by one complete tree.
pub const MAX_TREE_TEXT_BYTES: usize = 16 * 1024 * 1024;

/// Default accessible name of the placeholder row shown while children load.
pub const DEFAULT_TREE_LOADING_LABEL: &str = "Loading…";

const TREE_KEY_CONTEXT: &str = "Tree";
const TREE_ROW_ID_TAG: u64 = 0x7d5d_5f29_58ab_f1f7;
const TREE_DISCLOSURE_ID_TAG: u64 = 0xb55a_8c0a_f31f_00e9;
const TREE_LOADING_ID_TAG: u64 = 0x2c8f_47b1_e05d_9a34;
const TREE_VISIBLE_UNSET: u32 = u32::MAX;
/// Marks one visible row as the loading placeholder of the entry it wraps.
///
/// Node indices are bounded by [`MAX_TREE_NODES`], so the top bit of the compact 32-bit visible
/// index is free and the placeholder costs no extra vector.
const TREE_PLACEHOLDER_FLAG: u32 = 0x8000_0000;

/// Move to the previous visible enabled tree item.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct TreePrevious;
/// Move to the next visible enabled tree item.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct TreeNext;
/// Collapse an expanded branch, otherwise move to its parent.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct TreeCollapseOrParent;
/// Expand a collapsed branch, otherwise move to its first visible child.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct TreeExpandOrChild;
/// Move to the first visible enabled tree item.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct TreeFirst;
/// Move to the final visible enabled tree item.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct TreeLast;
/// Move one visible page upward.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct TreePageUp;
/// Move one visible page downward.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct TreePageDown;
/// Toggle the expansion state of the selected branch.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct TreeToggle;
/// Activate the selected item.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct TreeActivate;

/// Dispatched by the tree when expanding a branch whose children are not loaded yet.
///
/// The tree owns no loader: it shows a bounded placeholder row and asks the application once.
/// Answer with [`TreeState::set_children`], which validates and splices atomically.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TreeLoadChildren {
    pub node: ElementId,
}

/// Contextual bindings used by [`TreeState::element`].
pub fn tree_key_bindings() -> [KeyBinding; 10] {
    [
        KeyBinding::new("up", TreePrevious, Some(TREE_KEY_CONTEXT)),
        KeyBinding::new("down", TreeNext, Some(TREE_KEY_CONTEXT)),
        KeyBinding::new("left", TreeCollapseOrParent, Some(TREE_KEY_CONTEXT)),
        KeyBinding::new("right", TreeExpandOrChild, Some(TREE_KEY_CONTEXT)),
        KeyBinding::new("platform-up", TreeFirst, Some(TREE_KEY_CONTEXT)),
        KeyBinding::new("platform-down", TreeLast, Some(TREE_KEY_CONTEXT)),
        KeyBinding::new("pageup", TreePageUp, Some(TREE_KEY_CONTEXT)),
        KeyBinding::new("pagedown", TreePageDown, Some(TREE_KEY_CONTEXT)),
        KeyBinding::new("space", TreeToggle, Some(TREE_KEY_CONTEXT)),
        KeyBinding::new("enter", TreeActivate, Some(TREE_KEY_CONTEXT)),
    ]
}

/// Application value and hierarchy retained by a [`TreeState`].
#[derive(Clone, Debug)]
pub struct TreeNode<T> {
    id: ElementId,
    label: Arc<str>,
    value: T,
    children: Vec<TreeNode<T>>,
    disabled: bool,
    pending: bool,
}

impl<T> TreeNode<T> {
    pub fn new(id: impl Into<ElementId>, label: impl Into<Arc<str>>, value: T) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            value,
            children: Vec::new(),
            disabled: false,
            pending: false,
        }
    }

    /// Declare a branch whose children have not been loaded yet.
    ///
    /// A pending node expands like any other branch. The first expansion mounts one placeholder
    /// row and dispatches [`TreeLoadChildren`]; supplying children through
    /// [`TreeState::set_children`] clears the pending flag. A node that already carries children
    /// is an ordinary branch and ignores this flag.
    pub fn pending(mut self, pending: bool) -> Self {
        self.pending = pending;
        self
    }

    pub const fn is_pending(&self) -> bool {
        self.pending
    }

    pub fn child(mut self, child: TreeNode<T>) -> Self {
        self.children.push(child);
        self
    }

    pub fn children(mut self, children: impl IntoIterator<Item = TreeNode<T>>) -> Self {
        self.children.extend(children);
        self
    }

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

    pub const fn id(&self) -> ElementId {
        self.id
    }

    pub fn label(&self) -> &Arc<str> {
        &self.label
    }

    pub fn value(&self) -> &T {
        &self.value
    }

    pub fn child_nodes(&self) -> &[TreeNode<T>] {
        &self.children
    }

    pub const fn is_disabled(&self) -> bool {
        self.disabled
    }
}

/// A tree source exceeded a hard identity, depth, or retained-text boundary.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
pub enum TreeError {
    #[error("a tree supports at most {limit} nodes")]
    TooManyNodes { limit: usize },
    #[error("tree depth {depth} exceeds the limit of {limit}")]
    TooDeep { depth: usize, limit: usize },
    #[error("tree node {id:?} has a duplicate stable ID")]
    DuplicateId { id: ElementId },
    #[error("tree node {id:?} has {bytes} label bytes; the limit is {limit}")]
    LabelTooLong {
        id: ElementId,
        bytes: usize,
        limit: usize,
    },
    #[error("tree labels retain {bytes} bytes; the total limit is {limit}")]
    TextBudgetExceeded { bytes: usize, limit: usize },
}

/// Structural geometry retained by one virtualized tree.
///
/// Indentation, disclosure size, padding, colors, typography, borders, radii, opacity, and motion
/// all belong to the caller-owned row returned from [`TreeState::element`].
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TreeLayout {
    pub row_height: f32,
}

impl TreeLayout {
    pub fn new(row_height: f32) -> Self {
        Self {
            row_height: finite_clamped(row_height, 20.0, 256.0, 30.0),
        }
    }

    pub fn row_height(mut self, height: f32) -> Self {
        self.row_height = finite_clamped(height, 20.0, 256.0, 30.0);
        self
    }

    fn sanitized(mut self) -> Self {
        self.row_height = finite_clamped(self.row_height, 20.0, 256.0, 30.0);
        self
    }
}

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

struct TreeEntry<T> {
    id: ElementId,
    label: Arc<str>,
    value: T,
    parent: Option<u32>,
    level: u32,
    position_in_set: u32,
    size_of_set: u32,
    subtree_end: u32,
    child_count: u32,
    disabled: bool,
    pending: bool,
}

struct TreeArena<T> {
    entries: Vec<TreeEntry<T>>,
    id_index: Vec<(u64, u32)>,
    root_count: usize,
}

/// A borrowed visible tree row passed to application rendering.
pub struct TreeRow<'a, T> {
    entry: &'a TreeEntry<T>,
    expanded: bool,
    selected: bool,
    loading: Option<&'a Arc<str>>,
}

impl<T> Clone for TreeRow<'_, T> {
    fn clone(&self) -> Self {
        *self
    }
}

/// A row is a bundle of borrows, so it is copyable whatever the application value is.
impl<T> Copy for TreeRow<'_, T> {}

impl<T> fmt::Debug for TreeRow<'_, T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("TreeRow")
            .field("id", &self.entry.id)
            .field("label", &self.entry.label)
            .field("level", &self.entry.level)
            .field("expanded", &self.expanded)
            .field("selected", &self.selected)
            .field("disabled", &self.entry.disabled)
            .field("loading", &self.loading.is_some())
            .finish_non_exhaustive()
    }
}

impl<'a, T> TreeRow<'a, T> {
    /// The stable node ID of this row, or of the branch a loading placeholder belongs to.
    pub const fn id(self) -> ElementId {
        self.entry.id
    }

    /// The node's accessible label, or the loading label for a placeholder row.
    pub fn label(self) -> &'a Arc<str> {
        match self.loading {
            Some(label) => label,
            None => &self.entry.label,
        }
    }

    /// The application value of this node, or of the branch a loading placeholder belongs to.
    pub fn value(self) -> &'a T {
        &self.entry.value
    }

    pub const fn level(self) -> usize {
        match self.loading {
            Some(_) => self.entry.level as usize + 1,
            None => self.entry.level as usize,
        }
    }

    pub const fn position_in_set(self) -> usize {
        match self.loading {
            Some(_) => 0,
            None => self.entry.position_in_set as usize,
        }
    }

    pub const fn size_of_set(self) -> usize {
        match self.loading {
            Some(_) => 1,
            None => self.entry.size_of_set as usize,
        }
    }

    pub const fn has_children(self) -> bool {
        self.loading.is_none() && (self.entry.child_count != 0 || self.entry.pending)
    }

    /// Whether this row is the placeholder mounted while a branch's children load.
    pub const fn is_loading(self) -> bool {
        self.loading.is_some()
    }

    /// Whether this branch's children have not been supplied yet.
    pub const fn is_pending(self) -> bool {
        self.loading.is_none() && self.entry.pending
    }

    pub const fn is_expanded(self) -> bool {
        self.loading.is_none() && self.expanded
    }

    pub const fn is_selected(self) -> bool {
        self.loading.is_none() && self.selected
    }

    /// A loading placeholder is never selectable, so it always reports as disabled.
    pub const fn is_disabled(self) -> bool {
        self.loading.is_some() || self.entry.disabled
    }
}

/// Retained hierarchy, expansion, selection, and virtual-scroll state for a tree view.
///
/// Source nodes are flattened once into a bounded preorder arena. Expansion rebuilds only a lean
/// visible-index vector; ordinary frames and clean idle periods perform no hierarchy walk, timer,
/// or polling. Rows are mounted through the existing sparse [`ListState`] path.
pub struct TreeState<T> {
    entries: Vec<TreeEntry<T>>,
    id_index: Vec<(u64, u32)>,
    expanded: Vec<bool>,
    visible: Vec<u32>,
    visible_position: Vec<u32>,
    selected: Option<u32>,
    root_count: usize,
    loading_label: Arc<str>,
    list: ListState,
    layout: TreeLayout,
}

impl<T> fmt::Debug for TreeState<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("TreeState")
            .field("nodes", &self.entries.len())
            .field("visible", &self.visible.len())
            .field(
                "expanded",
                &self.expanded.iter().filter(|value| **value).count(),
            )
            .field("selected", &self.selected_id())
            .field("root_count", &self.root_count)
            .field("list", &self.list)
            .field("layout", &self.layout)
            .finish()
    }
}

impl<T> TreeState<T> {
    pub fn new(nodes: impl IntoIterator<Item = TreeNode<T>>) -> Result<Self, TreeError> {
        let arena = build_tree_arena(nodes)?;
        let layout = TreeLayout::default();
        let mut state = Self {
            expanded: vec![false; arena.entries.len()],
            visible: Vec::with_capacity(arena.root_count.min(256)),
            visible_position: vec![TREE_VISIBLE_UNSET; arena.entries.len()],
            selected: None,
            root_count: arena.root_count,
            loading_label: Arc::from(DEFAULT_TREE_LOADING_LABEL),
            list: ListState::new(0, layout.row_height).with_overscan(2),
            layout,
            entries: arena.entries,
            id_index: arena.id_index,
        };
        state.rebuild_visible();
        Ok(state)
    }

    pub fn with_layout(mut self, layout: TreeLayout) -> Self {
        self.layout = layout.sanitized();
        self.list = ListState::new(self.visible.len(), self.layout.row_height).with_overscan(2);
        self
    }

    /// Replace the accessible name of the placeholder row shown while children load.
    ///
    /// Longer text than [`MAX_TREE_LABEL_BYTES`] keeps [`DEFAULT_TREE_LOADING_LABEL`].
    pub fn with_loading_label(mut self, label: impl Into<Arc<str>>) -> Self {
        let label = label.into();
        if label.len() <= MAX_TREE_LABEL_BYTES {
            self.loading_label = label;
        }
        self
    }

    pub fn loading_label(&self) -> &Arc<str> {
        &self.loading_label
    }

    /// The entry index behind one visible row, or `None` for a loading placeholder.
    fn visible_entry(&self, visible_index: usize) -> Option<usize> {
        let value = *self.visible.get(visible_index)?;
        (value & TREE_PLACEHOLDER_FLAG == 0).then_some(value as usize)
    }

    /// The entry a visible row belongs to, including the parent of a loading placeholder.
    fn visible_owner(&self, visible_index: usize) -> Option<usize> {
        let value = *self.visible.get(visible_index)?;
        Some((value & !TREE_PLACEHOLDER_FLAG) as usize)
    }

    fn is_placeholder(&self, visible_index: usize) -> bool {
        self.visible
            .get(visible_index)
            .is_some_and(|value| value & TREE_PLACEHOLDER_FLAG != 0)
    }

    /// Whether keyboard or pointer selection may land on one visible row.
    fn is_selectable(&self, visible_index: usize) -> bool {
        self.visible_entry(visible_index)
            .is_some_and(|index| !self.entries[index].disabled)
    }

    /// Whether one branch is expanded with its children still outstanding.
    pub fn is_loading(&self, id: impl Into<ElementId>) -> bool {
        self.index_for_id(id.into()).is_some_and(|index| {
            self.entries[index].pending
                && self.entries[index].child_count == 0
                && self.expanded[index]
        })
    }

    /// Whether one branch still has to load its children.
    pub fn is_pending(&self, id: impl Into<ElementId>) -> bool {
        self.index_for_id(id.into())
            .is_some_and(|index| self.entries[index].pending)
    }

    pub const fn layout(&self) -> TreeLayout {
        self.layout
    }

    pub fn set_layout(&mut self, layout: TreeLayout) -> bool {
        let layout = layout.sanitized();
        if self.layout == layout {
            return false;
        }
        if self.layout.row_height != layout.row_height {
            let viewport = self.list.viewport_size();
            let offset = self.list.logical_scroll_top();
            self.list = ListState::new(self.visible.len(), layout.row_height).with_overscan(2);
            self.list.set_viewport_size(viewport.width, viewport.height);
            self.list.scroll_to(offset);
        }
        self.layout = layout;
        true
    }

    pub fn set_nodes(
        &mut self,
        nodes: impl IntoIterator<Item = TreeNode<T>>,
    ) -> Result<(), TreeError> {
        let arena = build_tree_arena(nodes)?;
        let selected_id = self.selected_id();
        let previous_offset = self.list.logical_scroll_top();
        let previous_top = self
            .visible_owner(previous_offset.item_ix)
            .map(|index| self.entries[index].id);
        let expanded_ids = self
            .entries
            .iter()
            .zip(&self.expanded)
            .filter_map(|(entry, expanded)| (*expanded).then_some(entry.id))
            .collect::<Vec<_>>();

        self.entries = arena.entries;
        self.id_index = arena.id_index;
        self.root_count = arena.root_count;
        self.expanded.clear();
        self.expanded.resize(self.entries.len(), false);
        self.visible_position.clear();
        self.visible_position
            .resize(self.entries.len(), TREE_VISIBLE_UNSET);
        for id in expanded_ids {
            if let Some(index) = self.index_for_id(id)
                && self.entries[index].child_count != 0
            {
                self.expanded[index] = true;
            }
        }
        self.selected = selected_id.and_then(|id| self.index_for_id(id).map(|index| index as u32));
        self.rebuild_visible_from_anchor(previous_offset, previous_top);
        Ok(())
    }

    pub fn node_count(&self) -> usize {
        self.entries.len()
    }

    pub fn visible_count(&self) -> usize {
        self.visible.len()
    }

    pub const fn root_count(&self) -> usize {
        self.root_count
    }

    pub fn selected_id(&self) -> Option<ElementId> {
        self.selected.map(|index| self.entries[index as usize].id)
    }

    pub fn selected_value(&self) -> Option<&T> {
        self.selected
            .map(|index| &self.entries[index as usize].value)
    }

    pub fn row(&self, visible_index: usize) -> Option<TreeRow<'_, T>> {
        let placeholder = self.is_placeholder(visible_index);
        let index = self.visible_owner(visible_index)?;
        Some(TreeRow {
            entry: &self.entries[index],
            expanded: self.expanded[index],
            selected: self.selected == Some(index as u32),
            loading: placeholder.then_some(&self.loading_label),
        })
    }

    pub fn visible_rows(&self) -> std::ops::Range<usize> {
        self.list.visible_rows().range
    }

    pub fn list_state(&self) -> &ListState {
        &self.list
    }

    pub fn is_expanded(&self, id: impl Into<ElementId>) -> bool {
        self.index_for_id(id.into())
            .is_some_and(|index| self.expanded[index])
    }

    pub fn set_expanded(&mut self, id: impl Into<ElementId>, expanded: bool) -> bool {
        let Some(index) = self.index_for_id(id.into()) else {
            return false;
        };
        let branch = self.entries[index].child_count != 0 || self.entries[index].pending;
        if !branch || self.expanded[index] == expanded {
            return false;
        }
        self.expanded[index] = expanded;
        self.rebuild_visible();
        true
    }

    pub fn toggle_expanded(&mut self, id: impl Into<ElementId>) -> bool {
        let id = id.into();
        let Some(index) = self.index_for_id(id) else {
            return false;
        };
        self.set_expanded(id, !self.expanded[index])
    }

    pub fn select(&mut self, id: impl Into<ElementId>) -> bool {
        let Some(index) = self.index_for_id(id.into()) else {
            return false;
        };
        let visible = self.visible_position[index];
        if visible == TREE_VISIBLE_UNSET || self.entries[index].disabled {
            return false;
        }
        self.select_visible(visible as usize)
    }

    pub fn focus_handle(id: impl Into<ElementId>) -> FocusHandle {
        FocusHandle::new(id)
    }

    pub fn row_id(id: impl Into<ElementId>, node: ElementId) -> ElementId {
        derived_tree_id(id.into(), TREE_ROW_ID_TAG, node.as_u64())
    }

    pub fn disclosure_id(id: impl Into<ElementId>, node: ElementId) -> ElementId {
        derived_tree_id(id.into(), TREE_DISCLOSURE_ID_TAG, node.as_u64())
    }

    /// The stable identity of the placeholder row mounted while one branch loads.
    pub fn loading_row_id(id: impl Into<ElementId>, node: ElementId) -> ElementId {
        derived_tree_id(id.into(), TREE_LOADING_ID_TAG, node.as_u64())
    }

    /// Supply the children of a pending branch in one atomic, validated splice.
    ///
    /// The replacement is built and validated before any live state changes: an over-deep, oversize,
    /// or duplicate-ID payload leaves the tree exactly as it was. On success the branch stops being
    /// pending, its placeholder row disappears, and selection, expansion, and the logical scroll
    /// anchor are preserved. An unknown node ID reports `Ok(false)`.
    pub fn set_children(
        &mut self,
        id: impl Into<ElementId>,
        children: impl IntoIterator<Item = TreeNode<T>>,
    ) -> Result<bool, TreeError> {
        let id = id.into();
        let Some(target) = self.index_for_id(id) else {
            return Ok(false);
        };
        if self.entries[target].child_count != 0 {
            return Ok(false);
        }
        let children = children.into_iter().collect::<Vec<_>>();
        let child_count = children.len();
        let level = self.entries[target].level as usize + 1;
        let mut total_text_bytes = self.entries.iter().fold(0usize, |bytes, entry| {
            bytes.saturating_add(entry.label.len())
        });
        let mut added = Vec::with_capacity(child_count);
        for (position, child) in children.into_iter().enumerate() {
            push_tree_node(
                child,
                None,
                level,
                position,
                child_count,
                &mut added,
                &mut total_text_bytes,
            )?;
        }
        if self.entries.len().saturating_add(added.len()) > MAX_TREE_NODES {
            return Err(TreeError::TooManyNodes {
                limit: MAX_TREE_NODES,
            });
        }

        let insert_at = self.entries[target].subtree_end as usize;
        let shift = added.len() as u32;
        let mut id_index = self
            .entries
            .iter()
            .map(|entry| entry.id)
            .chain(added.iter().map(|entry| entry.id))
            .enumerate()
            .map(|(index, id)| (id.as_u64(), index as u32))
            .collect::<Vec<_>>();
        id_index.sort_unstable_by_key(|(id, _)| *id);
        if let Some(duplicate) = id_index.windows(2).find(|pair| pair[0].0 == pair[1].0) {
            return Err(TreeError::DuplicateId {
                id: ElementId::new(duplicate[0].0),
            });
        }

        // Every check passed: from here the splice cannot fail.
        let previous_offset = self.list.logical_scroll_top();
        let previous_top = self
            .visible_owner(previous_offset.item_ix)
            .map(|index| self.entries[index].id);
        let offset = insert_at as u32;
        for entry in &mut added {
            entry.parent = Some(entry.parent.map_or(target as u32, |parent| parent + offset));
            entry.subtree_end += offset;
        }
        for entry in &mut self.entries {
            if entry.subtree_end >= offset {
                entry.subtree_end += shift;
            }
            if let Some(parent) = entry.parent
                && parent >= offset
            {
                entry.parent = Some(parent + shift);
            }
        }
        self.entries[target].child_count = shift;
        self.entries[target].pending = false;
        self.entries.splice(insert_at..insert_at, added);
        if self.selected.is_some_and(|selected| selected >= offset) {
            self.selected = self.selected.map(|selected| selected + shift);
        }
        self.expanded.splice(
            insert_at..insert_at,
            std::iter::repeat_n(false, shift as usize),
        );
        self.visible_position.clear();
        self.visible_position
            .resize(self.entries.len(), TREE_VISIBLE_UNSET);
        self.id_index = self
            .entries
            .iter()
            .enumerate()
            .map(|(index, entry)| (entry.id.as_u64(), index as u32))
            .collect();
        self.id_index.sort_unstable_by_key(|(id, _)| *id);
        self.rebuild_visible_from_anchor(previous_offset, previous_top);
        Ok(true)
    }

    /// Build a complete unstyled virtualized, expandable, keyboard-navigable tree.
    ///
    /// `render_row` runs only for mounted rows and receives an optional behavior-decorated,
    /// appearance-free disclosure element. The caller decides where to place that control and owns
    /// the complete row layout and paint. QuickGUI decorates the returned row with fixed virtual
    /// geometry, tree semantics, selection, and pointer behavior. `activate` receives the selected
    /// application value by stable node ID; the tree retains no closure or scheduler afterward.
    pub fn element<V, E, RenderRow, Activate>(
        &mut self,
        cx: &mut ViewContext<'_, V>,
        id: impl Into<ElementId>,
        access: fn(&mut V) -> &mut TreeState<T>,
        render_row: RenderRow,
        activate: Activate,
    ) -> Element
    where
        V: 'static,
        T: 'static,
        E: IntoElement,
        RenderRow: FnMut(TreeRow<'_, T>, Option<Element>) -> E,
        Activate: Fn(&mut V, ElementId, &mut EventContext) + Clone + 'static,
    {
        self.element_with(cx, id, StateAccessor::from(access), render_row, activate)
    }

    /// Build the tree against a per-instance retained-state accessor.
    ///
    /// A host that renders many declared trees through one view passes an accessor that captures
    /// which [`TreeState`] each registered listener resolves.
    pub fn element_with<V, E, RenderRow, Activate>(
        &mut self,
        cx: &mut ViewContext<'_, V>,
        id: impl Into<ElementId>,
        access_source: StateAccessor<V, TreeState<T>>,
        mut render_row: RenderRow,
        activate: Activate,
    ) -> Element
    where
        V: 'static,
        T: 'static,
        E: IntoElement,
        RenderRow: FnMut(TreeRow<'_, T>, Option<Element>) -> E,
        Activate: Fn(&mut V, ElementId, &mut EventContext) + Clone + 'static,
    {
        let id = id.into();
        let root_focus = Self::focus_handle(id);
        let layout = self.layout;

        let access = access_source.clone();
        let previous = cx.action_listener(id, move |view, _: &TreePrevious, cx| {
            if access.get(view).move_selection(false) {
                cx.invalidate();
            }
        });
        let access = access_source.clone();
        let next = cx.action_listener(id, move |view, _: &TreeNext, cx| {
            if access.get(view).move_selection(true) {
                cx.invalidate();
            }
        });
        let access = access_source.clone();
        let collapse = cx.action_listener(id, move |view, _: &TreeCollapseOrParent, cx| {
            if access.get(view).collapse_or_parent() {
                cx.invalidate();
            }
        });
        let access = access_source.clone();
        let expand = cx.action_listener(id, move |view, _: &TreeExpandOrChild, cx| {
            let node = access.get(view).selected_id();
            if access.get(view).expand_or_child() {
                cx.invalidate();
                if let Some(node) = node.filter(|node| access.get(view).is_loading(*node)) {
                    cx.dispatch_action(TreeLoadChildren { node });
                }
            }
        });
        let access = access_source.clone();
        let first = cx.action_listener(id, move |view, _: &TreeFirst, cx| {
            if access.get(view).select_edge(false) {
                cx.invalidate();
            }
        });
        let access = access_source.clone();
        let last = cx.action_listener(id, move |view, _: &TreeLast, cx| {
            if access.get(view).select_edge(true) {
                cx.invalidate();
            }
        });
        let access = access_source.clone();
        let page_up = cx.action_listener(id, move |view, _: &TreePageUp, cx| {
            if access.get(view).move_page(false) {
                cx.invalidate();
            }
        });
        let access = access_source.clone();
        let page_down = cx.action_listener(id, move |view, _: &TreePageDown, cx| {
            if access.get(view).move_page(true) {
                cx.invalidate();
            }
        });
        let access = access_source.clone();
        let toggle = cx.action_listener(id, move |view, _: &TreeToggle, cx| {
            let selected = access.get(view).selected_id();
            if selected.is_some_and(|selected| access.get(view).toggle_expanded(selected)) {
                cx.invalidate();
                if let Some(node) = selected.filter(|node| access.get(view).is_loading(*node)) {
                    cx.dispatch_action(TreeLoadChildren { node });
                }
            }
        });
        let confirm_activate = activate.clone();
        let access = access_source.clone();
        let confirm = cx.action_listener(id, move |view, _: &TreeActivate, cx| {
            if let Some(selected) = access.get(view).selected_id() {
                confirm_activate(view, selected, cx);
            }
        });

        let selected = self.selected;
        let list = self.list.clone();
        let visible = list.visible_rows().range;
        let rows = list.render_rows(visible, |visible_index| {
            let loading = self.is_placeholder(visible_index);
            let node_index = self
                .visible_owner(visible_index)
                .expect("a mounted tree row is inside the visible range");
            let entry = &self.entries[node_index];
            let node_id = entry.id;
            let row_id = if loading {
                Self::loading_row_id(id, node_id)
            } else {
                Self::row_id(id, node_id)
            };
            let expanded = self.expanded[node_index];
            let row_selected = !loading && selected == Some(node_index as u32);
            let row = TreeRow {
                entry,
                expanded,
                selected: row_selected,
                loading: loading.then_some(&self.loading_label),
            };
            let disclosure = if !loading && (entry.child_count != 0 || entry.pending) {
                let disclosure_id = Self::disclosure_id(id, node_id);
                let access = access_source.clone();
                let disclosure_click = cx.listener(disclosure_id, move |view, cx| {
                    cx.stop_propagation();
                    if access.get(view).toggle_expanded(node_id) {
                        cx.focus(root_focus);
                        cx.invalidate();
                        if access.get(view).is_loading(node_id) {
                            cx.dispatch_action(TreeLoadChildren { node: node_id });
                        }
                    }
                });
                Some(
                    div()
                        .id(disclosure_id)
                        .on_click(disclosure_click)
                        .tab_index(-1)
                        .accessibility_role(AccessibilityRole::Button)
                        .accessibility_label(if expanded { "Collapse" } else { "Expand" })
                        .app_region_no_drag()
                        .cursor_default()
                        .user_select_none(),
                )
            } else {
                None
            };

            let row_label = row.label().clone();
            let row_level = row.level();
            let row_position = row.position_in_set();
            let row_size = row.size_of_set();
            let row_disabled = row.is_disabled();
            let mut row_element = render_row(row, disclosure)
                .into_element()
                .id(row_id)
                .tab_index(-1)
                .accessibility_role(AccessibilityRole::TreeItem)
                .accessibility_label(row_label)
                .accessibility_level(row_level)
                .accessibility_position_in_set(row_position)
                .accessibility_size_of_set(row_size)
                .selected(row_selected)
                .disabled(row_disabled)
                .h(layout.row_height)
                .w_full()
                .min_w(0.0)
                .overflow_hidden()
                .cursor_default()
                .app_region_no_drag();
            if !loading && (entry.child_count != 0 || entry.pending) {
                row_element = row_element.accessibility_expanded(expanded);
            }
            if !loading && !entry.disabled {
                let access = access_source.clone();
                let clicked = cx.listener(row_id, move |view, cx| {
                    if access.get(view).select(node_id) {
                        cx.invalidate();
                    }
                    cx.focus(root_focus);
                });
                row_element = row_element.on_click(clicked);
            }
            row_element
        });

        let body = div()
            .relative()
            .size_full()
            .min_w(0.0)
            .min_h(0.0)
            .overflow_hidden()
            .variable_virtual_scroll(&list)
            .app_region_no_drag()
            .child(rows);

        let mut root = div()
            .id(id)
            .track_focus(root_focus)
            .key_context(TREE_KEY_CONTEXT)
            .on_action(previous)
            .on_action(next)
            .on_action(collapse)
            .on_action(expand)
            .on_action(first)
            .on_action(last)
            .on_action(page_up)
            .on_action(page_down)
            .on_action(toggle)
            .on_action(confirm)
            .accessibility_role(AccessibilityRole::Tree)
            .accessibility_size_of_set(self.root_count)
            .size_full()
            .min_w(0.0)
            .min_h(0.0)
            .overflow_hidden()
            .app_region_no_drag()
            .child(body);
        if let Some(selected) = self.selected {
            root = root.accessibility_active_descendant(Self::row_id(
                id,
                self.entries[selected as usize].id,
            ));
        }
        root
    }

    fn index_for_id(&self, id: ElementId) -> Option<usize> {
        self.id_index
            .binary_search_by_key(&id.as_u64(), |(id, _)| *id)
            .ok()
            .map(|position| self.id_index[position].1 as usize)
    }

    fn rebuild_visible(&mut self) {
        let previous_offset = self.list.logical_scroll_top();
        let previous_top = self
            .visible_owner(previous_offset.item_ix)
            .map(|index| self.entries[index].id);

        self.rebuild_visible_from_anchor(previous_offset, previous_top);
    }

    fn rebuild_visible_from_anchor(
        &mut self,
        previous_offset: ListOffset,
        previous_top: Option<ElementId>,
    ) {
        self.visible.clear();
        self.visible_position.fill(TREE_VISIBLE_UNSET);
        let mut index = 0usize;
        while index < self.entries.len() {
            let visible_index = self.visible.len();
            self.visible.push(index as u32);
            self.visible_position[index] = visible_index as u32;
            let entry = &self.entries[index];
            if entry.pending && entry.child_count == 0 && self.expanded[index] {
                self.visible.push(index as u32 | TREE_PLACEHOLDER_FLAG);
            }
            if entry.child_count != 0 && !self.expanded[index] {
                index = entry.subtree_end as usize;
            } else {
                index += 1;
            }
        }
        self.list.set_item_count(self.visible.len());

        if let Some(previous_top) = previous_top
            && let Some(index) = self.index_for_id(previous_top)
            && self.visible_position[index] != TREE_VISIBLE_UNSET
        {
            self.list.scroll_to(ListOffset {
                item_ix: self.visible_position[index] as usize,
                offset_in_item: previous_offset.offset_in_item,
            });
        }

        self.normalize_selection();
    }

    fn normalize_selection(&mut self) {
        if let Some(mut selected) = self.selected {
            while self.visible_position[selected as usize] == TREE_VISIBLE_UNSET {
                let Some(parent) = self.entries[selected as usize].parent else {
                    self.selected = None;
                    break;
                };
                selected = parent;
                self.selected = Some(parent);
            }
        }
        if self
            .selected
            .is_some_and(|index| self.entries[index as usize].disabled)
        {
            self.selected = None;
        }
        if self.selected.is_none() {
            self.selected = (0..self.visible.len())
                .find(|visible| self.is_selectable(*visible))
                .and_then(|visible| self.visible_entry(visible))
                .map(|index| index as u32);
        }
        if let Some(selected) = self.selected {
            let visible = self.visible_position[selected as usize];
            if visible != TREE_VISIBLE_UNSET {
                self.list.scroll_to_reveal_item(visible as usize);
            }
        }
    }

    fn select_visible(&mut self, visible_index: usize) -> bool {
        let Some(index) = self.visible_entry(visible_index).map(|index| index as u32) else {
            return false;
        };
        if self.entries[index as usize].disabled {
            return false;
        }
        let changed = self.selected != Some(index);
        self.selected = Some(index);
        self.list.scroll_to_reveal_item(visible_index) || changed
    }

    fn move_selection(&mut self, forward: bool) -> bool {
        self.normalize_selection();
        let Some(selected) = self.selected else {
            return false;
        };
        let current = self.visible_position[selected as usize] as usize;
        let candidate = if forward {
            (current + 1..self.visible.len()).find(|index| self.is_selectable(*index))
        } else {
            (0..current).rev().find(|index| self.is_selectable(*index))
        };
        candidate.is_some_and(|candidate| self.select_visible(candidate))
    }

    fn select_edge(&mut self, end: bool) -> bool {
        let candidate = if end {
            (0..self.visible.len())
                .rev()
                .find(|index| self.is_selectable(*index))
        } else {
            (0..self.visible.len()).find(|index| self.is_selectable(*index))
        };
        candidate.is_some_and(|candidate| self.select_visible(candidate))
    }

    fn move_page(&mut self, forward: bool) -> bool {
        self.normalize_selection();
        let Some(selected) = self.selected else {
            return false;
        };
        let current = self.visible_position[selected as usize] as usize;
        let page = (self.list.viewport_size().height / self.layout.row_height)
            .floor()
            .max(1.0) as usize;
        let target = if forward {
            current.saturating_add(page).min(self.visible.len() - 1)
        } else {
            current.saturating_sub(page)
        };
        let candidate = if forward {
            (target..self.visible.len())
                .find(|index| self.is_selectable(*index))
                .or_else(|| (0..target).rev().find(|index| self.is_selectable(*index)))
        } else {
            (0..=target)
                .rev()
                .find(|index| self.is_selectable(*index))
                .or_else(|| {
                    (target + 1..self.visible.len()).find(|index| self.is_selectable(*index))
                })
        };
        candidate.is_some_and(|candidate| self.select_visible(candidate))
    }

    fn collapse_or_parent(&mut self) -> bool {
        self.normalize_selection();
        let Some(selected) = self.selected else {
            return false;
        };
        let index = selected as usize;
        let branch = self.entries[index].child_count != 0 || self.entries[index].pending;
        if branch && self.expanded[index] {
            self.expanded[index] = false;
            self.rebuild_visible();
            return true;
        }
        let Some(parent) = self.entries[index].parent else {
            return false;
        };
        let parent = parent as usize;
        if self.entries[parent].disabled {
            return false;
        }
        self.select_visible(self.visible_position[parent] as usize)
    }

    fn expand_or_child(&mut self) -> bool {
        self.normalize_selection();
        let Some(selected) = self.selected else {
            return false;
        };
        let index = selected as usize;
        if self.entries[index].child_count == 0 && !self.entries[index].pending {
            return false;
        }
        if !self.expanded[index] {
            self.expanded[index] = true;
            self.rebuild_visible();
            return true;
        }
        let current = self.visible_position[index] as usize;
        let end = self.entries[index].subtree_end as usize;
        (current + 1..self.visible.len())
            .take_while(|visible| {
                self.visible_owner(*visible)
                    .is_some_and(|owner| owner < end || self.is_placeholder(*visible))
            })
            .find(|visible| self.is_selectable(*visible))
            .is_some_and(|visible| self.select_visible(visible))
    }
}

fn build_tree_arena<T>(
    nodes: impl IntoIterator<Item = TreeNode<T>>,
) -> Result<TreeArena<T>, TreeError> {
    let roots = nodes.into_iter().collect::<Vec<_>>();
    let root_count = roots.len();
    let mut entries = Vec::with_capacity(root_count.min(256));
    let mut total_text_bytes = 0usize;
    for (position, node) in roots.into_iter().enumerate() {
        push_tree_node(
            node,
            None,
            0,
            position,
            root_count,
            &mut entries,
            &mut total_text_bytes,
        )?;
    }

    let mut id_index = entries
        .iter()
        .enumerate()
        .map(|(index, entry)| (entry.id.as_u64(), index as u32))
        .collect::<Vec<_>>();
    id_index.sort_unstable_by_key(|(id, _)| *id);
    if let Some(duplicate) = id_index.windows(2).find(|pair| pair[0].0 == pair[1].0) {
        return Err(TreeError::DuplicateId {
            id: ElementId::new(duplicate[0].0),
        });
    }

    Ok(TreeArena {
        entries,
        id_index,
        root_count,
    })
}

#[allow(clippy::too_many_arguments)]
fn push_tree_node<T>(
    node: TreeNode<T>,
    parent: Option<u32>,
    level: usize,
    position_in_set: usize,
    size_of_set: usize,
    entries: &mut Vec<TreeEntry<T>>,
    total_text_bytes: &mut usize,
) -> Result<(), TreeError> {
    if entries.len() == MAX_TREE_NODES {
        return Err(TreeError::TooManyNodes {
            limit: MAX_TREE_NODES,
        });
    }
    if level >= MAX_TREE_DEPTH {
        return Err(TreeError::TooDeep {
            depth: level + 1,
            limit: MAX_TREE_DEPTH,
        });
    }
    if node.label.len() > MAX_TREE_LABEL_BYTES {
        return Err(TreeError::LabelTooLong {
            id: node.id,
            bytes: node.label.len(),
            limit: MAX_TREE_LABEL_BYTES,
        });
    }
    *total_text_bytes = total_text_bytes.saturating_add(node.label.len());
    if *total_text_bytes > MAX_TREE_TEXT_BYTES {
        return Err(TreeError::TextBudgetExceeded {
            bytes: *total_text_bytes,
            limit: MAX_TREE_TEXT_BYTES,
        });
    }

    let index = entries.len() as u32;
    let child_count = node.children.len();
    entries.push(TreeEntry {
        id: node.id,
        label: node.label,
        value: node.value,
        parent,
        level: level as u32,
        position_in_set: position_in_set as u32,
        size_of_set: size_of_set as u32,
        subtree_end: index + 1,
        child_count: child_count as u32,
        disabled: node.disabled,
        pending: node.pending && child_count == 0,
    });
    for (position, child) in node.children.into_iter().enumerate() {
        push_tree_node(
            child,
            Some(index),
            level + 1,
            position,
            child_count,
            entries,
            total_text_bytes,
        )?;
    }
    entries[index as usize].subtree_end = entries.len() as u32;
    Ok(())
}

fn derived_tree_id(parent: ElementId, tag: u64, node: u64) -> ElementId {
    let mut hash = parent.as_u64() ^ tag ^ node.wrapping_mul(0x9e37_79b9_7f4a_7c15);
    hash ^= hash >> 30;
    hash = hash.wrapping_mul(0xbf58_476d_1ce4_e5b9);
    hash ^= hash >> 27;
    hash = hash.wrapping_mul(0x94d0_49bb_1331_11eb);
    hash ^= hash >> 31;
    if hash == parent.as_u64() || hash == u64::MAX {
        hash ^= tag.rotate_left(17);
    }
    ElementId::new(hash)
}

fn finite_clamped(value: f32, minimum: f32, maximum: f32, fallback: f32) -> f32 {
    if value.is_finite() {
        value.clamp(minimum, maximum)
    } else {
        fallback.clamp(minimum, maximum)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Application, View, WindowOptions, text};

    fn sample_nodes() -> Vec<TreeNode<usize>> {
        vec![
            TreeNode::new("src", "src", 0)
                .child(TreeNode::new("lib", "lib.rs", 1))
                .child(
                    TreeNode::new("runtime", "runtime", 2)
                        .child(TreeNode::new("macos", "macos.rs", 3))
                        .child(TreeNode::new("test", "test_context.rs", 4).disabled(true)),
                ),
            TreeNode::new("cargo", "Cargo.toml", 5),
        ]
    }

    #[test]
    fn preorder_arena_expansion_navigation_and_top_anchor_are_bounded() {
        let mut tree = TreeState::new(sample_nodes()).unwrap();
        tree.list.set_viewport_size(300.0, 60.0);
        assert_eq!(tree.node_count(), 6);
        assert_eq!(tree.visible_count(), 2);
        assert_eq!(tree.selected_id(), Some("src".into()));

        assert!(tree.set_expanded("src", true));
        assert_eq!(tree.visible_count(), 4);
        assert!(tree.select("runtime"));
        assert!(tree.expand_or_child());
        assert_eq!(tree.visible_count(), 6);
        assert!(tree.expand_or_child());
        assert_eq!(tree.selected_id(), Some("macos".into()));
        assert!(tree.collapse_or_parent());
        assert_eq!(tree.selected_id(), Some("runtime".into()));
        assert!(tree.collapse_or_parent());
        assert!(!tree.is_expanded("runtime"));
        assert!(tree.move_selection(true));
        assert_eq!(tree.selected_id(), Some("cargo".into()));
        assert!(!tree.collapse_or_parent());
        assert!(tree.visible_rows().len() <= tree.visible_count());
    }

    #[test]
    fn large_flat_trees_mount_only_the_viewport_and_overscan() {
        let nodes = (0..100_000)
            .map(|index| TreeNode::new(index as u64 + 1, format!("Node {index}"), index));
        let tree = TreeState::new(nodes).unwrap();
        tree.list.set_viewport_size(320.0, 90.0);
        assert_eq!(tree.node_count(), 100_000);
        assert_eq!(tree.visible_count(), 100_000);
        assert!(tree.visible_rows().len() <= 7);
        assert_eq!(tree.list.stats().measured_items, 0);
    }

    #[test]
    fn layout_is_bounded_and_row_height_changes_preserve_the_logical_anchor() {
        let mut tree = TreeState::new(sample_nodes()).unwrap();
        tree.set_expanded("src", true);
        tree.list.set_viewport_size(300.0, 60.0);
        tree.list.scroll_to(ListOffset {
            item_ix: 2,
            offset_in_item: 4.0,
        });
        let before = tree.list.logical_scroll_top();

        assert!(tree.set_layout(TreeLayout::new(44.0)));
        assert_eq!(tree.layout().row_height, 44.0);
        assert_eq!(tree.list.logical_scroll_top().item_ix, before.item_ix);

        let invalid = TreeLayout {
            row_height: f32::NAN,
        };
        assert!(tree.set_layout(invalid));
        assert_eq!(tree.layout().row_height, 30.0);
        assert_eq!(tree.list.logical_scroll_top().item_ix, before.item_ix);
    }

    #[test]
    fn source_replacement_is_atomic_and_preserves_stable_state() {
        let mut tree = TreeState::new(sample_nodes()).unwrap();
        tree.list.set_viewport_size(300.0, 30.0);
        assert!(tree.set_expanded("src", true));
        assert!(tree.set_expanded("runtime", true));
        assert!(tree.select("macos"));
        tree.list.scroll_to(ListOffset {
            item_ix: 3,
            offset_in_item: 4.0,
        });

        tree.set_nodes([
            TreeNode::new("cargo", "Cargo.toml", 50),
            TreeNode::new("src", "source", 10)
                .child(
                    TreeNode::new("runtime", "platform", 20)
                        .child(TreeNode::new("macos", "macos.rs", 30))
                        .child(TreeNode::new("linux", "linux.rs", 40)),
                )
                .child(TreeNode::new("lib", "lib.rs", 11)),
        ])
        .unwrap();

        assert_eq!(tree.selected_id(), Some("macos".into()));
        assert_eq!(tree.selected_value(), Some(&30));
        assert!(tree.is_expanded("src"));
        assert!(tree.is_expanded("runtime"));
        let top = tree.list.logical_scroll_top();
        assert_eq!(tree.row(top.item_ix).map(TreeRow::id), Some("macos".into()));

        let error = tree
            .set_nodes([
                TreeNode::new("duplicate", "A", 1),
                TreeNode::new("duplicate", "B", 2),
            ])
            .unwrap_err();
        assert_eq!(
            error,
            TreeError::DuplicateId {
                id: "duplicate".into()
            }
        );
        assert_eq!(tree.node_count(), 6);
        assert_eq!(tree.selected_id(), Some("macos".into()));
        assert_eq!(tree.selected_value(), Some(&30));
        assert!(tree.is_expanded("src"));
        assert!(tree.is_expanded("runtime"));

        tree.set_nodes([TreeNode::new("cargo", "Cargo.toml", 60)])
            .unwrap();
        assert_eq!(tree.node_count(), 1);
        assert_eq!(tree.selected_id(), Some("cargo".into()));
    }

    #[test]
    fn duplicate_depth_and_text_limits_fail_deterministically() {
        assert_eq!(
            TreeState::new([
                TreeNode::new("same", "A", ()),
                TreeNode::new("same", "B", ()),
            ])
            .unwrap_err(),
            TreeError::DuplicateId { id: "same".into() }
        );

        let oversized = "x".repeat(MAX_TREE_LABEL_BYTES + 1);
        assert_eq!(
            TreeState::new([TreeNode::new("large", oversized, ())]).unwrap_err(),
            TreeError::LabelTooLong {
                id: "large".into(),
                bytes: MAX_TREE_LABEL_BYTES + 1,
                limit: MAX_TREE_LABEL_BYTES,
            }
        );

        let mut node = TreeNode::new(0_u64, "0", ());
        for level in 1..=MAX_TREE_DEPTH {
            node = TreeNode::new(level, level.to_string(), ()).child(node);
        }
        assert!(matches!(
            TreeState::new([node]),
            Err(TreeError::TooDeep {
                limit: MAX_TREE_DEPTH,
                ..
            })
        ));
    }

    #[derive(Debug)]
    struct TreeView {
        tree: TreeState<usize>,
        activated: Option<ElementId>,
    }

    impl Default for TreeView {
        fn default() -> Self {
            Self {
                tree: TreeState::new(sample_nodes()).unwrap(),
                activated: None,
            }
        }
    }

    impl TreeView {
        fn tree(view: &mut Self) -> &mut TreeState<usize> {
            &mut view.tree
        }
    }

    impl View for TreeView {
        fn render(&mut self, cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
            self.tree.element(
                cx,
                "tree",
                Self::tree,
                |row, disclosure| {
                    let disclosure = disclosure.map_or_else(
                        || div().w(20.0).h(30.0).flex_none(),
                        |disclosure| {
                            disclosure.w(20.0).h(30.0).child(if row.is_expanded() {
                                "⌄"
                            } else {
                                "›"
                            })
                        },
                    );
                    div()
                        .flex_row()
                        .items_center()
                        .child(disclosure)
                        .child(text(row.label().clone()).no_wrap().text_ellipsis())
                },
                |view, id, cx| {
                    view.activated = Some(id);
                    cx.invalidate();
                },
            )
        }
    }

    #[test]
    fn tree_uses_composite_focus_click_keyboard_activation_and_idle_paths() {
        let (mut cx, view) = Application::new()
            .bind_keys(tree_key_bindings())
            .into_test_context(WindowOptions::default(), TreeView::default())
            .unwrap();
        let window = view.window_handle();

        cx.simulate_keystrokes(window, "tab right down down right right enter")
            .unwrap();
        assert_eq!(cx.focused(window).unwrap(), Some("tree".into()));
        assert_eq!(
            cx.read(view, |view| view.tree.selected_id()).unwrap(),
            Some("macos".into())
        );
        assert_eq!(
            cx.read(view, |view| view.activated).unwrap(),
            Some("macos".into())
        );

        let cargo_row = TreeState::<usize>::row_id("tree", "cargo".into());
        cx.click(window, cargo_row).unwrap();
        assert_eq!(
            cx.read(view, |view| view.tree.selected_id()).unwrap(),
            Some("cargo".into())
        );
        assert_eq!(cx.focused(window).unwrap(), Some("tree".into()));

        let renders = cx.render_count(window).unwrap();
        cx.run_until_idle().unwrap();
        assert_eq!(cx.render_count(window).unwrap(), renders);
    }

    #[test]
    fn tree_bindings_are_contextual_and_complete() {
        let bindings = tree_key_bindings();
        assert_eq!(bindings.len(), 10);
        assert!(
            bindings.iter().all(
                |binding| binding.context_predicate().is_some_and(|context| context
                    .depth_of(&[crate::KeyContext::parse(TREE_KEY_CONTEXT).unwrap()])
                    .is_some())
            )
        );
    }

    #[test]
    fn pending_branches_mount_one_placeholder_and_load_atomically() {
        let mut tree = TreeState::new(vec![
            TreeNode::new("src", "src", 0).child(TreeNode::new("lib", "lib.rs", 1)),
            TreeNode::new("remote", "remote", 2).pending(true),
        ])
        .unwrap();
        assert_eq!(tree.node_count(), 3);
        assert_eq!(tree.visible_count(), 2);
        assert!(tree.is_pending("remote"));
        assert!(
            !tree.is_loading("remote"),
            "a collapsed branch loads nothing"
        );

        assert!(tree.set_expanded("remote", true));
        assert!(tree.is_loading("remote"));
        assert_eq!(tree.visible_count(), 3, "exactly one placeholder row");
        let placeholder = tree.row(2).expect("the placeholder row is visible");
        assert!(placeholder.is_loading());
        assert_eq!(&**placeholder.label(), DEFAULT_TREE_LOADING_LABEL);
        assert_eq!(
            placeholder.level(),
            1,
            "the placeholder sits under its branch"
        );
        assert!(placeholder.is_disabled());
        assert!(!placeholder.is_selected());
        assert_eq!(placeholder.id(), "remote".into());

        assert!(tree.select("remote"));
        assert!(
            !tree.move_selection(true),
            "keyboard selection never lands on a placeholder"
        );
        assert_eq!(tree.selected_id(), Some("remote".into()));

        // A rejected payload leaves every retained field untouched.
        let error = tree
            .set_children("remote", [TreeNode::new("lib", "duplicate", 9)])
            .unwrap_err();
        assert_eq!(error, TreeError::DuplicateId { id: "lib".into() });
        assert_eq!(tree.node_count(), 3);
        assert_eq!(tree.visible_count(), 3);
        assert!(tree.is_loading("remote"));

        assert!(
            tree.set_children(
                "remote",
                [
                    TreeNode::new("alpha", "alpha.rs", 10),
                    TreeNode::new("beta", "beta.rs", 11),
                ],
            )
            .unwrap()
        );
        assert_eq!(tree.node_count(), 5);
        assert!(!tree.is_pending("remote"));
        assert!(!tree.is_loading("remote"));
        assert_eq!(tree.visible_count(), 4);
        assert_eq!(tree.row(2).map(TreeRow::id), Some("alpha".into()));
        assert_eq!(tree.row(2).map(TreeRow::level), Some(1));
        assert!(!tree.row(2).unwrap().is_loading());
        assert_eq!(tree.selected_id(), Some("remote".into()));
        assert!(tree.select("beta"));
        assert_eq!(tree.selected_value(), Some(&11));
        assert!(tree.set_expanded("src", true));
        assert_eq!(
            tree.row(1).map(TreeRow::id),
            Some("lib".into()),
            "the untouched subtree keeps its order"
        );

        assert!(
            !tree
                .set_children("missing", [TreeNode::new("z", "z", 0)])
                .unwrap(),
            "an unknown node reports no load"
        );
        assert!(
            !tree
                .set_children("src", [TreeNode::new("z", "z", 0)])
                .unwrap(),
            "a loaded branch refuses a second splice"
        );

        // Loading nothing turns the branch into an ordinary leaf.
        let mut empty = TreeState::new([TreeNode::new("empty", "empty", 0).pending(true)]).unwrap();
        assert!(empty.set_expanded("empty", true));
        assert_eq!(empty.visible_count(), 2);
        assert!(
            empty
                .set_children("empty", Vec::<TreeNode<usize>>::new())
                .unwrap()
        );
        assert_eq!(empty.visible_count(), 1);
        assert!(!empty.is_pending("empty"));
        assert!(!empty.set_expanded("empty", true), "a leaf does not expand");
    }

    #[test]
    fn oversized_lazy_payloads_are_rejected_before_the_tree_changes() {
        let mut tree = TreeState::new([TreeNode::new("root", "root", 0).pending(true)]).unwrap();
        assert!(tree.set_expanded("root", true));

        let oversized = "x".repeat(MAX_TREE_LABEL_BYTES + 1);
        assert_eq!(
            tree.set_children("root", [TreeNode::new("big", oversized, 1)])
                .unwrap_err(),
            TreeError::LabelTooLong {
                id: "big".into(),
                bytes: MAX_TREE_LABEL_BYTES + 1,
                limit: MAX_TREE_LABEL_BYTES,
            }
        );
        assert_eq!(tree.node_count(), 1);
        assert!(tree.is_loading("root"));

        let mut deep = TreeNode::new(0_u64, "0", 0);
        for level in 1..MAX_TREE_DEPTH {
            deep = TreeNode::new(level as u64, level.to_string(), 0).child(deep);
        }
        assert!(matches!(
            tree.set_children("root", [deep]),
            Err(TreeError::TooDeep {
                limit: MAX_TREE_DEPTH,
                ..
            })
        ));
        assert_eq!(tree.node_count(), 1);
        assert!(tree.is_loading("root"));
    }

    #[derive(Debug)]
    struct LazyTreeView {
        tree: TreeState<usize>,
        requests: Vec<ElementId>,
    }

    impl Default for LazyTreeView {
        fn default() -> Self {
            Self {
                tree: TreeState::new([
                    TreeNode::new("remote", "remote", 0).pending(true),
                    TreeNode::new("local", "local", 1),
                ])
                .unwrap()
                .with_loading_label("Fetching…"),
                requests: Vec::new(),
            }
        }
    }

    impl LazyTreeView {
        fn tree(view: &mut Self) -> &mut TreeState<usize> {
            &mut view.tree
        }
    }

    impl View for LazyTreeView {
        fn render(&mut self, cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
            let load = cx.action_listener(
                "lazy-tree",
                |view: &mut Self, action: &TreeLoadChildren, cx| {
                    view.requests.push(action.node);
                    view.tree
                        .set_children(action.node, [TreeNode::new("child", "child.rs", 7)])
                        .expect("the payload is valid");
                    cx.invalidate();
                },
            );
            self.tree
                .element(
                    cx,
                    "lazy-tree",
                    Self::tree,
                    |row, disclosure| {
                        let disclosure = disclosure.map_or_else(
                            || div().w(20.0).h(30.0).flex_none(),
                            |handle| handle.w(20.0).h(30.0),
                        );
                        div()
                            .flex_row()
                            .items_center()
                            .child(disclosure)
                            .child(text(row.label().clone()).no_wrap())
                    },
                    |_view, _id, _cx| {},
                )
                .on_action(load)
        }
    }

    #[test]
    fn expanding_a_pending_branch_requests_children_once_through_a_typed_action() {
        let (mut cx, view) = Application::new()
            .bind_keys(tree_key_bindings())
            .into_test_context(WindowOptions::default(), LazyTreeView::default())
            .unwrap();
        let window = view.window_handle();

        cx.simulate_keystrokes(window, "tab").unwrap();
        assert_eq!(cx.focused(window).unwrap(), Some("lazy-tree".into()));
        assert_eq!(cx.read(view, |view| view.tree.node_count()).unwrap(), 2);

        cx.simulate_keystrokes(window, "right").unwrap();
        assert_eq!(
            cx.read(view, |view| view.requests.clone()).unwrap(),
            vec![ElementId::from("remote")]
        );
        assert_eq!(cx.read(view, |view| view.tree.node_count()).unwrap(), 3);
        assert!(
            !cx.read(view, |view| view.tree.is_loading("remote"))
                .unwrap()
        );
        assert_eq!(cx.read(view, |view| view.tree.visible_count()).unwrap(), 3);

        // A second expansion of a loaded branch asks for nothing.
        cx.simulate_keystrokes(window, "left right").unwrap();
        assert_eq!(cx.read(view, |view| view.requests.len()).unwrap(), 1);

        let renders = cx.render_count(window).unwrap();
        cx.run_until_idle().unwrap();
        assert_eq!(cx.render_count(window).unwrap(), renders);
    }
}