waft-widgets 0.2.0

Widget framework and widgets for Waft
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
//! Flutter counterpart: `widgets/transitions.dart`.
//!
//! `SliverFadeTransition` waits with the slivers, [`MatrixTransition`]'s `filterQuality`
//! with `RenderTransform`'s, and [`FadeTransition`]'s `alwaysIncludeSemantics` with
//! accessibility.

use std::f64::consts::PI;
use std::fmt::{self, Debug};
use std::rc::Rc;

use waft_animation::{Animatable, AnyAnimation};
use waft_embedder::{Matrix4, Offset, Rect, Size, TextAlign, TextDirection};
use waft_foundation::{App, Handle, Listenable, Listener};
use waft_painting::{Alignment, AlignmentGeometry, Axis, Decoration, TextOverflow, TextStyle};
use waft_rendering::{
    AnyRenderObject, DecorationPosition, RelativeRect, RenderAnimatedOpacity,
    RenderAnimatedOpacityMixin, RenderBox, RenderHandle,
};

use crate::framework::{
    BuildContext, IntoWidget, KeyRef, RenderObjectWidget, SingleChildRenderObjectWidget, State,
    StateData, Stateful, StatefulWidget, WidgetRef, downcast_widget,
};
use crate::widgets::basic::{Align, ClipRect, FractionalTranslation, Positioned, Transform};
use crate::widgets::container::DecoratedBox;
use crate::widgets::text::DefaultTextStyle;

/// A widget that rebuilds when the given [`Listenable`] changes value.
///
/// [`AnimatedWidget`] is most commonly used with `Animation` objects, which are
/// [`Listenable`], but it can be used with any [`Listenable`], including
/// `ChangeNotifier` and `ValueNotifier`.
///
/// [`AnimatedWidget`] is most useful for widgets that are otherwise stateless. To
/// use [`AnimatedWidget`], implement this trait with its [`build`](Self::build) function;
/// the value becomes a tree node with `.into_widget()`, like every widget kind.
///
/// ## Relationship to `ImplicitlyAnimatedWidget`s
///
/// [`AnimatedWidget`]s (and their subclasses) take an explicit [`Listenable`] as
/// argument, which is usually an `Animation` derived from an
/// `AnimationController`. In most cases, the lifecycle of that
/// `AnimationController` has to be managed manually by the developer.
/// In contrast to that, `ImplicitlyAnimatedWidget`s (and their subclasses)
/// automatically manage their own internal `AnimationController` making those
/// classes easier to use as no external `Animation` has to be provided by the
/// developer. If you only need to set a target value for the animation and
/// configure its duration/curve, consider using (a subclass of)
/// `ImplicitlyAnimatedWidget`s instead of (a subclass of) this class.
///
/// ## Common animated widgets
///
/// A number of animated widgets ship with the framework. They are usually named
/// `FooTransition`, where `Foo` is the name of the non-animated
/// version of that widget. The subclasses of this class should not be confused
/// with subclasses of `ImplicitlyAnimatedWidget` (see above), which are usually
/// named `AnimatedFoo`. Commonly used animated widgets include:
///
///  * `ListenableBuilder`, which uses a builder pattern that is useful for
///    complex [`Listenable`] use cases.
///  * `AnimatedBuilder`, which uses a builder pattern that is useful for
///    complex `Animation` use cases.
///  * `AlignTransition`, which is an animated version of `Align`.
///  * `DecoratedBoxTransition`, which is an animated version of `DecoratedBox`.
///  * `DefaultTextStyleTransition`, which is an animated version of
///    `DefaultTextStyle`.
///  * `PositionedTransition`, which is an animated version of `Positioned`.
///  * `RelativePositionedTransition`, which is an animated version of
///    `Positioned`.
///  * `RotationTransition`, which animates the rotation of a widget.
///  * `ScaleTransition`, which animates the scale of a widget.
///  * `SizeTransition`, which animates its own size.
///  * `SlideTransition`, which animates the position of a widget relative to
///    its normal position.
///  * [`FadeTransition`], which is an animated version of `Opacity`.
///  * `AnimatedModalBarrier`, which is an animated version of `ModalBarrier`.
pub trait AnimatedWidget: Debug + 'static {
    /// See [`Widget::key`](crate::Widget::key).
    fn key(&self) -> Option<&KeyRef> {
        None
    }

    /// The [`Listenable`] to which this widget is listening.
    ///
    /// Commonly an `Animation` or a `ChangeNotifier`.
    ///
    /// Dart compares the old and new widget's listenable by identity; here that is
    /// `Rc::ptr_eq`, so a widget that keeps one `Rc` and clones it into each rebuilt
    /// value keeps its subscription, and one that wraps a handle anew each call is
    /// re-subscribed on every update (the same listener set either way).
    fn listenable(&self) -> Rc<dyn Listenable>;

    /// Override this method to build widgets that depend on the state of the
    /// listenable (e.g., the current value of the animation).
    fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef;
}

/// The kind tag of [`IntoWidget`] for an [`AnimatedWidget`].
pub struct AnimatedKind;

/// The [`StatefulWidget`] an [`AnimatedWidget`] becomes; its state is Dart's
/// `_AnimatedState`. Subclasses typically do not override `createState`, so this is the
/// one state every animated widget gets.
pub struct Animated<W: AnimatedWidget>(pub W);

impl<W: AnimatedWidget> IntoWidget<AnimatedKind> for W {
    fn into_widget(self) -> WidgetRef {
        Rc::new(Stateful(Animated(self)))
    }
}

impl<W: AnimatedWidget> StatefulWidget for Animated<W> {
    type State = AnimatedState<W>;

    fn key(&self) -> Option<&KeyRef> {
        self.0.key()
    }

    fn create_state(&self) -> AnimatedState<W> {
        AnimatedState {
            state: StateData::new(),
        }
    }
}

impl<W: AnimatedWidget> Debug for Animated<W> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// Dart's `_AnimatedState`.
pub struct AnimatedState<W: AnimatedWidget> {
    state: StateData<Animated<W>>,
}

impl<W: AnimatedWidget> State for AnimatedState<W> {
    type Widget = Animated<W>;
    crate::state_accessors!();

    fn init_state(self: Handle<Self>, app: &mut App) {
        let listenable = self.widget(app).0.listenable();
        listenable.add_listener(app, Listener::handle_method(self, Self::handle_change));
    }

    fn did_update_widget(self: Handle<Self>, app: &mut App, old_widget: &Animated<W>) {
        let listenable = self.widget(app).0.listenable();
        let old_listenable = old_widget.0.listenable();
        if !Rc::ptr_eq(&listenable, &old_listenable) {
            old_listenable
                .remove_listener(app, &Listener::handle_method(self, Self::handle_change));
            listenable.add_listener(app, Listener::handle_method(self, Self::handle_change));
        }
    }

    fn dispose(self: Handle<Self>, app: &mut App) {
        let listenable = self.widget(app).0.listenable();
        listenable.remove_listener(app, &Listener::handle_method(self, Self::handle_change));
    }

    fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
        let widget = context.widget(app).clone();
        downcast_widget::<Animated<W>>(&*widget)
            .expect("an AnimatedState builds its AnimatedWidget")
            .0
            .build(app, context)
    }
}

impl<W: AnimatedWidget> AnimatedState<W> {
    fn handle_change(self: Handle<Self>, app: &mut App) {
        if !self.mounted(app) {
            return;
        }
        self.set_state(app, |_state| {
            // The listenable's state is our build state, and it changed already.
        });
    }
}

/// Signature for a builder used to control a page's exit transition.
///
/// When a new route enters the stack, the `animation` argument is typically
/// used to control the enter and exit transition of the topmost route. The exit
/// transition of the route just below the new route is controlled with the
/// `secondary_animation`, which also controls the transition of the old route
/// when the topmost route is popped off the stack.
///
/// Typically used as the argument for `ModalRoute::delegated_transition`.
pub type DelegatedTransitionBuilder = Rc<
    dyn Fn(
        &mut App,
        BuildContext,
        AnyAnimation<f64>,
        AnyAnimation<f64>,
        bool,
        Option<&WidgetRef>,
    ) -> Option<WidgetRef>,
>;

/// Animates the position of a widget relative to its normal position.
///
/// The translation is expressed as an [`Offset`] scaled to the child's size. For
/// example, an [`Offset`] with a `dx` of 0.25 will result in a horizontal
/// translation of one quarter the width of the child.
///
/// By default, the offsets are applied in the coordinate system of the canvas
/// (so positive x offsets move the child towards the right). If a
/// [`text_direction`](Self::text_direction) is provided, then the offsets are applied in the
/// reading direction, so in right-to-left text, positive x offsets move towards the
/// left, and in left-to-right text, positive x offsets move towards the right.
///
/// See also:
///
///  * [`AlignTransition`], an animated version of an `Align` that animates its
///    `Align::alignment` property.
///  * [`PositionedTransition`], a widget that animates its child from a start
///    position to an end position over the lifetime of the animation.
///  * [`RelativePositionedTransition`], a widget that transitions its child's
///    position based on the value of a rectangle relative to a bounding box.
#[derive(Debug)]
pub struct SlideTransition {
    /// See [`Widget::key`](crate::Widget::key).
    pub key: Option<KeyRef>,

