inset-rendering 0.6.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
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
//! Flutter counterpart: `rendering/box.dart` (`BoxConstraints`, `RenderBox`
//! layout wrapper, intrinsics, dry layout and baselines).
//!
//! `_DebugSize` and `debugAssertDoesMeetConstraints` wait.

use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::fmt::{self, Debug, Display};
use std::hash::{Hash, Hasher};

use std::cell::Cell;
use std::ops::{Add, Deref, DerefMut};

use inset_embedder::{
    Matrix4, Offset, Rect, Size, TextBaseline, ViewConstraints, clamp_double, lerp_double,
};
use inset_foundation::{App, Handle, HandleId};
use inset_gestures::{
    HitTestEntry, HitTestResult, HitTestTarget, PointerEvent, remove_perspective_transform,
};
use inset_painting::EdgeInsetsGeometry;
use inset_painting::transform_point;

use crate::object::{
    AnyRenderObject, Constraints, ContainerParentDataMixin, ContainerRenderObjectMixin, ParentData,
    RenderHandle, RenderObject, RenderObjectVTable, create, debug_checking_intrinsics, resolve,
};
use crate::painting_context::PaintingContext;
use crate::pipeline_owner::PipelineOwner;

thread_local! {
    static DEBUG_DOING_BASELINE: Cell<bool> = const { Cell::new(false) };
}

/// Immutable layout constraints for `RenderBox` layout.
///
/// A [`Size`] respects a [`BoxConstraints`] if, and only if, all of the following
/// relations hold:
///
/// * [`min_width`](Self::min_width) <= [`Size::width`] <= [`max_width`](Self::max_width)
/// * [`min_height`](Self::min_height) <= [`Size::height`] <= [`max_height`](Self::max_height)
#[derive(Clone, Copy)]
pub struct BoxConstraints {
    /// The minimum width that satisfies the constraints.
    pub min_width: f64,
    /// The maximum width that satisfies the constraints.
    ///
    /// Might be [`f64::INFINITY`].
    pub max_width: f64,
    /// The minimum height that satisfies the constraints.
    pub min_height: f64,
    /// The maximum height that satisfies the constraints.
    ///
    /// Might be [`f64::INFINITY`].
    pub max_height: f64,
}

impl BoxConstraints {
    /// Creates box constraints with the given constraints.
    pub const fn new() -> BoxConstraints {
        BoxConstraints {
            min_width: 0.0,
            max_width: f64::INFINITY,
            min_height: 0.0,
            max_height: f64::INFINITY,
        }
    }

    /// Sets [`min_width`](Self::min_width).
    pub const fn min_width(mut self, value: f64) -> BoxConstraints {
        self.min_width = value;
        self
    }

    /// Sets [`max_width`](Self::max_width).
    pub const fn max_width(mut self, value: f64) -> BoxConstraints {
        self.max_width = value;
        self
    }

    /// Sets [`min_height`](Self::min_height).
    pub const fn min_height(mut self, value: f64) -> BoxConstraints {
        self.min_height = value;
        self
    }

    /// Sets [`max_height`](Self::max_height).
    pub const fn max_height(mut self, value: f64) -> BoxConstraints {
        self.max_height = value;
        self
    }

    /// Creates box constraints that is respected only by the given size.
    pub fn tight(size: Size) -> BoxConstraints {
        BoxConstraints {
            min_width: size.width(),
            max_width: size.width(),
            min_height: size.height(),
            max_height: size.height(),
        }
    }

    /// Creates box constraints that require the given width or height.
    pub const fn tight_for(width: Option<f64>, height: Option<f64>) -> BoxConstraints {
        BoxConstraints {
            min_width: match width {
                Some(width) => width,
                None => 0.0,
            },
            max_width: match width {
                Some(width) => width,
                None => f64::INFINITY,
            },
            min_height: match height {
                Some(height) => height,
                None => 0.0,
            },
            max_height: match height {
                Some(height) => height,
                None => f64::INFINITY,
            },
        }
    }

    /// Creates box constraints that require the given width or height, except if
    /// they are infinite.
    pub const fn tight_for_finite(width: f64, height: f64) -> BoxConstraints {
        BoxConstraints {
            min_width: if width != f64::INFINITY { width } else { 0.0 },
            max_width: if width != f64::INFINITY {
                width
            } else {
                f64::INFINITY
            },
            min_height: if height != f64::INFINITY { height } else { 0.0 },
            max_height: if height != f64::INFINITY {
                height
            } else {
                f64::INFINITY
            },
        }
    }

    /// Creates box constraints that forbid sizes larger than the given size.
    pub fn loose(size: Size) -> BoxConstraints {
        BoxConstraints {
            min_width: 0.0,
            max_width: size.width(),
            min_height: 0.0,
            max_height: size.height(),
        }
    }

    /// Creates box constraints that expand to fill another box constraints.
    pub const fn expand(width: Option<f64>, height: Option<f64>) -> BoxConstraints {
        BoxConstraints {
            min_width: match width {
                Some(width) => width,
                None => f64::INFINITY,
            },
            max_width: match width {
                Some(width) => width,
                None => f64::INFINITY,
            },
            min_height: match height {
                Some(height) => height,
                None => f64::INFINITY,
            },
            max_height: match height {
                Some(height) => height,
                None => f64::INFINITY,
            },
        }
    }

    /// Creates box constraints that match the given view constraints.
    pub const fn from_view_constraints(constraints: ViewConstraints) -> BoxConstraints {
        BoxConstraints {
            min_width: constraints.min_width,
            max_width: constraints.max_width,
            min_height: constraints.min_height,
            max_height: constraints.max_height,
        }
    }

    /// Creates a copy of this box constraints. Chain field replacements.
    pub const fn copy_with(self) -> BoxConstraints {
        self
    }

    /// Returns new box constraints that are smaller by the given edge dimensions.
    pub fn deflate(self, edges: EdgeInsetsGeometry) -> BoxConstraints {
        debug_assert!(self.debug_assert_is_valid(false));
        let horizontal = edges.horizontal();
        let vertical = edges.vertical();
        let deflated_min_width = (self.min_width - horizontal).max(0.0);
        let deflated_min_height = (self.min_height - vertical).max(0.0);
        BoxConstraints {
            min_width: deflated_min_width,
            max_width: (self.max_width - horizontal).max(deflated_min_width),
            min_height: deflated_min_height,
            max_height: (self.max_height - vertical).max(deflated_min_height),
        }
    }

    /// Returns new box constraints that remove the minimum width and height requirements.
    pub fn loosen(self) -> BoxConstraints {
        debug_assert!(self.debug_assert_is_valid(false));
        BoxConstraints {
            min_width: 0.0,
            max_width: self.max_width,
            min_height: 0.0,
            max_height: self.max_height,
        }
    }

    /// Returns new box constraints that respect the given constraints while being
    /// as close as possible to the original constraints.
    pub fn enforce(self, constraints: BoxConstraints) -> BoxConstraints {
        BoxConstraints {
            min_width: clamp_double(self.min_width, constraints.min_width, constraints.max_width),
            max_width: clamp_double(self.max_width, constraints.min_width, constraints.max_width),
            min_height: clamp_double(
                self.min_height,
                constraints.min_height,
                constraints.max_height,
            ),
            max_height: clamp_double(
                self.max_height,
                constraints.min_height,
                constraints.max_height,
            ),
        }
    }

    /// Returns new box constraints with a tight width and/or height as close to
    /// the given width and height as possible while still respecting the original
    /// box constraints.
    pub fn tighten(self, width: Option<f64>, height: Option<f64>) -> BoxConstraints {
        BoxConstraints {
            min_width: match width {
                Some(width) => clamp_double(width, self.min_width, self.max_width),
                None => self.min_width,
            },
            max_width: match width {
                Some(width) => clamp_double(width, self.min_width, self.max_width),
                None => self.max_width,
            },
            min_height: match height {
                Some(height) => clamp_double(height, self.min_height, self.max_height),
                None => self.min_height,
            },
            max_height: match height {
                Some(height) => clamp_double(height, self.min_height, self.max_height),
                None => self.max_height,
            },
        }
    }

    /// A box constraints with the width and height constraints flipped.
    pub const fn flipped(self) -> BoxConstraints {
        BoxConstraints {
            min_width: self.min_height,
            max_width: self.max_height,
            min_height: self.min_width,
            max_height: self.max_width,
        }
    }

    /// Returns box constraints with the same width constraints but with
    /// unconstrained height.
    pub const fn width_constraints(self) -> BoxConstraints {
        BoxConstraints {
            min_width: self.min_width,
            max_width: self.max_width,
            min_height: 0.0,
            max_height: f64::INFINITY,
        }
    }

    /// Returns box constraints with the same height constraints but with
    /// unconstrained width.
    pub const fn height_constraints(self) -> BoxConstraints {
        BoxConstraints {
            min_width: 0.0,
            max_width: f64::INFINITY,
            min_height: self.min_height,
            max_height: self.max_height,
        }
    }

    /// Returns the width that both satisfies the constraints and is as close as
    /// possible to the given width.
    pub fn constrain_width(self, width: f64) -> f64 {
        debug_assert!(self.debug_assert_is_valid(false));
        clamp_double(width, self.min_width, self.max_width)
    }

    /// Returns the height that both satisfies the constraints and is as close as
    /// possible to the given height.
    pub fn constrain_height(self, height: f64) -> f64 {
        debug_assert!(self.debug_assert_is_valid(false));
        clamp_double(height, self.min_height, self.max_height)
    }

    /// Returns the size that both satisfies the constraints and is as close as
    /// possible to the given size.
    pub fn constrain(self, size: Size) -> Size {
        Size::new(
            self.constrain_width(size.width()),
            self.constrain_height(size.height()),
        )
    }

    /// Returns the size that both satisfies the constraints and is as close as
    /// possible to the given width and height.
    pub fn constrain_dimensions(self, width: f64, height: f64) -> Size {
        Size::new(self.constrain_width(width), self.constrain_height(height))
    }

    /// Returns a size that attempts to meet the following conditions, in order:
    ///
    ///  * The size must satisfy these constraints.
    ///  * The aspect ratio of the returned size matches the aspect ratio of the
    ///    given size.
    ///  * The returned size is as big as possible while still being equal to or
    ///    smaller than the given size.
    pub fn constrain_size_and_attempt_to_preserve_aspect_ratio(self, size: Size) -> Size {
        if self.is_tight() {
            return self.smallest();
        }
        if size.is_empty() {
            return self.constrain(size);
        }
        let mut width = size.width();
        let mut height = size.height();
        let aspect_ratio = width / height;
        if width > self.max_width {
            width = self.max_width;
            height = width / aspect_ratio;
        }
        if height > self.max_height {
            height = self.max_height;
            width = height * aspect_ratio;
        }
        if width < self.min_width {
            width = self.min_width;
            height = width / aspect_ratio;
        }
        if height < self.min_height {
            height = self.min_height;
            width = height * aspect_ratio;
        }
        Size::new(self.constrain_width(width), self.constrain_height(height))
    }

    /// The biggest size that satisfies the constraints.
    pub fn biggest(self) -> Size {
        Size::new(
            self.constrain_width(f64::INFINITY),
            self.constrain_height(f64::INFINITY),
        )
    }

    /// The smallest size that satisfies the constraints.
    pub fn smallest(self) -> Size {
        Size::new(self.constrain_width(0.0), self.constrain_height(0.0))
    }

    /// Whether there is exactly one width value that satisfies the constraints.
    pub fn has_tight_width(self) -> bool {
        self.min_width >= self.max_width
    }

    /// Whether there is exactly one height value that satisfies the constraints.
    pub fn has_tight_height(self) -> bool {
        self.min_height >= self.max_height
    }

    /// Whether there is an upper bound on the maximum width.
    pub fn has_bounded_width(self) -> bool {
        self.max_width < f64::INFINITY
    }

    /// Whether there is an upper bound on the maximum height.
    pub fn has_bounded_height(self) -> bool {
        self.max_height < f64::INFINITY
    }

    /// Whether the width constraint is infinite.
    pub fn has_infinite_width(self) -> bool {
        self.min_width >= f64::INFINITY
    }

    /// Whether the height constraint is infinite.
    pub fn has_infinite_height(self) -> bool {
        self.min_height >= f64::INFINITY
    }

    /// Whether the given size satisfies the constraints.
    pub fn is_satisfied_by(self, size: Size) -> bool {
        debug_assert!(self.debug_assert_is_valid(false));
        self.min_width <= size.width()
            && size.width() <= self.max_width
            && self.min_height <= size.height()
            && size.height() <= self.max_height
    }

    /// Scales each constraint parameter by the inverse of the given factor,
    /// rounded toward zero (Dart `~/`).
    pub fn truncating_div(self, factor: f64) -> BoxConstraints {
        BoxConstraints {
            min_width: (self.min_width / factor).trunc(),
            max_width: (self.max_width / factor).trunc(),
            min_height: (self.min_height / factor).trunc(),
            max_height: (self.max_height / factor).trunc(),
        }
    }

