waft-material 0.13.0

Material Design 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
//! Flutter counterpart: material_ui's `button.dart`.
//!
//! The `button_theme.dart` that Dart's file imports for its documentation waits with
//! `material_button.dart`.

use std::fmt;
use std::rc::Rc;
use std::time::Duration;

use waft_embedder::{Clip, Color, Offset, Size, TextBaseline};
use waft_foundation::{App, DiagnosticPropertiesBuilder, Handle, Listener, ValueChanged};
use waft_painting::{
    Alignment, EdgeInsets, EdgeInsetsGeometry, OutlinedBorderRef, RoundedRectangleBorder,
    ShapeBorderRef, TextStyle, force_to_point,
};
use waft_rendering::{
    AnyRenderBox, AnyRenderObject, BoxConstraints, BoxHitTestResult, BoxParentData,
    ChildLayoutHelper, ChildLayouter, PaintingContext, RenderBox, RenderBoxBase, RenderBoxData,
    RenderHandle, RenderObject, RenderObjectData, RenderObjectWithChildData,
    RenderObjectWithChildMixin, RenderShiftedBox,
};
use waft_services::MouseCursorRef;
use waft_widgets::{
    BuildContext, Center, ConstrainedBox, FocusNodeRef, IconTheme, IconThemeData, IntoWidget,
    KeyRef, Padding, RenderObjectWidget, Semantics, SingleChildRenderObjectWidget, State,
    StateData, StatefulWidget, WidgetRef, WidgetState, WidgetStateMouseCursor, WidgetStateProperty,
    WidgetStates,
};

use crate::constants::{K_MIN_INTERACTIVE_DIMENSION, K_THEME_CHANGE_DURATION};
use crate::ink_well::InkWell;
use crate::material::{Material, MaterialType};
use crate::material_state_mixin::{MaterialStateMixin, MaterialStateMixinData};
use crate::theme::Theme;
use crate::theme_data::{MaterialTapTargetSize, VisualDensity};

/// Creates a button based on `Semantics`, [`Material`], and [`InkWell`]
/// widgets.
///
/// This class does not use the current `Theme` or `ButtonTheme` to
/// compute default values for unspecified parameters. It's intended to
/// be used for custom Material buttons that optionally incorporate defaults
/// from the themes or from app-specific sources.
///
/// This class is planned to be deprecated in a future release, see
/// `ButtonStyleButton`, the base class of `ElevatedButton`, `FilledButton`,
/// `OutlinedButton` and `TextButton`.
///
/// See also:
///
///  * `ElevatedButton`, a filled button whose material elevates when pressed.
///  * `FilledButton`, a filled button that doesn't elevate when pressed.
///  * `FilledButton.tonal`, a filled button variant that uses a secondary fill color.
///  * `OutlinedButton`, a button with an outlined border and no fill color.
///  * `TextButton`, a button with no outline or fill color.
pub struct RawMaterialButton {
    pub key: Option<KeyRef>,

    /// Called when the button is tapped or otherwise activated.
    ///
    /// If this callback and [`on_long_press`](Self::on_long_press) are null, then the button
    /// will be disabled.
    ///
    /// See also:
    ///
    ///  * [`enabled`](Self::enabled), which is true if the button is enabled.
    pub on_pressed: Option<Listener>,

    /// Called when the button is long-pressed.
    ///
    /// If this callback and [`on_pressed`](Self::on_pressed) are null, then the button will be
    /// disabled.
    ///
    /// See also:
    ///
    ///  * [`enabled`](Self::enabled), which is true if the button is enabled.
    pub on_long_press: Option<Listener>,

    /// Called by the underlying [`InkWell`] widget's `InkWell.on_highlight_changed`
    /// callback.
    ///
    /// If [`on_pressed`](Self::on_pressed) changes from null to non-null while a gesture is
    /// ongoing, this can fire during the build phase (in which case calling
    /// [`State::set_state`] is not allowed).
    pub on_highlight_changed: Option<ValueChanged<bool>>,

    /// The cursor for a mouse pointer when it enters or is hovering over the
    /// button.
    ///
    /// If [`mouse_cursor`](Self::mouse_cursor) is a [`WidgetStateMouseCursor`],
    /// `WidgetStateProperty::resolve` is used for the following [`WidgetState`]s:
    ///
    ///  * [`WidgetState::Pressed`].
    ///  * [`WidgetState::Hovered`].
    ///  * [`WidgetState::Focused`].
    ///  * [`WidgetState::Disabled`].
    ///
    /// If this property is null, [`WidgetStateMouseCursor::adaptive_clickable`] will be used.
    pub mouse_cursor: Option<MouseCursorRef>,

    /// Defines the default text style, with [`Material::text_style`], for the
    /// button's [`child`](Self::child).
    ///
    /// If `TextStyle.color` is a `WidgetStateProperty<Color>`, `WidgetStateProperty::resolve`
    /// is used for the following [`WidgetState`]s:
    ///
    ///  * [`WidgetState::Pressed`].
    ///  * [`WidgetState::Hovered`].
    ///  * [`WidgetState::Focused`].
    ///  * [`WidgetState::Disabled`].
    pub text_style: Option<TextStyle>,

    /// The color of the button's [`Material`].
    pub fill_color: Option<Color>,

    /// The color for the button's [`Material`] when it has the input focus.
    pub focus_color: Option<Color>,

    /// The color for the button's [`Material`] when a pointer is hovering over it.
    pub hover_color: Option<Color>,

    /// The highlight color for the button's [`InkWell`].
    pub highlight_color: Option<Color>,

    /// The splash color for the button's [`InkWell`].
    pub splash_color: Option<Color>,

    /// The elevation for the button's [`Material`] when the button
    /// is [`enabled`](Self::enabled) but not pressed.
    ///
    /// Defaults to 2.0. The value is always non-negative.
    ///
    /// See also:
    ///
    ///  * [`highlight_elevation`](Self::highlight_elevation), the default elevation.
    ///  * [`hover_elevation`](Self::hover_elevation), the elevation when a pointer is
    ///    hovering over the button.
    ///  * [`focus_elevation`](Self::focus_elevation), the elevation when the button is
    ///    focused.
    ///  * [`disabled_elevation`](Self::disabled_elevation), the elevation when the button is
    ///    disabled.
    pub elevation: f64,

    /// The elevation for the button's [`Material`] when the button
    /// is [`enabled`](Self::enabled) and a pointer is hovering over it.
    ///
    /// Defaults to 4.0. The value is always non-negative.
    ///
    /// If the button is [`enabled`](Self::enabled), and being pressed (in the highlighted
    /// state), then the [`highlight_elevation`](Self::highlight_elevation) take precedence
    /// over the [`hover_elevation`](Self::hover_elevation).
    ///
    /// See also:
    ///
    ///  * [`elevation`](Self::elevation), the default elevation.
    ///  * [`focus_elevation`](Self::focus_elevation), the elevation when the button is
    ///    focused.
    ///  * [`disabled_elevation`](Self::disabled_elevation), the elevation when the button is
    ///    disabled.
    ///  * [`highlight_elevation`](Self::highlight_elevation), the elevation when the button
    ///    is pressed.
    pub hover_elevation: f64,