    /// The animation that controls the position of the child.
    ///
    /// If the current value of the position animation is `(dx, dy)`, the child
    /// will be translated horizontally by `width * dx` and vertically by
    /// `height * dy`, after applying the [`text_direction`](Self::text_direction) if available.
    pub position: AnyAnimation<Offset>,

    /// The direction to use for the x offset described by the [`position`](Self::position).
    ///
    /// If [`text_direction`](Self::text_direction) is `None`, the x offset is applied in the
    /// coordinate system of the canvas (so positive x offsets move the child towards the
    /// right).
    ///
    /// If [`text_direction`](Self::text_direction) is [`TextDirection::Rtl`], the x offset is
    /// applied in the reading direction such that x offsets move the child towards the left.
    ///
    /// If [`text_direction`](Self::text_direction) is [`TextDirection::Ltr`], the x offset is
    /// applied in the reading direction such that x offsets move the child towards the right.
    pub text_direction: Option<TextDirection>,

    /// Whether hit testing should be affected by the slide animation.
    ///
    /// If false, hit testing will proceed as if the child was not translated at
    /// all. Setting this value to false is useful for fast animations where you
    /// expect the user to commonly interact with the child widget in its final
    /// location and you want the user to benefit from "muscle memory".
    pub transform_hit_tests: bool,

    /// The widget below this widget in the tree.
    pub child: Option<WidgetRef>,
}

impl SlideTransition {
    /// Creates a fractional translation transition.
    pub fn new(position: AnyAnimation<Offset>) -> SlideTransition {
        SlideTransition {
            key: None,
            position,
            text_direction: None,
            transform_hit_tests: true,
            child: None,
        }
    }

    /// Dart `SlideTransition(key:)`.
    pub fn key(mut self, key: KeyRef) -> SlideTransition {
        self.key = Some(key);
        self
    }

    /// Dart `SlideTransition(textDirection:)`.
    pub fn text_direction(mut self, text_direction: TextDirection) -> SlideTransition {
        self.text_direction = Some(text_direction);
        self
    }

    /// Dart `SlideTransition(transformHitTests:)`.
    pub fn transform_hit_tests(mut self, transform_hit_tests: bool) -> SlideTransition {
        self.transform_hit_tests = transform_hit_tests;
        self
    }

    /// Dart `SlideTransition(child:)`.
    pub fn child<K>(mut self, child: impl IntoWidget<K>) -> SlideTransition {
        self.child = Some(child.into_widget());
        self
    }
}

impl AnimatedWidget for SlideTransition {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn listenable(&self) -> Rc<dyn Listenable> {
        Rc::new(self.position)
    }

    fn build(&self, app: &mut App, _context: BuildContext) -> WidgetRef {
        let mut offset = self.position.value(app);
        if self.text_direction == Some(TextDirection::Rtl) {
            offset = Offset::new(-offset.dx(), offset.dy());
        }
        let mut translation =
            FractionalTranslation::new(offset).transform_hit_tests(self.transform_hit_tests);
        if let Some(child) = &self.child {
            translation = translation.child(child.clone());
        }
        translation.into_widget()
    }
}

/// Signature for the callback to [`MatrixTransition::on_transform`].
///
/// Computes a [`Matrix4`] to be used in the [`MatrixTransition`] transformed widget
/// from the [`MatrixTransition::animation`] value.
pub type TransformCallback = Rc<dyn Fn(f64) -> Matrix4>;

/// Animates the [`Matrix4`] of a transformed widget.
///
/// The [`on_transform`](Self::on_transform) callback computes a [`Matrix4`] from the animated
/// value, it is called every time the [`animation`](Self::animation) changes its value.
///
/// See also:
///
///  * [`ScaleTransition`], which animates the scale of a widget, by providing a
///    matrix which scales along the X and Y axis.
///  * [`RotationTransition`], which animates the rotation of a widget, by
///    providing a matrix which rotates along the Z axis.
pub struct MatrixTransition {
    /// See [`Widget::key`](crate::Widget::key).
    pub key: Option<KeyRef>,

    /// The callback to compute a [`Matrix4`] from the [`animation`](Self::animation). It's
    /// called every time [`animation`](Self::animation) changes its value.
    pub on_transform: TransformCallback,

    /// The animation that controls the matrix of the child.
    ///
    /// The matrix will be computed from the animation with the
    /// [`on_transform`](Self::on_transform) callback.
    pub animation: AnyAnimation<f64>,

    /// The alignment of the origin of the coordinate system in which the
    /// transform takes place, relative to the size of the box.
    ///
    /// For example, to set the origin of the transform to bottom middle, you can
    /// use an alignment of (0.0, 1.0).
    pub alignment: Alignment,

    /// The widget below this widget in the tree.
    pub child: Option<WidgetRef>,
}

impl MatrixTransition {
    /// Creates a matrix transition.
    ///
    /// The [`alignment`](Self::alignment) defaults to [`Alignment::CENTER`].
    pub fn new(
        animation: AnyAnimation<f64>,
        on_transform: impl Fn(f64) -> Matrix4 + 'static,
    ) -> MatrixTransition {
        MatrixTransition {
            key: None,
            on_transform: Rc::new(on_transform),
            animation,
            alignment: Alignment::CENTER,
            child: None,
        }
    }

    /// Dart `MatrixTransition(key:)`.
    pub fn key(mut self, key: KeyRef) -> MatrixTransition {
        self.key = Some(key);
        self
    }

    /// Dart `MatrixTransition(alignment:)`.
    pub fn alignment(mut self, alignment: Alignment) -> MatrixTransition {
        self.alignment = alignment;
        self
    }

    /// Dart `MatrixTransition(child:)`.
    pub fn child<K>(mut self, child: impl IntoWidget<K>) -> MatrixTransition {
        self.child = Some(child.into_widget());
        self
    }

    /// `MatrixTransition.build`'s body, shared with [`ScaleTransition`] and
    /// [`RotationTransition`].
    fn build_transform(
        transform: Matrix4,
        alignment: Alignment,
        child: Option<&WidgetRef>,
    ) -> WidgetRef {
        let mut transform =
            Transform::new(transform).alignment(AlignmentGeometry::Alignment(alignment));
        if let Some(child) = child {
            transform = transform.child(child.clone());
        }
        transform.into_widget()
    }
}

impl Debug for MatrixTransition {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MatrixTransition")
            .field("animation", &self.animation)
            .field("alignment", &self.alignment)
            .field("child", &self.child)
            .finish_non_exhaustive()
    }
}

impl AnimatedWidget for MatrixTransition {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn listenable(&self) -> Rc<dyn Listenable> {
        Rc::new(self.animation)
    }

    fn build(&self, app: &mut App, _context: BuildContext) -> WidgetRef {
        let transform = (self.on_transform)(self.animation.value(app));
        MatrixTransition::build_transform(transform, self.alignment, self.child.as_ref())
    }
}

/// Animates the scale of a transformed widget.
///
/// See also:
///
///  * [`PositionedTransition`], a widget that animates its child from a start
///    position to an end position over the lifetime of the animation.
///  * [`RelativePositionedTransition`], a widget that transitions its child's
///    position based on the value of a rectangle relative to a bounding box.
///  * [`SizeTransition`], a widget that animates its own size and clips and
///    aligns its child.
#[derive(Debug)]
pub struct ScaleTransition {
    /// See [`Widget::key`](crate::Widget::key).
    pub key: Option<KeyRef>,

    /// See [`MatrixTransition::animation`]; read it as
    /// [`scale`](Self::scale).
    pub animation: AnyAnimation<f64>,

    /// See [`MatrixTransition::alignment`].
    pub alignment: Alignment,

    /// The widget below this widget in the tree.
    pub child: Option<WidgetRef>,
}

impl ScaleTransition {
    /// Creates a scale transition.
    ///
    /// The [`alignment`](Self::alignment) defaults to [`Alignment::CENTER`].
    pub fn new(scale: AnyAnimation<f64>) -> ScaleTransition {
        ScaleTransition {
            key: None,
            animation: scale,
            alignment: Alignment::CENTER,
            child: None,
        }
    }

    /// The animation that controls the scale of the child.
    pub fn scale(&self) -> AnyAnimation<f64> {
        self.animation
    }

    /// Dart `ScaleTransition(key:)`.
    pub fn key(mut self, key: KeyRef) -> ScaleTransition {
        self.key = Some(key);
        self
    }

    /// Dart `ScaleTransition(alignment:)`.
    pub fn alignment(mut self, alignment: Alignment) -> ScaleTransition {
        self.alignment = alignment;
        self
    }

    /// Dart `ScaleTransition(child:)`.
    pub fn child<K>(mut self, child: impl IntoWidget<K>) -> ScaleTransition {
        self.child = Some(child.into_widget());
        self
    }

    /// The callback that controls the scale of the child.
    ///
    /// If the current value of the animation is v, the child will be
    /// painted v times its normal size.
    fn handle_scale_matrix(value: f64) -> Matrix4 {
        Matrix4::scale(value as f32, value as f32)
    }
}

