legion_prof_viewer 0.5.0

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

use egui::{
    Color32, NumExt, Pos2, Rect, RichText, ScrollArea, Slider, Stroke, TextStyle, TextWrapMode,
    Vec2,
};
use egui_extras::{Column, TableBuilder};
#[cfg(not(target_arch = "wasm32"))]
use itertools::Itertools;
use log::warn;
use percentage::{Percentage, PercentageInteger};
use regex::{Regex, escape};
use serde::{Deserialize, Serialize};

use crate::app::tile_manager::TileManager;
use crate::data::{
    DataSourceInfo, EntryID, EntryIndex, EntryInfo, Field, FieldID, FieldSchema, ItemLink,
    ItemMeta, ItemUID, SlotMetaTileData, SlotTileData, SummaryTileData, TileID, UtilPoint,
};
use crate::deferred_data::{
    CountingDeferredDataSource, DeferredDataSource, LruDeferredDataSource, TileResult,
};
use crate::timestamp::{
    Interval, Timestamp, TimestampDisplay, TimestampParseError, TimestampUnits,
};

/// Overview:
///   ProfApp -> Context, Window *
///   Window -> Config, Panel
///   Panel -> Summary, { Panel | Slot } *
///   Summary
///   Slot -> Item *
///
/// Context:
///   * Global configuration state (i.e., for all profiles)
///
/// Window:
///   * One Windows per profile
///   * Owns the ScrollArea (there is only **ONE** ScrollArea)
///   * Handles pan/zoom (there is only **ONE** pan/zoom setting)
///
/// Config:
///   * Window configuration state (i.e., specific to a profile)
///
/// Panel:
///   * One Panel for each level of nesting in the profile (root, node, kind)
///   * Table widget for (nested) cells
///   * Each row contains: label, content
///
/// Summary:
///   * Utilization widget
///
/// Slot:
///   * One Slot for each processor, channel, memory
///   * Viewer widget for items

#[derive(Debug, Clone)]
struct Summary {
    entry_id: EntryID,
    color: Color32,
    tiles: BTreeMap<TileID, Option<TileResult<SummaryTileData>>>,
}

#[derive(Debug, Clone)]
struct Slot {
    entry_id: EntryID,
    short_name: String,
    long_name: String,
    expanded: bool,
    max_rows: u64,

    // These maps have to track four different kinds of states:
    //
    //  1. Entry missing: user navigated away before response came back.
    //  2. None: awaiting response.
    //  3. Some(Err(_)): response failed or returned with an error.
    //  4. Some(Ok(_)): successfully completed response.
    tiles: BTreeMap<TileID, Option<TileResult<SlotTileData>>>,
    tile_metas: BTreeMap<TileID, Option<TileResult<SlotMetaTileData>>>,
    tile_metas_full: BTreeMap<TileID, Option<TileResult<SlotMetaTileData>>>,
}

#[derive(Debug, Clone)]
struct Panel<S: Entry> {
    entry_id: EntryID,
    short_name: String,
    long_name: String,
    expanded: bool,

    summary: Option<Summary>,
    slots: Vec<S>,
}

#[derive(Debug, Clone)]
struct ItemLocator {
    // For vertical scroll, we need the item's entry ID and row index
    // (note: reversed, because we're in screen space)
    entry_id: EntryID,
    irow: Option<usize>,

    // If we can't find the item on the initial attempt, we track the ItemUID
    // and attempt to find it once the tile loads
    item_uid: ItemUID,
}

#[derive(Debug, Clone)]
struct ItemDetail {
    // We populate metadata lazily, so there can be a delay until this is full
    meta: Option<ItemMeta>,
    loc: ItemLocator,
}

#[derive(Debug, Clone)]
struct SearchCacheItem {
    item_uid: ItemUID,

    // Cache fields for display
    title: String,

    // For horizontal scroll, we need the item's interval
    interval: Interval,

    // For vertical scroll, we need the item's row index (note: reversed,
    // because we're in screen space)
    irow: usize,
}

#[derive(Debug, Clone)]
struct SearchState {
    title_field: FieldID,

    // Search parameters
    query: String,
    last_query: String,
    search_field: FieldID,
    last_search_field: FieldID,
    whole_word: bool,
    last_whole_word: bool,
    last_word_regex: Option<Regex>,
    include_collapsed_entries: bool,
    last_include_collapsed_entries: bool,
    last_view_interval: Option<Interval>,

    // Cache of matching items
    result_set: BTreeSet<ItemUID>,
    result_cache: BTreeMap<EntryID, BTreeMap<TileID, BTreeMap<ItemUID, SearchCacheItem>>>,
    entry_tree: BTreeMap<u64, BTreeMap<u64, BTreeSet<u64>>>,
}

struct Config {
    field_schema: FieldSchema,

    // Node selection
    min_node: u64,
    max_node: u64,

    // Kind selection
    kinds: Vec<String>,
    kind_filter: BTreeSet<String>,

    // This is just for the local profile
    interval: Interval,
    warning_message: Option<String>,

    data_source: CountingDeferredDataSource<LruDeferredDataSource<Box<dyn DeferredDataSource>>>,

    search_state: SearchState,

    // When the user clicks on an item, we put it here
    items_selected: BTreeMap<ItemUID, ItemDetail>,

    // When the user clicks "Zoom to Item" or a search result, we put it here
    scroll_to_item: Option<ItemLocator>,
    // Sometimes, we cannot find the correct row to scroll to. In this case we
    // populate the following field to track the re-scroll when the item is found
    scroll_to_item_retry: Option<ItemLocator>,

    tile_manager: TileManager,
}

struct Window {
    panel: Panel<Panel<Panel<Slot>>>, // nodes -> kind -> proc/chan/mem
    index: u64,
    config: Config,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
enum IntervalOrigin {
    Zoom,
    Pan,
}

#[derive(Debug, Clone, Default, Deserialize, Serialize)]
struct IntervalState {
    levels: Vec<Interval>,
    origins: Vec<IntervalOrigin>,
    index: usize,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
enum IntervalSelectError {
    InvalidValue,
    NoUnit,
    InvalidUnit,
    StartAfterStop,
    StartAfterEnd,
    StopBeforeStart,
}

impl From<TimestampParseError> for IntervalSelectError {
    fn from(val: TimestampParseError) -> Self {
        match val {
            TimestampParseError::InvalidValue => IntervalSelectError::InvalidValue,
            TimestampParseError::NoUnit => IntervalSelectError::NoUnit,
            TimestampParseError::InvalidUnit => IntervalSelectError::InvalidUnit,
        }
    }
}

impl fmt::Display for IntervalSelectError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IntervalSelectError::InvalidValue => write!(f, "invalid value"),
            IntervalSelectError::NoUnit => write!(f, "no unit"),
            IntervalSelectError::InvalidUnit => write!(f, "invalid unit"),
            IntervalSelectError::StartAfterStop => write!(f, "start after stop"),
            IntervalSelectError::StartAfterEnd => write!(f, "start after end"),
            IntervalSelectError::StopBeforeStart => write!(f, "stop before start"),
        }
    }
}

#[derive(Debug, Clone, Default)]
struct IntervalSelectState {
    // User-entered strings for the interval start/stop.
    start_buffer: String,
    stop_buffer: String,

    // Parse errors for the respective strings (if any).
    start_error: Option<IntervalSelectError>,
    stop_error: Option<IntervalSelectError>,
}

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
enum ItemLinkNavigationMode {
    #[default]
    Zoom,
    Pan,
}

impl ItemLinkNavigationMode {
    fn label_text(&self) -> &'static str {
        match *self {
            ItemLinkNavigationMode::Zoom => "Zoom to Item",
            ItemLinkNavigationMode::Pan => "Pan to Item",
        }
    }
}

#[derive(Debug, Clone, Default, Deserialize, Serialize)]
struct Context {
    #[serde(skip)]
    row_height: f32,
    #[serde(skip)]
    scale_factor: f32,

    #[serde(skip)]
    row_scroll_delta: i32,

    #[serde(skip)]
    subheading_size: f32,

    // This is across all profiles
    #[serde(skip)]
    total_interval: Interval,

    // Visible time range
    #[serde(skip)]
    view_interval: Interval,

    #[serde(skip)]
    drag_origin: Option<Pos2>,

    // Hack: We need to track the screenspace rect where slot/summary
    // data gets drawn. This gets used rendering the cursor, but we
    // only know it when we render slots. So stash it here.
    #[serde(skip)]
    slot_rect: Option<Rect>,

    item_link_mode: ItemLinkNavigationMode,

    toggle_dark_mode: bool,

    debug: bool,

    #[serde(skip)]
    show_controls: bool,

    #[serde(skip)]
    view_interval_history: IntervalState,
    #[serde(skip)]
    interval_select_state: IntervalSelectState,
}

#[derive(Default, Deserialize, Serialize)]
#[serde(default)] // deserialize missing fields as default value
struct ProfApp {
    // Data sources waiting to be turned into windows.
    #[serde(skip)]
    pending_data_sources: VecDeque<Box<dyn DeferredDataSource>>,

    #[serde(skip)]
    windows: Vec<Window>,

    cx: Context,

    #[cfg(not(target_arch = "wasm32"))]
    #[serde(skip)]
    last_update: Option<Instant>,
}

trait Entry {
    fn new(info: &EntryInfo, entry_id: EntryID) -> Self;

    fn entry_id(&self) -> &EntryID;
    fn label_text(&self) -> &str;
    fn hover_text(&self) -> &str;

    fn find_slot(&self, entry_id: &EntryID, level: u64) -> Option<&Slot>;
    fn find_slot_mut(&mut self, entry_id: &EntryID, level: u64) -> Option<&mut Slot>;
    fn find_summary_mut(&mut self, entry_id: &EntryID, level: u64) -> Option<&mut Summary>;

    fn expand_slot(&mut self, entry_id: &EntryID, level: u64);

    fn inflate_meta(&mut self, config: &mut Config, cx: &mut Context);

    fn search(&mut self, config: &mut Config);

    fn label(&mut self, ui: &mut egui::Ui, rect: Rect, cx: &Context) {
        let response = ui.allocate_rect(
            rect,
            if self.is_expandable() {
                egui::Sense::click()
            } else {
                egui::Sense::hover()
            },
        );

        let style = ui.style();
        let font_id = TextStyle::Body.resolve(style);
        let visuals = if self.is_expandable() {
            style.interact_selectable(&response, false)
        } else {
            *style.noninteractive()
        };

        ui.painter()
            .rect(rect, 0.0, visuals.bg_fill, visuals.bg_stroke);
        let spacing = style.spacing.item_spacing * Vec2::new(1.0, cx.scale_factor);
        let layout = ui.painter().layout(
            self.label_text().to_owned(),
            font_id,
            visuals.text_color(),
            rect.width() - spacing.x * 2.0,
        );
        ui.painter()
            .galley(rect.min + spacing, layout, visuals.text_color());

        if response.clicked() {
            // This will take effect next frame because we can't redraw this widget now
            self.toggle_expanded();
        } else if response.hovered() {
            response.on_hover_text(self.hover_text());
        }
    }

    fn content(
        &mut self,
        ui: &mut egui::Ui,
        rect: Rect,
        viewport: Rect,
        config: &mut Config,
        cx: &mut Context,
    );

    fn height(&self, prefix: Option<&EntryID>, config: &Config, cx: &Context) -> f32;

    fn is_expandable(&self) -> bool;

    fn toggle_expanded(&mut self);
}

