inset-rendering 0.1.0

Render-object tree and layout for Inset
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
//! Flutter counterpart: `rendering/viewport.dart`.
//!
//! Semantics and debug paint wait; see `PORTING.md`.

use std::any::{Any, TypeId};
use std::fmt::{self, Debug};
use std::rc::Rc;
use std::time::Duration;

use inset_animation::Curve;
use inset_embedder::{Clip, Matrix4, Offset, Rect, Size, clamp_double};
use inset_foundation::{App, Handle, HandleId, Listenable, Listener};
use inset_painting::{Axis, AxisDirection, axis_direction_to_axis, transform_rect};

use crate::box_::{BoxConstraints, BoxHitTestResult, RenderBox, RenderBoxData};
use crate::debug::debug_check_has_bounded_axis;
use crate::layer::{ClipRectLayer, LayerHandle};
use crate::object::{
    AnyRenderObject, ContainerRenderObjectData, ContainerRenderObjectMixin, RenderHandle,
    RenderObject, RenderObjectData, debug_checking_intrinsics, resolve, translate,
};
use crate::painting_context::PaintingContext;
use crate::pipeline_owner::PipelineOwner;
use crate::sliver::{
    AnyRenderSliver, GrowthDirection, SliverConstraints, SliverGeometry, SliverHitTestResult,
    SliverLogicalContainerParentData, SliverPhysicalContainerParentData,
    apply_growth_direction_to_axis_direction, apply_growth_direction_to_scroll_direction,
};
use crate::viewport_offset::AnyViewportOffset;

/// The unit of measurement for a viewport's cache extent.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CacheExtentStyle {
    /// Treat the cache extent as logical pixels.
    Pixel,

    /// Treat the cache extent as a multiplier of the main axis extent.
    Viewport,
}

/// The amount of additional content to display and lay out around the viewport.
///
/// The cache area is used to lay out slivers before they are visible on screen. Items that fall
/// in this cache area are laid out even though they are not visible on screen. This allows the
/// viewport to render them ahead of time, enabling a smoother scrolling experience.
///
/// This type encapsulates both the value and the style of the cache extent. It allows the cache
/// extent to be defined either as a fixed number of logical pixels using
/// [`ScrollCacheExtent::Pixels`] or as a multiplier of the viewport's main axis extent using
/// [`ScrollCacheExtent::Viewport`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ScrollCacheExtent {
    /// A cache extent in logical pixels.
    Pixels(f64),

    /// A cache extent as a multiplier of the viewport's main axis extent.
    ///
    /// The main axis extent is the size of the viewport in its main axis. For example, for a
    /// vertically scrolling list, the main axis extent is the height of the viewport. If the
    /// viewport is 600 logical pixels tall, then `ScrollCacheExtent::Viewport(2.0)` results in a
    /// cache extent of 1200 logical pixels.
    Viewport(f64),
}

impl ScrollCacheExtent {
    /// Returns the cache extent in logical pixels for a given `main_axis_extent`.
    fn calculate_cache_offset(self, main_axis_extent: f64) -> f64 {
        match self {
            ScrollCacheExtent::Pixels(pixels) => pixels,
            ScrollCacheExtent::Viewport(value) => value * main_axis_extent,
        }
    }

    /// Returns the style of the cache extent.
    pub fn style(self) -> CacheExtentStyle {
        match self {
            ScrollCacheExtent::Pixels(_) => CacheExtentStyle::Pixel,
            ScrollCacheExtent::Viewport(_) => CacheExtentStyle::Viewport,
        }
    }

    /// Returns the raw value of the cache extent.
    ///
    /// If [`style`](Self::style) is [`CacheExtentStyle::Pixel`], this is the number of pixels to
    /// cache. If it is [`CacheExtentStyle::Viewport`], this is the multiplier of the viewport's
    /// main axis extent to cache.
    pub fn value(self) -> f64 {
        match self {
            ScrollCacheExtent::Pixels(pixels) => pixels,
            ScrollCacheExtent::Viewport(value) => value,
        }
    }
}

/// Specifies an order in which to paint the slivers of a viewport.
///
/// Whichever order the slivers are painted in, they will be hit-tested in the opposite order.
///
/// This can also be thought of as an ordering in the z-direction: whichever sliver is painted
/// last (and hit-tested first) is on top, because it will paint over other slivers if there is
/// overlap. Similarly, whichever sliver is painted first (and hit-tested last) is on the bottom.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum SliverPaintOrder {
    /// The first sliver paints on top, and the last sliver on bottom.
    ///
    /// The slivers are painted in the reverse order of the viewport's children, and hit-tested
    /// in the same order as the viewport's children.
    ///
    /// This is the default order.
    #[default]
    FirstIsTop,

    /// The last sliver paints on top, and the first sliver on bottom.
    ///
    /// The slivers are painted in the same order as the viewport's children, and hit-tested in
    /// the reverse order.
    LastIsTop,
}

/// An interface for render objects that are bigger on the inside.
///
/// Some render objects, such as [`RenderViewport`], present a portion of their content, which
/// can be controlled by a [`crate::ViewportOffset`]. This interface lets the framework recognize
/// such render objects and interact with them without having specific knowledge of all the
/// various types of viewports.
///
/// Answer [`RenderObject::interface`] for [`AnyRenderAbstractViewport`] with
/// [`as_abstract_viewport`](Self::as_abstract_viewport) so that
/// [`AnyRenderAbstractViewport::maybe_of`] can find this render object.
pub trait RenderAbstractViewport: RenderObject {
    /// Returns the offset that would be needed to reveal the `target` render object.
    ///
    /// This is used by [`show_in_viewport`], which is itself used by
    /// [`RenderObject::show_on_screen`] for [`RenderViewportBase`].
    ///
    /// The optional `rect` parameter describes which area of that `target` object should be
    /// revealed in the viewport. If `rect` is `None`, the entire `target` (as defined by its
    /// paint bounds) will be revealed. If `rect` is provided it has to be given in the
    /// coordinate system of the `target` object.
    ///
    /// The `alignment` argument describes where the target should be positioned after applying
    /// the returned offset. If `alignment` is 0.0, the child must be positioned as close to the
    /// leading edge of the viewport as possible. If `alignment` is 1.0, the child must be
    /// positioned as close to the trailing edge of the viewport as possible. If `alignment` is
    /// 0.5, the child must be positioned as close to the center of the viewport as possible.
    ///
    /// The `target` might not be a direct child of this viewport but it must be a descendant of
    /// the viewport. Other viewports in between this viewport and the `target` will not be
    /// adjusted.
    ///
    /// This method assumes that the content of the viewport moves linearly, i.e. when the offset
    /// of the viewport is changed by x then `target` also moves by x within the viewport.
    ///
    /// The optional [`Axis`] determines which of two axes to compute an offset for.
    /// One-dimensional viewports ignore it, since there is only one [`Axis`].
    fn get_offset_to_reveal(
        self: RenderHandle<Self>,
        app: &App,
        target: AnyRenderObject,
        alignment: f64,
        rect: Option<Rect>,
        axis: Option<Axis>,
    ) -> RevealedOffset;

    /// The type-erased `RenderAbstractViewport` handle. Free: the vtable is a `const`, and the
    /// id is copied.
    fn as_abstract_viewport(self: RenderHandle<Self>) -> AnyRenderAbstractViewport {
        AnyRenderAbstractViewport {
            id: self.id(),
            vtable: const { &RenderAbstractViewportVTable::of::<Self>() },
        }
    }
}

/// The vtable of an erased [`AnyRenderAbstractViewport`].
struct RenderAbstractViewportVTable {
    get_offset_to_reveal: GetOffsetToRevealFn,
    as_object: fn(&App, HandleId) -> AnyRenderObject,
}

/// The dispatch signature of [`RenderAbstractViewport::get_offset_to_reveal`].
type GetOffsetToRevealFn =
    fn(&App, HandleId, AnyRenderObject, f64, Option<Rect>, Option<Axis>) -> RevealedOffset;

impl RenderAbstractViewportVTable {
    const fn of<T: RenderAbstractViewport>() -> RenderAbstractViewportVTable {
        RenderAbstractViewportVTable {
            get_offset_to_reveal: |app, id, target, alignment, rect, axis| {
                T::get_offset_to_reveal(resolve(id), app, target, alignment, rect, axis)
            },
            as_object: |app, id| resolve::<T>(id).as_render_object(app),
        }
    }
}

/// Erased [`RenderAbstractViewport`]: what a field or parameter Dart types as
/// `RenderAbstractViewport` becomes.
#[derive(Clone, Copy)]
pub struct AnyRenderAbstractViewport {
    id: HandleId,
    vtable: &'static RenderAbstractViewportVTable,
}

impl AnyRenderAbstractViewport {
    /// The default value for the cache extent of a viewport.
    ///
    /// This default assumes [`CacheExtentStyle::Pixel`].
    pub const DEFAULT_CACHE_EXTENT: f64 = 250.0;

    /// Returns the viewport that most tightly encloses the given render object.
    ///
    /// If the object does not have a viewport as an ancestor, this returns `None`.
    ///
    /// Dart's `RenderAbstractViewport.maybeOf`.
    pub fn maybe_of(
        app: &App,
        object: Option<AnyRenderObject>,
    ) -> Option<AnyRenderAbstractViewport> {
        let mut object = object;
        while let Some(current) = object {
            if let Some(viewport) = current.interface::<AnyRenderAbstractViewport>() {
                return Some(viewport);
            }
            object = current.parent(app);
        }
        None
    }

    /// Returns the viewport that most tightly encloses the given render object.
    ///
    /// Dart's `RenderAbstractViewport.of`.
    ///
    /// # Panics
    ///
    /// If the object does not have a viewport as an ancestor.
    pub fn of(app: &App, object: Option<AnyRenderObject>) -> AnyRenderAbstractViewport {
        AnyRenderAbstractViewport::maybe_of(app, object).unwrap_or_else(|| {
            panic!(
                "RenderAbstractViewport::of was called with a render object that was not a \
                 descendant of a RenderAbstractViewport: {object:?}"
            )
        })
    }

    /// The `RenderObject` view of this viewport.
    pub fn as_object(self, app: &App) -> AnyRenderObject {
        (self.vtable.as_object)(app, self.id)
    }

    /// See [`RenderAbstractViewport::get_offset_to_reveal`].
    pub fn get_offset_to_reveal(
        self,
        app: &App,
        target: AnyRenderObject,
        alignment: f64,
        rect: Option<Rect>,
        axis: Option<Axis>,
    ) -> RevealedOffset {
        (self.vtable.get_offset_to_reveal)(app, self.id, target, alignment, rect, axis)
    }
}

impl PartialEq for AnyRenderAbstractViewport {
    fn eq(&self, other: &AnyRenderAbstractViewport) -> bool {
        self.id == other.id
    }
}

impl Eq for AnyRenderAbstractViewport {}

impl Debug for AnyRenderAbstractViewport {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "AnyRenderAbstractViewport({:?})", self.id)
    }
}

/// Return value for [`RenderAbstractViewport::get_offset_to_reveal`].
///
/// It indicates the [`offset`](Self::offset) required to reveal an element in a viewport and the
/// [`rect`](Self::rect) position said element would have in the viewport at that offset.
#[derive(Clone, Copy, Debug)]
pub struct RevealedOffset {
    /// Offset for the viewport to reveal a specific element in the viewport.
    pub offset: f64,

    /// The [`Rect`] in the outer coordinate system of the viewport at which the to-be-revealed
    /// element would be located if the viewport's offset is set to [`offset`](Self::offset).
    ///
    /// A viewport usually has two coordinate systems and works as an adapter between the two:
    ///
    /// The inner coordinate system has its origin at the top left corner of the content that
    /// moves inside the viewport. The origin of this coordinate system usually moves around
    /// relative to the leading edge of the viewport when the viewport offset changes.
    ///
    /// The outer coordinate system has its origin at the top left corner of the visible part of
    /// the viewport. This origin stays at the same position regardless of the current viewport
    /// offset.
    pub rect: Rect,
}

impl RevealedOffset {
    /// Instantiates a return value for [`RenderAbstractViewport::get_offset_to_reveal`].
    pub const fn new(offset: f64, rect: Rect) -> RevealedOffset {
        RevealedOffset { offset, rect }
    }

    /// Determines which provided leading or trailing edge of the viewport will be used for
    /// [`show_in_viewport`], accounting for the size and already visible portion of the render
    /// object that is being revealed.
    ///
    /// If the target render object is already fully visible, this returns `None`.
    pub fn clamp_offset(
        leading_edge_offset: RevealedOffset,
        trailing_edge_offset: RevealedOffset,
        current_offset: f64,
    ) -> Option<RevealedOffset> {
        //           scrollOffset
        //                       0 +---------+
        //                         |         |
        //                       _ |         |
        //    viewport position |  |         |
        // with `descendant` at |  |         | _
        //        trailing edge |_ | xxxxxxx |  | viewport position
        //                         |         |  | with `descendant` at
        //                         |         | _| leading edge
        //                         |         |
        //                     800 +---------+
        //
        // `trailing_edge_offset`: Distance from scrollOffset 0 to the start of the viewport on
        //                         the left in the image above.
        // `leading_edge_offset`: Distance from scrollOffset 0 to the start of the viewport on
        //                        the right in the image above.
        //
        // The viewport position on the left is achieved by setting `offset.pixels` to
        // `trailing_edge_offset`, the one on the right by setting it to `leading_edge_offset`.
        let inverted = leading_edge_offset.offset < trailing_edge_offset.offset;
        let (smaller, larger) = if inverted {
            (leading_edge_offset, trailing_edge_offset)
        } else {
            (trailing_edge_offset, leading_edge_offset)
        };
        if current_offset > larger.offset {
            Some(larger)
        } else if current_offset < smaller.offset {
            Some(smaller)
        } else {
            None
        }
    }
}

