inset-rendering 0.16.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
//! Flutter counterpart: `rendering/table.dart`.
//!
//! The table's semantics (`describeSemanticsConfiguration`, `assembleSemanticsNode`) wait with
//! the rest of accessibility.

use std::any::{Any, TypeId};
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::rc::Rc;

use inset_embedder::{Offset, Rect, Size, TextBaseline, TextDirection};
use inset_foundation::{App, PRECISION_ERROR_TOLERANCE};
use inset_painting::{BoxPainter, ClipContext, Decoration, ImageConfiguration};

use crate::box_::{
    AnyRenderBox, BoxConstraints, BoxHitTestResult, BoxParentData, DryLayoutFailure, RenderBox,
    RenderBoxData,
};
use crate::object::{AnyRenderObject, ParentData, RenderHandle, RenderObject, RenderObjectData};
use crate::painting_context::PaintingContext;
use crate::table_border::TableBorder;

/// Parent data used by [`RenderTable`] for its children.
#[derive(Debug, Default)]
pub struct TableCellParentData {
    box_parent_data: BoxParentData,

    /// Where this cell should be placed vertically.
    ///
    /// When using [`TableCellVerticalAlignment::Baseline`], the text baseline must be set as
    /// well.
    pub vertical_alignment: Option<TableCellVerticalAlignment>,

    /// The column that the child was in the last time it was laid out.
    pub x: Option<usize>,

    /// The row that the child was in the last time it was laid out.
    pub y: Option<usize>,
}

impl TableCellParentData {
    /// Creates table cell parent data at offset zero, with the default vertical alignment.
    pub const fn new() -> TableCellParentData {
        TableCellParentData {
            box_parent_data: BoxParentData::new(),
            vertical_alignment: None,
            x: None,
            y: None,
        }
    }
}

impl ParentData for TableCellParentData {
    fn provide(&self, id: TypeId) -> Option<&dyn Any> {
        if id == TypeId::of::<TableCellParentData>() {
            return Some(self);
        }
        self.box_parent_data.provide(id)
    }

    fn provide_mut(&mut self, id: TypeId) -> Option<&mut dyn Any> {
        if id == TypeId::of::<TableCellParentData>() {
            return Some(self);
        }
        self.box_parent_data.provide_mut(id)
    }
}

impl fmt::Display for TableCellParentData {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.box_parent_data, f)?;
        match self.vertical_alignment {
            None => f.write_str("; default vertical alignment"),
            Some(vertical_alignment) => write!(f, "; {vertical_alignment:?}"),
        }
    }
}

/// A shared, immutable [`TableColumnWidth`] (a Dart object reference). Two references are the
/// same column width only when they point at the same value, as Dart compares these objects.
pub type TableColumnWidthRef = Rc<dyn TableColumnWidth>;

/// Base class to describe how wide a column in a [`RenderTable`] should be.
///
/// To size a column to a specific number of pixels, use a [`FixedColumnWidth`]. This is the
/// cheapest way to size a column.
///
/// Other algorithms that are relatively cheap include [`FlexColumnWidth`], which distributes
/// the space equally among the flexible columns, [`FractionColumnWidth`], which sizes a column
/// based on the size of the table's container.
pub trait TableColumnWidth: fmt::Debug {
    /// The smallest width that the column can have.
    ///
    /// The `cells` argument is an iterable that provides all the cells in the table for this
    /// column. Walking the cells is by definition O(N), so algorithms that do that should be
    /// considered expensive.
    ///
    /// The `container_width` argument is the `max_width` of the incoming constraints for the
    /// table, and might be infinite.
    fn min_intrinsic_width(
        &self,
        app: &mut App,
        cells: &[AnyRenderBox],
        container_width: f64,
    ) -> f64;

    /// The ideal width that the column should have. This must be equal to or greater than the
    /// [`min_intrinsic_width`](Self::min_intrinsic_width). The column might be bigger than this
    /// width, e.g. if the column is flexible or if the table's width ends up being forced to be
    /// bigger than the sum of all the `max_intrinsic_width` values.
    ///
    /// The `cells` argument is an iterable that provides all the cells in the table for this
    /// column. Walking the cells is by definition O(N), so algorithms that do that should be
    /// considered expensive.
    ///
    /// The `container_width` argument is the `max_width` of the incoming constraints for the
    /// table, and might be infinite.
    fn max_intrinsic_width(
        &self,
        app: &mut App,
        cells: &[AnyRenderBox],
        container_width: f64,
    ) -> f64;

    /// The flex factor to apply to the cell if there is any room left over when laying out the
    /// table. The remaining space is distributed to any columns with flex in proportion to
    /// their flex value (higher values get more space).
    ///
    /// The `cells` argument is an iterable that provides all the cells in the table for this
    /// column. Walking the cells is by definition O(N), so algorithms that do that should be
    /// considered expensive.
    fn flex(&self, app: &mut App, cells: &[AnyRenderBox]) -> Option<f64> {
        let _ = (app, cells);
        None
    }

    /// Downcast support for Dart's `is` checks on a column width.
    fn as_any(&self) -> &dyn Any;
}

/// Sizes the column according to the intrinsic dimensions of all the cells in that column.
///
/// This is a very expensive way to size a column.
///
/// A flex value can be provided. If specified (and non-`None`), the column will participate in
/// the distribution of remaining space once all the non-flexible columns have been sized.
pub struct IntrinsicColumnWidth {
    flex: Option<f64>,
}

impl IntrinsicColumnWidth {
    /// Creates a column width based on intrinsic sizing.
    ///
    /// This sizing algorithm is very expensive.
    ///
    /// The `flex` argument specifies the flex factor to apply to the column if there is any
    /// room left over when laying out the table. If `flex` is `None` (the default), the table
    /// will not distribute any extra space to the column.
    pub const fn new(flex: Option<f64>) -> IntrinsicColumnWidth {
        IntrinsicColumnWidth { flex }
    }
}

impl TableColumnWidth for IntrinsicColumnWidth {
    fn min_intrinsic_width(
        &self,
        app: &mut App,
        cells: &[AnyRenderBox],
        _container_width: f64,
    ) -> f64 {
        let mut result: f64 = 0.0;
        for cell in cells {
            result = result.max(cell.get_min_intrinsic_width(app, f64::INFINITY));
        }
        result
    }

    fn max_intrinsic_width(
        &self,
        app: &mut App,
        cells: &[AnyRenderBox],
        _container_width: f64,
    ) -> f64 {
        let mut result: f64 = 0.0;
        for cell in cells {
            result = result.max(cell.get_max_intrinsic_width(app, f64::INFINITY));
        }
        result
    }

    fn flex(&self, _app: &mut App, _cells: &[AnyRenderBox]) -> Option<f64> {
        self.flex
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl fmt::Debug for IntrinsicColumnWidth {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.flex {
            Some(flex) => write!(f, "IntrinsicColumnWidth(flex: {flex:.1})"),
            None => f.write_str("IntrinsicColumnWidth(flex: null)"),
        }
    }
}

/// Sizes the column to a specific number of pixels.
///
/// This is the cheapest way to size a column.
pub struct FixedColumnWidth {
    /// The width the column should occupy in logical pixels.
    pub value: f64,
}

impl FixedColumnWidth {
    /// Creates a column width based on a fixed number of logical pixels.
    pub const fn new(value: f64) -> FixedColumnWidth {
        FixedColumnWidth { value }
    }
}

impl TableColumnWidth for FixedColumnWidth {
    fn min_intrinsic_width(
        &self,
        _app: &mut App,
        _cells: &[AnyRenderBox],
        _container_width: f64,
    ) -> f64 {
        self.value
    }

