summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/content/widgets/tree-view.mjs
blob: aef622fa274a7c6af0c21c543b9a116a8e4dee9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/. */

const { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);
import { TreeSelection } from "chrome://messenger/content/tree-selection.mjs";

// Account for the mac OS accelerator key variation.
// Use these strings to check keyboard event properties.
const accelKeyName = AppConstants.platform == "macosx" ? "metaKey" : "ctrlKey";
const otherKeyName = AppConstants.platform == "macosx" ? "ctrlKey" : "metaKey";

const ANIMATION_DURATION_MS = 200;
const reducedMotionMedia = matchMedia("(prefers-reduced-motion)");

/**
 * Main tree view container that takes care of generating the main scrollable
 * DIV and the tree table.
 */
class TreeView extends HTMLElement {
  static observedAttributes = ["rows"];

  /**
   * The number of rows on either side to keep of the visible area to keep in
   * memory in order to avoid visible blank spaces while the user scrolls.
   *
   * This member is visible for testing and should not be used outside of this
   * class in production code.
   *
   * @type {integer}
   */
  _toleranceSize = 0;

  /**
   * Set the size of the tolerance buffer based on the number of rows which can
   * be visible at once.
   */
  #calculateToleranceBufferSize() {
    this._toleranceSize = this.#calculateVisibleRowCount() * 2;
  }

  /**
   * Index of the first row that exists in the DOM. Includes rows in the
   * tolerance buffer if they have been added.
   *
   * @type {integer}
   */
  #firstBufferRowIndex = 0;

  /**
   * Index of the last row that exists in the DOM. Includes rows in the
   * tolerance buffer if they have been added.
   *
   * @type {integer}
   */
  #lastBufferRowIndex = 0;

  /**
   * Index of the first visible row.
   *
   * @type {integer}
   */
  #firstVisibleRowIndex = 0;

  /**
   * Index of the last visible row.
   *
   * @type {integer}
   */
  #lastVisibleRowIndex = 0;

  /**
   * Row indices mapped to the row elements that exist in the DOM.
   *
   * @type {Map<integer, HTMLTableRowElement>}
   */
  _rows = new Map();

  /**
   * The current view.
   *
   * @type {nsITreeView}
   */
  _view = null;

  /**
   * The current selection.
   *
   * @type {nsITreeSelection}
   */
  _selection = null;

  /**
   * The function storing the timeout callback for the delayed select feature in
   * order to clear it when not needed.
   *
   * @type {integer}
   */
  _selectTimeout = null;

  /**
   * A handle to the callback to fill the buffer when we aren't busy painting.
   *
   * @type {number}
   */
  #bufferFillIdleCallbackHandle = null;

  /**
   * The virtualized table containing our rows.
   *
   * @type {TreeViewTable}
   */
  table = null;

  /**
   * An event to fire to indicate the work of filling the buffer is complete.
   * This will fire once both visible and tolerance rows are ready. It will also
   * fire if no change to the buffer is required.
   *
   * This member is visible in order to provide a reliable indicator to tests
   * that all expected rows should be in place. It should not be used in
   * production code.
   *
   * @type {Event}
   */
  _rowBufferReadyEvent = null;

  /**
   * Fire the provided event, if any, in order to indicate that any necessary
   * buffer modification work is complete, including if no work is necessary.
   */
  #dispatchRowBufferReadyEvent() {
    // Don't fire if we're currently waiting on buffer fills; let the callback
    // do that when it's finished.
    if (this._rowBufferReadyEvent && !this.#bufferFillIdleCallbackHandle) {
      this.dispatchEvent(this._rowBufferReadyEvent);
    }
  }

  /**
   * Determine the height of the visible row area, excluding any chrome which
   * covers elements.
   *
   * WARNING: This may cause synchronous reflow if used after modifying the DOM.
   *
   * @returns {integer} - The height of the area into which visible rows are
   *   rendered.
   */
  #calculateVisibleHeight() {
    // Account for the table header height in a sticky position above the body.
    return this.clientHeight - this.table.header.clientHeight;
  }

  /**
   * Determine how many rows are visible in the client presently.
   *
   * WARNING: This may cause synchronous reflow if used after modifying the DOM.
   *
   * @returns {integer} - The number of visible or partly-visible rows.
   */
  #calculateVisibleRowCount() {
    return Math.ceil(
      this.#calculateVisibleHeight() / this._rowElementClass.ROW_HEIGHT
    );
  }

  connectedCallback() {
    if (this.hasConnected) {
      return;
    }
    this.hasConnected = true;

    // Prevent this element from being part of the roving tab focus since we
    // handle that independently for the TreeViewTableBody and we don't want any
    // interference from this.
    this.tabIndex = -1;
    this.classList.add("tree-view-scrollable-container");

    this.table = document.createElement("table", { is: "tree-view-table" });
    this.appendChild(this.table);

    this.placeholder = this.querySelector(`slot[name="placeholders"]`);

    this.addEventListener("scroll", this);

    let lastHeight = 0;
    this.resizeObserver = new ResizeObserver(entries => {
      // The width of the table isn't important to virtualizing the table. Skip
      // updating if the height hasn't changed.
      if (this.clientHeight == lastHeight) {
        this.#dispatchRowBufferReadyEvent();
        return;
      }

      if (!this._rowElementClass) {
        this.#dispatchRowBufferReadyEvent();
        return;
      }

      // The number of rows in the tolerance buffer is based on the number of
      // rows which can be visible. Update it.
      this.#calculateToleranceBufferSize();

      // There's not much point in reducing the number of rows on resize. Scroll
      // height remains the same and we can retain the extra rows in the buffer.
      if (this.clientHeight > lastHeight) {
        this._ensureVisibleRowsAreDisplayed();
      } else {
        this.#dispatchRowBufferReadyEvent();
      }

      lastHeight = this.clientHeight;
    });
    this.resizeObserver.observe(this);
  }

  disconnectedCallback() {
    this.#resetRowBuffer();
    this.resizeObserver.disconnect();
  }

  attributeChangedCallback(attrName, oldValue, newValue) {
    this._rowElementName = newValue || "tree-view-table-row";
    this._rowElementClass = customElements.get(this._rowElementName);

    this.#calculateToleranceBufferSize();

    if (this._view) {
      this.reset();
    }
  }

  handleEvent(event) {
    switch (event.type) {
      case "keyup": {
        if (
          ["Tab", "F6"].includes(event.key) &&
          this.currentIndex == -1 &&
          this._view?.rowCount
        ) {
          let selectionChanged = false;
          if (this.selectedIndex == -1) {
            this._selection.select(0);
            selectionChanged = true;
          }
          this.currentIndex = this.selectedIndex;
          if (selectionChanged) {
            this.onSelectionChanged();
          }
        }
        break;
      }
      case "click": {
        if (event.button !== 0) {
          return;
        }

        let row = event.target.closest(`tr[is="${this._rowElementName}"]`);
        if (!row) {
          return;
        }

        let index = row.index;

        if (event.target.classList.contains("tree-button-thread")) {
          if (this._view.isContainerOpen(index)) {
            let children = 0;
            for (
              let i = index + 1;
              i < this._view.rowCount && this._view.getLevel(i) > 0;
              i++
            ) {
              children++;
            }
            this._selectRange(index, index + children, event[accelKeyName]);
          } else {
            let addedRows = this.expandRowAtIndex(index);
            this._selectRange(index, index + addedRows, event[accelKeyName]);
          }
          this.table.body.focus();
          return;
        }

        if (this._view.isContainer(index) && event.target.closest(".twisty")) {
          if (this._view.isContainerOpen(index)) {
            this.collapseRowAtIndex(index);
          } else {
            let addedRows = this.expandRowAtIndex(index);
            this.scrollToIndex(
              index + Math.min(addedRows, this.#calculateVisibleRowCount() - 1)
            );
          }
          this.table.body.focus();
          return;
        }

        // Handle the click as a CTRL extension if it happens on the checkbox
        // image inside the selection column.
        if (event.target.classList.contains("tree-view-row-select-checkbox")) {
          if (event.shiftKey) {
            this._selectRange(-1, index, event[accelKeyName]);
          } else {
            this._toggleSelected(index);
          }
          this.table.body.focus();
          return;
        }

        if (event.target.classList.contains("tree-button-request-delete")) {
          this.table.body.dispatchEvent(
            new CustomEvent("request-delete", {
              bubbles: true,
              detail: {
                index,
              },
            })
          );
          this.table.body.focus();
          return;
        }

        if (event.target.classList.contains("tree-button-flag")) {
          this.table.body.dispatchEvent(
            new CustomEvent("toggle-flag", {
              bubbles: true,
              detail: {
                isFlagged: row.dataset.properties.includes("flagged"),
                index,
              },
            })
          );
          this.table.body.focus();
          return;
        }

        if (event.target.classList.contains("tree-button-unread")) {
          this.table.body.dispatchEvent(
            new CustomEvent("toggle-unread", {
              bubbles: true,
              detail: {
                isUnread: row.dataset.properties.includes("unread"),
                index,
              },
            })
          );
          this.table.body.focus();
          return;
        }

        if (event.target.classList.contains("tree-button-spam")) {
          this.table.body.dispatchEvent(
            new CustomEvent("toggle-spam", {
              bubbles: true,
              detail: {
                isJunk: row.dataset.properties.split(" ").includes("junk"),
                index,
              },
            })
          );
          this.table.body.focus();
          return;
        }

        if (event[accelKeyName] && !event.shiftKey) {
          this._toggleSelected(index);
        } else if (event.shiftKey) {
          this._selectRange(-1, index, event[accelKeyName]);
        } else {
          this._selectSingle(index);
        }

        this.table.body.focus();
        break;
      }
      case "keydown": {
        if (event.altKey || event[otherKeyName]) {
          return;
        }

        let currentIndex = this.currentIndex == -1 ? 0 : this.currentIndex;
        let newIndex;
        switch (event.key) {
          case "ArrowUp":
            newIndex = currentIndex - 1;
            break;
          case "ArrowDown":
            newIndex = currentIndex + 1;
            break;
          case "ArrowLeft":
          case "ArrowRight": {
            event.preventDefault();
            if (this.currentIndex == -1) {
              return;
            }
            let isArrowRight = event.key == "ArrowRight";
            let isRTL = this.matches(":dir(rtl)");
            if (isArrowRight == isRTL) {
              // Collapse action.
              let currentLevel = this._view.getLevel(this.currentIndex);
              if (this._view.isContainerOpen(this.currentIndex)) {
                this.collapseRowAtIndex(this.currentIndex);
                return;
              } else if (currentLevel == 0) {
                return;
              }

              let parentIndex = this._view.getParentIndex(this.currentIndex);
              if (parentIndex != -1) {
                newIndex = parentIndex;
              }
            } else if (this._view.isContainer(this.currentIndex)) {
              // Expand action.
              if (!this._view.isContainerOpen(this.currentIndex)) {
                let addedRows = this.expandRowAtIndex(this.currentIndex);
                this.scrollToIndex(
                  this.currentIndex +
                    Math.min(addedRows, this.#calculateVisibleRowCount() - 1)
                );
              } else {
                newIndex = this.currentIndex + 1;
              }
            }
            if (newIndex != undefined) {
              this._selectSingle(newIndex);
            }
            return;
          }
          case "Home":
            newIndex = 0;
            break;
          case "End":
            newIndex = this._view.rowCount - 1;
            break;
          case "PageUp":
            newIndex = Math.max(
              0,
              currentIndex - this.#calculateVisibleRowCount()
            );
            break;
          case "PageDown":
            newIndex = Math.min(
              this._view.rowCount - 1,
              currentIndex + this.#calculateVisibleRowCount()
            );
            break;
        }

        if (newIndex != undefined) {
          newIndex = this._clampIndex(newIndex);
          if (newIndex != null) {
            if (event[accelKeyName] && !event.shiftKey) {
              // Change focus, but not selection.
              this.currentIndex = newIndex;
            } else if (event.shiftKey) {
              this._selectRange(-1, newIndex, event[accelKeyName]);
            } else {
              this._selectSingle(newIndex, true);
            }
          }
          event.preventDefault();
          return;
        }

        // Space bar keystroke selection toggling.
        if (event.key == " " && this.currentIndex != -1) {
          // Don't do anything if we're on macOS and the target row is already
          // selected.
          if (
            AppConstants.platform == "macosx" &&
            this._selection.isSelected(this.currentIndex)
          ) {
            return;
          }

          // Handle the macOS exception of toggling the selection with only
          // the space bar since CMD+Space is captured by the OS.
          if (event[accelKeyName] || AppConstants.platform == "macosx") {
            this._toggleSelected(this.currentIndex);
            event.preventDefault();
          } else if (!this._selection.isSelected(this.currentIndex)) {
            // The target row is not currently selected.
            this._selectSingle(this.currentIndex, true);
            event.preventDefault();
          }
        }
        break;
      }
      case "scroll":
        this._ensureVisibleRowsAreDisplayed();
        break;
    }
  }

  /**
   * The current view for this list.
   *
   * @type {nsITreeView}
   */
  get view() {
    return this._view;
  }

  set view(view) {
    this._selection = null;
    if (this._view) {
      this._view.setTree(null);
      this._view.selection = null;
    }
    if (this._selection) {
      this._selection.view = null;
    }

    this._view = view;
    if (view) {
      try {
        this._selection = new TreeSelection();
        this._selection.tree = this;
        this._selection.view = view;

        view.selection = this._selection;
        view.setTree(this);
      } catch (ex) {
        // This isn't a XULTreeElement, and we can't make it one, so if the
        // `setTree` call crosses XPCOM, an exception will be thrown.
        if (ex.result != Cr.NS_ERROR_XPC_BAD_CONVERT_JS) {
          throw ex;
        }
      }
    }

    // Clear the height of the top spacer to avoid confusing
    // `_ensureVisibleRowsAreDisplayed`.
    this.table.spacerTop.setHeight(0);
    this.reset();

    this.dispatchEvent(new CustomEvent("viewchange"));
  }

  /**
   * Set the colspan of the spacer row cells.
   *
   * @param {int} count - The amount of visible columns.
   */
  setSpacersColspan(count) {
    // Add an extra column if the table is editable to account for the column
    // picker column.
    if (this.parentNode.editable) {
      count++;
    }
    this.table.spacerTop.setColspan(count);
    this.table.spacerBottom.setColspan(count);
  }

  /**
   * Clear all rows from the buffer, empty the table body, and reset spacers.
   */
  #resetRowBuffer() {
    this.#cancelToleranceFillCallback();
    this.table.body.replaceChildren();
    this._rows.clear();
    this.#firstBufferRowIndex = 0;
    this.#lastBufferRowIndex = 0;
    this.#firstVisibleRowIndex = 0;

    // Set the height of the bottom spacer to account for the now-missing rows.
    // We want to ensure that the overall scroll height does not decrease.
    // Otherwise, we may lose our scroll position and cause unnecessary
    // scrolling. However, we don't always want to change the height of the top
    // spacer for the same reason.
    let rowCount = this._view?.rowCount ?? 0;
    this.table.spacerBottom.setHeight(
      rowCount * this._rowElementClass.ROW_HEIGHT
    );
  }

  /**
   * Clear all rows from the list and create them again.
   */
  reset() {
    this.#resetRowBuffer();
    this._ensureVisibleRowsAreDisplayed();
  }

  /**
   * Updates all existing rows in place, without removing all the rows and
   * starting again. This can be used if the row element class hasn't changed
   * and its `index` setter is capable of handling any modifications required.
   */
  invalidate() {
    this.invalidateRange(this.#firstBufferRowIndex, this.#lastBufferRowIndex);
  }

  /**
   * Perform the actions necessary to invalidate the specified row. Implemented
   * separately to allow {@link invalidateRange} to handle testing event fires
   * on its own.
   *
   * @param {integer} index
   */
  #doInvalidateRow(index) {
    const rowCount = this._view?.rowCount ?? 0;
    let row = this.getRowAtIndex(index);
    if (row) {
      if (index >= rowCount) {
        this._removeRowAtIndex(index);
      } else {
        row.index = index;
        row.selected = this._selection.isSelected(index);
      }
    } else if (
      index >= this.#firstBufferRowIndex &&
      index <= Math.min(rowCount - 1, this.#lastBufferRowIndex)
    ) {
      this._addRowAtIndex(index);
    }
  }

  /**
   * Invalidate the rows between `startIndex` and `endIndex`.
   *
   * @param {integer} startIndex
   * @param {integer} endIndex
   */
  invalidateRange(startIndex, endIndex) {
    for (
      let index = Math.max(startIndex, this.#firstBufferRowIndex),
        last = Math.min(endIndex, this.#lastBufferRowIndex);
      index <= last;
      index++
    ) {
      this.#doInvalidateRow(index);
    }
    this._ensureVisibleRowsAreDisplayed();
  }

  /**
   * Invalidate the row at `index` in place. If `index` refers to a row that
   * should exist but doesn't (because the row count increased), adds a row.
   * If `index` refers to a row that does exist but shouldn't (because the
   * row count decreased), removes it.
   *
   * @param {integer} index
   */
  invalidateRow(index) {
    this.#doInvalidateRow(index);
    this.#dispatchRowBufferReadyEvent();
  }

  /**
   * A contiguous range, inclusive of both extremes.
   *
   * @typedef InclusiveRange
   * @property {integer} first - The inclusive start of the range.
   * @property {integer} last - The inclusive end of the range.
   */

  /**
   * Calculate the range of rows we wish to have in a filled tolerance buffer
   * based on a given range of visible rows.
   *
   * @param {integer} firstVisibleRow - The first visible row in the range.
   * @param {integer} lastVisibleRow - The last visible row in the range.
   * @param {integer} dataRowCount - The total number of available rows in the
   *   source data.
   * @returns {InclusiveRange} - The full range of the desired buffer.
   */
  #calculateDesiredBufferRange(firstVisibleRow, lastVisibleRow, dataRowCount) {
    const desiredRowRange = {};

    desiredRowRange.first = Math.max(firstVisibleRow - this._toleranceSize, 0);
    desiredRowRange.last = Math.min(
      lastVisibleRow + this._toleranceSize,
      dataRowCount - 1
    );

    return desiredRowRange;
  }

  #createToleranceFillCallback() {
    // Don't schedule a new buffer fill callback if we already have one.
    if (!this.#bufferFillIdleCallbackHandle) {
      this.#bufferFillIdleCallbackHandle = requestIdleCallback(deadline =>
        this.#fillToleranceBuffer(deadline)
      );
    }
  }

  #cancelToleranceFillCallback() {
    cancelIdleCallback(this.#bufferFillIdleCallbackHandle);
    this.#bufferFillIdleCallbackHandle = null;
  }

  /**
   * Fill the buffer with tolerance rows above and below the visible rows.
   *
   * As fetching data and modifying the DOM is expensive, this is intended to be
   * run within an idle callback and includes management of the idle callback
   * handle and creation of further callbacks if work is not completed.
   *
   * @param {IdleDeadline} deadline - A deadline object for fetching the
   *   remaining time in the idle tick.
   */
  #fillToleranceBuffer(deadline) {
    this.#bufferFillIdleCallbackHandle = null;

    const rowCount = this._view?.rowCount ?? 0;
    if (!rowCount) {
      return;
    }

    const bufferRange = this.#calculateDesiredBufferRange(
      this.#firstVisibleRowIndex,
      this.#lastVisibleRowIndex,
      rowCount
    );

    // Set the amount of time to leave in the deadline to fill another row. In
    // order to cooperatively schedule work, we shouldn't overrun the time
    // allotted for the idle tick. This value should be set such that it leaves
    // enough time to perform another row fill and adjust the relevant spacer
    // while doing the maximal amount of work per callback.
    const MS_TO_LEAVE_PER_FILL = 1.25;

    // Fill in the beginning of the buffer.
    if (bufferRange.first < this.#firstBufferRowIndex) {
      for (
        let i = this.#firstBufferRowIndex - 1;
        i >= bufferRange.first &&
        deadline.timeRemaining() > MS_TO_LEAVE_PER_FILL;
        i--
      ) {
        this._addRowAtIndex(i, this.table.body.firstElementChild);

        // Update as we go in case we need to wait for the next idle.
        this.#firstBufferRowIndex = i;
      }

      // Adjust the height of the top spacer to account for the new rows we've
      // added.
      this.table.spacerTop.setHeight(
        this.#firstBufferRowIndex * this._rowElementClass.ROW_HEIGHT
      );

      // If we haven't completed the work of filling the tolerance buffer,
      // schedule a new job to do so.
      if (this.#firstBufferRowIndex != bufferRange.first) {
        this.#createToleranceFillCallback();
        return;
      }
    }

    // Fill in the end of the buffer.
    if (bufferRange.last > this.#lastBufferRowIndex) {
      for (
        let i = this.#lastBufferRowIndex + 1;
        i <= bufferRange.last &&
        deadline.timeRemaining() > MS_TO_LEAVE_PER_FILL;
        i++
      ) {
        this._addRowAtIndex(i);

        // Update as we go in case we need to wait for the next idle.
        this.#lastBufferRowIndex = i;
      }

      // Adjust the height of the bottom spacer to account for the new rows
      // we've added.
      this.table.spacerBottom.setHeight(
        (rowCount - 1 - this.#lastBufferRowIndex) *
          this._rowElementClass.ROW_HEIGHT
      );

      // If we haven't completed the work of filling the tolerance buffer,
      // schedule a new job to do so.
      if (this.#lastBufferRowIndex != bufferRange.last) {
        this.#createToleranceFillCallback();
        return;
      }
    }

    // Notify tests that we have finished work.
    this.#dispatchRowBufferReadyEvent();
  }

  /**
   * The calculated ranges which determine the shape of the row buffer at
   * various stages of processing.
   *
   * @typedef RowBufferRanges
   * @property {InclusiveRange} visibleRows - The range of rows which should be
   *   displayed to the user.
   * @property {integer?} pruneBefore - The index of the row before which any
   *   additional rows should be discarded.
   * @property {integer?} pruneAfter - The index of the row after which any
   *   additional rows should be discarded.
   * @property {InclusiveRange} finalizedRows - The range of rows which should
   *   exist in the row buffer after any additions and removals have been made.
   */

  /**
   * Calculate the values necessary for building the list of visible rows and
   * retaining any rows in the buffer which fall inside the desired tolerance
   * and form a contiguous range with the visible rows.
   *
   * WARNING: This function makes calculations based on existing DOM dimensions.
   * Do not use it after you have modified the DOM.
   *
   * @returns {RowBufferRanges}
   */
  #calculateRowBufferRanges(dataRowCount) {
    /** @type {RowBufferRanges} */
    const ranges = {
      visibleRows: {},
      pruneBefore: null,
      pruneAfter: null,
      finalizedRows: {},
    };

    // We adjust the row buffer in several stages. First, we'll use the new
    // scroll position to determine the boundaries of the buffer. Then, we'll
    // create and add any new rows which are necessary to fit the new
    // boundaries. Next, we prune rows added in previous scrolls which now fall
    // outside the boundaries. Finally, we recalculate the height of the spacers
    // which position the visible rows within the rendered area.
    ranges.visibleRows.first = Math.max(
      Math.floor(this.scrollTop / this._rowElementClass.ROW_HEIGHT),
      0
    );

    const lastPossibleVisibleRow = Math.ceil(
      (this.scrollTop + this.#calculateVisibleHeight()) /
        this._rowElementClass.ROW_HEIGHT
    );

    ranges.visibleRows.last =
      Math.min(lastPossibleVisibleRow, dataRowCount) - 1;

    // Determine the number of rows desired in the tolerance buffer in order to
    // determine whether there are any that we can save.
    const desiredRowRange = this.#calculateDesiredBufferRange(
      ranges.visibleRows.first,
      ranges.visibleRows.last,
      dataRowCount
    );

    // Determine which rows are no longer wanted in the buffer. If we've
    // scrolled past the previous visible rows, it's possible that the tolerance
    // buffer will still contain some rows we'd like to have in the buffer. Note
    // that we insist on a contiguous range of rows in the buffer to simplify
    // determining which rows exist and appropriately spacing the viewport.
    if (this.#lastBufferRowIndex < ranges.visibleRows.first) {
      // There is a discontiguity between the visible rows and anything that's
      // in the buffer. Prune everything before the visible rows.
      ranges.pruneBefore = ranges.visibleRows.first;
      ranges.finalizedRows.first = ranges.visibleRows.first;
    } else if (this.#firstBufferRowIndex < desiredRowRange.first) {
      // The range of rows in the buffer overlaps the start of the visible rows,
      // but there are rows outside of the desired buffer as well. Prune them.
      ranges.pruneBefore = desiredRowRange.first;
      ranges.finalizedRows.first = desiredRowRange.first;
    } else {
      // Determine the beginning of the finalized buffer based on whether the
      // buffer contains rows before the start of the visible rows.
      ranges.finalizedRows.first = Math.min(
        ranges.visibleRows.first,
        this.#firstBufferRowIndex
      );
    }

    if (this.#firstBufferRowIndex > ranges.visibleRows.last) {
      // There is a discontiguity between the visible rows and anything that's
      // in the buffer. Prune everything after the visible rows.
      ranges.pruneAfter = ranges.visibleRows.last;
      ranges.finalizedRows.last = ranges.visibleRows.last;
    } else if (this.#lastBufferRowIndex > desiredRowRange.last) {
      // The range of rows in the buffer overlaps the end of the visible rows,
      // but there are rows outside of the desired buffer as well. Prune them.
      ranges.pruneAfter = desiredRowRange.last;
      ranges.finalizedRows.last = desiredRowRange.last;
    } else {
      // Determine the end of the finalized buffer based on whether the buffer
      // contains rows after the end of the visible rows.
      ranges.finalizedRows.last = Math.max(
        ranges.visibleRows.last,
        this.#lastBufferRowIndex
      );
    }

    return ranges;
  }

  /**
   * Display the table rows which should be shown in the visible area and
   * request filling of the tolerance buffer when idle.
   */
  _ensureVisibleRowsAreDisplayed() {
    this.#cancelToleranceFillCallback();

    let rowCount = this._view?.rowCount ?? 0;
    this.placeholder?.classList.toggle("show", !rowCount);

    if (!rowCount || this.#calculateVisibleRowCount() == 0) {
      return;
    }

    if (this.scrollTop > rowCount * this._rowElementClass.ROW_HEIGHT) {
      // Beyond the end of the list. We're about to scroll anyway, so clear
      // everything out and wait for it to happen. Don't call `invalidate` here,
      // or you'll end up in an infinite loop.
      this.table.spacerTop.setHeight(0);
      this.#resetRowBuffer();
      return;
    }

    const ranges = this.#calculateRowBufferRanges(rowCount);

    // *WARNING: Do not request any DOM dimensions after this point. Modifying
    // the DOM will invalidate existing calculations and any additional requests
    // will cause synchronous reflow.

    // Add a row if the table is empty. Either we're initializing or have
    // invalidated the tree, and the next two steps pass over row zero if there
    // are no rows already in the buffer.
    if (
      this.#lastBufferRowIndex == 0 &&
      this.table.body.childElementCount == 0 &&
      ranges.visibleRows.first == 0
    ) {
      this._addRowAtIndex(0);
    }

    // Expand the row buffer to include newly-visible rows which weren't already
    // visible or preloaded in the tolerance buffer.

    const earliestMissingEndRowIdx = Math.max(
      this.#lastBufferRowIndex + 1,
      ranges.visibleRows.first
    );
    for (let i = earliestMissingEndRowIdx; i <= ranges.visibleRows.last; i++) {
      // We are missing rows at the end of the buffer. Either the last row of
      // the existing buffer lies within the range of visible rows and we begin
      // there, or the entire range of visible rows occurs after the end of the
      // buffer and we fill in from the start.
      this._addRowAtIndex(i);
    }

    const latestMissingStartRowIdx = Math.min(
      this.#firstBufferRowIndex - 1,
      ranges.visibleRows.last
    );
    for (let i = latestMissingStartRowIdx; i >= ranges.visibleRows.first; i--) {
      // We are missing rows at the start of the buffer. We'll add them working
      // backwards so that we can prepend. Either the first row of the existing
      // buffer lies within the range of visible rows and we begin there, or the
      // entire range of visible rows occurs before the end of the buffer and we
      // fill in from the end.
      this._addRowAtIndex(i, this.table.body.firstElementChild);
    }

    // Prune the buffer of any rows outside of our desired buffer range.
    if (ranges.pruneBefore !== null) {
      const pruneBeforeRow = this.getRowAtIndex(ranges.pruneBefore);
      let rowToPrune = pruneBeforeRow.previousElementSibling;
      while (rowToPrune) {
        this._removeRowAtIndex(rowToPrune.index);
        rowToPrune = pruneBeforeRow.previousElementSibling;
      }
    }

    if (ranges.pruneAfter !== null) {
      const pruneAfterRow = this.getRowAtIndex(ranges.pruneAfter);
      let rowToPrune = pruneAfterRow.nextElementSibling;
      while (rowToPrune) {
        this._removeRowAtIndex(rowToPrune.index);
        rowToPrune = pruneAfterRow.nextElementSibling;
      }
    }

    // Set the indices of the new first and last rows in the DOM. They may come
    // from the tolerance buffer if we haven't exhausted it.
    this.#firstBufferRowIndex = ranges.finalizedRows.first;
    this.#lastBufferRowIndex = ranges.finalizedRows.last;

    this.#firstVisibleRowIndex = ranges.visibleRows.first;
    this.#lastVisibleRowIndex = ranges.visibleRows.last;

    // Adjust the height of the spacers to ensure that visible rows fall within
    // the visible space and the overall scroll height is correct.
    this.table.spacerTop.setHeight(
      this.#firstBufferRowIndex * this._rowElementClass.ROW_HEIGHT
    );

    this.table.spacerBottom.setHeight(
      (rowCount - this.#lastBufferRowIndex - 1) *
        this._rowElementClass.ROW_HEIGHT
    );

    // The row buffer ideally contains some tolerance on either end to avoid
    // creating rows and fetching data for them during short scrolls. However,
    // actually creating those rows can be expensive, and during a long scroll
    // we may throw them away very quickly. To save the expense, only fill the
    // buffer while idle.

    this.#createToleranceFillCallback();
  }

  /**
   * Index of the first visible or partly visible row.
   *
   * @returns {integer}
   */
  getFirstVisibleIndex() {
    return this.#firstVisibleRowIndex;
  }

  /**
   * Index of the last visible or partly visible row.
   *
   * @returns {integer}
   */
  getLastVisibleIndex() {
    return this.#lastVisibleRowIndex;
  }

  /**
   * Ensures that the row at `index` is on the screen.
   *
   * @param {integer} index
   */
  scrollToIndex(index, instant = false) {
    const rowCount = this._view.rowCount;
    if (rowCount == 0) {
      // If there are no rows, make sure we're scrolled to the top.
      this.scrollTo({ top: 0, behavior: "instant" });
      return;
    }
    if (index < 0 || index >= rowCount) {
      // Bad index. Report, and do nothing.
      console.error(
        `<${this.localName} id="${this.id}"> tried to scroll to a row that doesn't exist: ${index}`
      );
      return;
    }

    const topOfRow = this._rowElementClass.ROW_HEIGHT * index;
    let scrollTop = this.scrollTop;
    const visibleHeight = this.#calculateVisibleHeight();
    const behavior = instant ? "instant" : "auto";

    // Scroll up to the row.
    if (topOfRow < scrollTop) {
      this.scrollTo({ top: topOfRow, behavior });
      return;
    }

    // Scroll down to the row.
    const bottomOfRow = topOfRow + this._rowElementClass.ROW_HEIGHT;
    if (bottomOfRow > scrollTop + visibleHeight) {
      this.scrollTo({ top: bottomOfRow - visibleHeight, behavior });
      return;
    }

    // Call `scrollTo` even if the row is in view, to stop any earlier smooth
    // scrolling that might be happening.
    this.scrollTo({ top: this.scrollTop, behavior });
  }

  /**
   * Updates the list to reflect added or removed rows.
   *
   * @param {integer} index - The position in the existing list where rows were
   *   added or removed.
   * @param {integer} delta - The change in number of rows; positive if rows
   *   were added and negative if rows were removed.
   */
  rowCountChanged(index, delta) {
    if (!this._selection) {
      return;
    }

    this._selection.adjustSelection(index, delta);
    this._updateCurrentIndexClasses();
    this.dispatchEvent(new CustomEvent("rowcountchange"));
  }

  /**
   * Clamps `index` to a value between 0 and `rowCount - 1`.
   *
   * @param {integer} index
   * @returns {integer}
   */
  _clampIndex(index) {
    if (!this._view.rowCount) {
      return null;
    }
    if (index < 0) {
      return 0;
    }
    if (index >= this._view.rowCount) {
      return this._view.rowCount - 1;
    }
    return index;
  }

  /**
   * Creates a new row element and adds it to the DOM.
   *
   * @param {integer} index
   */
  _addRowAtIndex(index, before = null) {
    let row = document.createElement("tr", { is: this._rowElementName });
    row.setAttribute("is", this._rowElementName);
    this.table.body.insertBefore(row, before);
    row.setAttribute("aria-setsize", this._view.rowCount);
    row.style.height = `${this._rowElementClass.ROW_HEIGHT}px`;
    row.index = index;
    if (this._selection?.isSelected(index)) {
      row.selected = true;
    }
    if (this.currentIndex === index) {
      row.classList.add("current");
      this.table.body.setAttribute("aria-activedescendant", row.id);
    }
    this._rows.set(index, row);
  }

  /**
   * Removes the row element at `index` from the DOM and map of rows.
   *
   * @param {integer} index
   */
  _removeRowAtIndex(index) {
    const row = this._rows.get(index);
    row?.remove();
    this._rows.delete(index);
  }

  /**
   * Returns the row element at `index` or null if `index` is out of range.
   *
   * @param {integer} index
   * @returns {HTMLTableRowElement}
   */
  getRowAtIndex(index) {
    return this._rows.get(index) ?? null;
  }

  /**
   * Collapses the row at `index` if it can be collapsed. If the selected
   * row is a descendant of the collapsing row, selection is moved to the
   * collapsing row.
   *
   * @param {integer} index
   */
  collapseRowAtIndex(index) {
    if (!this._view.isContainerOpen(index)) {
      return;
    }

    // If the selected row is going to be collapsed, move the selection.
    // Even if the row to be collapsed is already selected, set
    // selectIndex to ensure currentIndex also points to the correct row.
    let selectedIndex = this.selectedIndex;
    while (selectedIndex >= index) {
      if (selectedIndex == index) {
        this.selectedIndex = index;
        break;
      }
      selectedIndex = this._view.getParentIndex(selectedIndex);
    }

    // Check if the view calls rowCountChanged. If it didn't, we'll have to
    // call it. This can happen if the view has no reference to the tree.
    let rowCountDidChange = false;
    let rowCountChangeListener = () => {
      rowCountDidChange = true;
    };

    let countBefore = this._view.rowCount;
    this.addEventListener("rowcountchange", rowCountChangeListener);
    this._view.toggleOpenState(index);
    this.removeEventListener("rowcountchange", rowCountChangeListener);
    let countAdded = this._view.rowCount - countBefore;

    // Call rowCountChanged, if it hasn't already happened.
    if (countAdded && !rowCountDidChange) {
      this.invalidateRow(index);
      this.rowCountChanged(index + 1, countAdded);
    }

    this.dispatchEvent(
      new CustomEvent("collapsed", { bubbles: true, detail: index })
    );
  }

  /**
   * Expands the row at `index` if it can be expanded.
   *
   * @param {integer} index
   * @returns {integer} - the number of rows that were added
   */
  expandRowAtIndex(index) {
    if (!this._view.isContainer(index) || this._view.isContainerOpen(index)) {
      return 0;
    }

    // Check if the view calls rowCountChanged. If it didn't, we'll have to
    // call it. This can happen if the view has no reference to the tree.
    let rowCountDidChange = false;
    let rowCountChangeListener = () => {
      rowCountDidChange = true;
    };

    let countBefore = this._view.rowCount;
    this.addEventListener("rowcountchange", rowCountChangeListener);
    this._view.toggleOpenState(index);
    this.removeEventListener("rowcountchange", rowCountChangeListener);
    let countAdded = this._view.rowCount - countBefore;

    // Call rowCountChanged, if it hasn't already happened.
    if (countAdded && !rowCountDidChange) {
      this.invalidateRow(index);
      this.rowCountChanged(index + 1, countAdded);
    }

    this.dispatchEvent(
      new CustomEvent("expanded", { bubbles: true, detail: index })
    );

    return countAdded;
  }

  /**
   * In a selection, index of the most-recently-selected row.
   *
   * @type {integer}
   */
  get currentIndex() {
    return this._selection ? this._selection.currentIndex : -1;
  }

  set currentIndex(index) {
    if (!this._view) {
      return;
    }

    this._selection.currentIndex = index;
    this._updateCurrentIndexClasses();
    if (index >= 0 && index < this._view.rowCount) {
      this.scrollToIndex(index);
    }
  }

  /**
   * Set the "current" class on the right row, and remove it from all other rows.
   */
  _updateCurrentIndexClasses() {
    let index = this.currentIndex;

    for (let row of this.querySelectorAll(
      `tr[is="${this._rowElementName}"].current`
    )) {
      row.classList.remove("current");
    }

    if (!this._view || index < 0 || index > this._view.rowCount - 1) {
      this.table.body.removeAttribute("aria-activedescendant");
      return;
    }

    let row = this.getRowAtIndex(index);
    if (row) {
      // We need to clear the attribute in order to let screen readers know that
      // a new message has been selected even if the ID is identical. For
      // example when we delete the first message with ID 0, the next message
      // becomes ID 0 itself. Therefore the attribute wouldn't trigger the screen
      // reader to announce the new message without being cleared first.
      this.table.body.removeAttribute("aria-activedescendant");
      row.classList.add("current");
      this.table.body.setAttribute("aria-activedescendant", row.id);
    }
  }

  /**
   * Select and focus the given index.
   *
   * @param {integer} index - The index to select.
   * @param {boolean} [delaySelect=false] - If the selection should be delayed.
   */
  _selectSingle(index, delaySelect = false) {
    let changeSelection =
      this._selection.count != 1 || !this._selection.isSelected(index);
    // Update the TreeSelection selection to trigger a tree reset().
    if (changeSelection) {
      this._selection.select(index);
    }
    this.currentIndex = index;
    if (changeSelection) {
      this.onSelectionChanged(delaySelect);
    }
  }

  /**
   * Start or extend a range selection to the given index and focus it.
   *
   * @param {number} start - Start index of selection. -1 for current index.
   * @param {number} end - End index of selection.
   * @param {boolean} extend[false] - If the new selection range should extend
   *   the current selection.
   */
  _selectRange(start, end, extend = false) {
    this._selection.rangedSelect(start, end, extend);
    this.currentIndex = start == -1 ? end : start;
    this.onSelectionChanged();
  }

  /**
   * Toggle the selection state at the given index and focus it.
   *
   * @param {integer} index - The index to toggle.
   */
  _toggleSelected(index) {
    this._selection.toggleSelect(index);
    // We hack the internals of the TreeSelection to clear the
    // shiftSelectPivot.
    this._selection._shiftSelectPivot = null;
    this.currentIndex = index;
    this.onSelectionChanged();
  }

  /**
   * Select all rows.
   */
  selectAll() {
    this._selection.selectAll();
    this.onSelectionChanged();
  }

  /**
   * Toggle between selecting all rows or none, depending on the current
   * selection state.
   */
  toggleSelectAll() {
    if (!this.selectedIndices.length) {
      const index = this._view.rowCount - 1;
      this._selection.selectAll();
      this.currentIndex = index;
    } else {
      this._selection.clearSelection();
    }
    // Make sure the body is focused when the selection is changed as
    // clicking on the "select all" header button steals the focus.
    this.focus();

    this.onSelectionChanged();
  }

  /**
   * In a selection, index of the most-recently-selected row.
   *
   * @type {integer}
   */
  get selectedIndex() {
    if (!this._selection?.count) {
      return -1;
    }

    let min = {};
    this._selection.getRangeAt(0, min, {});
    return min.value;
  }

  set selectedIndex(index) {
    this._selectSingle(index);
  }

  /**
   * An array of the indices of all selected rows.
   *
   * @type {integer[]}
   */
  get selectedIndices() {
    let indices = [];
    let rangeCount = this._selection.getRangeCount();

    for (let range = 0; range < rangeCount; range++) {
      let min = {};
      let max = {};
      this._selection.getRangeAt(range, min, max);

      if (min.value == -1) {
        continue;
      }

      for (let index = min.value; index <= max.value; index++) {
        indices.push(index);
      }
    }

    return indices;
  }

  set selectedIndices(indices) {
    this.setSelectedIndices(indices);
  }

  /**
   * An array of the indices of all selected rows.
   *
   * @param {integer[]} indices
   * @param {boolean} suppressEvent - Prevent a "select" event firing.
   */
  setSelectedIndices(indices, suppressEvent) {
    this._selection.clearSelection();
    for (let index of indices) {
      this._selection.toggleSelect(index);
    }
    this.onSelectionChanged(false, suppressEvent);
  }

  /**
   * Changes the selection state of the row at `index`.
   *
   * @param {integer} index
   * @param {boolean?} selected - if set, set the selection state to this
   *   value, otherwise toggle the current state
   * @param {boolean?} suppressEvent - prevent a "select" event firing
   * @returns {boolean} - if the index is now selected
   */
  toggleSelectionAtIndex(index, selected, suppressEvent) {
    let wasSelected = this._selection.isSelected(index);
    if (selected === undefined) {
      selected = !wasSelected;
    }

    if (selected != wasSelected) {
      this._selection.toggleSelect(index);
      this.onSelectionChanged(false, suppressEvent);
    }

    return selected;
  }

  /**
   * Loop through all available child elements of the placeholder slot and
   * show those that are needed.
   * @param {array} idsToShow - Array of ids to show.
   */
  updatePlaceholders(idsToShow) {
    for (let element of this.placeholder.children) {
      element.hidden = !idsToShow.includes(element.id);
    }
  }

  /**
   * Update the classes on the table element to reflect the current selection
   * state, and dispatch an event to allow implementations to handle the
   * change in the selection state.
   *
   * @param {boolean} [delaySelect=false] - If the selection should be delayed.
   * @param {boolean} [suppressEvent=false] - Prevent a "select" event firing.
   */
  onSelectionChanged(delaySelect = false, suppressEvent = false) {
    const selectedCount = this._selection.count;
    const allSelected = selectedCount == this._view.rowCount;

    this.table.classList.toggle("all-selected", allSelected);
    this.table.classList.toggle("some-selected", !allSelected && selectedCount);
    this.table.classList.toggle("multi-selected", selectedCount > 1);

    const selectButton = this.table.querySelector(".tree-view-header-select");
    // Some implementations might not use a select header.
    if (selectButton) {
      // Only mark the `select` button as "checked" if all rows are selected.
      selectButton.toggleAttribute("aria-checked", allSelected);
      // The default action for the header button is to deselect all messages
      // if even one message is currently selected.
      document.l10n.setAttributes(
        selectButton,
        selectedCount
          ? "threadpane-column-header-deselect-all"
          : "threadpane-column-header-select-all"
      );
    }

    if (suppressEvent) {
      return;
    }

    // No need to handle a delayed select if not required.
    if (!delaySelect) {
      // Clear the timeout in case something was still running.
      if (this._selectTimeout) {
        window.clearTimeout(this._selectTimeout);
      }
      this.dispatchEvent(new CustomEvent("select", { bubbles: true }));
      return;
    }

    let delay = this.dataset.selectDelay || 50;
    if (delay != -1) {
      if (this._selectTimeout) {
        window.clearTimeout(this._selectTimeout);
      }
      this._selectTimeout = window.setTimeout(() => {
        this.dispatchEvent(new CustomEvent("select", { bubbles: true }));
        this._selectTimeout = null;
      }, delay);
    }
  }
}
customElements.define("tree-view", TreeView);