/// Flutter's `layoutChildSequence`'s `advance` callback: `childBefore` or `childAfter`.
pub type SliverAdvance<T> =
    dyn Fn(RenderHandle<T>, &App, AnyRenderSliver) -> Option<AnyRenderSliver>;

/// Flutter's `RenderViewportBase` fields.
pub struct RenderViewportBaseData {
    axis_direction: AxisDirection,
    cross_axis_direction: AxisDirection,
    offset: AnyViewportOffset,
    scroll_cache_extent: ScrollCacheExtent,
    /// This value is set during layout based on the
    /// [`scroll_cache_extent`](RenderViewportBase::scroll_cache_extent).
    ///
    /// When the style is [`CacheExtentStyle::Viewport`], it is the main axis extent of the
    /// viewport multiplied by the requested cache extent, which is still expressed in pixels.
    calculated_cache_extent: Option<f64>,
    paint_order: SliverPaintOrder,
    clip_behavior: Clip,
    clip_rect_layer: LayerHandle<Handle<ClipRectLayer>>,
}

impl RenderViewportBaseData {
    /// Initializes fields with Flutter's constructor defaults: an [`AxisDirection::Down`] axis,
    /// a [`AnyRenderAbstractViewport::DEFAULT_CACHE_EXTENT`] pixel cache extent,
    /// [`SliverPaintOrder::FirstIsTop`], and [`Clip::HardEdge`].
    pub fn new(
        cross_axis_direction: AxisDirection,
        offset: AnyViewportOffset,
    ) -> RenderViewportBaseData {
        RenderViewportBaseData {
            axis_direction: AxisDirection::Down,
            cross_axis_direction,
            offset,
            scroll_cache_extent: ScrollCacheExtent::Pixels(
                AnyRenderAbstractViewport::DEFAULT_CACHE_EXTENT,
            ),
            calculated_cache_extent: None,
            paint_order: SliverPaintOrder::FirstIsTop,
            clip_behavior: Clip::HardEdge,
            clip_rect_layer: LayerHandle::new(),
        }
    }
}

/// Implements [`RenderViewportBase`] field accessors for a `viewport` field.
#[macro_export]
macro_rules! render_viewport_base_accessors {
    () => {
        fn viewport_data(
            self: $crate::RenderHandle<Self>,
            app: &::inset_foundation::App,
        ) -> &$crate::RenderViewportBaseData {
            &self.get(app).viewport
        }
        fn viewport_data_mut(
            self: $crate::RenderHandle<Self>,
            app: &mut ::inset_foundation::App,
        ) -> &mut $crate::RenderViewportBaseData {
            &mut self.get_mut(app).viewport
        }
    };
}