    /// The elevation for the button's [`Material`] when the button
    /// is [`enabled`](Self::enabled) and has the input focus.
    ///
    /// Defaults to 4.0. The value is always non-negative.
    ///
    /// If the button is [`enabled`](Self::enabled), and being pressed (in the highlighted
    /// state), or a mouse cursor is hovering over the button, then the
    /// [`hover_elevation`](Self::hover_elevation) and
    /// [`highlight_elevation`](Self::highlight_elevation) take precedence over the
    /// [`focus_elevation`](Self::focus_elevation).
    ///
    /// See also:
    ///
    ///  * [`elevation`](Self::elevation), the default elevation.
    ///  * [`hover_elevation`](Self::hover_elevation), the elevation when a pointer is
    ///    hovering over the button.
    ///  * [`disabled_elevation`](Self::disabled_elevation), the elevation when the button is
    ///    disabled.
    ///  * [`highlight_elevation`](Self::highlight_elevation), the elevation when the button
    ///    is pressed.
    pub focus_elevation: f64,

    /// The elevation for the button's [`Material`] when the button
    /// is [`enabled`](Self::enabled) and pressed.
    ///
    /// Defaults to 8.0. The value is always non-negative.
    ///
    /// See also:
    ///
    ///  * [`elevation`](Self::elevation), the default elevation.
    ///  * [`hover_elevation`](Self::hover_elevation), the elevation when a pointer is
    ///    hovering over the button.
    ///  * [`focus_elevation`](Self::focus_elevation), the elevation when the button is
    ///    focused.
    ///  * [`disabled_elevation`](Self::disabled_elevation), the elevation when the button is
    ///    disabled.
    pub highlight_elevation: f64,

    /// The elevation for the button's [`Material`] when the button
    /// is not [`enabled`](Self::enabled).
    ///
    /// Defaults to 0.0. The value is always non-negative.
    ///
    /// See also:
    ///
    ///  * [`elevation`](Self::elevation), the default elevation.
    ///  * [`hover_elevation`](Self::hover_elevation), the elevation when a pointer is
    ///    hovering over the button.
    ///  * [`focus_elevation`](Self::focus_elevation), the elevation when the button is
    ///    focused.
    ///  * [`highlight_elevation`](Self::highlight_elevation), the elevation when the button
    ///    is pressed.
    pub disabled_elevation: f64,

    /// The internal padding for the button's [`child`](Self::child).
    pub padding: EdgeInsetsGeometry,

    /// Defines how compact the button's layout will be.
    ///
    /// See also:
    ///
    ///  * `ThemeData::visual_density`, which specifies the [`visual_density`](Self::visual_density)
    ///    for all widgets within a `Theme`.
    pub visual_density: VisualDensity,

    /// Defines the button's size.
    ///
    /// Typically used to constrain the button's minimum size.
    pub constraints: BoxConstraints,

    /// The shape of the button's [`Material`].
    ///
    /// The button's highlight and splash are clipped to this shape. If the
    /// button has an elevation, then its drop shadow is defined by this shape.
    ///
    /// If [`shape`](Self::shape) is a [`WidgetStateOutlinedBorder`](waft_widgets::WidgetStateOutlinedBorder),
    /// `WidgetStateProperty::resolve` is used for the following [`WidgetState`]s:
    ///
    /// * [`WidgetState::Pressed`].
    /// * [`WidgetState::Hovered`].
    /// * [`WidgetState::Focused`].
    /// * [`WidgetState::Disabled`].
    pub shape: ShapeBorderRef,

    /// Defines the duration of animated changes for [`shape`](Self::shape) and
    /// [`elevation`](Self::elevation).
    ///
    /// The default value is [`K_THEME_CHANGE_DURATION`].
    pub animation_duration: Duration,

    /// The content will be clipped (or not) according to this option.
    ///
    /// Defaults to [`Clip::None`].
    pub clip_behavior: Clip,

    /// An optional focus node to use as the focus node for this widget.
    ///
    /// See `Focus::focus_node`.
    pub focus_node: Option<FocusNodeRef>,

    /// True if this widget will be selected as the initial focus when no other node in its
    /// scope is currently focused.
    ///
    /// See `Focus::autofocus`.
    pub autofocus: bool,

    /// Configures the minimum size of the tap target.
    ///
    /// Defaults to [`MaterialTapTargetSize::Padded`].
    ///
    /// See also:
    ///
    ///  * [`MaterialTapTargetSize`], for a description of how this affects tap targets.
    pub material_tap_target_size: MaterialTapTargetSize,

    /// Typically the button's label.
    pub child: Option<WidgetRef>,

    /// Whether detected gestures should provide acoustic and/or haptic feedback.
    ///
    /// For example, on Android a tap will produce a clicking sound and a
    /// long-press will produce a short vibration, when feedback is enabled.
    ///
    /// See also:
    ///
    ///  * `Feedback` for providing platform-specific feedback to certain actions.
    pub enable_feedback: bool,
}

impl RawMaterialButton {
    /// Create a button based on `Semantics`, [`Material`], and [`InkWell`] widgets.
    ///
    /// The [`elevation`](Self::elevation), [`focus_elevation`](Self::focus_elevation),
    /// [`hover_elevation`](Self::hover_elevation),
    /// [`highlight_elevation`](Self::highlight_elevation), and
    /// [`disabled_elevation`](Self::disabled_elevation) parameters must be non-negative.
    pub fn new(on_pressed: Option<Listener>) -> RawMaterialButton {
        RawMaterialButton {
            key: None,
            on_pressed,
            on_long_press: None,
            on_highlight_changed: None,
            mouse_cursor: None,
            text_style: None,
            fill_color: None,
            focus_color: None,
            hover_color: None,
            highlight_color: None,
            splash_color: None,
            elevation: 2.0,
            focus_elevation: 4.0,
            hover_elevation: 4.0,
            highlight_elevation: 8.0,
            disabled_elevation: 0.0,
            padding: EdgeInsetsGeometry::ZERO,
            visual_density: VisualDensity::STANDARD,
            constraints: BoxConstraints::new().min_width(88.0).min_height(36.0),
            shape: ShapeBorderRef::new(RoundedRectangleBorder::default()),
            animation_duration: K_THEME_CHANGE_DURATION,
            clip_behavior: Clip::None,
            focus_node: None,
            autofocus: false,
            material_tap_target_size: MaterialTapTargetSize::Padded,
            child: None,
            enable_feedback: true,
        }
    }

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

    /// Dart `RawMaterialButton(onLongPress:)`.
    pub fn on_long_press(mut self, on_long_press: Listener) -> RawMaterialButton {
        self.on_long_press = Some(on_long_press);
        self
    }

    /// Dart `RawMaterialButton(onHighlightChanged:)`.
    pub fn on_highlight_changed(
        mut self,
        on_highlight_changed: ValueChanged<bool>,
    ) -> RawMaterialButton {
        self.on_highlight_changed = Some(on_highlight_changed);
        self
    }

    /// Dart `RawMaterialButton(mouseCursor:)`.
    pub fn mouse_cursor(mut self, mouse_cursor: MouseCursorRef) -> RawMaterialButton {
        self.mouse_cursor = Some(mouse_cursor);
        self
    }

    /// Dart `RawMaterialButton(textStyle:)`.
    pub fn text_style(mut self, text_style: TextStyle) -> RawMaterialButton {
        self.text_style = Some(text_style);
        self
    }