/**
 * The main <table> element containing the thead and the TreeViewTableBody
 * tbody. This class is used to expose all those methods and custom events
 * needed at the implementation level.
 */
class TreeViewTable extends HTMLTableElement {
  /**
   * The array of objects containing the data to generate the needed columns.
   * Keep this public so child elements can access it if needed.
   * @type {Array}
   */
  columns;

  /**
   * The header row for the table.
   *
   * @type {TreeViewTableHeader}
   */
  header;

  /**
   * Array containing the IDs of templates holding menu items to dynamically add
   * to the menupopup of the column picker.
   * @type {Array}
   */
  popupMenuTemplates = [];

  connectedCallback() {
    if (this.hasConnected) {
      return;
    }
    this.hasConnected = true;

    this.setAttribute("is", "tree-view-table");
    this.classList.add("tree-table");

    // Use a fragment to append child elements to later add them all at once
    // to the DOM. Performance is important.
    const fragment = new DocumentFragment();

    this.header = document.createElement("thead", {
      is: "tree-view-table-header",
    });
    fragment.append(this.header);

    this.spacerTop = document.createElement("tbody", {
      is: "tree-view-table-spacer",
    });
    fragment.append(this.spacerTop);

    this.body = document.createElement("tbody", {
      is: "tree-view-table-body",
    });
    fragment.append(this.body);

    this.spacerBottom = document.createElement("tbody", {
      is: "tree-view-table-spacer",
    });
    fragment.append(this.spacerBottom);

    this.append(fragment);
  }