/// A base class for render objects that are bigger on the inside.
///
/// This render object provides the shared code for render objects that host [`AnyRenderSliver`]
/// render objects inside a [`RenderBox`]. The viewport establishes an
/// [`axis_direction`](Self::axis_direction), which orients the sliver's coordinate system, which
/// is based on scroll offsets rather than Cartesian coordinates.
///
/// The viewport also listens to an [`offset`](Self::offset), which determines the
/// [`SliverConstraints::scroll_offset`] input to the sliver layout protocol.
///
/// Implementors typically override [`RenderObject::perform_layout`] and call
/// [`layout_child_sequence`](Self::layout_child_sequence), perhaps multiple times.
///
/// Flutter's `RenderViewportBase`; the state lives in a [`RenderViewportBaseData`] field.
pub trait RenderViewportBase:
    RenderBox + RenderAbstractViewport + ContainerRenderObjectMixin<ChildType = AnyRenderSliver>
{
    /// Mixin field access.
    fn viewport_data(self: RenderHandle<Self>, app: &App) -> &RenderViewportBaseData;

    /// See [`viewport_data`](Self::viewport_data).
    fn viewport_data_mut(self: RenderHandle<Self>, app: &mut App) -> &mut RenderViewportBaseData;

    /// The direction in which the [`SliverConstraints::scroll_offset`] increases.
    ///
    /// For example, if the axis direction is [`AxisDirection::Down`], a scroll offset of zero is
    /// at the top of the viewport and increases towards the bottom of the viewport.
    fn axis_direction(self: RenderHandle<Self>, app: &App) -> AxisDirection {
        self.viewport_data(app).axis_direction
    }

    /// Sets [`axis_direction`](Self::axis_direction).
    fn set_axis_direction(self: RenderHandle<Self>, app: &mut App, value: AxisDirection) {
        if value == self.axis_direction(app) {
            return;
        }
        self.viewport_data_mut(app).axis_direction = value;
        self.mark_needs_layout(app);
    }

    /// The direction in which child should be laid out in the cross axis.
    ///
    /// For example, if the [`axis_direction`](Self::axis_direction) is [`AxisDirection::Down`],
    /// this property is typically [`AxisDirection::Left`] if the ambient text direction is
    /// right-to-left and [`AxisDirection::Right`] if it is left-to-right.
    fn cross_axis_direction(self: RenderHandle<Self>, app: &App) -> AxisDirection {
        self.viewport_data(app).cross_axis_direction
    }

    /// Sets [`cross_axis_direction`](Self::cross_axis_direction).
    fn set_cross_axis_direction(self: RenderHandle<Self>, app: &mut App, value: AxisDirection) {
        if value == self.cross_axis_direction(app) {
            return;
        }
        self.viewport_data_mut(app).cross_axis_direction = value;
        self.mark_needs_layout(app);
    }

    /// The axis along which the viewport scrolls.
    ///
    /// For example, if the [`axis_direction`](Self::axis_direction) is [`AxisDirection::Down`],
    /// then the axis is [`Axis::Vertical`] and the viewport scrolls vertically.
    fn axis(self: RenderHandle<Self>, app: &App) -> Axis {
        axis_direction_to_axis(self.axis_direction(app))
    }

    /// Which part of the content inside the viewport should be visible.
    ///
    /// The [`crate::ViewportOffset::pixels`] value determines the scroll offset that the
    /// viewport uses to select which part of its content to display. As the user scrolls the
    /// viewport, this value changes, which changes the content that is displayed.
    fn offset(self: RenderHandle<Self>, app: &App) -> AnyViewportOffset {
        self.viewport_data(app).offset
    }

    /// Sets [`offset`](Self::offset).
    fn set_offset(self: RenderHandle<Self>, app: &mut App, value: AnyViewportOffset) {
        let old = self.offset(app);
        if value == old {
            return;
        }
        if self.attached(app) {
            old.remove_listener(app, &self.mark_needs_layout_listener());
        }
        self.viewport_data_mut(app).offset = value;
        if self.attached(app) {
            value.add_listener(app, self.mark_needs_layout_listener());
        }
        // We need to go through layout even if the new offset has the same pixels value as the
        // old offset so that we will apply our viewport and content dimensions.
        self.mark_needs_layout(app);
    }

    /// Dart's `_offset.addListener(markNeedsLayout)` tear-off.
    fn mark_needs_layout_listener(self: RenderHandle<Self>) -> Listener {
        Listener::handle_method(self.handle(), viewport_offset_changed::<Self>)
    }

    /// The viewport has an area before and after the visible area to cache items that are about
    /// to become visible when the user scrolls.
    ///
    /// Items that fall in this cache area are laid out even though they are not (yet) visible on
    /// screen. The scroll cache extent describes how much the cache area extends before the
    /// leading edge and after the trailing edge of the viewport.
    ///
    /// The total extent, which the viewport will try to cover with children, is the scroll cache
    /// extent before the leading edge + extent of the main axis + the scroll cache extent after
    /// the trailing edge.
    fn scroll_cache_extent(self: RenderHandle<Self>, app: &App) -> ScrollCacheExtent {
        self.viewport_data(app).scroll_cache_extent
    }

    /// Sets [`scroll_cache_extent`](Self::scroll_cache_extent).
    ///
    /// `None` resets the value to [`AnyRenderAbstractViewport::DEFAULT_CACHE_EXTENT`] pixels.
    fn set_scroll_cache_extent(
        self: RenderHandle<Self>,
        app: &mut App,
        value: Option<ScrollCacheExtent>,
    ) {
        let effective_value = value.unwrap_or(ScrollCacheExtent::Pixels(
            AnyRenderAbstractViewport::DEFAULT_CACHE_EXTENT,
        ));
        if effective_value == self.scroll_cache_extent(app) {
            return;
        }
        self.viewport_data_mut(app).scroll_cache_extent = effective_value;
        self.mark_needs_layout(app);
    }

    /// Deprecated. Use [`scroll_cache_extent`](Self::scroll_cache_extent) instead.
    #[deprecated(note = "Use scroll_cache_extent instead.")]
    fn cache_extent(self: RenderHandle<Self>, app: &App) -> f64 {
        self.scroll_cache_extent(app).value()
    }

    /// Deprecated. Use [`set_scroll_cache_extent`](Self::set_scroll_cache_extent) instead.
    ///
    /// `None` resets the value to [`AnyRenderAbstractViewport::DEFAULT_CACHE_EXTENT`] pixels, in
    /// which case the style becomes [`CacheExtentStyle::Pixel`].
    #[deprecated(note = "Use set_scroll_cache_extent instead.")]
    fn set_cache_extent(self: RenderHandle<Self>, app: &mut App, value: Option<f64>) {
        let current = self.scroll_cache_extent(app);
        if value == Some(current.value()) {
            return;
        }
        let replacement = match value {
            None => ScrollCacheExtent::Pixels(AnyRenderAbstractViewport::DEFAULT_CACHE_EXTENT),
            Some(value) => match current {
                ScrollCacheExtent::Pixels(_) => ScrollCacheExtent::Pixels(value),
                ScrollCacheExtent::Viewport(_) => ScrollCacheExtent::Viewport(value),
            },
        };
        self.viewport_data_mut(app).scroll_cache_extent = replacement;
        self.mark_needs_layout(app);
    }

    /// Deprecated. Use [`scroll_cache_extent`](Self::scroll_cache_extent) instead.
    #[deprecated(note = "Use scroll_cache_extent instead.")]
    fn cache_extent_style(self: RenderHandle<Self>, app: &App) -> CacheExtentStyle {
        self.scroll_cache_extent(app).style()
    }

    /// Deprecated. Use [`set_scroll_cache_extent`](Self::set_scroll_cache_extent) instead.
    #[deprecated(note = "Use set_scroll_cache_extent instead.")]
    fn set_cache_extent_style(self: RenderHandle<Self>, app: &mut App, value: CacheExtentStyle) {
        let current = self.scroll_cache_extent(app);
        if value == current.style() {
            return;
        }
        self.viewport_data_mut(app).scroll_cache_extent = match value {
            CacheExtentStyle::Pixel => ScrollCacheExtent::Pixels(current.value()),
            CacheExtentStyle::Viewport => ScrollCacheExtent::Viewport(current.value()),
        };
        self.mark_needs_layout(app);
    }

    /// The order in which to paint the slivers; equivalently, the order in which to arrange them
    /// in the z-direction.
    ///
    /// Whichever order the slivers are painted in, they will be hit-tested in the opposite
    /// order.
    ///
    /// The default is [`SliverPaintOrder::FirstIsTop`].
    fn paint_order(self: RenderHandle<Self>, app: &App) -> SliverPaintOrder {
        self.viewport_data(app).paint_order
    }

    /// Sets [`paint_order`](Self::paint_order).
    fn set_paint_order(self: RenderHandle<Self>, app: &mut App, value: SliverPaintOrder) {
        if value == self.paint_order(app) {
            return;
        }
        self.viewport_data_mut(app).paint_order = value;
        self.mark_needs_paint(app);
    }

    /// The content will be clipped (or not) according to this option.
    ///
    /// Defaults to [`Clip::HardEdge`].
    fn clip_behavior(self: RenderHandle<Self>, app: &App) -> Clip {
        self.viewport_data(app).clip_behavior
    }

    /// Sets [`clip_behavior`](Self::clip_behavior).
    fn set_clip_behavior(self: RenderHandle<Self>, app: &mut App, value: Clip) {
        if value == self.clip_behavior(app) {
            return;
        }
        self.viewport_data_mut(app).clip_behavior = value;
        self.mark_needs_paint(app);
    }

    /// The body of Flutter's `attach` override: listens to the offset.
    fn did_attach(
        self: RenderHandle<Self>,
        app: &mut App,
        owner: inset_foundation::Handle<PipelineOwner>,
    ) {
        ContainerRenderObjectMixin::did_attach(self, app, owner);
        self.offset(app)
            .add_listener(app, self.mark_needs_layout_listener());
    }

    /// The body of Flutter's `detach` override: stops listening to the offset.
    fn did_detach(self: RenderHandle<Self>, app: &mut App) {
        self.offset(app)
            .remove_listener(app, &self.mark_needs_layout_listener());
        ContainerRenderObjectMixin::did_detach(self, app);
    }

    /// Panics saying that the object does not support returning intrinsic dimensions if, in
    /// debug mode, we are not in the [`debug_checking_intrinsics`] mode.
    ///
    /// This is used by [`compute_min_intrinsic_width`](Self::compute_min_intrinsic_width) et al
    /// because viewports do not generally support returning intrinsic dimensions. See the
    /// discussion at [`RenderBox::compute_min_intrinsic_width`].
    fn debug_throw_if_not_checking_intrinsics(self: RenderHandle<Self>) -> bool {
        let _ = self;
        debug_assert!(
            debug_checking_intrinsics(),
            "{} does not support returning intrinsic dimensions.\n\
             Calculating the intrinsic dimensions would require instantiating every child of \
             the viewport, which defeats the point of viewports being lazy.\n\
             If you are merely trying to shrink-wrap the viewport in the main axis direction, \
             consider a RenderShrinkWrappingViewport render object (ShrinkWrappingViewport \
             widget), which achieves that effect without implementing the intrinsic dimension \
             API.",
            std::any::type_name::<Self>()
        );
        true
    }

    /// Zero; a viewport has no intrinsic dimensions.
    fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        let _ = (app, height);
        debug_assert!(self.debug_throw_if_not_checking_intrinsics());
        0.0
    }

    /// Zero; a viewport has no intrinsic dimensions.
    fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        let _ = (app, height);
        debug_assert!(self.debug_throw_if_not_checking_intrinsics());
        0.0
    }

    /// Zero; a viewport has no intrinsic dimensions.
    fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        let _ = (app, width);
        debug_assert!(self.debug_throw_if_not_checking_intrinsics());
        0.0
    }

    /// Zero; a viewport has no intrinsic dimensions.
    fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        let _ = (app, width);
        debug_assert!(self.debug_throw_if_not_checking_intrinsics());
        0.0
    }

    /// Determines the size and position of some of the children of the viewport.
    ///
    /// This function is the workhorse of `perform_layout` implementations in implementors.
    ///
    /// Layout starts with `child`, proceeds according to the `advance` callback, and stops once
    /// `advance` returns `None`.
    ///
    ///  * `scroll_offset` is the [`SliverConstraints::scroll_offset`] to pass the first child.
    ///    The scroll offset is adjusted by [`SliverGeometry::scroll_extent`] for subsequent
    ///    children.
    ///  * `overlap` is the [`SliverConstraints::overlap`] to pass the first child. The overlap
    ///    is adjusted by the [`SliverGeometry::paint_origin`] and
    ///    [`SliverGeometry::paint_extent`] for subsequent children.
    ///  * `layout_offset` is the layout offset at which to place the first child. The layout
    ///    offset is updated by the [`SliverGeometry::layout_extent`] for subsequent children.
    ///  * `remaining_paint_extent` is [`SliverConstraints::remaining_paint_extent`] to pass the
    ///    first child. The remaining paint extent is updated by the
    ///    [`SliverGeometry::layout_extent`] for subsequent children.
    ///  * `main_axis_extent` is the [`SliverConstraints::viewport_main_axis_extent`] to pass to
    ///    each child.
    ///  * `cross_axis_extent` is the [`SliverConstraints::cross_axis_extent`] to pass to each
    ///    child.
    ///  * `growth_direction` is the [`SliverConstraints::growth_direction`] to pass to each
    ///    child.
    ///
    /// Returns the first non-zero [`SliverGeometry::scroll_offset_correction`] encountered, if
    /// any. Otherwise returns 0.0. Typical callers will call this function repeatedly until it
    /// returns 0.0.
    #[allow(clippy::too_many_arguments)]
    fn layout_child_sequence(
        self: RenderHandle<Self>,
        app: &mut App,
        child: Option<AnyRenderSliver>,
        scroll_offset: f64,
        overlap: f64,
        layout_offset: f64,
        remaining_paint_extent: f64,
        main_axis_extent: f64,
        cross_axis_extent: f64,
        growth_direction: GrowthDirection,
        advance: &SliverAdvance<Self>,
        remaining_cache_extent: f64,
        cache_origin: f64,
    ) -> f64 {
        debug_assert!(scroll_offset.is_finite());
        debug_assert!(scroll_offset >= 0.0);
        let initial_layout_offset = layout_offset;
        let adjusted_user_scroll_direction = apply_growth_direction_to_scroll_direction(
            self.offset(app).user_scroll_direction(app),
            growth_direction,
        );
        let axis_direction = self.axis_direction(app);
        let cross_axis_direction = self.cross_axis_direction(app);
        let mut max_paint_offset = layout_offset + overlap;
        let mut preceding_scroll_extent = 0.0;
        let mut scroll_offset = scroll_offset;
        let mut layout_offset = layout_offset;
        let mut remaining_cache_extent = remaining_cache_extent;
        let mut cache_origin = cache_origin;
        let mut child = child;

        while let Some(current) = child {
            let sliver_scroll_offset = scroll_offset.max(0.0);
            // If the scrollOffset is too small we adjust the paddedOrigin because it doesn't
            // make sense to ask a sliver for content before its scroll offset.
            let corrected_cache_origin = cache_origin.max(-sliver_scroll_offset);
            let cache_extent_correction = cache_origin - corrected_cache_origin;

            debug_assert!(sliver_scroll_offset >= corrected_cache_origin.abs());
            debug_assert!(corrected_cache_origin <= 0.0);
            debug_assert!(sliver_scroll_offset >= 0.0);
            debug_assert!(cache_extent_correction <= 0.0);

            current.layout(
                app,
                SliverConstraints::new(
                    axis_direction,
                    growth_direction,
                    adjusted_user_scroll_direction,
                    sliver_scroll_offset,
                    preceding_scroll_extent,
                    max_paint_offset - layout_offset,
                    (remaining_paint_extent - layout_offset + initial_layout_offset).max(0.0),
                    cross_axis_extent,
                    cross_axis_direction,
                    main_axis_extent,
                    (remaining_cache_extent + cache_extent_correction).max(0.0),
                    corrected_cache_origin,
                ),
                true,
            );

            let child_layout_geometry = current.geometry(app);
            debug_assert!(child_layout_geometry.debug_assert_is_valid());

            // If there is a correction to apply, we'll have to start over.
            if let Some(correction) = child_layout_geometry.scroll_offset_correction {
                return correction;
            }

            // We use the child's paint origin in our coordinate system as the layoutOffset we
            // store in the child's parent data.
            let effective_layout_offset = layout_offset + child_layout_geometry.paint_origin;

            // `effective_layout_offset` becomes meaningless once we moved past the trailing edge
            // because `child_layout_geometry.layout_extent` is zero. Using the still increasing
            // `scroll_offset` to roughly position these invisible slivers in the right order.
            if child_layout_geometry.visible || scroll_offset > 0.0 {
                self.update_child_layout_offset(
                    app,
                    current,
                    effective_layout_offset,
                    growth_direction,
                );
            } else {
                self.update_child_layout_offset(
                    app,
                    current,
                    -scroll_offset + initial_layout_offset,
                    growth_direction,
                );
            }

            max_paint_offset = (effective_layout_offset + child_layout_geometry.paint_extent)
                .max(max_paint_offset);
            scroll_offset -= child_layout_geometry.scroll_extent;
            preceding_scroll_extent += child_layout_geometry.scroll_extent;
            layout_offset += child_layout_geometry.layout_extent;
            if child_layout_geometry.cache_extent != 0.0 {
                remaining_cache_extent -=
                    child_layout_geometry.cache_extent - cache_extent_correction;
                cache_origin =
                    (corrected_cache_origin + child_layout_geometry.cache_extent).min(0.0);
            }

            self.update_out_of_band_data(app, growth_direction, child_layout_geometry);

            // move on to the next child
            child = advance(self, app, current);
        }

        // we made it without a correction, whee!
        0.0
    }

    /// The body of Flutter's `paint` override.
    fn paint(
        self: RenderHandle<Self>,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
    ) {
        if self.first_child(app).is_none() {
            return;
        }
        let clip_behavior = self.clip_behavior(app);
        if self.has_visual_overflow(app) && clip_behavior != Clip::None {
            let bounds = Offset::ZERO & self.size(app);
            let old = self.viewport_data(app).clip_rect_layer.layer();
            let layer = context.push_clip_rect(
                app,
                self.as_object().needs_compositing(app),
                offset,
                bounds,
                |app, context, offset| self.paint_contents(app, context, offset),
                clip_behavior,
                old,
            );
            LayerHandle::set_layer(
                app,
                |app| &mut self.viewport_data_mut(app).clip_rect_layer,
                layer,
            );
        } else {
            LayerHandle::set_layer(
                app,
                |app| &mut self.viewport_data_mut(app).clip_rect_layer,
                None,
            );
            self.paint_contents(app, context, offset);
        }
    }

    /// Flutter's `dispose`: drop the clip layer handle, then `super.dispose()`.
    fn dispose(self: RenderHandle<Self>, app: &mut App) {
        LayerHandle::set_layer(
            app,
            |app| &mut self.viewport_data_mut(app).clip_rect_layer,
            None,
        );
        crate::object::RenderObjectBase::dispose(self, app);
    }

    /// Flutter's `_paintContents`.
    fn paint_contents(
        self: RenderHandle<Self>,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
    ) {
        for child in self.children_in_paint_order(app) {
            if child.geometry(app).visible {
                let child_offset = offset + self.paint_offset_of(app, child);
                context.paint_child(app, child.as_object(), child_offset);
            }
        }
    }

    /// The body of Flutter's `hitTestChildren` override.
    fn hit_test_children(
        self: RenderHandle<Self>,
        app: &mut App,
        result: &mut BoxHitTestResult<'_>,
        position: Offset,
    ) -> bool {
        let (main_axis_position, cross_axis_position) = match self.axis(app) {
            Axis::Vertical => (position.dy(), position.dx()),
            Axis::Horizontal => (position.dx(), position.dy()),
        };
        for child in self.children_in_hit_test_order(app) {
            if !child.geometry(app).visible {
                continue;
            }
            let mut transform = Matrix4::IDENTITY;
            // must be invertible
            RenderObject::apply_paint_transform(self, app, child.as_object(), &mut transform);
            let child_main_axis_position =
                self.compute_child_main_axis_position(app, child, main_axis_position);
            let is_hit =
                result.add_with_out_of_band_position(None, Some(transform), None, |result| {
                    child.hit_test(
                        app,
                        &mut SliverHitTestResult::wrap(result),
                        child_main_axis_position,
                        cross_axis_position,
                    )
                });
            if is_hit {
                return true;
            }
        }
        false
    }

    /// The body of Flutter's `getOffsetToReveal` override.
    fn get_offset_to_reveal(
        self: RenderHandle<Self>,
        app: &App,
        target: AnyRenderObject,
        alignment: f64,
        rect: Option<Rect>,
        _axis: Option<Axis>,
    ) -> RevealedOffset {
        // One dimensional viewport has only one axis, override if it was provided/may be
        // mismatched.
        let axis = self.axis(app);
        let this = self.as_object();

        // Steps to convert `rect` (from a RenderBox coordinate system) to its scroll offset
        // within this viewport (not in the exact order):
        //
        // 1. Pick the outermost RenderBox (between which, and the viewport, there is nothing but
        // RenderSlivers) as an intermediate reference frame (the `pivot`), convert `rect` to
        // that coordinate space.
        //
        // 2. Convert `rect` from the `pivot` coordinate space to its sliver parent's sliver
        // coordinate system (i.e., to a scroll offset), based on the axis direction and growth
        // direction of the parent.
        //
        // 3. Convert the scroll offset to its sliver parent's coordinate space using
        // `child_scroll_offset`, until we reach the viewport.
        //
        // 4. Make the final conversion from the outmost sliver to the viewport using
        // `scroll_offset_of`.

        let mut leading_scroll_offset = 0.0;
        // Starting at `target` and walking towards the root:
        //  - `child` will be the last object before we reach this viewport, and
        //  - `pivot` will be the last RenderBox before we reach this viewport.
        let mut child = target;
        let mut pivot = None;
        // ... between viewport and `target` (`target` included).
        let mut only_slivers = target.as_sliver().is_some();
        let mut rect = rect;
        while child.parent(app) != Some(this) {
            let parent = child
                .parent(app)
                .expect("target is a descendant of this viewport");
            if let Some(box_child) = child.as_box() {
                pivot = Some(box_child);
            }
            match parent.as_sliver() {
                Some(sliver) => {
                    leading_scroll_offset += sliver
                        .child_scroll_offset(app, child)
                        .expect("a sliver parent reports its child's scroll offset");
                }
                None => {
                    only_slivers = false;
                    leading_scroll_offset = 0.0;
                }
            }
            child = parent;
        }

        // `rect` in the new intermediate coordinate system.
        let rect_local;
        // Our new reference frame render object's main axis extent.
        let pivot_extent;
        let growth_direction;

        // `leading_scroll_offset` is currently the scrollOffset of our new reference frame
        // (`pivot` or `target`), within `child`.
        if let Some(pivot) = pivot {
            debug_assert!(pivot.as_object().parent(app).is_some());
            debug_assert_ne!(pivot.as_object().parent(app), Some(this));
            debug_assert_ne!(pivot.as_object(), this);
            let pivot_parent = pivot
                .as_object()
                .parent(app)
                .and_then(AnyRenderObject::as_sliver)
                .expect("only sliver parents are supported between a pivot and the viewport");
            growth_direction = pivot_parent.constraints(app).growth_direction;
            pivot_extent = match axis {
                Axis::Horizontal => pivot.size(app).width(),
                Axis::Vertical => pivot.size(app).height(),
            };
            let rect = *rect.get_or_insert_with(|| target.paint_bounds(app));
            rect_local =
                transform_rect(&target.get_transform_to(app, Some(pivot.as_object())), rect);
        } else if only_slivers {
            // `pivot` does not exist. We'll have to make up one from `target`, the innermost
            // sliver.
            let target_sliver = target.as_sliver().expect("only_slivers");
            growth_direction = target_sliver.constraints(app).growth_direction;
            pivot_extent = target_sliver.geometry(app).scroll_extent;
            rect_local = *rect.get_or_insert_with(|| {
                let constraints = target_sliver.constraints(app);
                let geometry = target_sliver.geometry(app);
                match axis {
                    Axis::Horizontal => Rect::from_ltwh(
                        0.0,
                        0.0,
                        geometry.scroll_extent,
                        constraints.cross_axis_extent,
                    ),
                    Axis::Vertical => Rect::from_ltwh(
                        0.0,
                        0.0,
                        constraints.cross_axis_extent,
                        geometry.scroll_extent,
                    ),
                }
            });
        } else {
            let rect = rect.expect("a non-sliver target must be given a rect");
            return RevealedOffset::new(self.offset(app).pixels(app), rect);
        }

        debug_assert_eq!(child.parent(app), Some(this));
        let sliver = child
            .as_sliver()
            .expect("a viewport's children are slivers");

        // The scroll offset of `rect` within `child`.
        leading_scroll_offset += match apply_growth_direction_to_axis_direction(
            self.axis_direction(app),
            growth_direction,
        ) {
            AxisDirection::Up => pivot_extent - rect_local.bottom,
            AxisDirection::Left => pivot_extent - rect_local.right,
            AxisDirection::Right => rect_local.left,
            AxisDirection::Down => rect_local.top,
        };

        // So far leading_scroll_offset is the scroll offset of `rect` in the `child` sliver's
        // sliver coordinate system. The sign of this value indicates whether the `rect`
        // protrudes the leading edge of the `child` sliver. When this value is non-negative and
        // `child`'s `max_scroll_obstruction_extent` is greater than 0, we assume `rect` can't be
        // obstructed by the leading edge of the viewport (i.e. it is pinned to the leading edge).
        let is_pinned = sliver.geometry(app).max_scroll_obstruction_extent > 0.0
            && leading_scroll_offset >= 0.0;

        // The scroll offset in the viewport to `rect`.
        leading_scroll_offset = self.scroll_offset_of(app, sliver, leading_scroll_offset);

        // This step assumes the viewport's layout is up-to-date, i.e., if offset.pixels is
        // changed after the last perform_layout, the new scroll position will not be accounted
        // for.
        let transform = target.get_transform_to(app, Some(this));
        let mut target_rect = transform_rect(&transform, rect.expect("set above"));
        let extent_of_pinned_slivers = self.max_scroll_obstruction_extent_before(app, sliver);

        match sliver.constraints(app).growth_direction {
            GrowthDirection::Forward => {
                if is_pinned && alignment <= 0.0 {
                    return RevealedOffset::new(f64::INFINITY, target_rect);
                }
                leading_scroll_offset -= extent_of_pinned_slivers;
            }
            GrowthDirection::Reverse => {
                if is_pinned && alignment >= 1.0 {
                    return RevealedOffset::new(f64::NEG_INFINITY, target_rect);
                }
                // If child's growth direction is reverse, when viewport.offset is
                // `leading_scroll_offset`, it is positioned just outside of the leading edge of
                // the viewport.
                leading_scroll_offset -= match axis {
                    Axis::Vertical => target_rect.height(),
                    Axis::Horizontal => target_rect.width(),
                };
            }
        }

        let size = self.size(app);
        let main_axis_extent_difference = match axis {
            Axis::Horizontal => size.width() - extent_of_pinned_slivers - rect_local.width(),
            Axis::Vertical => size.height() - extent_of_pinned_slivers - rect_local.height(),
        };

        let target_offset = leading_scroll_offset - main_axis_extent_difference * alignment;
        let offset_difference = self.offset(app).pixels(app) - target_offset;

        target_rect = match self.axis_direction(app) {
            AxisDirection::Up => target_rect.translate(0.0, -offset_difference),
            AxisDirection::Down => target_rect.translate(0.0, offset_difference),
            AxisDirection::Left => target_rect.translate(-offset_difference, 0.0),
            AxisDirection::Right => target_rect.translate(offset_difference, 0.0),
        };

        RevealedOffset::new(target_offset, target_rect)
    }

    /// The offset at which the given `child` should be painted.
    ///
    /// The returned offset is from the top left corner of the inside of the viewport to the top
    /// left corner of the paint coordinate system of the `child`.
    ///
    /// See also:
    ///
    ///  * [`paint_offset_of`](Self::paint_offset_of), which uses the layout offset and growth
    ///    direction computed for the child during layout.
    fn compute_absolute_paint_offset(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderSliver,
        layout_offset: f64,
        growth_direction: GrowthDirection,
    ) -> Offset {
        // this is only usable once we have a size
        debug_assert!(self.has_size(app));
        let paint_extent = child.geometry(app).paint_extent;
        let size = self.size(app);
        match apply_growth_direction_to_axis_direction(self.axis_direction(app), growth_direction) {
            AxisDirection::Up => Offset::new(0.0, size.height() - layout_offset - paint_extent),
            AxisDirection::Left => Offset::new(size.width() - layout_offset - paint_extent, 0.0),
            AxisDirection::Right => Offset::new(layout_offset, 0.0),
            AxisDirection::Down => Offset::new(0.0, layout_offset),
        }
    }

    // API TO BE IMPLEMENTED BY SUBCLASSES

    /// Whether the contents of this viewport would paint outside the bounds of the viewport if
    /// [`paint`](Self::paint) did not clip.
    ///
    /// This property enables an optimization whereby [`paint`](Self::paint) can skip applying a
    /// clip if the contents of the viewport are known to paint entirely within its bounds.
    fn has_visual_overflow(self: RenderHandle<Self>, app: &App) -> bool;

    /// Called during [`layout_child_sequence`](Self::layout_child_sequence) for each child.
    ///
    /// Typically used by implementors to update any out-of-band data, such as the max scroll
    /// extent, for each child.
    fn update_out_of_band_data(
        self: RenderHandle<Self>,
        app: &mut App,
        growth_direction: GrowthDirection,
        child_layout_geometry: SliverGeometry,
    );

    /// Called during [`layout_child_sequence`](Self::layout_child_sequence) to store the layout
    /// offset for the given child.
    ///
    /// Different implementors use different representations for their children's layout offset
    /// (e.g., logical or physical coordinates). This function lets them transform the child's
    /// layout offset before storing it in the child's parent data.
    fn update_child_layout_offset(
        self: RenderHandle<Self>,
        app: &mut App,
        child: AnyRenderSliver,
        layout_offset: f64,
        growth_direction: GrowthDirection,
    );

    /// The offset at which the given `child` should be painted.
    ///
    /// The returned offset is from the top left corner of the inside of the viewport to the top
    /// left corner of the paint coordinate system of the `child`.
    ///
    /// See also:
    ///
    ///  * [`compute_absolute_paint_offset`](Self::compute_absolute_paint_offset), which computes
    ///    the paint offset from an explicit layout offset and growth direction instead of using
    ///    the values computed for the child during layout.
    fn paint_offset_of(self: RenderHandle<Self>, app: &App, child: AnyRenderSliver) -> Offset;

    /// Returns the scroll offset within the viewport for the given `scroll_offset_within_child`
    /// within the given `child`.
    ///
    /// The returned value is an estimate that assumes the slivers within the viewport do not
    /// change the layout extent in response to changes in their scroll offset.
    fn scroll_offset_of(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderSliver,
        scroll_offset_within_child: f64,
    ) -> f64;

    /// Returns the total scroll obstruction extent of all slivers in the viewport before
    /// `child`.
    ///
    /// This is the extent by which the actual area in which content can scroll is reduced. For
    /// example, an app bar that is pinned at the top will reduce the area in which content can
    /// actually scroll by the height of the app bar.
    fn max_scroll_obstruction_extent_before(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderSliver,
    ) -> f64;

    /// Converts the `parent_main_axis_position` into the child's coordinate system.
    ///
    /// The `parent_main_axis_position` is a distance from the top edge (for vertical viewports)
    /// or left edge (for horizontal viewports) of the viewport bounds. This describes a line,
    /// perpendicular to the viewport's main axis, heretofore known as the target line.
    ///
    /// The child's coordinate system's origin in the main axis is at the leading edge of the
    /// given child, as given by the child's [`SliverConstraints::axis_direction`] and
    /// [`SliverConstraints::growth_direction`].
    ///
    /// This method returns the distance from the leading edge of the given child to the target
    /// line described above.
    fn compute_child_main_axis_position(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderSliver,
        parent_main_axis_position: f64,
    ) -> f64;

    /// The index of the first child of the viewport relative to the center child.
    ///
    /// For example, the center child has index zero and the first child in the reverse growth
    /// direction has index -1.
    fn index_of_first_child(self: RenderHandle<Self>, app: &App) -> i32;

    /// A short string to identify the child with the given index.
    fn label_for_child(self: RenderHandle<Self>, index: i32) -> String;

    /// Walks the children of the viewport, in the order that they should be painted.
    ///
    /// This is the reverse order of
    /// [`children_in_hit_test_order`](Self::children_in_hit_test_order).
    fn children_in_paint_order(self: RenderHandle<Self>, app: &App) -> Vec<AnyRenderSliver> {
        match self.paint_order(app) {
            SliverPaintOrder::FirstIsTop => self.children_last_to_first(app),
            SliverPaintOrder::LastIsTop => self.children_as_list(app),
        }
    }

    /// Walks the children of the viewport, in the order that hit-testing should use.
    ///
    /// This is the reverse order of [`children_in_paint_order`](Self::children_in_paint_order).
    fn children_in_hit_test_order(self: RenderHandle<Self>, app: &App) -> Vec<AnyRenderSliver> {
        match self.paint_order(app) {
            SliverPaintOrder::FirstIsTop => self.children_as_list(app),
            SliverPaintOrder::LastIsTop => self.children_last_to_first(app),
        }
    }

    /// Flutter's `_childrenLastToFirst`.
    fn children_last_to_first(self: RenderHandle<Self>, app: &App) -> Vec<AnyRenderSliver> {
        let mut children = Vec::new();
        let mut child = self.last_child(app);
        while let Some(current) = child {
            children.push(current);
            child = self.child_before(app, current);
        }
        children
    }

    /// The body of Flutter's `showOnScreen` override.
    fn show_on_screen(
        self: RenderHandle<Self>,
        app: &mut App,
        descendant: Option<AnyRenderObject>,
        rect: Option<Rect>,
        duration: Duration,
        curve: Rc<dyn Curve>,
    ) {
        let offset = self.offset(app);
        if !offset.allow_implicit_scrolling(app) {
            return crate::object::RenderObjectBase::show_on_screen(
                self, app, descendant, rect, duration, curve,
            );
        }

        let new_rect = show_in_viewport(
            app,
            descendant,
            rect,
            self.as_abstract_viewport(),
            offset,
            duration,
            Rc::clone(&curve),
        );
        crate::object::RenderObjectBase::show_on_screen(self, app, None, new_rect, duration, curve);
    }
}