    /// Linearly interpolate between two box constraints.
    ///
    /// If either is none, this function interpolates from a [`BoxConstraints`]
    /// object whose fields are all set to 0.0.
    pub fn lerp(
        a: Option<BoxConstraints>,
        b: Option<BoxConstraints>,
        t: f64,
    ) -> Option<BoxConstraints> {
        if a == b {
            return a;
        }
        let Some(a) = a else {
            return Some((b.unwrap() * t).normalize());
        };
        let Some(b) = b else {
            return Some((a * (1.0 - t)).normalize());
        };
        debug_assert!(a.debug_assert_is_valid(false));
        debug_assert!(b.debug_assert_is_valid(false));
        debug_assert!(
            (a.min_width.is_finite() && b.min_width.is_finite())
                || (a.min_width == f64::INFINITY && b.min_width == f64::INFINITY),
            "Cannot interpolate between finite constraints and unbounded constraints."
        );
        debug_assert!(
            (a.max_width.is_finite() && b.max_width.is_finite())
                || (a.max_width == f64::INFINITY && b.max_width == f64::INFINITY),
            "Cannot interpolate between finite constraints and unbounded constraints."
        );
        debug_assert!(
            (a.min_height.is_finite() && b.min_height.is_finite())
                || (a.min_height == f64::INFINITY && b.min_height == f64::INFINITY),
            "Cannot interpolate between finite constraints and unbounded constraints."
        );
        debug_assert!(
            (a.max_height.is_finite() && b.max_height.is_finite())
                || (a.max_height == f64::INFINITY && b.max_height == f64::INFINITY),
            "Cannot interpolate between finite constraints and unbounded constraints."
        );
        Some(
            BoxConstraints {
                min_width: if a.min_width.is_finite() {
                    lerp_double(Some(a.min_width), Some(b.min_width), t).unwrap()
                } else {
                    f64::INFINITY
                },
                max_width: if a.max_width.is_finite() {
                    lerp_double(Some(a.max_width), Some(b.max_width), t).unwrap()
                } else {
                    f64::INFINITY
                },
                min_height: if a.min_height.is_finite() {
                    lerp_double(Some(a.min_height), Some(b.min_height), t).unwrap()
                } else {
                    f64::INFINITY
                },
                max_height: if a.max_height.is_finite() {
                    lerp_double(Some(a.max_height), Some(b.max_height), t).unwrap()
                } else {
                    f64::INFINITY
                },
            }
            .normalize(),
        )
    }

    /// Returns a box constraints that [`is_normalized`](Self::is_normalized).
    pub fn normalize(self) -> BoxConstraints {
        if self.is_normalized() {
            return self;
        }
        let min_width = if self.min_width >= 0.0 {
            self.min_width
        } else {
            0.0
        };
        let min_height = if self.min_height >= 0.0 {
            self.min_height
        } else {
            0.0
        };
        BoxConstraints {
            min_width,
            max_width: if min_width > self.max_width {
                min_width
            } else {
                self.max_width
            },
            min_height,
            max_height: if min_height > self.max_height {
                min_height
            } else {
                self.max_height
            },
        }
    }
}

impl Default for BoxConstraints {
    fn default() -> BoxConstraints {
        BoxConstraints::new()
    }
}

impl Constraints for BoxConstraints {
    fn is_tight(&self) -> bool {
        self.has_tight_width() && self.has_tight_height()
    }

    fn is_normalized(&self) -> bool {
        self.min_width >= 0.0
            && self.min_width <= self.max_width
            && self.min_height >= 0.0
            && self.min_height <= self.max_height
    }

    fn debug_assert_is_valid(&self, is_applied_constraint: bool) -> bool {
        if cfg!(debug_assertions) {
            assert!(
                !self.min_width.is_nan()
                    && !self.max_width.is_nan()
                    && !self.min_height.is_nan()
                    && !self.max_height.is_nan(),
                "BoxConstraints has a NaN value."
            );
            assert!(
                self.min_width >= 0.0,
                "BoxConstraints has a negative minimum width."
            );
            assert!(
                self.min_height >= 0.0,
                "BoxConstraints has a negative minimum height."
            );
            assert!(
                self.max_width >= self.min_width,
                "BoxConstraints has non-normalized width constraints."
            );
            assert!(
                self.max_height >= self.min_height,
                "BoxConstraints has non-normalized height constraints."
            );
            if is_applied_constraint {
                assert!(
                    self.min_width.is_finite(),
                    "BoxConstraints forces an infinite width."
                );
                assert!(
                    self.min_height.is_finite(),
                    "BoxConstraints forces an infinite height."
                );
            }
            debug_assert!(self.is_normalized());
        }
        self.is_normalized()
    }
}

impl std::ops::Mul<f64> for BoxConstraints {
    type Output = BoxConstraints;

    fn mul(self, factor: f64) -> BoxConstraints {
        BoxConstraints {
            min_width: self.min_width * factor,
            max_width: self.max_width * factor,
            min_height: self.min_height * factor,
            max_height: self.max_height * factor,
        }
    }
}

impl std::ops::Div<f64> for BoxConstraints {
    type Output = BoxConstraints;

    fn div(self, factor: f64) -> BoxConstraints {
        BoxConstraints {
            min_width: self.min_width / factor,
            max_width: self.max_width / factor,
            min_height: self.min_height / factor,
            max_height: self.max_height / factor,
        }
    }
}

impl std::ops::Rem<f64> for BoxConstraints {
    type Output = BoxConstraints;

    fn rem(self, value: f64) -> BoxConstraints {
        BoxConstraints {
            min_width: self.min_width % value,
            max_width: self.max_width % value,
            min_height: self.min_height % value,
            max_height: self.max_height % value,
        }
    }
}

impl PartialEq for BoxConstraints {
    fn eq(&self, other: &BoxConstraints) -> bool {
        debug_assert!(self.debug_assert_is_valid(false));
        debug_assert!(other.debug_assert_is_valid(false));
        self.min_width == other.min_width
            && self.max_width == other.max_width
            && self.min_height == other.min_height
            && self.max_height == other.max_height
    }
}

impl Eq for BoxConstraints {}

impl Hash for BoxConstraints {
    fn hash<H: Hasher>(&self, state: &mut H) {
        debug_assert!(self.debug_assert_is_valid(false));
        self.min_width.to_bits().hash(state);
        self.max_width.to_bits().hash(state);
        self.min_height.to_bits().hash(state);
        self.max_height.to_bits().hash(state);
    }
}

impl Display for BoxConstraints {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let annotation = if self.is_normalized() {
            ""
        } else {
            "; NOT NORMALIZED"
        };
        if self.min_width == f64::INFINITY && self.min_height == f64::INFINITY {
            return write!(f, "BoxConstraints(biggest{annotation})");
        }
        if self.min_width == 0.0
            && self.max_width == f64::INFINITY
            && self.min_height == 0.0
            && self.max_height == f64::INFINITY
        {
            return write!(f, "BoxConstraints(unconstrained{annotation})");
        }
        fn describe(min: f64, max: f64, dim: &str) -> String {
            if min == max {
                format!("{dim}={min:.1}")
            } else {
                format!("{min:.1}<={dim}<={max:.1}")
            }
        }
        write!(
            f,
            "BoxConstraints({}, {}{annotation})",
            describe(self.min_width, self.max_width, "w"),
            describe(self.min_height, self.max_height, "h")
        )
    }
}

impl Debug for BoxConstraints {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Display::fmt(self, f)
    }
}

/// Method signature for hit testing a [`RenderBox`].
///
/// Used by [`BoxHitTestResult::add_with_paint_transform`] to hit test children of a
/// [`RenderBox`].
pub type BoxHitTest<'r> = dyn FnOnce(&mut BoxHitTestResult<'r>, Offset) -> bool;

/// The result of performing a hit test on [`RenderBox`]es.
///
/// A view over a [`HitTestResult`]: Dart's `BoxHitTestResult.wrap`. Dart's bare
/// `BoxHitTestResult()` constructor is `wrap` over a fresh `HitTestResult`.
pub struct BoxHitTestResult<'a>(&'a mut HitTestResult);

impl<'a> BoxHitTestResult<'a> {
    /// Wraps `result` to create a [`BoxHitTestResult`] that shares its path.
    pub fn wrap(result: &'a mut HitTestResult) -> BoxHitTestResult<'a> {
        BoxHitTestResult(result)
    }

    /// Transforms `position` to the local coordinate system of a child for hit-testing the
    /// child.
    ///
    /// The actual hit testing of the child needs to be implemented in the provided `hit_test`
    /// callback, which is invoked with the transformed `position` as argument.
    ///
    /// The provided paint `transform` (which describes the transform from the child to the
    /// parent in 3D) is processed by [`remove_perspective_transform`] to remove the
    /// perspective component and inverted before it is used to transform `position` from the
    /// coordinate system of the parent to the system of the child.
    ///
    /// If `transform` is `None` it will be treated as the identity transform and `position` is
    /// provided to the `hit_test` callback as-is. If `transform` cannot be inverted, the
    /// `hit_test` callback is not invoked and false is returned.
    pub fn add_with_paint_transform(
        &mut self,
        transform: Option<Matrix4>,
        position: Offset,
        hit_test: impl FnOnce(&mut BoxHitTestResult<'_>, Offset) -> bool,
    ) -> bool {
        let transform = match transform {
            Some(transform) => match remove_perspective_transform(transform).invert() {
                Some(inverted) => Some(inverted),
                // Objects are not visible on screen and cannot be hit-tested.
                None => return false,
            },
            None => None,
        };
        self.add_with_raw_transform(transform, position, hit_test)
    }

    /// Convenience method for hit testing children, that are translated by an [`Offset`].
    ///
    /// The actual hit testing of the child needs to be implemented in the provided `hit_test`
    /// callback, which is invoked with the transformed `position` as argument.
    pub fn add_with_paint_offset(
        &mut self,
        offset: Option<Offset>,
        position: Offset,
        hit_test: impl FnOnce(&mut BoxHitTestResult<'_>, Offset) -> bool,
    ) -> bool {
        let transformed_position = match offset {
            Some(offset) => position - offset,
            None => position,
        };
        if let Some(offset) = offset {
            self.0.push_offset(Offset::ZERO - offset);
        }
        let is_hit = hit_test(self, transformed_position);
        if offset.is_some() {
            self.0.pop_transform();
        }
        is_hit
    }

    /// Transforms `position` to the local coordinate system of a child for hit-testing the
    /// child.
    ///
    /// Unlike [`add_with_paint_transform`](Self::add_with_paint_transform), the provided
    /// `transform` is used as-is to transform `position`: it must describe the transform from
    /// the parent to the child.
    pub fn add_with_raw_transform(
        &mut self,
        transform: Option<Matrix4>,
        position: Offset,
        hit_test: impl FnOnce(&mut BoxHitTestResult<'_>, Offset) -> bool,
    ) -> bool {
        let transformed_position = match transform {
            Some(transform) => transform_point(&transform, position),
            None => position,
        };
        if let Some(transform) = transform {
            self.0.push_transform(transform);
        }
        let is_hit = hit_test(self, transformed_position);
        if transform.is_some() {
            self.0.pop_transform();
        }
        is_hit
    }

    /// Pass-through method for adding a hit test while manually managing the position
    /// transformation logic.
    ///
    /// Exactly one of `paint_offset`, `paint_transform`, or `raw_transform` must be given.
    pub fn add_with_out_of_band_position(
        &mut self,
        paint_offset: Option<Offset>,
        paint_transform: Option<Matrix4>,
        raw_transform: Option<Matrix4>,
        hit_test: impl FnOnce(&mut BoxHitTestResult<'_>) -> bool,
    ) -> bool {
        debug_assert_eq!(
            [
                paint_offset.is_some(),
                paint_transform.is_some(),
                raw_transform.is_some()
            ]
            .iter()
            .filter(|given| **given)
            .count(),
            1,
            "Exactly one transform or offset argument must be provided."
        );
        if let Some(paint_offset) = paint_offset {
            self.0.push_offset(Offset::ZERO - paint_offset);
        } else if let Some(raw_transform) = raw_transform {
            self.0.push_transform(raw_transform);
        } else {
            let paint_transform = paint_transform
                .expect("checked")
                .invert()
                .expect("paint_transform must be invertible.");
            self.0.push_transform(paint_transform);
        }
        let is_hit = hit_test(self);
        self.0.pop_transform();
        is_hit
    }
}

impl Deref for BoxHitTestResult<'_> {
    type Target = HitTestResult;

    fn deref(&self) -> &HitTestResult {
        self.0
    }
}

impl DerefMut for BoxHitTestResult<'_> {
    fn deref_mut(&mut self) -> &mut HitTestResult {
        self.0
    }
}

/// A hit test entry used by [`RenderBox`].
///
/// Dart's `BoxHitTestEntry` subclasses `HitTestEntry`; here it is the entry's target, carrying
/// the box and the position of the hit test in the local coordinates of the box. It becomes a
/// [`HitTestEntry`] with [`From`].
#[derive(Clone, Copy, Debug)]
pub struct BoxHitTestEntry {
    target: AnyRenderBox,
    local_position: Offset,
}

impl BoxHitTestEntry {
    /// Creates a box hit test entry.
    pub fn new(target: AnyRenderBox, local_position: Offset) -> BoxHitTestEntry {
        BoxHitTestEntry {
            target,
            local_position,
        }
    }

    /// The [`RenderBox`] that was hit.
    pub fn target(&self) -> AnyRenderBox {
        self.target
    }

    /// The position of the hit test in the local coordinates of [`target`](Self::target).
    pub fn local_position(&self) -> Offset {
        self.local_position
    }
}

impl HitTestTarget for BoxHitTestEntry {
    fn handle_event(&self, app: &mut App, event: &PointerEvent, _entry: &HitTestEntry) {
        self.target.handle_event(app, event, self);
    }

    fn retained_handle(&self) -> Option<HandleId> {
        Some(self.target.id)
    }
}

impl From<BoxHitTestEntry> for HitTestEntry {
    fn from(entry: BoxHitTestEntry) -> HitTestEntry {
        HitTestEntry::new(entry)
    }
}

/// Parent data used by [`RenderBox`] and its subclasses.
#[derive(Clone, Copy, Debug)]
pub struct BoxParentData {
    /// The offset at which to paint the child in the parent's coordinate system.
    pub offset: Offset,
}

impl BoxParentData {
    /// Creates box parent data with a zero offset.
    pub const fn new() -> BoxParentData {
        BoxParentData {
            offset: Offset::ZERO,
        }
    }
}

impl Default for BoxParentData {
    fn default() -> BoxParentData {
        BoxParentData::new()
    }
}

impl ParentData for BoxParentData {
    fn provide(&self, id: TypeId) -> Option<&dyn Any> {
        (id == TypeId::of::<BoxParentData>()).then_some(self)
    }

    fn provide_mut(&mut self, id: TypeId) -> Option<&mut dyn Any> {
        (id == TypeId::of::<BoxParentData>()).then_some(self)
    }
}

impl fmt::Display for BoxParentData {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "offset={:?}", self.offset)
    }
}

/// Parent data for a [`RenderBox`] subclass that uses [`ContainerRenderObjectMixin`].
///
/// Flutter's `ContainerBoxParentData`: a convenience class that mixes
/// [`ContainerParentDataMixin`] into a [`BoxParentData`]. Here it is the pair of accessors a
/// container's parent data provides, one for each half.
pub trait ContainerBoxParentData: ContainerParentDataMixin<ChildType = AnyRenderBox> {
    /// Mixin field access: the [`BoxParentData`] half.
    ///
    /// [`ParentData::provide`](crate::ParentData::provide) must answer [`BoxParentData`] with
    /// the same value, or the box protocol cannot position this child.
    fn box_parent_data(&self) -> &BoxParentData;