    /// Dart `RawMaterialButton(fillColor:)`.
    pub fn fill_color(mut self, fill_color: Color) -> RawMaterialButton {
        self.fill_color = Some(fill_color);
        self
    }

    /// Dart `RawMaterialButton(focusColor:)`.
    pub fn focus_color(mut self, focus_color: Color) -> RawMaterialButton {
        self.focus_color = Some(focus_color);
        self
    }

    /// Dart `RawMaterialButton(hoverColor:)`.
    pub fn hover_color(mut self, hover_color: Color) -> RawMaterialButton {
        self.hover_color = Some(hover_color);
        self
    }

    /// Dart `RawMaterialButton(highlightColor:)`.
    pub fn highlight_color(mut self, highlight_color: Color) -> RawMaterialButton {
        self.highlight_color = Some(highlight_color);
        self
    }

    /// Dart `RawMaterialButton(splashColor:)`.
    pub fn splash_color(mut self, splash_color: Color) -> RawMaterialButton {
        self.splash_color = Some(splash_color);
        self
    }

    /// Dart `RawMaterialButton(elevation:)`.
    pub fn elevation(mut self, elevation: f64) -> RawMaterialButton {
        debug_assert!(elevation >= 0.0);
        self.elevation = elevation;
        self
    }

    /// Dart `RawMaterialButton(focusElevation:)`.
    pub fn focus_elevation(mut self, focus_elevation: f64) -> RawMaterialButton {
        debug_assert!(focus_elevation >= 0.0);
        self.focus_elevation = focus_elevation;
        self
    }

    /// Dart `RawMaterialButton(hoverElevation:)`.
    pub fn hover_elevation(mut self, hover_elevation: f64) -> RawMaterialButton {
        debug_assert!(hover_elevation >= 0.0);
        self.hover_elevation = hover_elevation;
        self
    }

    /// Dart `RawMaterialButton(highlightElevation:)`.
    pub fn highlight_elevation(mut self, highlight_elevation: f64) -> RawMaterialButton {
        debug_assert!(highlight_elevation >= 0.0);
        self.highlight_elevation = highlight_elevation;
        self
    }

    /// Dart `RawMaterialButton(disabledElevation:)`.
    pub fn disabled_elevation(mut self, disabled_elevation: f64) -> RawMaterialButton {
        debug_assert!(disabled_elevation >= 0.0);
        self.disabled_elevation = disabled_elevation;
        self
    }

    /// Dart `RawMaterialButton(padding:)`.
    pub fn padding(mut self, padding: impl Into<EdgeInsetsGeometry>) -> RawMaterialButton {
        let padding = padding.into();
        self.padding = padding;
        self
    }

    /// Dart `RawMaterialButton(visualDensity:)`.
    pub fn visual_density(mut self, visual_density: VisualDensity) -> RawMaterialButton {
        self.visual_density = visual_density;
        self
    }

    /// Dart `RawMaterialButton(constraints:)`.
    pub fn constraints(mut self, constraints: BoxConstraints) -> RawMaterialButton {
        self.constraints = constraints;
        self
    }

    /// Dart `RawMaterialButton(shape:)`.
    pub fn shape(mut self, shape: impl Into<ShapeBorderRef>) -> RawMaterialButton {
        self.shape = shape.into();
        self
    }

    /// Dart `RawMaterialButton(animationDuration:)`.
    pub fn animation_duration(mut self, animation_duration: Duration) -> RawMaterialButton {
        self.animation_duration = animation_duration;
        self
    }

    /// Dart `RawMaterialButton(clipBehavior:)`.
    pub fn clip_behavior(mut self, clip_behavior: Clip) -> RawMaterialButton {
        self.clip_behavior = clip_behavior;
        self
    }

    /// Dart `RawMaterialButton(focusNode:)`.
    pub fn focus_node(mut self, focus_node: FocusNodeRef) -> RawMaterialButton {
        self.focus_node = Some(focus_node);
        self
    }

    /// Dart `RawMaterialButton(autofocus:)`.
    pub fn autofocus(mut self, autofocus: bool) -> RawMaterialButton {
        self.autofocus = autofocus;
        self
    }

    /// Dart `RawMaterialButton(materialTapTargetSize:)`.
    pub fn material_tap_target_size(
        mut self,
        material_tap_target_size: MaterialTapTargetSize,
    ) -> RawMaterialButton {
        self.material_tap_target_size = material_tap_target_size;
        self
    }

    /// Dart `RawMaterialButton(child:)`.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn child<K>(mut self, child: impl IntoWidget<K>) -> RawMaterialButton {
        self.child = Some(child.into_widget());
        self
    }

    /// Dart `RawMaterialButton(enableFeedback:)`.
    pub fn enable_feedback(mut self, enable_feedback: bool) -> RawMaterialButton {
        self.enable_feedback = enable_feedback;
        self
    }

    /// Whether the button is enabled or disabled.
    ///
    /// Buttons are disabled by default. To enable a button, set its
    /// [`on_pressed`](Self::on_pressed) or [`on_long_press`](Self::on_long_press) properties
    /// to a non-null value.
    pub fn enabled(&self) -> bool {
        self.on_pressed.is_some() || self.on_long_press.is_some()
    }
}

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

impl StatefulWidget for RawMaterialButton {
    type State = RawMaterialButtonState;

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

    fn create_state(&self) -> RawMaterialButtonState {
        RawMaterialButtonState {
            state: StateData::new(),
            material_state: MaterialStateMixinData::new(),
        }
    }
}

/// Dart's `_RawMaterialButtonState`.
pub struct RawMaterialButtonState {
    state: StateData<RawMaterialButton>,
    material_state: MaterialStateMixinData,
}

impl RawMaterialButtonState {
    fn effective_elevation(self: Handle<Self>, app: &App) -> f64 {
        let widget = self.widget(app);
        // These conditionals are in order of precedence, so be careful about
        // reorganizing them.
        if self.is_disabled(app) {
            return widget.disabled_elevation;
        }
        if self.is_pressed(app) {
            return widget.highlight_elevation;
        }
        if self.is_hovered(app) {
            return widget.hover_elevation;
        }
        if self.is_focused(app) {
            return widget.focus_elevation;
        }
        widget.elevation
    }
}

/// Dart's `WidgetStateProperty.resolveAs<ShapeBorder?>(shape, states)`: the border a
/// [`WidgetStateOutlinedBorder`](waft_widgets::WidgetStateOutlinedBorder) resolves to, and any
/// other border itself.
fn resolve_shape(shape: &ShapeBorderRef, states: &WidgetStates) -> Option<ShapeBorderRef> {
    let Some(outlined) = shape.as_outlined_border_ref() else {
        return Some(shape.clone());
    };
    let resolved =
        <dyn WidgetStateProperty<Option<OutlinedBorderRef>>>::resolve_as(&Some(outlined), states);
    resolved.map(ShapeBorderRef::from)
}

impl MaterialStateMixin for RawMaterialButtonState {
    fn material_state_data(self: Handle<Self>, app: &App) -> &MaterialStateMixinData {
        &app.get(self).material_state
    }

    fn material_state_data_mut(self: Handle<Self>, app: &mut App) -> &mut MaterialStateMixinData {
        &mut app.get_mut(self).material_state
    }
}

impl State for RawMaterialButtonState {
    type Widget = RawMaterialButton;
    waft_widgets::state_accessors!();