/// Dart's `_offset.addListener(markNeedsLayout)` tear-off body: a named function so that the
/// listener added on attach compares equal to the one removed on detach.
fn viewport_offset_changed<T: RenderViewportBase>(
    this: inset_foundation::Handle<T>,
    app: &mut App,
) {
    RenderHandle::from_handle(this).mark_needs_layout(app);
}

/// Make (a portion of) the given `descendant` of the given `viewport` fully visible in the
/// `viewport` by manipulating the provided [`AnyViewportOffset`] `offset`.
///
/// The optional `rect` parameter describes which area of the `descendant` should be shown in the
/// viewport. If `rect` is `None`, the entire `descendant` will be revealed. The `rect` parameter
/// is interpreted relative to the coordinate system of `descendant`.
///
/// The returned [`Rect`] describes the new location of `descendant` or `rect` in the viewport
/// after it has been revealed. See [`RevealedOffset::rect`] for a full definition of this rect.
///
/// If `descendant` is `None`, this is a no-op and `rect` is returned.
///
/// The `duration` parameter can be set to a non-zero value to animate the target object into the
/// viewport with an animation defined by `curve`.
///
/// Flutter's `RenderViewportBase.showInViewport` static.
pub fn show_in_viewport(
    app: &mut App,
    descendant: Option<AnyRenderObject>,
    rect: Option<Rect>,
    viewport: AnyRenderAbstractViewport,
    offset: AnyViewportOffset,
    duration: Duration,
    curve: Rc<dyn Curve>,
) -> Option<Rect> {
    let Some(descendant) = descendant else {
        return rect;
    };
    let leading_edge_offset = viewport.get_offset_to_reveal(app, descendant, 0.0, rect, None);
    let trailing_edge_offset = viewport.get_offset_to_reveal(app, descendant, 1.0, rect, None);
    let current_offset = offset.pixels(app);
    let target_offset =
        RevealedOffset::clamp_offset(leading_edge_offset, trailing_edge_offset, current_offset);
    let Some(target_offset) = target_offset else {
        // `descendant` is between leading and trailing edge and hence already fully shown on
        // screen. No action necessary.
        let viewport_object = viewport.as_object(app);
        debug_assert!(viewport_object.parent(app).is_some());
        let transform = descendant.get_transform_to(app, viewport_object.parent(app));
        return Some(transform_rect(
            &transform,
            rect.unwrap_or_else(|| descendant.paint_bounds(app)),
        ));
    };

    offset.move_to(app, target_offset.offset, Some(duration), Some(curve), None);
    Some(target_offset.rect)
}