  /**
   * If set to TRUE before generating the columns, the table will
   * automatically create a column picker in the table header.
   *
   * @type {boolean}
   */
  set editable(val) {
    this.dataset.editable = val;
  }

  get editable() {
    return this.dataset.editable === "true";
  }

  /**
   * Set the id attribute of the TreeViewTableBody for selection and styling
   * purpose.
   *
   * @param {string} id - The string ID to set.
   */
  setBodyID(id) {
    this.body.id = id;
  }

  setPopupMenuTemplates(array) {
    this.popupMenuTemplates = array;
  }

  /**
   * Set the columns array of the table. This should only be used during
   * initialization and any following change to the columns visibility should
   * be handled via the updateColumns() method.
   *
   * @param {Array} columns - The array of columns to generate.
   */
  setColumns(columns) {
    this.columns = columns;
    this.header.setColumns();
    this.#updateView();
  }

  /**
   * Update the currently visible columns.
   *
   * @param {Array} columns - The array of columns to update. It should match
   * the original array set via the setColumn() method since this method will
   * only update the column visibility without generating new elements.
   */
  updateColumns(columns) {
    this.columns = columns;
    this.#updateView();
  }

  /**
   * Store the newly resized column values in the xul store.
   *
   * @param {string} url - The document URL used to store the values.
   * @param {DOMEvent} event - The dom event bubbling from the resized action.
   */
  setColumnsWidths(url, event) {
    const width = event.detail.splitter.width;
    const column = event.detail.column;
    const newValue = `${column}:${width}`;
    let newWidths;

    // Check if we already have stored values and update it if so.
    let columnsWidths = Services.xulStore.getValue(url, "columns", "widths");
    if (columnsWidths) {
      let updated = false;
      columnsWidths = columnsWidths.split(",");
      for (let index = 0; index < columnsWidths.length; index++) {
        const cw = columnsWidths[index].split(":");
        if (cw[0] == column) {
          cw[1] = width;
          updated = true;
          columnsWidths[index] = newValue;
          break;
        }
      }
      // Push the new value into the array if we didn't have an existing one.
      if (!updated) {
        columnsWidths.push(newValue);
      }
      newWidths = columnsWidths.join(",");
    } else {
      newWidths = newValue;
    }

    // Store the values as a plain string with the current format:
    //   columnID:width,columnID:width,...
    Services.xulStore.setValue(url, "columns", "widths", newWidths);
  }