    /// See [`box_parent_data`](Self::box_parent_data).
    fn box_parent_data_mut(&mut self) -> &mut BoxParentData;

    /// The offset at which to paint the child in the parent's coordinate system.
    fn offset(&self) -> Offset {
        self.box_parent_data().offset
    }

    /// Sets [`offset`](Self::offset).
    fn set_offset(&mut self, value: Offset) {
        self.box_parent_data_mut().offset = value;
    }
}

/// A wrapper that represents the baseline location of a [`RenderBox`].
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct BaselineOffset(pub Option<f64>);

impl BaselineOffset {
    /// A value that indicates that the associated [`RenderBox`] does not have any baselines.
    ///
    /// [`BaselineOffset::NO_BASELINE`] is an identity element in most binary operations
    /// involving two [`BaselineOffset`]s (such as [`min_of`](Self::min_of)), for render objects
    /// with no baselines typically do not contribute to the baseline offset of their parents.
    pub const NO_BASELINE: BaselineOffset = BaselineOffset(None);

    /// The distance from the top of the box, or `None` for no baseline.
    pub fn offset(self) -> Option<f64> {
        self.0
    }

    /// Compares this [`BaselineOffset`] and `other`, and returns whichever is closer to the
    /// origin.
    ///
    /// When both `self` and `other` are [`NO_BASELINE`](Self::NO_BASELINE), this method returns
    /// [`NO_BASELINE`](Self::NO_BASELINE). When one of them is
    /// [`NO_BASELINE`](Self::NO_BASELINE), this method returns the other operand.
    pub fn min_of(self, other: BaselineOffset) -> BaselineOffset {
        match (self.0, other.0) {
            (Some(lhs), Some(rhs)) => {
                if lhs >= rhs {
                    other
                } else {
                    self
                }
            }
            (Some(_), None) => self,
            (None, _) => other,
        }
    }
}

impl Add<f64> for BaselineOffset {
    type Output = BaselineOffset;

    /// Returns a new baseline location that is `offset` pixels further away from the origin than
    /// `self`, or unchanged if `self` is [`NO_BASELINE`](Self::NO_BASELINE).
    fn add(self, offset: f64) -> BaselineOffset {
        BaselineOffset(self.0.map(|value| value + offset))
    }
}

/// Why a box cannot compute a dry layout, for
/// [`RenderBox::debug_cannot_compute_dry_layout`].
///
/// Flutter's `debugCannotComputeDryLayout` takes a `reason` or an `error`, exactly one of them.
#[derive(Clone, Copy, Debug)]
pub enum DryLayoutFailure<'a> {
    /// The box does not implement dry layout; the panic names the box and adds this reason.
    Reason(&'a str),

    /// The box cannot lay out with the given constraints; the panic is this error.
    Error(&'a str),
}

/// Intrinsic dimension calculation that computes the intrinsic width given the max height, or
/// the intrinsic height given the max width.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
enum IntrinsicDimension {
    MinWidth,
    MaxWidth,
    MinHeight,
    MaxHeight,
}

/// An `f64` used as a map key, as Dart uses a `double`.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
struct DoubleKey(u64);

impl DoubleKey {
    fn new(value: f64) -> DoubleKey {
        DoubleKey(value.to_bits())
    }
}

/// Flutter's `_LayoutCacheStorage`: what one [`RenderBox`] has memoized.
///
/// The layout cache storage is typically cleared in
/// [`RenderBox::mark_needs_layout`], but is usually kept across
/// [`AnyRenderBox::layout`] calls because the incoming [`BoxConstraints`] is always an input of
/// every layout computation.
#[derive(Default)]
struct LayoutCacheStorage {
    cached_intrinsic_dimensions: HashMap<(IntrinsicDimension, DoubleKey), f64>,
    cached_dry_layout_sizes: HashMap<BoxConstraints, Size>,
    cached_alphabetic_baseline: HashMap<BoxConstraints, BaselineOffset>,
    cached_ideo_baseline: HashMap<BoxConstraints, BaselineOffset>,
}

impl LayoutCacheStorage {
    /// Empties the storage, and returns whether it had anything cached.
    fn clear(&mut self) -> bool {
        let has_cache = !self.cached_dry_layout_sizes.is_empty()
            || !self.cached_intrinsic_dimensions.is_empty()
            || !self.cached_alphabetic_baseline.is_empty()
            || !self.cached_ideo_baseline.is_empty();
        if has_cache {
            self.cached_dry_layout_sizes.clear();
            self.cached_intrinsic_dimensions.clear();
            self.cached_alphabetic_baseline.clear();
            self.cached_ideo_baseline.clear();
        }
        has_cache
    }

    fn baselines(
        &mut self,
        baseline: TextBaseline,
    ) -> &mut HashMap<BoxConstraints, BaselineOffset> {
        match baseline {
            TextBaseline::Alphabetic => &mut self.cached_alphabetic_baseline,
            TextBaseline::Ideographic => &mut self.cached_ideo_baseline,
        }
    }
}

/// Flutter's `RenderBox` fields.
pub struct RenderBoxData {
    pub(crate) size: Option<Size>,
    pub(crate) constraints: Option<BoxConstraints>,
    layout_cache: LayoutCacheStorage,
    debug_computing_this_dry_layout: bool,
    debug_computing_this_dry_baseline: bool,
}

impl RenderBoxData {
    /// Unlaid-out box state.
    pub fn new() -> RenderBoxData {
        RenderBoxData {
            size: None,
            constraints: None,
            layout_cache: LayoutCacheStorage::default(),
            debug_computing_this_dry_layout: false,
            debug_computing_this_dry_baseline: false,
        }
    }
}

impl Default for RenderBoxData {
    fn default() -> RenderBoxData {
        RenderBoxData::new()
    }
}

/// Implements [`RenderBox`] field accessors for a `render_box` field.
#[macro_export]
macro_rules! render_box_accessors {
    () => {
        fn render_box_data(
            self: $crate::RenderHandle<Self>,
            app: &::inset_foundation::App,
        ) -> &$crate::RenderBoxData {
            &self.get(app).render_box
        }
        fn render_box_data_mut(
            self: $crate::RenderHandle<Self>,
            app: &mut ::inset_foundation::App,
        ) -> &mut $crate::RenderBoxData {
            &mut self.get_mut(app).render_box
        }
    };
}

/// Dart's `child.parentData! as BoxParentData`.
///
/// # Panics
///
/// If the child has no parent data, or its parent data is not a box's.
fn box_parent_data_of(app: &App, child: AnyRenderObject) -> &BoxParentData {
    child
        .parent_data(app)
        .and_then(|parent_data| parent_data.part::<BoxParentData>())
        .expect("parent data is not a BoxParentData")
}

/// A mixin that provides useful default behaviors for boxes with children managed by the
/// [`ContainerRenderObjectMixin`] mixin.
///
/// By convention, this trait doesn't override any members of the supertrait. Instead, it
/// provides helpful functions that render objects can call as appropriate.
///
/// Flutter's `RenderBoxContainerDefaultsMixin`.
pub trait RenderBoxContainerDefaultsMixin:
    ContainerRenderObjectMixin<ChildType = AnyRenderBox, ParentDataType: ContainerBoxParentData>
    + RenderBox
{
    /// Returns the baseline of the first child with a baseline.
    ///
    /// Useful when the children are displayed vertically in the same order they appear in the
    /// child list.
    fn default_compute_distance_to_first_actual_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        baseline: TextBaseline,
    ) -> Option<f64> {
        debug_assert!(!self.as_object().debug_needs_layout(app));
        let mut child = self.first_child(app);
        while let Some(current) = child {
            let child_parent_data = current
                .as_object()
                .parent_data_of::<Self::ParentDataType>(app);
            let (offset, next_sibling) =
                (child_parent_data.offset(), child_parent_data.next_sibling());
            if let Some(result) = current.get_distance_to_actual_baseline(app, baseline) {
                return Some(result + offset.dy());
            }
            child = next_sibling;
        }
        None
    }

    /// Returns the minimum baseline value among every child.
    ///
    /// Useful when the vertical position of the children isn't determined by the order in the
    /// child list.
    fn default_compute_distance_to_highest_actual_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        baseline: TextBaseline,
    ) -> Option<f64> {
        debug_assert!(!self.as_object().debug_needs_layout(app));
        let mut min_baseline = BaselineOffset::NO_BASELINE;
        let mut child = self.first_child(app);
        while let Some(current) = child {
            let child_parent_data = current
                .as_object()
                .parent_data_of::<Self::ParentDataType>(app);
            let (offset, next_sibling) =
                (child_parent_data.offset(), child_parent_data.next_sibling());
            let candidate = BaselineOffset(current.get_distance_to_actual_baseline(app, baseline))
                + offset.dy();
            min_baseline = min_baseline.min_of(candidate);
            child = next_sibling;
        }
        min_baseline.offset()
    }

    /// Performs a hit test on each child by walking the child list backwards.
    ///
    /// Stops walking once after the first child reports that it contains the given point.
    /// Returns whether any children contain the given point.
    ///
    /// See also:
    ///
    ///  * [`default_paint`](Self::default_paint), which paints the children appropriate for
    ///    this hit-testing strategy.
    fn default_hit_test_children(
        self: RenderHandle<Self>,
        app: &mut App,
        result: &mut BoxHitTestResult<'_>,
        position: Offset,
    ) -> bool {
        let mut child = self.last_child(app);
        while let Some(current) = child {
            // The x, y parameters have the top left of the node's box as the origin.
            let child_parent_data = current
                .as_object()
                .parent_data_of::<Self::ParentDataType>(app);
            let (offset, previous_sibling) = (
                child_parent_data.offset(),
                child_parent_data.previous_sibling(),
            );
            let is_hit =
                result.add_with_paint_offset(Some(offset), position, |result, transformed| {
                    debug_assert_eq!(transformed, position - offset);
                    current.hit_test(app, result, transformed)
                });
            if is_hit {
                return true;
            }
            child = previous_sibling;
        }
        false
    }

    /// Paints each child by walking the child list forwards.
    ///
    /// See also:
    ///
    ///  * [`default_hit_test_children`](Self::default_hit_test_children), which implements
    ///    hit-testing of the children in a manner appropriate for this painting strategy.
    fn default_paint(
        self: RenderHandle<Self>,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
    ) {
        let mut child = self.first_child(app);
        while let Some(current) = child {
            let child_parent_data = current
                .as_object()
                .parent_data_of::<Self::ParentDataType>(app);
            let (child_offset, next_sibling) =
                (child_parent_data.offset(), child_parent_data.next_sibling());
            context.paint_child(app, current.as_object(), child_offset + offset);
            child = next_sibling;
        }
    }
}