impl AnimatedWidget for ScaleTransition {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn listenable(&self) -> Rc<dyn Listenable> {
        Rc::new(self.animation)
    }

    fn build(&self, app: &mut App, _context: BuildContext) -> WidgetRef {
        let transform = ScaleTransition::handle_scale_matrix(self.animation.value(app));
        MatrixTransition::build_transform(transform, self.alignment, self.child.as_ref())
    }
}

/// Animates the rotation of a widget.
///
/// See also:
///
///  * [`ScaleTransition`], a widget that animates the scale of a transformed
///    widget.
///  * [`SizeTransition`], a widget that animates its own size and clips and
///    aligns its child.
#[derive(Debug)]
pub struct RotationTransition {
    /// See [`Widget::key`](crate::Widget::key).
    pub key: Option<KeyRef>,

    /// See [`MatrixTransition::animation`]; read it as [`turns`](Self::turns).
    pub animation: AnyAnimation<f64>,

    /// See [`MatrixTransition::alignment`].
    pub alignment: Alignment,

    /// The widget below this widget in the tree.
    pub child: Option<WidgetRef>,
}

impl RotationTransition {
    /// Creates a rotation transition.
    pub fn new(turns: AnyAnimation<f64>) -> RotationTransition {
        RotationTransition {
            key: None,
            animation: turns,
            alignment: Alignment::CENTER,
            child: None,
        }
    }

    /// The animation that controls the rotation of the child.
    pub fn turns(&self) -> AnyAnimation<f64> {
        self.animation
    }

    /// Dart `RotationTransition(key:)`.
    pub fn key(mut self, key: KeyRef) -> RotationTransition {
        self.key = Some(key);
        self
    }

    /// Dart `RotationTransition(alignment:)`.
    pub fn alignment(mut self, alignment: Alignment) -> RotationTransition {
        self.alignment = alignment;
        self
    }

    /// Dart `RotationTransition(child:)`.
    pub fn child<K>(mut self, child: impl IntoWidget<K>) -> RotationTransition {
        self.child = Some(child.into_widget());
        self
    }

    /// The callback that controls the rotation of the child.
    ///
    /// If the current value of the animation is v, the child will be rotated
    /// v * 2 * pi radians before being painted.
    fn handle_turns_matrix(value: f64) -> Matrix4 {
        Matrix4::rotation((value * PI * 2.0) as f32)
    }
}

impl AnimatedWidget for RotationTransition {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn listenable(&self) -> Rc<dyn Listenable> {
        Rc::new(self.animation)
    }

    fn build(&self, app: &mut App, _context: BuildContext) -> WidgetRef {
        let transform = RotationTransition::handle_turns_matrix(self.animation.value(app));
        MatrixTransition::build_transform(transform, self.alignment, self.child.as_ref())
    }
}

/// Animates its own size and clips and aligns its child.
///
/// [`SizeTransition`] acts as a `ClipRect` that animates either its width or its
/// height, depending upon the value of [`axis`](Self::axis). The alignment of the child is
/// specified by the [`alignment`](Self::alignment).
///
/// Like most widgets, [`SizeTransition`] will conform to the constraints it is
/// given, so be sure to put it in a context where it can change size. For
/// instance, if you place it into a `Container` with a fixed size, then the
/// [`SizeTransition`] will not be able to change size, and will appear to do
/// nothing.
///
/// See also:
///
///  * `AnimatedCrossFade`, for a widget that automatically animates between
///    the sizes of two children, fading between them.
///  * [`ScaleTransition`], a widget that scales the size of the child instead of
///    clipping it.
///  * [`PositionedTransition`], a widget that animates its child from a start
///    position to an end position over the lifetime of the animation.
///  * [`RelativePositionedTransition`], a widget that transitions its child's
///    position based on the value of a rectangle relative to a bounding box.
#[derive(Debug)]
pub struct SizeTransition {
    /// See [`Widget::key`](crate::Widget::key).
    pub key: Option<KeyRef>,

    /// [`Axis::Horizontal`] if [`size_factor`](Self::size_factor) modifies the width,
    /// otherwise [`Axis::Vertical`].
    pub axis: Axis,

    /// The animation that controls the (clipped) size of the child.
    ///
    /// The width or height (depending on the [`axis`](Self::axis) value) of this widget will
    /// be its intrinsic width or height multiplied by
    /// [`size_factor`](Self::size_factor)'s value at the current point in the animation.
    ///
    /// If the value of [`size_factor`](Self::size_factor) is less than one, the child will be
    /// clipped in the appropriate axis.
    pub size_factor: AnyAnimation<f64>,

    /// Describes how to align the child along the axis that
    /// [`size_factor`](Self::size_factor) is modifying.
    ///
    /// A value of -1.0 indicates the top when [`axis`](Self::axis) is [`Axis::Vertical`], and
    /// the start when [`axis`](Self::axis) is [`Axis::Horizontal`]. The start is on the left
    /// when the text direction in effect is [`TextDirection::Ltr`] and on the right when it
    /// is [`TextDirection::Rtl`].
    ///
    /// A value of 1.0 indicates the bottom or end, depending upon the [`axis`](Self::axis).
    ///
    /// A value of 0.0 (the default) indicates the center for either [`axis`](Self::axis)
    /// value.
    ///
    /// This property has been deprecated and superseded by
    /// [`alignment`](Self::alignment). Existing usages can be migrated as follows:
    /// - If [`axis`](Self::axis) is [`Axis::Horizontal`], replace with
    ///   `AlignmentGeometry::xy(axis_alignment.unwrap_or(0.0), -1.0)`.
    /// - If [`axis`](Self::axis) is [`Axis::Vertical`], replace with
    ///   `AlignmentGeometry::xy(-1.0, axis_alignment.unwrap_or(0.0))`.
    #[deprecated(
        note = "Use alignment instead. This property provides full control over both axes, \
                which is an improvement over the old axisAlignment. This feature was \
                deprecated after v3.41.0-1.0.pre."
    )]
    pub axis_alignment: Option<f64>,

    /// The alignment of the child within the parent during the transition.
    pub alignment: Option<AlignmentGeometry>,

    /// The factor by which to multiply the cross axis size of the child.
    ///
    /// If the value of [`fixed_cross_axis_size_factor`](Self::fixed_cross_axis_size_factor)
    /// is less than one, the child will be clipped along the appropriate axis.
    ///
    /// If `None` (the default), the cross axis size is as large as the parent.
    pub fixed_cross_axis_size_factor: Option<f64>,

    /// The widget below this widget in the tree.
    pub child: Option<WidgetRef>,
}

impl SizeTransition {
    /// Creates a size transition.
    ///
    /// The [`axis`](Self::axis) defaults to [`Axis::Vertical`]. The
    /// [`alignment`](Self::alignment) defaults to the center of the modified axis, which
    /// centers the child within the parent during the transition.
    #[allow(deprecated)]
    pub fn new(size_factor: AnyAnimation<f64>) -> SizeTransition {
        SizeTransition {
            key: None,
            axis: Axis::Vertical,
            size_factor,
            axis_alignment: None,
            alignment: None,
            fixed_cross_axis_size_factor: None,
            child: None,
        }
    }

    /// Dart `SizeTransition(key:)`.
    pub fn key(mut self, key: KeyRef) -> SizeTransition {
        self.key = Some(key);
        self
    }

    /// Dart `SizeTransition(axis:)`.
    pub fn axis(mut self, axis: Axis) -> SizeTransition {
        self.axis = axis;
        self
    }

    /// Dart `SizeTransition(axisAlignment:)`.
    #[deprecated(
        note = "Use alignment instead. This property provides full control over both axes, \
                which is an improvement over the old axisAlignment. This feature was \
                deprecated after v3.41.0-1.0.pre."
    )]
    #[allow(deprecated)]
    pub fn axis_alignment(mut self, axis_alignment: f64) -> SizeTransition {
        debug_assert!(
            self.alignment.is_none(),
            "Cannot provide both axisAlignment and alignment as axisAlignment has been \
             deprecated and superseded by alignment."
        );
        self.axis_alignment = Some(axis_alignment);
        self
    }

    /// Dart `SizeTransition(alignment:)`.
    #[allow(deprecated)]
    pub fn alignment(mut self, alignment: AlignmentGeometry) -> SizeTransition {
        debug_assert!(
            self.axis_alignment.is_none(),
            "Cannot provide both axisAlignment and alignment as axisAlignment has been \
             deprecated and superseded by alignment."
        );
        self.alignment = Some(alignment);
        self
    }

    /// Dart `SizeTransition(fixedCrossAxisSizeFactor:)`.
    pub fn fixed_cross_axis_size_factor(
        mut self,
        fixed_cross_axis_size_factor: f64,
    ) -> SizeTransition {
        debug_assert!(fixed_cross_axis_size_factor >= 0.0);
        self.fixed_cross_axis_size_factor = Some(fixed_cross_axis_size_factor);
        self
    }

    /// Dart `SizeTransition(child:)`.
    pub fn child<K>(mut self, child: impl IntoWidget<K>) -> SizeTransition {
        self.child = Some(child.into_widget());
        self
    }
}