  /**
   * Restore the previously saved widths of the various columns if we have
   * any.
   *
   * @param {string} url - The document URL used to store the values.
   */
  restoreColumnsWidths(url) {
    let columnsWidths = Services.xulStore.getValue(url, "columns", "widths");
    if (!columnsWidths) {
      return;
    }

    for (let column of columnsWidths.split(",")) {
      column = column.split(":");
      this.querySelector(`#${column[0]}`)?.style.setProperty(
        `--${column[0]}Splitter-width`,
        `${column[1]}px`
      );
    }
  }

  /**
   * Update the visibility of the currently available columns.
   */
  #updateView() {
    let lastResizableColumn = this.columns.findLast(
      c => !c.hidden && (c.resizable ?? true)
    );

    for (let column of this.columns) {
      document.getElementById(column.id).hidden = column.hidden;

      // No need to update the splitter visibility if the column is
      // specifically not resizable.
      if (column.resizable === false) {
        continue;
      }

      document.getElementById(column.id).resizable =
        column != lastResizableColumn;
    }
  }
}
customElements.define("tree-view-table", TreeViewTable, { extends: "table" });

/**
 * Class used to generate the thead of the TreeViewTable. This class will take
 * care of handling columns sizing and sorting order, with bubbling events to
 * allow listening for those changes on the implementation level.
 */