    fn debug_fill_properties(
        self: Handle<Self>,
        app: &App,
        properties: &mut DiagnosticPropertiesBuilder,
    ) {
        self.material_state_data(app)
            .debug_fill_properties(properties);
    }

    fn init_state(self: Handle<Self>, app: &mut App) {
        let enabled = self.widget(app).enabled();
        self.set_material_state(app, WidgetState::Disabled, !enabled);
    }

    fn did_update_widget(self: Handle<Self>, app: &mut App, _old_widget: &RawMaterialButton) {
        let enabled = self.widget(app).enabled();
        self.set_material_state(app, WidgetState::Disabled, !enabled);
        // If the button is disabled while a press gesture is currently ongoing,
        // InkWell makes a call to handleHighlightChanged. This causes an exception
        // because it calls setState in the middle of a build. To preempt this, we
        // manually update pressed to false when this situation occurs.
        if self.is_disabled(app) && self.is_pressed(app) {
            self.remove_material_state(app, WidgetState::Pressed);
        }
    }

    fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
        // Dart reads the theme where it builds the `Material`; `widget` below borrows `app`.
        let theme = Theme::of(app, context);
        let widget = self.widget(app);
        let states = self.material_states(app);
        let effective_text_color = <dyn WidgetStateProperty<Color>>::resolve_as(
            &widget
                .text_style
                .as_ref()
                .and_then(|text_style| text_style.color.clone()),
            states,
        );
        let effective_shape = resolve_shape(&widget.shape, states);
        let density_adjustment = widget.visual_density.base_size_adjustment();
        let effective_constraints = widget
            .visual_density
            .effective_constraints(widget.constraints);
        let mouse_cursor: MouseCursorRef = widget
            .mouse_cursor
            .clone()
            .unwrap_or_else(|| Rc::new(WidgetStateMouseCursor::adaptive_clickable()));
        let effective_mouse_cursor =
            <dyn WidgetStateProperty<MouseCursorRef>>::resolve_as(&mouse_cursor, states);
        let padding = widget
            .padding
            .add(EdgeInsetsGeometry::Insets(EdgeInsets::only(
                density_adjustment.dx(),
                density_adjustment.dy(),
                density_adjustment.dx(),
                density_adjustment.dy(),
            )))
            .clamp(&EdgeInsetsGeometry::ZERO, &EdgeInsetsGeometry::INFINITY);

        let mut center = Center::new().width_factor(1.0).height_factor(1.0);
        if let Some(child) = &widget.child {
            center = center.child(child.clone());
        }
        let mut icon_theme_data = IconThemeData::new();
        if let Some(color) = effective_text_color.clone() {
            icon_theme_data = icon_theme_data.color(color);
        }
        let mut ink_well =
            InkWell::new()
                .can_request_focus(widget.enabled())
                .on_focus_change(self.update_material_state(WidgetState::Focused, None))
                .autofocus(widget.autofocus)
                .on_highlight_changed(self.update_material_state(
                    WidgetState::Pressed,
                    widget.on_highlight_changed.clone(),
                ))
                .on_hover(self.update_material_state(WidgetState::Hovered, None))
                .enable_feedback(widget.enable_feedback)
                .mouse_cursor(effective_mouse_cursor)
                .child(IconTheme::merge(
                    None,
                    icon_theme_data,
                    Padding::new(padding).child(center),
                ));
        if let Some(focus_node) = widget.focus_node.clone() {
            ink_well = ink_well.focus_node(focus_node);
        }
        if let Some(splash_color) = widget.splash_color {
            ink_well = ink_well.splash_color(splash_color);
        }
        if let Some(highlight_color) = widget.highlight_color {
            ink_well = ink_well.highlight_color(highlight_color);
        }
        if let Some(focus_color) = widget.focus_color {
            ink_well = ink_well.focus_color(focus_color);
        }
        if let Some(hover_color) = widget.hover_color {
            ink_well = ink_well.hover_color(hover_color);
        }
        if let Some(on_pressed) = widget.on_pressed.clone() {
            ink_well = ink_well.on_tap(on_pressed);
        }
        if let Some(on_long_press) = widget.on_long_press.clone() {
            ink_well = ink_well.on_long_press(on_long_press);
        }
        if let Some(effective_shape) = &effective_shape {
            ink_well = ink_well.custom_border(effective_shape.clone());
        }

        let mut material = Material::new()
            .elevation(self.effective_elevation(app))
            .r#type(if widget.fill_color.is_none() {
                MaterialType::Transparency
            } else {
                MaterialType::Button
            })
            .animation_duration(widget.animation_duration)
            .clip_behavior(widget.clip_behavior)
            .child(ink_well);
        if let Some(text_style) = &widget.text_style {
            let mut text_style = text_style.copy_with();
            if let Some(color) = effective_text_color {
                text_style = text_style.color(color);
            }
            material = material.text_style(text_style);
        }
        if let Some(effective_shape) = effective_shape {
            material = material.shape(effective_shape);
        }
        if let Some(fill_color) = widget.fill_color {
            material = material.color(fill_color);
        }
        // For compatibility during the M3 migration the default shadow needs to be passed.
        if theme.use_material3 {
            material = material.shadow_color(theme.shadow_color);
        }
        let result = ConstrainedBox::new(effective_constraints).child(material);

        let min_size = match widget.material_tap_target_size {
            MaterialTapTargetSize::Padded => {
                let min_size = Size::new(
                    K_MIN_INTERACTIVE_DIMENSION + density_adjustment.dx(),
                    K_MIN_INTERACTIVE_DIMENSION + density_adjustment.dy(),
                );
                debug_assert!(min_size.width() >= 0.0);
                debug_assert!(min_size.height() >= 0.0);
                min_size
            }
            MaterialTapTargetSize::ShrinkWrap => Size::ZERO,
        };

        Semantics::new()
            .container(true)
            .button(true)
            .enabled(widget.enabled())
            .child(InputPadding {
                child: Some(result.into_widget()),
                min_size,
            })
            .into_widget()
    }
}

/// A widget to pad the area around a `MaterialButton`'s inner [`Material`].
///
/// Redirect taps that occur in the padded area around the child to the center
/// of the child. This increases the size of the button and the button's
/// "tap target", but not its material or its ink splashes.
///
/// Dart's `_InputPadding`.
#[derive(Debug)]
struct InputPadding {
    child: Option<WidgetRef>,
    min_size: Size,
}

impl RenderObjectWidget for InputPadding {
    type RenderObject = RenderInputPadding;

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

    fn update_render_object(
        &self,
        app: &mut App,
        _context: BuildContext,
        render_object: RenderHandle<RenderInputPadding>,
    ) {
        render_object.set_min_size(app, self.min_size);
    }
}

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

/// Dart's `_RenderInputPadding`.
struct RenderInputPadding {
    render_object: RenderObjectData,
    render_box: RenderBoxData,
    child: RenderObjectWithChildData<AnyRenderBox>,
    min_size: Size,
}

impl RenderInputPadding {
    fn new(
        app: &mut App,
        min_size: Size,
        child: Option<AnyRenderBox>,
    ) -> RenderHandle<RenderInputPadding> {
        let this = RenderHandle::new_box(
            app,
            RenderInputPadding {
                render_object: RenderObjectData::new(),
                render_box: RenderBoxData::new(),
                child: RenderObjectWithChildData::new(),
                min_size,
            },
        );
        this.set_child(app, child);
        this
    }

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