    fn max_intrinsic_width(
        &self,
        _app: &mut App,
        _cells: &[AnyRenderBox],
        _container_width: f64,
    ) -> f64 {
        self.value
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

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

/// Sizes the column to a fraction of the table's constraints' `max_width`.
///
/// This is a cheap way to size a column.
pub struct FractionColumnWidth {
    /// The fraction of the table's constraints' `max_width` that this column should occupy.
    pub value: f64,
}

impl FractionColumnWidth {
    /// Creates a column width based on a fraction of the table's constraints' `max_width`.
    pub const fn new(value: f64) -> FractionColumnWidth {
        FractionColumnWidth { value }
    }
}

impl TableColumnWidth for FractionColumnWidth {
    fn min_intrinsic_width(
        &self,
        _app: &mut App,
        _cells: &[AnyRenderBox],
        container_width: f64,
    ) -> f64 {
        if !container_width.is_finite() {
            return 0.0;
        }
        self.value * container_width
    }

    fn max_intrinsic_width(
        &self,
        _app: &mut App,
        _cells: &[AnyRenderBox],
        container_width: f64,
    ) -> f64 {
        if !container_width.is_finite() {
            return 0.0;
        }
        self.value * container_width
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

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

/// Sizes the column by taking a part of the remaining space once all the other columns have
/// been laid out.
///
/// For example, if two columns have a [`FlexColumnWidth`], then half the space will go to one
/// and half the space will go to the other.
///
/// This is a cheap way to size a column.
pub struct FlexColumnWidth {
    /// The fraction of the remaining space once all the other columns have been laid out that
    /// this column should occupy.
    pub value: f64,
}

thread_local! {
    static DEFAULT_FLEX_COLUMN_WIDTH: TableColumnWidthRef = Rc::new(FlexColumnWidth::new(1.0));
}

impl FlexColumnWidth {
    /// Creates a column width based on a fraction of the remaining space once all the other
    /// columns have been laid out. Dart's default `value` is 1.0.
    pub const fn new(value: f64) -> FlexColumnWidth {
        FlexColumnWidth { value }
    }

    /// Dart's `const FlexColumnWidth()`, the default column width of a table: one shared value,
    /// as Dart's constant is one object, so a table given the default again is unchanged.
    pub fn default_width() -> TableColumnWidthRef {
        DEFAULT_FLEX_COLUMN_WIDTH.with(Rc::clone)
    }
}

impl TableColumnWidth for FlexColumnWidth {
    fn min_intrinsic_width(
        &self,
        _app: &mut App,
        _cells: &[AnyRenderBox],
        _container_width: f64,
    ) -> f64 {
        0.0
    }

    fn max_intrinsic_width(
        &self,
        _app: &mut App,
        _cells: &[AnyRenderBox],
        _container_width: f64,
    ) -> f64 {
        0.0
    }

    fn flex(&self, _app: &mut App, _cells: &[AnyRenderBox]) -> Option<f64> {
        Some(self.value)
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

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

/// Sizes the column such that it is the size that is the maximum of two column width
/// specifications.
///
/// For example, to have a column be 10% of the container width or 100px, whichever is bigger,
/// you could use:
///
/// ```ignore
/// MaxColumnWidth::new(Rc::new(FixedColumnWidth::new(100.0)), Rc::new(FractionColumnWidth::new(0.1)))
/// ```
///
/// Both specifications are evaluated, so if either specification is expensive, so is this.
#[derive(Debug)]
pub struct MaxColumnWidth {
    /// A lower bound for the width of this column.
    pub a: TableColumnWidthRef,
    /// Another lower bound for the width of this column.
    pub b: TableColumnWidthRef,
}

impl MaxColumnWidth {
    /// Creates a column width that is the maximum of two other column widths.
    pub fn new(a: TableColumnWidthRef, b: TableColumnWidthRef) -> MaxColumnWidth {
        MaxColumnWidth { a, b }
    }
}

impl TableColumnWidth for MaxColumnWidth {
    fn min_intrinsic_width(
        &self,
        app: &mut App,
        cells: &[AnyRenderBox],
        container_width: f64,
    ) -> f64 {
        self.a
            .min_intrinsic_width(app, cells, container_width)
            .max(self.b.min_intrinsic_width(app, cells, container_width))
    }

    fn max_intrinsic_width(
        &self,
        app: &mut App,
        cells: &[AnyRenderBox],
        container_width: f64,
    ) -> f64 {
        self.a
            .max_intrinsic_width(app, cells, container_width)
            .max(self.b.max_intrinsic_width(app, cells, container_width))
    }

    fn flex(&self, app: &mut App, cells: &[AnyRenderBox]) -> Option<f64> {
        let a_flex = self.a.flex(app, cells);
        let b_flex = self.b.flex(app, cells);
        match (a_flex, b_flex) {
            (None, _) => b_flex,
            (Some(_), None) => a_flex,
            (Some(a_flex), Some(b_flex)) => Some(a_flex.max(b_flex)),
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// Sizes the column such that it is the size that is the minimum of two column width
/// specifications.
///
/// For example, to have a column be 10% of the container width but never bigger than 100px,
/// you could use:
///
/// ```ignore
/// MinColumnWidth::new(Rc::new(FixedColumnWidth::new(100.0)), Rc::new(FractionColumnWidth::new(0.1)))
/// ```
///
/// Both specifications are evaluated, so if either specification is expensive, so is this.
#[derive(Debug)]
pub struct MinColumnWidth {
    /// An upper bound for the width of this column.
    pub a: TableColumnWidthRef,
    /// Another upper bound for the width of this column.
    pub b: TableColumnWidthRef,
}

impl MinColumnWidth {
    /// Creates a column width that is the minimum of two other column widths.
    pub fn new(a: TableColumnWidthRef, b: TableColumnWidthRef) -> MinColumnWidth {
        MinColumnWidth { a, b }
    }
}

impl TableColumnWidth for MinColumnWidth {
    fn min_intrinsic_width(
        &self,
        app: &mut App,
        cells: &[AnyRenderBox],
        container_width: f64,
    ) -> f64 {
        self.a
            .min_intrinsic_width(app, cells, container_width)
            .min(self.b.min_intrinsic_width(app, cells, container_width))
    }

    fn max_intrinsic_width(
        &self,
        app: &mut App,
        cells: &[AnyRenderBox],
        container_width: f64,
    ) -> f64 {
        self.a
            .max_intrinsic_width(app, cells, container_width)
            .min(self.b.max_intrinsic_width(app, cells, container_width))
    }

    fn flex(&self, app: &mut App, cells: &[AnyRenderBox]) -> Option<f64> {
        let a_flex = self.a.flex(app, cells);
        let b_flex = self.b.flex(app, cells);
        match (a_flex, b_flex) {
            (None, _) => b_flex,
            (Some(_), None) => a_flex,
            (Some(a_flex), Some(b_flex)) => Some(a_flex.min(b_flex)),
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// Vertical alignment options for cells in [`RenderTable`] objects.
///
/// This is specified using [`TableCellParentData`] objects on the parent data of the children
/// of the [`RenderTable`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TableCellVerticalAlignment {
    /// Cells with this alignment are placed with their top at the top of the row.
    #[default]
    Top,

    /// Cells with this alignment are vertically centered in the row.
    Middle,

    /// Cells with this alignment are placed with their bottom at the bottom of the row.
    Bottom,

    /// Cells with this alignment are aligned such that they all share the same baseline. Cells
    /// with no baseline are top-aligned instead. The baseline used is specified by
    /// [`RenderTable::text_baseline`]. It is not valid to use the baseline value if
    /// [`RenderTable::text_baseline`] is not specified.
    ///
    /// This vertical alignment is relatively expensive because it causes the table to compute
    /// the baseline for each cell in the row.
    Baseline,

    /// Cells with this alignment are sized to be as tall as the row, then made to fit the row.
    /// If all the cells have this alignment, then the row will have zero height.
    Fill,

    /// Cells with this alignment are sized to be the same height as the tallest cell in the
    /// row.
    IntrinsicHeight,
}

/// A table where the columns and rows are sized to fit the contents of the cells.
pub struct RenderTable {
    render_object: RenderObjectData,
    render_box: RenderBoxData,
    // Children are stored in row-major order.
    // children.len() must be rows * columns
    children: Vec<Option<AnyRenderBox>>,
    columns: usize,
    rows: usize,
    column_widths: HashMap<usize, TableColumnWidthRef>,
    default_column_width: TableColumnWidthRef,
    text_direction: TextDirection,
    border: Option<TableBorder>,
    // row_decorations and row_decoration_painters need to be in sync. They have to either both
    // be None or have same length.
    row_decorations: Option<Vec<Option<Box<dyn Decoration>>>>,
    row_decoration_painters: Option<Vec<Option<Box<dyn BoxPainter>>>>,
    configuration: ImageConfiguration,
    default_vertical_alignment: TableCellVerticalAlignment,
    text_baseline: Option<TextBaseline>,
    // cache the table geometry for painting purposes
    row_tops: Vec<f64>,
    column_lefts: Option<Vec<f64>>,
    table_width: f64,
    baseline_distance: Option<f64>,
}

impl RenderTable {
    /// Creates a table render object with no rows or columns.
    ///
    /// Dart's `columns`, `rows` and `children` arguments are [`set_columns`](Self::set_columns),
    /// [`set_rows`](Self::set_rows) and [`set_children`](Self::set_children), and its other named
    /// arguments are the setters: [`set_column_widths`](Self::set_column_widths) (empty),
    /// [`set_default_column_width`](Self::set_default_column_width)
    /// ([`FlexColumnWidth::default_width`]), [`set_border`](Self::set_border),
    /// [`set_row_decorations`](Self::set_row_decorations),
    /// [`set_configuration`](Self::set_configuration) ([`ImageConfiguration::EMPTY`]),
    /// [`set_default_vertical_alignment`](Self::set_default_vertical_alignment)
    /// ([`TableCellVerticalAlignment::Top`]) and [`set_text_baseline`](Self::set_text_baseline).
    pub fn new(app: &mut App, text_direction: TextDirection) -> RenderHandle<RenderTable> {
        RenderHandle::new_box(
            app,
            RenderTable {
                render_object: RenderObjectData::new(),
                render_box: RenderBoxData::new(),
                children: Vec::new(),
                columns: 0,
                rows: 0,
                column_widths: HashMap::new(),
                default_column_width: FlexColumnWidth::default_width(),
                text_direction,
                border: None,
                row_decorations: None,
                row_decoration_painters: None,
                configuration: ImageConfiguration::EMPTY,
                default_vertical_alignment: TableCellVerticalAlignment::Top,
                text_baseline: None,
                row_tops: Vec::new(),
                column_lefts: None,
                table_width: 0.0,
                baseline_distance: None,
            },
        )
    }

    /// The number of vertical alignment lines in this table.
    ///
    /// Changing the number of columns will remove any children that no longer fit in the
    /// table.
    ///
    /// Changing the number of columns is an expensive operation because the table needs to
    /// rearrange its internal representation.
    pub fn columns(self: RenderHandle<Self>, app: &App) -> usize {
        self.get(app).columns
    }

    /// Sets [`columns`](Self::columns).
    pub fn set_columns(self: RenderHandle<Self>, app: &mut App, value: usize) {
        if value == self.columns(app) {
            return;
        }
        let old_columns = self.columns(app);
        let rows = self.rows(app);
        let this = self.get_mut(app);
        let old_children = std::mem::take(&mut this.children);
        this.columns = value;
        let columns = value;
        this.children = vec![None; columns * rows];
        let columns_to_copy = columns.min(old_columns);
        for y in 0..rows {
            for x in 0..columns_to_copy {
                this.children[x + y * columns] = old_children[x + y * old_columns];
            }
        }
        if old_columns > columns {
            for y in 0..rows {
                for x in columns..old_columns {
                    let xy = x + y * old_columns;
                    if let Some(old_child) = old_children[xy] {
                        self.drop_child(app, old_child.as_object());
                    }
                }
            }
        }
        self.mark_needs_layout(app);
    }

    /// The number of horizontal alignment lines in this table.
    ///
    /// Changing the number of rows will remove any children that no longer fit in the table.
    pub fn rows(self: RenderHandle<Self>, app: &App) -> usize {
        self.get(app).rows
    }

    /// Sets [`rows`](Self::rows).
    pub fn set_rows(self: RenderHandle<Self>, app: &mut App, value: usize) {
        if value == self.rows(app) {
            return;
        }
        let columns = self.columns(app);
        if self.get(app).rows > value {
            for xy in columns * value..self.get(app).children.len() {
                if let Some(child) = self.get(app).children[xy] {
                    self.drop_child(app, child.as_object());
                }
            }
        }
        let this = self.get_mut(app);
        this.rows = value;
        this.children.resize(columns * value, None);
        self.mark_needs_layout(app);
    }

    /// How the horizontal extents of the columns of this table should be determined.
    ///
    /// If the map has no entry for a given column, the table uses the
    /// [`default_column_width`](Self::default_column_width) instead.
    ///
    /// The layout performance of the table depends critically on which column sizing
    /// algorithms are used here. In particular, [`IntrinsicColumnWidth`] is quite expensive
    /// because it needs to measure each cell in the column to determine the intrinsic size of
    /// the column.
    ///
    /// This property can never return `None`. If it is set to `None`, and the existing map is
    /// not empty, then the value is replaced by an empty map. (If it is set to `None` while the
    /// current value is an empty map, the value is not changed.)
    pub fn column_widths(
        self: RenderHandle<Self>,
        app: &App,
    ) -> HashMap<usize, TableColumnWidthRef> {
        self.get(app).column_widths.clone()
    }

    /// Sets [`column_widths`](Self::column_widths).
    pub fn set_column_widths(
        self: RenderHandle<Self>,
        app: &mut App,
        value: Option<HashMap<usize, TableColumnWidthRef>>,
    ) {
        if self.get(app).column_widths.is_empty() && value.is_none() {
            return;
        }
        self.get_mut(app).column_widths = value.unwrap_or_default();
        self.mark_needs_layout(app);
    }

    /// Determines how the width of column with the given index is determined.
    pub fn set_column_width(
        self: RenderHandle<Self>,
        app: &mut App,
        column: usize,
        value: TableColumnWidthRef,
    ) {
        if self
            .get(app)
            .column_widths
            .get(&column)
            .is_some_and(|current| Rc::ptr_eq(current, &value))
        {
            return;
        }
        self.get_mut(app).column_widths.insert(column, value);
        self.mark_needs_layout(app);
    }

    /// How to determine with widths of columns that don't have an explicit sizing algorithm.
    ///
    /// Specifically, the [`default_column_width`](Self::default_column_width) is used for
    /// column `i` if `column_widths[i]` is absent.
    pub fn default_column_width(self: RenderHandle<Self>, app: &App) -> TableColumnWidthRef {
        Rc::clone(&self.get(app).default_column_width)
    }

    /// Sets [`default_column_width`](Self::default_column_width).
    pub fn set_default_column_width(
        self: RenderHandle<Self>,
        app: &mut App,
        value: TableColumnWidthRef,
    ) {
        if Rc::ptr_eq(&self.get(app).default_column_width, &value) {
            return;
        }
        self.get_mut(app).default_column_width = value;
        self.mark_needs_layout(app);
    }

    /// The direction in which the columns are ordered.
    pub fn text_direction(self: RenderHandle<Self>, app: &App) -> TextDirection {
        self.get(app).text_direction
    }

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

    /// The style to use when painting the boundary and interior divisions of the table.
    pub fn border(self: RenderHandle<Self>, app: &App) -> Option<TableBorder> {
        self.get(app).border
    }

    /// Sets [`border`](Self::border).
    pub fn set_border(self: RenderHandle<Self>, app: &mut App, value: Option<TableBorder>) {
        if self.border(app) == value {
            return;
        }
        self.get_mut(app).border = value;
        self.mark_needs_paint(app);
    }

    /// The decorations to use for each row of the table.
    ///
    /// Row decorations fill the horizontal and vertical extent of each row in the table, unlike
    /// decorations for individual cells, which might not fill either.
    pub fn row_decorations(self: RenderHandle<Self>, app: &App) -> &[Option<Box<dyn Decoration>>] {
        self.get(app).row_decorations.as_deref().unwrap_or(&[])
    }

    /// Sets [`row_decorations`](Self::row_decorations).
    pub fn set_row_decorations(
        self: RenderHandle<Self>,
        app: &mut App,
        value: Option<Vec<Option<Box<dyn Decoration>>>>,
    ) {
        let this = self.get_mut(app);
        this.row_decorations = value;
        if let Some(painters) = this.row_decoration_painters.take() {
            for mut painter in painters.into_iter().flatten() {
                painter.dispose();
            }
        }
        this.row_decoration_painters = this
            .row_decorations
            .as_ref()
            .map(|decorations| decorations.iter().map(|_| None).collect());
    }

    /// The settings to pass to the [`row_decorations`](Self::row_decorations) when painting, so
    /// that they can resolve images appropriately. See [`ImageConfiguration`].
    pub fn configuration(self: RenderHandle<Self>, app: &App) -> &ImageConfiguration {
        &self.get(app).configuration
    }

    /// Sets [`configuration`](Self::configuration).
    pub fn set_configuration(self: RenderHandle<Self>, app: &mut App, value: ImageConfiguration) {
        if value == self.get(app).configuration {
            return;
        }
        self.get_mut(app).configuration = value;
        self.mark_needs_paint(app);
    }

    /// How cells that do not explicitly specify a vertical alignment are aligned vertically.
    pub fn default_vertical_alignment(
        self: RenderHandle<Self>,
        app: &App,
    ) -> TableCellVerticalAlignment {
        self.get(app).default_vertical_alignment
    }

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

    /// The text baseline to use when aligning rows using
    /// [`TableCellVerticalAlignment::Baseline`].
    pub fn text_baseline(self: RenderHandle<Self>, app: &App) -> Option<TextBaseline> {
        self.get(app).text_baseline
    }

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

    /// Replaces the children of this table with the given cells.
    ///
    /// The cells are divided into the specified number of columns before replacing the
    /// existing children.
    ///
    /// If the new cells contain any existing children of the table, those children are moved
    /// to their new location in the table rather than removed from the table and re-added.
    pub fn set_flat_children(
        self: RenderHandle<Self>,
        app: &mut App,
        columns: usize,
        cells: Vec<Option<AnyRenderBox>>,
    ) {
        // consider the case of a newly empty table
        if columns == 0 || cells.is_empty() {
            debug_assert!(cells.is_empty());
            self.get_mut(app).columns = columns;
            if self.get(app).children.is_empty() {
                debug_assert_eq!(self.get(app).rows, 0);
                return;
            }
            let old_children = std::mem::take(&mut self.get_mut(app).children);
            for old_child in old_children.into_iter().flatten() {
                self.drop_child(app, old_child.as_object());
            }
            self.get_mut(app).rows = 0;
            self.mark_needs_layout(app);
            return;
        }
        debug_assert!(cells.len().is_multiple_of(columns));
        let (old_columns, old_rows) = (self.get(app).columns, self.get(app).rows);
        // fill a set with the cells that are moving (it's important not to drop_child a child
        // that's remaining with us, because that would clear their parent data field)
        let mut lost_children: HashSet<AnyRenderBox> = HashSet::new();
        for y in 0..old_rows {
            for x in 0..old_columns {
                let xy_old = x + y * old_columns;
                let xy_new = x + y * columns;
                if let Some(old_child) = self.get(app).children[xy_old]
                    && (x >= columns || xy_new >= cells.len() || Some(old_child) != cells[xy_new])
                {
                    lost_children.insert(old_child);
                }
            }
        }
        // adopt cells that are arriving, and cross cells that are just moving off our list of
        // lost_children
        let mut y = 0;
        while y * columns < cells.len() {
            for x in 0..columns {
                let xy_new = x + y * columns;
                let xy_old = x + y * old_columns;
                if let Some(cell) = cells[xy_new]
                    && (x >= old_columns
                        || y >= old_rows
                        || self.get(app).children[xy_old] != Some(cell))
                    && !lost_children.remove(&cell)
                {
                    self.adopt_child(app, cell.as_object());
                }
            }
            y += 1;
        }
        // drop all the lost children
        for child in lost_children {
            self.drop_child(app, child.as_object());
        }
        // update our internal values
        let this = self.get_mut(app);
        this.columns = columns;
        this.rows = cells.len() / columns;
        this.children = cells;
        debug_assert_eq!(this.children.len(), this.rows * this.columns);
        self.mark_needs_layout(app);
    }

    /// Replaces the children of this table with the given cells.
    pub fn set_children(
        self: RenderHandle<Self>,
        app: &mut App,
        cells: Option<Vec<Vec<AnyRenderBox>>>,
    ) {
        // TODO(ianh): Make this smarter, like set_flat_children
        let Some(cells) = cells else {
            self.set_flat_children(app, 0, Vec::new());
            return;
        };
        let old_children = std::mem::take(&mut self.get_mut(app).children);
        for old_child in old_children.into_iter().flatten() {
            self.drop_child(app, old_child.as_object());
        }
        let this = self.get_mut(app);
        this.columns = cells.first().map_or(0, Vec::len);
        this.rows = 0;
        for row in cells {
            self.add_row(app, row.into_iter().map(Some).collect());
        }
        debug_assert_eq!(
            self.get(app).children.len(),
            self.rows(app) * self.columns(app)
        );
    }

    /// Adds a row to the end of the table.
    ///
    /// The newly added children must not already have parents.
    pub fn add_row(self: RenderHandle<Self>, app: &mut App, cells: Vec<Option<AnyRenderBox>>) {
        debug_assert_eq!(cells.len(), self.columns(app));
        debug_assert_eq!(
            self.get(app).children.len(),
            self.rows(app) * self.columns(app)
        );
        let this = self.get_mut(app);
        this.rows += 1;
        this.children.extend(cells.iter().copied());
        for cell in cells.into_iter().flatten() {
            self.adopt_child(app, cell.as_object());
        }
        self.mark_needs_layout(app);
    }

    /// Replaces the child at the given position with the given child.
    ///
    /// If the given child is already located at the given position, this function does not
    /// modify the table. Otherwise, the given child must not already have a parent.
    pub fn set_child(
        self: RenderHandle<Self>,
        app: &mut App,
        x: usize,
        y: usize,
        value: Option<AnyRenderBox>,
    ) {
        debug_assert!(x < self.columns(app) && y < self.rows(app));
        debug_assert_eq!(
            self.get(app).children.len(),
            self.rows(app) * self.columns(app)
        );
        let xy = x + y * self.columns(app);
        let old_child = self.get(app).children[xy];
        if old_child == value {
            return;
        }
        if let Some(old_child) = old_child {
            self.drop_child(app, old_child.as_object());
        }
        self.get_mut(app).children[xy] = value;
        if let Some(value) = value {
            self.adopt_child(app, value.as_object());
        }
    }

    /// Returns the list of [`RenderBox`] objects that are in the given column, in row order,
    /// starting from the first row.
    pub fn column(self: RenderHandle<Self>, app: &App, x: usize) -> Vec<AnyRenderBox> {
        let this = self.get(app);
        (0..this.rows)
            .filter_map(|y| this.children[x + y * this.columns])
            .collect()
    }

    /// Returns the list of [`RenderBox`] objects that are on the given row, in column order,
    /// starting with the first column.
    pub fn row(self: RenderHandle<Self>, app: &App, y: usize) -> Vec<AnyRenderBox> {
        let this = self.get(app);
        let start = y * this.columns;
        let end = (y + 1) * this.columns;
        this.children[start..end]
            .iter()
            .flatten()
            .copied()
            .collect()
    }

    /// The column width the table uses for column `x`.
    fn column_width_for(self: RenderHandle<Self>, app: &App, x: usize) -> TableColumnWidthRef {
        let this = self.get(app);
        Rc::clone(
            this.column_widths
                .get(&x)
                .unwrap_or(&this.default_column_width),
        )
    }

    fn compute_column_widths(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
    ) -> Vec<f64> {
        let columns = self.columns(app);
        debug_assert_eq!(self.get(app).children.len(), self.rows(app) * columns);
        // We apply the constraints to the column widths in the order of
        // least important to most important:
        // 1. apply the ideal widths (max_intrinsic_width)
        // 2. grow the flex columns so that the table has the max_width (if
        //    finite) or the min_width (if not)
        // 3. if there were no flex columns, then grow the table to the
        //    min_width.
        // 4. apply the maximum width of the table, shrinking columns as
        //    necessary, applying minimum column widths as we go

        // 1. apply ideal widths, and collect information we'll need later
        let mut widths = vec![0.0; columns];
        let mut min_widths = vec![0.0; columns];
        let mut flexes: Vec<Option<f64>> = vec![None; columns];
        let mut table_width = 0.0; // running tally of the sum of widths[x] for all x
        let mut unflexed_table_width = 0.0; // sum of the max_intrinsic_widths of any column that has None flex
        let mut total_flex = 0.0;
        for x in 0..columns {
            let column_width = self.column_width_for(app, x);
            let column_cells = self.column(app, x);
            // apply ideal width (max_intrinsic_width)
            let max_intrinsic_width =
                column_width.max_intrinsic_width(app, &column_cells, constraints.max_width);
            debug_assert!(max_intrinsic_width.is_finite());
            debug_assert!(max_intrinsic_width >= 0.0);
            widths[x] = max_intrinsic_width;
            table_width += max_intrinsic_width;
            // collect min width information while we're at it
            let min_intrinsic_width =
                column_width.min_intrinsic_width(app, &column_cells, constraints.max_width);
            debug_assert!(min_intrinsic_width.is_finite());
            debug_assert!(min_intrinsic_width >= 0.0);
            min_widths[x] = min_intrinsic_width;
            debug_assert!(max_intrinsic_width >= min_intrinsic_width);
            // collect flex information while we're at it
            let flex = column_width.flex(app, &column_cells);
            if let Some(flex) = flex {
                debug_assert!(flex.is_finite());
                debug_assert!(flex > 0.0);
                flexes[x] = Some(flex);
                total_flex += flex;
            } else {
                unflexed_table_width += max_intrinsic_width;
            }
        }
        let max_width_constraint = constraints.max_width;
        let min_width_constraint = constraints.min_width;

        // 2. grow the flex columns so that the table has the max_width (if
        //    finite) or the min_width (if not)
        if total_flex > 0.0 {
            // this can only grow the table, but it _will_ grow the table at
            // least as big as the target width.
            let target_width = if max_width_constraint.is_finite() {
                max_width_constraint
            } else {
                min_width_constraint
            };
            if table_width < target_width {
                let remaining_width = target_width - unflexed_table_width;
                debug_assert!(remaining_width.is_finite());
                debug_assert!(remaining_width >= 0.0);
                for x in 0..columns {
                    if let Some(flex) = flexes[x] {
                        let flexed_width = remaining_width * flex / total_flex;
                        debug_assert!(flexed_width.is_finite());
                        debug_assert!(flexed_width >= 0.0);
                        if widths[x] < flexed_width {
                            let delta = flexed_width - widths[x];
                            table_width += delta;
                            widths[x] = flexed_width;
                        }
                    }
                }
                debug_assert!(table_width + PRECISION_ERROR_TOLERANCE >= target_width);
            }
        }
        // step 2 and 3 are mutually exclusive
        // 3. if there were no flex columns, then grow the table to the
        //    min_width.
        else if table_width < min_width_constraint {
            let delta = (min_width_constraint - table_width) / columns as f64;
            for width in &mut widths {
                *width += delta;
            }
            table_width = min_width_constraint;
        }

        // beyond this point, unflexed_table_width is no longer valid

        // 4. apply the maximum width of the table, shrinking columns as
        //    necessary, applying minimum column widths as we go
        if table_width > max_width_constraint {
            let mut deficit = table_width - max_width_constraint;
            // Some columns may have low flex but have all the free space.
            // (Consider a case with a 1px wide column of flex 1000.0 and
            // a 1000px wide column of flex 1.0; the sizes coming from the
            // max_intrinsic_widths. If the maximum table width is 2px, then
            // just applying the flexes to the deficit would result in a
            // table with one column at -998px and one column at 990px,
            // which is wildly unhelpful.)
            // Similarly, some columns may be flexible, but not actually
            // be shrinkable due to a large minimum width. (Consider a
            // case with two columns, one is flex and one isn't, both have
            // 1000px max_intrinsic_widths, but the flex one has 1000px
            // min_intrinsic_width also. The whole deficit will have to come
            // from the non-flex column.)
            // So what we do is we repeatedly iterate through the flexible
            // columns shrinking them proportionally until we have no
            // available columns, then do the same to the non-flexible ones.
            let mut available_columns = columns;
            while deficit > PRECISION_ERROR_TOLERANCE && total_flex > PRECISION_ERROR_TOLERANCE {
                let mut new_total_flex = 0.0;
                for x in 0..columns {
                    if let Some(flex) = flexes[x] {
                        let new_width = widths[x] - deficit * flex / total_flex;
                        debug_assert!(new_width.is_finite());
                        if new_width <= min_widths[x] {
                            // shrank to minimum
                            deficit -= widths[x] - min_widths[x];
                            widths[x] = min_widths[x];
                            flexes[x] = None;
                            available_columns -= 1;
                        } else {
                            deficit -= widths[x] - new_width;
                            widths[x] = new_width;
                            new_total_flex += flex;
                        }
                        debug_assert!(widths[x] >= 0.0);
                    }
                }
                total_flex = new_total_flex;
            }
            while deficit > PRECISION_ERROR_TOLERANCE && available_columns > 0 {
                // Now we have to take out the remaining space from the
                // columns that aren't minimum sized.
                // To make this fair, we repeatedly remove equal amounts from
                // each column, clamped to the minimum width, until we run out
                // of columns that aren't at their min_width.
                let delta = deficit / available_columns as f64;
                debug_assert!(delta != 0.0);
                let mut new_available_columns = 0;
                for x in 0..columns {
                    let available_delta = widths[x] - min_widths[x];
                    if available_delta > 0.0 {
                        if available_delta <= delta {
                            // shrank to minimum
                            deficit -= widths[x] - min_widths[x];
                            widths[x] = min_widths[x];
                        } else {
                            deficit -= delta;
                            widths[x] -= delta;
                            new_available_columns += 1;
                        }
                    }
                }
                available_columns = new_available_columns;
            }
        }
        widths
    }

    /// Returns the position and dimensions of the box that the given row covers, in this render
    /// object's coordinate space (so the left coordinate is always 0.0).
    ///
    /// The row being queried must exist.
    ///
    /// This is only valid after layout.
    pub fn get_row_box(self: RenderHandle<Self>, app: &App, row: usize) -> Rect {
        debug_assert!(row < self.rows(app));
        debug_assert!(!self.debug_needs_layout(app));
        let row_tops = &self.get(app).row_tops;
        Rect::from_ltrb(
            0.0,
            row_tops[row],
            self.size(app).width(),
            row_tops[row + 1],
        )
    }

    /// The vertical alignment of `child`: its own, else the table's default.
    fn vertical_alignment_of(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderBox,
    ) -> TableCellVerticalAlignment {
        child
            .as_object()
            .parent_data_of::<TableCellParentData>(app)
            .vertical_alignment
            .unwrap_or(self.get(app).default_vertical_alignment)
    }

    fn set_child_offset(app: &mut App, child: AnyRenderBox, offset: Offset) {
        child.parent_data_of_mut::<BoxParentData>(app).offset = offset;
    }
}

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

    fn did_detach(self: RenderHandle<Self>, app: &mut App) {
        let this = self.get_mut(app);
        if let Some(painters) = this.row_decoration_painters.take() {
            for mut painter in painters.into_iter().flatten() {
                painter.dispose();
            }
            let decorations = this.row_decorations.as_ref().map_or(0, Vec::len);
            this.row_decoration_painters = Some((0..decorations).map(|_| None).collect());
        }
        let children = self.get(app).children.clone();
        for child in children.into_iter().flatten() {
            child.as_object().detach(app);
        }
    }

    fn visit_children(
        self: RenderHandle<Self>,
        app: &App,
        visitor: &mut dyn FnMut(AnyRenderObject),
    ) {
        let this = self.get(app);
        debug_assert_eq!(this.children.len(), this.rows * this.columns);
        for child in this.children.iter().flatten() {
            visitor(child.as_object());
        }
    }

    fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
        let constraints = self.constraints(app);
        let rows = self.rows(app);
        let columns = self.columns(app);
        debug_assert_eq!(self.get(app).children.len(), rows * columns);
        if rows * columns == 0 {
            // TODO(ianh): if columns is zero, this should be zero width
            // TODO(ianh): if columns is not zero, this should be based on the column width
            // specifications
            self.get_mut(app).table_width = 0.0;
            self.set_size(app, constraints.constrain(Size::ZERO));
            return;
        }
        let widths = self.compute_column_widths(app, constraints);
        let mut positions = vec![0.0; columns];
        let (column_lefts, table_width) = match self.text_direction(app) {
            TextDirection::Rtl => {
                positions[columns - 1] = 0.0;
                for x in (0..columns - 1).rev() {
                    positions[x] = positions[x + 1] + widths[x + 1];
                }
                let table_width = positions[0] + widths[0];
                (positions.iter().rev().copied().collect(), table_width)
            }
            TextDirection::Ltr => {
                positions[0] = 0.0;
                for x in 1..columns {
                    positions[x] = positions[x - 1] + widths[x - 1];
                }
                let table_width = positions[columns - 1] + widths[columns - 1];
                (positions.clone(), table_width)
            }
        };
        let this = self.get_mut(app);
        this.column_lefts = Some(column_lefts);
        this.table_width = table_width;
        this.row_tops.clear();
        this.baseline_distance = None;
        // then, lay out each row
        let mut row_top = 0.0;
        for y in 0..rows {
            self.get_mut(app).row_tops.push(row_top);
            let mut row_height: f64 = 0.0;
            let mut have_baseline = false;
            let mut before_baseline_distance: f64 = 0.0;
            let mut after_baseline_distance: f64 = 0.0;
            let mut baselines = vec![0.0; columns];
            for x in 0..columns {
                let xy = x + y * columns;
                let Some(child) = self.get(app).children[xy] else {
                    continue;
                };
                let child_parent_data = child
                    .as_object()
                    .parent_data_of_mut::<TableCellParentData>(app);
                child_parent_data.x = Some(x);
                child_parent_data.y = Some(y);
                match self.vertical_alignment_of(app, child) {
                    TableCellVerticalAlignment::Baseline => {
                        let text_baseline = self.text_baseline(app).expect(
                            "An explicit textBaseline is required when using baseline alignment.",
                        );
                        child.layout(app, BoxConstraints::tight_for(Some(widths[x]), None), true);
                        let child_baseline =
                            child.get_distance_to_baseline(app, text_baseline, true);
                        if let Some(child_baseline) = child_baseline {
                            before_baseline_distance = before_baseline_distance.max(child_baseline);
                            after_baseline_distance = after_baseline_distance
                                .max(child.size(app).height() - child_baseline);
                            baselines[x] = child_baseline;
                            have_baseline = true;
                        } else {
                            row_height = row_height.max(child.size(app).height());
                            RenderTable::set_child_offset(
                                app,
                                child,
                                Offset::new(positions[x], row_top),
                            );
                        }
                    }
                    TableCellVerticalAlignment::Top
                    | TableCellVerticalAlignment::Middle
                    | TableCellVerticalAlignment::Bottom
                    | TableCellVerticalAlignment::IntrinsicHeight => {
                        child.layout(app, BoxConstraints::tight_for(Some(widths[x]), None), true);
                        row_height = row_height.max(child.size(app).height());
                    }
                    TableCellVerticalAlignment::Fill => {}
                }
            }
            if have_baseline {
                if y == 0 {
                    self.get_mut(app).baseline_distance = Some(before_baseline_distance);
                }
                row_height = row_height.max(before_baseline_distance + after_baseline_distance);
            }
            for x in 0..columns {
                let xy = x + y * columns;
                let Some(child) = self.get(app).children[xy] else {
                    continue;
                };
                match self.vertical_alignment_of(app, child) {
                    TableCellVerticalAlignment::Baseline => {
                        RenderTable::set_child_offset(
                            app,
                            child,
                            Offset::new(
                                positions[x],
                                row_top + before_baseline_distance - baselines[x],
                            ),
                        );
                    }
                    TableCellVerticalAlignment::Top => {
                        RenderTable::set_child_offset(
                            app,
                            child,
                            Offset::new(positions[x], row_top),
                        );
                    }
                    TableCellVerticalAlignment::Middle => {
                        let offset = Offset::new(
                            positions[x],
                            row_top + (row_height - child.size(app).height()) / 2.0,
                        );
                        RenderTable::set_child_offset(app, child, offset);
                    }
                    TableCellVerticalAlignment::Bottom => {
                        let offset = Offset::new(
                            positions[x],
                            row_top + row_height - child.size(app).height(),
                        );
                        RenderTable::set_child_offset(app, child, offset);
                    }
                    TableCellVerticalAlignment::Fill
                    | TableCellVerticalAlignment::IntrinsicHeight => {
                        child.layout(
                            app,
                            BoxConstraints::tight_for(Some(widths[x]), Some(row_height)),
                            false,
                        );
                        RenderTable::set_child_offset(
                            app,
                            child,
                            Offset::new(positions[x], row_top),
                        );
                    }
                }
            }
            row_top += row_height;
        }
        self.get_mut(app).row_tops.push(row_top);
        let table_width = self.get(app).table_width;
        self.set_size(app, constraints.constrain(Size::new(table_width, row_top)));
        debug_assert_eq!(self.get(app).row_tops.len(), rows + 1);
    }

    fn paint(
        self: RenderHandle<Self>,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
    ) {
        let rows = self.rows(app);
        let columns = self.columns(app);
        debug_assert_eq!(self.get(app).children.len(), rows * columns);
        if rows * columns == 0 {
            if let Some(border) = self.border(app) {
                let border_rect =
                    Rect::from_ltwh(offset.dx(), offset.dy(), self.get(app).table_width, 0.0);
                border.paint(context.canvas(), border_rect, &[], &[]);
            }
            return;
        }
        debug_assert_eq!(self.get(app).row_tops.len(), rows + 1);
        if self.get(app).row_decorations.is_some() {
            let size = self.size(app);
            let this = self.get_mut(app);
            let decorations = this.row_decorations.as_ref().expect("checked above");
            let painters = this
                .row_decoration_painters
                .as_mut()
                .expect("in sync with the decorations");
            debug_assert_eq!(decorations.len(), painters.len());
            let canvas = context.canvas();
            for y in 0..rows {
                if decorations.len() <= y {
                    break;
                }
                if let Some(decoration) = &decorations[y] {
                    let painter = painters[y].get_or_insert_with(|| {
                        decoration.create_box_painter(Some(Rc::new(move |app| {
                            self.mark_needs_paint(app)
                        })))
                    });
                    let row_size = Size::new(size.width(), this.row_tops[y + 1] - this.row_tops[y]);
                    painter.paint(
                        canvas,
                        Offset::new(offset.dx(), offset.dy() + this.row_tops[y]),
                        &this
                            .configuration
                            .copy_with(None, None, None, None, Some(row_size), None),
                    );
                }
            }
        }
        let children = self.get(app).children.clone();
        for child in children.into_iter().flatten() {
            let child_offset = child.box_parent_data(app).offset;
            context.paint_child(app, child.as_object(), child_offset + offset);
        }
        debug_assert_eq!(rows, self.get(app).row_tops.len() - 1);
        debug_assert_eq!(
            Some(columns),
            self.get(app).column_lefts.as_ref().map(Vec::len)
        );
        if let Some(border) = self.border(app) {
            // The border rect might not fill the entire height of this render object
            // if the rows underflow. We always force the columns to fill the width of
            // the render object, which means the columns cannot underflow.
            let this = self.get(app);
            let row_tops = &this.row_tops;
            let border_rect = Rect::from_ltwh(
                offset.dx(),
                offset.dy(),
                this.table_width,
                row_tops[row_tops.len() - 1],
            );
            let rows = row_tops[1..row_tops.len() - 1].to_vec();
            let columns = this.column_lefts.as_ref().expect("laid out")[1..].to_vec();
            border.paint(context.canvas(), border_rect, &rows, &columns);
        }
    }
}

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

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

    fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, _height: f64) -> f64 {
        let (rows, columns) = (self.rows(app), self.columns(app));
        debug_assert_eq!(self.get(app).children.len(), rows * columns);
        if rows * columns == 0 {
            return 0.0;
        }
        let mut total_min_width = 0.0;
        for x in 0..columns {
            let column_width = self.column_width_for(app, x);
            let column_cells = self.column(app, x);
            total_min_width += column_width.min_intrinsic_width(app, &column_cells, f64::INFINITY);
        }
        total_min_width
    }

    fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, _height: f64) -> f64 {
        let (rows, columns) = (self.rows(app), self.columns(app));
        debug_assert_eq!(self.get(app).children.len(), rows * columns);
        if rows * columns == 0 {
            return 0.0;
        }
        let mut total_max_width = 0.0;
        for x in 0..columns {
            let column_width = self.column_width_for(app, x);
            let column_cells = self.column(app, x);
            total_max_width += column_width.max_intrinsic_width(app, &column_cells, f64::INFINITY);
        }
        total_max_width
    }

    fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        // winner of the 2016 world's most expensive intrinsic dimension function award
        // honorable mention, most likely to improve if taught about memoization award
        let (rows, columns) = (self.rows(app), self.columns(app));
        debug_assert_eq!(self.get(app).children.len(), rows * columns);
        if rows * columns == 0 {
            return 0.0;
        }
        let widths =
            self.compute_column_widths(app, BoxConstraints::tight_for_finite(width, f64::INFINITY));
        let mut row_top = 0.0;
        for y in 0..rows {
            let mut row_height: f64 = 0.0;
            for (x, &width) in widths.iter().enumerate() {
                let xy = x + y * columns;
                if let Some(child) = self.get(app).children[xy] {
                    row_height = row_height.max(child.get_max_intrinsic_height(app, width));
                }
            }
            row_top += row_height;
        }
        row_top
    }

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

    /// Returns the baseline offset of the cell in the first row with the lowest baseline, and
    /// uses [`TableCellVerticalAlignment::Baseline`].
    fn compute_distance_to_actual_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        _baseline: TextBaseline,
    ) -> Option<f64> {
        debug_assert!(!self.debug_needs_layout(app));
        self.get(app).baseline_distance
    }

    fn compute_dry_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
        baseline: TextBaseline,
    ) -> Option<f64> {
        let columns = self.columns(app);
        if self.rows(app) * columns == 0 {
            return None;
        }
        let widths = self.compute_column_widths(app, constraints);
        let mut baseline_offset: Option<f64> = None;
        for (col, &width) in widths.iter().enumerate() {
            let Some(child) = self.get(app).children[col] else {
                continue;
            };
            let child_constraints = BoxConstraints::tight_for(Some(width), None);
            let child_baseline = match self.vertical_alignment_of(app, child) {
                TableCellVerticalAlignment::Baseline => {
                    child.get_dry_baseline(app, child_constraints, baseline)
                }
                TableCellVerticalAlignment::Top
                | TableCellVerticalAlignment::Middle
                | TableCellVerticalAlignment::Bottom
                | TableCellVerticalAlignment::Fill
                | TableCellVerticalAlignment::IntrinsicHeight => None,
            };
            if let Some(child_baseline) = child_baseline
                && baseline_offset.is_none_or(|offset| offset < child_baseline)
            {
                baseline_offset = Some(child_baseline);
            }
        }
        baseline_offset
    }

    fn compute_dry_layout(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
    ) -> Size {
        let (rows, columns) = (self.rows(app), self.columns(app));
        if rows * columns == 0 {
            return constraints.constrain(Size::ZERO);
        }
        let widths = self.compute_column_widths(app, constraints);
        let table_width: f64 = widths.iter().sum();
        let mut row_top = 0.0;
        for y in 0..rows {
            let mut row_height: f64 = 0.0;
            for (x, &width) in widths.iter().enumerate() {
                let xy = x + y * columns;
                let Some(child) = self.get(app).children[xy] else {
                    continue;
                };
                match self.vertical_alignment_of(app, child) {
                    TableCellVerticalAlignment::Baseline => {
                        self.debug_cannot_compute_dry_layout(DryLayoutFailure::Reason(
                            "TableCellVerticalAlignment.baseline requires a full layout for \
                             baseline metrics to be available.",
                        ));
                        return Size::ZERO;
                    }
                    TableCellVerticalAlignment::Top
                    | TableCellVerticalAlignment::Middle
                    | TableCellVerticalAlignment::Bottom
                    | TableCellVerticalAlignment::IntrinsicHeight => {
                        let child_size =
                            child.get_dry_layout(app, BoxConstraints::tight_for(Some(width), None));
                        row_height = row_height.max(child_size.height());
                    }
                    TableCellVerticalAlignment::Fill => {}
                }
            }
            row_top += row_height;
        }
        constraints.constrain(Size::new(table_width, row_top))
    }

    fn hit_test_children(
        self: RenderHandle<Self>,
        app: &mut App,
        result: &mut BoxHitTestResult<'_>,
        position: Offset,
    ) -> bool {
        debug_assert_eq!(
            self.get(app).children.len(),
            self.rows(app) * self.columns(app)
        );
        let children = self.get(app).children.clone();
        for child in children.into_iter().rev().flatten() {
            let child_offset = child.box_parent_data(app).offset;
            let is_hit = result.add_with_paint_offset(
                Some(child_offset),
                position,
                |result, transformed| {
                    debug_assert_eq!(transformed, position - child_offset);
                    child.hit_test(app, result, transformed)
                },
            );
            if is_hit {
                return true;
            }
        }
        false
    }
}

#[cfg(test)]
mod tests {
    use inset_embedder::valo::Op;
    use inset_embedder::{Color, Radius};
    use inset_foundation::{AppCell, Handle};
    use inset_painting::{AlignmentGeometry, BorderRadius, BorderStyle};

    use super::*;
    use crate::layer::{ContainerLayer, ErasedLayer, OffsetLayer, PictureLayer};
    use crate::pipeline_owner::PipelineOwner;
    use crate::proxy_box::{RenderConstrainedBox, RenderRepaintBoundary};
    use crate::shifted_box::RenderPositionedBox;

    fn sized_box(app: &mut App, width: f64, height: f64) -> AnyRenderBox {
        RenderConstrainedBox::new(app, BoxConstraints::tight(Size::new(width, height)), None)
            .as_box()
    }

    fn positioned_box(app: &mut App) -> AnyRenderBox {
        RenderPositionedBox::new(app, AlignmentGeometry::CENTER, None, None, None, None).as_box()
    }

    /// `rendering_tester.dart`'s `layout`: attach `root` under a repaint boundary, lay it out
    /// with `constraints`, and paint it. The default constraints are an 800 by 600 screen.
    struct Laid {
        owner: Handle<PipelineOwner>,
        boundary: RenderHandle<RenderRepaintBoundary>,
    }

    fn layout(app: &mut App, root: AnyRenderBox, constraints: BoxConstraints) -> Laid {
        let boundary = RenderRepaintBoundary::new(app, Some(root));
        let owner = PipelineOwner::new(app, None, None);
        owner.set_root_node(app, Some(boundary.as_object()));
        boundary.as_box().layout(app, constraints, false);
        let paint_root = OffsetLayer::new(app, Offset::ZERO);
        paint_root.as_layer().attach(app, boundary.as_object().id());
        boundary
            .as_object()
            .schedule_initial_paint(app, paint_root.as_container_layer());
        pump_frame(app, owner);
        Laid { owner, boundary }
    }

    fn screen() -> BoxConstraints {
        BoxConstraints::tight(Size::new(800.0, 600.0))
    }

    /// `rendering_tester.dart`'s `pumpFrame`: layout, compositing bits and paint.
    fn pump_frame(app: &mut App, owner: Handle<PipelineOwner>) {
        owner.flush_layout(app);
        owner.flush_compositing_bits(app);
        owner.flush_paint(app);
    }

    /// The display list ops the boundary painted, in order.
    fn painted(app: &App, laid: &Laid) -> Vec<Op> {
        let layer = laid.boundary.as_object().debug_layer(app).expect("painted");
        layer
            .depth_first_iterate_children(app)
            .into_iter()
            .filter_map(|child| {
                app.handle::<PictureLayer>(child.id())
                    .and_then(|picture| picture.picture(app).map(|p| p.ops().to_vec()))
            })
            .flatten()
            .collect()
    }

    fn paths_painted(app: &App, laid: &Laid) -> usize {
        painted(app, laid)
            .iter()
            .filter(|op| matches!(op, Op::DrawPath { .. }))
            .count()
    }

    /// `table_test.dart`: "Table control test; tight".
    #[test]
    fn an_empty_table_fills_tight_constraints() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let table = RenderTable::new(&mut app, TextDirection::Ltr);
        layout(&mut app, table.as_box(), screen());
        assert_eq!(table.size(&app), Size::new(800.0, 600.0));
    }

    /// `table_test.dart`: "Table control test; loose".
    #[test]
    fn an_empty_table_is_empty_in_loose_constraints() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let table = RenderTable::new(&mut app, TextDirection::Ltr);
        let center = RenderPositionedBox::new(
            &mut app,
            AlignmentGeometry::CENTER,
            None,
            None,
            None,
            Some(table.as_box()),
        );
        layout(&mut app, center.as_box(), screen());
        assert_eq!(table.size(&app), Size::ZERO);
    }

    /// `table_test.dart`: "Table control test: constrained flex columns".
    #[test]
    fn flex_columns_share_the_width() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let table = RenderTable::new(&mut app, TextDirection::Ltr);
        let children: Vec<AnyRenderBox> = (0..6).map(|_| positioned_box(&mut app)).collect();
        table.set_flat_children(&mut app, 6, children.iter().copied().map(Some).collect());
        layout(
            &mut app,
            table.as_box(),
            BoxConstraints::tight_for(Some(100.0), None),
        );
        let expected_width = 100.0 / 6.0;
        for child in children {
            assert!((child.size(&app).width() - expected_width).abs() < 1e-10);
        }
    }