impl Summary {
    fn inflate(&mut self, config: &mut Config, cx: &mut Context) {
        const PART: bool = false;
        let tile_ids = config.request_tiles(cx.view_interval, PART);
        Config::invalidate_cache(&tile_ids, &mut self.tiles);
        for tile_id in tile_ids {
            self.tiles.entry(tile_id).or_insert_with(|| {
                config
                    .data_source
                    .fetch_summary_tile(&self.entry_id, tile_id, PART);
                None
            });
        }
    }
}

impl Entry for Summary {
    fn new(info: &EntryInfo, entry_id: EntryID) -> Self {
        if let EntryInfo::Summary { color } = info {
            Self {
                entry_id,
                color: *color,
                tiles: BTreeMap::new(),
            }
        } else {
            unreachable!()
        }
    }

    fn entry_id(&self) -> &EntryID {
        &self.entry_id
    }
    fn label_text(&self) -> &str {
        "avg"
    }
    fn hover_text(&self) -> &str {
        "Utilization Plot of Average Usage Over Time"
    }

    fn find_slot(&self, _entry_id: &EntryID, _level: u64) -> Option<&Slot> {
        unreachable!()
    }

    fn find_slot_mut(&mut self, _entry_id: &EntryID, _level: u64) -> Option<&mut Slot> {
        unreachable!()
    }

    fn find_summary_mut(&mut self, entry_id: &EntryID, level: u64) -> Option<&mut Summary> {
        assert_eq!(entry_id.level(), level);
        assert_eq!(entry_id.index(level - 1)?, EntryIndex::Summary);
        Some(self)
    }

    fn expand_slot(&mut self, _entry_id: &EntryID, _level: u64) {
        unreachable!()
    }

    fn inflate_meta(&mut self, _config: &mut Config, _cx: &mut Context) {
        unreachable!()
    }

    fn search(&mut self, _config: &mut Config) {
        unreachable!()
    }

    fn content(
        &mut self,
        ui: &mut egui::Ui,
        rect: Rect,
        _viewport: Rect,
        config: &mut Config,
        cx: &mut Context,
    ) {
        cx.slot_rect = Some(rect); // Save slot rect for use later

        const TOOLTIP_RADIUS: f32 = 4.0;
        let hover_pos = ui.rect_hover_pos(rect); // where is the mouse hovering?

        self.inflate(config, cx);

        let style = ui.style();
        let visuals = style.noninteractive();
        ui.painter()
            .rect(rect, 0.0, visuals.bg_fill, visuals.bg_stroke);

        let stroke = Stroke::new(visuals.bg_stroke.width, self.color);

        // Conversions to and from screen space coordinates
        let util_to_screen = |util: &UtilPoint| {
            let time = cx.view_interval.unlerp(util.time);
            rect.lerp_inside(Vec2::new(time, 1.0 - util.util))
        };
        let screen_to_util = |screen: Pos2| UtilPoint {
            time: cx
                .view_interval
                .lerp((screen.x - rect.left()) / rect.width()),
            util: 1.0 - (screen.y - rect.top()) / rect.height(),
        };

        // Linear interpolation along the line from p1 to p2
        let interpolate = |p1: Pos2, p2: Pos2, x: f32| {
            let ratio = (x - p1.x) / (p2.x - p1.x);
            Rect::from_min_max(p1, p2).lerp_inside(Vec2::new(ratio, ratio))
        };

        let mut last_util: Option<&UtilPoint> = None;
        let mut last_point: Option<Pos2> = None;
        let mut hover_util = None;
        for tile in self.tiles.values().flatten() {
            let tile = match tile {
                Ok(t) => t,
                Err(e) => {
                    warn!("{}", e);
                    // Paint the entire tile red to indicate the error.
                    ui.painter().rect(rect, 0.0, Color32::RED, Stroke::NONE);
                    return;
                }
            };

            for util in &tile.utilization {
                let mut point = util_to_screen(util);
                if let Some(mut last) = last_point {
                    let last_util = last_util.unwrap();
                    if cx
                        .view_interval
                        .overlaps(Interval::new(last_util.time, util.time))
                    {
                        // Interpolate when out of view
                        if last.x < rect.min.x {
                            last = interpolate(last, point, rect.min.x);
                        }
                        if point.x > rect.max.x {
                            point = interpolate(last, point, rect.max.x);
                        }

                        ui.painter().line_segment([last, point], stroke);

                        if let Some(hover) = hover_pos {
                            if last.x <= hover.x && hover.x < point.x {
                                let interp = interpolate(last, point, hover.x);
                                ui.painter().circle_stroke(
                                    interp,
                                    TOOLTIP_RADIUS,
                                    visuals.fg_stroke,
                                );
                                hover_util = Some(screen_to_util(interp));
                            }
                        }
                    }
                }

                last_point = Some(point);
                last_util = Some(util);
            }
        }

        if let Some(util) = hover_util {
            let time = cx.view_interval.unlerp(util.time);
            let util_rect = Rect::from_min_max(
                rect.lerp_inside(Vec2::new(time - 0.05, 0.0)),
                rect.lerp_inside(Vec2::new(time + 0.05, 1.0)),
            );
            ui.show_tooltip(
                "utilization_tooltip",
                &util_rect,
                format!("{:.0}% Utilization", util.util * 100.0),
            );
        }
    }

    fn height(&self, prefix: Option<&EntryID>, _config: &Config, cx: &Context) -> f32 {
        assert!(prefix.is_none());
        const ROWS: u64 = 4;
        ROWS as f32 * cx.row_height
    }

    fn is_expandable(&self) -> bool {
        false
    }

    fn toggle_expanded(&mut self) {
        unreachable!();
    }
}

impl fmt::Display for Field {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Field::I64(value) => write!(f, "{value}"),
            Field::U64(value) => write!(f, "{value}"),
            Field::String(value) => write!(f, "{value}"),
            Field::Interval(value) => write!(f, "{value}"),
            Field::ItemLink(ItemLink { title, .. }) => write!(f, "{title}"),
            Field::Vec(fields) => {
                for (i, field) in fields.iter().enumerate() {
                    write!(f, "{field}")?;
                    if i < fields.len() {
                        write!(f, ", ")?;
                    }
                }
                Ok(())
            }
            Field::Empty => write!(f, ""),
        }
    }
}

struct FieldWithName<'a>(&'a str, &'a Field);

impl fmt::Display for FieldWithName<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let FieldWithName(name, value) = self;
        match value {
            Field::Empty => write!(f, "{name}"),
            _ => write!(f, "{name}: {value}"),
        }
    }
}

impl Slot {
    fn rows(&self) -> u64 {
        const UNEXPANDED_ROWS: u64 = 2;
        if self.expanded {
            self.max_rows.at_least(UNEXPANDED_ROWS)
        } else {
            UNEXPANDED_ROWS
        }
    }

    fn inflate(&mut self, config: &mut Config, cx: &mut Context) -> Vec<TileID> {
        const PART: bool = false;
        let tile_ids = config.request_tiles(cx.view_interval, PART);
        Config::invalidate_cache(&tile_ids, &mut self.tiles);
        Config::invalidate_cache(&tile_ids, &mut self.tile_metas);
        for tile_id in &tile_ids {
            self.tiles.entry(*tile_id).or_insert_with(|| {
                config
                    .data_source
                    .fetch_slot_tile(&self.entry_id, *tile_id, false);
                None
            });
        }
        tile_ids
    }

    fn fetch_meta_tile(
        &mut self,
        tile_id: TileID,
        config: &mut Config,
        full: bool,
    ) -> Option<&TileResult<SlotMetaTileData>> {
        let metas = if full {
            &mut self.tile_metas_full
        } else {
            &mut self.tile_metas
        };

        metas
            .entry(tile_id)
            .or_insert_with(|| {
                config
                    .data_source
                    .fetch_slot_meta_tile(&self.entry_id, tile_id, full);
                None
            })
            .as_ref()
    }

    #[allow(clippy::too_many_arguments)]
    fn render_tile(
        &mut self,
        tile_id: TileID,
        rows: u64,
        mut hover_pos: Option<Pos2>,
        ui: &mut egui::Ui,
        rect: Rect,
        viewport: Rect,
        config: &mut Config,
        cx: &mut Context,
    ) -> Option<Pos2> {
        let tile = self.tiles.get(&tile_id).unwrap();

        if !tile.is_some() {
            // Tile hasn't finished loading.
            return hover_pos;
        }
        let tile = tile.as_ref().unwrap();

        let tile = match tile {
            Ok(t) => t,
            Err(e) => {
                warn!("{}", e);
                // Paint the entire tile red to indicate the error.
                ui.painter().rect(rect, 0.0, Color32::RED, Stroke::NONE);
                return hover_pos;
            }
        };

        if !cx.view_interval.overlaps(tile_id.0) {
            return hover_pos;
        }

        // Figure out roughly how large a pixel is on the screen.
        let pixel_ns = (cx.view_interval.duration_ns() as f32 / rect.width()) as i64;

        // Track which item, if any, we're interacting with
        let mut interact_item = None;

        for (row, row_items) in tile.items.iter().enumerate() {
            // Need to reverse the rows because we're working in screen space
            let irow = rows - (row as u64) - 1;

            // We want to do this first on rows, so that we can cut the
            // entire row if we don't need it

            // Compute bounds for the whole row
            let row_min = rect.lerp_inside(Vec2::new(0.0, (irow as f32 + 0.05) / rows as f32));
            let row_max = rect.lerp_inside(Vec2::new(1.0, (irow as f32 + 0.95) / rows as f32));

            // Cull if out of bounds
            // Note: need to shift by rect.min to get to viewport space
            if row_max.y - rect.min.y < viewport.min.y {
                break;
            } else if row_min.y - rect.min.y > viewport.max.y {
                continue;
            }

            // Check if mouse is hovering over this row
            let row_rect = Rect::from_min_max(row_min, row_max);
            let row_hover = hover_pos.is_some_and(|h| row_rect.contains(h));

            // Now handle the items
            for (item_idx, item) in row_items.iter().enumerate() {
                if !cx.view_interval.overlaps(item.interval) {
                    continue;
                }

                // Expand interval to use at least one pixel, but do NOT
                // overlap neighboring items.
                let mut interval = item.interval;
                if interval.duration_ns() < pixel_ns {
                    let expand_ns = (pixel_ns - interval.duration_ns()) / 2;
                    interval = interval.grow(expand_ns).intersection(tile_id.0);
                    if item_idx > 0 {
                        let last_item = &row_items[item_idx - 1];
                        interval = interval.subtract_before(last_item.interval.stop);
                    }
                    if item_idx < row_items.len() - 1 {
                        let next_item = &row_items[item_idx + 1];
                        interval = interval.subtract_after(next_item.interval.start);
                    }
                }

                // Note: the interval is EXCLUSIVE. This turns out to be what
                // we want here, because in screen coordinates interval.stop
                // is the BEGINNING of the interval.stop nanosecond.
                let start = cx.view_interval.unlerp(interval.start).at_least(0.0);
                let stop = cx.view_interval.unlerp(interval.stop).at_most(1.0);
                let min = rect.lerp_inside(Vec2::new(start, (irow as f32 + 0.05) / rows as f32));
                let max = rect.lerp_inside(Vec2::new(stop, (irow as f32 + 0.95) / rows as f32));

                let item_rect = Rect::from_min_max(min, max);
                if row_hover && hover_pos.is_some_and(|h| item_rect.contains(h)) {
                    hover_pos = None;
                    interact_item = Some((row, item_idx, item_rect, tile_id));
                }

                let highlight = config.items_selected.contains_key(&item.item_uid);

                let mut color = item.color;
                if !config.search_state.query.is_empty() {
                    if config.search_state.result_set.contains(&item.item_uid) || highlight {
                        color = Color32::RED;
                    } else {
                        color = color.gamma_multiply(0.2);
                    }
                } else if highlight {
                    color = Color32::RED;
                }

                ui.painter().rect(item_rect, 0.0, color, Stroke::NONE);
            }
        }

        if let Some((row, item_idx, item_rect, tile_id)) = interact_item {
            // Hack: clone here  to avoid mutability conflict.
            let entry_id = self.entry_id.clone();
            const PART: bool = false;
            if let Some(tile_meta) = self.fetch_meta_tile(tile_id, config, PART) {
                let tile_meta = match tile_meta {
                    Ok(t) => t,
                    Err(e) => {
                        warn!("{}", e);
                        ui.show_tooltip("task_tooltip", &item_rect, e);
                        return hover_pos;
                    }
                };

                let item_meta = &tile_meta.items[row][item_idx];
                ui.show_tooltip_ui("task_tooltip", &item_rect, |ui| {
                    ui.label(&item_meta.title);
                    if cx.debug {
                        ui.label(format!("Item UID: {}", item_meta.item_uid.0));
                    }
                    for (field_id, field, color) in &item_meta.fields {
                        let name = config.field_schema.get_name(*field_id).unwrap();
                        let text = format!("{}", FieldWithName(name, field));
                        if let Some(color) = color {
                            ui.label(RichText::new(text).color(*color));
                        } else {
                            ui.label(text);
                        }
                    }
                    ui.label("(Click to show details.)");
                });

                // Also mark task as selected if the mouse has been clicked
                ui.input(|i| {
                    // A "click" is measured on *release*, assuming certain
                    // properties hold (e.g., the button was held less than
                    // some duration, and it moved less than some amount).
                    if i.pointer.any_click() && i.pointer.primary_released() {
                        let irow = Some(rows as usize - row - 1);
                        match config.items_selected.entry(item_meta.item_uid) {
                            std::collections::btree_map::Entry::Vacant(e) => {
                                e.insert(ItemDetail {
                                    meta: Some(item_meta.clone()),
                                    loc: ItemLocator {
                                        entry_id,
                                        irow,
                                        item_uid: item_meta.item_uid,
                                    },
                                });
                            }
                            std::collections::btree_map::Entry::Occupied(e) => {
                                e.remove_entry();
                            }
                        }
                    }
                });
            }
        }

        hover_pos
    }
}