impl AnimatedWidget for SizeTransition {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn listenable(&self) -> Rc<dyn Listenable> {
        Rc::new(self.size_factor)
    }

    #[allow(deprecated)]
    fn build(&self, app: &mut App, _context: BuildContext) -> WidgetRef {
        let axis_alignment = self.axis_alignment.unwrap_or(0.0);
        let alignment = self.alignment.unwrap_or(match self.axis {
            Axis::Horizontal => AlignmentGeometry::directional(axis_alignment, -1.0),
            Axis::Vertical => AlignmentGeometry::directional(-1.0, axis_alignment),
        });
        let size_factor = self.size_factor.value(app).max(0.0);
        let (height_factor, width_factor) = match self.axis {
            Axis::Vertical => (Some(size_factor), self.fixed_cross_axis_size_factor),
            Axis::Horizontal => (self.fixed_cross_axis_size_factor, Some(size_factor)),
        };
        let mut align = Align::new().alignment(alignment);
        if let Some(height_factor) = height_factor {
            align = align.height_factor(height_factor);
        }
        if let Some(width_factor) = width_factor {
            align = align.width_factor(width_factor);
        }
        if let Some(child) = &self.child {
            align = align.child(child.clone());
        }
        ClipRect::new().child(align).into_widget()
    }
}

/// Animates the opacity of a widget.
///
/// For a widget that automatically animates between the sizes of two children,
/// fading between them, see `AnimatedCrossFade`.
///
/// ## Hit testing
///
/// Setting the [`opacity`](Self::opacity) to zero does not prevent hit testing from being
/// applied to the descendants of the [`FadeTransition`] widget. This can be
/// confusing for the user, who may not see anything, and may believe the area
/// of the interface where the [`FadeTransition`] is hiding a widget to be
/// non-interactive.
///
/// With certain widgets, such as `Flow`, that compute their positions only when
/// they are painted, this can actually lead to bugs (from unexpected geometry
/// to exceptions), because those widgets are not painted by the
/// [`FadeTransition`] widget at all when the [`opacity`](Self::opacity) animation reaches
/// zero.
///
/// To avoid such problems, it is generally a good idea to combine this widget
/// with an `IgnorePointer` that one enables when the [`opacity`](Self::opacity) animation
/// reaches zero. This prevents interactions with any children in the subtree
/// when the [`child`](Self::child) is not visible. For performance reasons, when
/// implementing this, care should be taken not to rebuild the relevant widget (e.g. by
/// calling `State::set_state`) except at the transition point.
///
/// See also:
///
///  * `Opacity`, which does not animate changes in opacity.
///  * `AnimatedOpacity`, which animates changes in opacity without taking an
///    explicit `Animation` argument.
#[derive(Debug)]
pub struct FadeTransition {
    /// See [`Widget::key`](crate::Widget::key).
    pub key: Option<KeyRef>,

    /// The animation that controls the opacity of the child.
    ///
    /// If the current value of the opacity animation is v, the child will be
    /// painted with an opacity of v. For example, if v is 0.5, the child will be
    /// blended 50% with its background. Similarly, if v is 0.0, the child will be
    /// completely transparent.
    pub opacity: AnyAnimation<f64>,

    /// The widget below this widget in the tree.
    pub child: Option<WidgetRef>,
}

impl FadeTransition {
    /// Creates an opacity transition.
    pub fn new(opacity: AnyAnimation<f64>) -> FadeTransition {
        FadeTransition {
            key: None,
            opacity,
            child: None,
        }
    }

    /// Dart `FadeTransition(key:)`.
    pub fn key(mut self, key: KeyRef) -> FadeTransition {
        self.key = Some(key);
        self
    }

    /// Dart `FadeTransition(child:)`.
    pub fn child<K>(mut self, child: impl IntoWidget<K>) -> FadeTransition {
        self.child = Some(child.into_widget());
        self
    }
}

impl RenderObjectWidget for FadeTransition {
    type RenderObject = RenderAnimatedOpacity;

    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
        RenderAnimatedOpacity::new(app, self.opacity, None).as_object()
    }

    fn update_render_object(
        &self,
        app: &mut App,
        _context: BuildContext,
        render_object: RenderHandle<RenderAnimatedOpacity>,
    ) {
        render_object.set_opacity(app, self.opacity);
    }
}

impl SingleChildRenderObjectWidget for FadeTransition {
    fn child(&self) -> Option<&WidgetRef> {
        self.child.as_ref()
    }
}

/// An interpolation between two relative rects.
///
/// This class specializes the interpolation of `Tween<RelativeRect>` to
/// use [`RelativeRect::lerp`].
///
/// See `Tween` for a discussion on how to use interpolation objects.
///
/// Unlike the tweens in `waft_animation`, this one is a value rather than an arena
/// object: the orphan rule leaves no local type in
/// `impl Animatable<RelativeRect> for Handle<RelativeRectTween>`. A driven animation
/// therefore holds a clone, and writing [`begin`](Self::begin) or [`end`](Self::end)
/// afterwards does not reach it.
#[derive(Clone, Debug)]
pub struct RelativeRectTween {
    /// The value this variable has at the beginning of the animation.
    pub begin: Option<RelativeRect>,

    /// The value this variable has at the end of the animation.
    pub end: Option<RelativeRect>,
}

impl RelativeRectTween {
    /// Creates a [`RelativeRect`] tween.
    ///
    /// The [`begin`](Self::begin) and [`end`](Self::end) properties may be `None`; the `None`
    /// value is treated as [`RelativeRect::FILL`].
    pub fn new(begin: Option<RelativeRect>, end: Option<RelativeRect>) -> RelativeRectTween {
        RelativeRectTween { begin, end }
    }

    /// Returns the value this variable has at the given animation clock value.
    pub fn lerp(&self, t: f64) -> RelativeRect {
        RelativeRect::lerp(self.begin, self.end, t)
            .expect("RelativeRectTween.begin or end must be set before use")
    }
}

impl Animatable<RelativeRect> for RelativeRectTween {
    fn transform(&self, _app: &App, t: f64) -> RelativeRect {
        if t == 0.0 {
            return self.begin.expect("Tween.begin must be set before use");
        }
        if t == 1.0 {
            return self.end.expect("Tween.end must be set before use");
        }
        self.lerp(t)
    }
}

/// Animated version of `Positioned` which takes a specific
/// `Animation<RelativeRect>` to transition the child's position from a start
/// position to an end position over the lifetime of the animation.
///
/// Only works if it's the child of a `Stack`.
///
/// See also:
///
///  * `AnimatedPositioned`, which transitions a child's position without
///    taking an explicit `Animation` argument.
///  * [`RelativePositionedTransition`], a widget that transitions its child's
///    position based on the value of a rectangle relative to a bounding box.
///  * [`SlideTransition`], a widget that animates the position of a widget
///    relative to its normal position.
///  * [`AlignTransition`], an animated version of an `Align` that animates its
///    `Align::alignment` property.
///  * [`ScaleTransition`], a widget that animates the scale of a transformed
///    widget.
///  * [`SizeTransition`], a widget that animates its own size and clips and
///    aligns its child.
#[derive(Debug)]
pub struct PositionedTransition {
    /// See [`Widget::key`](crate::Widget::key).
    pub key: Option<KeyRef>,

    /// The animation that controls the child's size and position.
    pub rect: AnyAnimation<RelativeRect>,

    /// The widget below this widget in the tree.
    pub child: WidgetRef,
}

impl PositionedTransition {
    /// Creates a transition for `Positioned`.
    pub fn new<K>(
        rect: AnyAnimation<RelativeRect>,
        child: impl IntoWidget<K>,
    ) -> PositionedTransition {
        PositionedTransition {
            key: None,
            rect,
            child: child.into_widget(),
        }
    }

    /// Dart `PositionedTransition(key:)`.
    pub fn key(mut self, key: KeyRef) -> PositionedTransition {
        self.key = Some(key);
        self
    }
}

impl AnimatedWidget for PositionedTransition {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn listenable(&self) -> Rc<dyn Listenable> {
        Rc::new(self.rect)
    }

    fn build(&self, app: &mut App, _context: BuildContext) -> WidgetRef {
        Positioned::from_relative_rect(self.rect.value(app), self.child.clone()).into_widget()
    }
}

/// Animated version of `Positioned` which transitions the child's position
/// based on the value of [`rect`](Self::rect) relative to a bounding box with the
/// specified [`size`](Self::size).
///
/// Only works if it's the child of a `Stack`.
///
/// See also:
///
///  * [`PositionedTransition`], a widget that animates its child from a start
///    position to an end position over the lifetime of the animation.
///  * [`AlignTransition`], an animated version of an `Align` that animates its
///    `Align::alignment` property.
///  * [`ScaleTransition`], a widget that animates the scale of a transformed
///    widget.
///  * [`SizeTransition`], a widget that animates its own size and clips and
///    aligns its child.
///  * [`SlideTransition`], a widget that animates the position of a widget
///    relative to its normal position.
#[derive(Debug)]
pub struct RelativePositionedTransition {
    /// See [`Widget::key`](crate::Widget::key).
    pub key: Option<KeyRef>,