    /// `table_test.dart`: "Table test: combinations". Boxes have no baseline, so a baseline
    /// row top-aligns them.
    #[test]
    fn intrinsic_columns_fit_their_widest_cell() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let table = RenderTable::new(&mut app, TextDirection::Ltr);
        table.set_columns(&mut app, 5);
        table.set_rows(&mut app, 5);
        table.set_default_column_width(&mut app, Rc::new(IntrinsicColumnWidth::new(None)));
        table.set_default_vertical_alignment(&mut app, TableCellVerticalAlignment::Baseline);
        table.set_text_baseline(&mut app, Some(TextBaseline::Alphabetic));
        let center = RenderPositionedBox::new(
            &mut app,
            AlignmentGeometry::CENTER,
            None,
            None,
            None,
            Some(table.as_box()),
        );
        let laid = layout(&mut app, center.as_box(), screen());
        assert_eq!(table.size(&app), Size::ZERO);

        let tall = sized_box(&mut app, 100.0, 200.0);
        table.set_child(&mut app, 2, 4, Some(tall));
        pump_frame(&mut app, laid.owner);
        assert_eq!(table.size(&app), Size::new(100.0, 200.0));

        let first = sized_box(&mut app, 10.0, 30.0);
        let second = sized_box(&mut app, 20.0, 20.0);
        let third = sized_box(&mut app, 30.0, 10.0);
        table.set_child(&mut app, 0, 0, Some(first));
        table.set_child(&mut app, 1, 0, Some(second));
        table.set_child(&mut app, 2, 0, Some(third));
        pump_frame(&mut app, laid.owner);
        assert_eq!(table.size(&app), Size::new(130.0, 230.0));
        assert_eq!(
            table.get(&app).column_lefts.as_deref(),
            Some(&[0.0, 10.0, 30.0, 130.0, 130.0][..])
        );
        assert_eq!(
            table.get(&app).row_tops,
            [0.0, 30.0, 30.0, 30.0, 30.0, 230.0]
        );
        assert_eq!(third.size(&app), Size::new(100.0, 10.0));
        assert_eq!(tall.box_parent_data(&app).offset, Offset::new(30.0, 30.0));
        let parent_data = third
            .as_object()
            .parent_data_of::<TableCellParentData>(&app);
        assert_eq!((parent_data.x, parent_data.y), (Some(2), Some(0)));
    }