/// A render object in a 2D Cartesian coordinate system.
///
/// Flutter's counterpart is `RenderBox`. [`RenderObject::perform_layout`] is the leaf override;
/// [`AnyRenderBox::layout`] is the framework wrapper. `RenderObject`'s tree operations are
/// provided here and forward to [`as_object`](Self::as_object).
pub trait RenderBox: RenderObject {
    /// Mixin field access.
    fn render_box_data(self: RenderHandle<Self>, app: &App) -> &RenderBoxData;

    /// See [`render_box_data`](Self::render_box_data).
    fn render_box_data_mut(self: RenderHandle<Self>, app: &mut App) -> &mut RenderBoxData;

    /// The box constraints most recently supplied by the parent.
    ///
    /// # Panics
    ///
    /// If layout has not yet happened.
    fn constraints(self: RenderHandle<Self>, app: &App) -> BoxConstraints {
        self.render_box_data(app).constraints.unwrap_or_else(|| {
            panic!("A RenderObject does not have any constraints before it has been laid out.")
        })
    }

    /// Installs [`BoxParentData`] unless the child already has it.
    ///
    /// The box default for [`RenderObject::setup_parent_data`]; override it here.
    fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
        if !child.parent_data_is::<BoxParentData>(app) {
            child.set_parent_data(app, BoxParentData::new());
        }
    }

    /// See [`AnyRenderBox::get_min_intrinsic_width`].
    fn get_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        self.as_box().get_min_intrinsic_width(app, height)
    }

    /// Computes the value returned by [`get_min_intrinsic_width`](Self::get_min_intrinsic_width).
    /// Do not call this function directly, instead, call
    /// [`get_min_intrinsic_width`](Self::get_min_intrinsic_width).
    ///
    /// Override in subclasses that implement [`RenderObject::perform_layout`]. This method
    /// should return the minimum width that this box could be without failing to correctly
    /// paint its contents within itself, without clipping.
    ///
    /// If the layout algorithm is independent of the context (e.g. it always tries to be a
    /// particular size), or if the layout algorithm is width-in-height-out, or if the layout
    /// algorithm uses both the incoming width and height constraints (e.g. it always sizes
    /// itself to [`BoxConstraints::biggest`]), then the `height` argument should be ignored.
    ///
    /// If the layout algorithm is strictly height-in-width-out, or is height-in-width-out when
    /// the width is unconstrained, then the height argument is the height to use.
    ///
    /// The `height` argument will never be negative. It may be infinite.
    ///
    /// If this algorithm depends on the intrinsic dimensions of a child, the intrinsic
    /// dimensions of that child should be obtained using the functions whose names start with
    /// `get_`, not `compute_`.
    ///
    /// This function should never return a negative or infinite value.
    ///
    /// # When the intrinsic dimensions cannot be known
    ///
    /// There are cases where render objects do not have an efficient way to compute their
    /// intrinsic dimensions. For example, it may be prohibitively expensive to reify and
    /// measure every child of a lazy viewport, or the dimensions may be computed by a callback
    /// about which the render object cannot reason.
    ///
    /// In such cases the intrinsic functions should panic when
    /// [`debug_checking_intrinsics`](crate::debug_checking_intrinsics) is false and debug
    /// assertions are enabled, and return 0.0 otherwise. See
    /// [`RenderViewportBase::debug_throw_if_not_checking_intrinsics`](crate::RenderViewportBase::debug_throw_if_not_checking_intrinsics).
    ///
    /// # Aspect-ratio-driven boxes
    ///
    /// Some boxes always return a fixed size based on the constraints. For these boxes, the
    /// intrinsic functions should return the appropriate size when the incoming `height` or
    /// `width` argument is finite, treating that as a tight constraint in the respective
    /// direction and treating the other direction's constraints as unbounded. When the incoming
    /// argument is not finite, then they should return the actual intrinsic dimensions based on
    /// the contents, as any other box would.
    fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        let _ = (self, app, height);
        0.0
    }

    /// See [`AnyRenderBox::get_max_intrinsic_width`].
    fn get_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        self.as_box().get_max_intrinsic_width(app, height)
    }

    /// Computes the value returned by [`get_max_intrinsic_width`](Self::get_max_intrinsic_width).
    /// Do not call this function directly, instead, call
    /// [`get_max_intrinsic_width`](Self::get_max_intrinsic_width).
    ///
    /// Override in subclasses that implement [`RenderObject::perform_layout`]. This should
    /// return the smallest width beyond which increasing the width never decreases the preferred
    /// height. The preferred height is the value that would be returned by
    /// [`compute_min_intrinsic_height`](Self::compute_min_intrinsic_height) for that width.
    ///
    /// If the layout algorithm is strictly height-in-width-out, or is height-in-width-out when
    /// the width is unconstrained, then this should return the same value as
    /// [`compute_min_intrinsic_width`](Self::compute_min_intrinsic_width) for the same height.
    ///
    /// Otherwise, the height argument should be ignored, and the returned value should be equal
    /// to or bigger than the value returned by
    /// [`compute_min_intrinsic_width`](Self::compute_min_intrinsic_width).
    ///
    /// The value returned by this method might not match the size that the object would actually
    /// take. For example, a box that always exactly sizes itself using
    /// [`BoxConstraints::biggest`] might well size itself bigger than its max intrinsic size.
    fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        let _ = (self, app, height);
        0.0
    }

    /// See [`AnyRenderBox::get_min_intrinsic_height`].
    fn get_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        self.as_box().get_min_intrinsic_height(app, width)
    }

    /// Computes the value returned by
    /// [`get_min_intrinsic_height`](Self::get_min_intrinsic_height). Do not call this function
    /// directly, instead, call [`get_min_intrinsic_height`](Self::get_min_intrinsic_height).
    ///
    /// Override in subclasses that implement [`RenderObject::perform_layout`]. Should return the
    /// minimum height that this box could be without failing to correctly paint its contents
    /// within itself, without clipping.
    ///
    /// If the layout algorithm is independent of the context, or if the layout algorithm is
    /// height-in-width-out, or if the layout algorithm uses both the incoming height and width
    /// constraints, then the `width` argument should be ignored.
    ///
    /// If the layout algorithm is strictly width-in-height-out, or is width-in-height-out when
    /// the height is unconstrained, then the width argument is the width to use.
    ///
    /// The `width` argument will never be negative. It may be infinite.
    fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        let _ = (self, app, width);
        0.0
    }

    /// See [`AnyRenderBox::get_max_intrinsic_height`].
    fn get_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        self.as_box().get_max_intrinsic_height(app, width)
    }

    /// Computes the value returned by
    /// [`get_max_intrinsic_height`](Self::get_max_intrinsic_height). Do not call this function
    /// directly, instead, call [`get_max_intrinsic_height`](Self::get_max_intrinsic_height).
    ///
    /// Override in subclasses that implement [`RenderObject::perform_layout`]. Should return the
    /// smallest height beyond which increasing the height never decreases the preferred width.
    /// The preferred width is the value that would be returned by
    /// [`compute_min_intrinsic_width`](Self::compute_min_intrinsic_width) for that height.
    ///
    /// If the layout algorithm is strictly width-in-height-out, or is width-in-height-out when
    /// the height is unconstrained, then this should return the same value as
    /// [`compute_min_intrinsic_height`](Self::compute_min_intrinsic_height) for the same width.
    ///
    /// Otherwise, the width argument should be ignored, and the returned value should be equal
    /// to or bigger than the value returned by
    /// [`compute_min_intrinsic_height`](Self::compute_min_intrinsic_height).
    fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        let _ = (self, app, width);
        0.0
    }

    /// See [`AnyRenderBox::get_dry_layout`].
    fn get_dry_layout(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
    ) -> Size {
        self.as_box().get_dry_layout(app, constraints)
    }

    /// Computes the value returned by [`get_dry_layout`](Self::get_dry_layout). Do not call this
    /// function directly, instead, call [`get_dry_layout`](Self::get_dry_layout).
    ///
    /// Override in subclasses that implement [`RenderObject::perform_layout`] or
    /// [`perform_resize`](Self::perform_resize), or when setting
    /// [`RenderObject::sized_by_parent`] to true without overriding
    /// [`perform_resize`](Self::perform_resize). This method should return the [`Size`] that
    /// this box would like to be given the provided [`BoxConstraints`].
    ///
    /// The size returned by this method must match the [`size`](Self::size) that the box will
    /// compute for itself in [`RenderObject::perform_layout`] (or
    /// [`perform_resize`](Self::perform_resize), if
    /// [`RenderObject::sized_by_parent`] is true).
    ///
    /// If this algorithm depends on the size of a child, the size of that child should be
    /// obtained using its [`get_dry_layout`](Self::get_dry_layout) method.
    ///
    /// # When the size cannot be known
    ///
    /// There are cases where render objects do not have an efficient way to compute their size.
    /// For example, the size may be computed by a callback about which the render object cannot
    /// reason. In such cases, the function should call
    /// [`debug_cannot_compute_dry_layout`](Self::debug_cannot_compute_dry_layout) and return
    /// [`Size::ZERO`].
    fn compute_dry_layout(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
    ) -> Size {
        let _ = (app, constraints);
        self.debug_cannot_compute_dry_layout(DryLayoutFailure::Reason(
            "It does not implement RenderBox::compute_dry_layout.",
        ));
        Size::ZERO
    }

    /// See [`AnyRenderBox::get_dry_baseline`].
    fn get_dry_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
        baseline: TextBaseline,
    ) -> Option<f64> {
        self.as_box().get_dry_baseline(app, constraints, baseline)
    }

    /// Computes the value returned by [`get_dry_baseline`](Self::get_dry_baseline).
    ///
    /// This method is for overriding only and shouldn't be called directly. To get this box's
    /// speculative baseline location for the given `constraints`, call
    /// [`get_dry_baseline`](Self::get_dry_baseline) instead.
    ///
    /// The "dry" in the method name means the implementation must not produce observable side
    /// effects when called. For example, it must not change the [`size`](Self::size) of the box,
    /// or its children's paint offsets. Moreover, accessing the current layout of this box or a
    /// child box usually indicates a bug in the implementation, as the current layout is
    /// typically calculated using a set of [`BoxConstraints`] that's different from the
    /// `constraints` given as the first parameter. To get the size of this box or a child box in
    /// this method's implementation, use [`get_dry_layout`](Self::get_dry_layout) instead.
    ///
    /// The implementation must return a value that represents the distance from the top of the
    /// box to the first baseline of the box's contents, for the given `constraints`, or `None`
    /// if the box has no baselines. It's the same exact value
    /// [`compute_distance_to_actual_baseline`](Self::compute_distance_to_actual_baseline) would
    /// return, when this box was laid out at `constraints` in the same exact state.
    ///
    /// Not all boxes support dry baseline computation. In such cases the box must call
    /// [`debug_cannot_compute_dry_layout`](Self::debug_cannot_compute_dry_layout) and return a
    /// dummy baseline offset value (such as `None`).
    fn compute_dry_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
        baseline: TextBaseline,
    ) -> Option<f64> {
        let _ = (app, constraints, baseline);
        self.debug_cannot_compute_dry_layout(DryLayoutFailure::Reason(
            "It does not implement RenderBox::compute_dry_baseline.",
        ));
        None
    }

    /// Called from [`compute_dry_layout`](Self::compute_dry_layout) or
    /// [`compute_dry_baseline`](Self::compute_dry_baseline) if this box does not support
    /// calculating a dry layout.
    ///
    /// When debug assertions are enabled and
    /// [`debug_checking_intrinsics`](crate::debug_checking_intrinsics) is not true, this method
    /// panics with the given [`DryLayoutFailure`].
    fn debug_cannot_compute_dry_layout(self: RenderHandle<Self>, failure: DryLayoutFailure<'_>) {
        let _ = self;
        if !cfg!(debug_assertions) || debug_checking_intrinsics() {
            return;
        }
        match failure {
            DryLayoutFailure::Reason(reason) => panic!(
                "The {} class does not support dry layout. {reason}",
                std::any::type_name::<Self>()
            ),
            DryLayoutFailure::Error(error) => panic!("{error}"),
        }
    }

    /// See [`AnyRenderBox::get_distance_to_baseline`].
    fn get_distance_to_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        baseline: TextBaseline,
        only_real: bool,
    ) -> Option<f64> {
        self.as_box()
            .get_distance_to_baseline(app, baseline, only_real)
    }

    /// See [`AnyRenderBox::get_distance_to_actual_baseline`].
    fn get_distance_to_actual_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        baseline: TextBaseline,
    ) -> Option<f64> {
        self.as_box().get_distance_to_actual_baseline(app, baseline)
    }

    /// Returns the distance from the y-coordinate of the position of the box to the y-coordinate
    /// of the first given baseline in the box's contents, if any, or `None` otherwise.
    ///
    /// Do not call this function directly. If you need to know the baseline of a child from an
    /// invocation of [`RenderObject::perform_layout`] or [`RenderObject::paint`], call
    /// [`get_distance_to_baseline`](Self::get_distance_to_baseline).
    ///
    /// Subclasses should override this method to supply the distances to their baselines. When
    /// implementing this method, there are generally three strategies:
    ///
    ///  * For classes that use the [`ContainerRenderObjectMixin`] child model, consider
    ///    implementing [`RenderBoxContainerDefaultsMixin`] and using
    ///    [`RenderBoxContainerDefaultsMixin::default_compute_distance_to_first_actual_baseline`].
    ///
    ///  * For classes that define a particular baseline themselves, return that value directly.
    ///
    ///  * For classes that have a child to which they wish to defer the computation, call
    ///    [`get_distance_to_actual_baseline`](Self::get_distance_to_actual_baseline) on the
    ///    child.
    fn compute_distance_to_actual_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        baseline: TextBaseline,
    ) -> Option<f64> {
        let _ = (self, app, baseline);
        debug_assert!(
            DEBUG_DOING_BASELINE.get(),
            "Please see the documentation for compute_distance_to_actual_baseline for the \
             required calling conventions of this method."
        );
        None
    }

    /// Mark this render object's layout information as dirty.
    ///
    /// Flutter's `RenderBox.markNeedsLayout`: it also clears the intrinsics, dry layout and
    /// baseline caches, and defers to the parent when they held anything.
    fn mark_needs_layout(self: RenderHandle<Self>, app: &mut App) {
        // If the cache was not empty, then this box's layout is used by the parent's layout
        // algorithm (it's possible that the parent only used the intrinsics for paint, but
        // there's no good way to detect that so we conservatively assume it's a layout
        // dependency).
        //
        // A render object's perform_layout implementation may depend on the baseline location or
        // the intrinsic dimensions of a descendant, even when there are relayout boundaries
        // between them.
        //
        // Some calculations may fail (dry baseline, for example). The layout dependency is still
        // established, but only from the box that failed to compute the dry baseline to the
        // ancestor that queried the dry baseline.
        if self.render_box_data_mut(app).layout_cache.clear() && self.parent(app).is_some() {
            self.as_object().mark_parent_needs_layout(app);
            return;
        }
        crate::object::RenderObjectBase::mark_needs_layout(self, app);
    }

    /// Updates the box's size using only the constraints.
    ///
    /// By default this method sets [`size`](Self::size) to the result of
    /// [`compute_dry_layout`](Self::compute_dry_layout) called with the current
    /// [`constraints`](Self::constraints). Instead of overriding this method, consider
    /// overriding [`compute_dry_layout`](Self::compute_dry_layout).
    fn perform_resize(self: RenderHandle<Self>, app: &mut App) {
        RenderBoxBase::perform_resize(self, app);
    }

    /// The size of this box.
    fn size(self: RenderHandle<Self>, app: &App) -> Size {
        self.as_box().size(app)
    }

    /// Multiply the transform from the parent's coordinate system to this box's coordinate
    /// system into the given transform.
    ///
    /// This function is used to convert coordinate systems between boxes. Subclasses that
    /// apply transforms during painting should override this function to factor those
    /// transforms into the calculation.
    ///
    /// The [`RenderBox`] implementation takes care of adjusting the matrix for the position
    /// of the given child as determined during layout and stored on the child's parent data.
    fn apply_paint_transform(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderObject,
        transform: &mut Matrix4,
    ) {
        debug_assert!(child.parent(app).map(AnyRenderObject::id) == Some(self.id()));
        // Dart asserts the child's parent data is a `BoxParentData` with a message naming
        // this type; `box_parent_data` panics the same way.
        let offset = box_parent_data_of(app, child).offset;
        crate::object::translate(transform, offset.dx(), offset.dy());
    }

    /// An estimate of the bounds within which this render object will paint: the box's own size.
    fn paint_bounds(self: RenderHandle<Self>, app: &App) -> Rect {
        Offset::ZERO & self.size(app)
    }

    /// Whether this render object has undergone layout and has a [`size`](Self::size).
    fn has_size(self: RenderHandle<Self>, app: &App) -> bool {
        self.render_box_data(app).size.is_some()
    }

    /// Override this method to handle pointer events that hit this render object.
    ///
    /// For [`RenderBox`] objects, the `entry` argument is a [`BoxHitTestEntry`]. From this
    /// object you can determine the position of the hit test in the local coordinates of the
    /// render object (via `entry.local_position()`).
    fn handle_event(
        self: RenderHandle<Self>,
        app: &mut App,
        event: &PointerEvent,
        entry: &BoxHitTestEntry,
    ) {
        let _ = (self, app, event, entry);
    }

    /// Determines the set of render objects located at the given position.
    ///
    /// Returns true, and adds any render objects that contain the point to the given hit test
    /// result, if this render object or one of its descendants absorbs the hit (preventing
    /// objects below this one from being hit). Returns false if the hit can continue to other
    /// objects below this one.
    ///
    /// The caller is responsible for transforming `position` from global coordinates to its
    /// location relative to the origin of this [`RenderBox`]. This [`RenderBox`] is responsible
    /// for checking whether the given position is within its bounds.
    ///
    /// # Panics
    ///
    /// In debug builds, if this box has not been laid out.
    fn hit_test(
        self: RenderHandle<Self>,
        app: &mut App,
        result: &mut BoxHitTestResult<'_>,
        position: Offset,
    ) -> bool {
        RenderBoxBase::hit_test(self, app, result, position)
    }

    /// Override this method if this render object can be hit even if its children were not hit.
    ///
    /// Returns true if the specified `position` should be considered a hit on this render
    /// object.
    fn hit_test_self(self: RenderHandle<Self>, _app: &App, _position: Offset) -> bool {
        let _ = self;
        false
    }

    /// Override this method to check whether any children are located at the given position.
    ///
    /// Subclasses should return true if at least one child reported a hit at the specified
    /// position.
    fn hit_test_children(
        self: RenderHandle<Self>,
        app: &mut App,
        result: &mut BoxHitTestResult<'_>,
        position: Offset,
    ) -> bool {
        let _ = (self, app, result, position);
        false
    }

    /// Sets the size of this box. Call from [`RenderObject::perform_layout`] or
    /// [`RenderObject::perform_resize`].
    fn set_size(self: RenderHandle<Self>, app: &mut App, size: Size) {
        self.as_box().set_size(app, size)
    }

    /// See [`AnyRenderBox::layout`].
    fn layout(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
        parent_uses_size: bool,
    ) {
        self.as_box().layout(app, constraints, parent_uses_size)
    }

    /// The type-erased `RenderBox` handle. Free: the vtable is a `const`, and the id is copied.
    fn as_box(self: RenderHandle<Self>) -> AnyRenderBox {
        AnyRenderBox {
            id: self.id(),
            vtable: const { &RenderBoxVTable::of::<Self>() },
        }
    }

    /// The type-erased `RenderObject` handle, through [`as_box`](Self::as_box).
    fn as_object(self: RenderHandle<Self>) -> AnyRenderObject {
        self.as_box().as_object()
    }

    /// See [`AnyRenderObject::adopt_child`].
    fn adopt_child(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
        self.as_object().adopt_child(app, child)
    }

    /// See [`AnyRenderObject::drop_child`].
    fn drop_child(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
        self.as_object().drop_child(app, child)
    }

    /// See [`AnyRenderObject::mark_needs_paint`].
    fn mark_needs_paint(self: RenderHandle<Self>, app: &mut App) {
        self.as_object().mark_needs_paint(app)
    }

    /// See [`AnyRenderObject::mark_needs_composited_layer_update`].
    fn mark_needs_composited_layer_update(self: RenderHandle<Self>, app: &mut App) {
        self.as_object().mark_needs_composited_layer_update(app)
    }

    /// See [`AnyRenderObject::mark_needs_compositing_bits_update`].
    fn mark_needs_compositing_bits_update(self: RenderHandle<Self>, app: &mut App) {
        self.as_object().mark_needs_compositing_bits_update(app)
    }

    /// See [`AnyRenderObject::schedule_initial_layout`].
    fn schedule_initial_layout(self: RenderHandle<Self>, app: &mut App) {
        self.as_object().schedule_initial_layout(app)
    }

    /// See [`AnyRenderObject::parent`].
    fn parent(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderObject> {
        self.as_object().parent(app)
    }

    /// See [`AnyRenderObject::owner`].
    fn owner(self: RenderHandle<Self>, app: &App) -> Option<Handle<PipelineOwner>> {
        self.as_object().owner(app)
    }

    /// See [`AnyRenderObject::attached`].
    fn attached(self: RenderHandle<Self>, app: &App) -> bool {
        self.as_object().attached(app)
    }

    /// See [`AnyRenderObject::debug_needs_layout`].
    fn debug_needs_layout(self: RenderHandle<Self>, app: &App) -> bool {
        self.as_object().debug_needs_layout(app)
    }
}