    /// The animation that controls the child's size and position.
    ///
    /// If the animation returns a `None` [`Rect`], the rect is assumed to be
    /// [`Rect::ZERO`].
    ///
    /// See also:
    ///
    ///  * [`size`](Self::size), which gets the size of the box that the `Positioned` widget's
    ///    offsets are relative to.
    pub rect: AnyAnimation<Option<Rect>>,

    /// The `Positioned` widget's offsets are relative to a box of this
    /// size whose origin is 0,0.
    pub size: Size,

    /// The widget below this widget in the tree.
    pub child: WidgetRef,
}

impl RelativePositionedTransition {
    /// Create an animated version of `Positioned`.
    ///
    /// Each frame, the `Positioned` widget will be configured to represent the
    /// current value of the [`rect`](Self::rect) argument assuming that the stack has the
    /// given [`size`](Self::size).
    pub fn new<K>(
        rect: AnyAnimation<Option<Rect>>,
        size: Size,
        child: impl IntoWidget<K>,
    ) -> RelativePositionedTransition {
        RelativePositionedTransition {
            key: None,
            rect,
            size,
            child: child.into_widget(),
        }
    }

    /// Dart `RelativePositionedTransition(key:)`.
    pub fn key(mut self, key: KeyRef) -> RelativePositionedTransition {
        self.key = Some(key);
        self
    }
}

impl AnimatedWidget for RelativePositionedTransition {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn listenable(&self) -> Rc<dyn Listenable> {
        Rc::new(self.rect)
    }

    fn build(&self, app: &mut App, _context: BuildContext) -> WidgetRef {
        let offsets =
            RelativeRect::from_size(self.rect.value(app).unwrap_or(Rect::ZERO), self.size);
        Positioned::new(self.child.clone())
            .top(offsets.top)
            .right(offsets.right)
            .bottom(offsets.bottom)
            .left(offsets.left)
            .into_widget()
    }
}

/// Animated version of a [`DecoratedBox`] that animates the different properties
/// of its `Decoration`.
///
/// See also:
///
///  * [`DecoratedBox`], which also draws a `Decoration` but is not animated.
///  * `AnimatedContainer`, a more full-featured container that also animates on
///    decoration using an internal animation.
#[derive(Debug)]
pub struct DecoratedBoxTransition {
    /// See [`Widget::key`](crate::Widget::key).
    pub key: Option<KeyRef>,

    /// Animation of the decoration to paint.
    ///
    /// Can be created using a `DecorationTween` interpolating typically between
    /// two `BoxDecoration`.
    pub decoration: AnyAnimation<Box<dyn Decoration>>,

    /// Whether to paint the box decoration behind or in front of the child.
    pub position: DecorationPosition,

    /// The widget below this widget in the tree.
    pub child: WidgetRef,
}

impl DecoratedBoxTransition {
    /// Creates an animated [`DecoratedBox`] whose `Decoration` animation updates
    /// the widget.
    ///
    /// See also:
    ///
    ///  * [`DecoratedBox::new`]
    pub fn new<K>(
        decoration: AnyAnimation<Box<dyn Decoration>>,
        child: impl IntoWidget<K>,
    ) -> DecoratedBoxTransition {
        DecoratedBoxTransition {
            key: None,
            decoration,
            position: DecorationPosition::Background,
            child: child.into_widget(),
        }
    }

    /// Dart `DecoratedBoxTransition(key:)`.
    pub fn key(mut self, key: KeyRef) -> DecoratedBoxTransition {
        self.key = Some(key);
        self
    }

    /// Dart `DecoratedBoxTransition(position:)`.
    pub fn position(mut self, position: DecorationPosition) -> DecoratedBoxTransition {
        self.position = position;
        self
    }
}

impl AnimatedWidget for DecoratedBoxTransition {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn listenable(&self) -> Rc<dyn Listenable> {
        Rc::new(self.decoration)
    }

    fn build(&self, app: &mut App, _context: BuildContext) -> WidgetRef {
        DecoratedBox {
            key: None,
            decoration: self.decoration.value(app),
            position: self.position,
            child: Some(self.child.clone()),
        }
        .into_widget()
    }
}

/// Animated version of an `Align` that animates its `Align::alignment` property.
///
/// See also:
///
///  * `AnimatedAlign`, which animates changes to the [`alignment`](Self::alignment) without
///    taking an explicit `Animation` argument.
///  * [`PositionedTransition`], a widget that animates its child from a start
///    position to an end position over the lifetime of the animation.
///  * [`RelativePositionedTransition`], a widget that transitions its child's
///    position based on the value of a rectangle relative to a bounding box.
///  * [`SizeTransition`], a widget that animates its own size and clips and
///    aligns its child.
///  * [`SlideTransition`], a widget that animates the position of a widget
///    relative to its normal position.
#[derive(Debug)]
pub struct AlignTransition {
    /// See [`Widget::key`](crate::Widget::key).
    pub key: Option<KeyRef>,

    /// The animation that controls the child's alignment.
    pub alignment: AnyAnimation<AlignmentGeometry>,

    /// If non-`None`, the child's width factor, see `Align::width_factor`.
    pub width_factor: Option<f64>,

    /// If non-`None`, the child's height factor, see `Align::height_factor`.
    pub height_factor: Option<f64>,

    /// The widget below this widget in the tree.
    pub child: WidgetRef,
}

impl AlignTransition {
    /// Creates an animated `Align` whose [`AlignmentGeometry`] animation updates
    /// the widget.
    ///
    /// See also:
    ///
    ///  * `Align::new`.
    pub fn new<K>(
        alignment: AnyAnimation<AlignmentGeometry>,
        child: impl IntoWidget<K>,
    ) -> AlignTransition {
        AlignTransition {
            key: None,
            alignment,
            width_factor: None,
            height_factor: None,
            child: child.into_widget(),
        }
    }

    /// Dart `AlignTransition(key:)`.
    pub fn key(mut self, key: KeyRef) -> AlignTransition {
        self.key = Some(key);
        self
    }

    /// Dart `AlignTransition(widthFactor:)`.
    pub fn width_factor(mut self, width_factor: f64) -> AlignTransition {
        self.width_factor = Some(width_factor);
        self
    }

    /// Dart `AlignTransition(heightFactor:)`.
    pub fn height_factor(mut self, height_factor: f64) -> AlignTransition {
        self.height_factor = Some(height_factor);
        self
    }
}

impl AnimatedWidget for AlignTransition {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn listenable(&self) -> Rc<dyn Listenable> {
        Rc::new(self.alignment)
    }

    fn build(&self, app: &mut App, _context: BuildContext) -> WidgetRef {
        let mut align = Align::new().alignment(self.alignment.value(app));
        if let Some(width_factor) = self.width_factor {
            align = align.width_factor(width_factor);
        }
        if let Some(height_factor) = self.height_factor {
            align = align.height_factor(height_factor);
        }
        align.child(self.child.clone()).into_widget()
    }
}

/// Animated version of a [`DefaultTextStyle`] that animates the different properties
/// of its [`TextStyle`].
///
/// See also:
///
///  * `AnimatedDefaultTextStyle`, which animates changes in text style without
///    taking an explicit `Animation` argument.
///  * [`DefaultTextStyle`], which also defines a [`TextStyle`] for its descendants
///    but is not animated.
#[derive(Debug)]
pub struct DefaultTextStyleTransition {
    /// See [`Widget::key`](crate::Widget::key).
    pub key: Option<KeyRef>,

    /// The animation that controls the descendants' text style.
    pub style: AnyAnimation<TextStyle>,

    /// How the text should be aligned horizontally.
    pub text_align: Option<TextAlign>,

    /// Whether the text should break at soft line breaks.
    ///
    /// See [`DefaultTextStyle::soft_wrap`] for more details.
    pub soft_wrap: bool,

    /// How visual overflow should be handled.
    pub overflow: TextOverflow,

    /// An optional maximum number of lines for the text to span, wrapping if necessary.
    ///
    /// See [`DefaultTextStyle::max_lines`] for more details.
    pub max_lines: Option<i32>,

    /// The widget below this widget in the tree.
    pub child: WidgetRef,
}

impl DefaultTextStyleTransition {
    /// Creates an animated [`DefaultTextStyle`] whose [`TextStyle`] animation updates
    /// the widget.
    pub fn new<K>(
        style: AnyAnimation<TextStyle>,
        child: impl IntoWidget<K>,
    ) -> DefaultTextStyleTransition {
        DefaultTextStyleTransition {
            key: None,
            style,
            text_align: None,
            soft_wrap: true,
            overflow: TextOverflow::Clip,
            max_lines: None,
            child: child.into_widget(),
        }
    }

    /// Dart `DefaultTextStyleTransition(key:)`.
    pub fn key(mut self, key: KeyRef) -> DefaultTextStyleTransition {
        self.key = Some(key);
        self
    }

    /// Dart `DefaultTextStyleTransition(textAlign:)`.
    pub fn text_align(mut self, text_align: TextAlign) -> DefaultTextStyleTransition {
        self.text_align = Some(text_align);
        self
    }