class TreeViewTableHeader extends HTMLTableSectionElement {
  /**
   * An array of all table header cells that can be reordered.
   *
   * @returns {HTMLTableCellElement[]}
   */
  get #orderableChildren() {
    return [...this.querySelectorAll("th[draggable]:not([hidden])")];
  }

  /**
   * Used to simulate a change in the order. The element remains in the same
   * DOM position.
   *
   * @param {HTMLTableRowElement} element - The row to animate.
   * @param {number} to - The new Y position of the element after animation.
   */
  static _transitionTranslation(element, to) {
    if (!reducedMotionMedia.matches) {
      element.style.transition = `transform ${ANIMATION_DURATION_MS}ms ease`;
    }
    element.style.transform = to ? `translateX(${to}px)` : null;
  }

  connectedCallback() {
    if (this.hasConnected) {
      return;
    }
    this.hasConnected = true;

    this.setAttribute("is", "tree-view-table-header");
    this.classList.add("tree-table-header");
    this.row = document.createElement("tr");
    this.appendChild(this.row);

    this.addEventListener("keypress", this);
    this.addEventListener("dragstart", this);
    this.addEventListener("dragover", this);
    this.addEventListener("dragend", this);
    this.addEventListener("drop", this);
  }

  handleEvent(event) {
    switch (event.type) {
      case "keypress":
        this.#onKeyPress(event);
        break;
      case "dragstart":
        this.#onDragStart(event);
        break;
      case "dragover":
        this.#onDragOver(event);
        break;
      case "dragend":
        this.#onDragEnd();
        break;
      case "drop":
        this.#onDrop(event);
        break;
    }
  }

  #onKeyPress(event) {
    if (!event.altKey || !["ArrowRight", "ArrowLeft"].includes(event.key)) {
      this.triggerTableHeaderRovingTab(event);
      return;
    }

    let column = event.target.closest(`th[is="tree-view-table-header-cell"]`);
    if (!column) {
      return;
    }

    let visibleColumns = this.parentNode.columns.filter(c => !c.hidden);
    let forward =
      event.key == (document.dir === "rtl" ? "ArrowLeft" : "ArrowRight");

    // Bail out if the user is trying to shift backward the first column, or
    // shift forward the last column.
    if (
      (!forward && visibleColumns.at(0)?.id == column.id) ||
      (forward && visibleColumns.at(-1)?.id == column.id)
    ) {
      return;
    }

    event.preventDefault();
    this.dispatchEvent(
      new CustomEvent("shift-column", {
        bubbles: true,
        detail: {
          column: column.id,
          forward,
        },
      })
    );
  }

  #onDragStart(event) {
    if (!event.target.closest("th[draggable]")) {
      // This shouldn't be necessary, but is?!
      event.preventDefault();
      return;
    }

    const orderable = this.#orderableChildren;
    if (orderable.length < 2) {
      return;
    }

    const headerCell = orderable.find(th => th.contains(event.target));
    const rect = headerCell.getBoundingClientRect();

    this._dragInfo = {
      cell: headerCell,
      // How far can we move `headerCell` horizontally.
      min: orderable.at(0).getBoundingClientRect().left - rect.left,
      max: orderable.at(-1).getBoundingClientRect().right - rect.right,
      // Where is the drag event starting.
      startX: event.clientX,
      offsetX: event.clientX - rect.left,
    };

    headerCell.classList.add("column-dragging");
    // Prevent `headerCell` being used as the drag image. We don't
    // really want any drag image, but there's no way to not have one.
    event.dataTransfer.setDragImage(document.createElement("img"), 0, 0);
  }

  #onDragOver(event) {
    if (!this._dragInfo) {
      return;
    }

    const { cell, min, max, startX, offsetX } = this._dragInfo;
    // Move `cell` with the mouse pointer.
    let dragX = Math.min(max, Math.max(min, event.clientX - startX));
    cell.style.transform = `translateX(${dragX}px)`;

    let thisRect = this.getBoundingClientRect();

    // How much space is there before the `cell`? We'll see how many cells fit
    // in the space and put the `cell` in after them.
    let spaceBefore = Math.max(
      0,
      event.clientX + this.scrollLeft - offsetX - thisRect.left
    );
    // The width of all cells seen in the loop so far.
    let totalWidth = 0;
    // If we've looped past the cell being dragged.
    let afterDraggedTh = false;
    // The cell before where a drop would take place. If null, drop would
    // happen at the start of the table header.
    let header = null;

    for (let headerCell of this.#orderableChildren) {
      if (headerCell == cell) {
        afterDraggedTh = true;
        continue;
      }

      let rect = headerCell.getBoundingClientRect();
      let enoughSpace = spaceBefore > totalWidth + rect.width / 2;

      let multiplier = 0;
      if (enoughSpace) {
        if (afterDraggedTh) {
          multiplier = -1;
        }
        header = headerCell;
      } else if (!afterDraggedTh) {
        multiplier = 1;
      }
      TreeViewTableHeader._transitionTranslation(
        headerCell,
        multiplier * cell.clientWidth
      );

      totalWidth += rect.width;
    }

    this._dragInfo.dropTarget = header;

    event.preventDefault();
  }

  #onDragEnd() {
    if (!this._dragInfo) {
      return;
    }

    this._dragInfo.cell.classList.remove("column-dragging");
    delete this._dragInfo;

    for (let headerCell of this.#orderableChildren) {
      headerCell.style.transform = null;
      headerCell.style.transition = null;
    }
  }

  #onDrop(event) {
    if (!this._dragInfo) {
      return;
    }

    let { cell, startX, dropTarget } = this._dragInfo;

    let newColumns = this.parentNode.columns.map(column => ({ ...column }));

    const draggedColumn = newColumns.find(c => c.id == cell.id);
    const initialPosition = newColumns.indexOf(draggedColumn);

    let targetCell;
    let newPosition;
    if (!dropTarget) {
      // Get the first visible cell.
      targetCell = this.querySelector("th:not([hidden])");
      newPosition = newColumns.indexOf(
        newColumns.find(c => c.id == targetCell.id)
      );
    } else {
      // Get the next non hidden sibling.
      targetCell = dropTarget.nextElementSibling;
      while (targetCell.hidden) {
        targetCell = targetCell.nextElementSibling;
      }
      newPosition = newColumns.indexOf(
        newColumns.find(c => c.id == targetCell.id)
      );
    }

    // Reduce the new position index if we're moving forward in order to get the
    // accurate index position of the column we're taking the position of.
    if (event.clientX > startX) {
      newPosition -= 1;
    }

    newColumns.splice(newPosition, 0, newColumns.splice(initialPosition, 1)[0]);

    // Update the ordinal of the columns to reflect the new positions.
    newColumns.forEach((column, index) => {
      column.ordinal = index;
    });

    this.querySelector("tr").insertBefore(cell, targetCell);

    this.dispatchEvent(
      new CustomEvent("reorder-columns", {
        bubbles: true,
        detail: {
          columns: newColumns,
        },
      })
    );
    event.preventDefault();
  }

  /**
   * Create all the table header cells based on the currently set columns.
   */
  setColumns() {
    this.row.replaceChildren();

    for (let column of this.parentNode.columns) {
      /** @type {TreeViewTableHeaderCell} */
      let cell = document.createElement("th", {
        is: "tree-view-table-header-cell",
      });
      this.row.appendChild(cell);
      cell.setColumn(column);
    }

    // Create a column picker if the table is editable.
    if (this.parentNode.editable) {
      const picker = document.createElement("th", {
        is: "tree-view-table-column-picker",
      });
      this.row.appendChild(picker);
    }

    this.updateRovingTab();
  }

  /**
   * Get all currently visible columns of the table header.
   *
   * @returns {Array} An array of buttons.
   */
  get headerColumns() {
    return this.row.querySelectorAll(`th:not([hidden]) button`);
  }

  /**
   * Update the `tabindex` attribute of the currently visible columns.
   */
  updateRovingTab() {
    for (let button of this.headerColumns) {
      button.tabIndex = -1;
    }
    // Allow focus on the first available button.
    this.headerColumns[0].tabIndex = 0;
  }

  /**
   * Handles the keypress event on the table header.
   *
   * @param {Event} event - The keypress DOMEvent.
   */
  triggerTableHeaderRovingTab(event) {
    if (!["ArrowRight", "ArrowLeft"].includes(event.key)) {
      return;
    }

    const headerColumns = [...this.headerColumns];
    let focusableButton = headerColumns.find(b => b.tabIndex != -1);
    let elementIndex = headerColumns.indexOf(focusableButton);

    // Find the adjacent focusable element based on the pressed key.
    let isRTL = document.dir == "rtl";
    if (
      (isRTL && event.key == "ArrowLeft") ||
      (!isRTL && event.key == "ArrowRight")
    ) {
      elementIndex++;
      if (elementIndex > headerColumns.length - 1) {
        elementIndex = 0;
      }
    } else if (
      (!isRTL && event.key == "ArrowLeft") ||
      (isRTL && event.key == "ArrowRight")
    ) {
      elementIndex--;
      if (elementIndex == -1) {
        elementIndex = headerColumns.length - 1;
      }
    }

    // Move the focus to a new column and update the tabindex attribute.
    let newFocusableButton = headerColumns[elementIndex];
    if (newFocusableButton) {
      focusableButton.tabIndex = -1;
      newFocusableButton.tabIndex = 0;
      newFocusableButton.focus();
    }
  }
}
customElements.define("tree-view-table-header", TreeViewTableHeader, {
  extends: "thead",
});