    fn set_min_size(self: RenderHandle<Self>, app: &mut App, value: Size) {
        if self.min_size(app) == value {
            return;
        }
        self.get_mut(app).min_size = value;
        RenderBox::mark_needs_layout(self, app);
    }

    fn compute_size(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
        layout_child: ChildLayouter,
    ) -> Size {
        if let Some(child) = self.child(app) {
            let min_size = self.min_size(app);
            let child_size = layout_child(app, child, constraints);
            let width = child_size.width().max(min_size.width());
            let height = child_size.height().max(min_size.height());
            return constraints.constrain(Size::new(width, height));
        }
        Size::ZERO
    }
}

impl RenderObjectWithChildMixin for RenderInputPadding {
    type ChildType = AnyRenderBox;

    fn child_data(self: RenderHandle<Self>, app: &App) -> &RenderObjectWithChildData<AnyRenderBox> {
        &self.get(app).child
    }

    fn child_data_mut(
        self: RenderHandle<Self>,
        app: &mut App,
    ) -> &mut RenderObjectWithChildData<AnyRenderBox> {
        &mut self.get_mut(app).child
    }
}

impl RenderShiftedBox for RenderInputPadding {}

impl RenderObject for RenderInputPadding {
    waft_rendering::render_object_accessors!();

    fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
        let constraints = self.constraints(app);
        let size = self.compute_size(app, constraints, ChildLayoutHelper::layout_child);
        self.set_size(app, size);
        if let Some(child) = self.child(app) {
            let child_size = child.size(app);
            child.parent_data_of_mut::<BoxParentData>(app).offset =
                Alignment::CENTER.along_offset(size - child_size);
        }
    }

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

    fn visit_children(
        self: RenderHandle<Self>,
        app: &App,
        visitor: &mut dyn FnMut(AnyRenderObject),
    ) {
        if let Some(child) = self.child(app) {
            visitor(child.as_object());
        }
    }
}

impl RenderBox for RenderInputPadding {
    waft_rendering::render_box_accessors!();

    fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        if let Some(child) = self.child(app) {
            return child
                .get_min_intrinsic_width(app, height)
                .max(self.min_size(app).width());
        }
        0.0
    }

    fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        if let Some(child) = self.child(app) {
            return child
                .get_min_intrinsic_height(app, width)
                .max(self.min_size(app).height());
        }
        0.0
    }

    fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        if let Some(child) = self.child(app) {
            return child
                .get_max_intrinsic_width(app, height)
                .max(self.min_size(app).width());
        }
        0.0
    }

    fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        if let Some(child) = self.child(app) {
            return child
                .get_max_intrinsic_height(app, width)
                .max(self.min_size(app).height());
        }
        0.0
    }

    fn compute_dry_layout(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
    ) -> Size {
        self.compute_size(app, constraints, ChildLayoutHelper::dry_layout_child)
    }

    fn compute_dry_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
        baseline: TextBaseline,
    ) -> Option<f64> {
        let child = self.child(app)?;
        let result = child.get_dry_baseline(app, constraints, baseline)?;
        let child_size = child.get_dry_layout(app, constraints);
        let size = self.get_dry_layout(app, constraints);
        Some(result + Alignment::CENTER.along_offset(size - child_size).dy())
    }

    fn compute_distance_to_actual_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        baseline: TextBaseline,
    ) -> Option<f64> {
        RenderShiftedBox::compute_distance_to_actual_baseline(self, app, baseline)
    }

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

    fn hit_test(
        self: RenderHandle<Self>,
        app: &mut App,
        result: &mut BoxHitTestResult<'_>,
        position: Offset,
    ) -> bool {
        if RenderBoxBase::hit_test(self, app, result, position) {
            return true;
        }
        let child = self.child(app).expect("a hit on the padding needs a child");
        let center = child.size(app).center(Offset::ZERO);
        result.add_with_raw_transform(Some(force_to_point(center)), center, |result, position| {
            debug_assert!(
                (position.dx() - center.dx()).abs() <= center.dx().abs() * f32::EPSILON as f64
                    && (position.dy() - center.dy()).abs()
                        <= center.dy().abs() * f32::EPSILON as f64,
                "the forced transform maps every hit to the child's center"
            );
            child.hit_test(app, result, center)
        })
    }
}

#[cfg(test)]
mod tests {
    use std::any::Any;
    use std::cell::Cell;

    use waft_embedder::{Rect, TextDirection};
    use waft_gestures::{GestureBinding, HitTestResult};
    use waft_rendering::BoxHitTestEntry;
    use waft_services::SystemMouseCursors;
    use waft_widgets::{
        Actions, ActivateIntent, Builder, ColoredBox, Column, CrossAxisAlignment, Directionality,
        FocusHighlightStrategy, FocusManager, FocusNode, FocusScope, GlobalKey, MouseRegion,
        SizedBox, Text,
    };

    use waft_widgets::testing::{TestPointer, WidgetTester, test_widgets};

    use super::*;
    use crate::test_support::{PaintInspection, path_like};
    use crate::theme_data::ThemeData;
    use waft_rendering::testing::RecordedCall;

    fn noop() -> Option<Listener> {
        Some(Listener::new(|_app| {}))
    }

    /// `Theme(data: ThemeData(useMaterial3: false), child: Directionality(ltr, child))`.
    fn m2_ltr<K>(tester: &WidgetTester, child: impl IntoWidget<K>) -> WidgetRef {
        let data = ThemeData::builder()
            .use_material3(false)
            .build(&tester.cell.borrow());
        Theme::new(data, Directionality::new(TextDirection::Ltr, child)).into_widget()
    }

    fn ltr<K>(child: impl IntoWidget<K>) -> WidgetRef {
        Directionality::new(TextDirection::Ltr, child).into_widget()
    }

    fn center_of_view() -> Offset {
        WidgetTester::SIZE.center(Offset::ZERO)
    }

    fn set_highlight_strategy(tester: &WidgetTester, strategy: FocusHighlightStrategy) {
        let mut app = tester.cell.borrow_mut();
        FocusManager::instance(&mut app).set_highlight_strategy(&mut app, strategy);
    }

    fn focus_node(tester: &WidgetTester) -> FocusNodeRef {
        FocusNode::new(&mut tester.cell.borrow_mut()).into_erased()
    }

    /// Dart's `Text(text)`.
    fn label(text: &str) -> Text {
        Text::new(text)
    }

    fn red_square() -> SizedBox {
        SizedBox::new()
            .width(100.0)
            .height(100.0)
            .child(ColoredBox::new(Color::new(0xffff0000)))
    }

    /// The colors of the rects the first material's ink features draw.
    fn ink_rect_colors(tester: &WidgetTester) -> Vec<u32> {
        tester
            .ink_calls()
            .into_iter()
            .filter_map(|drawn| match drawn {
                RecordedCall::Rect { color, .. } => Some(color.to_argb32()),
                _ => None,
            })
            .collect()
    }

    /// The colors and bounds of the circles (paths) the first material's ink features draw.
    fn ink_circles(tester: &WidgetTester) -> Vec<(u32, Rect)> {
        tester
            .ink_calls()
            .into_iter()
            .filter_map(|drawn| {
                path_like(&drawn).map(|(color, bounds)| (color.to_argb32(), bounds))
            })
            .collect()
    }