/// A render object that is bigger on the inside.
///
/// [`RenderViewport`] is the visual workhorse of the scrolling machinery. It displays a subset of
/// its children according to its own dimensions and the given
/// [`offset`](RenderViewportBase::offset). As the offset varies, different children are visible
/// through the viewport.
///
/// [`RenderViewport`] hosts a bidirectional list of slivers in a single shared [`Axis`], anchored
/// on a [`center`](Self::center) sliver, which is placed at the zero scroll offset. The center
/// widget is displayed in the viewport according to the [`anchor`](Self::anchor) property.
///
/// Slivers that are earlier in the child list than [`center`](Self::center) are displayed in
/// reverse order in the reverse [`RenderViewportBase::axis_direction`] starting from the center.
///
/// [`RenderViewport`] cannot contain [`RenderBox`] children directly. Instead, use a
/// [`crate::RenderSliverList`], [`crate::RenderSliverFixedExtentList`], or a
/// [`crate::RenderSliverToBoxAdapter`], for example.
pub struct RenderViewport {
    render_object: RenderObjectData,
    render_box: RenderBoxData,
    container: ContainerRenderObjectData<AnyRenderSliver>,
    viewport: RenderViewportBaseData,
    anchor: f64,
    center: Option<AnyRenderSliver>,
    // Out-of-band data computed during layout.
    min_scroll_extent: f64,
    max_scroll_extent: f64,
    has_visual_overflow: bool,
}

impl RenderViewport {
    /// Dart's `_maxLayoutCyclesPerChild`.
    const MAX_LAYOUT_CYCLES_PER_CHILD: usize = 10;

    /// Creates a viewport for [`AnyRenderSliver`] objects.
    ///
    /// If the `center` is not specified, then the first child in the `children` list, if any, is
    /// used.
    ///
    /// For testing purposes, consider passing a [`crate::FixedViewportOffset`] as the `offset`.
    pub fn new(
        app: &mut App,
        cross_axis_direction: AxisDirection,
        offset: AnyViewportOffset,
        children: Option<Vec<AnyRenderSliver>>,
        center: Option<AnyRenderSliver>,
    ) -> RenderHandle<RenderViewport> {
        let this = RenderHandle::new_box(
            app,
            RenderViewport {
                render_object: RenderObjectData::new(),
                render_box: RenderBoxData::new(),
                container: ContainerRenderObjectData::new(),
                viewport: RenderViewportBaseData::new(cross_axis_direction, offset),
                anchor: 0.0,
                center,
                min_scroll_extent: 0.0,
                max_scroll_extent: 0.0,
                has_visual_overflow: false,
            },
        );
        this.add_all(app, children);
        if center.is_none()
            && let Some(first_child) = this.first_child(app)
        {
            this.get_mut(app).center = Some(first_child);
        }
        this
    }

    /// The relative position of the zero scroll offset.
    ///
    /// For example, if the [`RenderViewportBase::axis_direction`] is [`AxisDirection::Down`] or
    /// [`AxisDirection::Up`] and the anchor is 0.5, then the zero scroll offset is vertically
    /// centered within the viewport. If the anchor is 1.0, and the axis direction is
    /// [`AxisDirection::Right`], then the zero scroll offset is on the left edge of the viewport.
    pub fn anchor(self: RenderHandle<Self>, app: &App) -> f64 {
        self.get(app).anchor
    }

    /// Sets [`anchor`](Self::anchor).
    pub fn set_anchor(self: RenderHandle<Self>, app: &mut App, value: f64) {
        debug_assert!((0.0..=1.0).contains(&value));
        if value == self.get(app).anchor {
            return;
        }
        self.get_mut(app).anchor = value;
        self.mark_needs_layout(app);
    }

    /// The first child in the [`GrowthDirection::Forward`] growth direction.
    ///
    /// This child will be at the position defined by [`anchor`](Self::anchor) when the
    /// [`crate::ViewportOffset::pixels`] of [`RenderViewportBase::offset`] is `0`.
    ///
    /// Children after the center will be placed in the [`RenderViewportBase::axis_direction`]
    /// relative to the center. Children before it will be placed in the opposite direction, and
    /// will have a growth direction of [`GrowthDirection::Reverse`].
    ///
    /// The center must be a direct child of the viewport.
    pub fn center(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderSliver> {
        self.get(app).center
    }

    /// Sets [`center`](Self::center).
    pub fn set_center(self: RenderHandle<Self>, app: &mut App, value: Option<AnyRenderSliver>) {
        if value == self.get(app).center {
            return;
        }
        self.get_mut(app).center = value;
        self.mark_needs_layout(app);
    }

    /// Flutter's `_attemptLayout`.
    fn attempt_layout(
        self: RenderHandle<Self>,
        app: &mut App,
        main_axis_extent: f64,
        cross_axis_extent: f64,
        corrected_offset: f64,
    ) -> f64 {
        debug_assert!(!main_axis_extent.is_nan());
        debug_assert!(main_axis_extent >= 0.0);
        debug_assert!(main_axis_extent.is_finite());
        debug_assert!(cross_axis_extent.is_finite());
        debug_assert!(cross_axis_extent >= 0.0);
        debug_assert!(corrected_offset.is_finite());
        let this = self.get_mut(app);
        this.min_scroll_extent = 0.0;
        this.max_scroll_extent = 0.0;
        this.has_visual_overflow = false;

        // center_offset is the offset from the leading edge of the viewport to the zero scroll
        // offset (the line between the forward slivers and the reverse slivers).
        let anchor = self.get(app).anchor;
        let center_offset = main_axis_extent * anchor - corrected_offset;
        let reverse_direction_remaining_paint_extent =
            clamp_double(center_offset, 0.0, main_axis_extent);
        let forward_direction_remaining_paint_extent =
            clamp_double(main_axis_extent - center_offset, 0.0, main_axis_extent);

        let calculated_cache_extent = self
            .scroll_cache_extent(app)
            .calculate_cache_offset(main_axis_extent);
        self.viewport_data_mut(app).calculated_cache_extent = Some(calculated_cache_extent);

        let full_cache_extent = main_axis_extent + 2.0 * calculated_cache_extent;
        let center_cache_offset = center_offset + calculated_cache_extent;
        let reverse_direction_remaining_cache_extent =
            clamp_double(center_cache_offset, 0.0, full_cache_extent);
        let forward_direction_remaining_cache_extent = clamp_double(
            full_cache_extent - center_cache_offset,
            0.0,
            full_cache_extent,
        );

        let center = self.center(app).expect("checked by perform_layout");
        let leading_negative_child = self.child_before(app, center);

        if let Some(leading_negative_child) = leading_negative_child {
            // negative scroll offsets
            let result = self.layout_child_sequence(
                app,
                Some(leading_negative_child),
                main_axis_extent.max(center_offset) - main_axis_extent,
                0.0,
                forward_direction_remaining_paint_extent,
                reverse_direction_remaining_paint_extent,
                main_axis_extent,
                cross_axis_extent,
                GrowthDirection::Reverse,
                &|this, app, child| this.child_before(app, child),
                reverse_direction_remaining_cache_extent,
                clamp_double(
                    main_axis_extent - center_offset,
                    -calculated_cache_extent,
                    0.0,
                ),
            );
            if result != 0.0 {
                return -result;
            }
        }

        // positive scroll offsets
        self.layout_child_sequence(
            app,
            Some(center),
            (-center_offset).max(0.0),
            if leading_negative_child.is_none() {
                (-center_offset).min(0.0)
            } else {
                0.0
            },
            if center_offset >= main_axis_extent {
                center_offset
            } else {
                reverse_direction_remaining_paint_extent
            },
            forward_direction_remaining_paint_extent,
            main_axis_extent,
            cross_axis_extent,
            GrowthDirection::Forward,
            &|this, app, child| this.child_after(app, child),
            forward_direction_remaining_cache_extent,
            clamp_double(center_offset, -calculated_cache_extent, 0.0),
        )
    }
}

impl ContainerRenderObjectMixin for RenderViewport {
    type ChildType = AnyRenderSliver;
    type ParentDataType = SliverPhysicalContainerParentData;

    fn container_data(
        self: RenderHandle<Self>,
        app: &App,
    ) -> &ContainerRenderObjectData<AnyRenderSliver> {
        &self.get(app).container
    }

    fn container_data_mut(
        self: RenderHandle<Self>,
        app: &mut App,
    ) -> &mut ContainerRenderObjectData<AnyRenderSliver> {
        &mut self.get_mut(app).container
    }
}

impl RenderViewportBase for RenderViewport {
    crate::render_viewport_base_accessors!();

    fn has_visual_overflow(self: RenderHandle<Self>, app: &App) -> bool {
        self.get(app).has_visual_overflow
    }

    fn update_out_of_band_data(
        self: RenderHandle<Self>,
        app: &mut App,
        growth_direction: GrowthDirection,
        child_layout_geometry: SliverGeometry,
    ) {
        let this = self.get_mut(app);
        match growth_direction {
            GrowthDirection::Forward => {
                this.max_scroll_extent += child_layout_geometry.scroll_extent
            }
            GrowthDirection::Reverse => {
                this.min_scroll_extent -= child_layout_geometry.scroll_extent
            }
        }
        if child_layout_geometry.has_visual_overflow {
            this.has_visual_overflow = true;
        }
    }

    fn update_child_layout_offset(
        self: RenderHandle<Self>,
        app: &mut App,
        child: AnyRenderSliver,
        layout_offset: f64,
        growth_direction: GrowthDirection,
    ) {
        let paint_offset =
            self.compute_absolute_paint_offset(app, child, layout_offset, growth_direction);
        child
            .as_object()
            .parent_data_of_mut::<SliverPhysicalContainerParentData>(app)
            .set_paint_offset(paint_offset);
    }

    fn paint_offset_of(self: RenderHandle<Self>, app: &App, child: AnyRenderSliver) -> Offset {
        child
            .as_object()
            .parent_data_of::<SliverPhysicalContainerParentData>(app)
            .paint_offset()
    }

    fn scroll_offset_of(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderSliver,
        scroll_offset_within_child: f64,
    ) -> f64 {
        debug_assert_eq!(child.as_object().parent(app), Some(self.as_object()));
        match child.constraints(app).growth_direction {
            GrowthDirection::Forward => {
                let mut scroll_offset_to_child = 0.0;
                let mut current = self.center(app);
                while current != Some(child) {
                    let sliver = current.expect("the center precedes a forward child");
                    scroll_offset_to_child += sliver.geometry(app).scroll_extent;
                    current = self.child_after(app, sliver);
                }
                scroll_offset_to_child + scroll_offset_within_child
            }
            GrowthDirection::Reverse => {
                let mut scroll_offset_to_child = 0.0;
                let center = self.center(app).expect("a laid-out viewport has a center");
                let mut current = self.child_before(app, center);
                while current != Some(child) {
                    let sliver = current.expect("a reverse child precedes the center");
                    scroll_offset_to_child -= sliver.geometry(app).scroll_extent;
                    current = self.child_before(app, sliver);
                }
                scroll_offset_to_child - scroll_offset_within_child
            }
        }
    }

    fn max_scroll_obstruction_extent_before(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderSliver,
    ) -> f64 {
        debug_assert_eq!(child.as_object().parent(app), Some(self.as_object()));
        let mut pinned_extent = 0.0;
        match child.constraints(app).growth_direction {
            GrowthDirection::Forward => {
                let mut current = self.center(app);
                while current != Some(child) {
                    let sliver = current.expect("the center precedes a forward child");
                    pinned_extent += sliver.geometry(app).max_scroll_obstruction_extent;
                    current = self.child_after(app, sliver);
                }
            }
            GrowthDirection::Reverse => {
                let center = self.center(app).expect("a laid-out viewport has a center");
                let mut current = self.child_before(app, center);
                while current != Some(child) {
                    let sliver = current.expect("a reverse child precedes the center");
                    pinned_extent += sliver.geometry(app).max_scroll_obstruction_extent;
                    current = self.child_before(app, sliver);
                }
            }
        }
        pinned_extent
    }