impl Entry for Slot {
    fn new(info: &EntryInfo, entry_id: EntryID) -> Self {
        if let EntryInfo::Slot {
            short_name,
            long_name,
            max_rows,
        } = info
        {
            Self {
                entry_id,
                short_name: short_name.to_owned(),
                long_name: long_name.to_owned(),
                expanded: true,
                max_rows: *max_rows,
                tiles: BTreeMap::new(),
                tile_metas: BTreeMap::new(),
                tile_metas_full: BTreeMap::new(),
            }
        } else {
            unreachable!()
        }
    }

    fn entry_id(&self) -> &EntryID {
        &self.entry_id
    }
    fn label_text(&self) -> &str {
        &self.short_name
    }
    fn hover_text(&self) -> &str {
        &self.long_name
    }

    fn find_slot(&self, entry_id: &EntryID, level: u64) -> Option<&Slot> {
        assert_eq!(entry_id.level(), level);
        assert!(entry_id.slot_index(level - 1).is_some());
        Some(self)
    }

    fn find_slot_mut(&mut self, entry_id: &EntryID, level: u64) -> Option<&mut Slot> {
        assert_eq!(entry_id.level(), level);
        assert!(entry_id.slot_index(level - 1).is_some());
        Some(self)
    }

    fn find_summary_mut(&mut self, _entry_id: &EntryID, _level: u64) -> Option<&mut Summary> {
        unreachable!()
    }

    fn expand_slot(&mut self, entry_id: &EntryID, level: u64) {
        assert_eq!(entry_id.level(), level);
        assert!(entry_id.slot_index(level - 1).is_some());
        self.expanded = true;
    }

    fn inflate_meta(&mut self, config: &mut Config, cx: &mut Context) {
        const FULL: bool = true;
        let tile_ids = config.request_tiles(cx.view_interval, FULL);
        Config::invalidate_cache(&tile_ids, &mut self.tile_metas_full);
        for tile_id in tile_ids {
            self.fetch_meta_tile(tile_id, config, FULL);
        }
    }

    fn search(&mut self, config: &mut Config) {
        if !config.search_state.start_entry(self) {
            return;
        }

        for (tile_id, tile) in &self.tile_metas_full {
            if let Some(Ok(tile)) = tile {
                if !config.search_state.start_tile(self, *tile_id) {
                    continue;
                }

                for (row, row_items) in tile.items.iter().enumerate() {
                    for item in row_items {
                        if config.search_state.is_match(item) {
                            // Reverse rows because we're in screen space
                            let irow = tile.items.len() - row - 1;
                            config.search_state.insert(self, *tile_id, irow, item);
                        }
                    }
                }
            }
        }
    }

    fn content(
        &mut self,
        ui: &mut egui::Ui,
        rect: Rect,
        viewport: Rect,
        config: &mut Config,
        cx: &mut Context,
    ) {
        cx.slot_rect = Some(rect); // Save slot rect for use later

        let mut hover_pos = ui.rect_hover_pos(rect); // where is the mouse hovering?

        if self.expanded {
            let tile_ids = self.inflate(config, cx);

            let style = ui.style();
            let visuals = style.noninteractive();
            ui.painter()
                .rect(rect, 0.0, visuals.bg_fill, visuals.bg_stroke);

            let rows = self.rows();
            for tile_id in tile_ids {
                hover_pos =
                    self.render_tile(tile_id, rows, hover_pos, ui, rect, viewport, config, cx);
            }
        }
    }

    fn height(&self, _prefix: Option<&EntryID>, _config: &Config, cx: &Context) -> f32 {
        self.rows() as f32 * cx.row_height
    }

    fn is_expandable(&self) -> bool {
        true
    }

    fn toggle_expanded(&mut self) {
        self.expanded = !self.expanded;
    }
}

impl<S: Entry> Panel<S> {
    fn render<T: Entry>(
        ui: &mut egui::Ui,
        rect: Rect,
        viewport: Rect,
        slot: &mut T,
        y: &mut f32,
        config: &mut Config,
        cx: &mut Context,
    ) -> bool {
        const LABEL_WIDTH: f32 = 60.0;
        const COL_PADDING: f32 = 4.0;
        const ROW_PADDING: f32 = 4.0;

        // Compute the size of this slot
        // This is in screen (i.e., rect) space
        let min_y = *y;
        let max_y = min_y + slot.height(None, config, cx);
        *y = max_y + ROW_PADDING;

        // Cull if out of bounds
        // Note: need to shift by rect.min to get to viewport space
        if max_y - rect.min.y < viewport.min.y {
            return false;
        } else if min_y - rect.min.y > viewport.max.y {
            return true;
        }

        // Draw label and content
        let label_min = rect.min.x;
        let label_max = (rect.min.x + LABEL_WIDTH).at_most(rect.max.x);
        let content_min = (label_max + COL_PADDING).at_most(rect.max.x);
        let content_max = rect.max.x;

        let label_subrect =
            Rect::from_min_max(Pos2::new(label_min, min_y), Pos2::new(label_max, max_y));
        let content_subrect =
            Rect::from_min_max(Pos2::new(content_min, min_y), Pos2::new(content_max, max_y));

        // Shift viewport up by the amount consumed
        // Invariant: (0, 0) in viewport is rect.min
        //   (i.e., subtracting rect.min gets us from screen space to viewport space)
        // Note: viewport.min is NOT necessarily (0, 0)
        let content_viewport = viewport.translate(Vec2::new(0.0, rect.min.y - min_y));

        slot.content(ui, content_subrect, content_viewport, config, cx);
        slot.label(ui, label_subrect, cx);

        false
    }

    fn is_slot_visible(slot: &S, config: &Config) -> bool {
        let level = slot.entry_id().level();
        if level == 1 {
            // Apply node filter.
            let index = slot.entry_id().last_slot_index().unwrap();
            index >= config.min_node && index <= config.max_node
        } else if level == 2 {
            // Apply kind filter.
            let kind = slot.label_text();
            config.kind_filter.is_empty() || config.kind_filter.contains(kind)
        } else {
            true
        }
    }
}

impl<S: Entry> Entry for Panel<S> {
    fn new(info: &EntryInfo, entry_id: EntryID) -> Self {
        if let EntryInfo::Panel {
            short_name,
            long_name,
            summary,
            slots,
        } = info
        {
            let expanded = entry_id.level() != 2;
            let summary = summary
                .as_ref()
                .map(|s| Summary::new(s, entry_id.summary()));
            let slots = slots
                .iter()
                .enumerate()
                .map(|(i, s)| S::new(s, entry_id.child(i as u64)))
                .collect();
            Self {
                entry_id,
                short_name: short_name.to_owned(),
                long_name: long_name.to_owned(),
                expanded,
                summary,
                slots,
            }
        } else {
            unreachable!()
        }
    }

    fn entry_id(&self) -> &EntryID {
        &self.entry_id
    }
    fn label_text(&self) -> &str {
        &self.short_name
    }
    fn hover_text(&self) -> &str {
        &self.long_name
    }

    fn find_slot(&self, entry_id: &EntryID, level: u64) -> Option<&Slot> {
        self.slots
            .get(entry_id.slot_index(level)? as usize)?
            .find_slot(entry_id, level + 1)
    }

    fn find_slot_mut(&mut self, entry_id: &EntryID, level: u64) -> Option<&mut Slot> {
        self.slots
            .get_mut(entry_id.slot_index(level)? as usize)?
            .find_slot_mut(entry_id, level + 1)
    }

    fn find_summary_mut(&mut self, entry_id: &EntryID, level: u64) -> Option<&mut Summary> {
        if level < entry_id.level() - 1 {
            self.slots
                .get_mut(entry_id.slot_index(level)? as usize)?
                .find_summary_mut(entry_id, level + 1)
        } else {
            self.summary.as_mut()?.find_summary_mut(entry_id, level + 1)
        }
    }

    fn expand_slot(&mut self, entry_id: &EntryID, level: u64) {
        self.slots
            .get_mut(entry_id.slot_index(level).unwrap() as usize)
            .unwrap()
            .expand_slot(entry_id, level + 1);
        self.expanded = true;
    }

    fn inflate_meta(&mut self, config: &mut Config, cx: &mut Context) {
        let force = config.search_state.include_collapsed_entries;
        if self.expanded || force {
            for slot in &mut self.slots {
                // Apply visibility settings
                if !force && !Self::is_slot_visible(slot, config) {
                    continue;
                }

                slot.inflate_meta(config, cx);
            }
        }
    }

    fn search(&mut self, config: &mut Config) {
        let force = config.search_state.include_collapsed_entries;
        if self.expanded || force {
            for slot in &mut self.slots {
                // Apply visibility settings
                if !force && !Self::is_slot_visible(slot, config) {
                    continue;
                }

                slot.search(config);
            }
        }
    }

    fn content(
        &mut self,
        ui: &mut egui::Ui,
        rect: Rect,
        viewport: Rect,
        config: &mut Config,
        cx: &mut Context,
    ) {
        let mut y = rect.min.y;
        if let Some(summary) = &mut self.summary {
            Self::render(ui, rect, viewport, summary, &mut y, config, cx);
        }

        if self.expanded {
            for slot in &mut self.slots {
                // Apply visibility settings
                if !Self::is_slot_visible(slot, config) {
                    continue;
                }

                if Self::render(ui, rect, viewport, slot, &mut y, config, cx) {
                    break;
                }
            }
        }
    }