    /// Whether the first material draws a circle of the color `rgb`, whatever its alpha.
    fn has_splash(tester: &WidgetTester, rgb: u32) -> bool {
        ink_circles(tester)
            .iter()
            .any(|(color, _)| color & 0x00ff_ffff == rgb)
    }

    /// Dart's `finder.hitTestable()` for the render object of `key`'s widget: it is on the path
    /// of a hit test at its own center.
    fn is_hit_testable(tester: &WidgetTester, key: &GlobalKey) -> bool {
        let render = tester.render_object(key);
        let render_box = render.as_box().expect("a render box");
        let mut app = tester.cell.borrow_mut();
        let center =
            render_box.local_to_global(&app, render_box.size(&app).center(Offset::ZERO), None);
        let view = app.platform().views().into_iter().next().expect("one view");
        let mut result = HitTestResult::new();
        GestureBinding::instance(&mut app).hit_test_in_view(
            &mut app,
            &mut result,
            center,
            view.id(),
        );
        result.path().iter().any(|entry| {
            (entry.target() as &dyn Any)
                .downcast_ref::<BoxHitTestEntry>()
                .is_some_and(|hit| hit.target().as_object() == render)
        })
    }

    fn rect_of(tester: &WidgetTester, key: &GlobalKey) -> Rect {
        let render_box = tester.render_object(key).as_box().expect("a render box");
        let app = tester.cell.borrow();
        let origin = render_box.local_to_global(&app, Offset::ZERO, None);
        origin & render_box.size(&app)
    }

    #[test]
    fn raw_material_button_responds_when_tapped() {
        test_widgets(|tester| {
            let pressed = Rc::new(Cell::new(false));
            let splash_color = Color::new(0xff00ff00);
            tester.pump_widget(m2_ltr(
                tester,
                Center::new().child(
                    RawMaterialButton::new(Some(Listener::new({
                        let pressed = Rc::clone(&pressed);
                        move |_app| pressed.set(true)
                    })))
                    .splash_color(splash_color)
                    .child(label("BUTTON")),
                ),
            ));

            tester.tap_at(center_of_view(), 1);
            tester.pump_by(Duration::from_millis(10));
            // valo records no circle of radius 0, which the splash has until its first
            // frame after this one; by then it has begun to fade.
            tester.pump_by(Duration::from_millis(16));

            assert!(has_splash(tester, 0x00ff00));

            tester.pump_and_settle();

            assert!(pressed.get());
        });
    }

    /// Dart sends the shortcuts' keys; here the intent is invoked at a context under the
    /// button, where the focused `Shortcuts` would invoke it.
    #[test]
    fn raw_material_button_responds_to_shortcut_when_activated() {
        test_widgets(|tester| {
            let pressed = Rc::new(Cell::new(false));
            let inner: Rc<Cell<Option<BuildContext>>> = Rc::default();
            let focus_node = focus_node(tester);
            tester.pump_widget(m2_ltr(
                tester,
                Center::new().child(
                    RawMaterialButton::new(Some(Listener::new({
                        let pressed = Rc::clone(&pressed);
                        move |_app| pressed.set(true)
                    })))
                    .splash_color(Color::new(0xff00ff00))
                    .focus_node(focus_node.clone())
                    .child(Builder::new({
                        let inner = Rc::clone(&inner);
                        move |_app, context| {
                            inner.set(Some(context));
                            SizedBox::shrink().into_widget()
                        }
                    })),
                ),
            ));
            focus_node.request_focus(&mut tester.cell.borrow_mut(), None);
            tester.pump();
            let context = inner.get().expect("built");

            for _ in 0..2 {
                pressed.set(false);
                Actions::invoke(&mut tester.cell.borrow_mut(), context, &ActivateIntent);
                tester.pump_by(Duration::from_millis(10));
                // valo records no circle of radius 0, which the splash has until its first
                // frame after this one.
                tester.pump_by(Duration::from_millis(16));
                assert!(has_splash(tester, 0x00ff00));
                tester.pump_and_settle();
                assert!(pressed.get());
            }
        });
    }

    #[test]
    fn material_tap_target_size_padded_expands_hit_test_area() {
        test_widgets(|tester| {
            let pressed = Rc::new(Cell::new(0));
            tester.pump_widget(ltr(RawMaterialButton::new(Some(Listener::new({
                let pressed = Rc::clone(&pressed);
                move |_app| pressed.set(pressed.get() + 1)
            })))
            .constraints(BoxConstraints::tight(Size::new(10.0, 10.0)))
            .material_tap_target_size(MaterialTapTargetSize::Padded)
            .child(label("+"))));

            tester.tap_at(Offset::new(40.0, 400.0), 1);

            assert_eq!(pressed.get(), 1);
        });
    }

    fn splash_case(tester: &mut WidgetTester, at: impl Fn(Offset) -> Offset) -> Vec<(u32, Rect)> {
        let splash_color = Color::new(0xAA0000FF);
        tester.pump_widget(m2_ltr(
            tester,
            Center::new().child(
                RawMaterialButton::new(noop())
                    .material_tap_target_size(MaterialTapTargetSize::Padded)
                    .fill_color(Color::new(0xFFEF5350))
                    .highlight_color(Color::new(0xAAFF0000))
                    .splash_color(splash_color)
                    .child(SizedBox::new()),
            ),
        ));
        let mut gesture = TestPointer::touch(1);
        tester.down(&mut gesture, at(center_of_view()));
        tester.pump(); // start gesture
        tester.pump_by(Duration::from_millis(200)); // wait for splash to be well under way
        let circles = ink_circles(tester);
        tester.up(&gesture);
        circles
    }

    #[test]
    fn ink_splash_from_center_tap_originates_in_correct_location() {
        test_widgets(|tester| {
            let circles = splash_case(tester, |center| center);
            // centered in material button.
            assert!(
                circles.iter().any(|(color, bounds)| *color == 0xAA0000FF
                    && bounds.center() == Offset::new(44.0, 18.0)),
                "{circles:?}"
            );
        });
    }

    #[test]
    fn ink_splash_from_tap_above_material_originates_in_correct_location() {
        test_widgets(|tester| {
            // The top center of the button's 88x36 material, which is centered in the view.
            let circles = splash_case(tester, |center| center - Offset::new(0.0, 18.0));
            // paints above material
            assert!(
                circles.iter().any(|(color, bounds)| *color == 0xAA0000FF
                    && bounds.center() == Offset::new(44.0, 0.0)),
                "{circles:?}"
            );
        });
    }

    /// Not in Flutter's file, whose version of this case taps where the root's tight
    /// constraints put the button anyway: a tap in the padding around a 10x10 material reaches
    /// it, and the semantics case's 48x48 size is the padding's. Like Dart's, the redirect does not
    /// check the position is in bounds.
    #[test]
    fn a_tap_in_the_padding_around_the_material_reaches_the_button() {
        test_widgets(|tester| {
            let key = GlobalKey::new();
            let pressed = Rc::new(Cell::new(0));
            tester.pump_widget(m2_ltr(
                tester,
                Center::new().child(
                    RawMaterialButton::new(Some(Listener::new({
                        let pressed = Rc::clone(&pressed);
                        move |_app| pressed.set(pressed.get() + 1)
                    })))
                    .key(key.clone().into())
                    .constraints(BoxConstraints::tight(Size::new(10.0, 10.0)))
                    .material_tap_target_size(MaterialTapTargetSize::Padded)
                    .child(label("+")),
                ),
            ));
            assert_eq!(tester.get_size(&key), Size::new(48.0, 48.0));

            tester.tap_at(center_of_view() + Offset::new(20.0, 0.0), 1);
            assert_eq!(pressed.get(), 1);
        });
    }