/// Flutter's `RenderBox` bodies that an override calls through `super`.
///
/// A trait default cannot call `super`, and a leaf's [`RenderBox::hit_test`] shadows the default
/// it would call; this sibling trait, blanket-implemented for every box, carries that body (the
/// shape `ElementBase` has for `Element`).
pub(crate) trait RenderBoxBase: RenderBox {
    /// Flutter's `RenderBox.performResize`.
    fn perform_resize(self: RenderHandle<Self>, app: &mut App) {
        // Default behavior for subclasses that have sized_by_parent = true.
        let constraints = RenderBox::constraints(self, app);
        let size = RenderBox::compute_dry_layout(self, app, constraints);
        debug_assert!(size.width().is_finite() && size.height().is_finite());
        self.set_size(app, size);
    }

    /// Flutter's `RenderBox.hitTest`.
    fn hit_test(
        self: RenderHandle<Self>,
        app: &mut App,
        result: &mut BoxHitTestResult<'_>,
        position: Offset,
    ) -> bool {
        debug_assert!(
            self.has_size(app),
            "Cannot hit test a render box that has never been laid out: {self:?}"
        );
        if self.size(app).contains(position)
            && (RenderBox::hit_test_children(self, app, result, position)
                || RenderBox::hit_test_self(self, app, position))
        {
            result.add(BoxHitTestEntry::new(self.as_box(), position).into());
            return true;
        }
        false
    }
}

impl<T: RenderBox> RenderBoxBase for T {}