/**
 * Class to generated the TH elements for the TreeViewTableHeader.
 */
class TreeViewTableHeaderCell extends HTMLTableCellElement {
  /**
   * The div needed to handle the header button in an absolute position.
   * @type {HTMLElement}
   */
  #container;

  /**
   * The clickable button to change the sorting of the table.
   * @type {HTMLButtonElement}
   */
  #button;

  /**
   * If this cell is resizable.
   * @type {boolean}
   */
  #resizable = true;

  /**
   * If this cell can be clicked to affect the sorting order of the tree.
   * @type {boolean}
   */
  #sortable = true;

  connectedCallback() {
    if (this.hasConnected) {
      return;
    }
    this.hasConnected = true;

    this.setAttribute("is", "tree-view-table-header-cell");
    this.draggable = true;

    this.#container = document.createElement("div");
    this.#container.classList.add(
      "tree-table-cell",
      "tree-table-cell-container"
    );

    this.#button = document.createElement("button");
    this.#container.appendChild(this.#button);
    this.appendChild(this.#container);
  }

  /**
   * Set the proper data to the newly generated table header cell and create
   * the needed child elements.
   *
   * @param {object} column - The column object with all the data to generate
   *   the correct header cell.
   */
  setColumn(column) {
    // Set a public ID so parent elements can loop through the available
    // columns after they're created.
    this.id = column.id;
    this.#button.id = `${column.id}Button`;

    // Add custom classes if needed.
    if (column.classes) {
      this.#button.classList.add(...column.classes);
    }

    if (column.l10n?.header) {
      document.l10n.setAttributes(this.#button, column.l10n.header);
    }

    // Add an image if this is a table header that needs to display an icon,
    // and set the column as icon.
    if (column.icon) {
      this.dataset.type = "icon";
      const img = document.createElement("img");
      img.src = "";
      img.alt = "";
      this.#button.appendChild(img);
    }

    this.resizable = column.resizable ?? true;

    this.hidden = column.hidden;

    this.#sortable = column.sortable ?? true;
    // Make the button clickable if the column can trigger a sorting of rows.
    if (this.#sortable) {
      this.#button.addEventListener("click", () => {
        this.dispatchEvent(
          new CustomEvent("sort-changed", {
            bubbles: true,
            detail: {
              column: column.id,
            },
          })
        );
      });
    }