    fn height(&self, prefix: Option<&EntryID>, config: &Config, cx: &Context) -> f32 {
        const UNEXPANDED_ROWS: u64 = 2;
        const ROW_PADDING: f32 = 4.0;

        let mut total = 0.0;
        let mut rows: i64 = 0;
        if let Some(summary) = &self.summary {
            total += summary.height(None, config, cx);
            rows += 1;
        } else if !self.expanded {
            // Need some minimum space if this panel has no summary and is collapsed
            total += UNEXPANDED_ROWS as f32 * cx.row_height;
            rows += 1;
        }

        if self.expanded {
            for slot in &self.slots {
                if let Some(prefix) = prefix {
                    // If this is our entry, stop
                    if slot.entry_id() == prefix {
                        break;
                    }
                }

                // Apply visibility settings
                if !Self::is_slot_visible(slot, config) {
                    continue;
                }

                total += slot.height(prefix, config, cx);

                if let Some(prefix) = prefix {
                    // If we're a prefix of the entry, recurse and then stop
                    if prefix.has_prefix(slot.entry_id()) {
                        break;
                    }
                }

                rows += 1;
            }
        }

        total += (rows - 1).at_least(0) as f32 * ROW_PADDING;

        total
    }

    fn is_expandable(&self) -> bool {
        !self.slots.is_empty()
    }

    fn toggle_expanded(&mut self) {
        self.expanded = !self.expanded;
    }
}

impl SearchState {
    fn new(title_id: FieldID) -> Self {
        Self {
            title_field: title_id,

            query: "".to_owned(),
            last_query: "".to_owned(),
            search_field: title_id,
            last_search_field: title_id,
            whole_word: false,
            last_whole_word: false,
            last_word_regex: None,
            include_collapsed_entries: false,
            last_include_collapsed_entries: false,
            last_view_interval: None,

            result_set: BTreeSet::new(),
            result_cache: BTreeMap::new(),
            entry_tree: BTreeMap::new(),
        }
    }

    fn clear(&mut self) {
        self.result_set.clear();
        self.result_cache.clear();
        self.entry_tree.clear();
    }

    fn ensure_valid_cache(&mut self, cx: &Context) {
        let mut invalidate = false;

        // Invalidate when the search query changes.
        if self.query != self.last_query {
            invalidate = true;
            self.last_query.clone_from(&self.query);
        }

        // Invalidate when the search field changes.
        if self.search_field != self.last_search_field {
            invalidate = true;
            self.last_search_field = self.search_field;
        }

        // Invalidate when the whole word setting changes.
        if self.whole_word != self.last_whole_word {
            invalidate = true;
            self.last_whole_word = self.whole_word;
        }

        // Invalidate when EXCLUDING collapsed entries. (I.e., because the
        // searched set shrinks. Growing is ok because search is monotonic.)
        if self.include_collapsed_entries != self.last_include_collapsed_entries
            && !self.include_collapsed_entries
        {
            invalidate = true;
            self.last_include_collapsed_entries = self.include_collapsed_entries;
        }

        // Invalidate when the view interval changes.
        if self.last_view_interval != Some(cx.view_interval) {
            invalidate = true;
            self.last_view_interval = Some(cx.view_interval);
        }

        if invalidate {
            if self.whole_word {
                let regex_string = format!("\\b{}\\b", escape(&self.query));
                self.last_word_regex = Some(Regex::new(&regex_string).unwrap());
            }

            self.clear();
        }
    }

    fn is_string_match(&self, s: &str) -> bool {
        if self.whole_word {
            let Some(regex) = &self.last_word_regex else {
                unreachable!();
            };
            regex.is_match(s)
        } else {
            s.contains(&self.query)
        }
    }

    fn is_field_match(&self, field: &Field) -> bool {
        match field {
            Field::String(s) => self.is_string_match(s),
            Field::ItemLink(ItemLink { title, .. }) => self.is_string_match(title),
            Field::Vec(fields) => fields.iter().any(|f| self.is_field_match(f)),
            _ => false,
        }
    }

    fn is_match(&self, item: &ItemMeta) -> bool {
        let field = self.search_field;
        if field == self.title_field {
            self.is_string_match(&item.title)
        } else if let Some((_, value, _)) = item.fields.iter().find(|(x, _, _)| *x == field) {
            self.is_field_match(value)
        } else {
            false
        }
    }

    const MAX_SEARCH_RESULTS: usize = 100_000;

    fn start_entry<E: Entry>(&mut self, entry: &E) -> bool {
        // Early exit if we found enough items.
        if self.result_set.len() >= Self::MAX_SEARCH_RESULTS {
            return false;
        }

        // Double lookup is better than cloning unconditionally.
        if !self.result_cache.contains_key(entry.entry_id()) {
            self.result_cache
                .entry(entry.entry_id().clone())
                .or_default();
        }

        // Always recurse into tiles, because results can be fetched
        // asynchronously.
        true
    }

    fn start_tile<E: Entry>(&mut self, entry: &E, tile_id: TileID) -> bool {
        // Early exit if we found enough items.
        if self.result_set.len() >= Self::MAX_SEARCH_RESULTS {
            return false;
        }

        let mut result = true;
        // Always called second, so we know the entry exists.
        let cache = self.result_cache.get_mut(entry.entry_id()).unwrap();
        cache
            .entry(tile_id)
            .and_modify(|_| {
                result = false;
            })
            .or_default();
        result
    }

    fn insert<E: Entry>(&mut self, entry: &E, tile_id: TileID, irow: usize, item: &ItemMeta) {
        if self.result_set.len() >= Self::MAX_SEARCH_RESULTS {
            return;
        }

        // We want each item to appear once, so check the result set first
        // before inserting.
        if self.result_set.insert(item.item_uid) {
            let cache = self.result_cache.get_mut(entry.entry_id()).unwrap();
            let cache = cache.get_mut(&tile_id).unwrap();
            cache
                .entry(item.item_uid)
                .or_insert_with(|| SearchCacheItem {
                    item_uid: item.item_uid,
                    irow,
                    interval: item.original_interval,
                    title: item.title.clone(),
                });
        }
    }

    fn build_entry_tree(&mut self) {
        for (entry_id, cache) in &self.result_cache {
            let cache_size: u64 = cache.values().map(|x| x.len() as u64).sum();
            if cache_size == 0 {
                continue;
            }

            let level0_index = entry_id.slot_index(0).unwrap();
            let level1_index = entry_id.slot_index(1).unwrap();
            let level2_index = entry_id.slot_index(2).unwrap();

            let level0_subtree = self.entry_tree.entry(level0_index).or_default();
            let level1_subtree = level0_subtree.entry(level1_index).or_default();
            level1_subtree.insert(level2_index);
        }
    }
}

impl Config {
    fn new(data_source: Box<dyn DeferredDataSource>, info: DataSourceInfo) -> Self {
        let max_node = info.entry_info.nodes();
        let kinds = info.entry_info.kinds();
        let interval = info.interval;
        let tile_set = info.tile_set;
        let warning_message = info.warning_message;

        let mut field_schema = info.field_schema;
        assert!(!field_schema.contains_name("Title"));
        let title_id = field_schema.insert("Title".to_owned(), true);
        let search_state = SearchState::new(title_id);

        Self {
            field_schema,
            min_node: 0,
            max_node,
            kinds,
            kind_filter: BTreeSet::new(),
            interval,
            warning_message,
            data_source: CountingDeferredDataSource::new(LruDeferredDataSource::new(
                data_source,
                NonZeroUsize::new(1024).unwrap(),
            )),
            search_state,
            items_selected: BTreeMap::new(),
            scroll_to_item: None,
            scroll_to_item_retry: None,
            tile_manager: TileManager::new(tile_set, interval),
        }
    }

    fn request_tiles(&mut self, view_interval: Interval, full: bool) -> Vec<TileID> {
        self.tile_manager.request_tiles(view_interval, full)
    }

    fn invalidate_cache<T>(tile_ids: &[TileID], cache: &mut BTreeMap<TileID, T>) {
        TileManager::invalidate_cache(tile_ids, cache);
    }

    fn scroll_to_item(&mut self, item_loc: ItemLocator) {
        self.scroll_to_item = Some(item_loc.clone());
        self.scroll_to_item_retry = None;

        self.items_selected
            .entry(item_loc.item_uid)
            .or_insert_with(|| ItemDetail {
                meta: None,
                loc: item_loc,
            });
    }
}

impl Window {
    fn new(data_source: Box<dyn DeferredDataSource>, info: DataSourceInfo, index: u64) -> Self {
        Self {
            panel: Panel::new(&info.entry_info, EntryID::root()),
            index,
            config: Config::new(data_source, info),
        }
    }

    fn find_slot(&self, entry_id: &EntryID) -> Option<&Slot> {
        self.panel.find_slot(entry_id, 0)
    }

    fn find_slot_mut(&mut self, entry_id: &EntryID) -> Option<&mut Slot> {
        self.panel.find_slot_mut(entry_id, 0)
    }

    fn find_summary_mut(&mut self, entry_id: &EntryID) -> Option<&mut Summary> {
        self.panel.find_summary_mut(entry_id, 0)
    }

    fn expand_slot(&mut self, entry_id: &EntryID) {
        self.panel.expand_slot(entry_id, 0);
    }

    fn inflate_meta(&mut self, entry_id: &EntryID, cx: &mut Context) {
        // Use the panel version directly to avoid a mutability conflict
        let slot = self.panel.find_slot_mut(entry_id, 0).unwrap();
        slot.inflate_meta(&mut self.config, cx);
    }

    fn find_item_irow(&self, entry_id: &EntryID, item_uid: ItemUID) -> Option<usize> {
        let slot = self.find_slot(entry_id)?;
        for tile in slot.tiles.values() {
            let Some(Ok(tile)) = tile else {
                continue;
            };
            for (row, items) in tile.items.iter().enumerate() {
                for item in items {
                    if item.item_uid == item_uid {
                        let rows = tile.items.len();
                        return Some(rows - row - 1);
                    }
                }
            }
        }
        None
    }

    fn find_item_meta(&self, entry_id: &EntryID, item_uid: ItemUID) -> Option<&ItemMeta> {
        let slot = self.find_slot(entry_id)?;
        for tile in slot.tile_metas_full.values() {
            let Some(Ok(tile)) = tile else {
                continue;
            };
            for items in &tile.items {
                for item in items {
                    if item.item_uid == item_uid {
                        return Some(item);
                    }
                }
            }
        }
        None
    }