    /// Dart `DefaultTextStyleTransition(softWrap:)`.
    pub fn soft_wrap(mut self, soft_wrap: bool) -> DefaultTextStyleTransition {
        self.soft_wrap = soft_wrap;
        self
    }

    /// Dart `DefaultTextStyleTransition(overflow:)`.
    pub fn overflow(mut self, overflow: TextOverflow) -> DefaultTextStyleTransition {
        self.overflow = overflow;
        self
    }

    /// Dart `DefaultTextStyleTransition(maxLines:)`.
    pub fn max_lines(mut self, max_lines: i32) -> DefaultTextStyleTransition {
        self.max_lines = Some(max_lines);
        self
    }
}

impl AnimatedWidget for DefaultTextStyleTransition {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn listenable(&self) -> Rc<dyn Listenable> {
        Rc::new(self.style)
    }

    fn build(&self, app: &mut App, _context: BuildContext) -> WidgetRef {
        let mut default_text_style =
            DefaultTextStyle::new(self.style.value(app), self.child.clone())
                .soft_wrap(self.soft_wrap)
                .overflow(self.overflow);
        if let Some(text_align) = self.text_align {
            default_text_style = default_text_style.text_align(text_align);
        }
        if let Some(max_lines) = self.max_lines {
            default_text_style = default_text_style.max_lines(max_lines);
        }
        default_text_style.into_widget()
    }
}

/// A builder that builds a widget given a child.
///
/// The child is passed into the builder to allow the builder to reuse a widget that does
/// not depend on the listenable, as [`ListenableBuilder`] and `AnimatedBuilder` do.
pub type TransitionBuilder = Rc<dyn Fn(&mut App, BuildContext, Option<&WidgetRef>) -> WidgetRef>;

/// A general-purpose widget for building a widget subtree when a `Listenable` changes.
///
/// [`ListenableBuilder`] is useful for more complex widgets that wish to listen to changes in
/// other objects as part of a larger build function. To use [`ListenableBuilder`], construct
/// the widget and pass it a [`builder`](Self::builder) function.
///
/// Any subtype of `Listenable` (such as a `ChangeNotifier`, `ValueNotifier`, or `Animation`)
/// can be used with a [`ListenableBuilder`] to rebuild only certain parts of a widget when
/// the `Listenable` notifies its listeners. Although they have identical implementations, if
/// an `Animation` is being listened to, consider using an `AnimatedBuilder` instead for
/// better readability.
///
/// ## Performance optimizations
///
/// If the [`builder`](Self::builder) function contains a subtree that does not depend on the
/// [`listenable`](Self::listenable), it is more efficient to build that subtree once instead
/// of rebuilding it on every change of the listenable.
///
/// Performance is optimized by building the subtree once and passing it as the
/// [`child`](Self::child); the builder receives it as its third argument.
pub struct ListenableBuilder {
    pub key: Option<KeyRef>,
    /// The `Listenable` supplied to the constructor.
    ///
    /// Also accessible through `animation` for an `AnimatedBuilder`.
    pub listenable: Rc<dyn Listenable>,
    /// Called every time the [`listenable`](Self::listenable) notifies about a change.
    ///
    /// The child given to the builder should typically be part of the returned widget tree.
    pub builder: TransitionBuilder,
    /// The child widget to pass to the [`builder`](Self::builder).
    ///
    /// If a [`builder`](Self::builder) callback's return value contains a subtree that does
    /// not depend on the listenable, it's more efficient to build that subtree once instead
    /// of rebuilding it on every change of the listenable.
    ///
    /// If the pre-built subtree is passed as the [`child`](Self::child) parameter, the
    /// [`ListenableBuilder`] will pass it back to the [`builder`](Self::builder) function so
    /// that it can be incorporated into the build.
    ///
    /// Using this pre-built child is entirely optional, but can improve performance
    /// significantly in some cases and is therefore a good practice.
    pub child: Option<WidgetRef>,
}

/// Dart's `class AnimatedBuilder extends ListenableBuilder`, which only renames the
/// listenable `animation`: the same widget.
pub type AnimatedBuilder = ListenableBuilder;

impl ListenableBuilder {
    /// Creates a builder that responds to changes in `listenable`.
    pub fn new(
        listenable: Rc<dyn Listenable>,
        builder: impl Fn(&mut App, BuildContext, Option<&WidgetRef>) -> WidgetRef + 'static,
    ) -> ListenableBuilder {
        ListenableBuilder {
            key: None,
            listenable,
            builder: Rc::new(builder),
            child: None,
        }
    }

    /// Dart `ListenableBuilder(key:)`.
    pub fn key(mut self, key: KeyRef) -> ListenableBuilder {
        self.key = Some(key);
        self
    }

    /// Dart `ListenableBuilder(child:)`.
    pub fn child<K>(mut self, child: impl IntoWidget<K>) -> ListenableBuilder {
        self.child = Some(child.into_widget());
        self
    }

    /// The `Listenable` supplied to the constructor (Dart's `AnimatedBuilder.animation`).
    pub fn animation(&self) -> Rc<dyn Listenable> {
        Rc::clone(&self.listenable)
    }
}

impl Debug for ListenableBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ListenableBuilder")
            .field("child", &self.child)
            .finish_non_exhaustive()
    }
}

impl AnimatedWidget for ListenableBuilder {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn listenable(&self) -> Rc<dyn Listenable> {
        Rc::clone(&self.listenable)
    }

    fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
        (self.builder)(app, context, self.child.as_ref())
    }
}

#[cfg(test)]
mod tests {
    use std::cell::Cell;
    use std::time::Duration;
    use waft_foundation::AppCell;

    use waft_animation::{AnimationBehavior, AnimationController, CallbackAnimatable};
    use waft_embedder::Color;
    use waft_foundation::{ChangeNotifier, ValueNotifier};
    use waft_painting::{AnyColor, BoxDecoration};
    use waft_rendering::{
        BoxConstraints, ContainerRenderObjectMixin, RenderAligningShiftedBox, RenderConstrainedBox,
        RenderFractionalTranslation, RenderObjectWithChildMixin, RenderTransform,
    };
    use waft_scheduler::{Ticker, TickerCallback, TickerProvider};

    use super::*;
    use crate::framework::LeafRenderObjectWidget;
    use crate::test_harness::Harness;
    use crate::widgets::basic::{Builder, Directionality, SizedBox, Stack};

    /// flutter_test's `TestVSync`: vends ordinary tickers, in no view.
    struct TestVSync;

    impl TickerProvider for TestVSync {
        fn create_ticker(&self, app: &mut App, on_tick: TickerCallback) -> Handle<Ticker> {
            Ticker::new(app, on_tick, None)
        }
    }

    /// `SizedBox` in miniature.
    #[derive(Debug)]
    struct Sized {
        size: Size,
    }

    impl RenderObjectWidget for Sized {
        type RenderObject = RenderConstrainedBox;

        fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
            RenderConstrainedBox::new(app, BoxConstraints::tight(self.size), None).as_object()
        }

