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
use crate::prelude::*;
use std::any::Any;
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
use std::sync::Arc;
use super::{
adaptive_pool::AdaptiveItemPool,
error::{IntoVirtualListError, VirtualListError, VirtualListResult},
item_context::ItemContext,
};
// Global registry for panel-to-context mapping removed
// TASK 3.1: Global registry removed - replaced with instance-owned registry for memory safety
// PHASE 3: ItemContext moved to item_context.rs module for better code organization
/// Trait for providing data to the virtual list
pub trait VirtualListDataSource: Send + Sync {
/// Get the total number of items
fn get_item_count(&self) -> usize;
/// Get the data for a specific item
fn get_item_data(&self, index: usize) -> Box<dyn Any + Send + Sync>;
}
/// Trait for rendering items in the virtual list
pub trait VirtualListItemRenderer: Send + Sync {
/// Create a new panel for rendering an item
fn create_item(&self, parent: &Panel) -> Panel;
/// Update the content of a panel for a specific item with its data
fn update_item(&self, panel: &Panel, index: usize, data: &dyn Any);
}
/// Layout modes for the virtual list
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VirtualListLayoutMode {
/// Items arranged vertically (rows), each item stretches to container width
Vertical,
/// Items arranged horizontally (columns), each item stretches to container height
Horizontal,
}
/// Item sizing behavior for cache invalidation strategy
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ItemSizingMode {
/// Items have fixed size regardless of container width (images, fixed text, etc.)
/// Cache entries are preserved when width changes for better performance
FixedSize,
/// Items resize based on container width (text wrapping, responsive layouts, etc.)
/// Cache entries are invalidated when width changes to ensure correct layout
DynamicSize,
}
/// PHASE 1.2: Measurement confidence system for layout stability
#[derive(Debug, Clone, Copy)]
enum MeasurementState {
/// Actually measured but not yet validated for stability
Measured {
value: i32,
validated: bool, // Has this measurement been confirmed across multiple cycles?
},
/// Measurement verified as stable across multiple measurement cycles
Verified {
value: i32,
stable_count: u8, // How many times this measurement has been confirmed
},
}
impl MeasurementState {
/// Upgrade measurement state with new information
fn upgrade_with_measurement(self, new_value: i32, tolerance: i32) -> Self {
match self {
MeasurementState::Measured {
value: old_value,
validated,
} => {
let is_stable = (old_value - new_value).abs() <= tolerance;
if is_stable && validated {
// Promote to verified
MeasurementState::Verified {
value: new_value,
stable_count: 1,
}
} else if is_stable {
// Mark as validated
MeasurementState::Measured {
value: new_value,
validated: true,
}
} else {
// New measurement differs significantly - start over
MeasurementState::Measured {
value: new_value,
validated: false,
}
}
}
MeasurementState::Verified {
value: old_value,
stable_count,
} => {
let is_stable = (old_value - new_value).abs() <= tolerance;
if is_stable {
// Increase stability count
MeasurementState::Verified {
value: new_value,
stable_count: (stable_count + 1).min(10), // Cap at 10
}
} else {
// Measurement changed significantly - demote to measured
MeasurementState::Measured {
value: new_value,
validated: false,
}
}
}
}
}
}
/// Enhanced cache entry with measurement confidence system
#[derive(Debug, Clone, Copy)]
struct EnhancedCacheEntry {
size: Size,
measurement_state: MeasurementState,
measured_at_dimension: i32,
}
/// PHASE 1.3: LRU-managed size cache with automatic eviction
#[derive(Debug, Clone)]
struct SizeCache {
/// Cache entries keyed by item index
entries: HashMap<usize, EnhancedCacheEntry>,
/// LRU access order: most recently used at back, least recently used at front
access_order: std::collections::VecDeque<usize>,
/// Maximum number of entries to keep in cache
max_size: usize,
/// Current generation for cleanup purposes
generation: u64,
}
impl SizeCache {
/// Create a new size cache with specified maximum size
fn new(max_size: usize) -> Self {
Self {
entries: HashMap::new(),
access_order: std::collections::VecDeque::new(),
max_size,
generation: 0,
}
}
/// Insert or update an entry, handling LRU eviction automatically
fn insert(&mut self, index: usize, entry: EnhancedCacheEntry) {
// If entry exists, remove it from access order first
if self.entries.contains_key(&index) {
self.access_order.retain(|&x| x != index);
}
// Insert/update the entry
self.entries.insert(index, entry);
self.access_order.push_back(index);
// Evict oldest entries if we exceed max size
while self.entries.len() > self.max_size {
self.evict_oldest();
}
}
/// Get an entry and mark it as recently used
fn get(&mut self, index: usize) -> Option<&EnhancedCacheEntry> {
if self.entries.contains_key(&index) {
// Move to end of access order (most recently used)
self.access_order.retain(|&x| x != index);
self.access_order.push_back(index);
self.entries.get(&index)
} else {
None
}
}
/// Get an entry without updating access order (for read-only operations)
fn peek(&self, index: usize) -> Option<&EnhancedCacheEntry> {
self.entries.get(&index)
}
/// Remove specific entry
fn remove(&mut self, index: usize) -> Option<EnhancedCacheEntry> {
if let Some(entry) = self.entries.remove(&index) {
self.access_order.retain(|&x| x != index);
Some(entry)
} else {
None
}
}
/// Evict the least recently used entry
fn evict_oldest(&mut self) {
if let Some(oldest_index) = self.access_order.pop_front() {
self.entries.remove(&oldest_index);
}
}
/// Clear all entries
fn clear(&mut self) {
self.entries.clear();
self.access_order.clear();
self.generation += 1;
}
/// Get number of cached entries
fn len(&self) -> usize {
self.entries.len()
}
/// Clean stale entries based on generation threshold
fn clean_stale_entries(&mut self, max_generations_old: u64) {
// For now, we'll use generation-based cleanup
// In the future, this could be enhanced with timestamp-based cleanup
if self.generation > max_generations_old {
let cutoff_generation = self.generation - max_generations_old;
// For simplicity, we'll implement a basic cleanup strategy
// This could be enhanced to track per-entry generations if needed
if cutoff_generation > 10 {
// Clear cache if it's very stale
self.clear();
}
}
}
}
/// PHASE 2.3: Batched Layout Operations System
/// Reduces layout thrashing by collecting and executing panel operations in batches
struct PanelCreateOperation {
index: usize,
panel: Panel,
data: Box<dyn Any + Send + Sync>,
}
struct PanelUpdateOperation {
index: usize,
panel: Panel,
data: Box<dyn Any + Send + Sync>,
new_size: Size,
}
struct PanelPositionOperation {
panel: Panel,
position: Point,
final_size: Size,
}
struct PanelDestroyOperation {
panel: Panel,
}
/// Batch collector for all panel operations to minimize layout thrashing
struct LayoutBatch {
/// Panels to create with initial setup
panels_to_create: Vec<PanelCreateOperation>,
/// Panels to update content and/or size
panels_to_update: Vec<PanelUpdateOperation>,
/// Panels to position and show
panels_to_position: Vec<PanelPositionOperation>,
/// Panels to hide and return to pool
panels_to_destroy: Vec<PanelDestroyOperation>,
/// Whether to defer final layout until batch execution
defer_layout: bool,
}
impl LayoutBatch {
fn new() -> Self {
Self {
panels_to_create: Vec::new(),
panels_to_update: Vec::new(),
panels_to_position: Vec::new(),
panels_to_destroy: Vec::new(),
defer_layout: true,
}
}
/// Add a panel creation operation
fn add_create_operation(&mut self, index: usize, panel: Panel, data: Box<dyn Any + Send + Sync>) {
self.panels_to_create.push(PanelCreateOperation { index, panel, data });
}
/// Add a panel content/size update operation
fn add_update_operation(&mut self, index: usize, panel: Panel, data: Box<dyn Any + Send + Sync>, new_size: Size) {
self.panels_to_update.push(PanelUpdateOperation {
index,
panel,
data,
new_size,
});
}
/// Add a panel positioning operation
fn add_position_operation(&mut self, panel: Panel, position: Point, final_size: Size) {
self.panels_to_position.push(PanelPositionOperation {
panel,
position,
final_size,
});
}
/// Add a panel destruction operation
fn add_destroy_operation(&mut self, panel: Panel) {
self.panels_to_destroy.push(PanelDestroyOperation { panel });
}
/// Execute all batched operations efficiently to minimize layout thrashing
fn execute_batch(
&mut self,
item_renderer: &dyn VirtualListItemRenderer,
state: &mut VirtualListState,
) -> BatchExecutionResult {
let mut created_panels = Vec::new();
let mut updated_measurements = Vec::new();
// PHASE 1: Create and setup new panels (no layout yet)
for create_op in &self.panels_to_create {
// CRITICAL FIX: Set correct initial size based on layout mode
let initial_size = match state.layout_mode {
VirtualListLayoutMode::Vertical => {
Size::new(state.viewport_size.width, state.internal_params.temporary_panel_height)
}
VirtualListLayoutMode::Horizontal => Size::new(
2000, // Give plenty of width for natural text flow measurement
state.viewport_size.height,
),
};
create_op.panel.set_size(initial_size);
// CRITICAL FIX: Hide panel until properly positioned
create_op.panel.show(false);
// Update content
item_renderer.update_item(&create_op.panel, create_op.index, create_op.data.as_ref());
// Store context
state.store_item_context(&create_op.panel, create_op.index, create_op.data.as_ref());
created_panels.push((create_op.index, create_op.panel));
}
// PHASE 2: Update existing panels (no layout yet)
for update_op in &self.panels_to_update {
// Update size first
update_op.panel.set_size(update_op.new_size);
// Update content
item_renderer.update_item(&update_op.panel, update_op.index, update_op.data.as_ref());
updated_measurements.push((update_op.index, update_op.panel));
}
// PHASE 3: Single layout pass for all modified panels
if self.defer_layout {
// Batch layout all created panels
for (_, panel) in &created_panels {
panel.layout();
}
// Batch layout all updated panels
for (_, panel) in &updated_measurements {
panel.layout();
}
}
// PHASE 4: Measure all panels and collect final sizes
let mut final_measurements = Vec::new();
for (index, panel) in &created_panels {
let measured_size = panel.get_best_size();
final_measurements.push((*index, measured_size));
}
for (index, panel) in &updated_measurements {
let measured_size = panel.get_best_size();
final_measurements.push((*index, measured_size));
}
// PHASE 5: Apply final sizes and positions in one batch
for position_op in &self.panels_to_position {
// Apply final size
position_op.panel.set_size(position_op.final_size);
// Apply position
position_op.panel.move_window(position_op.position.x, position_op.position.y);
// Show panel
position_op.panel.show(true);
}
// PHASE 6: Hide and cleanup destroyed panels
for destroy_op in &self.panels_to_destroy {
destroy_op.panel.show(false);
state.remove_panel_context(&destroy_op.panel);
}
// Clear batch for next use
self.panels_to_create.clear();
self.panels_to_update.clear();
self.panels_to_position.clear();
self.panels_to_destroy.clear();
BatchExecutionResult { final_measurements }
}
}
/// Result of executing a layout batch
struct BatchExecutionResult {
/// Final measurements for all processed panels (index, measured_size)
final_measurements: Vec<(usize, Size)>,
}
/// Context for batch execution operations to reduce parameter count
struct BatchExecutionContext<'a> {
items_to_show: &'a [(usize, i32, i32)],
new_visible_items: &'a HashSet<usize>,
current_visible_items: &'a HashSet<usize>,
data_source: &'a Rc<dyn VirtualListDataSource>,
item_renderer: &'a Rc<dyn VirtualListItemRenderer>,
parent: &'a Panel,
scroll_position: i32,
current_viewport_dimension: i32,
dimension_changed: bool,
}
/// Internal state for the virtual list
#[derive(Clone)]
pub struct VirtualListState {
// Configuration
layout_mode: VirtualListLayoutMode,
internal_params: super::config::VirtualListInternalParams,
// Content sources
data_source: Option<Rc<dyn VirtualListDataSource>>,
item_renderer: Option<Rc<dyn VirtualListItemRenderer>>,
// Pooling and layout
item_pool: AdaptiveItemPool,
// Currently visible items (index -> panel)
item_to_panel: HashMap<usize, Panel>,
// PHASE 1.2: Enhanced size caching with measurement confidence system
item_size_cache: SizeCache,
item_sizing_mode: ItemSizingMode,
cache_generation: u64,
// TASK 2.4: Measurement deduplication tracking
current_update_cycle: u64,
measured_in_current_cycle: HashSet<usize>,
// Viewport tracking
viewport_size: Size,
scroll_position: Point,
total_content_size: Size,
visible_range: std::ops::Range<usize>,
// Track viewport width changes to force content re-layout when needed
previous_viewport_width: i32,
// TASK 3.1: Owned panel context registry (replaces global static)
panel_context_registry: HashMap<i32, ItemContext>,
// Re-entrancy guard to avoid handling scroll events triggered by programmatic updates
is_updating_scrollbar: bool,
}
impl std::fmt::Debug for VirtualListState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("VirtualListState")
.field("layout_mode", &self.layout_mode)
.field("data_source", &self.data_source.is_some())
.field("item_renderer", &self.item_renderer.is_some())
.field("item_pool", &self.item_pool)
.field("item_to_panel", &format!("{} items", self.item_to_panel.len()))
.field("item_size_cache", &format!("{} cached sizes", self.item_size_cache.len()))
.field("cache_generation", &self.cache_generation)
.field("viewport_size", &self.viewport_size)
.field("scroll_position", &self.scroll_position)
.field("total_content_size", &self.total_content_size)
.field("visible_range", &self.visible_range)
.finish()
}
}
impl VirtualListState {
/// Store or update a measurement with progressive enhancement
fn store_progressive_measurement(&mut self, index: usize, measured_size: Size, current_dimension: i32) {
let size_value = match self.layout_mode {
VirtualListLayoutMode::Vertical => measured_size.height,
VirtualListLayoutMode::Horizontal => measured_size.width,
};
let tolerance = self.internal_params.measurement_tolerance;
let measurement_state = if let Some(existing_entry) = self.item_size_cache.peek(index) {
// Upgrade existing measurement
existing_entry
.measurement_state
.upgrade_with_measurement(size_value, tolerance)
} else {
// New measurement - start as unvalidated
MeasurementState::Measured {
value: size_value,
validated: false,
}
};
let enhanced_entry = EnhancedCacheEntry {
size: measured_size,
measurement_state,
measured_at_dimension: current_dimension,
};
self.item_size_cache.insert(index, enhanced_entry);
}
fn new(
layout_mode: VirtualListLayoutMode,
scrollbar_size: Option<i32>,
keyboard_scroll_amount: Option<i32>,
mouse_wheel_multiplier: f32,
) -> Self {
// Create intelligent internal params that auto-configure based on default viewport
let internal_params = super::config::VirtualListInternalParams::new_for_viewport(
(800, 600), // Default viewport for initialization
scrollbar_size,
keyboard_scroll_amount,
mouse_wheel_multiplier,
);
Self {
layout_mode,
internal_params: internal_params.clone(),
data_source: None,
item_renderer: None,
item_pool: AdaptiveItemPool::new(layout_mode, &internal_params),
item_to_panel: HashMap::new(),
item_size_cache: SizeCache::new(100),
item_sizing_mode: ItemSizingMode::DynamicSize, // Default to dynamic sizing for safety
cache_generation: 0,
// TASK 2.4: Initialize measurement deduplication tracking
current_update_cycle: 0,
measured_in_current_cycle: HashSet::new(),
viewport_size: Size::new(0, 0),
scroll_position: Point::new(0, 0),
total_content_size: Size::new(0, 0),
visible_range: 0..0,
previous_viewport_width: 0,
// TASK 3.1: Initialize owned panel context registry
panel_context_registry: HashMap::new(),
// Re-entrancy guard initial state
is_updating_scrollbar: false,
}
}
fn set_data_source(&mut self, data_source: Rc<dyn VirtualListDataSource>) {
self.data_source = Some(data_source);
// Clear entire cache since data source changed completely
self.item_size_cache.clear();
}
fn set_item_renderer(&mut self, item_renderer: Rc<dyn VirtualListItemRenderer>) {
self.item_renderer = Some(item_renderer);
// Clear entire cache since item renderer changed completely
self.item_size_cache.clear();
}
fn invalidate_layout(&mut self) {
// Clear current visible items
self.hide_all_items();
// Intelligently invalidate cache based on item sizing mode
self.selective_cache_invalidation();
// Total content size will be recalculated when items are actually rendered
// in update_visible_items where we have access to the parent panel
}
fn hide_all_items(&mut self) {
// Return all panels to the pool and clean up their context
let panels_to_clean: Vec<Panel> = self.item_to_panel.drain().map(|(_, panel)| panel).collect();
for panel in panels_to_clean {
self.remove_panel_context(&panel);
self.item_pool.return_item(panel);
}
self.visible_range = 0..0;
}
/// PHASE 1.1 REFACTORED: Main update method with focused concerns
/// PHASE 2.3 ENHANCED: Now uses batched layout operations to reduce layout thrashing
/// Complexity reduced from 300+ lines to ~80 lines using focused helper methods + batching
fn update_visible_items(&mut self, parent: &Panel) {
// Early return if we don't have both data source and renderer
let (data_source, item_renderer) = if let (Some(ds), Some(ir)) = (&self.data_source, &self.item_renderer) {
(ds.clone(), ir.clone())
} else {
return;
};
// Update current cycle for measurement deduplication
self.current_update_cycle = self.current_update_cycle.wrapping_add(1);
self.measured_in_current_cycle.clear();
let total_items = data_source.get_item_count();
if total_items == 0 {
self.hide_all_items();
return;
}
// Detect dimension changes for cache invalidation strategy
let current_viewport_dimension = match self.layout_mode {
VirtualListLayoutMode::Vertical => self.viewport_size.width,
VirtualListLayoutMode::Horizontal => self.viewport_size.height,
};
let dimension_changed = current_viewport_dimension != self.previous_viewport_width;
if dimension_changed {
self.previous_viewport_width = current_viewport_dimension;
// Apply intelligent cache invalidation strategy
self.selective_cache_invalidation();
}
// STEP 1: Calculate which items should be visible
let items_to_show = self.extract_visible_range_calculation(&data_source, &item_renderer, parent);
// STEP 2: PHASE 2.3 - Execute all operations using batched layout system
let new_visible_items: HashSet<usize> = items_to_show.iter().map(|(idx, _, _)| *idx).collect();
let current_visible_items: HashSet<usize> = self.item_to_panel.keys().cloned().collect();
// Only update if the set of visible items has changed OR if dimensions changed
if current_visible_items != new_visible_items || dimension_changed {
let scroll_position = match self.layout_mode {
VirtualListLayoutMode::Vertical => self.scroll_position.y,
VirtualListLayoutMode::Horizontal => self.scroll_position.x,
};
// PHASE 2.3: Execute all operations using batched layout system
let context = BatchExecutionContext {
items_to_show: &items_to_show,
new_visible_items: &new_visible_items,
current_visible_items: ¤t_visible_items,
data_source: &data_source,
item_renderer: &item_renderer,
parent,
scroll_position,
current_viewport_dimension,
dimension_changed,
};
self.execute_batched_layout_operations(context);
// CRITICAL FIX: Update scrollbar state AFTER measurements are stored in cache
let estimated_item_size = match self.layout_mode {
VirtualListLayoutMode::Vertical => self.internal_params.estimated_item_height,
VirtualListLayoutMode::Horizontal => self.internal_params.estimated_item_width,
};
self.update_scrollbar_state(total_items, estimated_item_size);
}
}
/// Measure the actual size of an item by creating, updating, and laying out a temporary panel
/// PHASE 2B: Enhanced measurement with deduplication tracking
fn measure_item_size(
&mut self,
index: usize,
data_source: &dyn VirtualListDataSource,
item_renderer: &dyn VirtualListItemRenderer,
parent: &Panel,
) -> Size {
// TASK 2.4: Check if already measured in current cycle
if self.measured_in_current_cycle.contains(&index) {
// Already measured in this cycle - check cache
if let Some(cached_size) = self.item_size_cache.peek(index) {
return cached_size.size;
}
}
// Check cache first (normal cache check)
if let Some(cached_size) = self.item_size_cache.get(index) {
// Mark as measured in current cycle even for cache hits
self.measured_in_current_cycle.insert(index);
return cached_size.size;
}
// Mark as measured in current cycle before performing expensive measurement
self.measured_in_current_cycle.insert(index);
// Create temporary panel for measurement
let temp_panel = item_renderer.create_item(parent);
// CRITICAL: Set proper size BEFORE updating content for accurate measurement
let temp_size = match self.layout_mode {
VirtualListLayoutMode::Vertical => Size::new(self.viewport_size.width, 100), // Fixed width, temporary height
VirtualListLayoutMode::Horizontal => Size::new(self.internal_params.temporary_panel_width, self.viewport_size.height), // Temporary width, fixed height
};
temp_panel.set_size(temp_size);
let item_data = data_source.get_item_data(index);
item_renderer.update_item(&temp_panel, index, item_data.as_ref());
// CRITICAL: Force layout so sizers can do their work
temp_panel.layout();
let measured_size = temp_panel.get_best_size();
// Hide temp panel (cleanup)
temp_panel.show(false);
// Cache the result with layout-aware dimensions
let cached_size = match self.layout_mode {
VirtualListLayoutMode::Vertical => Size::new(self.viewport_size.width, measured_size.height),
VirtualListLayoutMode::Horizontal => Size::new(measured_size.width, self.viewport_size.height),
};
let current_dimension = match self.layout_mode {
VirtualListLayoutMode::Vertical => self.viewport_size.width,
VirtualListLayoutMode::Horizontal => self.viewport_size.height,
};
// PHASE 1.2: Use progressive measurement system
self.store_progressive_measurement(index, cached_size, current_dimension);
cached_size
}
/// PHASE 2B: Measure an item that is about to become visible with deduplication
fn measure_item_size_for_visible(
&mut self,
index: usize,
data_source: &dyn VirtualListDataSource,
item_renderer: &dyn VirtualListItemRenderer,
parent: &Panel,
) -> Size {
// TASK 2.4: Check if already measured in current cycle
if self.measured_in_current_cycle.contains(&index) {
// Already measured in this cycle - return cached result
if let Some(cached_size) = self.item_size_cache.peek(index) {
return cached_size.size;
}
}
// Mark as measured in current cycle before measuring
self.measured_in_current_cycle.insert(index);
// Perform measurement (same as measure_item_size but with deduplication tracking)
self.measure_item_size(index, data_source, item_renderer, parent)
}
/// Calculate total content size using progressive measurement approach
fn calculate_total_content_size_progressive(&self, total_items: usize, estimated_item_size: i32) -> Size {
let total_size = match self.item_sizing_mode {
ItemSizingMode::FixedSize => {
// For fixed size items, use pure estimation without measurements
// This ensures consistent size calculation regardless of which items have been measured
(total_items as i32) * estimated_item_size
}
ItemSizingMode::DynamicSize => {
// For dynamic size items, use mix of measurements and estimates
let mut size = 0;
for index in 0..total_items {
let item_size = if let Some(cached_size) = self.item_size_cache.peek(index) {
// Use actual measurement if we have it
match self.layout_mode {
VirtualListLayoutMode::Vertical => cached_size.size.height,
VirtualListLayoutMode::Horizontal => cached_size.size.width,
}
} else {
// Use estimate for non-measured items
estimated_item_size
};
size += item_size;
}
size
}
};
match self.layout_mode {
VirtualListLayoutMode::Vertical => Size::new(self.viewport_size.width, total_size),
VirtualListLayoutMode::Horizontal => Size::new(total_size, self.viewport_size.height),
}
}
// ===============================================
// PHASE 1.1 REFACTORING: Focused Update Methods
// ===============================================
/// Calculate which items should be visible based on scroll position and viewport
fn extract_visible_range_calculation(
&mut self,
data_source: &Rc<dyn VirtualListDataSource>,
item_renderer: &Rc<dyn VirtualListItemRenderer>,
parent: &Panel,
) -> Vec<(usize, i32, i32)> {
let total_items = data_source.get_item_count();
// Layout mode specific variables
let (scroll_position, viewport_length, estimated_item_size) = match self.layout_mode {
VirtualListLayoutMode::Vertical => (
self.scroll_position.y,
self.viewport_size.height,
self.internal_params.estimated_item_height,
),
VirtualListLayoutMode::Horizontal => (
self.scroll_position.x,
self.viewport_size.width,
self.internal_params.estimated_item_width,
),
};
// CRITICAL FIX: Two-pass approach for consistent position calculation
// PASS 1: Determine actual visible items without creating measurement inconsistencies
let mut current_position = 0;
let mut potentially_visible_items: Vec<usize> = Vec::new();
// Use cache-first approach: prioritize existing measurements for consistency
for index in 0..total_items {
let item_size = if let Some(cached_size) = self.item_size_cache.peek(index) {
// Use cached measurement - most consistent
match self.layout_mode {
VirtualListLayoutMode::Vertical => cached_size.size.height,
VirtualListLayoutMode::Horizontal => cached_size.size.width,
}
} else {
// Use estimate for all uncached items to avoid position shifts
estimated_item_size
};
// Check if this item is potentially visible
let item_start = current_position;
let item_end = current_position + item_size;
if item_end > scroll_position && item_start < scroll_position + viewport_length {
potentially_visible_items.push(index);
}
current_position += item_size;
// Early termination: stop well past the visible area
// CRITICAL FIX: Don't early terminate when near the end of content to ensure last items are considered
let is_near_end = index >= total_items.saturating_sub(5); // Always process last 5 items
if !is_near_end
&& current_position > scroll_position + viewport_length + self.internal_params.early_termination_threshold
{
break;
}
}
// PASS 2: Measure only the confirmed visible items to avoid position inconsistencies
let mut current_position = 0;
let mut items_to_show: Vec<(usize, i32, i32)> = Vec::new();
for index in 0..total_items {
let item_size = if let Some(cached_size) = self.item_size_cache.peek(index) {
// Use cached measurement
match self.layout_mode {
VirtualListLayoutMode::Vertical => cached_size.size.height,
VirtualListLayoutMode::Horizontal => cached_size.size.width,
}
} else if potentially_visible_items.contains(&index) {
// Only measure items that are confirmed to be visible
let size = self.measure_item_size_for_visible(index, data_source.as_ref(), item_renderer.as_ref(), parent);
match self.layout_mode {
VirtualListLayoutMode::Vertical => size.height,
VirtualListLayoutMode::Horizontal => size.width,
}
} else {
// Use estimate for all other items
estimated_item_size
};
// Check if this item is visible
let item_start = current_position;
let item_end = current_position + item_size;
if item_end > scroll_position && item_start < scroll_position + viewport_length {
items_to_show.push((index, item_start, item_size));
}
current_position += item_size;
// Early termination: stop well past the visible area
// CRITICAL FIX: Don't early terminate when near the end of content to ensure last items are considered
let is_near_end = index >= total_items.saturating_sub(5); // Always process last 5 items
if !is_near_end
&& current_position > scroll_position + viewport_length + self.internal_params.early_termination_threshold
{
break;
}
}
items_to_show
}
/// Update total content size and scrollbar state
fn update_scrollbar_state(&mut self, total_items: usize, estimated_item_size: i32) {
// Update total content size using mix of actual + estimated
self.total_content_size = self.calculate_total_content_size_progressive(total_items, estimated_item_size);
// Scrollbar position is managed by the scroll event handlers bound during setup.
}
/// PHASE 2 OPTIMIZATION: Truly Selective Cache Invalidation
/// Intelligently invalidate only cache entries that are actually affected by viewport changes
fn selective_cache_invalidation(&mut self) {
let current_dimension = match self.layout_mode {
VirtualListLayoutMode::Vertical => self.viewport_size.width,
VirtualListLayoutMode::Horizontal => self.viewport_size.height,
};
let previous_dimension = self.previous_viewport_width;
let dimension_change = current_dimension - previous_dimension;
// If dimension change is tiny, preserve all cache entries
if dimension_change.abs() < self.internal_params.width_change_threshold {
return;
}
match self.item_sizing_mode {
ItemSizingMode::DynamicSize => {
// For dynamic items, use intelligent selective invalidation
self.selective_dynamic_cache_invalidation(current_dimension, previous_dimension);
}
ItemSizingMode::FixedSize => {
// For fixed size items, only invalidate if layout mode changed or major viewport change
self.selective_fixed_cache_invalidation(dimension_change);
}
}
self.cache_generation = self.cache_generation.wrapping_add(1);
}
/// Selective invalidation for dynamic size items
fn selective_dynamic_cache_invalidation(&mut self, current_dimension: i32, previous_dimension: i32) {
let dimension_change = current_dimension - previous_dimension;
let change_ratio = dimension_change.abs() as f64 / previous_dimension.max(1) as f64;
// Strategy based on magnitude of change
if change_ratio > 0.3 {
// Major change (>30%) - invalidate all to avoid layout issues
self.item_size_cache.clear();
} else if change_ratio > 0.1 {
// Moderate change (>10%) - invalidate cached items that were measured at very different widths
self.invalidate_dimension_sensitive_items(current_dimension);
} else {
// Minor change (<10%) - only invalidate items that might wrap differently
self.invalidate_potentially_affected_items(current_dimension, previous_dimension);
}
}
/// Selective invalidation for fixed size items
fn selective_fixed_cache_invalidation(&mut self, dimension_change: i32) {
// For fixed size items, very conservative invalidation
if dimension_change.abs() > 100 {
// Only invalidate if change is substantial (>100px)
// and only invalidate very old cache entries that might be stale
let current_generation = self.cache_generation;
// Keep cache but mark as potentially stale for future validation
// In a more sophisticated implementation, we'd track cache entry ages
if current_generation.is_multiple_of(10) {
// Every 10 generations, use LRU cache's built-in cleanup
self.item_size_cache.clean_stale_entries(10);
}
}
// Otherwise preserve all cache entries for maximum performance
}
/// Invalidate items that were cached at significantly different dimensions
fn invalidate_dimension_sensitive_items(&mut self, current_dimension: i32) {
let threshold = current_dimension as f64 * 0.15; // 15% difference threshold
let items_to_invalidate: Vec<usize> = self
.item_size_cache
.entries
.iter()
.filter_map(|(&index, cached_entry)| {
// Use the stored measured_at_dimension for more accurate comparison
let dimension_diff = (cached_entry.measured_at_dimension - current_dimension).abs() as f64;
if dimension_diff > threshold { Some(index) } else { None }
})
.collect();
for index in items_to_invalidate {
self.item_size_cache.remove(index);
}
}
/// Invalidate only items that are likely to be affected by small dimension changes
fn invalidate_potentially_affected_items(&mut self, current_dimension: i32, previous_dimension: i32) {
// For small changes, use heuristics to identify items that might reflow
// Invalidate items near text wrapping boundaries
let critical_widths = [300, 400, 500, 600, 800, 1000, 1200]; // Common breakpoints
let crossed_boundary = critical_widths.iter().any(|&width| {
(previous_dimension <= width && current_dimension > width)
|| (previous_dimension > width && current_dimension <= width)
});
if crossed_boundary {
// Only invalidate items that might be affected by text wrapping
// Priority: recently visible items and items near current scroll position
let visible_items: std::collections::HashSet<usize> = self.item_to_panel.keys().copied().collect();
// Calculate scroll-based priority range
let estimated_item_height = self.internal_params.estimated_item_height;
let current_scroll_item = if estimated_item_height > 0 {
(self.scroll_position.y / estimated_item_height) as usize
} else {
0
};
let priority_range = current_scroll_item.saturating_sub(50)..=(current_scroll_item + 100);
let items_to_invalidate: Vec<usize> = self
.item_size_cache
.entries
.keys()
.filter(|&&index| {
// Invalidate if recently visible or in priority scroll range
visible_items.contains(&index) || priority_range.contains(&index)
})
.copied()
.collect();
for index in items_to_invalidate {
self.item_size_cache.remove(index);
}
}
// Otherwise, preserve all cache entries for minimal changes
}
// Content type detection removed - replaced with user-controlled item_sizing_mode
// Users now explicitly specify whether their items have dynamic sizing
/// PHASE 3: Store item context using owned registry (replaces global registry)
fn store_item_context(&mut self, panel: &Panel, index: usize, data: &dyn Any) {
// Store current item index and data in panel context registry
// This ensures event handlers always get the current item context
let panel_id = panel.get_id();
// For now, we'll store a simplified version of the data
// In a real implementation, data sources would provide Clone + Send + Sync data
let stored_data: Arc<dyn Any + Send + Sync> = if let Some(string_data) = data.downcast_ref::<String>() {
Arc::new(string_data.clone())
} else {
Arc::new(format!("Item {index}"))
};
let context = ItemContext {
index,
data: stored_data,
};
// TASK 3.1: Use owned registry instead of global static
self.panel_context_registry.insert(panel_id, context);
}
/// PHASE 3: Remove panel context using owned registry
fn remove_panel_context(&mut self, panel: &Panel) {
let panel_id = panel.get_id();
// TASK 3.1: Use owned registry instead of global static
self.panel_context_registry.remove(&panel_id);
}
fn hit_test(&self, point: Point) -> Option<usize> {
// Simple hit test based on currently visible items
for (&index, panel) in &self.item_to_panel {
let panel_pos = panel.get_position();
let panel_size = panel.get_size();
if point.x >= panel_pos.x
&& point.x < panel_pos.x + panel_size.width
&& point.y >= panel_pos.y
&& point.y < panel_pos.y + panel_size.height
{
return Some(index);
}
}
None
}
fn clear_all_items(&mut self) {
// Clear all items and return panels to pool
self.hide_all_items();
self.item_pool.clear_all();
}
/// PHASE 3: Get item context for a panel using owned registry
fn get_item_context_for_panel(&self, panel: &Panel) -> Option<ItemContext> {
let panel_id = panel.get_id();
self.panel_context_registry.get(&panel_id).cloned()
}
/// PHASE 3: Get item index for a panel (convenience method)
fn get_index_for_panel(&self, panel: &Panel) -> Option<usize> {
self.get_item_context_for_panel(panel).map(|ctx| ctx.index)
}
/// PHASE 3: Get typed data for a panel (convenience method)
fn get_data_for_panel<T>(&self, panel: &Panel) -> Option<T>
where
T: Clone + 'static,
{
self.get_item_context_for_panel(panel)
.and_then(|ctx| ctx.data.downcast_ref::<T>().cloned())
}
/// PHASE 2.3: Execute all panel operations using batched layout system to eliminate layout thrashing
/// This method replaces the individual cleanup_invisible_panels, update_panel_content_batch,
/// handle_dimension_change_batch, and position_visible_panels methods with a unified batching approach
fn execute_batched_layout_operations(&mut self, context: BatchExecutionContext) {
let mut batch = LayoutBatch::new();
// BATCH PHASE 1: Collect panels to destroy (no longer visible)
let items_to_hide: Vec<usize> = context
.current_visible_items
.difference(context.new_visible_items)
.cloned()
.collect();
for item_index in items_to_hide {
if let Some(panel) = self.item_to_panel.remove(&item_index) {
batch.add_destroy_operation(panel);
// Return panel to pool for reuse (will be done after batch execution)
self.item_pool.return_item(panel);
}
}
// BATCH PHASE 2: Collect panel creation/update operations
for (data_index, _item_start, _) in context.items_to_show {
if !self.item_to_panel.contains_key(data_index) {
// NEW PANEL: Need to create this item
let item_panel = self
.item_pool
.get_or_create_item(context.parent, || context.item_renderer.create_item(context.parent));
let item_data = context.data_source.get_item_data(*data_index);
batch.add_create_operation(*data_index, item_panel, item_data);
// Track this panel for this data index
self.item_to_panel.insert(*data_index, item_panel);
} else if context.dimension_changed {
// EXISTING PANEL: Update due to dimension change
if let Some(panel) = self.item_to_panel.get(data_index) {
let item_data = context.data_source.get_item_data(*data_index);
let initial_size = match self.layout_mode {
VirtualListLayoutMode::Vertical => {
Size::new(self.viewport_size.width, self.internal_params.temporary_panel_height)
}
VirtualListLayoutMode::Horizontal => Size::new(
2000, // Give plenty of width for natural text flow measurement
self.viewport_size.height,
),
};
batch.add_update_operation(*data_index, *panel, item_data, initial_size);
}
}
}
// BATCH PHASE 3: Execute create/update operations first to get measurements
let execution_result = batch.execute_batch(context.item_renderer.as_ref(), self);
// BATCH PHASE 4: Update cache with fresh measurements
let mut fresh_measurements = std::collections::HashMap::new();
for (index, measured_size) in &execution_result.final_measurements {
// PHASE 1.2: Store progressive measurement in cache
self.store_progressive_measurement(*index, *measured_size, context.current_viewport_dimension);
fresh_measurements.insert(*index, *measured_size);
}
// BATCH PHASE 5: Position all items using fresh measurements and cache
let mut position_batch = LayoutBatch::new();
for (data_index, item_start, _) in context.items_to_show {
if let Some(panel) = self.item_to_panel.get(data_index) {
// Get final size: prioritize fresh measurements, then cache, then estimates
let final_size = if let Some(fresh_size) = fresh_measurements.get(data_index) {
// Use fresh measurement from this update cycle
match self.layout_mode {
VirtualListLayoutMode::Vertical => Size::new(self.viewport_size.width, fresh_size.height),
VirtualListLayoutMode::Horizontal => Size::new(fresh_size.width, self.viewport_size.height),
}
} else if let Some(cached_size) = self.item_size_cache.peek(*data_index) {
// Use cached measurement
match self.layout_mode {
VirtualListLayoutMode::Vertical => Size::new(self.viewport_size.width, cached_size.size.height),
VirtualListLayoutMode::Horizontal => Size::new(cached_size.size.width, self.viewport_size.height),
}
} else {
// Fallback to estimated size
match self.layout_mode {
VirtualListLayoutMode::Vertical => {
Size::new(self.viewport_size.width, self.internal_params.estimated_item_height)
}
VirtualListLayoutMode::Horizontal => {
Size::new(self.internal_params.estimated_item_width, self.viewport_size.height)
}
}
};
let position = match self.layout_mode {
VirtualListLayoutMode::Vertical => Point::new(0, item_start - context.scroll_position),
VirtualListLayoutMode::Horizontal => Point::new(item_start - context.scroll_position, 0),
};
position_batch.add_position_operation(*panel, position, final_size);
}
}
// BATCH PHASE 6: Execute positioning with correct sizes
position_batch.execute_batch(context.item_renderer.as_ref(), self);
}
}
// TASK 3.1: Add Drop implementation for automatic cleanup
impl Drop for VirtualListState {
fn drop(&mut self) {
// Clear the panel context registry to prevent memory leaks
// This ensures all contexts are cleaned up when the virtual list is destroyed
self.panel_context_registry.clear();
}
}
custom_widget!(
name: VirtualList,
fields: {
layout_mode: VirtualListLayoutMode = VirtualListLayoutMode::Vertical,
scrollbar_size: Option<i32> = None,
keyboard_scroll_amount: Option<i32> = None,
mouse_wheel_multiplier: f32 = 1.0,
state: Rc<RefCell<VirtualListState>> = Rc::new(RefCell::new(VirtualListState::new(
VirtualListLayoutMode::Vertical,
None, // scrollbar_size
None, // keyboard_scroll_amount
1.0, // mouse_wheel_multiplier
))),
},
setup_impl: |config, panel| {
// Set up the panel for virtual list behavior with proper clipping
panel.set_background_style(BackgroundStyle::System); // Use default system background
// Enable clipping of child windows to prevent items from appearing outside bounds
panel.add_style(WindowStyle::ClipChildren);
// Border styling is inherited from the panel's window style flags.
// VirtualList setup complete
// Create scrollbars based on layout mode
let panel_size = panel.get_size();
let scrollbar_size = config.scrollbar_size.unwrap_or(16); // User preference or system default
let (content_area, scrollbar) = match config.layout_mode {
VirtualListLayoutMode::Vertical => {
// Create vertical scrollbar on the right side
let scrollbar = crate::widgets::ScrollBar::builder(&panel)
.with_style(crate::widgets::ScrollBarStyle::Vertical)
.with_pos(Point::new(panel_size.width - scrollbar_size, 0))
.with_size(Size::new(scrollbar_size, panel_size.height))
.build();
// Content area is full height, reduced width
let content_area = Size::new(panel_size.width - scrollbar_size, panel_size.height);
(content_area, Some((scrollbar, true))) // true = vertical
},
VirtualListLayoutMode::Horizontal => {
// Create horizontal scrollbar at the bottom
let scrollbar = crate::widgets::ScrollBar::builder(&panel)
.with_style(crate::widgets::ScrollBarStyle::Default) // Default = horizontal
.with_pos(Point::new(0, panel_size.height - scrollbar_size))
.with_size(Size::new(panel_size.width, scrollbar_size))
.build();
// Content area is full width, reduced height
let content_area = Size::new(panel_size.width, panel_size.height - scrollbar_size);
(content_area, Some((scrollbar, false))) // false = horizontal
}
};
// Store scrollbar references for event handling
let (v_scrollbar, h_scrollbar) = if let Some((scrollbar_widget, is_vertical)) = scrollbar {
if is_vertical {
// Initialize vertical scrollbar with proper values
scrollbar_widget.set_scrollbar(0, 20, 100, 20, true);
scrollbar_widget.enable(true); // Explicitly enable scrollbar
scrollbar_widget.show(true); // Explicitly show scrollbar
(Some(scrollbar_widget), None)
} else {
// Initialize horizontal scrollbar with proper values
scrollbar_widget.set_scrollbar(0, 20, 100, 20, true);
scrollbar_widget.enable(true); // Explicitly enable scrollbar
scrollbar_widget.show(true); // Explicitly show scrollbar
(None, Some(scrollbar_widget))
}
} else {
(None, None)
};
// Initialize the state with the actual layout mode and content area
{
config.state.borrow_mut().layout_mode = config.layout_mode;
// Use content area (excluding scrollbar) as viewport
config.state.borrow_mut().viewport_size = content_area;
}
// Function to update scrollbar properties
// ScrollBar is Copy, so no need to clone
let v_scrollbar_update = v_scrollbar;
let h_scrollbar_update = h_scrollbar;
let layout_mode_update = config.layout_mode;
let state_update = config.state.clone();
let update_scrollbars = move || {
let state = state_update.clone();
match layout_mode_update {
VirtualListLayoutMode::Vertical => {
if let Some(ref vscrollbar) = v_scrollbar_update {
let max_scroll = (state.borrow().total_content_size.height - state.borrow().viewport_size.height).max(1);
let current_pos = if max_scroll > 0 {
(state.borrow().scroll_position.y * 100 / max_scroll).min(100)
} else {
0
};
// Set scrollbar: position, thumb_size, range, page_size, refresh
// thumb_size represents visible portion, page_size is for page up/down
let thumb_size = if state.borrow().total_content_size.height > 0 {
((state.borrow().viewport_size.height * 100) / state.borrow().total_content_size.height).clamp(1, 99)
} else {
95
};
vscrollbar.set_scrollbar(current_pos, thumb_size, 100, thumb_size, true);
}
},
VirtualListLayoutMode::Horizontal => {
if let Some(ref hscrollbar) = h_scrollbar_update {
// Calculate actual content scroll range without artificial buffer
let max_scroll = (state.borrow().total_content_size.width - state.borrow().viewport_size.width).max(1);
let current_pos = if max_scroll > 0 {
(state.borrow().scroll_position.x * 100 / max_scroll).min(100)
} else {
0
};
// Set scrollbar: position, thumb_size, range, page_size, refresh
// Use smaller thumb size to ensure full range accessibility
let thumb_size = if state.borrow().total_content_size.width > 0 {
((state.borrow().viewport_size.width * 100) / state.borrow().total_content_size.width).clamp(1, 95)
} else {
90
};
hscrollbar.set_scrollbar(current_pos, thumb_size, 100, thumb_size, true);
}
}
}
};
// Set up paint event for rendering visible items
let update_scrollbars_paint = update_scrollbars.clone();
panel.on_paint(move |event| {
// Paint events should only draw existing content, not recalculate layout
update_scrollbars_paint();
event.skip(true);
});
// Set up resize event to update viewport and reposition scrollbars
let panel_resize = panel;
let state_resize = config.state.clone();
let layout_mode_resize = config.layout_mode;
let vscrollbar_resize = v_scrollbar;
let hscrollbar_resize = h_scrollbar;
let update_scrollbars_resize = update_scrollbars.clone();
panel.on_size(move |event| {
let new_size = panel_resize.get_size();
let scrollbar_size = 16;
// Reposition scrollbars and calculate new content area
let content_area = match layout_mode_resize {
VirtualListLayoutMode::Vertical => {
if let Some(ref vscrollbar) = vscrollbar_resize {
vscrollbar.move_window(new_size.width - scrollbar_size, 0);
vscrollbar.set_size(Size::new(scrollbar_size, new_size.height));
}
Size::new(new_size.width - scrollbar_size, new_size.height)
},
VirtualListLayoutMode::Horizontal => {
if let Some(ref hscrollbar) = hscrollbar_resize {
hscrollbar.move_window(0, new_size.height - scrollbar_size);
hscrollbar.set_size(Size::new(new_size.width, scrollbar_size));
}
Size::new(new_size.width, new_size.height - scrollbar_size)
}
};
let state = state_resize.clone();
let old_viewport_size = state.borrow().viewport_size;
state.borrow_mut().viewport_size = content_area;
// Auto-reconfigure internal parameters when viewport size changes significantly
if (old_viewport_size.width - content_area.width).abs() > 50 ||
(old_viewport_size.height - content_area.height).abs() > 50 {
state.borrow_mut().internal_params.auto_configure(
(content_area.width, content_area.height),
config.scrollbar_size,
config.keyboard_scroll_amount,
config.mouse_wheel_multiplier,
);
}
let mut cloned_state = state.borrow().clone();
cloned_state.update_visible_items(&panel_resize);
*state.borrow_mut() = cloned_state;
// Update scrollbars after viewport size change
update_scrollbars_resize();
panel_resize.refresh(false, None);
event.skip(true);
});
// Set up mouse wheel scrolling with proper wheel delta handling
let panel_scroll = panel;
let state_scroll = config.state.clone();
let panel_scroll_wheel = panel_scroll;
let state_scroll_wheel = state_scroll.clone();
let update_scrollbars_wheel = update_scrollbars.clone();
// Mouse wheel with proper bidirectional scrolling using wheel rotation
panel.on_mouse_wheel(move |event| {
// Guard against re-entrancy while we're updating scrollbars/state
{
let mut st = state_scroll_wheel.borrow_mut();
if st.is_updating_scrollbar { return; }
st.is_updating_scrollbar = true;
}
// Extract wheel rotation from event data for bidirectional scrolling
let (wheel_rotation, _wheel_delta) = match event {
crate::event::window_events::WindowEventData::MouseButton(ref mouse_event) => {
(mouse_event.event.get_wheel_rotation(), mouse_event.event.get_wheel_delta())
},
crate::event::window_events::WindowEventData::General(ref general_event) => {
(general_event.get_wheel_rotation(), general_event.get_wheel_delta())
},
_ => (0, 120)
};
// Calculate scroll amount - negative rotation means scroll down (increase scroll position)
// Positive rotation means scroll up (decrease scroll position)
let scroll_amount = -wheel_rotation; // Invert to match expected scroll direction
if scroll_amount == 0 {
event.skip(true);
state_scroll_wheel.borrow_mut().is_updating_scrollbar = false;
return;
}
let state = state_scroll_wheel.clone();
let layout_mode = state.borrow().layout_mode;
match layout_mode {
VirtualListLayoutMode::Vertical => {
let max_scroll = (state.borrow().total_content_size.height - state.borrow().viewport_size.height).max(0);
let tentative_scroll_y = (state.borrow().scroll_position.y + scroll_amount)
.max(0)
.min(max_scroll);
// CRITICAL FIX: Mouse wheel end-of-list handling to prevent clipping
let new_scroll_y = if max_scroll > 0 {
// Calculate base content size (without padding) to determine when end-of-list logic is needed
let data_source_opt = state.borrow().data_source.clone();
let base_max_scroll = if let Some(ref data_source) = data_source_opt {
let total_items = data_source.get_item_count();
if total_items > 0 {
// Calculate base content height (same logic as calculate_total_content_size_progressive)
let mut base_total_height = 0;
for i in 0..total_items {
let item_height = if let Some(cached_size) = state.borrow().item_size_cache.peek(i) {
cached_size.size.height
} else {
state.borrow().internal_params.estimated_item_height
};
base_total_height += item_height;
}
(base_total_height - state.borrow().viewport_size.height).max(0)
} else {
max_scroll
}
} else {
max_scroll
};
// Only trigger end-of-list logic when actually trying to scroll past the base content
// This eliminates false triggers from ratio-based detection
let scrolling_down = scroll_amount > 0;
if tentative_scroll_y >= base_max_scroll && scrolling_down {
let data_source_opt = state.borrow().data_source.clone();
let item_renderer_opt = state.borrow().item_renderer.clone();
if let (Some(data_source), Some(item_renderer)) = (data_source_opt, item_renderer_opt) {
let total_items = data_source.get_item_count();
if total_items > 0 {
let last_item_index = total_items - 1;
// Force measurement of the last item to get its actual size
let mut cloned_state = state.borrow().clone();
let last_item_size = cloned_state.measure_item_size(
last_item_index,
data_source.as_ref(),
item_renderer.as_ref(),
&panel_scroll_wheel,
);
*state.borrow_mut() = cloned_state;
// Calculate total content size up to and including the last item
let mut total_height = 0;
for i in 0..total_items {
let item_height = if let Some(cached_size) = state.borrow().item_size_cache.peek(i) {
cached_size.size.height
} else if i == last_item_index {
last_item_size.height
} else {
state.borrow().internal_params.estimated_item_height
};
total_height += item_height;
}
// Add safety padding to ensure last item is fully visible
let safety_padding = state.borrow().internal_params.safety_padding;
let padded_total_height = total_height + safety_padding;
// CRITICAL FIX: Update total_content_size to match the padded calculation
// This ensures consistency between scrollbar and mouse wheel scrolling
state.borrow_mut().total_content_size.height = padded_total_height;
// Calculate scroll position to show the end with safety padding
(padded_total_height - state.borrow().viewport_size.height).max(0)
} else {
tentative_scroll_y
}
} else {
tentative_scroll_y
}
} else {
// Normal scrolling: allow reaching the base content boundary to trigger end-of-list logic
tentative_scroll_y.max(0).min(base_max_scroll.max(max_scroll))
}
} else {
tentative_scroll_y
};
let old_y = state.borrow().scroll_position.y;
if new_scroll_y != old_y {
state.borrow_mut().scroll_position.y = new_scroll_y;
let mut cloned_state = state.borrow().clone();
cloned_state.update_visible_items(&panel_scroll_wheel);
*state.borrow_mut() = cloned_state;
// Use central scrollbar update function for consistent behavior
update_scrollbars_wheel();
panel_scroll_wheel.refresh(false, None);
}
}
VirtualListLayoutMode::Horizontal => {
// Calculate actual content scroll range without artificial buffer
let max_scroll = (state.borrow().total_content_size.width - state.borrow().viewport_size.width).max(0);
let new_scroll_x = (state.borrow().scroll_position.x + scroll_amount)
.max(0)
.min(max_scroll);
let old_x = state.borrow().scroll_position.x;
if new_scroll_x != old_x {
state.borrow_mut().scroll_position.x = new_scroll_x;
let mut cloned_state = state.borrow().clone();
cloned_state.update_visible_items(&panel_scroll_wheel);
*state.borrow_mut() = cloned_state;
// Use central scrollbar update function for consistent behavior
update_scrollbars_wheel();
panel_scroll_wheel.refresh(false, None);
}
}
}
// Release guard at end
state_scroll_wheel.borrow_mut().is_updating_scrollbar = false;
event.skip(true);
});
// Also keep keyboard controls as backup/alternative navigation
// Arrow Down = scroll forward, Arrow Up = scroll backward
let update_scrollbars_key = update_scrollbars.clone();
panel.on_key_down(move |event| {
// Guard against re-entrancy while we're updating scrollbars/state
{
let mut st = state_scroll.borrow_mut();
if st.is_updating_scrollbar { return; }
st.is_updating_scrollbar = true;
}
let key_code = match event {
crate::event::window_events::WindowEventData::Keyboard(ref kbd_event) => {
kbd_event.get_key_code().unwrap_or(0)
}
_ => 0
};
let scroll_delta = match key_code {
314 => Some(300), // Down arrow - scroll forward
315 => Some(-300), // Up arrow - scroll backward
_ => None
};
if let Some(delta) = scroll_delta {
let state = state_scroll.clone();
let layout_mode = state.borrow().layout_mode;
match layout_mode {
VirtualListLayoutMode::Vertical => {
let max_scroll = (state.borrow().total_content_size.height - state.borrow().viewport_size.height).max(0);
let new_scroll_y = (state.borrow().scroll_position.y + delta)
.max(0)
.min(max_scroll);
let old_y = state.borrow().scroll_position.y;
if new_scroll_y != old_y {
state.borrow_mut().scroll_position.y = new_scroll_y;
let mut cloned_state = state.borrow().clone();
cloned_state.update_visible_items(&panel_scroll);
*state.borrow_mut() = cloned_state;
// Use central scrollbar update function for consistent behavior
update_scrollbars_key();
panel_scroll.refresh(false, None);
}
}
VirtualListLayoutMode::Horizontal => {
// Calculate actual content scroll range without artificial buffer
let max_scroll = (state.borrow().total_content_size.width - state.borrow().viewport_size.width).max(0);
let new_scroll_x = (state.borrow().scroll_position.x + delta)
.max(0)
.min(max_scroll);
let old_x = state.borrow().scroll_position.x;
if new_scroll_x != old_x {
state.borrow_mut().scroll_position.x = new_scroll_x;
let mut cloned_state = state.borrow().clone();
cloned_state.update_visible_items(&panel_scroll);
*state.borrow_mut() = cloned_state;
// Use central scrollbar update function for consistent behavior
update_scrollbars_key();
panel_scroll.refresh(false, None);
}
}
};
}
// Release guard at end
state_scroll.borrow_mut().is_updating_scrollbar = false;
event.skip(true);
});
// Simplified scrollbar event handling - add full support for click/page/drag
if let Some(ref vscrollbar) = v_scrollbar {
let panel_vscroll = panel;
let state_vscroll = config.state.clone();
let update_scrollbars_vscroll = update_scrollbars.clone();
// Only handle thumb track for smooth drag scrolling
vscrollbar.on_thumb_track(move |event| {
// Re-entrancy guard for entire handler
{
let mut st = state_vscroll.borrow_mut();
if st.is_updating_scrollbar { return; }
st.is_updating_scrollbar = true;
}
if let Some(position) = event.get_position() {
let state = state_vscroll.clone();
// Map scrollbar position using the current effective range (range - thumb_size)
// Compute thumb size the same way we set it in update_scrollbars
let (viewport_h, content_h) = {
let b = state.borrow();
(b.viewport_size.height, b.total_content_size.height)
};
let thumb_size = if content_h > 0 {
((viewport_h * 100) / content_h).clamp(1, 99)
} else {
95
};
let effective_max_position = (100 - thumb_size).max(1) as f32;
let scroll_ratio = (position as f32 / effective_max_position).clamp(0.0, 1.0);
// CRITICAL FIX: Last-item-aware scrolling for end-of-list positioning
if scroll_ratio >= 0.95 {
// User is dragging near the end - ensure last item is fully visible
let (data_source_opt, item_renderer_opt) = {
let b = state.borrow();
(b.data_source.clone(), b.item_renderer.clone())
};
if let (Some(data_source), Some(item_renderer)) = (data_source_opt, item_renderer_opt) {
let total_items = data_source.get_item_count();
if total_items > 0 {
let last_item_index = total_items - 1;
// Work entirely on a cloned state to avoid overlapping borrows
let mut cloned = { state.borrow().clone() };
let last_item_size = cloned.measure_item_size(
last_item_index,
data_source.as_ref(),
item_renderer.as_ref(),
&panel_vscroll,
);
// Calculate total content size up to and including the last item using cloned cache
let mut total_height = 0;
for i in 0..total_items {
let item_height = if let Some(cached_size) = cloned.item_size_cache.peek(i) {
cached_size.size.height
} else if i == last_item_index {
last_item_size.height
} else {
cloned.internal_params.estimated_item_height
};
total_height += item_height;
}
// Add safety padding and set on cloned state
let padded_total_height = total_height + cloned.internal_params.safety_padding;
cloned.total_content_size.height = padded_total_height;
// Calculate scroll position to show the end with the safety padding
let target_scroll_y = (padded_total_height - cloned.viewport_size.height).max(0);
cloned.scroll_position.y = target_scroll_y;
// Update visible items on cloned state and commit
cloned.update_visible_items(&panel_vscroll);
*state.borrow_mut() = cloned;
}
}
} else {
// Normal scrolling for non-end positions
let old_max_scroll = {
let b = state.borrow();
(b.total_content_size.height - b.viewport_size.height).max(0)
};
let target_scroll_y = if old_max_scroll > 0 {
(scroll_ratio * old_max_scroll as f32).round() as i32
} else {
0
};
let old_y = { state.borrow().scroll_position.y };
if target_scroll_y != old_y {
let mut cloned = { state.borrow().clone() };
cloned.scroll_position.y = target_scroll_y;
cloned.update_visible_items(&panel_vscroll);
*state.borrow_mut() = cloned;
}
}
// For DynamicSize mode, ensure a final layout pass after rapid scrolling
if state.borrow().item_sizing_mode == ItemSizingMode::DynamicSize {
// Force layout on all visible panels to ensure proper text wrapping
for panel in state.borrow().item_to_panel.values() {
panel.layout();
}
}
// Use central scrollbar update function for consistent behavior
update_scrollbars_vscroll();
panel_vscroll.refresh(false, None);
}
// Release guard at end
state_vscroll.borrow_mut().is_updating_scrollbar = false;
});
// Also handle single-clicks on arrow/page areas and final position changes
let panel_vscroll_click = panel;
let state_vscroll_click = config.state.clone();
let update_scrollbars_vscroll_click = update_scrollbars.clone();
// Helper: apply a relative vertical delta (positive = down, negative = up)
let state_for_apply = state_vscroll_click.clone();
let panel_for_apply = panel_vscroll_click;
let update_for_apply = update_scrollbars_vscroll_click.clone();
let apply_vertical_delta = move |delta: i32| {
let state = state_for_apply.clone();
// Guard for entire operation
{
let mut st = state.borrow_mut();
if st.is_updating_scrollbar { return; }
st.is_updating_scrollbar = true;
}
// Compute new target within bounds using cloned state
let (old_y, max_scroll) = { let b = state.borrow(); (b.scroll_position.y, (b.total_content_size.height - b.viewport_size.height).max(0)) };
let mut cloned = { state.borrow().clone() };
let new_y = (old_y + delta).max(0).min(max_scroll);
if new_y != old_y {
cloned.scroll_position.y = new_y;
cloned.update_visible_items(&panel_for_apply);
*state.borrow_mut() = cloned;
update_for_apply();
panel_for_apply.refresh(false, None);
}
// Release guard
{ let mut st = state.borrow_mut(); st.is_updating_scrollbar = false; }
};
let state_for_handle = state_vscroll_click.clone();
let panel_for_handle = panel_vscroll_click;
let update_for_handle = update_scrollbars_vscroll_click.clone();
let handle_vertical_position = move |pos: i32| {
let state = state_for_handle.clone();
// Re-entrancy guard for entire handler
{
let mut st = state.borrow_mut();
if st.is_updating_scrollbar { return; }
st.is_updating_scrollbar = true;
}
// Map scrollbar logical position (0..range-thumb) to content scroll range
// Compute thumb size as in update_scrollbars
let (viewport_h, content_h) = { let b = state.borrow(); (b.viewport_size.height, b.total_content_size.height) };
let thumb_size = if content_h > 0 {
((viewport_h * 100) / content_h).clamp(1, 99)
} else {
95
};
let effective_max_position = (100 - thumb_size).max(1) as f32;
let scroll_ratio = (pos as f32 / effective_max_position).clamp(0.0, 1.0);
// Compute target based on current content size
let max_scroll = { let b = state.borrow(); (b.total_content_size.height - b.viewport_size.height).max(0) };
let mut target_scroll_y = if max_scroll > 0 {
(scroll_ratio * max_scroll as f32).round() as i32
} else {
0
};
// End-of-list assist to ensure last item fully visible when near end
if scroll_ratio >= 0.95 {
let (data_source_opt, item_renderer_opt) = { let b = state.borrow(); (b.data_source.clone(), b.item_renderer.clone()) };
if let (Some(data_source), Some(item_renderer)) = (data_source_opt, item_renderer_opt) {
let total_items = data_source.get_item_count();
if total_items > 0 {
let last = total_items - 1;
let mut cloned = { state.borrow().clone() };
let last_size = cloned.measure_item_size(last, data_source.as_ref(), item_renderer.as_ref(), &panel_for_handle);
let mut total_h = 0;
for i in 0..total_items {
let h = if let Some(cached) = cloned.item_size_cache.peek(i) {
cached.size.height
} else if i == last {
last_size.height
} else {
cloned.internal_params.estimated_item_height
};
total_h += h;
}
let padded = total_h + cloned.internal_params.safety_padding;
cloned.total_content_size.height = padded;
target_scroll_y = (padded - cloned.viewport_size.height).max(0);
// Commit cloned later below when we also update visible items
*state.borrow_mut() = cloned;
}
}
}
let old_y = { state.borrow().scroll_position.y };
if target_scroll_y != old_y {
let mut cloned = { state.borrow().clone() };
cloned.scroll_position.y = target_scroll_y;
cloned.update_visible_items(&panel_for_handle);
*state.borrow_mut() = cloned;
update_for_handle();
panel_for_handle.refresh(false, None);
}
// Release guard at end
{
let mut st = state.borrow_mut();
st.is_updating_scrollbar = false;
}
};
// Bind scroll events with relative movement for line/page/top/bottom
{
// LineUp: small negative delta
let apply = apply_vertical_delta.clone();
let state_for_step = config.state.clone();
vscrollbar.on_scroll_lineup(move |_e| {
let step = { let b = state_for_step.borrow(); (b.viewport_size.height / 10).max(b.internal_params.estimated_item_height).max(1) };
apply(-step);
});
}
{
// LineDown: small positive delta
let apply = apply_vertical_delta.clone();
let state_for_step = config.state.clone();
vscrollbar.on_scroll_linedown(move |_e| {
let step = { let b = state_for_step.borrow(); (b.viewport_size.height / 10).max(b.internal_params.estimated_item_height).max(1) };
apply(step);
});
}
{
// PageUp: negative viewport height
let apply = apply_vertical_delta.clone();
let state_for_step = config.state.clone();
vscrollbar.on_scroll_pageup(move |_e| {
let page = { let b = state_for_step.borrow(); b.viewport_size.height.max(1) };
apply(-page);
});
}
{
// PageDown: positive viewport height
let apply = apply_vertical_delta.clone();
let state_for_step = config.state.clone();
vscrollbar.on_scroll_pagedown(move |_e| {
let page = { let b = state_for_step.borrow(); b.viewport_size.height.max(1) };
apply(page);
});
}
{
// Top
let state_for_top = config.state.clone();
let panel_for_top = panel;
let update_for_top = update_scrollbars.clone();
vscrollbar.on_scroll_top(move |_e| {
{
let mut st = state_for_top.borrow_mut();
if st.is_updating_scrollbar { return; }
st.is_updating_scrollbar = true;
}
let mut cloned = { state_for_top.borrow().clone() };
cloned.scroll_position.y = 0;
cloned.update_visible_items(&panel_for_top);
*state_for_top.borrow_mut() = cloned;
update_for_top();
panel_for_top.refresh(false, None);
{ let mut st = state_for_top.borrow_mut(); st.is_updating_scrollbar = false; }
});
}
{
// Bottom: compute end position robustly
let state_for_bottom = config.state.clone();
let panel_for_bottom = panel;
let update_for_bottom = update_scrollbars.clone();
vscrollbar.on_scroll_bottom(move |_e| {
{
let mut st = state_for_bottom.borrow_mut();
if st.is_updating_scrollbar { return; }
st.is_updating_scrollbar = true;
}
let mut cloned = { state_for_bottom.borrow().clone() };
let (content_h, view_h) = (cloned.total_content_size.height, cloned.viewport_size.height);
let max_scroll = (content_h - view_h).max(0);
cloned.scroll_position.y = max_scroll;
cloned.update_visible_items(&panel_for_bottom);
*state_for_bottom.borrow_mut() = cloned;
update_for_bottom();
panel_for_bottom.refresh(false, None);
{ let mut st = state_for_bottom.borrow_mut(); st.is_updating_scrollbar = false; }
});
}
// Changed: map position proportionally (kept for sanity when toolkit reports new pos)
let handler_changed = handle_vertical_position.clone();
vscrollbar.on_scroll_changed(move |e| { if let Some(p) = e.get_position() { handler_changed(p); } });
}
if let Some(ref hscrollbar) = h_scrollbar {
let panel_hscroll = panel;
let state = config.state.clone();
let update_scrollbars_hscroll = update_scrollbars.clone();
// Only handle thumb track for horizontal scrollbar drag
hscrollbar.on_thumb_track(move |event| {
// Re-entrancy guard for entire handler
{
let mut st = state.borrow_mut();
if st.is_updating_scrollbar { return; }
st.is_updating_scrollbar = true;
}
if let Some(position) = event.get_position() {
// Map scrollbar position using current effective range (range - thumb_size)
let (viewport_w, content_w) = { let b = state.borrow(); (b.viewport_size.width, b.total_content_size.width) };
let thumb_size = if content_w > 0 {
((viewport_w * 100) / content_w).clamp(1, 95)
} else {
90
};
let effective_scrollbar_range = (100 - thumb_size).max(1) as f32;
let scroll_ratio = (position as f32 / effective_scrollbar_range).clamp(0.0, 1.0);
// Calculate initial scroll position
let old_max_scroll = { let b = state.borrow(); (b.total_content_size.width - b.viewport_size.width).max(0) };
let initial_scroll_x = if old_max_scroll > 0 {
(scroll_ratio * old_max_scroll as f32).round() as i32
} else {
0
};
let old_x = { state.borrow().scroll_position.x };
if initial_scroll_x != old_x {
let mut cloned = { state.borrow().clone() };
cloned.scroll_position.x = initial_scroll_x;
cloned.update_visible_items(&panel_hscroll);
*state.borrow_mut() = cloned;
// CRITICAL FIX: Force immediate content size recalculation with fresh measurements
// The issue was that update_visible_items measures items but cache update happens later
let (total_items_opt, estimated_item_size) = { let b = state.borrow(); (b.data_source.as_ref().map(|ds| ds.get_item_count()), b.internal_params.estimated_item_width) };
if let Some(total_items) = total_items_opt {
let mut cloned = { state.borrow().clone() };
let new_content_size = cloned.calculate_total_content_size_progressive(total_items, estimated_item_size);
cloned.total_content_size = new_content_size;
*state.borrow_mut() = cloned;
}
// Now recalculate scroll position with the corrected content size
let new_max_scroll = { let b = state.borrow(); (b.total_content_size.width - b.viewport_size.width).max(0) };
let final_scroll_x = if new_max_scroll > 0 {
(scroll_ratio * new_max_scroll as f32).round() as i32
} else {
0
}.max(0).min(new_max_scroll);
// Update scroll position to account for content size changes
let mut cloned = { state.borrow().clone() };
cloned.scroll_position.x = final_scroll_x;
*state.borrow_mut() = cloned;
// For DynamicSize mode, ensure a final layout pass after rapid scrolling
if state.borrow().item_sizing_mode == ItemSizingMode::DynamicSize {
// Force layout on all visible panels to ensure proper text wrapping
let panels: Vec<_> = { state.borrow().item_to_panel.values().cloned().collect() };
for panel in panels { panel.layout(); }
}
// Use central scrollbar update function for consistent behavior
update_scrollbars_hscroll();
panel_hscroll.refresh(false, None);
}
}
// Release guard at end
state.borrow_mut().is_updating_scrollbar = false;
});
// Also handle single-clicks and page events for horizontal scrollbar
let panel_hscroll_click = panel;
let state_hscroll_click = config.state.clone();
let update_scrollbars_hscroll_click = update_scrollbars.clone();
// Clone shared captures before moving into the handler to avoid moving originals
let state_for_handle_h = state_hscroll_click.clone();
let panel_for_handle_h = panel_hscroll_click;
let update_for_handle_h = update_scrollbars_hscroll_click.clone();
let handle_horizontal_position = move |pos: i32| {
let state = state_for_handle_h.clone();
// Re-entrancy guard for entire handler
{
let mut st = state.borrow_mut();
if st.is_updating_scrollbar { return; }
st.is_updating_scrollbar = true;
}
// Compute thumb size and effective range like update_scrollbars
let (viewport_w, content_w) = { let b = state.borrow(); (b.viewport_size.width, b.total_content_size.width) };
let thumb_size = if content_w > 0 {
((viewport_w * 100) / content_w).clamp(1, 95)
} else {
90
};
let effective_range = (100 - thumb_size).max(1) as f32;
let scroll_ratio = (pos as f32 / effective_range).clamp(0.0, 1.0);
let max_scroll = { let b = state.borrow(); (b.total_content_size.width - b.viewport_size.width).max(0) };
let target_scroll_x = if max_scroll > 0 {
(scroll_ratio * max_scroll as f32).round() as i32
} else { 0 };
let old_x = { state.borrow().scroll_position.x };
if target_scroll_x != old_x {
let mut cloned = { state.borrow().clone() };
cloned.scroll_position.x = target_scroll_x;
cloned.update_visible_items(&panel_for_handle_h);
*state.borrow_mut() = cloned;
// Recompute content size to reflect fresh measurements
let (total_items_opt, estimated) = { let b = state.borrow(); (b.data_source.as_ref().map(|ds| ds.get_item_count()), b.internal_params.estimated_item_width) };
if let Some(total_items) = total_items_opt {
let mut cloned = { state.borrow().clone() };
let new_size = cloned.calculate_total_content_size_progressive(total_items, estimated);
cloned.total_content_size = new_size;
*state.borrow_mut() = cloned;
}
update_for_handle_h();
panel_for_handle_h.refresh(false, None);
}
// Release guard at end
state.borrow_mut().is_updating_scrollbar = false;
};
// Horizontal delta handler: moves scroll by a relative amount
let apply_horizontal_delta = {
let state = state_hscroll_click.clone();
let panel = panel_hscroll_click;
let update_scrollbars = update_scrollbars_hscroll_click.clone();
move |delta: i32| {
// Re-entrancy guard for entire handler
{
let mut st = state.borrow_mut();
if st.is_updating_scrollbar { return; }
st.is_updating_scrollbar = true;
}
let max_scroll = { let b = state.borrow(); (b.total_content_size.width - b.viewport_size.width).max(0) };
let old_x = { state.borrow().scroll_position.x };
let mut new_x = old_x.saturating_add(delta);
new_x = new_x.clamp(0, max_scroll);
if new_x != old_x {
let mut cloned = { state.borrow().clone() };
cloned.scroll_position.x = new_x;
cloned.update_visible_items(&panel);
*state.borrow_mut() = cloned;
// Recompute content size to reflect fresh measurements
let (total_items_opt, estimated) = {
let b = state.borrow();
(b.data_source.as_ref().map(|ds| ds.get_item_count()), b.internal_params.estimated_item_width)
};
if let Some(total_items) = total_items_opt {
let mut cloned = { state.borrow().clone() };
let new_size = cloned.calculate_total_content_size_progressive(total_items, estimated);
cloned.total_content_size = new_size;
*state.borrow_mut() = cloned;
}
update_scrollbars();
panel.refresh(false, None);
}
// Release guard at end
state.borrow_mut().is_updating_scrollbar = false;
}
};
// Use relative deltas for lineup/linedown/pageup/pagedown
let handler_lineup = apply_horizontal_delta.clone();
let state_for_lineup = state_hscroll_click.clone();
hscrollbar.on_scroll_lineup(move |_| {
// Move left by one "line" (estimated item width)
let estimated = { state_for_lineup.borrow().internal_params.estimated_item_width.max(1) };
handler_lineup(-estimated);
});
let handler_linedown = apply_horizontal_delta.clone();
let state_for_linedown = state_hscroll_click.clone();
hscrollbar.on_scroll_linedown(move |_| {
let estimated = { state_for_linedown.borrow().internal_params.estimated_item_width.max(1) };
handler_linedown(estimated);
});
let handler_pageup = apply_horizontal_delta.clone();
let state_for_pageup = state_hscroll_click.clone();
hscrollbar.on_scroll_pageup(move |_| {
let viewport_w = { state_for_pageup.borrow().viewport_size.width.max(1) };
handler_pageup(-viewport_w);
});
let handler_pagedown = apply_horizontal_delta.clone();
let state_for_pagedown = state_hscroll_click.clone();
hscrollbar.on_scroll_pagedown(move |_| {
let viewport_w = { state_for_pagedown.borrow().viewport_size.width.max(1) };
handler_pagedown(viewport_w);
});
// Use absolute position for top/bottom/changed
let handler_top = handle_horizontal_position.clone();
hscrollbar.on_scroll_top(move |e| { if let Some(p) = e.get_position() { handler_top(p); } });
let handler_bottom = handle_horizontal_position.clone();
hscrollbar.on_scroll_bottom(move |e| { if let Some(p) = e.get_position() { handler_bottom(p); } });
let handler_changed = handle_horizontal_position.clone();
hscrollbar.on_scroll_changed(move |e| { if let Some(p) = e.get_position() { handler_changed(p); } });
}
// Hit testing is delegated to child widgets via standard wxWidgets event propagation.
// VirtualList setup completed
}
);
impl VirtualList {
/// Set the data source for the virtual list
pub fn set_data_source<T: VirtualListDataSource + 'static>(&self, data_source: T) {
// Setting data source
self.config().state.borrow_mut().set_data_source(Rc::new(data_source));
// Total content size will be recalculated on next update
self.refresh(false, None);
// Trigger initial update
self.trigger_initial_update();
}
/// Set the item renderer for the virtual list
pub fn set_item_renderer<T: VirtualListItemRenderer + 'static>(&self, item_renderer: T) {
// Setting item renderer
self.config().state.borrow_mut().set_item_renderer(Rc::new(item_renderer));
// Total content size will be recalculated on next update
self.refresh(false, None);
// Trigger initial update
self.trigger_initial_update();
}
/// Set the item sizing mode to control cache invalidation behavior
///
/// - `ItemSizingMode::FixedSize`: Use for items with fixed size (images, fixed text)
/// Cache entries are preserved when width changes for better performance
///
/// - `ItemSizingMode::DynamicSize`: Use for items that resize based on container width
/// (text wrapping, responsive layouts). Cache entries are invalidated when width changes
pub fn set_item_sizing_mode(&self, sizing_mode: ItemSizingMode) {
self.config().state.borrow_mut().item_sizing_mode = sizing_mode;
}
/// Manually trigger an initial update to ensure items become visible
fn trigger_initial_update(&self) {
// Force item creation without changing the viewport size (it's already set to content area)
let mut cloned_state = self.config().state.borrow().clone();
cloned_state.update_visible_items(self);
*self.config().state.borrow_mut() = cloned_state;
// Force a redraw
self.refresh(false, None);
}
/// PHASE 3: Scroll to show a specific item with error handling
pub fn scroll_to_item(&self, index: usize) -> VirtualListResult<()> {
let state = self.config().state.clone();
let total_items = state.borrow().data_source.clone().map(|ds| ds.get_item_count());
let Some(total_items) = total_items else {
return Err(VirtualListError::data_source_error("No data source set"));
};
// Validate index bounds
if index >= total_items {
return Err(VirtualListError::invalid_index(index, total_items));
}
// Calculate position using mix of actual measurements and estimates
let estimated_item_height = 80; // Same estimate as in update_visible_items
let mut y_position = 0;
for i in 0..index {
let item_height = if let Some(size) = state.borrow().item_size_cache.peek(i) {
// Use actual measurement if available
match state.borrow().layout_mode {
VirtualListLayoutMode::Vertical => size.size.height,
VirtualListLayoutMode::Horizontal => size.size.width,
}
} else {
// Use estimate for unmeasured items
estimated_item_height
};
y_position += item_height;
}
let new_scroll_position = match state.borrow().layout_mode {
VirtualListLayoutMode::Vertical => Point::new(0, y_position),
VirtualListLayoutMode::Horizontal => Point::new(y_position, 0),
};
state.borrow_mut().scroll_position = new_scroll_position;
self.refresh(false, None);
Ok(())
}
/// Refresh the virtual list (recalculate everything)
pub fn refresh_virtual_list(&self) {
self.config().state.borrow_mut().invalidate_layout();
self.refresh(false, None);
}
/// Get the currently visible range of items
pub fn get_visible_range(&self) -> std::ops::Range<usize> {
self.config().state.borrow().visible_range.clone()
}
/// Get pool statistics for debugging/monitoring
pub fn get_pool_stats(&self) -> super::adaptive_pool::PoolStats {
self.config().state.borrow().item_pool.get_pool_stats()
}
/// PHASE 3: Hit test to find which item is at a given point with validation
pub fn hit_test(&self, point: Point) -> VirtualListResult<Option<usize>> {
// Validate point coordinates
if point.x < 0 || point.y < 0 {
return Err(VirtualListError::invalid_config(format!(
"Invalid hit test coordinates: ({}, {})",
point.x, point.y
)));
}
let state = self.config().state.borrow();
Ok(state.hit_test(point))
}
/// Clear all items and reset the virtual list
pub fn clear(&self) {
self.config().state.borrow_mut().clear_all_items();
self.refresh(false, None);
}
/// Get the total content size
pub fn get_total_content_size(&self) -> Size {
// Return the cached total content size or a default
self.config().state.borrow().total_content_size
}
/// PHASE 3: Get item context for a panel with error handling (replaces global registry access)
pub fn get_item_context_for_panel(&self, panel: &Panel) -> VirtualListResult<ItemContext> {
self.config()
.state
.borrow()
.get_item_context_for_panel(panel)
.into_vl_error("get_item_context_for_panel")
}
/// PHASE 3: Get item index for a panel with error handling (convenience method)
pub fn get_index_for_panel(&self, panel: &Panel) -> VirtualListResult<usize> {
self.config()
.state
.borrow()
.get_index_for_panel(panel)
.into_vl_error("get_index_for_panel")
}
/// PHASE 3: Get typed data for a panel with error handling (convenience method)
pub fn get_data_for_panel<T>(&self, panel: &Panel) -> VirtualListResult<T>
where
T: Clone + 'static,
{
self.config()
.state
.borrow()
.get_data_for_panel(panel)
.into_vl_error("get_data_for_panel")
}
}