/// The vtable of an [`AnyRenderBox`]: the object vtable plus the box accessors.
pub(crate) struct RenderBoxVTable {
    pub object: RenderObjectVTable,
    pub box_data: fn(&App, HandleId) -> &RenderBoxData,
    pub box_data_mut: fn(&mut App, HandleId) -> &mut RenderBoxData,
    pub hit_test: fn(&mut App, HandleId, &mut BoxHitTestResult<'_>, Offset) -> bool,
    pub handle_event: fn(&mut App, HandleId, &PointerEvent, &BoxHitTestEntry),
    pub compute_min_intrinsic_width: fn(&mut App, HandleId, f64) -> f64,
    pub compute_max_intrinsic_width: fn(&mut App, HandleId, f64) -> f64,
    pub compute_min_intrinsic_height: fn(&mut App, HandleId, f64) -> f64,
    pub compute_max_intrinsic_height: fn(&mut App, HandleId, f64) -> f64,
    pub compute_dry_layout: fn(&mut App, HandleId, BoxConstraints) -> Size,
    pub compute_dry_baseline: fn(&mut App, HandleId, BoxConstraints, TextBaseline) -> Option<f64>,
    pub compute_distance_to_actual_baseline: fn(&mut App, HandleId, TextBaseline) -> Option<f64>,
}

impl RenderBoxVTable {
    const fn of<T: RenderBox>() -> RenderBoxVTable {
        RenderBoxVTable {
            object: RenderObjectVTable::of::<T>(
                |app, id, child| <T as RenderBox>::setup_parent_data(resolve(id), app, child),
                |app, id| T::paint_bounds(resolve(id), app),
                |app, id, child, transform| {
                    <T as RenderBox>::apply_paint_transform(resolve(id), app, child, transform)
                },
                |app, id| <T as RenderBox>::perform_resize(resolve(id), app),
                |app, id| <T as RenderBox>::mark_needs_layout(resolve(id), app),
                Some(|| const { &RenderBoxVTable::of::<T>() }),
                None,
            ),
            box_data: |app, id| T::render_box_data(resolve(id), app),
            box_data_mut: |app, id| T::render_box_data_mut(resolve(id), app),
            hit_test: |app, id, result, position| T::hit_test(resolve(id), app, result, position),
            handle_event: |app, id, event, entry| T::handle_event(resolve(id), app, event, entry),
            compute_min_intrinsic_width: |app, id, height| {
                T::compute_min_intrinsic_width(resolve(id), app, height)
            },
            compute_max_intrinsic_width: |app, id, height| {
                T::compute_max_intrinsic_width(resolve(id), app, height)
            },
            compute_min_intrinsic_height: |app, id, width| {
                T::compute_min_intrinsic_height(resolve(id), app, width)
            },
            compute_max_intrinsic_height: |app, id, width| {
                T::compute_max_intrinsic_height(resolve(id), app, width)
            },
            compute_dry_layout: |app, id, constraints| {
                T::compute_dry_layout(resolve(id), app, constraints)
            },
            compute_dry_baseline: |app, id, constraints, baseline| {
                T::compute_dry_baseline(resolve(id), app, constraints, baseline)
            },
            compute_distance_to_actual_baseline: |app, id, baseline| {
                T::compute_distance_to_actual_baseline(resolve(id), app, baseline)
            },
        }
    }
}

impl<T: RenderBox> RenderHandle<T> {
    /// Creates a box-protocol render object in `app`.
    pub fn new_box(app: &mut App, object: T) -> RenderHandle<T> {
        let this = create(app, object);
        let data = this.render_object_data_mut(app);
        data.object_vtable = Some(&const { RenderBoxVTable::of::<T>() }.object);
        // Flutter's `RenderObject()` constructor:
        // `_needsCompositing = isRepaintBoundary || alwaysNeedsCompositing` and
        // `_wasRepaintBoundary = isRepaintBoundary`.
        let is_repaint_boundary = this.is_repaint_boundary(app);
        let always_needs_compositing = this.always_needs_compositing(app);
        let data = this.render_object_data_mut(app);
        data.was_repaint_boundary = is_repaint_boundary;
        data.needs_compositing = is_repaint_boundary || always_needs_compositing;
        this
    }
}

/// Flutter's `RenderBox._debugSetDoingBaseline(true)` and the `finally` that clears it.
struct DebugDoingBaseline;

impl DebugDoingBaseline {
    fn enter() -> DebugDoingBaseline {
        DEBUG_DOING_BASELINE.set(cfg!(debug_assertions));
        DebugDoingBaseline
    }
}

impl Drop for DebugDoingBaseline {
    fn drop(&mut self) {
        DEBUG_DOING_BASELINE.set(false);
    }
}

/// Flutter's `RenderBox._computeIntrinsics` preamble: whether the result may be memoized.
fn should_cache_intrinsics(this: AnyRenderBox, app: &App) -> bool {
    // perform_resize should not depend on anything except the incoming constraints.
    debug_assert!(debug_checking_intrinsics() || !this.as_object().debug_doing_this_resize(app));
    // We don't want the debug-mode intrinsic tests to affect who gets marked dirty, etc.
    !cfg!(debug_assertions) || !debug_checking_intrinsics()
}

/// Flutter's `_IntrinsicDimension.memoize`.
fn memoize_intrinsic(
    this: AnyRenderBox,
    app: &mut App,
    dimension: IntrinsicDimension,
    input: f64,
    computer: fn(&mut App, HandleId, f64) -> f64,
) -> f64 {
    if !should_cache_intrinsics(this, app) {
        return computer(app, this.id, input);
    }
    let key = (dimension, DoubleKey::new(input));
    if let Some(cached) = this.layout_cache(app).cached_intrinsic_dimensions.get(&key) {
        return *cached;
    }
    let result = computer(app, this.id, input);
    this.layout_cache_mut(app)
        .cached_intrinsic_dimensions
        .insert(key, result);
    result
}

/// Flutter's `_Baseline.memoize`.
fn memoize_baseline(
    this: AnyRenderBox,
    app: &mut App,
    constraints: BoxConstraints,
    baseline: TextBaseline,
    computer: impl FnOnce(AnyRenderBox, &mut App) -> BaselineOffset,
) -> BaselineOffset {
    if !should_cache_intrinsics(this, app) {
        return computer(this, app);
    }
    if let Some(cached) = this
        .layout_cache_mut(app)
        .baselines(baseline)
        .get(&constraints)
    {
        return *cached;
    }
    let result = computer(this, app);
    this.layout_cache_mut(app)
        .baselines(baseline)
        .insert(constraints, result);
    result
}

/// Erased `RenderBox`.
#[derive(Clone, Copy)]
pub struct AnyRenderBox {
    id: HandleId,
    vtable: &'static RenderBoxVTable,
}

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

impl Eq for AnyRenderBox {}

impl std::hash::Hash for AnyRenderBox {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

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

impl AnyRenderBox {
    pub(crate) fn from_vtable(id: HandleId, vtable: &'static RenderBoxVTable) -> AnyRenderBox {
        AnyRenderBox { id, vtable }
    }

    /// The `RenderObject` view of this box. Free: points into the nested table.
    /// Whether this render object has undergone layout and has a size.
    pub fn has_size(self, app: &App) -> bool {
        self.box_data(app).size.is_some()
    }

    pub fn as_object(self) -> AnyRenderObject {
        AnyRenderObject::from_vtable(self.id, &self.vtable.object)
    }

    /// See [`RenderBox::hit_test`].
    pub fn hit_test(
        self,
        app: &mut App,
        result: &mut BoxHitTestResult<'_>,
        position: Offset,
    ) -> bool {
        (self.vtable.hit_test)(app, self.id, result, position)
    }

    /// See [`RenderBox::handle_event`].
    pub fn handle_event(self, app: &mut App, event: &PointerEvent, entry: &BoxHitTestEntry) {
        (self.vtable.handle_event)(app, self.id, event, entry)
    }

    fn box_data(self, app: &App) -> &RenderBoxData {
        (self.vtable.box_data)(app, self.id)
    }

    fn box_data_mut(self, app: &mut App) -> &mut RenderBoxData {
        (self.vtable.box_data_mut)(app, self.id)
    }

    /// Compute the layout for this box.
    pub fn layout(self, app: &mut App, constraints: BoxConstraints, parent_uses_size: bool) {
        debug_assert!(
            !app.is_disposed(self.id),
            "layout on a disposed render object"
        );
        debug_assert!(constraints.debug_assert_is_valid(true));
        let same = self.box_data(app).constraints == Some(constraints);
        self.as_object()
            .run_layout(app, parent_uses_size, constraints.is_tight(), same, |app| {
                self.box_data_mut(app).constraints = Some(constraints)
            });
    }

    /// The size of this box.
    ///
    /// # Panics
    ///
    /// If this box has not been laid out.
    pub fn size(self, app: &App) -> Size {
        self.box_data(app)
            .size
            .unwrap_or_else(|| panic!("RenderBox was not laid out: {self:?}"))
    }

    /// Sets the size of this box.
    pub fn set_size(self, app: &mut App, size: Size) {
        self.box_data_mut(app).size = Some(size);
    }

    /// The box constraints most recently supplied by the parent.
    ///
    /// # Panics
    ///
    /// If layout has not yet happened.
    pub fn constraints(self, app: &App) -> BoxConstraints {
        self.box_data(app).constraints.unwrap_or_else(|| {
            panic!("A RenderObject does not have any constraints before it has been laid out.")
        })
    }

    /// Returns the minimum width that this box could be without failing to correctly paint its
    /// contents within itself, without clipping.
    ///
    /// The height argument may give a specific height to assume. The given height can be
    /// infinite, meaning that the intrinsic width in an unconstrained environment is being
    /// requested. The given height should never be negative.
    ///
    /// This function should only be called on one's children. Calling this function couples the
    /// child with the parent so that when the child's layout changes, the parent is notified
    /// (via [`RenderBox::mark_needs_layout`]).
    ///
    /// Calling this function is expensive as it can result in O(N^2) behavior.
    ///
    /// Do not override this method. Instead, implement
    /// [`RenderBox::compute_min_intrinsic_width`].
    pub fn get_min_intrinsic_width(self, app: &mut App, height: f64) -> f64 {
        debug_assert!(
            height >= 0.0,
            "The height argument to get_min_intrinsic_width was negative. If you perform \
             computations on another height before passing it to get_min_intrinsic_width, \
             consider using f64::max or clamp_double to force the value into the valid range."
        );
        memoize_intrinsic(
            self,
            app,
            IntrinsicDimension::MinWidth,
            height,
            self.vtable.compute_min_intrinsic_width,
        )
    }

    /// Returns the smallest width beyond which increasing the width never decreases the
    /// preferred height. The preferred height is the value that would be returned by
    /// [`get_min_intrinsic_height`](Self::get_min_intrinsic_height) for that width.
    ///
    /// Do not override this method. Instead, implement
    /// [`RenderBox::compute_max_intrinsic_width`].
    pub fn get_max_intrinsic_width(self, app: &mut App, height: f64) -> f64 {
        debug_assert!(
            height >= 0.0,
            "The height argument to get_max_intrinsic_width was negative."
        );
        memoize_intrinsic(
            self,
            app,
            IntrinsicDimension::MaxWidth,
            height,
            self.vtable.compute_max_intrinsic_width,
        )
    }

    /// Returns the minimum height that this box could be without failing to correctly paint its
    /// contents within itself, without clipping.
    ///
    /// Do not override this method. Instead, implement
    /// [`RenderBox::compute_min_intrinsic_height`].
    pub fn get_min_intrinsic_height(self, app: &mut App, width: f64) -> f64 {
        debug_assert!(
            width >= 0.0,
            "The width argument to get_min_intrinsic_height was negative."
        );
        memoize_intrinsic(
            self,
            app,
            IntrinsicDimension::MinHeight,
            width,
            self.vtable.compute_min_intrinsic_height,
        )
    }

    /// Returns the smallest height beyond which increasing the height never decreases the
    /// preferred width. The preferred width is the value that would be returned by
    /// [`get_min_intrinsic_width`](Self::get_min_intrinsic_width) for that height.
    ///
    /// Do not override this method. Instead, implement
    /// [`RenderBox::compute_max_intrinsic_height`].
    pub fn get_max_intrinsic_height(self, app: &mut App, width: f64) -> f64 {
        debug_assert!(
            width >= 0.0,
            "The width argument to get_max_intrinsic_height was negative."
        );
        memoize_intrinsic(
            self,
            app,
            IntrinsicDimension::MaxHeight,
            width,
            self.vtable.compute_max_intrinsic_height,
        )
    }

    /// Returns the [`Size`] that this box would like to be given the provided
    /// [`BoxConstraints`].
    ///
    /// The size returned by this method is guaranteed to be the same size that this box computes
    /// for itself during layout given the same constraints.
    ///
    /// This function should only be called on one's children. Calling this function couples the
    /// child with the parent so that when the child's layout changes, the parent is notified
    /// (via [`RenderBox::mark_needs_layout`]).
    ///
    /// This layout is called "dry" layout as opposed to the regular "wet" layout run performed
    /// by [`RenderObject::perform_layout`] because it computes the desired size for the given
    /// constraints without changing any internal state.
    ///
    /// Calling this function is expensive as it can result in O(N^2) behavior.
    ///
    /// Do not override this method. Instead, implement [`RenderBox::compute_dry_layout`].
    pub fn get_dry_layout(self, app: &mut App, constraints: BoxConstraints) -> Size {
        if !should_cache_intrinsics(self, app) {
            return self.compute_dry_layout(app, constraints);
        }
        if let Some(cached) = self
            .layout_cache(app)
            .cached_dry_layout_sizes
            .get(&constraints)
        {
            return *cached;
        }
        let result = self.compute_dry_layout(app, constraints);
        self.layout_cache_mut(app)
            .cached_dry_layout_sizes
            .insert(constraints, result);
        result
    }

    /// Flutter's `RenderBox._computeDryLayout`: the re-entrancy guard around the virtual.
    fn compute_dry_layout(self, app: &mut App, constraints: BoxConstraints) -> Size {
        if cfg!(debug_assertions) {
            assert!(!self.box_data(app).debug_computing_this_dry_layout);
            self.box_data_mut(app).debug_computing_this_dry_layout = true;
        }
        let result = (self.vtable.compute_dry_layout)(app, self.id, constraints);
        if cfg!(debug_assertions) {
            self.box_data_mut(app).debug_computing_this_dry_layout = false;
        }
        result
    }

    /// Returns the distance from the top of the box to the first baseline of the box's contents
    /// for the given `constraints`, or `None` if this box does not have any baselines.
    ///
    /// This method calls [`RenderBox::compute_dry_baseline`] under the hood and caches the
    /// result. Boxes typically don't override
    /// [`get_dry_baseline`](Self::get_dry_baseline). Instead, consider overriding
    /// [`RenderBox::compute_dry_baseline`] such that it returns a baseline location that is
    /// consistent with [`get_distance_to_actual_baseline`](Self::get_distance_to_actual_baseline).
    ///
    /// This method is usually called by the [`RenderBox::compute_dry_baseline`] or the
    /// [`RenderBox::compute_dry_layout`] implementation of a parent box to get the baseline
    /// location of a box child. Unlike
    /// [`get_distance_to_baseline`](Self::get_distance_to_baseline), this method takes a
    /// [`BoxConstraints`] as an argument and computes the baseline location as if the box was
    /// laid out by the parent using that [`BoxConstraints`].
    pub fn get_dry_baseline(
        self,
        app: &mut App,
        constraints: BoxConstraints,
        baseline: TextBaseline,
    ) -> Option<f64> {
        let baseline_offset = memoize_baseline(self, app, constraints, baseline, |this, app| {
            this.compute_dry_baseline(app, constraints, baseline)
        })
        .offset();
        // This assert makes sure compute_dry_baseline always gets called in debug mode, in case
        // the compute_dry_baseline implementation invokes debug_cannot_compute_dry_layout. The
        // check is skipped when debug_checking_intrinsics is true to avoid slowing down the app
        // significantly.
        if cfg!(debug_assertions) && !debug_checking_intrinsics() {
            assert_eq!(
                baseline_offset,
                (self.vtable.compute_dry_baseline)(app, self.id, constraints, baseline)
            );
        }
        baseline_offset
    }