    fn content(&mut self, ui: &mut egui::Ui, cx: &mut Context) {
        ui.horizontal(|ui| {
            ui.heading(format!("Profile {}", self.index));
            ui.label(cx.view_interval.to_string());
            if let Some(message) = &self.config.warning_message {
                ui.label(RichText::new(message).color(Color32::RED));
            }
        });

        ScrollArea::vertical()
            .auto_shrink([false; 2])
            .show_viewport(ui, |ui, viewport| {
                let height = self.panel.height(None, &self.config, cx);
                ui.set_height(height);
                ui.set_width(ui.available_width());

                let rect = Rect::from_min_size(ui.min_rect().min, viewport.size());

                let scroll_to = |irow, prefix_height| {
                    let mut item_rect =
                        rect.translate(Vec2::new(0.0, prefix_height + irow as f32 * cx.row_height));
                    item_rect.set_height(cx.row_height);
                    ui.scroll_to_rect(item_rect, Some(egui::Align::Center));
                };

                // First scroll attempt goes to the processor
                if let Some(ItemLocator {
                    ref entry_id, irow, ..
                }) = self.config.scroll_to_item
                {
                    let prefix_height = self.panel.height(Some(entry_id), &self.config, cx);
                    scroll_to(irow.unwrap_or(0), prefix_height);
                    if irow.is_none() {
                        let mut item = None;
                        std::mem::swap(&mut item, &mut self.config.scroll_to_item);
                        self.config.scroll_to_item_retry = item;
                    }
                    self.config.scroll_to_item = None;
                }

                // If we're able to find the item, we do a second scroll to the item
                let mut found_irow = None;
                if let Some(ItemLocator {
                    ref entry_id,
                    irow,
                    item_uid,
                }) = self.config.scroll_to_item_retry
                {
                    assert!(irow.is_none());
                    found_irow = self.find_item_irow(entry_id, item_uid);
                }

                if let Some(ItemLocator { ref entry_id, .. }) = self.config.scroll_to_item_retry {
                    if let Some(irow) = found_irow {
                        let prefix_height = self.panel.height(Some(entry_id), &self.config, cx);
                        scroll_to(irow, prefix_height);
                        self.config.scroll_to_item_retry = None;
                    }
                }

                // Root panel has no label
                self.panel.content(ui, rect, viewport, &mut self.config, cx);
            });
    }

    fn node_selection(&mut self, ui: &mut egui::Ui, cx: &Context) {
        ui.subheading("Node Selection", cx);
        let total = self.panel.slots.len().saturating_sub(1) as u64;
        let min_node = &mut self.config.min_node;
        let max_node = &mut self.config.max_node;
        ui.add(Slider::new(min_node, 0..=total).text("First"));
        if *min_node > *max_node {
            *max_node = *min_node;
        }
        ui.add(Slider::new(max_node, 0..=total).text("Last"));
        if *min_node > *max_node {
            *min_node = *max_node;
        }
    }

    fn filter_by_kind(&mut self, ui: &mut egui::Ui, cx: &Context) {
        ui.subheading("Filter by Kind", cx);
        ui.horizontal_wrapped(|ui| {
            for kind in &self.config.kinds {
                let initial = self.config.kind_filter.contains(kind);
                let mut enabled = initial;
                ui.toggle_value(&mut enabled, kind);
                if initial != enabled {
                    if enabled {
                        self.config.kind_filter.insert(kind.clone());
                    } else {
                        self.config.kind_filter.remove(kind);
                    }
                }
            }
        });
    }

    fn expand_collapse(&mut self, ui: &mut egui::Ui, cx: &Context) {
        let mut toggle_all = |label, toggle| {
            for node in &mut self.panel.slots {
                for kind in &mut node.slots {
                    if kind.expanded == toggle && kind.label_text() == label {
                        kind.toggle_expanded();
                    }
                }
            }
        };

        ui.subheading("Expand/Collapse", cx);
        ui.label("Expand by kind:");
        ui.horizontal_wrapped(|ui| {
            for kind in &self.config.kinds {
                if ui.button(kind).clicked() {
                    toggle_all(kind.to_lowercase(), false);
                }
            }
        });
        ui.label("Collapse by kind:");
        ui.horizontal_wrapped(|ui| {
            for kind in &self.config.kinds {
                if ui.button(kind).clicked() {
                    toggle_all(kind.to_lowercase(), true);
                }
            }
        });
    }

    fn select_interval(&mut self, ui: &mut egui::Ui, cx: &mut Context) {
        ui.subheading("Interval", cx);
        let start_res = ui
            .horizontal(|ui| {
                ui.label("Start:");
                ui.text_edit_singleline(&mut cx.interval_select_state.start_buffer)
            })
            .inner;

        if let Some(error) = cx.interval_select_state.start_error {
            ui.label(RichText::new(error.to_string()).color(Color32::RED));
        }

        let stop_res = ui
            .horizontal(|ui| {
                ui.label("Stop:");
                ui.text_edit_singleline(&mut cx.interval_select_state.stop_buffer)
            })
            .inner;

        if let Some(error) = cx.interval_select_state.stop_error {
            ui.label(RichText::new(error.to_string()).color(Color32::RED));
        }

        if start_res.lost_focus()
            && cx.interval_select_state.start_buffer != cx.view_interval.start.to_string()
        {
            match Timestamp::parse(&cx.interval_select_state.start_buffer) {
                Ok(start) => {
                    // validate timestamp
                    if start > cx.view_interval.stop {
                        cx.interval_select_state.start_error =
                            Some(IntervalSelectError::StartAfterStop);
                        return;
                    }
                    if start > cx.total_interval.stop {
                        cx.interval_select_state.start_error =
                            Some(IntervalSelectError::StartAfterEnd);
                        return;
                    }
                    let target = Interval::new(start, cx.view_interval.stop);
                    ProfApp::zoom(cx, target);
                }
                Err(e) => {
                    cx.interval_select_state.start_error = Some(e.into());
                }
            }
        }
        if stop_res.lost_focus()
            && cx.interval_select_state.stop_buffer != cx.view_interval.stop.to_string()
        {
            match Timestamp::parse(&cx.interval_select_state.stop_buffer) {
                Ok(stop) => {
                    // validate timestamp
                    if stop < cx.view_interval.start {
                        cx.interval_select_state.stop_error =
                            Some(IntervalSelectError::StopBeforeStart);
                        return;
                    }
                    let target = Interval::new(cx.view_interval.start, stop);
                    ProfApp::zoom(cx, target);
                }
                Err(e) => {
                    cx.interval_select_state.stop_error = Some(e.into());
                }
            }
        }
    }

    fn controls(&mut self, ui: &mut egui::Ui, cx: &mut Context) {
        const WIDGET_PADDING: f32 = 8.0;
        ui.heading(format!("Profile {}: Controls", self.index));
        ui.add_space(WIDGET_PADDING);
        self.node_selection(ui, cx);
        ui.add_space(WIDGET_PADDING);
        self.filter_by_kind(ui, cx);
        ui.add_space(WIDGET_PADDING);
        self.expand_collapse(ui, cx);
        ui.add_space(WIDGET_PADDING);
        self.select_interval(ui, cx);
    }

    fn search(&mut self, cx: &mut Context) {
        // Invalidate cache if the search query changed.
        self.config.search_state.ensure_valid_cache(cx);

        // If search query empty, skip search. (Note: do this after
        // invalidating cache, otherwise we get leftover search results when
        // clearing the query.)
        if self.config.search_state.query.is_empty() {
            return;
        }

        // Expand meta tiles. (Including collapsed entries, if requested).
        self.panel.inflate_meta(&mut self.config, cx);

        // Search whatever data we have. Results are cached by entry/tile.
        self.panel.search(&mut self.config);

        // Cache is now full and we can highlight/render the entries.
    }

    fn search_box(&mut self, ui: &mut egui::Ui, cx: &mut Context) {
        ui.horizontal(|ui| {
            // Hack: need to estimate the button width or else the text box
            // overflows. Refer to the source for egui::widgets::Button::ui
            // for calculations.
            let button_label = "✖";
            let button_padding = ui.spacing().button_padding;
            let available_width = ui.available_width() - 2.0 * button_padding.x;
            let button_text: egui::WidgetText = "✖".into();
            let button_text =
                button_text.into_galley(ui, None, available_width, egui::TextStyle::Button);
            let button_size = button_text.size() + 2.0 * button_padding;

            const MARGIN: f32 = 4.0; // From egui::TextEdit::margin
            let query_size =
                ui.available_size().x - button_size.x - ui.spacing().item_spacing.x - 2.0 * MARGIN;
            egui::TextEdit::singleline(&mut self.config.search_state.query)
                .desired_width(query_size)
                .show(ui);
            if ui.button(button_label).clicked() {
                self.config.search_state.query.clear();
            }
        });
        ui.horizontal(|ui| {
            ui.label("Search field:");
            let schema = &self.config.field_schema;
            let search_field = &mut self.config.search_state.search_field;
            egui::ComboBox::from_id_source("Search field")
                .selected_text(schema.get_name(*search_field).unwrap())
                .show_ui(ui, |ui| {
                    for field in schema.searchable() {
                        let name = schema.get_name(*field).unwrap();
                        ui.selectable_value(search_field, *field, name);
                    }
                });
        });
        ui.checkbox(
            &mut self.config.search_state.whole_word,
            "Match whole words only",
        );
        ui.checkbox(
            &mut self.config.search_state.include_collapsed_entries,
            "Include collapsed processors",
        );

        self.search(cx);
    }

    fn search_results(&mut self, ui: &mut egui::Ui, cx: &mut Context) {
        if self.config.search_state.query.is_empty() {
            ui.label("Enter a search to see results displayed here.");
            return;
        }

        if self.config.search_state.result_set.is_empty() {
            ui.label("No results found. Expand search to include collapsed processors?");

            return;
        }

        let num_results = self.config.search_state.result_set.len();
        if num_results >= SearchState::MAX_SEARCH_RESULTS {
            ui.label(format!(
                "Found {} results. (Limited to {}.)",
                num_results,
                SearchState::MAX_SEARCH_RESULTS
            ));
        } else {
            ui.label(format!("Found {} results.", num_results));
        }

        self.config.search_state.build_entry_tree();

        let mut scroll_target = None;
        ScrollArea::vertical()
            // Hack: estimate size of bottom UI.
            .max_height(ui.available_height() - 70.0)
            .auto_shrink([false; 2])
            .show(ui, |ui| {
                let root_tree = &self.config.search_state.entry_tree;
                for (level0_index, level0_subtree) in root_tree {
                    let level0_slot = &mut self.panel.slots[*level0_index as usize];
                    ui.collapsing(&level0_slot.long_name, |ui| {
                        for (level1_index, level1_subtree) in level0_subtree {
                            let level1_slot = &mut level0_slot.slots[*level1_index as usize];
                            ui.collapsing(&level1_slot.long_name, |ui| {
                                for level2_index in level1_subtree {
                                    let level2_slot =
                                        &mut level1_slot.slots[*level2_index as usize];
                                    ui.collapsing(&level2_slot.long_name, |ui| {
                                        let cache = &self.config.search_state.result_cache;
                                        let cache = cache.get(&level2_slot.entry_id).unwrap();
                                        for tile_cache in cache.values() {
                                            for item in tile_cache.values() {
                                                let button =
                                                    egui::widgets::Button::new(&item.title).small();
                                                if ui.add(button).clicked() {
                                                    let interval = item
                                                        .interval
                                                        .grow(item.interval.duration_ns() / 20);
                                                    ProfApp::zoom(cx, interval);
                                                    scroll_target = Some(ItemLocator {
                                                        entry_id: level2_slot.entry_id.clone(),
                                                        irow: Some(item.irow),
                                                        item_uid: item.item_uid,
                                                    });
                                                    level2_slot.expanded = true;
                                                    level1_slot.expanded = true;
                                                    level0_slot.expanded = true;
                                                }
                                            }
                                        }
                                    });
                                }
                            });
                        }
                    });
                }
            });
        if let Some(target) = scroll_target {
            self.config.scroll_to_item(target);
        }
    }