    this.#button.addEventListener("contextmenu", event => {
      event.stopPropagation();
      const table = this.closest("table");
      if (table.editable) {
        table
          .querySelector("#columnPickerMenuPopup")
          .openPopup(event.target, { triggerEvent: event });
      }
    });

    // This is the column handling the thread toggling.
    if (column.thread) {
      this.#button.classList.add("tree-view-header-thread");
      this.#button.addEventListener("click", () => {
        this.dispatchEvent(
          new CustomEvent("thread-changed", {
            bubbles: true,
          })
        );
      });
    }

    // This is the column handling bulk selection.
    if (column.select) {
      this.#button.classList.add("tree-view-header-select");
      this.#button.addEventListener("click", () => {
        this.closest("tree-view").toggleSelectAll();
      });
    }

    // This is the column handling delete actions.
    if (column.delete) {
      this.#button.classList.add("tree-view-header-delete");
    }
  }

  /**
   * Set this table header as responsible for the sorting of rows.
   *
   * @param {string["ascending"|"descending"]} direction - The new sorting
   *   direction.
   */
  setSorting(direction) {
    this.#button.classList.add("sorting", direction);
  }

  /**
   * If this current column can be resized.
   *
   * @type {boolean}
   */
  set resizable(val) {
    this.#resizable = val;
    this.dataset.resizable = val;

    let splitter = this.querySelector("hr");

    // Add a splitter if we don't have one already.
    if (!splitter) {
      splitter = document.createElement("hr", { is: "pane-splitter" });
      splitter.setAttribute("is", "pane-splitter");
      this.appendChild(splitter);
      splitter.resizeDirection = "horizontal";
      splitter.resizeElement = this;
      splitter.id = `${this.id}Splitter`;
      // Emit a custom event after a resize action. Methods at implementation
      // level should listen to this event if the edited column size needs to
      // be stored or used.
      splitter.addEventListener("splitter-resized", () => {
        this.dispatchEvent(
          new CustomEvent("column-resized", {
            bubbles: true,
            detail: {
              splitter,
              column: this.id,
            },
          })
        );
      });
    }

    this.style.setProperty("width", val ? `var(--${splitter.id}-width)` : null);
    // Disable the splitter if this is not a resizable column.
    splitter.isDisabled = !val;
  }

  get resizable() {
    return this.#resizable;
  }

  /**
   * If the current column can trigger a sorting of rows.
   *
   * @type {boolean}
   */
  set sortable(val) {
    this.#sortable = val;
    this.#button.disabled = !val;
  }

  get sortable() {
    return this.#sortable;
  }
}
customElements.define("tree-view-table-header-cell", TreeViewTableHeaderCell, {
  extends: "th",
});

/**
 * Class used to generate a column picker used for the TreeViewTableHeader in
 * case the visibility of the columns of a table can be changed.
 *
 * Include treeView.ftl for strings.
 */
class TreeViewTableColumnPicker extends HTMLTableCellElement {
  /**
   * The clickable button triggering the picker context menu.
   * @type {HTMLButtonElement}
   */
  #button;