    fn compute_child_main_axis_position(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderSliver,
        parent_main_axis_position: f64,
    ) -> f64 {
        let paint_offset = child
            .as_object()
            .parent_data_of::<SliverPhysicalContainerParentData>(app)
            .paint_offset();
        let constraints = child.constraints(app);
        let paint_extent = child.geometry(app).paint_extent;
        match apply_growth_direction_to_axis_direction(
            constraints.axis_direction,
            constraints.growth_direction,
        ) {
            AxisDirection::Down => parent_main_axis_position - paint_offset.dy(),
            AxisDirection::Right => parent_main_axis_position - paint_offset.dx(),
            AxisDirection::Up => paint_extent - (parent_main_axis_position - paint_offset.dy()),
            AxisDirection::Left => paint_extent - (parent_main_axis_position - paint_offset.dx()),
        }
    }

    fn index_of_first_child(self: RenderHandle<Self>, app: &App) -> i32 {
        let center = self.center(app).expect("a laid-out viewport has a center");
        debug_assert_eq!(center.as_object().parent(app), Some(self.as_object()));
        debug_assert!(self.first_child(app).is_some());
        let mut count = 0;
        let mut child = Some(center);
        while child != self.first_child(app) {
            count -= 1;
            child = self.child_before(app, child.expect("the first child terminates the walk"));
        }
        count
    }

    fn label_for_child(self: RenderHandle<Self>, index: i32) -> String {
        if index == 0 {
            "center child".to_owned()
        } else {
            format!("child {index}")
        }
    }
}

impl RenderAbstractViewport for RenderViewport {
    fn get_offset_to_reveal(
        self: RenderHandle<Self>,
        app: &App,
        target: AnyRenderObject,
        alignment: f64,
        rect: Option<Rect>,
        axis: Option<Axis>,
    ) -> RevealedOffset {
        RenderViewportBase::get_offset_to_reveal(self, app, target, alignment, rect, axis)
    }
}

impl RenderObject for RenderViewport {
    crate::render_object_accessors!();

    fn visit_children(
        self: RenderHandle<Self>,
        app: &App,
        visitor: &mut dyn FnMut(AnyRenderObject),
    ) {
        ContainerRenderObjectMixin::visit_children(self, app, visitor)
    }

    fn did_attach(
        self: RenderHandle<Self>,
        app: &mut App,
        owner: inset_foundation::Handle<PipelineOwner>,
    ) {
        RenderViewportBase::did_attach(self, app, owner)
    }

    fn did_detach(self: RenderHandle<Self>, app: &mut App) {
        RenderViewportBase::did_detach(self, app)
    }

    fn redepth_children(self: RenderHandle<Self>, app: &mut App) {
        ContainerRenderObjectMixin::redepth_children(self, app)
    }

    fn is_repaint_boundary(self: RenderHandle<Self>, _app: &App) -> bool {
        let _ = self;
        true
    }

    fn interface(self: RenderHandle<Self>, id: TypeId) -> Option<Box<dyn Any>> {
        (id == TypeId::of::<AnyRenderAbstractViewport>())
            .then(|| Box::new(self.as_abstract_viewport()) as Box<dyn Any>)
    }

    fn sized_by_parent(self: RenderHandle<Self>, _app: &App) -> bool {
        let _ = self;
        true
    }

    fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
        // Ignore the return value of apply_viewport_dimension because we are doing a layout
        // regardless.
        let offset = self.offset(app);
        let size = self.size(app);
        match self.axis(app) {
            Axis::Vertical => offset.apply_viewport_dimension(app, size.height()),
            Axis::Horizontal => offset.apply_viewport_dimension(app, size.width()),
        };

        let Some(center) = self.center(app) else {
            debug_assert!(self.first_child(app).is_none());
            let this = self.get_mut(app);
            this.min_scroll_extent = 0.0;
            this.max_scroll_extent = 0.0;
            this.has_visual_overflow = false;
            offset.apply_content_dimensions(app, 0.0, 0.0);
            return;
        };
        debug_assert_eq!(center.as_object().parent(app), Some(self.as_object()));

        let (main_axis_extent, cross_axis_extent) = match self.axis(app) {
            Axis::Vertical => (size.height(), size.width()),
            Axis::Horizontal => (size.width(), size.height()),
        };

        let center_offset_adjustment = center.center_offset_adjustment(app);
        let max_layout_cycles = RenderViewport::MAX_LAYOUT_CYCLES_PER_CHILD * self.child_count(app);

        let anchor = self.get(app).anchor;
        let mut count = 0;
        loop {
            let correction = self.attempt_layout(
                app,
                main_axis_extent,
                cross_axis_extent,
                offset.pixels(app) + center_offset_adjustment,
            );
            if correction != 0.0 {
                offset.correct_by(app, correction);
            } else {
                let min_scroll_extent = self.get(app).min_scroll_extent;
                let max_scroll_extent = self.get(app).max_scroll_extent;
                if offset.apply_content_dimensions(
                    app,
                    (min_scroll_extent + main_axis_extent * anchor).min(0.0),
                    (max_scroll_extent - main_axis_extent * (1.0 - anchor)).max(0.0),
                ) {
                    break;
                }
            }
            count += 1;
            if count >= max_layout_cycles {
                break;
            }
        }
        debug_assert!(
            count < max_layout_cycles,
            "A RenderViewport exceeded its maximum number of layout cycles.\n\
             RenderViewport render objects, during layout, can retry if either their slivers or \
             their ViewportOffset decide that the offset should be corrected to take into \
             account information collected during that layout.\n\
             In the case of this RenderViewport object, however, this happened {count} times and \
             still there was no consensus on the scroll offset. This usually indicates a bug."
        );
    }

    fn paint(
        self: RenderHandle<Self>,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
    ) {
        RenderViewportBase::paint(self, app, context, offset)
    }

    fn dispose(self: RenderHandle<Self>, app: &mut App) {
        RenderViewportBase::dispose(self, app)
    }

    fn show_on_screen(
        self: RenderHandle<Self>,
        app: &mut App,
        descendant: Option<AnyRenderObject>,
        rect: Option<Rect>,
        duration: Duration,
        curve: Rc<dyn Curve>,
    ) {
        RenderViewportBase::show_on_screen(self, app, descendant, rect, duration, curve)
    }
}

impl RenderBox for RenderViewport {
    crate::render_box_accessors!();

    fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
        if !child.parent_data_is::<SliverPhysicalContainerParentData>(app) {
            child.set_parent_data(app, SliverPhysicalContainerParentData::new());
        }
    }

    fn apply_paint_transform(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderObject,
        transform: &mut Matrix4,
    ) {
        // Hit test logic relies on this always providing an invertible matrix.
        child
            .parent_data_of::<SliverPhysicalContainerParentData>(app)
            .apply_paint_transform(transform);
    }

    fn hit_test_children(
        self: RenderHandle<Self>,
        app: &mut App,
        result: &mut BoxHitTestResult<'_>,
        position: Offset,
    ) -> bool {
        RenderViewportBase::hit_test_children(self, app, result, position)
    }

    fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        RenderViewportBase::compute_min_intrinsic_width(self, app, height)
    }

    fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        RenderViewportBase::compute_max_intrinsic_width(self, app, height)
    }

    fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        RenderViewportBase::compute_min_intrinsic_height(self, app, width)
    }

    fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        RenderViewportBase::compute_max_intrinsic_height(self, app, width)
    }

    fn compute_dry_layout(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
    ) -> Size {
        debug_assert!(debug_check_has_bounded_axis(self.axis(app), constraints));
        constraints.biggest()
    }
}

/// A render object that is bigger on the inside and shrink wraps its children in the main axis.
///
/// [`RenderShrinkWrappingViewport`] displays a subset of its children according to its own
/// dimensions and the given [`RenderViewportBase::offset`]. As the offset varies, different
/// children are visible through the viewport.
///
/// It differs from [`RenderViewport`] in that [`RenderViewport`] expands to fill the main axis
/// whereas this shrink-wraps itself to match its children in the main axis. This shrink wrapping
/// behavior is expensive because the children, and hence the viewport, could potentially change
/// size whenever the offset changes (e.g., because of a collapsing header).
///
/// [`RenderShrinkWrappingViewport`] cannot contain [`RenderBox`] children directly. Instead, use
/// a [`crate::RenderSliverList`], [`crate::RenderSliverFixedExtentList`], or a
/// [`crate::RenderSliverToBoxAdapter`], for example.
pub struct RenderShrinkWrappingViewport {
    render_object: RenderObjectData,
    render_box: RenderBoxData,
    container: ContainerRenderObjectData<AnyRenderSliver>,
    viewport: RenderViewportBaseData,
    // Out-of-band data computed during layout.
    max_scroll_extent: f64,
    shrink_wrap_extent: f64,
    has_visual_overflow: bool,
}

impl RenderShrinkWrappingViewport {
    /// Creates a viewport (for [`AnyRenderSliver`] objects) that shrink-wraps its contents.
    ///
    /// For testing purposes, consider passing a [`crate::FixedViewportOffset`] as the `offset`.
    pub fn new(
        app: &mut App,
        cross_axis_direction: AxisDirection,
        offset: AnyViewportOffset,
        children: Option<Vec<AnyRenderSliver>>,
    ) -> RenderHandle<RenderShrinkWrappingViewport> {
        let this = RenderHandle::new_box(
            app,
            RenderShrinkWrappingViewport {
                render_object: RenderObjectData::new(),
                render_box: RenderBoxData::new(),
                container: ContainerRenderObjectData::new(),
                viewport: RenderViewportBaseData::new(cross_axis_direction, offset),
                max_scroll_extent: 0.0,
                shrink_wrap_extent: 0.0,
                has_visual_overflow: false,
            },
        );
        this.add_all(app, children);
        this
    }

    /// Flutter's `_debugCheckHasBoundedCrossAxis`.
    fn debug_check_has_bounded_cross_axis(self: RenderHandle<Self>, app: &App) -> bool {
        let constraints = self.constraints(app);
        match self.axis(app) {
            Axis::Vertical => debug_assert!(
                constraints.has_bounded_width(),
                "Vertical viewport was given unbounded width.\n\
                 Viewports expand in the cross axis to fill their container and constrain their \
                 children to match their extent in the cross axis. In this case, a vertical \
                 shrinkwrapping viewport was given an unlimited amount of horizontal space in \
                 which to expand."
            ),
            Axis::Horizontal => debug_assert!(
                constraints.has_bounded_height(),
                "Horizontal viewport was given unbounded height.\n\
                 Viewports expand in the cross axis to fill their container and constrain their \
                 children to match their extent in the cross axis. In this case, a horizontal \
                 shrinkwrapping viewport was given an unlimited amount of vertical space in \
                 which to expand."
            ),
        }
        true
    }

    /// Flutter's `_attemptLayout`.
    fn attempt_layout(
        self: RenderHandle<Self>,
        app: &mut App,
        main_axis_extent: f64,
        cross_axis_extent: f64,
        corrected_offset: f64,
    ) -> f64 {
        // We can't assert main_axis_extent is finite, because it could be infinite if it is
        // within a column or row for example. In such a case, there's not even any scrolling to
        // do, although some scroll physics could still temporarily scroll the content in a
        // simulation.
        debug_assert!(!main_axis_extent.is_nan());
        debug_assert!(main_axis_extent >= 0.0);
        debug_assert!(cross_axis_extent.is_finite());
        debug_assert!(cross_axis_extent >= 0.0);
        debug_assert!(corrected_offset.is_finite());
        let this = self.get_mut(app);
        this.max_scroll_extent = 0.0;
        this.shrink_wrap_extent = 0.0;
        // Since the viewport is shrinkwrapped, we know that any negative overscroll into the
        // potentially infinite main_axis_extent will overflow the end of the viewport.
        this.has_visual_overflow = corrected_offset < 0.0;
        let calculated_cache_extent = if main_axis_extent.is_finite() {
            self.scroll_cache_extent(app)
                .calculate_cache_offset(main_axis_extent)
        } else {
            // If main_axis_extent is infinite, it builds everything anyway, so we don't need any
            // extra cache.
            0.0
        };
        self.viewport_data_mut(app).calculated_cache_extent = Some(calculated_cache_extent);

        let first_child = self.first_child(app);
        self.layout_child_sequence(
            app,
            first_child,
            corrected_offset.max(0.0),
            corrected_offset.min(0.0),
            (-corrected_offset).max(0.0),
            main_axis_extent + corrected_offset.min(0.0),
            main_axis_extent,
            cross_axis_extent,
            GrowthDirection::Forward,
            &|this, app, child| this.child_after(app, child),
            main_axis_extent + 2.0 * calculated_cache_extent,
            -calculated_cache_extent,
        )
    }
}

impl ContainerRenderObjectMixin for RenderShrinkWrappingViewport {
    type ChildType = AnyRenderSliver;
    type ParentDataType = SliverLogicalContainerParentData;

    fn container_data(
        self: RenderHandle<Self>,
        app: &App,
    ) -> &ContainerRenderObjectData<AnyRenderSliver> {
        &self.get(app).container
    }

    fn container_data_mut(
        self: RenderHandle<Self>,
        app: &mut App,
    ) -> &mut ContainerRenderObjectData<AnyRenderSliver> {
        &mut self.get_mut(app).container
    }
}