    fn search_controls(&mut self, ui: &mut egui::Ui, cx: &mut Context) {
        const WIDGET_PADDING: f32 = 8.0;
        ui.heading(format!("Profile {}: Search", self.index));
        ui.add_space(WIDGET_PADDING);
        self.search_box(ui, cx);
        ui.add_space(WIDGET_PADDING);
        self.search_results(ui, cx);
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
enum PanDirection {
    Left,
    Right,
}

impl ProfApp {
    /// Called once before the first frame.
    pub fn new(
        cc: &eframe::CreationContext<'_>,
        mut data_sources: Vec<Box<dyn DeferredDataSource>>,
    ) -> Self {
        // This is also where you can customized the look at feel of egui using
        // `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`.

        // Load previous app state (if any).
        // Note that you must enable the `persistence` feature for this to work.
        let mut result: Self = if let Some(storage) = cc.storage {
            eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default()
        } else {
            Default::default()
        };

        for data_source in &mut data_sources {
            data_source.fetch_info();
        }
        result.pending_data_sources.clear();
        result.pending_data_sources.extend(data_sources);

        result.windows.clear();

        result.cx.scale_factor = 1.0;
        result.cx.row_scroll_delta = 0;

        #[cfg(not(target_arch = "wasm32"))]
        {
            result.last_update = Some(Instant::now());
        }

        let theme = if result.cx.toggle_dark_mode {
            egui::Visuals::dark()
        } else {
            egui::Visuals::light()
        };
        cc.egui_ctx.set_visuals(theme);

        // Set solid scroll bar (default from egui pre-0.24)
        // The new default "thin" style isn't clickable with our canvas widget
        cc.egui_ctx.style_mut(|style| {
            style.spacing.scroll = egui::style::ScrollStyle::solid();
        });

        result
    }

    fn update_interval_select_state(cx: &mut Context) {
        cx.interval_select_state.start_buffer = cx.view_interval.start.to_string();
        cx.interval_select_state.stop_buffer = cx.view_interval.stop.to_string();
        cx.interval_select_state.start_error = None;
        cx.interval_select_state.stop_error = None;
    }

    fn update_view_interval(cx: &mut Context, interval: Interval, origin: IntervalOrigin) {
        cx.view_interval = interval;

        let history = &mut cx.view_interval_history;
        let index = history.index;

        // Only keep at most one Pan origin in a row
        if !history.levels.is_empty()
            && history.origins[index] == IntervalOrigin::Pan
            && origin == IntervalOrigin::Pan
        {
            history.levels.truncate(index);
            history.origins.truncate(index);
        }

        history.levels.truncate(index + 1);
        history.levels.push(interval);
        history.origins.truncate(index + 1);
        history.origins.push(origin);
        history.index = history.levels.len() - 1;
    }

    fn pan(cx: &mut Context, percent: PercentageInteger, dir: PanDirection) {
        if percent.value() == 0 {
            return;
        }

        let duration = percent.apply_to(cx.view_interval.duration_ns());
        let sign = match dir {
            PanDirection::Left => -1,
            PanDirection::Right => 1,
        };
        let interval = cx.view_interval.translate(duration * sign);

        ProfApp::update_view_interval(cx, interval, IntervalOrigin::Pan);
        ProfApp::update_interval_select_state(cx);
    }

    fn zoom(cx: &mut Context, interval: Interval) {
        if cx.view_interval == interval {
            return;
        }

        ProfApp::update_view_interval(cx, interval, IntervalOrigin::Zoom);
        ProfApp::update_interval_select_state(cx);
    }

    fn undo_pan_zoom(cx: &mut Context) {
        if cx.view_interval_history.index == 0 {
            return;
        }
        cx.view_interval_history.index -= 1;
        cx.view_interval = cx.view_interval_history.levels[cx.view_interval_history.index];
        ProfApp::update_interval_select_state(cx);
    }

    fn redo_pan_zoom(cx: &mut Context) {
        if cx.view_interval_history.index + 1 >= cx.view_interval_history.levels.len() {
            return;
        }
        cx.view_interval_history.index += 1;
        cx.view_interval = cx.view_interval_history.levels[cx.view_interval_history.index];
        ProfApp::update_interval_select_state(cx);
    }

    fn zoom_in(cx: &mut Context) {
        let quarter = -cx.view_interval.duration_ns() / 4;
        Self::zoom(cx, cx.view_interval.grow(quarter));
    }

    fn zoom_out(cx: &mut Context) {
        let half = cx.view_interval.duration_ns() / 2;
        Self::zoom(
            cx,
            cx.view_interval.grow(half).intersection(cx.total_interval),
        );
    }

    fn multiply_scale_factor(cx: &mut Context, factor: f32) {
        cx.scale_factor = (cx.scale_factor * factor).clamp(0.25, 4.0);
    }

    fn reset_scale_factor(cx: &mut Context) {
        cx.scale_factor = 1.0;
    }

    fn reset_ui(cx: &mut Context, windows: &mut [Window]) {
        cx.show_controls = false;
        for window in windows.iter_mut() {
            window.config.items_selected.clear();
        }
    }

    fn keyboard(ctx: &egui::Context, cx: &mut Context, windows: &mut [Window]) {
        // Focus is elsewhere, don't check any keys
        if ctx.memory(|m| m.focused().is_some()) {
            return;
        }

        enum Actions {
            ZoomIn,
            ZoomOut,
            UndoZoom,
            RedoZoom,
            ResetZoom,
            Pan(PercentageInteger, PanDirection),
            Scroll(i32),
            ExpandVertical,
            ShrinkVertical,
            ResetVertical,
            ToggleControls,
            ResetUI,
            NoAction,
        }
        let action = ctx.input(|i| {
            if i.modifiers.ctrl {
                if i.modifiers.alt {
                    if i.key_pressed(egui::Key::Plus) || i.key_pressed(egui::Key::Equals) {
                        Actions::ExpandVertical
                    } else if i.key_pressed(egui::Key::Minus) {
                        Actions::ShrinkVertical
                    } else if i.key_pressed(egui::Key::Num0) {
                        Actions::ResetVertical
                    } else {
                        Actions::NoAction
                    }
                } else if i.key_pressed(egui::Key::Plus) || i.key_pressed(egui::Key::Equals) {
                    Actions::ZoomIn
                } else if i.key_pressed(egui::Key::Minus) {
                    Actions::ZoomOut
                } else if i.key_pressed(egui::Key::ArrowLeft) {
                    Actions::UndoZoom
                } else if i.key_pressed(egui::Key::ArrowRight) {
                    Actions::RedoZoom
                } else if i.key_pressed(egui::Key::Num0) {
                    Actions::ResetZoom
                } else {
                    Actions::NoAction
                }
            } else if i.modifiers.shift {
                if i.key_pressed(egui::Key::ArrowLeft) {
                    Actions::Pan(Percentage::from(1), PanDirection::Left)
                } else if i.key_pressed(egui::Key::ArrowRight) {
                    Actions::Pan(Percentage::from(1), PanDirection::Right)
                } else if i.key_pressed(egui::Key::ArrowUp) {
                    Actions::Scroll(1)
                } else if i.key_pressed(egui::Key::ArrowDown) {
                    Actions::Scroll(-1)
                } else {
                    Actions::NoAction
                }
            } else if i.key_pressed(egui::Key::H) {
                Actions::ToggleControls
            } else if i.key_pressed(egui::Key::Escape) {
                Actions::ResetUI
            } else if i.key_pressed(egui::Key::ArrowLeft) {
                Actions::Pan(Percentage::from(5), PanDirection::Left)
            } else if i.key_pressed(egui::Key::ArrowRight) {
                Actions::Pan(Percentage::from(5), PanDirection::Right)
            } else if i.key_pressed(egui::Key::ArrowUp) {
                Actions::Scroll(5)
            } else if i.key_pressed(egui::Key::ArrowDown) {
                Actions::Scroll(-5)
            } else {
                Actions::NoAction
            }
        });
        match action {
            Actions::ZoomIn => ProfApp::zoom_in(cx),
            Actions::ZoomOut => ProfApp::zoom_out(cx),
            Actions::UndoZoom => ProfApp::undo_pan_zoom(cx),
            Actions::RedoZoom => ProfApp::redo_pan_zoom(cx),
            Actions::ResetZoom => ProfApp::zoom(cx, cx.total_interval),
            Actions::Pan(percent, dir) => ProfApp::pan(cx, percent, dir),
            Actions::Scroll(rows) => cx.row_scroll_delta = rows,
            Actions::ExpandVertical => ProfApp::multiply_scale_factor(cx, 2.0),
            Actions::ShrinkVertical => ProfApp::multiply_scale_factor(cx, 0.5),
            Actions::ResetVertical => ProfApp::reset_scale_factor(cx),
            Actions::ToggleControls => cx.show_controls = !cx.show_controls,
            Actions::ResetUI => ProfApp::reset_ui(cx, windows),
            Actions::NoAction => {}
        }
    }

    fn cursor(ui: &mut egui::Ui, cx: &mut Context) {
        // Hack: the UI rect we have at this point is not where the
        // timeline is being drawn. So fish out the coordinates we
        // need to draw the correct rect.

        // Sometimes slot_rect is None when initializing the UI
        if cx.slot_rect.is_none() {
            return;
        }

        let ui_rect = ui.min_rect();
        let slot_rect = cx.slot_rect.unwrap();
        let rect = Rect::from_min_max(
            Pos2::new(slot_rect.min.x, ui_rect.min.y),
            Pos2::new(slot_rect.max.x, ui_rect.max.y),
        );

        let response = ui.allocate_rect(rect, egui::Sense::drag());

        // Handle drag detection
        let mut drag_interval = None;

        let is_active_drag = response.dragged_by(egui::PointerButton::Primary);
        if is_active_drag && response.drag_started() {
            // On the beginning of a drag, save our position so we can
            // calculate the delta
            cx.drag_origin = response.interact_pointer_pos();
        }

        if let Some(origin) = cx.drag_origin {
            // We're in a drag, calculate the drag inetrval
            let current = response.interact_pointer_pos().unwrap();
            let min = origin.x.min(current.x);
            let max = origin.x.max(current.x);

            let start = (min - rect.left()) / rect.width();
            let start = cx.view_interval.lerp(start);
            let stop = (max - rect.left()) / rect.width();
            let stop = cx.view_interval.lerp(stop);

            let interval = Interval::new(start, stop);

            if is_active_drag {
                // Still in drag, draw a rectangle to show the dragged region
                let drag_rect =
                    Rect::from_min_max(Pos2::new(min, rect.min.y), Pos2::new(max, rect.max.y));
                let color = Color32::DARK_GRAY.linear_multiply(0.5);
                ui.painter().rect(drag_rect, 0.0, color, Stroke::NONE);

                drag_interval = Some(interval);
            } else if response.drag_stopped() {
                // Only set view interval if the drag was a certain amount
                const MIN_DRAG_DISTANCE: f32 = 4.0;
                if max - min > MIN_DRAG_DISTANCE {
                    ProfApp::zoom(cx, interval);
                }

                cx.drag_origin = None;
            }
        }

        // Handle hover detection
        if let Some(hover) = response.hover_pos() {
            let visuals = ui.style().interact_selectable(&response, false);

            // Draw vertical line through cursor
            const RADIUS: f32 = 12.0;
            let top = Pos2::new(hover.x, ui.min_rect().min.y);
            let mid_top = Pos2::new(hover.x, (hover.y - RADIUS).at_least(ui.min_rect().min.y));
            let mid_bottom = Pos2::new(hover.x, (hover.y + RADIUS).at_most(ui.min_rect().max.y));
            let bottom = Pos2::new(hover.x, ui.min_rect().max.y);
            ui.painter().line_segment([top, mid_top], visuals.fg_stroke);
            ui.painter()
                .line_segment([mid_bottom, bottom], visuals.fg_stroke);

            // Show timestamp popup

            const HOVER_PADDING: f32 = 8.0;
            let time = (hover.x - rect.left()) / rect.width();
            let time = cx.view_interval.lerp(time);

            let label_text = if let Some(drag) = drag_interval {
                format!("{drag}")
            } else {
                let units: TimestampUnits = cx.view_interval.into();
                let time_units = TimestampDisplay {
                    timestamp: time,
                    units,
                    include_units: true,
                };
                format!("t={time_units}")
            };

            let label_size = {
                let label_margin = ui.spacing().window_margin;
                let available_width = ui.available_width() - 2.0 * label_margin.sum().x;
                let label_text: egui::WidgetText = (&label_text).into();
                let label_text =
                    label_text.into_galley(ui, None, available_width, egui::TextStyle::Body);
                label_text.size() + 2.0 * label_margin.sum()
            };

            // Hack: This avoids an issue where popups displayed normally are
            // forced to stack, even when an explicit position is
            // requested. Instead we display the popup manually via black magic
            let popup_size = label_size.x;
            let mut popup_rect = Rect::from_min_size(
                Pos2::new(top.x + HOVER_PADDING, top.y),
                Vec2::new(popup_size, 100.0),
            );
            // This is a hack to keep the time viewer on the screen when we
            // approach the right edge.
            if popup_rect.right() > ui.min_rect().right() {
                popup_rect = popup_rect
                    .translate(Vec2::new(ui.min_rect().right() - popup_rect.right(), 0.0));
            }
            let mut popup_ui = egui::Ui::new(
                ui.ctx().clone(),
                ui.layer_id(),
                ui.id(),
                popup_rect,
                popup_rect.expand(16.0),
                ui.stack().info.clone(),
            );
            egui::Frame::popup(ui.style()).show(&mut popup_ui, |ui| {
                ui.label(label_text);
            });
        }
    }

    fn display_controls(ui: &mut egui::Ui, mode: &mut ItemLinkNavigationMode) {
        fn show_row_ui(
            body: &mut egui_extras::TableBody<'_>,
            label: &str,
            thunk: impl FnMut(&mut egui::Ui),
        ) {
            body.row(20.0, |mut row| {
                row.col(|ui| {
                    ui.strong(label);
                });
                row.col(thunk);
            });
        }

        TableBuilder::new(ui)
            .striped(true)
            .cell_layout(egui::Layout::left_to_right(egui::Align::Center))
            .column(Column::auto())
            .column(Column::remainder())
            .body(|mut body| {
                let mut show_row = |a, b| {
                    show_row_ui(&mut body, a, |ui| {
                        ui.label(b);
                    });
                };
                show_row("Zoom to Interval", "Click and Drag");
                show_row("Pan 5%", "Left/Right Arrow");
                show_row("Pan 1%", "Shift + Left/Right Arrow");
                show_row("Vertical Scroll", "Up/Down Arrow");
                show_row("Fine Vertical Scroll", "Shift + Up/Down Arrow");
                show_row("Zoom In", "Ctrl + Plus/Equals");
                show_row("Zoom Out", "Ctrl + Minus");
                show_row("Undo Pan/Zoom", "Ctrl + Left Arrow");
                show_row("Redo Pan/Zoom", "Ctrl + Right Arrow");
                show_row("Reset Pan/Zoom", "Ctrl + 0");
                show_row("Expand Vertical Spacing", "Ctrl + Alt + Plus/Equals");
                show_row("Shrink Vertical Spacing", "Ctrl + Alt + Minus");
                show_row("Reset Vertical Spacing", "Ctrl + Alt + 0");
                show_row("Toggle This Window", "H");
                show_row_ui(&mut body, "Item Link Zoom or Pan", |ui: &mut _| {
                    egui::ComboBox::from_id_source("Item Link Zoom or Pan")
                        .selected_text(format!("{:?}", mode))
                        .show_ui(ui, |ui| {
                            ui.selectable_value(mode, ItemLinkNavigationMode::Zoom, "Zoom");
                            ui.selectable_value(mode, ItemLinkNavigationMode::Pan, "Pan");
                        });
                });
            });
    }

    fn compute_text_height(text: String, width: f32, ui: &mut egui::Ui) -> f32 {
        let style = ui.style();
        let font_id = TextStyle::Body.resolve(style);
        let visuals = style.noninteractive();
        let layout = ui
            .painter()
            .layout(text, font_id, visuals.text_color(), width);

        layout.size().y + style.spacing.item_spacing.y * 2.0
    }

    fn render_field_as_text(
        field: &Field,
        mode: ItemLinkNavigationMode,
    ) -> Vec<(String, Option<&'static str>)> {
        match field {
            Field::I64(value) => vec![(format!("{value}"), None)],
            Field::U64(value) => vec![(format!("{value}"), None)],
            Field::String(value) => vec![(value.to_string(), None)],
            Field::Interval(value) => vec![(format!("{value}"), None)],
            Field::ItemLink(ItemLink { title, .. }) => {
                vec![(title.to_string(), Some(mode.label_text()))]
            }
            Field::Vec(fields) => fields
                .iter()
                .flat_map(|f| Self::render_field_as_text(f, mode))
                .collect(),
            Field::Empty => vec![("".to_string(), None)],
        }
    }

    fn compute_field_height(
        field: &Field,
        width: f32,
        mode: ItemLinkNavigationMode,
        ui: &mut egui::Ui,
    ) -> f32 {
        let text = Self::render_field_as_text(field, mode);
        text.into_iter()
            .map(|(mut v, b)| {
                // Hack: if we have button text, guess how much space it will need
                // by extending the string.
                if let Some(b) = b {
                    v.push(' ');
                    v.push_str(b);
                }
                Self::compute_text_height(v, width, ui)
            })
            .sum()
    }

    fn render_field_as_ui(
        field: &Field,
        color: Option<Color32>,
        mode: ItemLinkNavigationMode,
        ui: &mut egui::Ui,
    ) -> Option<(ItemLocator, Interval)> {
        let mut result = None;
        let label = |ui: &mut egui::Ui, v| {
            if let Some(color) = color {
                ui.add(
                    egui::Label::new(RichText::new(v).color(color)).wrap_mode(TextWrapMode::Wrap),
                );
            } else {
                ui.add(egui::Label::new(v).wrap_mode(TextWrapMode::Wrap));
            }
        };
        let label_button = |ui: &mut egui::Ui, v, b| {
            label(ui, v);
            if let Some(color) = color {
                ui.button(RichText::new(b).color(color)).clicked()
            } else {
                ui.button(b).clicked()
            }
        };
        match field {
            Field::I64(value) => label(ui, &format!("{value}")),
            Field::U64(value) => label(ui, &format!("{value}")),
            Field::String(value) => label(ui, value),
            Field::Interval(value) => label(ui, &format!("{value}")),
            Field::ItemLink(ItemLink {
                title,
                item_uid,
                interval,
                entry_id,
            }) => {
                if label_button(ui, title, mode.label_text()) {
                    result = Some((
                        ItemLocator {
                            entry_id: entry_id.clone(),
                            irow: None,
                            item_uid: *item_uid,
                        },
                        *interval,
                    ));
                }
            }
            Field::Vec(fields) => {
                ui.vertical(|ui| {
                    for f in fields {
                        ui.horizontal(|ui| {
                            if let Some(x) = Self::render_field_as_ui(f, color, mode, ui) {
                                result = Some(x);
                            }
                        });
                    }
                });
            }
            Field::Empty => {}
        }
        result
    }

    fn display_item_details(
        ui: &mut egui::Ui,
        item: &ItemDetail,
        field_schema: &FieldSchema,
        cx: &Context,
    ) -> Option<(ItemLocator, Interval)> {
        let Some(ref item_meta) = item.meta else {
            ui.with_layout(egui::Layout::top_down(egui::Align::Center), |ui| {
                ui.label("Item will be displayed once data is available.");
            });
            return None;
        };

        let font_id = TextStyle::Body.resolve(ui.style());
        let row_height = ui.fonts(|f| f.row_height(&font_id));

        let mut result: Option<(ItemLocator, Interval)> = None;
        TableBuilder::new(ui)
            .striped(true)
            .cell_layout(egui::Layout::left_to_right(egui::Align::Center))
            .column(Column::auto())
            .column(Column::remainder())
            .body(|mut body| {
                let mut show_row = |k: &str, field: &Field, color: Option<Color32>| {
                    // We need to manually work out the height of the labels
                    // so that the table knows how large to make each row.
                    let width = body.widths()[1];

                    let ui = body.ui_mut();
                    let height = Self::compute_field_height(field, width, cx.item_link_mode, ui)
                        .max(row_height);

                    body.row(height, |mut row| {
                        row.col(|ui| {
                            if let Some(color) = color {
                                ui.label(RichText::new(k).color(color).strong());
                            } else {
                                ui.strong(k);
                            }
                        });
                        row.col(|ui| {
                            if let Some(x) =
                                Self::render_field_as_ui(field, color, cx.item_link_mode, ui)
                            {
                                result = Some(x);
                            }
                        });
                    });
                };

                show_row("Title", &Field::String(item_meta.title.to_string()), None);
                if cx.debug {
                    show_row("Item UID", &Field::U64(item_meta.item_uid.0), None);
                }
                for (field_id, field, color) in &item_meta.fields {
                    let name = field_schema.get_name(*field_id).unwrap();
                    show_row(name, field, *color);
                }
            });
        ui.with_layout(egui::Layout::top_down(egui::Align::Center), |ui| {
            if ui.button(cx.item_link_mode.label_text()).clicked() {
                result = Some((item.loc.clone(), item_meta.original_interval));
            }
        });
        result
    }
}

impl eframe::App for ProfApp {
    /// Called to save state before shutdown.
    fn save(&mut self, storage: &mut dyn eframe::Storage) {
        eframe::set_value(storage, eframe::APP_KEY, self);
    }

    /// Called each time the UI needs repainting.
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        let Self {
            pending_data_sources,
            windows,
            cx,
            #[cfg(not(target_arch = "wasm32"))]
            last_update,
            ..
        } = self;

        if let Some(mut source) = pending_data_sources.pop_front() {
            // We made one request, so we know there is always zero or one
            // elements in this list.
            if let Some(info) = source.get_infos().pop() {
                let window = Window::new(source, info, windows.len() as u64);
                if windows.is_empty() {
                    cx.total_interval = window.config.interval;
                } else {
                    cx.total_interval = cx.total_interval.union(window.config.interval);
                }
                ProfApp::zoom(cx, cx.total_interval);
                windows.push(window);
            } else {
                pending_data_sources.push_front(source);
            }
        }

        for window in windows.iter_mut() {
            for (tile, req) in window.config.data_source.get_summary_tiles() {
                if let Some(entry) = window.find_summary_mut(&req.entry_id) {
                    // If the entry doesn't exist, we already zoomed away and
                    // are no longer interested in this tile.
                    entry
                        .tiles
                        .entry(req.tile_id)
                        .and_modify(|t| *t = Some(tile.map(|s| s.data)));
                }
            }

            for (tile, req) in window.config.data_source.get_slot_tiles() {
                if let Some(entry) = window.find_slot_mut(&req.entry_id) {
                    // If the entry doesn't exist, we already zoomed away and
                    // are no longer interested in this tile.
                    entry
                        .tiles
                        .entry(req.tile_id)
                        .and_modify(|t| *t = Some(tile.map(|s| s.data)));
                }
            }

            for (tile, req) in window.config.data_source.get_slot_meta_tiles() {
                if let Some(entry) = window.find_slot_mut(&req.entry_id) {
                    // If the entry doesn't exist, we already zoomed away and
                    // are no longer interested in this tile.
                    let metas = if req.full {
                        &mut entry.tile_metas_full
                    } else {
                        &mut entry.tile_metas
                    };
                    metas
                        .entry(req.tile_id)
                        .and_modify(|t| *t = Some(tile.map(|s| s.data)));
                }
            }
        }

        let mut _fps = 0.0;
        #[cfg(not(target_arch = "wasm32"))]
        {
            let now = Instant::now();
            if let Some(last) = last_update {
                _fps = 1.0 / now.duration_since(*last).as_secs_f64();
            }
            *last_update = Some(now);
        }

        #[cfg(not(target_arch = "wasm32"))]
        egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
            egui::menu::bar(ui, |ui| {
                ui.menu_button("File", |ui| {
                    if ui.button("Quit").clicked() {
                        ctx.send_viewport_cmd(egui::ViewportCommand::Close);
                    }
                });
            });
        });