    #[test]
    fn off_center_child_is_hit_testable() {
        test_widgets(|tester| {
            let key = GlobalKey::new();
            tester.pump_widget(m2_ltr(
                tester,
                Column::new().children([RawMaterialButton::new(noop())
                    .material_tap_target_size(MaterialTapTargetSize::Padded)
                    .child(
                        SizedBox::square(Some(400.0)).child(
                            Column::new()
                                .main_axis_alignment(waft_rendering::MainAxisAlignment::End)
                                .children([SizedBox::new()
                                    .height(50.0)
                                    .width(400.0)
                                    .child(label("Material").key(key.clone().into()))
                                    .into_widget()]),
                        ),
                    )
                    .into_widget()]),
            ));
            assert!(is_hit_testable(tester, &key));
        });
    }

    #[test]
    fn smaller_child_is_hit_testable() {
        test_widgets(|tester| {
            let key = GlobalKey::new();
            tester.pump_widget(m2_ltr(
                tester,
                Column::new().children([RawMaterialButton::new(noop())
                    .material_tap_target_size(MaterialTapTargetSize::Padded)
                    .child(
                        SizedBox::new()
                            .key(key.clone().into())
                            .width(8.0)
                            .height(8.0)
                            .child(ColoredBox::new(Color::new(0xFFAABBCC))),
                    )
                    .into_widget()]),
            ));
            assert!(is_hit_testable(tester, &key));
        });
    }

    #[test]
    fn raw_material_button_can_be_expanded_by_parent_constraints() {
        test_widgets(|tester| {
            let key = GlobalKey::new();
            tester.pump_widget(m2_ltr(
                tester,
                Column::new()
                    .cross_axis_alignment(CrossAxisAlignment::Stretch)
                    .children([RawMaterialButton::new(noop())
                        .key(key.clone().into())
                        .child(SizedBox::new())
                        .into_widget()]),
            ));

            assert_eq!(tester.get_size(&key), Size::new(800.0, 48.0));
        });
    }