impl RenderViewportBase for RenderShrinkWrappingViewport {
    crate::render_viewport_base_accessors!();

    fn debug_throw_if_not_checking_intrinsics(self: RenderHandle<Self>) -> bool {
        let _ = self;
        debug_assert!(
            debug_checking_intrinsics(),
            "{} does not support returning intrinsic dimensions.\n\
             Calculating the intrinsic dimensions would require instantiating every child of \
             the viewport, which defeats the point of viewports being lazy.\n\
             If you are merely trying to shrink-wrap the viewport in the main axis direction, \
             you should be able to achieve that effect by just giving the viewport loose \
             constraints, without needing to measure its intrinsic dimensions.",
            std::any::type_name::<Self>()
        );
        true
    }

    fn has_visual_overflow(self: RenderHandle<Self>, app: &App) -> bool {
        self.get(app).has_visual_overflow
    }

    fn update_out_of_band_data(
        self: RenderHandle<Self>,
        app: &mut App,
        growth_direction: GrowthDirection,
        child_layout_geometry: SliverGeometry,
    ) {
        debug_assert_eq!(growth_direction, GrowthDirection::Forward);
        let this = self.get_mut(app);
        this.max_scroll_extent += child_layout_geometry.scroll_extent;
        if child_layout_geometry.has_visual_overflow {
            this.has_visual_overflow = true;
        }
        this.shrink_wrap_extent += child_layout_geometry.max_paint_extent;
    }

    fn update_child_layout_offset(
        self: RenderHandle<Self>,
        app: &mut App,
        child: AnyRenderSliver,
        layout_offset: f64,
        growth_direction: GrowthDirection,
    ) {
        debug_assert_eq!(growth_direction, GrowthDirection::Forward);
        child
            .as_object()
            .parent_data_of_mut::<SliverLogicalContainerParentData>(app)
            .set_layout_offset(Some(layout_offset));
    }

    fn paint_offset_of(self: RenderHandle<Self>, app: &App, child: AnyRenderSliver) -> Offset {
        let layout_offset = child
            .as_object()
            .parent_data_of::<SliverLogicalContainerParentData>(app)
            .layout_offset()
            .expect("a laid-out child has a layout offset");
        self.compute_absolute_paint_offset(app, child, layout_offset, GrowthDirection::Forward)
    }

    fn scroll_offset_of(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderSliver,
        scroll_offset_within_child: f64,
    ) -> f64 {
        debug_assert_eq!(child.as_object().parent(app), Some(self.as_object()));
        debug_assert_eq!(
            child.constraints(app).growth_direction,
            GrowthDirection::Forward
        );
        let mut scroll_offset_to_child = 0.0;
        let mut current = self.first_child(app);
        while current != Some(child) {
            let sliver = current.expect("child is in the child list");
            scroll_offset_to_child += sliver.geometry(app).scroll_extent;
            current = self.child_after(app, sliver);
        }
        scroll_offset_to_child + scroll_offset_within_child
    }

    fn max_scroll_obstruction_extent_before(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderSliver,
    ) -> f64 {
        debug_assert_eq!(child.as_object().parent(app), Some(self.as_object()));
        debug_assert_eq!(
            child.constraints(app).growth_direction,
            GrowthDirection::Forward
        );
        let mut pinned_extent = 0.0;
        let mut current = self.first_child(app);
        while current != Some(child) {
            let sliver = current.expect("child is in the child list");
            pinned_extent += sliver.geometry(app).max_scroll_obstruction_extent;
            current = self.child_after(app, sliver);
        }
        pinned_extent
    }

    fn compute_child_main_axis_position(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderSliver,
        parent_main_axis_position: f64,
    ) -> f64 {
        debug_assert!(self.has_size(app));
        let layout_offset = child
            .as_object()
            .parent_data_of::<SliverLogicalContainerParentData>(app)
            .layout_offset()
            .expect("a laid-out child has a layout offset");
        let constraints = child.constraints(app);
        let size = self.size(app);
        match apply_growth_direction_to_axis_direction(
            constraints.axis_direction,
            constraints.growth_direction,
        ) {
            AxisDirection::Down | AxisDirection::Right => parent_main_axis_position - layout_offset,
            AxisDirection::Up => size.height() - parent_main_axis_position - layout_offset,
            AxisDirection::Left => size.width() - parent_main_axis_position - layout_offset,
        }
    }

    fn index_of_first_child(self: RenderHandle<Self>, _app: &App) -> i32 {
        let _ = self;
        0
    }

    fn label_for_child(self: RenderHandle<Self>, index: i32) -> String {
        let _ = self;
        format!("child {index}")
    }
}

impl RenderAbstractViewport for RenderShrinkWrappingViewport {
    fn get_offset_to_reveal(
        self: RenderHandle<Self>,
        app: &App,
        target: AnyRenderObject,
        alignment: f64,
        rect: Option<Rect>,
        axis: Option<Axis>,
    ) -> RevealedOffset {
        RenderViewportBase::get_offset_to_reveal(self, app, target, alignment, rect, axis)
    }
}

impl RenderObject for RenderShrinkWrappingViewport {
    crate::render_object_accessors!();

    fn visit_children(
        self: RenderHandle<Self>,
        app: &App,
        visitor: &mut dyn FnMut(AnyRenderObject),
    ) {
        ContainerRenderObjectMixin::visit_children(self, app, visitor)
    }

    fn did_attach(
        self: RenderHandle<Self>,
        app: &mut App,
        owner: inset_foundation::Handle<PipelineOwner>,
    ) {
        RenderViewportBase::did_attach(self, app, owner)
    }

    fn did_detach(self: RenderHandle<Self>, app: &mut App) {
        RenderViewportBase::did_detach(self, app)
    }

    fn redepth_children(self: RenderHandle<Self>, app: &mut App) {
        ContainerRenderObjectMixin::redepth_children(self, app)
    }

    fn is_repaint_boundary(self: RenderHandle<Self>, _app: &App) -> bool {
        let _ = self;
        true
    }

    fn interface(self: RenderHandle<Self>, id: TypeId) -> Option<Box<dyn Any>> {
        (id == TypeId::of::<AnyRenderAbstractViewport>())
            .then(|| Box::new(self.as_abstract_viewport()) as Box<dyn Any>)
    }

    fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
        let constraints = self.constraints(app);
        let offset = self.offset(app);
        if self.first_child(app).is_none() {
            // Shrinkwrapping viewport only requires the cross axis to be bounded.
            debug_assert!(self.debug_check_has_bounded_cross_axis(app));
            let size = match self.axis(app) {
                Axis::Vertical => Size::new(constraints.max_width, constraints.min_height),
                Axis::Horizontal => Size::new(constraints.min_width, constraints.max_height),
            };
            self.set_size(app, size);
            offset.apply_viewport_dimension(app, 0.0);
            let this = self.get_mut(app);
            this.max_scroll_extent = 0.0;
            this.shrink_wrap_extent = 0.0;
            this.has_visual_overflow = false;
            offset.apply_content_dimensions(app, 0.0, 0.0);
            return;
        }

        // Shrinkwrapping viewport only requires the cross axis to be bounded.
        debug_assert!(self.debug_check_has_bounded_cross_axis(app));
        let (main_axis_extent, cross_axis_extent) = match self.axis(app) {
            Axis::Vertical => (constraints.max_height, constraints.max_width),
            Axis::Horizontal => (constraints.max_width, constraints.max_height),
        };

        let effective_extent;
        loop {
            let corrected_offset = offset.pixels(app);
            let correction =
                self.attempt_layout(app, main_axis_extent, cross_axis_extent, corrected_offset);
            if correction != 0.0 {
                offset.correct_by(app, correction);
                continue;
            }
            let shrink_wrap_extent = self.get(app).shrink_wrap_extent;
            let extent = match self.axis(app) {
                Axis::Vertical => constraints.constrain_height(shrink_wrap_extent),
                Axis::Horizontal => constraints.constrain_width(shrink_wrap_extent),
            };
            let did_accept_viewport_dimension = offset.apply_viewport_dimension(app, extent);
            let max_scroll_extent = self.get(app).max_scroll_extent;
            let did_accept_content_dimension =
                offset.apply_content_dimensions(app, 0.0, (max_scroll_extent - extent).max(0.0));
            if did_accept_viewport_dimension && did_accept_content_dimension {
                effective_extent = extent;
                break;
            }
        }
        let size = match self.axis(app) {
            Axis::Vertical => constraints.constrain_dimensions(cross_axis_extent, effective_extent),
            Axis::Horizontal => {
                constraints.constrain_dimensions(effective_extent, cross_axis_extent)
            }
        };
        self.set_size(app, size);
    }

    fn paint(
        self: RenderHandle<Self>,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
    ) {
        RenderViewportBase::paint(self, app, context, offset)
    }

    fn dispose(self: RenderHandle<Self>, app: &mut App) {
        RenderViewportBase::dispose(self, app)
    }

    fn show_on_screen(
        self: RenderHandle<Self>,
        app: &mut App,
        descendant: Option<AnyRenderObject>,
        rect: Option<Rect>,
        duration: Duration,
        curve: Rc<dyn Curve>,
    ) {
        RenderViewportBase::show_on_screen(self, app, descendant, rect, duration, curve)
    }
}

impl RenderBox for RenderShrinkWrappingViewport {
    crate::render_box_accessors!();

    fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
        if !child.parent_data_is::<SliverLogicalContainerParentData>(app) {
            child.set_parent_data(app, SliverLogicalContainerParentData::new());
        }
    }

    fn apply_paint_transform(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderObject,
        transform: &mut Matrix4,
    ) {
        // Hit test logic relies on this always providing an invertible matrix.
        let offset = self.paint_offset_of(
            app,
            child
                .as_sliver()
                .expect("a viewport's children are slivers"),
        );
        translate(transform, offset.dx(), offset.dy());
    }

    fn hit_test_children(
        self: RenderHandle<Self>,
        app: &mut App,
        result: &mut BoxHitTestResult<'_>,
        position: Offset,
    ) -> bool {
        RenderViewportBase::hit_test_children(self, app, result, position)
    }

    fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        RenderViewportBase::compute_min_intrinsic_width(self, app, height)
    }

    fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        RenderViewportBase::compute_max_intrinsic_width(self, app, height)
    }

    fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        RenderViewportBase::compute_min_intrinsic_height(self, app, width)
    }

    fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        RenderViewportBase::compute_max_intrinsic_height(self, app, width)
    }
}

#[cfg(test)]
mod tests {
    use inset_foundation::AppCell;
    use std::cell::Cell;
    use std::rc::Rc;

    use inset_foundation::Handle;
    use inset_gestures::HitTestResult;

    use super::*;
    use crate::box_::BoxConstraints;
    use crate::layer::{ContainerLayer, ErasedLayer, OffsetLayer};
    use crate::proxy_box::RenderConstrainedBox;
    use crate::sliver::{RenderSliver, RenderSliverToBoxAdapter};
    use crate::viewport_offset::{FixedViewportOffset, ViewportOffset};

    /// A sliver of a fixed scroll extent that records the offsets it was painted at.
    struct TestSliver {
        render_object: RenderObjectData,
        render_sliver: crate::sliver::RenderSliverData,
        extent: f64,
        paint_offsets: Rc<Cell<Vec<Offset>>>,
    }

    impl RenderObject for TestSliver {
        crate::render_object_accessors!();

        fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
            let constraints = self.constraints(app);
            let extent = self.get(app).extent;
            let painted = self.calculate_paint_offset(app, constraints, 0.0, extent);
            let cached = self.calculate_cache_offset(app, constraints, 0.0, extent);
            self.set_geometry(
                app,
                SliverGeometry::new()
                    .scroll_extent(extent)
                    .paint_extent(painted)
                    .cache_extent(cached)
                    .max_paint_extent(extent),
            );
        }