        egui::SidePanel::left("side_panel").show(ctx, |ui| {
            let body = TextStyle::Body.resolve(ui.style()).size;
            let heading = TextStyle::Heading.resolve(ui.style()).size;
            // Just set this on every frame for now
            cx.subheading_size = (heading + body) * 0.5;

            const WIDGET_PADDING: f32 = 8.0;
            ui.add_space(WIDGET_PADDING);

            for window in windows.iter_mut() {
                egui::Frame::group(ui.style()).show(ui, |ui| {
                    ui.set_width(ui.available_width());
                    window.controls(ui, cx);
                });
            }

            for window in windows.iter_mut() {
                egui::Frame::group(ui.style()).show(ui, |ui| {
                    ui.set_width(ui.available_width());
                    window.search_controls(ui, cx);
                });
            }

            ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
                ui.horizontal(|ui| {
                    ui.spacing_mut().item_spacing.x = 0.0;
                    ui.label("powered by ");
                    ui.hyperlink_to("egui", "https://github.com/emilk/egui");
                    ui.label(" and ");
                    ui.hyperlink_to(
                        "eframe",
                        "https://github.com/emilk/egui/tree/master/crates/eframe",
                    );
                    ui.label(".");
                });

                ui.horizontal(|ui| {
                    let mut current_theme = if cx.toggle_dark_mode {
                        egui::Visuals::dark()
                    } else {
                        egui::Visuals::light()
                    };

                    current_theme.light_dark_radio_buttons(ui);
                    if current_theme.dark_mode != cx.toggle_dark_mode {
                        cx.toggle_dark_mode = current_theme.dark_mode;
                        ctx.set_visuals(current_theme);
                    }

                    ui.toggle_value(&mut cx.debug, "🛠 Debug");
                });

                ui.horizontal(|ui| {
                    if ui.button("Show Controls").clicked() {
                        cx.show_controls = true;
                    }

                    #[cfg(not(target_arch = "wasm32"))]
                    {
                        if cx.debug {
                            ui.label(format!("FPS: {_fps:.0}"));
                        }
                    }
                });

                ui.separator();
                egui::warn_if_debug_build(ui);
            });
        });

        egui::CentralPanel::default().show(ctx, |ui| {
            // Use body font to figure out how tall to draw rectangles.
            let font_id = TextStyle::Body.resolve(ui.style());
            let row_height = ui.fonts(|f| f.row_height(&font_id));
            // Just set this on every frame for now
            cx.row_height = row_height * cx.scale_factor;

            let y_scroll_delta = cx.row_height * cx.row_scroll_delta as f32;
            ui.scroll_with_delta(Vec2::new(0.0, y_scroll_delta));
            cx.row_scroll_delta = 0;

            let mut remaining = windows.len();
            // Only wrap in a frame if more than one profile
            if remaining > 1 {
                for window in windows.iter_mut() {
                    egui::Frame::group(ui.style()).show(ui, |ui| {
                        ui.push_id(window.index, |ui| {
                            ui.set_height(ui.available_height() / (remaining as f32));
                            ui.set_width(ui.available_width());
                            window.content(ui, cx);
                            remaining -= 1;
                        });
                    });
                }
            } else {
                for window in windows.iter_mut() {
                    window.content(ui, cx);
                }
            }

            Self::cursor(ui, cx);
        });

        egui::Window::new("Controls")
            .open(&mut cx.show_controls)
            .resizable(false)
            .show(ctx, |ui| Self::display_controls(ui, &mut cx.item_link_mode));

        for window in windows.iter_mut() {
            let mut zoom_target = None;

            // Hack: work around mutability conflict
            let mut items_selected = BTreeMap::new();
            std::mem::swap(&mut items_selected, &mut window.config.items_selected);
            items_selected.retain(|_, item| {
                // Populate the item meta if it's not already there
                if item.meta.is_none() {
                    window.inflate_meta(&item.loc.entry_id, cx);
                    if let Some(meta) = window.find_item_meta(&item.loc.entry_id, item.loc.item_uid)
                    {
                        item.meta = Some(meta.clone());
                    }
                }

                let short_title = match &item.meta {
                    Some(meta) => meta.title.chars().take(50).collect(),
                    None => format!("Item <Item UID: {}>", item.loc.item_uid.0),
                };

                let mut enabled = true;
                egui::Window::new(short_title)
                    .id(egui::Id::new(item.loc.item_uid.0))
                    .open(&mut enabled)
                    .resizable(true)
                    .show(ctx, |ui| {
                        let target =
                            Self::display_item_details(ui, item, &window.config.field_schema, cx);
                        if target.is_some() {
                            zoom_target = target;
                        }
                    });
                enabled
            });
            std::mem::swap(&mut items_selected, &mut window.config.items_selected);

            if let Some((item_loc, interval)) = zoom_target {
                let interval = match cx.item_link_mode {
                    // In Zoom mode, put the item in the center of the view
                    // interval with a small amount of padding on either side.
                    ItemLinkNavigationMode::Zoom => interval.grow(interval.duration_ns() / 20),
                    // In Pan mode, maintain the current window size but shift
                    // the center to place the item in the middle of it.
                    ItemLinkNavigationMode::Pan => cx
                        .view_interval
                        .translate(interval.center().0 - cx.view_interval.center().0),
                };
                ProfApp::zoom(cx, interval);
                window.expand_slot(&item_loc.entry_id);
                window.config.scroll_to_item(item_loc);
            }
        }

        Self::keyboard(ctx, cx, windows);

        // Keep repainting as long as we have outstanding requests.
        if !pending_data_sources.is_empty()
            || windows
                .iter()
                .any(|w| w.config.data_source.outstanding_requests() > 0)
        {
            ctx.request_repaint_after(Duration::from_millis(50));
        }
    }
}