    #[test]
    fn raw_material_button_handles_focus() {
        test_widgets(|tester| {
            let focus_node = focus_node(tester);
            let focus_color = Color::new(0xff00ff00);

            set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTraditional);
            tester.pump_widget(m2_ltr(
                tester,
                Center::new().child(
                    RawMaterialButton::new(noop())
                        .focus_node(focus_node.clone())
                        .focus_color(focus_color)
                        .child(red_square()),
                ),
            ));
            assert!(!ink_rect_colors(tester).contains(&0xff00ff00));

            focus_node.request_focus(&mut tester.cell.borrow_mut(), None);
            tester.pump_and_settle();

            assert!(ink_rect_colors(tester).contains(&0xff00ff00));
        });
    }

    #[test]
    fn raw_material_button_loses_focus_when_disabled() {
        test_widgets(|tester| {
            let focus_node = focus_node(tester);
            tester.pump_widget(m2_ltr(
                tester,
                Center::new().child(
                    RawMaterialButton::new(noop())
                        .autofocus(true)
                        .focus_node(focus_node.clone())
                        .child(red_square()),
                ),
            ));

            tester.pump();
            assert!(focus_node.has_primary_focus(&tester.cell.borrow()));

            tester.pump_widget(m2_ltr(
                tester,
                Center::new().child(
                    RawMaterialButton::new(None)
                        .focus_node(focus_node.clone())
                        .child(red_square()),
                ),
            ));

            tester.pump();
            assert!(!focus_node.has_primary_focus(&tester.cell.borrow()));
        });
    }

    #[test]
    fn disabled_raw_material_button_cant_be_traversed_to() {
        test_widgets(|tester| {
            let focus_node1 = focus_node(tester);
            let focus_node2 = focus_node(tester);
            tester.pump_widget(m2_ltr(
                tester,
                FocusScope::new(
                    Center::new().child(
                        Column::new().children([
                            RawMaterialButton::new(noop())
                                .autofocus(true)
                                .focus_node(focus_node1.clone())
                                .child(red_square())
                                .into_widget(),
                            RawMaterialButton::new(None)
                                .autofocus(true)
                                .focus_node(focus_node2.clone())
                                .child(red_square())
                                .into_widget(),
                        ]),
                    ),
                ),
            ));
            tester.pump();

            assert!(focus_node1.has_primary_focus(&tester.cell.borrow()));
            assert!(!focus_node2.has_primary_focus(&tester.cell.borrow()));

            assert!(!focus_node1.next_focus(&mut tester.cell.borrow_mut()));
            tester.pump();

            assert!(focus_node1.has_primary_focus(&tester.cell.borrow()));
            assert!(!focus_node2.has_primary_focus(&tester.cell.borrow()));
        });
    }

    #[test]
    fn raw_material_button_handles_hover() {
        test_widgets(|tester| {
            let key = GlobalKey::new();
            let hover_color = Color::new(0xff00ff00);

            set_highlight_strategy(tester, FocusHighlightStrategy::AlwaysTraditional);
            tester.pump_widget(m2_ltr(
                tester,
                Center::new().child(
                    RawMaterialButton::new(noop())
                        .key(key.clone().into())
                        .hover_color(hover_color)
                        .hover_elevation(10.5)
                        .child(red_square()),
                ),
            ));
            let mut mouse = TestPointer::mouse(1, 0);
            tester.add_pointer(&mut mouse, Offset::ZERO);
            assert!(!ink_rect_colors(tester).contains(&0xff00ff00));

            tester.move_to(&mut mouse, tester.get_center(&key), false);
            tester.pump_and_settle();

            assert!(ink_rect_colors(tester).contains(&0xff00ff00));
        });
    }

    #[test]
    fn raw_material_button_on_pressed_and_on_long_press_callbacks_are_correctly_called_when_non_null()
     {
        test_widgets(|tester| {
            let key = GlobalKey::new();
            let was_pressed = Rc::new(Cell::new(false));
            let was_pressed_listener = || {
                let was_pressed = Rc::clone(&was_pressed);
                Listener::new(move |_app| was_pressed.set(true))
            };
            // Returns the button's `enabled`, which Dart reads off `tester.widget`.
            let build_frame = |tester: &mut WidgetTester,
                               on_pressed: Option<Listener>,
                               on_long_press: Option<Listener>|
             -> bool {
                let mut button = RawMaterialButton::new(on_pressed)
                    .key(key.clone().into())
                    .child(label("button"));
                button.on_long_press = on_long_press;
                let enabled = button.enabled();
                tester.pump_widget(ltr(button));
                enabled
            };

            // onPressed not null, onLongPress null.
            was_pressed.set(false);
            assert!(build_frame(tester, Some(was_pressed_listener()), None));
            tester.tap_at(tester.get_center(&key), 1);
            assert!(was_pressed.get());

            // onPressed null, onLongPress not null.
            was_pressed.set(false);
            assert!(build_frame(tester, None, Some(was_pressed_listener())));
            tester.long_press_at(tester.get_center(&key), 2);
            assert!(was_pressed.get());

            // onPressed null, onLongPress null.
            assert!(!build_frame(tester, None, None));
        });
    }

    #[test]
    fn raw_material_button_on_pressed_and_on_long_press_callbacks_are_distinctly_recognized() {
        test_widgets(|tester| {
            let key = GlobalKey::new();
            let did_press_button = Rc::new(Cell::new(false));
            let did_long_press_button = Rc::new(Cell::new(false));
            tester.pump_widget(ltr(RawMaterialButton::new(Some(Listener::new({
                let did_press_button = Rc::clone(&did_press_button);
                move |_app| did_press_button.set(true)
            })))
            .key(key.clone().into())
            .on_long_press(Listener::new({
                let did_long_press_button = Rc::clone(&did_long_press_button);
                move |_app| did_long_press_button.set(true)
            }))
            .child(label("button"))));

            assert!(!did_press_button.get());
            tester.tap_at(tester.get_center(&key), 1);
            assert!(did_press_button.get());

            assert!(!did_long_press_button.get());
            tester.long_press_at(tester.get_center(&key), 2);
            assert!(did_long_press_button.get());
        });
    }

    #[test]
    fn raw_material_button_responds_to_density_changes() {
        test_widgets(|tester| {
            let key = GlobalKey::new();
            let child_key = GlobalKey::new();

            let build_test =
                |tester: &mut WidgetTester, visual_density: VisualDensity, use_text: bool| {
                    let child: WidgetRef = if use_text {
                        label("Text").key(child_key.clone().into()).into_widget()
                    } else {
                        SizedBox::new()
                            .key(child_key.clone().into())
                            .width(100.0)
                            .height(100.0)
                            .child(ColoredBox::new(Color::new(0xffff0000)))
                            .into_widget()
                    };
                    let data = ThemeData::builder()
                        .use_material3(false)
                        .build(&tester.cell.borrow());
                    tester.pump_widget(
                        Theme::new(
                            data,
                            Directionality::new(
                                TextDirection::Rtl,
                                Center::new().child(
                                    RawMaterialButton::new(noop())
                                        .visual_density(visual_density)
                                        .key(key.clone().into())
                                        .child(child),
                                ),
                            ),
                        )
                        .into_widget(),
                    );
                };

            build_test(tester, VisualDensity::STANDARD, false);
            tester.pump_and_settle();
            assert_eq!(tester.get_size(&key), Size::new(100.0, 100.0));
            assert_eq!(
                rect_of(tester, &child_key),
                Rect::from_ltrb(350.0, 250.0, 450.0, 350.0)
            );

            build_test(tester, VisualDensity::new(3.0, 3.0), false);
            tester.pump_and_settle();
            assert_eq!(tester.get_size(&key), Size::new(124.0, 124.0));
            assert_eq!(
                rect_of(tester, &child_key),
                Rect::from_ltrb(350.0, 250.0, 450.0, 350.0)
            );

            build_test(tester, VisualDensity::new(-3.0, -3.0), false);
            tester.pump_and_settle();
            assert_eq!(tester.get_size(&key), Size::new(100.0, 100.0));
            assert_eq!(
                rect_of(tester, &child_key),
                Rect::from_ltrb(350.0, 250.0, 450.0, 350.0)
            );

            build_test(tester, VisualDensity::STANDARD, true);
            tester.pump_and_settle();
            assert_eq!(tester.get_size(&key), Size::new(88.0, 48.0));
            assert_eq!(
                rect_of(tester, &child_key),
                Rect::from_ltrb(372.0, 293.0, 428.0, 307.0)
            );

            build_test(tester, VisualDensity::new(3.0, 3.0), true);
            tester.pump_and_settle();
            assert_eq!(tester.get_size(&key), Size::new(100.0, 60.0));
            assert_eq!(
                rect_of(tester, &child_key),
                Rect::from_ltrb(372.0, 293.0, 428.0, 307.0)
            );

            build_test(tester, VisualDensity::new(-3.0, -3.0), true);
            tester.pump_and_settle();
            assert_eq!(tester.get_size(&key), Size::new(76.0, 36.0));
            assert_eq!(
                rect_of(tester, &child_key),
                Rect::from_ltrb(372.0, 293.0, 428.0, 307.0)
            );
        });
    }

    fn active_cursor(tester: &WidgetTester) -> MouseCursorRef {
        let mut app = tester.cell.borrow_mut();
        let tracker = waft_rendering::RendererBinding::instance(&mut app).mouse_tracker(&app);
        tracker
            .debug_device_active_cursor(&app, 1)
            .expect("the mouse is over the view")
    }

    #[test]
    fn raw_material_button_changes_mouse_cursor_as_expected_when_hovered() {
        test_widgets(|tester| {
            let in_region = |child: RawMaterialButton| {
                ltr(MouseRegion::new()
                    .cursor(SystemMouseCursors::FORBIDDEN.into())
                    .child(child))
            };
            tester.pump_widget(in_region(
                RawMaterialButton::new(noop()).mouse_cursor(SystemMouseCursors::TEXT.into()),
            ));
            let mut mouse = TestPointer::mouse(1, 0);
            tester.add_pointer(&mut mouse, Offset::ZERO);
            tester.pump();

            let text: MouseCursorRef = SystemMouseCursors::TEXT.into();
            assert!(*active_cursor(tester) == *text);

            // Test default cursor
            tester.pump_widget(in_region(RawMaterialButton::new(noop())));
            let basic: MouseCursorRef = SystemMouseCursors::BASIC.into();
            assert!(*active_cursor(tester) == *basic);

            // Test default cursor when disabled
            tester.pump_widget(in_region(RawMaterialButton::new(None)));
            assert!(*active_cursor(tester) == *basic);
        });
    }

    /// Not in Flutter's file: `resolve_shape` stands for its `resolveAs<ShapeBorder?>`.
    #[test]
    fn a_widget_state_outlined_border_resolves_to_the_border_of_the_states() {
        let enabled = RoundedRectangleBorder::default();
        let disabled = RoundedRectangleBorder {
            border_radius: waft_painting::BorderRadius::circular(8.0).into(),
            ..RoundedRectangleBorder::default()
        };
        let property = waft_widgets::WidgetStateOutlinedBorder::resolve_with({
            move |states| {
                let border = if states.contains(&WidgetState::Disabled) {
                    disabled
                } else {
                    enabled
                };
                Some(OutlinedBorderRef::new(border))
            }
        });

        let resolved = |shape: &ShapeBorderRef, states: &[WidgetState]| {
            resolve_shape(shape, &states.iter().copied().collect()).expect("a border")
        };

        let property = ShapeBorderRef::new(property);
        assert!(resolved(&property, &[]).eq_shape(&enabled));
        assert!(resolved(&property, &[WidgetState::Disabled]).eq_shape(&disabled));
        // A border that is not a property is its own value.
        let disabled = ShapeBorderRef::new(disabled);
        assert_eq!(resolved(&disabled, &[]), disabled);
    }

    #[test]
    fn raw_material_button_does_not_crash_at_zero_area() {
        test_widgets(|tester| {
            let key = GlobalKey::new();
            tester.pump_widget(m2_ltr(
                tester,
                Center::new().child(
                    SizedBox::shrink()
                        .child(RawMaterialButton::new(noop()).key(key.clone().into())),
                ),
            ));
            assert_eq!(tester.get_size(&key), Size::ZERO);
        });
    }
}