  /**
   * The menupopup allowing users to show and hide columns.
   * @type {XULElement}
   */
  #context;

  connectedCallback() {
    if (this.hasConnected) {
      return;
    }
    this.hasConnected = true;

    this.setAttribute("is", "tree-view-table-column-picker");
    this.classList.add("tree-table-cell-container");

    this.#button = document.createElement("button");
    document.l10n.setAttributes(this.#button, "tree-list-view-column-picker");
    this.#button.classList.add("button-flat", "button-column-picker");
    this.appendChild(this.#button);

    const img = document.createElement("img");
    img.src = "";
    img.alt = "";
    this.#button.appendChild(img);

    this.#context = document.createXULElement("menupopup");
    this.#context.id = "columnPickerMenuPopup";
    this.#context.setAttribute("position", "bottomleft topleft");
    this.appendChild(this.#context);
    this.#context.addEventListener("popupshowing", event => {
      // Bail out if we're opening a submenu.
      if (event.target.id != this.#context.id) {
        return;
      }

      if (!this.#context.hasChildNodes()) {
        this.#initPopup();
      }

      let columns = this.closest("table").columns;
      for (let column of columns) {
        let item = this.#context.querySelector(`[value="${column.id}"]`);
        if (!item) {
          continue;
        }

        if (!column.hidden) {
          item.setAttribute("checked", "true");
          continue;
        }

        item.removeAttribute("checked");
      }
    });

    this.#button.addEventListener("click", event => {
      this.#context.openPopup(event.target, { triggerEvent: event });
    });
  }

  /**
   * Add all toggable columns to the context menu popup of the picker button.
   */
  #initPopup() {
    let table = this.closest("table");
    let columns = table.columns;
    let items = new DocumentFragment();
    for (let column of columns) {
      // Skip those columns we don't want to allow hiding.
      if (column.picker === false) {
        continue;
      }

      let menuitem = document.createXULElement("menuitem");
      items.append(menuitem);
      menuitem.setAttribute("type", "checkbox");
      menuitem.setAttribute("name", "toggle");
      menuitem.setAttribute("value", column.id);
      menuitem.setAttribute("closemenu", "none");
      if (column.l10n?.menuitem) {
        document.l10n.setAttributes(menuitem, column.l10n.menuitem);
      }

      menuitem.addEventListener("command", () => {
        this.dispatchEvent(
          new CustomEvent("columns-changed", {
            bubbles: true,
            detail: {
              target: menuitem,
              value: column.id,
            },
          })
        );
      });
    }

    items.append(document.createXULElement("menuseparator"));
    let restoreItem = document.createXULElement("menuitem");
    restoreItem.id = "restoreColumnOrder";
    restoreItem.addEventListener("command", () => {
      this.dispatchEvent(
        new CustomEvent("restore-columns", {
          bubbles: true,
        })
      );
    });
    document.l10n.setAttributes(
      restoreItem,
      "tree-list-view-column-picker-restore"
    );
    items.append(restoreItem);

    for (const templateID of table.popupMenuTemplates) {
      items.append(document.getElementById(templateID).content.cloneNode(true));
    }

    this.#context.replaceChildren(items);
  }
}
customElements.define(
  "tree-view-table-column-picker",
  TreeViewTableColumnPicker,
  { extends: "th" }
);

/**
 * A more powerful list designed to be used with a view (nsITreeView or
 * whatever replaces it in time) and be scalable to a very large number of
 * items if necessary. Multiple selections are possible and changes in the
 * connected view are cause updates to the list (provided `rowCountChanged`/
 * `invalidate` are called as appropriate).
 *
 * Rows are provided by a custom element that inherits from
 * TreeViewTableRow below. Set the name of the custom element as the "rows"
 * attribute.
 *
 * Include tree-listbox.css for appropriate styling.
 */
class TreeViewTableBody extends HTMLTableSectionElement {
  connectedCallback() {
    if (this.hasConnected) {
      return;
    }
    this.hasConnected = true;

    this.tabIndex = 0;
    this.setAttribute("is", "tree-view-table-body");
    this.setAttribute("role", "tree");
    this.setAttribute("aria-multiselectable", "true");

    let treeView = this.closest("tree-view");
    this.addEventListener("keyup", treeView);
    this.addEventListener("click", treeView);
    this.addEventListener("keydown", treeView);

    if (treeView.dataset.labelId) {
      this.setAttribute("aria-labelledby", treeView.dataset.labelId);
    }
  }
}
customElements.define("tree-view-table-body", TreeViewTableBody, {
  extends: "tbody",
});

/**
 * Base class for rows in a TreeViewTableBody. Rows have a fixed height and
 * their position on screen is managed by the owning list.
 *
 * Sub-classes should override ROW_HEIGHT, styles, and fragment to suit the
 * intended layout. The index getter/setter should be overridden to fill the
 * layout with values.
 */
class TreeViewTableRow extends HTMLTableRowElement {
  /**
   * Fixed height of this row. Rows in the list will be spaced this far
   * apart. This value must not change at runtime.
   *
   * @type {integer}
   */
  static ROW_HEIGHT = 50;

  connectedCallback() {
    if (this.hasConnected) {
      return;
    }
    this.hasConnected = true;

    this.tabIndex = -1;
    this.list = this.closest("tree-view");
    this.view = this.list.view;
    this.setAttribute("aria-selected", !!this.selected);
  }

  /**
   * The 0-based position of this row in the list. Override this setter to
   * fill layout based on values from the list's view. Always call back to
   * this class's getter/setter when inheriting.
   *
   * @note Don't short-circuit the setter if the given index is equal to the
   * existing index. Rows can be reused to display new data at the same index.
   *
   * @type {integer}
   */
  get index() {
    return this._index;
  }

  set index(index) {
    this.setAttribute(
      "role",
      this.list.table.body.getAttribute("role") === "tree"
        ? "treeitem"
        : "option"
    );
    this.setAttribute("aria-posinset", index + 1);
    this.id = `${this.list.id}-row${index}`;

    const isGroup = this.view.isContainer(index);
    this.classList.toggle("children", isGroup);

    const isGroupOpen = this.view.isContainerOpen(index);
    if (isGroup) {
      this.setAttribute("aria-expanded", isGroupOpen);
    } else {
      this.removeAttribute("aria-expanded");
    }
    this.classList.toggle("collapsed", !isGroupOpen);
    this._index = index;

    let table = this.closest("table");
    for (let column of table.columns) {
      let cell = this.querySelector(`.${column.id.toLowerCase()}-column`);
      // No need to do anything if this cell doesn't exist. This can happen
      // for non-table layouts.
      if (!cell) {
        continue;
      }

      // Always clear the colspan when updating the columns.
      cell.removeAttribute("colspan");

      // No need to do anything if this column is hidden.
      if (cell.hidden) {
        continue;
      }

      // Handle the special case for the selectable checkbox column.
      if (column.select) {
        let img = cell.firstElementChild;
        if (!img) {
          cell.classList.add("tree-view-row-select");
          img = document.createElement("img");
          img.src = "";
          img.tabIndex = -1;
          img.classList.add("tree-view-row-select-checkbox");
          cell.replaceChildren(img);
        }
        document.l10n.setAttributes(
          img,
          this.list._selection.isSelected(index)
            ? "tree-list-view-row-deselect"
            : "tree-list-view-row-select"
        );
        continue;
      }

      // No need to do anything if an earlier call to this function already
      // added the cell contents.
      if (cell.firstElementChild) {
        continue;
      }
    }

    // Account for the column picker in the last visible column if the table
    // if editable.
    if (table.editable) {
      let last = table.columns.filter(c => !c.hidden).pop();
      this.querySelector(`.${last.id.toLowerCase()}-column`)?.setAttribute(
        "colspan",
        "2"
      );
    }
  }

  /**
   * Tracks the selection state of the current row.
   *
   * @type {boolean}
   */
  get selected() {
    return this.classList.contains("selected");
  }

  set selected(selected) {
    this.setAttribute("aria-selected", !!selected);
    this.classList.toggle("selected", !!selected);
  }
}
customElements.define("tree-view-table-row", TreeViewTableRow, {
  extends: "tr",
});

/**
 * Simple tbody spacer used above and below the main tbody for space
 * allocation and ensuring the correct scrollable height.
 */
class TreeViewTableSpacer extends HTMLTableSectionElement {
  connectedCallback() {
    if (this.hasConnected) {
      return;
    }
    this.hasConnected = true;

    this.cell = document.createElement("td");
    const row = document.createElement("tr");
    row.appendChild(this.cell);
    this.appendChild(row);
  }

  /**
   * Set the cell colspan to reflect the number of visible columns in order
   * to generate a correct HTML markup.
   *
   * @param {int} count - The columns count.
   */
  setColspan(count) {
    this.cell.setAttribute("colspan", count);
  }

  /**
   * Set the height of the cell in order to occupy the empty area that will
   * be filled by new rows on demand when needed.
   *
   * @param {int} val - The pixel height the row should occupy.
   */
  setHeight(val) {
    this.cell.style.height = `${val}px`;
  }
}
customElements.define("tree-view-table-spacer", TreeViewTableSpacer, {
  extends: "tbody",
});