        fn update_render_object(
            &self,
            app: &mut App,
            _context: BuildContext,
            render_object: RenderHandle<RenderConstrainedBox>,
        ) {
            render_object.set_additional_constraints(app, BoxConstraints::tight(self.size));
        }
    }

    impl LeafRenderObjectWidget for Sized {}

    fn new_controller(app: &mut App) -> Handle<AnimationController> {
        AnimationController::create(
            app,
            None,
            Some(Duration::from_millis(100)),
            None,
            0.0,
            1.0,
            AnimationBehavior::Normal,
            TestVSync,
        )
    }

    fn fade(opacity: AnyAnimation<f64>) -> WidgetRef {
        FadeTransition::new(opacity)
            .child(Sized {
                size: Size::new(10.0, 10.0),
            })
            .into_widget()
    }

    fn fade_under_root(harness: &Harness, app: &App) -> RenderHandle<RenderAnimatedOpacity> {
        harness
            .render_root(app)
            .child(app)
            .expect("a child")
            .as_object()
            .downcast::<RenderAnimatedOpacity>(app)
            .expect("a RenderAnimatedOpacity")
    }

    fn root_child_size(harness: &Harness, app: &App) -> Size {
        harness
            .render_root(app)
            .child(app)
            .expect("a child")
            .size(app)
    }

    /// The render object directly under the test root, as `T`.
    fn under_root<T: waft_rendering::RenderObject>(
        harness: &Harness,
        app: &App,
    ) -> RenderHandle<T> {
        harness
            .render_root(app)
            .child(app)
            .expect("a child")
            .as_object()
            .downcast::<T>(app)
            .expect("the expected render object")
    }

    /// Under a [`Directionality`], for the widgets that resolve a directional alignment.
    fn ltr<K>(child: impl IntoWidget<K>) -> WidgetRef {
        Directionality::new(TextDirection::Ltr, child).into_widget()
    }

    fn ten_by_ten() -> Sized {
        Sized {
            size: Size::new(10.0, 10.0),
        }
    }

    fn assert_close(actual: Offset, expected: Offset) {
        assert!(
            (actual.dx() - expected.dx()).abs() < 1e-4
                && (actual.dy() - expected.dy()).abs() < 1e-4,
            "{actual:?} is not {expected:?}"
        );
    }

    // transitions_test.dart 'SlideTransition transformHitTests'
    #[test]
    fn a_slide_transition_translates_by_the_animation_and_flips_in_rtl() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller = new_controller(&mut app);
        let position = controller.drive(
            &mut app,
            CallbackAnimatable::from_callback(|t| Offset::new(t, t * 2.0)),
        );
        let slide = |text_direction: Option<TextDirection>| {
            let mut transition = SlideTransition::new(position)
                .transform_hit_tests(false)
                .child(ten_by_ten());
            if let Some(text_direction) = text_direction {
                transition = transition.text_direction(text_direction);
            }
            transition.into_widget()
        };
        let harness = Harness::mount(&mut app, slide(None));
        harness.pump(&mut app);
        controller.set_value(&mut app, 0.5);
        harness.pump(&mut app);
        let translation: RenderHandle<RenderFractionalTranslation> = under_root(&harness, &app);
        assert_eq!(translation.translation(&app), Offset::new(0.5, 1.0));
        assert!(!translation.transform_hit_tests(&app));

        harness.set_child(&mut app, slide(Some(TextDirection::Rtl)));
        harness.pump(&mut app);
        assert_eq!(translation.translation(&app), Offset::new(-0.5, 1.0));

        harness.set_child(&mut app, slide(Some(TextDirection::Ltr)));
        harness.pump(&mut app);
        assert_eq!(translation.translation(&app), Offset::new(0.5, 1.0));

        controller.dispose(&mut app);
    }

    // transitions_test.dart 'MatrixTransition animates'
    #[test]
    fn a_matrix_transition_paints_the_matrix_its_callback_returns() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller = new_controller(&mut app);
        let widget = MatrixTransition::new(controller.view(), |value| {
            Matrix4::translation(value as f32 * 100.0, 0.0)
        })
        .alignment(Alignment::TOP_LEFT)
        .child(ten_by_ten())
        .into_widget();
        let harness = Harness::mount(&mut app, widget);
        harness.pump(&mut app);
        let transform: RenderHandle<RenderTransform> = under_root(&harness, &app);
        assert_eq!(
            transform.alignment(&app),
            Some(AlignmentGeometry::Alignment(Alignment::TOP_LEFT))
        );

        controller.set_value(&mut app, 0.5);
        harness.pump(&mut app);
        let child = transform.child(&app).expect("the sized child");
        assert_close(
            child.local_to_global(&app, Offset::ZERO, None),
            Offset::new(50.0, 0.0),
        );

        controller.dispose(&mut app);
    }

    // transitions_test.dart 'ScaleTransition animates'
    #[test]
    fn a_scale_transition_scales_about_its_alignment() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller = new_controller(&mut app);
        let widget = ScaleTransition::new(controller.view())
            .child(ten_by_ten())
            .into_widget();
        let harness = Harness::mount(&mut app, widget);
        controller.set_value(&mut app, 0.5);
        harness.pump(&mut app);

        let transform: RenderHandle<RenderTransform> = under_root(&harness, &app);
        assert_eq!(
            transform.alignment(&app),
            Some(AlignmentGeometry::Alignment(Alignment::CENTER))
        );
        let child = transform.child(&app).expect("the sized child");
        // Half size about the 10x10 child's centre: its top-left lands at (2.5, 2.5).
        assert_close(
            child.local_to_global(&app, Offset::ZERO, None),
            Offset::new(2.5, 2.5),
        );

        controller.set_value(&mut app, 1.0);
        harness.pump(&mut app);
        assert_close(
            child.local_to_global(&app, Offset::ZERO, None),
            Offset::ZERO,
        );

        controller.dispose(&mut app);
    }

    // transitions_test.dart 'RotationTransition animates'
    #[test]
    fn a_rotation_transition_turns_a_full_circle_per_unit() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller = new_controller(&mut app);
        let widget = RotationTransition::new(controller.view())
            .child(ten_by_ten())
            .into_widget();
        let harness = Harness::mount(&mut app, widget);
        controller.set_value(&mut app, 0.25);
        harness.pump(&mut app);

        let transform: RenderHandle<RenderTransform> = under_root(&harness, &app);
        let child = transform.child(&app).expect("the sized child");
        // A quarter turn clockwise about the 10x10 child's centre.
        assert_close(
            child.local_to_global(&app, Offset::ZERO, None),
            Offset::new(10.0, 0.0),
        );

        controller.set_value(&mut app, 0.5);
        harness.pump(&mut app);
        assert_close(
            child.local_to_global(&app, Offset::ZERO, None),
            Offset::new(10.0, 10.0),
        );

        controller.dispose(&mut app);
    }

    // transitions_test.dart 'SizeTransition clamps negative size factors'
    #[test]
    fn a_size_transition_clips_its_child_to_the_size_factor() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller = new_controller(&mut app);
        let widget = SizeTransition::new(controller.view())
            .child(ten_by_ten())
            .into_widget();
        let harness = Harness::mount(&mut app, ltr(widget));
        harness.pump(&mut app);
        let clip: RenderHandle<waft_rendering::RenderClipRect> = under_root(&harness, &app);
        assert_eq!(clip.size(&app).height(), 0.0);

        controller.set_value(&mut app, 0.5);
        harness.pump(&mut app);
        assert_eq!(clip.size(&app).height(), 5.0);
        assert_eq!(
            clip.size(&app).width(),
            crate::test_harness::VIEW_WIDTH,
            "the cross axis is as large as the parent"
        );

        controller.set_value(&mut app, 1.0);
        harness.pump(&mut app);
        assert_eq!(clip.size(&app).height(), 10.0);

        controller.dispose(&mut app);
    }

    #[test]
    fn a_horizontal_size_transition_fixes_its_cross_axis_factor() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller = new_controller(&mut app);
        let widget = SizeTransition::new(controller.view())
            .axis(Axis::Horizontal)
            .fixed_cross_axis_size_factor(1.0)
            .child(ten_by_ten())
            .into_widget();
        let harness = Harness::mount(&mut app, ltr(widget));
        controller.set_value(&mut app, 0.5);
        harness.pump(&mut app);
        let clip: RenderHandle<waft_rendering::RenderClipRect> = under_root(&harness, &app);
        assert_eq!(clip.size(&app), Size::new(5.0, 10.0));

        controller.dispose(&mut app);
    }

    // transitions_test.dart 'PositionedTransition animates'
    #[test]
    fn a_positioned_transition_places_its_child_by_the_relative_rect() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller = new_controller(&mut app);
        let tween = RelativeRectTween::new(
            Some(RelativeRect::from_ltrb(0.0, 0.0, 200.0, 100.0)),
            Some(RelativeRect::from_ltrb(100.0, 50.0, 0.0, 0.0)),
        );
        let rect = controller.drive(&mut app, tween);
        let widget = Stack::new()
            .children([PositionedTransition::new(rect, ten_by_ten()).into_widget()])
            .into_widget();
        let harness = Harness::mount(&mut app, ltr(widget));
        controller.set_value(&mut app, 0.5);
        harness.pump(&mut app);

        let stack: RenderHandle<waft_rendering::RenderStack> = under_root(&harness, &app);
        let child = stack.first_child(&app).expect("the positioned child");
        assert_close(
            child.local_to_global(&app, Offset::ZERO, None),
            Offset::new(50.0, 25.0),
        );
        assert_eq!(child.size(&app), Size::new(150.0, 125.0));

        controller.dispose(&mut app);
    }

    // transitions_test.dart 'RelativePositionedTransition animates'
    #[test]
    fn a_relative_positioned_transition_places_its_child_inside_the_given_size() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller = new_controller(&mut app);
        let rect = controller.drive(
            &mut app,
            CallbackAnimatable::from_callback(|t| {
                Some(Rect::from_ltwh(t * 40.0, t * 20.0, 30.0, 15.0))
            }),
        );
        let widget = Stack::new()
            .children([RelativePositionedTransition::new(
                rect,
                Size::new(100.0, 100.0),
                ten_by_ten(),
            )
            .into_widget()])
            .into_widget();
        let harness = Harness::mount(&mut app, ltr(widget));
        controller.set_value(&mut app, 0.5);
        harness.pump(&mut app);

        let stack: RenderHandle<waft_rendering::RenderStack> = under_root(&harness, &app);
        let child = stack.first_child(&app).expect("the positioned child");
        // The rect (20, 10, 50, 25) inside a 100x100 box, applied to the 300x200 stack.
        assert_close(
            child.local_to_global(&app, Offset::ZERO, None),
            Offset::new(20.0, 10.0),
        );
        assert_eq!(
            child.size(&app),
            Size::new(
                crate::test_harness::VIEW_WIDTH - 20.0 - 50.0,
                crate::test_harness::VIEW_HEIGHT - 10.0 - 75.0,
            )
        );

        controller.dispose(&mut app);
    }

    /// A decoration that fades black in; `CallbackAnimatable`'s derived `Clone` asks for
    /// `Box<dyn Decoration>: Clone`, which no trait object can be.
    #[derive(Clone)]
    struct FadingBlack;

    impl Animatable<Box<dyn Decoration>> for FadingBlack {
        fn transform(&self, _app: &App, t: f64) -> Box<dyn Decoration> {
            Box::new(BoxDecoration::new().color(Color::from_argb(
                (t * 255.0).round() as i32,
                0,
                0,
                0,
            )))
        }
    }

    // transitions_test.dart 'DecoratedBoxTransition test'
    #[test]
    fn a_decorated_box_transition_hands_the_animated_decoration_to_its_render_object() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller = new_controller(&mut app);
        let decoration = controller.drive(&mut app, FadingBlack);
        let widget = DecoratedBoxTransition::new(decoration, ten_by_ten())
            .position(DecorationPosition::Foreground)
            .into_widget();
        let harness = Harness::mount(&mut app, widget);
        controller.set_value(&mut app, 0.5);
        harness.pump(&mut app);

        let decorated: RenderHandle<waft_rendering::RenderDecoratedBox> =
            under_root(&harness, &app);
        assert_eq!(decorated.position(&app), DecorationPosition::Foreground);
        let painted = (decorated.decoration(&app) as &dyn std::any::Any)
            .downcast_ref::<BoxDecoration>()
            .expect("a BoxDecoration");
        assert_eq!(
            painted.color.as_ref().map(AnyColor::color),
            Some(Color::from_argb(128, 0, 0, 0))
        );

        controller.dispose(&mut app);
    }

    // transitions_test.dart 'AlignTransition animates'
    #[test]
    fn an_align_transition_hands_the_animated_alignment_to_its_render_object() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller = new_controller(&mut app);
        let alignment = controller.drive(
            &mut app,
            CallbackAnimatable::from_callback(|t| AlignmentGeometry::xy(t, -t)),
        );
        let widget = AlignTransition::new(alignment, ten_by_ten())
            .width_factor(2.0)
            .height_factor(3.0)
            .into_widget();
        let harness = Harness::mount(&mut app, widget);
        controller.set_value(&mut app, 0.5);
        harness.pump(&mut app);

        let positioned: RenderHandle<waft_rendering::RenderPositionedBox> =
            under_root(&harness, &app);
        assert_eq!(
            RenderAligningShiftedBox::alignment(positioned, &app),
            AlignmentGeometry::xy(0.5, -0.5)
        );
        assert_eq!(positioned.width_factor(&app), Some(2.0));
        assert_eq!(positioned.height_factor(&app), Some(3.0));
        assert_eq!(positioned.size(&app), Size::new(20.0, 30.0));

        controller.dispose(&mut app);
    }

    // transitions_test.dart 'DefaultTextStyleTransition test'
    #[test]
    fn a_default_text_style_transition_publishes_the_animated_style() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller = new_controller(&mut app);
        let style = controller.drive(
            &mut app,
            CallbackAnimatable::from_callback(|t| TextStyle::new().font_size(10.0 + t * 10.0)),
        );
        let seen: Rc<Cell<f64>> = Rc::new(Cell::new(0.0));
        let child = Builder {
            key: None,
            builder: Rc::new({
                let seen = Rc::clone(&seen);
                move |app, context| {
                    let default = DefaultTextStyle::of(app, context);
                    seen.set(default.style.font_size.expect("a font size"));
                    ten_by_ten().into_widget()
                }
            }),
        };
        let widget = DefaultTextStyleTransition::new(style, child)
            .max_lines(3)
            .overflow(TextOverflow::Ellipsis)
            .soft_wrap(false)
            .into_widget();
        let harness = Harness::mount(&mut app, widget);
        controller.set_value(&mut app, 0.5);
        harness.pump(&mut app);
        assert_eq!(seen.get(), 15.0);

        controller.set_value(&mut app, 1.0);
        harness.pump(&mut app);
        assert_eq!(seen.get(), 20.0);

        controller.dispose(&mut app);
    }

    // transitions_test.dart 'FadeTransition animates'
    #[test]
    fn a_fade_transition_hands_its_animation_to_the_render_object() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller = new_controller(&mut app);
        let harness = Harness::mount(&mut app, fade(controller.view()));
        harness.pump(&mut app);
        let render_object = fade_under_root(&harness, &app);
        assert_eq!(render_object.opacity(&app), controller.view());
        assert_eq!(render_object.opacity(&app).value(&app), 0.0);
        assert!(
            !render_object.as_object().is_repaint_boundary(&app),
            "fully transparent"
        );

        for value in [0.25, 0.5, 0.75, 1.0] {
            controller.set_value(&mut app, value);
            harness.pump(&mut app);
            assert_eq!(render_object.opacity(&app).value(&app), value);
        }
        assert!(
            render_object.as_object().is_repaint_boundary(&app),
            "the render object heard the controller"
        );

        let other = new_controller(&mut app);
        harness.set_child(&mut app, fade(other.view()));
        harness.pump(&mut app);
        assert_eq!(
            fade_under_root(&harness, &app),
            render_object,
            "reconfigured in place"
        );
        assert_eq!(render_object.opacity(&app), other.view());

        controller.dispose(&mut app);
        other.dispose(&mut app);
    }

    /// An animated widget sized by a notifier's value.
    #[derive(Debug)]
    struct Grower {
        notifier: Handle<ValueNotifier<f64>>,
        builds: Rc<Cell<u32>>,
    }

    impl AnimatedWidget for Grower {
        fn listenable(&self) -> Rc<dyn Listenable> {
            Rc::new(self.notifier)
        }

        fn build(&self, app: &mut App, _context: BuildContext) -> WidgetRef {
            self.builds.set(self.builds.get() + 1);
            let width = *app.get(self.notifier).value();
            Sized {
                size: Size::new(width, 10.0),
            }
            .into_widget()
        }
    }

    fn has_listeners(app: &App, notifier: Handle<ValueNotifier<f64>>) -> bool {
        app.get(notifier).change_notifier_data().has_listeners()
    }

    #[test]
    fn an_animated_widget_rebuilds_when_its_listenable_notifies() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let notifier = app.create(ValueNotifier::new(10.0));
        let builds = Rc::new(Cell::new(0));
        let harness = Harness::mount(
            &mut app,
            Grower {
                notifier,
                builds: Rc::clone(&builds),
            }
            .into_widget(),
        );
        harness.pump(&mut app);
        assert_eq!(builds.get(), 1);
        assert!(has_listeners(&app, notifier));
        assert_eq!(root_child_size(&harness, &app), Size::new(10.0, 10.0));

        notifier.set_value(&mut app, 20.0);
        assert_eq!(builds.get(), 1, "rebuilt at the next frame, not inline");
        harness.pump(&mut app);
        assert_eq!(builds.get(), 2);
        assert_eq!(root_child_size(&harness, &app), Size::new(20.0, 10.0));
    }

    #[test]
    fn an_animated_widget_follows_a_new_listenable_and_unsubscribes_on_unmount() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let first = app.create(ValueNotifier::new(10.0));
        let second = app.create(ValueNotifier::new(30.0));
        let builds = Rc::new(Cell::new(0));
        let grower = |notifier| {
            Grower {
                notifier,
                builds: Rc::clone(&builds),
            }
            .into_widget()
        };
        let harness = Harness::mount(&mut app, grower(first));
        harness.pump(&mut app);

        harness.set_child(&mut app, grower(second));
        harness.pump(&mut app);
        assert!(
            !has_listeners(&app, first),
            "did_update_widget left the old listenable"
        );
        assert!(has_listeners(&app, second));
        assert_eq!(root_child_size(&harness, &app), Size::new(30.0, 10.0));

        harness.set_child(
            &mut app,
            Sized {
                size: Size::new(5.0, 5.0),
            }
            .into_widget(),
        );
        harness.pump(&mut app);
        assert!(!has_listeners(&app, second), "dispose removed the listener");
        let builds_before = builds.get();
        second.set_value(&mut app, 40.0);
        harness.pump(&mut app);
        assert_eq!(
            builds.get(),
            builds_before,
            "a disposed state never builds again"
        );
    }

    #[test]
    fn a_listenable_builder_rebuilds_on_notify_and_hands_back_its_child() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let notifier = app.create(ValueNotifier::new(0i32));
        let builds = Rc::new(Cell::new(0));
        let child: WidgetRef = SizedBox::square(Some(4.0)).into_widget();
        let same_child = Rc::new(Cell::new(true));
        let widget = AnimatedBuilder::new(Rc::new(notifier), {
            let builds = Rc::clone(&builds);
            let same_child = Rc::clone(&same_child);
            let child = child.clone();
            move |_app, _context, passed| {
                builds.set(builds.get() + 1);
                same_child.set(passed.is_some_and(|passed| Rc::ptr_eq(passed, &child)));
                passed.expect("the child is passed back").clone()
            }
        })
        .child(child.clone());
        let harness = Harness::mount(&mut app, widget.into_widget());
        harness.pump(&mut app);
        assert_eq!(builds.get(), 1);
        assert!(same_child.get());

        notifier.set_value(&mut app, 1);
        harness.pump(&mut app);
        assert_eq!(builds.get(), 2);
    }
}