        fn paint(
            self: RenderHandle<Self>,
            app: &mut App,
            _context: &mut PaintingContext,
            offset: Offset,
        ) {
            let recorded = self.get(app).paint_offsets.clone();
            let mut offsets = recorded.take();
            offsets.push(offset);
            recorded.set(offsets);
        }
    }

    impl RenderSliver for TestSliver {
        crate::render_sliver_accessors!();

        fn hit_test_self(
            self: RenderHandle<Self>,
            _app: &App,
            _main_axis_position: f64,
            _cross_axis_position: f64,
        ) -> bool {
            let _ = self;
            true
        }
    }

    fn test_sliver(app: &mut App, extent: f64) -> (AnyRenderSliver, Rc<Cell<Vec<Offset>>>) {
        let paint_offsets = Rc::new(Cell::new(Vec::new()));
        let sliver = RenderHandle::new_sliver(
            app,
            TestSliver {
                render_object: RenderObjectData::new(),
                render_sliver: crate::sliver::RenderSliverData::new(),
                extent,
                paint_offsets: paint_offsets.clone(),
            },
        )
        .as_sliver();
        (sliver, paint_offsets)
    }

    /// The test binding's first frame: root the tree, lay it out, and flush paint.
    fn first_frame(
        app: &mut App,
        root: crate::box_::AnyRenderBox,
        constraints: BoxConstraints,
    ) -> Handle<PipelineOwner> {
        let owner = PipelineOwner::new(app, None);
        owner.set_root_node(app, Some(root.as_object()));
        root.layout(app, constraints, false);
        let paint_root = OffsetLayer::new(app, Offset::ZERO);
        paint_root.as_layer().attach(app, root.as_object().id());
        root.as_object()
            .schedule_initial_paint(app, paint_root.as_container_layer());
        owner.flush_compositing_bits(app);
        owner.flush_paint(app);
        owner
    }

    fn vertical_viewport(
        app: &mut App,
        offset: Handle<FixedViewportOffset>,
        children: Vec<AnyRenderSliver>,
    ) -> RenderHandle<RenderViewport> {
        RenderViewport::new(
            app,
            AxisDirection::Right,
            offset.as_viewport_offset(),
            Some(children),
            None,
        )
    }

    /// `viewport_test.dart`: a viewport does not support intrinsics, because answering would
    /// mean instantiating every child.
    #[test]
    #[should_panic(expected = "does not support returning intrinsic dimensions")]
    fn a_viewport_has_no_intrinsic_dimensions() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let offset = FixedViewportOffset::zero(&mut app);
        let viewport = vertical_viewport(&mut app, offset, vec![]);
        viewport.as_box().get_min_intrinsic_width(&mut app, 0.0);
    }

    /// While the framework is checking intrinsics, a viewport answers zero instead of panicking.
    #[test]
    fn a_viewport_reports_zero_intrinsics_while_checking_them() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let offset = FixedViewportOffset::zero(&mut app);
        let viewport = vertical_viewport(&mut app, offset, vec![]);
        crate::object::set_debug_checking_intrinsics(true);
        let width = viewport.as_box().get_min_intrinsic_width(&mut app, 0.0);
        let height = viewport.as_box().get_max_intrinsic_height(&mut app, 0.0);
        crate::object::set_debug_checking_intrinsics(false);
        assert_eq!((width, height), (0.0, 0.0));
    }

    /// A viewport is sized by its parent: its dry layout is the biggest size the constraints
    /// allow, and that is the size it takes.
    #[test]
    fn a_viewport_dry_layout_is_the_biggest_size() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let (sliver, _paints) = test_sliver(&mut app, 100.0);
        let offset = FixedViewportOffset::zero(&mut app);
        let viewport = vertical_viewport(&mut app, offset, vec![sliver]);
        let constraints = BoxConstraints::new().max_width(200.0).max_height(400.0);
        let dry = viewport.as_box().get_dry_layout(&mut app, constraints);
        first_frame(&mut app, viewport.as_box(), constraints);
        assert_eq!(dry, Size::new(200.0, 400.0));
        assert_eq!(dry, viewport.size(&app));
    }

    /// `viewport_test.dart`: the slivers are laid out one after the other from the leading edge,
    /// and the viewport reports the total scroll extent to its offset.
    #[test]
    fn a_viewport_lays_its_slivers_out_in_order() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let (first, first_paints) = test_sliver(&mut app, 100.0);
        let (second, second_paints) = test_sliver(&mut app, 300.0);
        let offset = FixedViewportOffset::zero(&mut app);
        let viewport = vertical_viewport(&mut app, offset, vec![first, second]);

        first_frame(
            &mut app,
            viewport.as_box(),
            BoxConstraints::tight(Size::new(200.0, 400.0)),
        );

        assert_eq!(viewport.size(&app), Size::new(200.0, 400.0));
        assert_eq!(viewport.center(&app), Some(first));
        assert_eq!(first.geometry(&app).paint_extent, 100.0);
        assert_eq!(second.geometry(&app).paint_extent, 300.0);
        assert_eq!(viewport.paint_offset_of(&app, first), Offset::ZERO);
        assert_eq!(
            viewport.paint_offset_of(&app, second),
            Offset::new(0.0, 100.0)
        );
        assert_eq!(first_paints.take(), vec![Offset::ZERO]);
        assert_eq!(second_paints.take(), vec![Offset::new(0.0, 100.0)]);
    }

    /// A non-zero offset scrolls the content: the first sliver is partly scrolled off and the
    /// second moves up by the same amount.
    #[test]
    fn a_scrolled_viewport_moves_its_slivers() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let (first, _) = test_sliver(&mut app, 100.0);
        let (second, _) = test_sliver(&mut app, 300.0);
        let offset = FixedViewportOffset::new(&mut app, 60.0);
        let viewport = vertical_viewport(&mut app, offset, vec![first, second]);

        first_frame(
            &mut app,
            viewport.as_box(),
            BoxConstraints::tight(Size::new(200.0, 400.0)),
        );

        assert_eq!(first.constraints(&app).scroll_offset, 60.0);
        assert_eq!(first.geometry(&app).paint_extent, 40.0);
        assert_eq!(viewport.paint_offset_of(&app, first), Offset::ZERO);
        assert_eq!(
            viewport.paint_offset_of(&app, second),
            Offset::new(0.0, 40.0)
        );
        assert_eq!(viewport.scroll_offset_of(&app, second, 0.0), 100.0);
    }

    /// A hit lands on the sliver that covers the position, in the viewport's coordinate system.
    #[test]
    fn a_viewport_hit_tests_the_sliver_under_the_position() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let (first, _) = test_sliver(&mut app, 100.0);
        let (second, _) = test_sliver(&mut app, 300.0);
        let offset = FixedViewportOffset::zero(&mut app);
        let viewport = vertical_viewport(&mut app, offset, vec![first, second]);
        first_frame(
            &mut app,
            viewport.as_box(),
            BoxConstraints::tight(Size::new(200.0, 400.0)),
        );

        let mut result = HitTestResult::new();
        let hit = viewport.hit_test(
            &mut app,
            &mut BoxHitTestResult::wrap(&mut result),
            Offset::new(20.0, 150.0),
        );

        assert!(hit);
        assert_eq!(result.path().len(), 2);
        // The viewport and the second sliver, which starts at 100.
        assert_eq!(
            viewport.compute_child_main_axis_position(&app, second, 150.0),
            50.0
        );
    }

    /// The cache extent widens the region slivers are asked to lay out for.
    #[test]
    fn the_cache_extent_widens_the_laid_out_region() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let (sliver, _) = test_sliver(&mut app, 1000.0);
        let offset = FixedViewportOffset::zero(&mut app);
        let viewport = vertical_viewport(&mut app, offset, vec![sliver]);
        viewport.set_scroll_cache_extent(&mut app, Some(ScrollCacheExtent::Pixels(100.0)));

        first_frame(
            &mut app,
            viewport.as_box(),
            BoxConstraints::tight(Size::new(200.0, 400.0)),
        );

        assert_eq!(sliver.constraints(&app).remaining_cache_extent, 500.0);
        assert_eq!(sliver.geometry(&app).cache_extent, 500.0);
        assert_eq!(sliver.geometry(&app).paint_extent, 400.0);

        viewport.set_scroll_cache_extent(&mut app, Some(ScrollCacheExtent::Viewport(0.5)));
        viewport.as_box().layout(
            &mut app,
            BoxConstraints::tight(Size::new(200.0, 400.0)),
            false,
        );
        assert_eq!(sliver.constraints(&app).remaining_cache_extent, 600.0);
    }

    /// `RenderShrinkWrappingViewport` sizes itself to the total extent of its slivers, up to the
    /// incoming constraints.
    #[test]
    fn a_shrink_wrapping_viewport_sizes_itself_to_its_slivers() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let (first, _) = test_sliver(&mut app, 60.0);
        let (second, _) = test_sliver(&mut app, 90.0);
        let offset = FixedViewportOffset::zero(&mut app);
        let viewport = RenderShrinkWrappingViewport::new(
            &mut app,
            AxisDirection::Right,
            offset.as_viewport_offset(),
            Some(vec![first, second]),
        );

        first_frame(
            &mut app,
            viewport.as_box(),
            BoxConstraints::new()
                .max_width(200.0)
                .min_width(200.0)
                .max_height(400.0),
        );

        assert_eq!(viewport.size(&app), Size::new(200.0, 150.0));
        assert_eq!(
            viewport.paint_offset_of(&app, second),
            Offset::new(0.0, 60.0)
        );
        assert_eq!(viewport.index_of_first_child(&app), 0);
        assert_eq!(viewport.label_for_child(1), "child 1");
    }

    /// A box adapter inside a viewport reaches the box protocol: the box is laid out with the
    /// viewport's cross axis extent and painted at the sliver's paint offset.
    #[test]
    fn a_viewport_hosts_a_box_through_the_adapter() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let child =
            RenderConstrainedBox::new(&mut app, BoxConstraints::tight_for(None, Some(80.0)), None);
        let adapter = RenderSliverToBoxAdapter::new(&mut app, Some(child.as_box()));
        let offset = FixedViewportOffset::zero(&mut app);
        let viewport = vertical_viewport(&mut app, offset, vec![adapter.as_sliver()]);

        first_frame(
            &mut app,
            viewport.as_box(),
            BoxConstraints::tight(Size::new(200.0, 400.0)),
        );

        assert_eq!(child.size(&app), Size::new(200.0, 80.0));
        assert_eq!(adapter.geometry(&app).scroll_extent, 80.0);
        assert_eq!(
            child.as_box().local_to_global(&app, Offset::ZERO, None),
            Offset::ZERO
        );
    }

    /// The viewport is the `RenderAbstractViewport` that `maybe_of` finds from a descendant.
    #[test]
    fn maybe_of_finds_the_enclosing_viewport() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let child =
            RenderConstrainedBox::new(&mut app, BoxConstraints::tight_for(None, Some(80.0)), None);
        let adapter = RenderSliverToBoxAdapter::new(&mut app, Some(child.as_box()));
        let offset = FixedViewportOffset::zero(&mut app);
        let viewport = vertical_viewport(&mut app, offset, vec![adapter.as_sliver()]);
        first_frame(
            &mut app,
            viewport.as_box(),
            BoxConstraints::tight(Size::new(200.0, 400.0)),
        );

        let found = AnyRenderAbstractViewport::maybe_of(&app, Some(child.as_object()));
        assert_eq!(found, Some(viewport.as_abstract_viewport()));
        assert!(AnyRenderAbstractViewport::maybe_of(&app, None).is_none());
    }

    /// `get_offset_to_reveal` reports the offset that brings a descendant box to the leading edge.
    #[test]
    fn get_offset_to_reveal_reports_the_leading_edge_offset() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let leading =
            RenderConstrainedBox::new(&mut app, BoxConstraints::tight_for(None, Some(100.0)), None);
        let first = RenderSliverToBoxAdapter::new(&mut app, Some(leading.as_box()));
        let target =
            RenderConstrainedBox::new(&mut app, BoxConstraints::tight_for(None, Some(50.0)), None);
        let second = RenderSliverToBoxAdapter::new(&mut app, Some(target.as_box()));
        let offset = FixedViewportOffset::zero(&mut app);
        let viewport = vertical_viewport(
            &mut app,
            offset,
            vec![first.as_sliver(), second.as_sliver()],
        );
        first_frame(
            &mut app,
            viewport.as_box(),
            BoxConstraints::tight(Size::new(200.0, 400.0)),
        );

        let revealed = RenderAbstractViewport::get_offset_to_reveal(
            viewport,
            &app,
            target.as_object(),
            0.0,
            None,
            None,
        );
        assert_eq!(revealed.offset, 100.0);
        // At that offset the target sits at the leading edge of the viewport.
        assert_eq!(revealed.rect.top, 0.0);
        assert_eq!(revealed.rect.bottom, 50.0);
    }

    /// `RevealedOffset::clamp_offset` picks the nearer edge, and `None` when already visible.
    #[test]
    fn clamp_offset_picks_the_nearer_edge() {
        let leading = RevealedOffset::new(100.0, Rect::ZERO);
        let trailing = RevealedOffset::new(20.0, Rect::ZERO);
        assert_eq!(
            RevealedOffset::clamp_offset(leading, trailing, 200.0).map(|offset| offset.offset),
            Some(100.0)
        );
        assert_eq!(
            RevealedOffset::clamp_offset(leading, trailing, 0.0).map(|offset| offset.offset),
            Some(20.0)
        );
        assert!(RevealedOffset::clamp_offset(leading, trailing, 50.0).is_none());
    }

    /// The paint order decides which sliver paints last and which is hit-tested first.
    #[test]
    fn the_paint_order_reverses_the_hit_test_order() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let (first, _) = test_sliver(&mut app, 100.0);
        let (second, _) = test_sliver(&mut app, 300.0);
        let offset = FixedViewportOffset::zero(&mut app);
        let viewport = vertical_viewport(&mut app, offset, vec![first, second]);
        first_frame(
            &mut app,
            viewport.as_box(),
            BoxConstraints::tight(Size::new(200.0, 400.0)),
        );

        assert_eq!(viewport.children_in_paint_order(&app), vec![second, first]);
        assert_eq!(
            viewport.children_in_hit_test_order(&app),
            vec![first, second]
        );

        viewport.set_paint_order(&mut app, SliverPaintOrder::LastIsTop);
        assert_eq!(viewport.children_in_paint_order(&app), vec![first, second]);
        assert_eq!(
            viewport.children_in_hit_test_order(&app),
            vec![second, first]
        );
    }
}