    /// Flutter's `RenderBox._computeDryBaseline`: the re-entrancy guard around the virtual.
    fn compute_dry_baseline(
        self,
        app: &mut App,
        constraints: BoxConstraints,
        baseline: TextBaseline,
    ) -> BaselineOffset {
        if cfg!(debug_assertions) {
            assert!(!self.box_data(app).debug_computing_this_dry_baseline);
            self.box_data_mut(app).debug_computing_this_dry_baseline = true;
        }
        let result = BaselineOffset((self.vtable.compute_dry_baseline)(
            app,
            self.id,
            constraints,
            baseline,
        ));
        if cfg!(debug_assertions) {
            self.box_data_mut(app).debug_computing_this_dry_baseline = false;
        }
        result
    }

    /// Returns the distance from the y-coordinate of the position of the box to the y-coordinate
    /// of the first given baseline in the box's contents.
    ///
    /// Used by certain layout models to align adjacent boxes on a common baseline, regardless of
    /// padding, font size differences, etc. If there is no baseline, this function returns the
    /// distance from the y-coordinate of the position of the box to the y-coordinate of the
    /// bottom of the box (i.e., the height of the box) unless the caller passes true for
    /// `only_real`, in which case the function returns `None`.
    ///
    /// Only call this function after calling [`layout`](Self::layout) on this box. You are only
    /// allowed to call this from the parent of this box during that parent's
    /// [`RenderObject::perform_layout`] or [`RenderObject::paint`] functions.
    ///
    /// To override the baseline computation, override
    /// [`RenderBox::compute_distance_to_actual_baseline`].
    pub fn get_distance_to_baseline(
        self,
        app: &mut App,
        baseline: TextBaseline,
        only_real: bool,
    ) -> Option<f64> {
        debug_assert!(
            !DEBUG_DOING_BASELINE.get(),
            "Please see the documentation for compute_distance_to_actual_baseline for the \
             required calling conventions of this method."
        );
        debug_assert!(!self.as_object().debug_needs_layout(app) || debug_checking_intrinsics());
        let result = {
            let _doing_baseline = DebugDoingBaseline::enter();
            self.get_distance_to_actual_baseline(app, baseline)
        };
        match result {
            None if !only_real => Some(self.size(app).height()),
            result => result,
        }
    }

    /// Calls [`RenderBox::compute_distance_to_actual_baseline`] and caches the result.
    ///
    /// This function must only be called from
    /// [`get_distance_to_baseline`](Self::get_distance_to_baseline) and
    /// [`RenderBox::compute_distance_to_actual_baseline`].
    pub fn get_distance_to_actual_baseline(
        self,
        app: &mut App,
        baseline: TextBaseline,
    ) -> Option<f64> {
        debug_assert!(
            DEBUG_DOING_BASELINE.get() || !cfg!(debug_assertions),
            "Please see the documentation for compute_distance_to_actual_baseline for the \
             required calling conventions of this method."
        );
        let constraints = self
            .box_data(app)
            .constraints
            .expect("a RenderBox has constraints once it has been laid out");
        memoize_baseline(self, app, constraints, baseline, |this, app| {
            BaselineOffset((this.vtable.compute_distance_to_actual_baseline)(
                app, this.id, baseline,
            ))
        })
        .offset()
    }

    fn layout_cache(self, app: &App) -> &LayoutCacheStorage {
        &self.box_data(app).layout_cache
    }

    fn layout_cache_mut(self, app: &mut App) -> &mut LayoutCacheStorage {
        &mut self.box_data_mut(app).layout_cache
    }

    /// [`BoxParentData`] stored on this child by its box parent.
    ///
    /// Dart's `child.parentData! as BoxParentData`: a subclass answers with its own
    /// [`BoxParentData`] half through [`ParentData::provide`].
    pub fn box_parent_data(self, app: &App) -> &BoxParentData {
        box_parent_data_of(app, self.as_object())
    }

    /// Convert the given point from the global coordinate system in logical pixels to the
    /// local coordinate system for this box.
    ///
    /// This method will un-project the point from the screen onto the widget, which makes it
    /// different from `MatrixUtils.transformPoint`.
    ///
    /// If the transform from global coordinates to local coordinates is degenerate, this
    /// function returns `Offset::ZERO`.
    ///
    /// If `ancestor` is non-null, this function converts the given point from the coordinate
    /// system of `ancestor` (which must be an ancestor of this render object) instead of from
    /// the global coordinate system.
    ///
    /// This method is implemented in terms of [`AnyRenderObject::get_transform_to`].
    pub fn global_to_local(
        self,
        app: &App,
        point: Offset,
        ancestor: Option<AnyRenderObject>,
    ) -> Offset {
        // We want to find the local point that corresponds to the given point on
        // the screen, but that also physically resides on this RenderBox's local
        // render plane, so that it is useful for visually accurate gesture
        // processing in the local space. For that, we cannot simply transform the
        // 2D screen point to the 3D local space since the screen space lacks the
        // depth component |z|, and so there are many 3D points that correspond to
        // the screen point. We must first unproject the screen point onto the local
        // render plane to find the true 3D point that corresponds to the screen
        // point.
        //
        // We do orthogonal unprojection after undoing perspective, in local space.
        // The local render plane is the XY plane with normal vector <0, 0, 1>.
        // Unprojection is done by finding the intersection of the view vector with
        // the local XY plane at z = 0.
        let transform = self.as_object().get_transform_to(app, ancestor);
        let Some(transform) = transform.invert() else {
            return Offset::ZERO;
        };

        // Two points with the same screen x and y but different depths define the
        // view direction in local coordinates.
        let n = [0.0, 0.0, 1.0];
        let i = crate::object::perspective_transform(&transform, [0.0, 0.0, 0.0]);
        let d = subtract(
            crate::object::perspective_transform(&transform, [0.0, 0.0, 1.0]),
            i,
        );
        let s = crate::object::perspective_transform(&transform, [point.dx(), point.dy(), 0.0]);
        // Project the screen point onto the local render plane.
        let p = subtract(s, scale(d, dot(n, s) / dot(n, d)));
        Offset::new(p[0], p[1])
    }

    /// Convert the given point from the local coordinate system for this box to the global
    /// coordinate system in logical pixels.
    ///
    /// If `ancestor` is non-null, this function converts the given point to the coordinate
    /// system of `ancestor` (which must be an ancestor of this render object) instead of to
    /// the global coordinate system.
    ///
    /// This method is implemented in terms of [`AnyRenderObject::get_transform_to`]. If the
    /// transform matrix puts the given `point` on the line at infinity (for instance, when
    /// the transform matrix is the zero matrix), this method returns (NaN, NaN).
    pub fn local_to_global(
        self,
        app: &App,
        point: Offset,
        ancestor: Option<AnyRenderObject>,
    ) -> Offset {
        inset_painting::transform_point(&self.as_object().get_transform_to(app, ancestor), point)
    }

    /// See [`AnyRenderObject::parent_data_of_mut`].
    pub fn parent_data_of_mut<P: crate::object::ParentData + 'static>(
        self,
        app: &mut App,
    ) -> &mut P {
        self.as_object().parent_data_of_mut(app)
    }

    /// See [`AnyRenderObject::parent_data_is`].
    pub fn parent_data_is<P: crate::object::ParentData + 'static>(self, app: &App) -> bool {
        self.as_object().parent_data_is::<P>(app)
    }
}

impl crate::object::ErasedRenderObject for AnyRenderBox {
    fn as_object(self) -> AnyRenderObject {
        AnyRenderBox::as_object(self)
    }

    fn from_object(object: AnyRenderObject) -> AnyRenderBox {
        object
            .as_box()
            .expect("the render object is not a RenderBox")
    }
}

impl From<AnyRenderBox> for AnyRenderObject {
    fn from(box_: AnyRenderBox) -> AnyRenderObject {
        box_.as_object()
    }
}

fn subtract(a: [f64; 3], b: [f64; 3]) -> [f64; 3] {
    [a[0] - b[0], a[1] - b[1], a[2] - b[2]]
}

fn scale(a: [f64; 3], factor: f64) -> [f64; 3] {
    [a[0] * factor, a[1] * factor, a[2] * factor]
}

fn dot(a: [f64; 3], b: [f64; 3]) -> f64 {
    a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn to_string_marks_biggest_unconstrained_and_tight() {
        assert!(
            BoxConstraints::expand(None, None)
                .to_string()
                .contains("biggest")
        );
        assert!(BoxConstraints::new().to_string().contains("unconstrained"));
        assert!(
            BoxConstraints::tight_for(Some(50.0), None)
                .to_string()
                .contains("w=50")
        );
    }

    #[test]
    fn copy_with_replaces_fields() {
        let constraints = BoxConstraints::new()
            .min_width(3.0)
            .max_width(7.0)
            .min_height(11.0)
            .max_height(17.0);
        let copy = constraints.copy_with();
        assert_eq!(copy, constraints);
        let copy = constraints
            .copy_with()
            .min_width(13.0)
            .max_width(17.0)
            .min_height(111.0)
            .max_height(117.0);
        assert_eq!(copy.min_width, 13.0);
        assert_eq!(copy.max_width, 17.0);
        assert_eq!(copy.min_height, 111.0);
        assert_eq!(copy.max_height, 117.0);
        assert_ne!(copy, constraints);
        let mut copy_hasher = std::collections::hash_map::DefaultHasher::new();
        let mut original_hasher = std::collections::hash_map::DefaultHasher::new();
        copy.hash(&mut copy_hasher);
        constraints.hash(&mut original_hasher);
        assert_ne!(copy_hasher.finish(), original_hasher.finish());
    }

    #[test]
    fn operators() {
        let constraints = BoxConstraints::new()
            .min_width(3.0)
            .max_width(7.0)
            .min_height(11.0)
            .max_height(17.0);
        let copy = constraints * 2.0;
        assert_eq!(copy.min_width, 6.0);
        assert_eq!(copy.max_width, 14.0);
        assert_eq!(copy.min_height, 22.0);
        assert_eq!(copy.max_height, 34.0);
        assert_eq!(copy / 2.0, constraints);
        let copy = constraints.truncating_div(2.0);
        assert_eq!(copy.min_width, 1.0);
        assert_eq!(copy.max_width, 3.0);
        assert_eq!(copy.min_height, 5.0);
        assert_eq!(copy.max_height, 8.0);
        let copy = constraints % 3.0;
        assert_eq!(copy.min_width, 0.0);
        assert_eq!(copy.max_width, 1.0);
        assert_eq!(copy.min_height, 2.0);
        assert_eq!(copy.max_height, 2.0);
    }

    #[test]
    fn lerp_from_null_and_between() {
        assert!(BoxConstraints::lerp(None, None, 0.5).is_none());
        let constraints = BoxConstraints::new()
            .min_width(3.0)
            .max_width(7.0)
            .min_height(11.0)
            .max_height(17.0);
        let copy = BoxConstraints::lerp(None, Some(constraints), 0.5).unwrap();
        assert!((copy.min_width - 1.5).abs() < 1e-9);
        assert!((copy.max_width - 3.5).abs() < 1e-9);
        assert!((copy.min_height - 5.5).abs() < 1e-9);
        assert!((copy.max_height - 8.5).abs() < 1e-9);
        let copy = BoxConstraints::lerp(Some(constraints), None, 0.5).unwrap();
        assert!((copy.min_width - 1.5).abs() < 1e-9);
        let copy = BoxConstraints::lerp(
            Some(
                BoxConstraints::new()
                    .min_width(13.0)
                    .max_width(17.0)
                    .min_height(111.0)
                    .max_height(117.0),
            ),
            Some(constraints),
            0.2,
        )
        .unwrap();
        assert!((copy.min_width - 11.0).abs() < 1e-9);
        assert!((copy.max_width - 15.0).abs() < 1e-9);
        assert!((copy.min_height - 91.0).abs() < 1e-9);
        assert!((copy.max_height - 97.0).abs() < 1e-9);
    }

    #[test]
    fn lerp_unbounded_axes() {
        let constraints1 = BoxConstraints::new()
            .min_width(f64::INFINITY)
            .min_height(10.0)
            .max_height(20.0);
        let constraints2 = BoxConstraints::new()
            .min_width(f64::INFINITY)
            .min_height(20.0)
            .max_height(30.0);
        let constraints3 = BoxConstraints::new()
            .min_width(f64::INFINITY)
            .min_height(15.0)
            .max_height(25.0);
        assert_eq!(
            BoxConstraints::lerp(Some(constraints1), Some(constraints2), 0.5),
            Some(constraints3)
        );
    }

    #[test]
    fn lerp_overshoot_is_normalized() {
        assert_eq!(
            BoxConstraints::lerp(
                Some(BoxConstraints::tight_for(None, Some(25.0))),
                Some(BoxConstraints::tight_for(None, Some(0.0))),
                1.2,
            ),
            Some(BoxConstraints::tight_for(None, Some(0.0)))
        );
        let lerped = BoxConstraints::lerp(
            Some(BoxConstraints::tight(Size::new(10.0, 10.0))),
            Some(BoxConstraints::new().max_width(100.0).max_height(100.0)),
            -0.1,
        )
        .unwrap();
        assert!(lerped.is_normalized());
        assert_eq!(lerped, BoxConstraints::tight(Size::new(11.0, 11.0)));
    }

    #[test]
    fn normalize_raises_inverted_max() {
        let constraints = BoxConstraints::new()
            .min_width(3.0)
            .max_width(2.0)
            .min_height(11.0)
            .max_height(18.0);
        let copy = constraints.normalize();
        assert_eq!(copy.min_width, 3.0);
        assert_eq!(copy.max_width, 3.0);
        assert_eq!(copy.min_height, 11.0);
        assert_eq!(copy.max_height, 18.0);
    }

    #[test]
    fn from_view_constraints() {
        let unconstrained = BoxConstraints::from_view_constraints(ViewConstraints::default());
        assert_eq!(unconstrained, BoxConstraints::new());
        let constraints =
            BoxConstraints::from_view_constraints(ViewConstraints::new(1.0, 2.0, 3.0, 4.0));
        assert_eq!(
            constraints,
            BoxConstraints::new()
                .min_width(1.0)
                .max_width(2.0)
                .min_height(3.0)
                .max_height(4.0)
        );
    }

    #[test]
    fn constrain_size_and_attempt_to_preserve_aspect_ratio_empty_size() {
        let constraints = BoxConstraints::new()
            .min_width(10.0)
            .max_width(20.0)
            .min_height(10.0)
            .max_height(20.0);
        let constrained =
            constraints.constrain_size_and_attempt_to_preserve_aspect_ratio(Size::new(15.0, 0.0));
        assert_eq!(constrained, Size::new(15.0, 10.0));
    }
}