trait UiExtra {
    fn subheading(&mut self, text: impl Into<egui::RichText>, cx: &Context) -> egui::Response;
    fn show_tooltip(
        &mut self,
        id_source: impl core::hash::Hash,
        rect: &Rect,
        text: impl Into<egui::WidgetText>,
    );
    fn show_tooltip_ui(
        &mut self,
        id_source: impl core::hash::Hash,
        rect: &Rect,
        add_contents: impl FnOnce(&mut egui::Ui),
    );
    fn rect_hover_pos(&self, rect: Rect) -> Option<Pos2>;
}

impl UiExtra for egui::Ui {
    fn subheading(&mut self, text: impl Into<egui::RichText>, cx: &Context) -> egui::Response {
        self.add(egui::Label::new(
            text.into().heading().size(cx.subheading_size),
        ))
    }

    /// This is a method for showing a fast, very responsive
    /// tooltip. The standard hover methods force a delay (presumably
    /// to confirm the mouse has stopped), this bypasses that. Best
    /// used in situations where the user might quickly skim over the
    /// content (e.g., utilization plots).
    fn show_tooltip(
        &mut self,
        id_source: impl core::hash::Hash,
        rect: &Rect,
        text: impl Into<egui::WidgetText>,
    ) {
        self.show_tooltip_ui(id_source, rect, |ui| {
            ui.add(egui::Label::new(text));
        });
    }
    fn show_tooltip_ui(
        &mut self,
        id_source: impl core::hash::Hash,
        rect: &Rect,
        add_contents: impl FnOnce(&mut egui::Ui),
    ) {
        egui::containers::show_tooltip_for(
            self.ctx(),
            self.layer_id(),
            self.auto_id_with(id_source),
            rect,
            add_contents,
        );
    }

    /// Starting with egui 0.26, ui.allocate_rect() never returns interactions
    /// for multiple overlapping rectangles at once. This method is required to
    /// interact for more complex widgets where we want to e.g., hover one
    /// widget while dragging another widget (where those widgets overlap).
    fn rect_hover_pos(&self, rect: Rect) -> Option<Pos2> {
        if self.rect_contains_pointer(rect) {
            self.input(|i| i.pointer.hover_pos())
        } else {
            None
        }
    }
}

#[cfg(not(target_arch = "wasm32"))]
fn get_locator(data_sources: &[Box<dyn DeferredDataSource>]) -> String {
    let all_locators = data_sources
        .iter()
        .flat_map(|x| x.fetch_description().source_locator)
        .collect::<Vec<_>>();

    let unique_locators = all_locators.into_iter().unique().collect_vec();

    match &unique_locators[..] {
        [] => "No data source".to_string(),
        [x] => x.to_string(),
        [x, ..] => format!("{} and {} other sources", x, unique_locators.len() - 1),
    }
}

#[cfg(not(target_arch = "wasm32"))]
pub fn start(data_sources: Vec<Box<dyn DeferredDataSource>>) {
    env_logger::try_init().unwrap_or(()); // Log to stderr (if you run with `RUST_LOG=debug`).

    // IMPORTANT: This will be used as the directory name for the storage
    // location for the persisted app.ron configuration. eframe is not good
    // about sanitizing these directory names, so it is VERY IMPORTANT that
    // this be a short, predictable name without weird characters in it.
    let app_name = "Legion Prof";

    // This is what will be displayed as the window's actual title.
    let locator = format!("{} - {}", get_locator(&data_sources), app_name);

    let native_options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default().with_title(locator),
        ..Default::default()
    };
    eframe::run_native(
        app_name,
        native_options,
        Box::new(|cc| Ok(Box::new(ProfApp::new(cc, data_sources)))),
    )
    .expect("failed to start eframe");
}

#[cfg(target_arch = "wasm32")]
pub fn start(data_sources: Vec<Box<dyn DeferredDataSource>>) {
    // Redirect `log` message to `console.log` and friends:
    eframe::WebLogger::init(log::LevelFilter::Debug).ok();

    let web_options = eframe::WebOptions::default();

    wasm_bindgen_futures::spawn_local(async {
        let start_result = eframe::WebRunner::new()
            .start(
                "the_canvas_id",
                web_options,
                Box::new(|cc| Ok(Box::new(ProfApp::new(cc, data_sources)))),
            )
            .await;

        // Remove the loading text and spinner:
        let loading_text = web_sys::window()
            .and_then(|w| w.document())
            .and_then(|d| d.get_element_by_id("loading_text"));
        if let Some(loading_text) = loading_text {
            match start_result {
                Ok(_) => {
                    loading_text.remove();
                }
                Err(e) => {
                    loading_text.set_inner_html(
                        "<p> The app has crashed. See the developer console for details. </p>",
                    );
                    panic!("Failed to start eframe: {e:?}");
                }
            }
        }
    });
}