    /// `table_test.dart`: "Table test: removing cells".
    #[test]
    fn fewer_rows_drop_the_cells_below() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let table = RenderTable::new(&mut app, TextDirection::Ltr);
        table.set_columns(&mut app, 5);
        table.set_rows(&mut app, 5);
        let child = sized_box(&mut app, 10.0, 10.0);
        table.set_child(&mut app, 4, 4, Some(child));
        layout(&mut app, table.as_box(), screen());
        assert!(child.as_object().attached(&app));
        table.set_rows(&mut app, 4);
        assert!(!child.as_object().attached(&app));
    }

    /// `table_test.dart`: "Table test: replacing cells". A cell that moves keeps its parent.
    #[test]
    fn moving_cells_keeps_them_in_the_table() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let table = RenderTable::new(&mut app, TextDirection::Ltr);
        let moving: Vec<AnyRenderBox> = (0..3).map(|_| positioned_box(&mut app)).collect();
        let mut cells = Vec::new();
        for child in &moving {
            cells.push(Some(*child));
            cells.push(Some(positioned_box(&mut app)));
        }
        table.set_flat_children(&mut app, 3, cells);
        assert_eq!(table.rows(&app), 2);
        let laid = layout(&mut app, table.as_box(), screen());
        for _ in 0..2 {
            let mut cells = Vec::new();
            for child in &moving {
                cells.push(Some(positioned_box(&mut app)));
                cells.push(Some(*child));
            }
            table.set_flat_children(&mut app, 3, cells);
            pump_frame(&mut app, laid.owner);
        }
        assert_eq!(table.columns(&app), 3);
        assert_eq!(table.rows(&app), 2);
        for child in moving {
            assert_eq!(child.as_object().parent(&app), Some(table.as_object()));
        }
    }

    /// `table_test.dart`: "Table border painting": the inner lines are one path per direction,
    /// after which the outer border paints its four sides.
    #[test]
    fn a_border_paints_its_inner_lines_and_four_sides() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let table = RenderTable::new(&mut app, TextDirection::Rtl);
        table.set_border(
            &mut app,
            Some(TableBorder::all(
                Color::new(0xFF000000),
                1.0,
                BorderStyle::Solid,
                BorderRadius::ZERO,
            )),
        );
        let laid = layout(&mut app, table.as_box(), screen());
        for (columns, cells, paths) in [
            (1, 0, 4),
            (1, 1, 4),
            (1, 2, 5),
            (2, 2, 5),
            (2, 4, 6),
            (3, 6, 6),
        ] {
            let children = (0..cells).map(|_| Some(positioned_box(&mut app))).collect();
            table.set_flat_children(&mut app, columns, children);
            pump_frame(&mut app, laid.owner);
            assert_eq!(
                paths_painted(&app, &laid),
                paths,
                "{columns} columns, {cells} cells"
            );
        }
    }

    /// `table_test.dart`: "Table paints a borderRadius": a uniform rounded border is one ring
    /// after the two inner lines.
    #[test]
    fn a_rounded_uniform_border_paints_one_ring() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let table = RenderTable::new(&mut app, TextDirection::Ltr);
        table.set_border(
            &mut app,
            Some(TableBorder::all(
                Color::new(0xFF000000),
                1.0,
                BorderStyle::Solid,
                BorderRadius::all(Radius::circular(8.0)),
            )),
        );
        let laid = layout(&mut app, table.as_box(), screen());
        let children = (0..4).map(|_| Some(positioned_box(&mut app))).collect();
        table.set_flat_children(&mut app, 2, children);
        pump_frame(&mut app, laid.owner);
        assert_eq!(paths_painted(&app, &laid), 3);
    }

    /// Row decorations paint behind each row, filling it.
    #[test]
    fn row_decorations_paint_behind_their_rows() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let table = RenderTable::new(&mut app, TextDirection::Ltr);
        let rows = vec![
            vec![sized_box(&mut app, 10.0, 10.0)],
            vec![sized_box(&mut app, 10.0, 20.0)],
        ];
        table.set_children(&mut app, Some(rows));
        table.set_row_decorations(
            &mut app,
            Some(vec![
                None,
                Some(Box::new(
                    inset_painting::BoxDecoration::new().color(Color::new(0xFF0000FF)),
                )),
            ]),
        );
        let laid = layout(
            &mut app,
            table.as_box(),
            BoxConstraints::tight_for(Some(100.0), None),
        );
        let rects: Vec<_> = painted(&app, &laid)
            .into_iter()
            .filter_map(|op| match op {
                Op::DrawRect { rect, .. } => Some(rect),
                _ => None,
            })
            .collect();
        assert_eq!(rects.len(), 1, "only the second row is decorated");
        assert_eq!(
            table.get_row_box(&app, 1),
            Rect::from_ltrb(0.0, 10.0, 100.0, 30.0)
        );
    }

    /// `table_test.dart`: "MaxColumnWidth.flex returns the correct result" and
    /// "MinColumnWidth.flex returns the correct result".
    #[test]
    fn max_and_min_widths_take_the_flex_of_either() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let fixed: TableColumnWidthRef = Rc::new(FixedColumnWidth::new(100.0));
        let flex: TableColumnWidthRef = Rc::new(FlexColumnWidth::new(1.0));
        for width in [
            Rc::new(MaxColumnWidth::new(Rc::clone(&fixed), Rc::clone(&flex)))
                as TableColumnWidthRef,
            Rc::new(MaxColumnWidth::new(Rc::clone(&flex), Rc::clone(&fixed))),
            Rc::new(MinColumnWidth::new(Rc::clone(&fixed), Rc::clone(&flex))),
            Rc::new(MinColumnWidth::new(Rc::clone(&flex), Rc::clone(&fixed))),
        ] {
            assert_eq!(width.flex(&mut app, &[]), Some(1.0), "{width:?}");
        }
    }

    /// Fixed and fractional columns take their width; a flexible one takes the rest.
    #[test]
    fn fixed_fraction_and_flex_columns_divide_the_width() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let table = RenderTable::new(&mut app, TextDirection::Ltr);
        let children: Vec<AnyRenderBox> = (0..3).map(|_| positioned_box(&mut app)).collect();
        table.set_flat_children(&mut app, 3, children.iter().copied().map(Some).collect());
        let mut widths: HashMap<usize, TableColumnWidthRef> = HashMap::new();
        widths.insert(0, Rc::new(FixedColumnWidth::new(50.0)));
        widths.insert(1, Rc::new(FractionColumnWidth::new(0.25)));
        table.set_column_widths(&mut app, Some(widths));
        layout(
            &mut app,
            table.as_box(),
            BoxConstraints::tight_for(Some(400.0), None),
        );
        let widths: Vec<f64> = children
            .iter()
            .map(|child| child.size(&app).width())
            .collect();
        assert_eq!(widths, [50.0, 100.0, 250.0]);
    }

    /// Too little room shrinks no column below its minimum: the flexible column stops at its
    /// fixed lower bound, and the table is cut to its constraints.
    #[test]
    fn no_column_shrinks_below_its_minimum() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let table = RenderTable::new(&mut app, TextDirection::Ltr);
        let children: Vec<AnyRenderBox> = (0..2).map(|_| positioned_box(&mut app)).collect();
        table.set_flat_children(&mut app, 2, children.iter().copied().map(Some).collect());
        let mut widths: HashMap<usize, TableColumnWidthRef> = HashMap::new();
        widths.insert(0, Rc::new(FixedColumnWidth::new(150.0)));
        widths.insert(
            1,
            Rc::new(MaxColumnWidth::new(
                Rc::new(FixedColumnWidth::new(80.0)),
                Rc::new(FlexColumnWidth::new(1.0)),
            )),
        );
        table.set_column_widths(&mut app, Some(widths));
        layout(
            &mut app,
            table.as_box(),
            BoxConstraints::tight_for(Some(200.0), None),
        );
        let widths: Vec<f64> = children
            .iter()
            .map(|child| child.size(&app).width())
            .collect();
        assert_eq!(widths, [150.0, 80.0]);
        assert_eq!(table.size(&app).width(), 200.0);
    }

    /// `table_test.dart`: "TableRows with different constraints, but vertically with
    /// intrinsicHeight": every cell takes the height of the tallest.
    #[test]
    fn intrinsic_height_cells_match_the_tallest() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let short = RenderConstrainedBox::new(
            &mut app,
            BoxConstraints::tight_for(Some(100.0), Some(100.0)),
            None,
        );
        let tall = RenderConstrainedBox::new(
            &mut app,
            BoxConstraints::tight_for(Some(200.0), Some(200.0)),
            None,
        );
        let table = RenderTable::new(&mut app, TextDirection::Rtl);
        table.set_default_vertical_alignment(&mut app, TableCellVerticalAlignment::IntrinsicHeight);
        table.set_children(&mut app, Some(vec![vec![short.as_box(), tall.as_box()]]));
        layout(
            &mut app,
            table.as_box(),
            BoxConstraints::tight(Size::new(300.0, 300.0)),
        );
        assert_eq!(table.size(&app), Size::new(300.0, 300.0));
        assert_eq!(short.size(&app).height(), tall.size(&app).height());
    }

    /// `table_test.dart`: "Empty table intrinsic dimensions should not crash".
    #[test]
    fn an_empty_table_has_no_intrinsic_size() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let table = RenderTable::new(&mut app, TextDirection::Ltr).as_box();
        for extent in [100.0, f64::INFINITY] {
            assert_eq!(table.get_min_intrinsic_width(&mut app, extent), 0.0);
            assert_eq!(table.get_max_intrinsic_width(&mut app, extent), 0.0);
            assert_eq!(table.get_min_intrinsic_height(&mut app, extent), 0.0);
            assert_eq!(table.get_max_intrinsic_height(&mut app, extent), 0.0);
        }
    }

    /// The intrinsic widths sum the columns, and the intrinsic height at a width sums the
    /// tallest cell of each row; the dry layout matches the layout.
    #[test]
    fn intrinsics_and_dry_layout_follow_the_cells() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let table = RenderTable::new(&mut app, TextDirection::Ltr);
        table.set_default_column_width(&mut app, Rc::new(IntrinsicColumnWidth::new(None)));
        let rows = vec![
            vec![
                sized_box(&mut app, 10.0, 30.0),
                sized_box(&mut app, 40.0, 5.0),
            ],
            vec![
                sized_box(&mut app, 20.0, 10.0),
                sized_box(&mut app, 5.0, 15.0),
            ],
        ];
        table.set_children(&mut app, Some(rows));
        let table = table.as_box();
        assert_eq!(table.get_min_intrinsic_width(&mut app, f64::INFINITY), 60.0);
        assert_eq!(table.get_max_intrinsic_width(&mut app, f64::INFINITY), 60.0);
        assert_eq!(table.get_min_intrinsic_height(&mut app, 60.0), 45.0);
        assert_eq!(table.get_max_intrinsic_height(&mut app, 60.0), 45.0);
        let loose = BoxConstraints::new().max_width(500.0).max_height(500.0);
        let dry = table.get_dry_layout(&mut app, loose);
        layout(&mut app, table, loose);
        assert_eq!(dry, Size::new(60.0, 45.0));
        assert_eq!(table.size(&app), dry);
    }

    /// What the shell does at start-up: the app-wide fonts, here the OS fonts.
    fn install_fonts(app: &mut App) {
        let binding = inset_painting::PaintingBinding::instance(app);
        if !binding.has_fonts(app) {
            binding.install_fonts(app, |fonts| {
                fonts.add_source(valo_system_fonts::SystemFonts::load());
            });
        }
    }

    /// Baseline cells share the row's baseline, and the table's baseline is the first row's.
    #[test]
    fn baseline_cells_share_the_first_rows_baseline() {
        use inset_painting::{TextSpan, TextStyle};

        use crate::paragraph::RenderParagraph;

        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        install_fonts(&mut app);
        let paragraph = |app: &mut App, font_size: f64| {
            let text = TextSpan::new()
                .text("Hg")
                .style(TextStyle::new().font_size(font_size))
                .into_span();
            RenderParagraph::new(app, text, TextDirection::Ltr, None).as_box()
        };
        let small = paragraph(&mut app, 10.0);
        let large = paragraph(&mut app, 30.0);
        let table = RenderTable::new(&mut app, TextDirection::Ltr);
        table.set_default_vertical_alignment(&mut app, TableCellVerticalAlignment::Baseline);
        table.set_text_baseline(&mut app, Some(TextBaseline::Alphabetic));
        table.set_children(&mut app, Some(vec![vec![small, large]]));
        layout(
            &mut app,
            table.as_box(),
            BoxConstraints::tight_for(Some(400.0), None),
        );

        let baseline_of = |app: &mut App, child: AnyRenderBox| {
            child.box_parent_data(app).offset.dy()
                + child
                    .get_distance_to_baseline(app, TextBaseline::Alphabetic, true)
                    .expect("text has a baseline")
        };
        let small_baseline = baseline_of(&mut app, small);
        let large_baseline = baseline_of(&mut app, large);
        assert!((small_baseline - large_baseline).abs() < 1e-9);
        assert!(
            small.box_parent_data(&app).offset.dy() > 0.0,
            "the small text is lowered"
        );
        assert_eq!(
            table
                .as_box()
                .get_distance_to_baseline(&mut app, TextBaseline::Alphabetic, true),
            Some(large_baseline)
        );
    }
}