#[cfg(test)]
mod hit_test_tests {
    use super::*;

    #[derive(Debug)]
    struct DummyHitTestTarget;

    impl HitTestTarget for DummyHitTestTarget {
        fn handle_event(&self, _app: &mut App, _event: &PointerEvent, _entry: &HitTestEntry) {}
    }

    /// `box_test.dart`: `BoxHitTestResult wrapping HitTestResult`.
    #[test]
    fn wrapping_shares_the_path() {
        let transform = Matrix4::translation(40.0, 150.0);
        let mut wrapped = HitTestResult::new();
        wrapped.push_transform(transform);
        wrapped.add(HitTestEntry::new(DummyHitTestTarget));
        assert_eq!(wrapped.path().len(), 1);
        assert_eq!(wrapped.path()[0].transform(), Some(transform));

        let mut wrapping = BoxHitTestResult::wrap(&mut wrapped);
        wrapping.add(HitTestEntry::new(DummyHitTestTarget));
        assert_eq!(wrapping.path().len(), 2);
        assert_eq!(wrapping.path()[1].transform(), Some(transform));

        wrapped.add(HitTestEntry::new(DummyHitTestTarget));
        assert_eq!(wrapped.path().len(), 3);
        assert_eq!(wrapped.path()[2].transform(), Some(transform));
    }

    /// `box_test.dart`: `addWithPaintTransform`.
    #[test]
    fn add_with_paint_transform() {
        let mut base = HitTestResult::new();
        let mut result = BoxHitTestResult::wrap(&mut base);
        let mut positions = Vec::new();

        let is_hit = result.add_with_paint_transform(None, Offset::ZERO, |_, position| {
            positions.push(position);
            true
        });
        assert!(is_hit);
        assert_eq!(positions, [Offset::ZERO]);
        positions.clear();

        let is_hit = result.add_with_paint_transform(
            Some(Matrix4::translation(20.0, 30.0)),
            Offset::ZERO,
            |_, position| {
                positions.push(position);
                true
            },
        );
        assert!(is_hit);
        assert_eq!(positions, [Offset::new(-20.0, -30.0)]);
        positions.clear();

        let position = Offset::new(3.0, 4.0);
        let is_hit = result.add_with_paint_transform(None, position, |_, position| {
            positions.push(position);
            false
        });
        assert!(!is_hit);
        assert_eq!(positions, [position]);
        positions.clear();

        let is_hit =
            result.add_with_paint_transform(Some(Matrix4::IDENTITY), position, |_, position| {
                positions.push(position);
                true
            });
        assert!(is_hit);
        assert_eq!(positions, [position]);
        positions.clear();

        let is_hit = result.add_with_paint_transform(
            Some(Matrix4::translation(20.0, 30.0)),
            position,
            |_, position| {
                positions.push(position);
                true
            },
        );
        assert!(is_hit);
        assert_eq!(positions, [position - Offset::new(20.0, 30.0)]);
        positions.clear();

        let is_hit = result.add_with_paint_transform(
            Some(force_to_point(position)), // cannot be inverted
            position,
            |_, position| {
                positions.push(position);
                true
            },
        );
        assert!(!is_hit);
        assert!(positions.is_empty());
    }

    /// Dart's `MatrixUtils.forceToPoint`: every point maps to `offset`.
    fn force_to_point(offset: Offset) -> Matrix4 {
        let mut storage = [0.0; 16];
        storage[10] = 1.0;
        storage[12] = offset.dx() as f32;
        storage[13] = offset.dy() as f32;
        storage[15] = 1.0;
        Matrix4::from_flutter_array(&storage)
    }

    /// A child under a perspective transform, as Flutter's docs build one:
    /// `Matrix4.identity()..setEntry(3, 2, 0.001)..rotateY(0.5)`. Inverting it whole left
    /// its third row and column away from `(0, 0, 1, 0)`, and `push_transform`'s check
    /// panicked; with the perspective removed first the pointer lands where Flutter's does.
    #[test]
    fn a_hit_test_through_a_perspective_transform_lands_where_flutters_does() {
        let angle = 0.5_f64;
        let (cos, sin) = (angle.cos(), angle.sin());
        let perspective = 0.001;
        let mut storage = Matrix4::IDENTITY.to_flutter_array();
        storage[0] = cos as f32;
        storage[2] = -sin as f32;
        storage[8] = sin as f32;
        storage[10] = cos as f32;
        // Row 3 of `setEntry(3, 2, p)` times the rotation: (-p sin, 0, p cos, 1).
        storage[3] = (-perspective * sin) as f32;
        storage[11] = (perspective * cos) as f32;
        let transform = Matrix4::from_flutter_array(&storage);

        let mut base = HitTestResult::new();
        let mut result = BoxHitTestResult::wrap(&mut base);
        let position = Offset::new(100.0, 50.0);
        let mut landed = None;
        let is_hit = result.add_with_paint_transform(Some(transform), position, |result, local| {
            result.add(HitTestEntry::new(DummyHitTestTarget));
            landed = Some(local);
            true
        });
        assert!(is_hit);

        // Flutter's arithmetic in f64: without its third row and column the transform is
        // x' = x cos, y' = y, w' = 1 - p sin x, and its inverse divides the other way.
        let w = 1.0 + perspective * sin * position.dx() / cos;
        let expected = Offset::new(position.dx() / cos / w, position.dy() / w);
        let landed = landed.expect("the child was hit-tested");
        assert!(
            (landed.dx() - expected.dx()).abs() < 1e-3
                && (landed.dy() - expected.dy()).abs() < 1e-3,
            "{landed:?} != {expected:?}"
        );
    }

    /// `box_test.dart`: `addWithPaintOffset`.
    #[test]
    fn add_with_paint_offset() {
        let mut base = HitTestResult::new();
        let mut result = BoxHitTestResult::wrap(&mut base);
        let mut positions = Vec::new();

        let is_hit = result.add_with_paint_offset(None, Offset::ZERO, |_, position| {
            positions.push(position);
            true
        });
        assert!(is_hit);
        assert_eq!(positions, [Offset::ZERO]);
        positions.clear();

        let is_hit = result.add_with_paint_offset(
            Some(Offset::new(55.0, 32.0)),
            Offset::ZERO,
            |_, position| {
                positions.push(position);
                true
            },
        );
        assert!(is_hit);
        assert_eq!(positions, [Offset::new(-55.0, -32.0)]);
        positions.clear();

        let position = Offset::new(3.0, 4.0);
        let is_hit = result.add_with_paint_offset(
            Some(Offset::new(55.0, 32.0)),
            position,
            |result, position| {
                result.add(HitTestEntry::new(DummyHitTestTarget));
                positions.push(position);
                true
            },
        );
        assert!(is_hit);
        assert_eq!(positions, [position - Offset::new(55.0, 32.0)]);
        assert_eq!(
            result.path()[0].transform(),
            Some(Matrix4::translation(-55.0, -32.0)),
            "the entry records the transform to the child"
        );
    }

    /// `box_test.dart`: `addWithRawTransform`.
    #[test]
    fn add_with_raw_transform() {
        let mut base = HitTestResult::new();
        let mut result = BoxHitTestResult::wrap(&mut base);
        let mut positions = Vec::new();

        let is_hit = result.add_with_raw_transform(
            Some(Matrix4::translation(20.0, 30.0)),
            Offset::ZERO,
            |_, position| {
                positions.push(position);
                true
            },
        );
        assert!(is_hit);
        assert_eq!(positions, [Offset::new(20.0, 30.0)]);
        positions.clear();

        let position = Offset::new(3.0, 4.0);
        let is_hit =
            result.add_with_raw_transform(Some(Matrix4::IDENTITY), position, |_, position| {
                positions.push(position);
                true
            });
        assert!(is_hit);
        assert_eq!(positions, [position]);
    }
}

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

    use super::*;
    use crate::object::RenderObjectData;

    /// A leaf whose minimum intrinsic width is `100.0 + height`, counting how often it is
    /// actually computed.
    struct CountingBox {
        render_object: RenderObjectData,
        render_box: RenderBoxData,
        computations: Rc<Cell<u32>>,
    }

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

        fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
            let size = self.constraints(app).smallest();
            self.set_size(app, size);
        }
    }

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

        fn compute_min_intrinsic_width(
            self: RenderHandle<Self>,
            app: &mut App,
            height: f64,
        ) -> f64 {
            let computations = self.get(app).computations.clone();
            computations.set(computations.get() + 1);
            100.0 + height
        }
    }

    /// `box_test.dart`: `Intrinsics cache' and `Intrinsics cache is cleared when the render
    /// object is marked as needing layout`.
    #[test]
    fn the_intrinsic_cache_is_reused_and_cleared_by_mark_needs_layout() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let computations = Rc::new(Cell::new(0));
        let box_ = RenderHandle::new_box(
            &mut app,
            CountingBox {
                render_object: RenderObjectData::new(),
                render_box: RenderBoxData::new(),
                computations: computations.clone(),
            },
        );

        assert_eq!(box_.as_box().get_min_intrinsic_width(&mut app, 0.0), 100.0);
        assert_eq!(computations.get(), 1);

        assert_eq!(box_.as_box().get_min_intrinsic_width(&mut app, 0.0), 100.0);
        assert_eq!(computations.get(), 1, "the memoized value is reused");

        assert_eq!(box_.as_box().get_min_intrinsic_width(&mut app, 10.0), 110.0);
        assert_eq!(computations.get(), 2, "another height is another entry");

        box_.mark_needs_layout(&mut app);
        assert_eq!(box_.as_box().get_min_intrinsic_width(&mut app, 0.0), 100.0);
        assert_eq!(computations.get(), 3, "mark_needs_layout cleared the cache");
    }

    /// Flutter's `RenderBox.markNeedsLayout`: a box whose intrinsics the parent read defers to
    /// the parent, because the parent's layout depends on them.
    #[test]
    fn clearing_a_cached_intrinsic_marks_the_parent() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let computations = Rc::new(Cell::new(0));
        let child = RenderHandle::new_box(
            &mut app,
            CountingBox {
                render_object: RenderObjectData::new(),
                render_box: RenderBoxData::new(),
                computations,
            },
        );
        let parent = crate::proxy_box::RenderConstrainedBox::new(
            &mut app,
            BoxConstraints::new(),
            Some(child.as_box()),
        );
        parent.layout(&mut app, BoxConstraints::new().max_width(50.0), false);
        assert!(!parent.debug_needs_layout(&app));

        child.as_box().get_min_intrinsic_width(&mut app, 0.0);
        child.mark_needs_layout(&mut app);
        assert!(
            parent.debug_needs_layout(&app),
            "the parent read an intrinsic, so it has to lay out again"
        );
    }
}