azul-layout 0.0.13

Layout solver + font and image loader the Azul GUI framework
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
//! Switch (toggle) widget — a boolean on/off control rendered as a rounded,
//! pill-shaped "track" with a sliding circular "knob". A near-clone of
//! [`crate::widgets::check_box::CheckBox`] (boolean state + an `on_toggle`
//! callback) restyled as a switch: toggling flips the knob's horizontal
//! position (via `margin-left`) and the track's background colour.
//!
//! Key types: [`Switch`], [`SwitchState`], [`SwitchOnToggle`].

use azul_core::{
    callbacks::{CoreCallbackData, Update},
    dom::{Dom, IdOrClass, IdOrClass::Class, IdOrClassVec, TabIndex},
    refany::RefAny,
};
use azul_css::dynamic_selector::{CssPropertyWithConditions, CssPropertyWithConditionsVec};
use azul_css::{
    props::{
        basic::{color::ColorU, *},
        layout::{LayoutDisplay, LayoutFlexDirection, LayoutAlignItems, LayoutAlignSelf, LayoutFlexGrow, LayoutWidth, LayoutHeight, LayoutPaddingLeft, LayoutPaddingRight, LayoutPaddingTop, LayoutPaddingBottom, LayoutMarginLeft},
        property::{CssProperty, *},
        style::{StyleBackgroundContent, StyleBackgroundContentVec, StyleBorderTopLeftRadius, StyleBorderTopRightRadius, StyleBorderBottomLeftRadius, StyleBorderBottomRightRadius, StyleCursor},
    },
    impl_option_inner, AzString,
};

use crate::callbacks::{Callback, CallbackInfo};

static SWITCH_TRACK_CLASS: &[IdOrClass] =
    &[Class(AzString::from_const_str("__azul-native-switch"))];
static SWITCH_KNOB_CLASS: &[IdOrClass] =
    &[Class(AzString::from_const_str("__azul-native-switch-knob"))];

/// Callback function type invoked when the switch is toggled.
pub type SwitchOnToggleCallbackType = extern "C" fn(RefAny, CallbackInfo, SwitchState) -> Update;
impl_widget_callback!(
    SwitchOnToggle,
    OptionSwitchOnToggle,
    SwitchOnToggleCallback,
    SwitchOnToggleCallbackType
);

azul_core::impl_managed_callback! {
    wrapper:        SwitchOnToggleCallback,
    info_ty:        CallbackInfo,
    return_ty:      Update,
    default_ret:    Update::DoNothing,
    invoker_static: SWITCH_ON_TOGGLE_INVOKER,
    invoker_ty:     AzSwitchOnToggleCallbackInvoker,
    thunk_fn:       az_switch_on_toggle_callback_thunk,
    setter_fn:      AzApp_setSwitchOnToggleCallbackInvoker,
    from_handle_fn: AzSwitchOnToggleCallback_createFromHostHandle,
    extra_args:     [ state: SwitchState ],
}

/// A toggleable on/off switch widget with a sliding knob and toggle callback.
#[derive(Debug, Clone, PartialEq, Eq)]
#[repr(C)]
pub struct Switch {
    pub switch_state: SwitchStateWrapper,
    /// Style for the switch track (the pill-shaped container)
    pub track_style: CssPropertyWithConditionsVec,
    /// Style for the sliding knob
    pub knob_style: CssPropertyWithConditionsVec,
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[repr(C)]
pub struct SwitchStateWrapper {
    /// On/off state of this Switch
    pub inner: SwitchState,
    /// Optional: function to call when the Switch is toggled
    pub on_toggle: OptionSwitchOnToggle,
}

/// The on/off state of a [`Switch`].
#[derive(Copy, Debug, Default, Clone, PartialEq, Eq)]
#[repr(C)]
pub struct SwitchState {
    /// `true` = on (knob slid right), `false` = off (knob at left)
    pub checked: bool,
}

// ---- dimensions ----
const TRACK_WIDTH: isize = 36;
const TRACK_HEIGHT: isize = 20;
const TRACK_PADDING: isize = 2;
const TRACK_RADIUS: isize = 10;
const KNOB_SIZE: isize = 16;
const KNOB_RADIUS: isize = 8;
/// Horizontal travel of the knob = `track_width` − 2·padding − `knob_size`.
const KNOB_TRAVEL: isize = TRACK_WIDTH - (2 * TRACK_PADDING) - KNOB_SIZE;

// ---- colours ----
const TRACK_OFF_COLOR: ColorU = ColorU {
    r: 204,
    g: 204,
    b: 204,
    a: 255,
}; // #cccccc
const TRACK_ON_COLOR: ColorU = ColorU {
    r: 76,
    g: 217,
    b: 100,
    a: 255,
}; // #4cd964
const KNOB_COLOR: ColorU = ColorU {
    r: 255,
    g: 255,
    b: 255,
    a: 255,
}; // white

const TRACK_OFF_BG_ITEMS: &[StyleBackgroundContent] =
    &[StyleBackgroundContent::Color(TRACK_OFF_COLOR)];
const TRACK_OFF_BG: StyleBackgroundContentVec =
    StyleBackgroundContentVec::from_const_slice(TRACK_OFF_BG_ITEMS);
const TRACK_ON_BG_ITEMS: &[StyleBackgroundContent] =
    &[StyleBackgroundContent::Color(TRACK_ON_COLOR)];
const TRACK_ON_BG: StyleBackgroundContentVec =
    StyleBackgroundContentVec::from_const_slice(TRACK_ON_BG_ITEMS);
const KNOB_BG_ITEMS: &[StyleBackgroundContent] = &[StyleBackgroundContent::Color(KNOB_COLOR)];
const KNOB_BG: StyleBackgroundContentVec =
    StyleBackgroundContentVec::from_const_slice(KNOB_BG_ITEMS);

/// Build the track (pill container) style. Background colour is the only
/// state-dependent property, so the style is built at runtime per the recipe's
/// "runtime vec if param-dependent" path.
fn build_track_style(checked: bool) -> CssPropertyWithConditionsVec {
    let bg = if checked { TRACK_ON_BG } else { TRACK_OFF_BG };
    CssPropertyWithConditionsVec::from_vec(alloc::vec![
        CssPropertyWithConditions::simple(CssProperty::const_display(LayoutDisplay::Flex)),
        CssPropertyWithConditions::simple(CssProperty::const_flex_direction(
            LayoutFlexDirection::Row,
        )),
        CssPropertyWithConditions::simple(CssProperty::const_align_items(LayoutAlignItems::Center)),
        CssPropertyWithConditions::simple(CssProperty::align_self(LayoutAlignSelf::Center)),
        CssPropertyWithConditions::simple(CssProperty::const_flex_grow(LayoutFlexGrow::const_new(
            0,
        ))),
        CssPropertyWithConditions::simple(CssProperty::const_width(LayoutWidth::const_px(
            TRACK_WIDTH,
        ))),
        CssPropertyWithConditions::simple(CssProperty::const_height(LayoutHeight::const_px(
            TRACK_HEIGHT,
        ))),
        CssPropertyWithConditions::simple(CssProperty::const_padding_left(
            LayoutPaddingLeft::const_px(TRACK_PADDING),
        )),
        CssPropertyWithConditions::simple(CssProperty::const_padding_right(
            LayoutPaddingRight::const_px(TRACK_PADDING),
        )),
        CssPropertyWithConditions::simple(CssProperty::const_padding_top(
            LayoutPaddingTop::const_px(TRACK_PADDING),
        )),
        CssPropertyWithConditions::simple(CssProperty::const_padding_bottom(
            LayoutPaddingBottom::const_px(TRACK_PADDING),
        )),
        CssPropertyWithConditions::simple(CssProperty::const_border_top_left_radius(
            StyleBorderTopLeftRadius::const_px(TRACK_RADIUS),
        )),
        CssPropertyWithConditions::simple(CssProperty::const_border_top_right_radius(
            StyleBorderTopRightRadius::const_px(TRACK_RADIUS),
        )),
        CssPropertyWithConditions::simple(CssProperty::const_border_bottom_left_radius(
            StyleBorderBottomLeftRadius::const_px(TRACK_RADIUS),
        )),
        CssPropertyWithConditions::simple(CssProperty::const_border_bottom_right_radius(
            StyleBorderBottomRightRadius::const_px(TRACK_RADIUS),
        )),
        CssPropertyWithConditions::simple(CssProperty::const_cursor(StyleCursor::Pointer)),
        CssPropertyWithConditions::simple(CssProperty::const_background_content(bg)),
    ])
}

/// Build the knob style. The knob's `margin-left` is the state-dependent
/// property that slides it between the off (left) and on (right) positions.
fn build_knob_style(checked: bool) -> CssPropertyWithConditionsVec {
    let margin = if checked { KNOB_TRAVEL } else { 0 };
    CssPropertyWithConditionsVec::from_vec(alloc::vec![
        CssPropertyWithConditions::simple(CssProperty::const_width(LayoutWidth::const_px(
            KNOB_SIZE,
        ))),
        CssPropertyWithConditions::simple(CssProperty::const_height(LayoutHeight::const_px(
            KNOB_SIZE,
        ))),
        CssPropertyWithConditions::simple(CssProperty::const_flex_grow(LayoutFlexGrow::const_new(
            0,
        ))),
        CssPropertyWithConditions::simple(CssProperty::const_border_top_left_radius(
            StyleBorderTopLeftRadius::const_px(KNOB_RADIUS),
        )),
        CssPropertyWithConditions::simple(CssProperty::const_border_top_right_radius(
            StyleBorderTopRightRadius::const_px(KNOB_RADIUS),
        )),
        CssPropertyWithConditions::simple(CssProperty::const_border_bottom_left_radius(
            StyleBorderBottomLeftRadius::const_px(KNOB_RADIUS),
        )),
        CssPropertyWithConditions::simple(CssProperty::const_border_bottom_right_radius(
            StyleBorderBottomRightRadius::const_px(KNOB_RADIUS),
        )),
        CssPropertyWithConditions::simple(CssProperty::const_background_content(KNOB_BG)),
        CssPropertyWithConditions::simple(CssProperty::const_margin_left(
            LayoutMarginLeft::const_px(margin),
        )),
    ])
}

impl Switch {
    /// Creates a new switch in the given on/off state with default styling.
    #[must_use] pub fn create(checked: bool) -> Self {
        Self {
            switch_state: SwitchStateWrapper {
                inner: SwitchState { checked },
                ..Default::default()
            },
            track_style: build_track_style(checked),
            knob_style: build_knob_style(checked),
        }
    }

    #[inline]
    #[must_use] pub fn swap_with_default(&mut self) -> Self {
        let mut s = Self::create(false);
        core::mem::swap(&mut s, self);
        s
    }

    #[inline]
    pub fn set_on_toggle<C: Into<SwitchOnToggleCallback>>(&mut self, data: RefAny, on_toggle: C) {
        self.switch_state.on_toggle = Some(SwitchOnToggle {
            callback: on_toggle.into(),
            refany: data,
        })
        .into();
    }

    #[inline]
    #[must_use] pub fn with_on_toggle<C: Into<SwitchOnToggleCallback>>(
        mut self,
        data: RefAny,
        on_toggle: C,
    ) -> Self {
        self.set_on_toggle(data, on_toggle);
        self
    }

    #[inline]
    #[must_use] pub fn dom(self) -> Dom {
        use azul_core::{
            callbacks::{CoreCallback, CoreCallbackData},
            dom::{Dom, EventFilter, HoverEventFilter},
        };

        Dom::create_div()
            .with_ids_and_classes(IdOrClassVec::from(SWITCH_TRACK_CLASS))
            .with_css_props(self.track_style)
            .with_callbacks(
                vec![CoreCallbackData {
                    event: EventFilter::Hover(HoverEventFilter::MouseUp),
                    callback: CoreCallback {
                        cb: input::default_on_switch_clicked as usize,
                        ctx: azul_core::refany::OptionRefAny::None,
                    },
                    refany: RefAny::new(self.switch_state),
                }]
                .into(),
            )
            .with_tab_index(TabIndex::Auto)
            .with_children(
                vec![Dom::create_div()
                    .with_ids_and_classes(IdOrClassVec::from(SWITCH_KNOB_CLASS))
                    .with_css_props(self.knob_style)]
                .into(),
            )
    }
}

impl Default for Switch {
    fn default() -> Self {
        Self::create(false)
    }
}

// handle input events for the switch
mod input {

    use azul_core::{callbacks::Update, refany::RefAny};
    use azul_css::props::{layout::LayoutMarginLeft, property::CssProperty};

    use super::{SwitchOnToggle, SwitchStateWrapper, KNOB_TRAVEL, TRACK_OFF_BG, TRACK_ON_BG};
    use crate::callbacks::CallbackInfo;

    pub(super) extern "C" fn default_on_switch_clicked(
        mut switch: RefAny,
        mut info: CallbackInfo,
    ) -> Update {
        let Some(mut switch) = switch.downcast_mut::<SwitchStateWrapper>() else {
            return Update::DoNothing;
        };

        let track_id = info.get_hit_node();
        let Some(knob_id) = info.get_first_child(track_id) else {
            return Update::DoNothing;
        };

        switch.inner.checked = !switch.inner.checked;

        let result = {
            // rustc doesn't understand the borrowing lifetime here
            let switch = &mut *switch;
            let on_toggle = &mut switch.on_toggle;
            let inner = switch.inner;

            match on_toggle.as_mut() {
                Some(SwitchOnToggle {
                    callback,
                    refany: data,
                }) => (callback.cb)(data.clone(), info, inner),
                None => Update::DoNothing,
            }
        };

        // CallbackInfo is Copy, so `info` is still usable after the call above.
        if switch.inner.checked {
            info.set_css_property(track_id, CssProperty::const_background_content(TRACK_ON_BG));
            info.set_css_property(
                knob_id,
                CssProperty::const_margin_left(LayoutMarginLeft::const_px(KNOB_TRAVEL)),
            );
        } else {
            info.set_css_property(track_id, CssProperty::const_background_content(TRACK_OFF_BG));
            info.set_css_property(
                knob_id,
                CssProperty::const_margin_left(LayoutMarginLeft::const_px(0)),
            );
        }

        result
    }
}

impl From<Switch> for Dom {
    fn from(s: Switch) -> Self {
        s.dom()
    }
}

#[cfg(test)]
#[allow(clippy::float_cmp)] // every float here is an exact, integral px constant
// `assertions_on_constants`: these are deliberate invariant guards over sibling
// `const`s in this module. They are const-foldable *today*, which is exactly the
// point — they must go red the moment someone edits one of those constants into an
// inconsistent value. Deleting them (clippy's suggestion) would delete the check.
#[allow(clippy::assertions_on_constants)]
mod autotest_generated {
    use std::{
        collections::{BTreeMap, HashMap},
        mem::discriminant,
        sync::{Arc, Mutex},
    };

    use azul_core::{
        dom::{DomId, DomNodeId, EventFilter, HoverEventFilter, NodeId, NodeType},
        geom::{LogicalRect, OptionLogicalPosition},
        gl::OptionGlContextPtr,
        hit_test::ScrollPosition,
        refany::OptionRefAny,
        resources::RendererResources,
        styled_dom::{NodeHierarchyItemId, StyledDom},
        window::{MonitorVec, RawWindowHandle},
    };
    use azul_css::props::basic::{length::SizeMetric, pixel::PixelValue};
    use rust_fontconfig::FcFontCache;

    use super::*;
    #[cfg(feature = "icu")]
    use crate::icu::IcuLocalizerHandle;
    use crate::{
        callbacks::{CallbackChange, CallbackInfoRefData, ExternalSystemCallbacks},
        solver3::{display_list::DisplayList, layout_tree::LayoutTree},
        window::{DomLayoutResult, LayoutWindow},
        window_state::FullWindowState,
    };

    // ------------------------------------------------------------------
    // Geometry, spelled out independently of the module's own constants
    // ------------------------------------------------------------------
    //
    // These literals are deliberately *not* derived from `TRACK_WIDTH` & friends:
    // they are the numbers a designer signed off on. If a constant upstream drifts,
    // the relation tests below fail instead of silently re-deriving themselves.

    const TRACK_W: f32 = 36.0;
    const TRACK_H: f32 = 20.0;
    const PAD: f32 = 2.0;
    const TRACK_R: f32 = 10.0;
    const KNOB: f32 = 16.0;
    const KNOB_R: f32 = 8.0;
    /// `36 - 2*2 - 16`
    const TRAVEL: f32 = 16.0;

    /// Flattened node ids of `Switch::dom()` (pre-order).
    const TRACK: usize = 0;
    const KNOB_NODE: usize = 1;

    // ------------------------------------------------------------------
    // Callback harness
    // ------------------------------------------------------------------

    /// A `DomNodeId` in the root DOM pointing at flattened node `idx`.
    fn node(idx: usize) -> DomNodeId {
        DomNodeId {
            dom: DomId::ROOT_ID,
            node: NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(idx))),
        }
    }

    /// A `DomNodeId` whose node component is `None` — the "no concrete node was hit"
    /// case. `CallbackInfo::set_css_property` *panics* on such an id, so the handler
    /// must bail out before ever reaching it.
    fn node_none() -> DomNodeId {
        DomNodeId {
            dom: DomId::ROOT_ID,
            node: NodeHierarchyItemId::NONE,
        }
    }

    /// A `DomLayoutResult` carrying only a `styled_dom`: the switch handler reaches
    /// exactly two `CallbackInfo` queries (`get_hit_node`, `get_first_child`), and both
    /// read the node hierarchy only — no real layout (and no font) is needed.
    fn layout_result(styled_dom: StyledDom) -> DomLayoutResult {
        DomLayoutResult {
            styled_dom,
            layout_tree: LayoutTree {
                nodes: Vec::new(),
                warm: Vec::new(),
                cold: Vec::new(),
                root: 0,
                dom_to_layout: BTreeMap::new(),
                children_arena: Vec::new(),
                children_offsets: Vec::new(),
                subtree_needs_intrinsic: Vec::new(),
            },
            calculated_positions: Vec::new(),
            viewport: LogicalRect::zero(),
            display_list: DisplayList::default(),
            scroll_ids: HashMap::new(),
            scroll_id_to_node_id: HashMap::new(),
        }
    }

    /// Runs `f` with a `CallbackInfo` whose window holds `styled_dom` as the root DOM
    /// and whose hit node is `hit`. Returns `f`'s value plus every change the callback
    /// pushed onto the transaction log.
    fn with_info<R>(
        styled_dom: StyledDom,
        hit: DomNodeId,
        f: impl FnOnce(&mut CallbackInfo) -> R,
    ) -> (R, Vec<CallbackChange>) {
        let mut layout_window =
            LayoutWindow::new(FcFontCache::default()).expect("LayoutWindow::new failed");
        layout_window
            .layout_results
            .insert(DomId::ROOT_ID, layout_result(styled_dom));

        let renderer_resources = RendererResources::default();
        let previous_window_state: Option<FullWindowState> = None;
        let current_window_state = FullWindowState::default();
        let gl_context = OptionGlContextPtr::None;
        let scroll_states: BTreeMap<DomId, BTreeMap<NodeHierarchyItemId, ScrollPosition>> =
            BTreeMap::new();
        let window_handle = RawWindowHandle::Unsupported;
        let system_callbacks = ExternalSystemCallbacks::rust_internal();

        let ref_data = CallbackInfoRefData {
            layout_window: &layout_window,
            renderer_resources: &renderer_resources,
            previous_window_state: &previous_window_state,
            current_window_state: &current_window_state,
            gl_context: &gl_context,
            current_scroll_manager: &scroll_states,
            current_window_handle: &window_handle,
            system_callbacks: &system_callbacks,
            system_style: Arc::new(azul_css::system::SystemStyle::default()),
            monitors: Arc::new(Mutex::new(MonitorVec::from_const_slice(&[]))),
            #[cfg(feature = "icu")]
            icu_localizer: IcuLocalizerHandle::default(),
            ctx: OptionRefAny::None,
        };

        let changes: Arc<Mutex<Vec<CallbackChange>>> = Arc::new(Mutex::new(Vec::new()));

        let mut info = CallbackInfo::new(
            &ref_data,
            &changes,
            hit,
            OptionLogicalPosition::None,
            OptionLogicalPosition::None,
        );

        let r = f(&mut info);
        let pushed = info.take_changes();
        (r, pushed)
    }

    /// Renders `switch`, then hands back both the laid-out DOM *and* the very `RefAny`
    /// the widget registered on its own mouse-up callback. Driving the handler with
    /// these two is the real wiring — nothing is re-created by hand, so a mismatch
    /// between what `dom()` stores and what the handler expects cannot hide behind the
    /// fixture.
    fn laid_out(switch: Switch) -> (StyledDom, RefAny) {
        let dom = switch.dom();
        let state = dom.root.callbacks.as_ref()[0].refany.clone();
        (StyledDom::create_from_dom(dom), state)
    }

    /// One "mouse-up on `hit`" delivered to the widget's own registered handler.
    fn click(
        styled_dom: StyledDom,
        state: &RefAny,
        hit: DomNodeId,
    ) -> (Update, Vec<CallbackChange>) {
        with_info(styled_dom, hit, |info| {
            input::default_on_switch_clicked(state.clone(), *info)
        })
    }

    fn is_checked(state: &RefAny) -> bool {
        let mut state = state.clone();
        let wrapper = state
            .downcast_ref::<SwitchStateWrapper>()
            .expect("the widget state changed type");
        wrapper.inner.checked
    }

    /// Every `(node, property)` pair the handler wrote, flattened and in push order.
    fn pushed_pairs(changes: &[CallbackChange]) -> Vec<(NodeId, CssProperty)> {
        changes
            .iter()
            .filter_map(|c| match c {
                CallbackChange::ChangeNodeCssProperties {
                    node_id, properties, ..
                } => Some((*node_id, properties.as_ref().to_vec())),
                _ => None,
            })
            .flat_map(|(n, ps)| ps.into_iter().map(move |p| (n, p)))
            .collect()
    }

    fn pushed_backgrounds(changes: &[CallbackChange]) -> Vec<(NodeId, StyleBackgroundContentVec)> {
        pushed_pairs(changes)
            .into_iter()
            .filter_map(|(n, p)| match p {
                CssProperty::BackgroundContent(b) => b.get_property().cloned().map(|b| (n, b)),
                _ => None,
            })
            .collect()
    }

    fn pushed_margins(changes: &[CallbackChange]) -> Vec<(NodeId, f32)> {
        pushed_pairs(changes)
            .into_iter()
            .filter_map(|(n, p)| match p {
                CssProperty::MarginLeft(m) => m.get_property().map(|m| (n, px(&m.inner))),
                _ => None,
            })
            .collect()
    }

    // ------------------------------------------------------------------
    // Style-vec probes
    // ------------------------------------------------------------------

    fn properties(v: &CssPropertyWithConditionsVec) -> Vec<CssProperty> {
        v.as_ref().iter().map(|p| p.property.clone()).collect()
    }

    fn find<T>(v: &CssPropertyWithConditionsVec, f: impl Fn(&CssProperty) -> Option<T>) -> Option<T> {
        v.as_ref().iter().find_map(|p| f(&p.property))
    }

    /// The `f32` of a `PixelValue`, asserting it is an absolute `px` length. An `em`
    /// or `%` slipping into the switch geometry would resolve against the parent
    /// font/box, so a 36px track could render at any size at all — and the knob's
    /// travel would no longer line up with it.
    fn px(pv: &PixelValue) -> f32 {
        assert_eq!(
            pv.metric,
            SizeMetric::Px,
            "switch geometry must be absolute px, got {:?}",
            pv.metric,
        );
        pv.number.get()
    }

    fn width_px(v: &CssPropertyWithConditionsVec) -> Option<f32> {
        find(v, |p| match p {
            CssProperty::Width(w) => match w.get_property() {
                Some(LayoutWidth::Px(pv)) => Some(px(pv)),
                _ => None,
            },
            _ => None,
        })
    }

    fn height_px(v: &CssPropertyWithConditionsVec) -> Option<f32> {
        find(v, |p| match p {
            CssProperty::Height(h) => match h.get_property() {
                Some(LayoutHeight::Px(pv)) => Some(px(pv)),
                _ => None,
            },
            _ => None,
        })
    }

    /// The knob's `margin-left` — the single property that encodes "which side is the
    /// knob on". This is the only thing distinguishing the two knob styles.
    fn margin_left_px(v: &CssPropertyWithConditionsVec) -> Option<f32> {
        find(v, |p| match p {
            CssProperty::MarginLeft(m) => m.get_property().map(|m| px(&m.inner)),
            _ => None,
        })
    }

    /// `(top, right, bottom, left)` padding, each as an absolute px.
    fn paddings_px(v: &CssPropertyWithConditionsVec) -> (Option<f32>, Option<f32>, Option<f32>, Option<f32>) {
        let get = |f: fn(&CssProperty) -> Option<f32>| find(v, f);
        (
            get(|p| match p {
                CssProperty::PaddingTop(x) => x.get_property().map(|x| px(&x.inner)),
                _ => None,
            }),
            get(|p| match p {
                CssProperty::PaddingRight(x) => x.get_property().map(|x| px(&x.inner)),
                _ => None,
            }),
            get(|p| match p {
                CssProperty::PaddingBottom(x) => x.get_property().map(|x| px(&x.inner)),
                _ => None,
            }),
            get(|p| match p {
                CssProperty::PaddingLeft(x) => x.get_property().map(|x| px(&x.inner)),
                _ => None,
            }),
        )
    }

    /// The four corner radii, in declaration order. (Each corner is its own newtype, so
    /// the arms cannot be collapsed into an or-pattern.)
    fn radii_px(v: &CssPropertyWithConditionsVec) -> Vec<f32> {
        v.as_ref()
            .iter()
            .filter_map(|p| match &p.property {
                CssProperty::BorderTopLeftRadius(r) => r.get_property().map(|r| px(&r.inner)),
                CssProperty::BorderTopRightRadius(r) => r.get_property().map(|r| px(&r.inner)),
                CssProperty::BorderBottomLeftRadius(r) => r.get_property().map(|r| px(&r.inner)),
                CssProperty::BorderBottomRightRadius(r) => r.get_property().map(|r| px(&r.inner)),
                _ => None,
            })
            .collect()
    }

    fn flex_grow(v: &CssPropertyWithConditionsVec) -> Option<f32> {
        find(v, |p| match p {
            CssProperty::FlexGrow(g) => g.get_property().map(|g| g.inner.get()),
            _ => None,
        })
    }

    fn background(v: &CssPropertyWithConditionsVec) -> Option<StyleBackgroundContentVec> {
        find(v, |p| match p {
            CssProperty::BackgroundContent(b) => b.get_property().cloned(),
            _ => None,
        })
    }

    /// The solid-colour layers of a background, in order. A gradient/image layer is
    /// dropped, so `solid_colors(bg).len() != bg.len()` means something non-solid crept in.
    fn solid_colors(bg: &StyleBackgroundContentVec) -> Vec<ColorU> {
        bg.as_ref()
            .iter()
            .filter_map(|c| match c {
                StyleBackgroundContent::Color(c) => Some(*c),
                _ => None,
            })
            .collect()
    }

    /// Every absolute-px number declared by a style vec.
    fn px_values(v: &CssPropertyWithConditionsVec) -> Vec<f32> {
        v.as_ref()
            .iter()
            .filter_map(|p| match &p.property {
                CssProperty::Width(x) => match x.get_property() {
                    Some(LayoutWidth::Px(pv)) => Some(px(pv)),
                    _ => None,
                },
                CssProperty::Height(x) => match x.get_property() {
                    Some(LayoutHeight::Px(pv)) => Some(px(pv)),
                    _ => None,
                },
                CssProperty::PaddingTop(x) => x.get_property().map(|x| px(&x.inner)),
                CssProperty::PaddingRight(x) => x.get_property().map(|x| px(&x.inner)),
                CssProperty::PaddingBottom(x) => x.get_property().map(|x| px(&x.inner)),
                CssProperty::PaddingLeft(x) => x.get_property().map(|x| px(&x.inner)),
                CssProperty::MarginLeft(x) => x.get_property().map(|x| px(&x.inner)),
                CssProperty::BorderTopLeftRadius(x) => x.get_property().map(|x| px(&x.inner)),
                CssProperty::BorderTopRightRadius(x) => x.get_property().map(|x| px(&x.inner)),
                CssProperty::BorderBottomLeftRadius(x) => x.get_property().map(|x| px(&x.inner)),
                CssProperty::BorderBottomRightRadius(x) => x.get_property().map(|x| px(&x.inner)),
                _ => None,
            })
            .collect()
    }

    fn classes(dom: &Dom) -> Vec<String> {
        dom.root
            .get_ids_and_classes()
            .as_ref()
            .iter()
            .filter_map(|c| match c {
                IdOrClass::Class(s) => Some(s.as_str().to_string()),
                IdOrClass::Id(_) => None,
            })
            .collect()
    }

    /// The properties of a rendered node's *inline* style, in declaration order.
    fn inline_properties(dom: &Dom) -> Vec<CssProperty> {
        dom.root
            .style
            .iter_inline_properties()
            .map(|(p, _)| p.clone())
            .collect()
    }

    // ------------------------------------------------------------------
    // Toggle callbacks
    // ------------------------------------------------------------------

    /// A payload the toggle callback writes into. It arrives as the `data: RefAny`
    /// argument — a *shared* clone of what the test still holds — so the test can read
    /// back exactly what the widget passed, without any global state.
    #[derive(Debug, Clone, PartialEq, Eq)]
    struct ToggleLog {
        seen: Vec<bool>,
        payload: u32,
    }

    extern "C" fn record_toggle(mut data: RefAny, _info: CallbackInfo, state: SwitchState) -> Update {
        if let Some(mut log) = data.downcast_mut::<ToggleLog>() {
            log.seen.push(state.checked);
        }
        Update::RefreshDom
    }

    extern "C" fn toggle_do_nothing(
        _data: RefAny,
        _info: CallbackInfo,
        _state: SwitchState,
    ) -> Update {
        Update::DoNothing
    }

    extern "C" fn toggle_refresh_all(
        _data: RefAny,
        _info: CallbackInfo,
        _state: SwitchState,
    ) -> Update {
        Update::RefreshDomAllWindows
    }

    /// A `Callback`-shaped (2-arg) function — the shape FFI bindings hand in, which the
    /// `From<Callback>` arm *transmutes* into the 3-arg switch slot. Never called.
    extern "C" fn generic_shaped(_data: RefAny, _info: CallbackInfo) -> Update {
        Update::DoNothing
    }

    fn log_refany() -> RefAny {
        RefAny::new(ToggleLog {
            seen: Vec::new(),
            payload: 0xDEAD_BEEF,
        })
    }

    fn read_log(probe: &RefAny) -> ToggleLog {
        let mut probe = probe.clone();
        let log = probe
            .downcast_ref::<ToggleLog>()
            .expect("the user payload changed type");
        log.clone()
    }

    // ==================================================================
    // Geometry constants — numeric limits / relations
    // ==================================================================

    #[test]
    fn knob_travel_matches_its_documented_formula_and_is_positive() {
        // The doc comment on KNOB_TRAVEL *is* the spec. A negative travel (knob wider
        // than the track's content box) would slide the knob left, off the widget.
        assert_eq!(
            KNOB_TRAVEL,
            TRACK_WIDTH - (2 * TRACK_PADDING) - KNOB_SIZE,
            "KNOB_TRAVEL no longer matches its own documented formula",
        );
        assert!(
            KNOB_TRAVEL > 0,
            "the knob has no room to travel: KNOB_TRAVEL = {KNOB_TRAVEL}",
        );
        assert_eq!(KNOB_TRAVEL as f32, TRAVEL);
    }

    #[test]
    fn the_knob_exactly_fills_the_track_from_edge_to_edge_when_on() {
        // padding + travel + knob + padding == track width. One px of drift either way
        // and the "on" knob either overhangs the pill or leaves a visible gap.
        assert_eq!(
            TRACK_PADDING + KNOB_TRAVEL + KNOB_SIZE + TRACK_PADDING,
            TRACK_WIDTH,
            "the switched-on knob does not sit flush against the track's right padding edge",
        );
    }

    #[test]
    fn the_knob_exactly_fills_the_tracks_vertical_padding_box() {
        // A knob taller than `track_height - 2*padding` overflows the pill vertically;
        // a shorter one floats. 16 == 20 - 2*2.
        assert_eq!(
            KNOB_SIZE,
            TRACK_HEIGHT - (2 * TRACK_PADDING),
            "the knob no longer fits the track's vertical padding box",
        );
    }

    #[test]
    fn both_shapes_are_fully_round_not_merely_rounded() {
        // radius == half the cross-axis extent is what makes a pill a pill and a knob a
        // circle. Anything less renders a rounded rectangle.
        assert_eq!(TRACK_RADIUS * 2, TRACK_HEIGHT, "the track is not a pill");
        assert_eq!(KNOB_RADIUS * 2, KNOB_SIZE, "the knob is not a circle");
    }

    #[test]
    fn every_geometry_constant_survives_floatvalues_fixed_point_encoding() {
        // `FloatValue::const_new` multiplies by 1000 in *isize* arithmetic with no
        // checked path — a constant near isize::MAX/1000 would wrap silently and produce
        // a nonsense length. Assert every constant is comfortably representable, and
        // that the encode/decode round-trips exactly.
        const MULT: isize = 1000;
        for (name, v) in [
            ("TRACK_WIDTH", TRACK_WIDTH),
            ("TRACK_HEIGHT", TRACK_HEIGHT),
            ("TRACK_PADDING", TRACK_PADDING),
            ("TRACK_RADIUS", TRACK_RADIUS),
            ("KNOB_SIZE", KNOB_SIZE),
            ("KNOB_RADIUS", KNOB_RADIUS),
            ("KNOB_TRAVEL", KNOB_TRAVEL),
        ] {
            assert!(
                v.checked_mul(MULT).is_some(),
                "{name} = {v} overflows FloatValue's fixed-point encoding",
            );
            assert!(v >= 0, "{name} = {v} is negative");

            // encode -> decode must be lossless for these integral px values.
            let encoded = LayoutMarginLeft::const_px(v);
            assert_eq!(
                px(&encoded.inner),
                v as f32,
                "{name} = {v} does not round-trip through PixelValue",
            );
        }
    }

    #[test]
    fn no_declared_px_value_is_nan_infinite_or_negative() {
        for checked in [false, true] {
            for (name, v) in [
                ("track", build_track_style(checked)),
                ("knob", build_knob_style(checked)),
            ] {
                let values = px_values(&v);
                assert!(
                    !values.is_empty(),
                    "checked={checked}: the {name} style declares no px lengths at all",
                );
                for x in values {
                    assert!(
                        x.is_finite(),
                        "checked={checked}: the {name} style declares a non-finite length {x}",
                    );
                    assert!(
                        x >= 0.0,
                        "checked={checked}: the {name} style declares a negative length {x}",
                    );
                }
            }
        }
    }

    // ==================================================================
    // build_track_style
    // ==================================================================

    #[test]
    fn build_track_style_is_pure() {
        for checked in [false, true] {
            assert_eq!(
                properties(&build_track_style(checked)),
                properties(&build_track_style(checked)),
                "build_track_style({checked}) is not deterministic",
            );
        }
    }

    #[test]
    fn build_track_style_differs_between_the_two_states_only_in_the_background() {
        // Everything but the colour must be byte-for-byte identical: a track that
        // changed size or radius when flipped would reflow its neighbours mid-animation.
        let on = properties(&build_track_style(true));
        let off = properties(&build_track_style(false));
        assert_eq!(
            on.len(),
            off.len(),
            "the two track styles declare a different number of properties",
        );

        let differing: Vec<_> = on
            .iter()
            .zip(off.iter())
            .filter(|(a, b)| a != b)
            .map(|(a, _)| discriminant(a))
            .collect();
        assert_eq!(
            differing,
            vec![discriminant(&CssProperty::const_background_content(TRACK_ON_BG))],
            "the on/off track styles differ in something other than the background",
        );
    }

    #[test]
    fn build_track_style_maps_on_to_green_and_off_to_grey() {
        // A swapped branch here yields a switch that reads as "on" when it is off —
        // which still type-checks and still animates.
        let on = background(&build_track_style(true)).expect("the on track has no background");
        let off = background(&build_track_style(false)).expect("the off track has no background");

        assert_eq!(solid_colors(&on), vec![TRACK_ON_COLOR]);
        assert_eq!(solid_colors(&off), vec![TRACK_OFF_COLOR]);
        assert_ne!(
            solid_colors(&on),
            solid_colors(&off),
            "the on and off tracks are the same colour — the switch has no visible state",
        );
    }

    #[test]
    fn the_track_background_is_exactly_one_fully_opaque_solid_layer() {
        // A translucent (or multi-layer, or gradient) track would let whatever is behind
        // it show through, so "off grey" would not actually be grey.
        for checked in [false, true] {
            let bg = background(&build_track_style(checked)).expect("no background");
            assert_eq!(
                bg.as_ref().len(),
                1,
                "checked={checked}: the track stacks {} background layers",
                bg.as_ref().len(),
            );
            let colors = solid_colors(&bg);
            assert_eq!(
                colors.len(),
                1,
                "checked={checked}: the track background is not a plain solid colour",
            );
            assert_eq!(
                colors[0].a, 255,
                "checked={checked}: the track background is translucent (a = {})",
                colors[0].a,
            );
        }
    }

    #[test]
    fn build_track_style_geometry_is_absolute_px_in_both_states() {
        for checked in [false, true] {
            let v = build_track_style(checked);
            // `px()` asserts SizeMetric::Px — an em/% here would scale with the parent
            // and desynchronise the knob's (absolute px) travel from the track.
            assert_eq!(width_px(&v), Some(TRACK_W), "checked={checked}: track width");
            assert_eq!(height_px(&v), Some(TRACK_H), "checked={checked}: track height");
            assert_eq!(
                paddings_px(&v),
                (Some(PAD), Some(PAD), Some(PAD), Some(PAD)),
                "checked={checked}: the track padding is not uniform",
            );
            assert_eq!(
                radii_px(&v),
                vec![TRACK_R; 4],
                "checked={checked}: the track's four corners are not all {TRACK_R}px",
            );
        }
    }

    #[test]
    fn build_track_style_declares_no_property_twice() {
        // A duplicate declaration means the later one silently wins — a latent
        // "why is my override ignored" bug that never surfaces as an error.
        for checked in [false, true] {
            let props = properties(&build_track_style(checked));
            let mut seen = Vec::new();
            for p in &props {
                let d = discriminant(p);
                assert!(
                    !seen.contains(&d),
                    "checked={checked}: the track style declares {p:?} twice",
                );
                seen.push(d);
            }
        }
    }

    #[test]
    fn build_track_style_marks_the_track_as_clickable_and_non_growing() {
        for checked in [false, true] {
            let v = build_track_style(checked);
            let cursor = find(&v, |p| match p {
                CssProperty::Cursor(c) => c.get_property().copied(),
                _ => None,
            });
            assert_eq!(
                cursor,
                Some(StyleCursor::Pointer),
                "checked={checked}: the switch does not present as clickable",
            );
            // flex-grow > 0 would let the track stretch past 36px inside a flex row,
            // and the knob's fixed 16px travel would no longer reach the right edge.
            assert_eq!(
                flex_grow(&v),
                Some(0.0),
                "checked={checked}: the track is allowed to grow",
            );
        }
    }

    #[test]
    fn build_track_style_lays_the_knob_out_as_a_centred_row() {
        // The knob is positioned by `margin-left` alone, which only behaves as a
        // left-anchored offset inside a row flex container.
        for checked in [false, true] {
            let v = build_track_style(checked);
            assert_eq!(
                find(&v, |p| match p {
                    CssProperty::Display(d) => d.get_property().copied(),
                    _ => None,
                }),
                Some(LayoutDisplay::Flex),
            );
            assert_eq!(
                find(&v, |p| match p {
                    CssProperty::FlexDirection(d) => d.get_property().copied(),
                    _ => None,
                }),
                Some(LayoutFlexDirection::Row),
                "checked={checked}: margin-left only slides the knob in a row container",
            );
            assert_eq!(
                find(&v, |p| match p {
                    CssProperty::AlignItems(a) => a.get_property().copied(),
                    _ => None,
                }),
                Some(LayoutAlignItems::Center),
            );
        }
    }

    // ==================================================================
    // build_knob_style
    // ==================================================================

    #[test]
    fn build_knob_style_is_pure() {
        for checked in [false, true] {
            assert_eq!(
                properties(&build_knob_style(checked)),
                properties(&build_knob_style(checked)),
                "build_knob_style({checked}) is not deterministic",
            );
        }
    }

    #[test]
    fn build_knob_style_differs_between_the_two_states_only_in_margin_left() {
        let on = properties(&build_knob_style(true));
        let off = properties(&build_knob_style(false));
        assert_eq!(
            on.len(),
            off.len(),
            "the two knob styles declare a different number of properties",
        );

        let differing: Vec<_> = on
            .iter()
            .zip(off.iter())
            .filter(|(a, b)| a != b)
            .map(|(a, _)| discriminant(a))
            .collect();
        assert_eq!(
            differing,
            vec![discriminant(&CssProperty::const_margin_left(
                LayoutMarginLeft::const_px(0)
            ))],
            "the on/off knob styles differ in something other than margin-left",
        );
    }

    #[test]
    fn build_knob_style_parks_the_knob_left_when_off_and_right_when_on() {
        assert_eq!(
            margin_left_px(&build_knob_style(false)),
            Some(0.0),
            "the off knob is not flush against the track's left padding edge",
        );
        assert_eq!(
            margin_left_px(&build_knob_style(true)),
            Some(TRAVEL),
            "the on knob does not travel the full width of the track",
        );
    }

    #[test]
    fn build_knob_style_geometry_is_a_circle_in_absolute_px() {
        for checked in [false, true] {
            let v = build_knob_style(checked);
            assert_eq!(width_px(&v), Some(KNOB), "checked={checked}: knob width");
            assert_eq!(height_px(&v), Some(KNOB), "checked={checked}: knob height");
            assert_eq!(
                width_px(&v),
                height_px(&v),
                "checked={checked}: the knob is not square, so it cannot be a circle",
            );
            assert_eq!(
                radii_px(&v),
                vec![KNOB_R; 4],
                "checked={checked}: the knob's four corners are not all {KNOB_R}px",
            );
            assert_eq!(
                flex_grow(&v),
                Some(0.0),
                "checked={checked}: the knob is allowed to grow and would fill the track",
            );
        }
    }

    #[test]
    fn build_knob_style_declares_no_property_twice() {
        // Two `margin-left` declarations would make the knob's position depend on
        // declaration order rather than on `checked`.
        for checked in [false, true] {
            let props = properties(&build_knob_style(checked));
            let mut seen = Vec::new();
            for p in &props {
                let d = discriminant(p);
                assert!(
                    !seen.contains(&d),
                    "checked={checked}: the knob style declares {p:?} twice",
                );
                seen.push(d);
            }
        }
    }

    #[test]
    fn the_knob_is_opaque_white_in_both_states() {
        // The knob must never inherit or blend with the track colour, or the "off" knob
        // would disappear into the grey.
        for checked in [false, true] {
            let bg = background(&build_knob_style(checked)).expect("the knob has no background");
            assert_eq!(bg.as_ref().len(), 1, "checked={checked}: the knob stacks layers");
            assert_eq!(solid_colors(&bg), vec![KNOB_COLOR]);
            assert_eq!(
                KNOB_COLOR.a, 255,
                "the knob is translucent and would tint with the track",
            );
            assert_ne!(
                solid_colors(&bg),
                vec![TRACK_OFF_COLOR],
                "checked={checked}: the knob is the same colour as the off track",
            );
            assert_ne!(
                solid_colors(&bg),
                vec![TRACK_ON_COLOR],
                "checked={checked}: the knob is the same colour as the on track",
            );
        }
    }

    #[test]
    fn the_knob_never_leaves_the_track_in_either_state() {
        // The real safety property, read back out of the *built styles* rather than the
        // constants: left edge >= 0 and right edge <= the track's content width.
        let content_w = TRACK_W - 2.0 * PAD;
        for checked in [false, true] {
            let margin = margin_left_px(&build_knob_style(checked)).expect("no margin-left");
            let size = width_px(&build_knob_style(checked)).expect("no width");

            assert!(margin >= 0.0, "checked={checked}: the knob is pushed off the left edge");
            assert!(
                margin + size <= content_w,
                "checked={checked}: the knob overhangs the track ({margin} + {size} > {content_w})",
            );
            assert!(
                height_px(&build_knob_style(checked)).expect("no height") <= TRACK_H - 2.0 * PAD,
                "checked={checked}: the knob overflows the track vertically",
            );
        }
    }

    // ==================================================================
    // Switch::create / Default
    // ==================================================================

    #[test]
    fn create_stores_the_flag_and_installs_no_callback() {
        for checked in [false, true] {
            let s = Switch::create(checked);
            assert_eq!(
                s.switch_state.inner.checked, checked,
                "create({checked}) did not store the flag it was given",
            );
            assert!(
                s.switch_state.on_toggle.as_ref().is_none(),
                "create({checked}) invented a toggle callback out of nowhere",
            );
        }
    }

    #[test]
    fn create_is_pure_and_its_two_states_are_distinguishable() {
        assert_eq!(Switch::create(true), Switch::create(true));
        assert_eq!(Switch::create(false), Switch::create(false));
        assert_ne!(
            Switch::create(true),
            Switch::create(false),
            "an on and an off switch are indistinguishable",
        );
    }

    #[test]
    fn create_wires_the_flag_through_to_both_style_builders() {
        // The one way `create` can be wrong without any test noticing: passing the flag
        // to one builder and a literal (or the negation) to the other, so the track says
        // "on" while the knob sits left.
        for checked in [false, true] {
            let s = Switch::create(checked);
            assert_eq!(
                properties(&s.track_style),
                properties(&build_track_style(checked)),
                "create({checked}) did not build the track for state {checked}",
            );
            assert_eq!(
                properties(&s.knob_style),
                properties(&build_knob_style(checked)),
                "create({checked}) did not build the knob for state {checked}",
            );
        }
    }

    #[test]
    fn the_rendered_colour_and_the_knob_position_always_agree_with_the_stored_flag() {
        for checked in [false, true] {
            let s = Switch::create(checked);
            let bg = background(&s.track_style).expect("no track background");
            let margin = margin_left_px(&s.knob_style).expect("no knob margin");

            let (expected_color, expected_margin) = if s.switch_state.inner.checked {
                (TRACK_ON_COLOR, TRAVEL)
            } else {
                (TRACK_OFF_COLOR, 0.0)
            };
            assert_eq!(solid_colors(&bg), vec![expected_color]);
            assert_eq!(
                margin, expected_margin,
                "checked={checked}: the knob position contradicts the track colour",
            );
        }
    }

    #[test]
    fn default_is_an_off_switch() {
        assert_eq!(Switch::default(), Switch::create(false));
        assert!(!Switch::default().switch_state.inner.checked);
        assert!(!SwitchState::default().checked, "the default SwitchState is not off");
        assert!(!SwitchStateWrapper::default().inner.checked);
        assert!(SwitchStateWrapper::default().on_toggle.as_ref().is_none());
    }

    // ==================================================================
    // Switch::swap_with_default
    // ==================================================================

    #[test]
    fn swap_with_default_returns_the_old_widget_and_leaves_an_off_switch_behind() {
        let mut s = Switch::create(true);
        let old = s.swap_with_default();

        assert_eq!(old, Switch::create(true), "the old widget was not returned intact");
        assert_eq!(s, Switch::create(false), "what was left behind is not a fresh off switch");
    }

    #[test]
    fn swap_with_default_on_an_already_default_widget_is_a_no_op() {
        let mut s = Switch::create(false);
        let old = s.swap_with_default();
        assert_eq!(old, s, "swapping a default with a default produced two different widgets");
        assert_eq!(old, Switch::create(false));
    }

    #[test]
    fn swapping_twice_round_trips_the_original_widget() {
        let mut a = Switch::create(true);
        let mut b = a.swap_with_default(); // a = default, b = on
        let c = b.swap_with_default(); // b = default, c = on

        assert_eq!(c, Switch::create(true));
        assert_eq!(a, Switch::create(false));
        assert_eq!(b, Switch::create(false));
    }

    #[test]
    fn swap_with_default_moves_the_toggle_callback_out_rather_than_copying_or_dropping_it() {
        let probe = log_refany();
        let mut s = Switch::create(true)
            .with_on_toggle(probe.clone(), record_toggle as SwitchOnToggleCallbackType);

        let old = s.swap_with_default();

        // The callback (and its payload) left with the returned value ...
        let moved = old
            .switch_state
            .on_toggle
            .as_ref()
            .expect("the toggle callback vanished during the swap");
        assert_eq!(
            moved.callback.cb as *const () as usize,
            record_toggle as SwitchOnToggleCallbackType as *const () as usize,
            "the fn pointer was mangled by the swap",
        );

        // ... and did NOT stay behind: a duplicated callback would fire twice, and a
        // duplicated RefAny would double-free its payload.
        assert!(
            s.switch_state.on_toggle.as_ref().is_none(),
            "the toggle callback was copied instead of moved",
        );

        // The payload is still alive and unchanged after the move.
        assert_eq!(read_log(&probe).payload, 0xDEAD_BEEF);
    }

    // ==================================================================
    // Switch::set_on_toggle / with_on_toggle
    // ==================================================================

    #[test]
    fn set_on_toggle_stores_the_function_pointer_and_the_payload_verbatim() {
        let mut s = Switch::create(false);
        s.set_on_toggle(
            RefAny::new(0xDEAD_BEEF_u32),
            toggle_do_nothing as SwitchOnToggleCallbackType,
        );

        let t = s
            .switch_state
            .on_toggle
            .as_ref()
            .expect("set_on_toggle did not store anything");
        assert_eq!(
            t.callback.cb as *const () as usize,
            toggle_do_nothing as SwitchOnToggleCallbackType as *const () as usize,
            "the fn pointer was corrupted on the way in",
        );

        let mut data = t.refany.clone();
        assert_eq!(
            *data.downcast_ref::<u32>().expect("the payload changed type"),
            0xDEAD_BEEF,
            "the payload was corrupted",
        );
        assert!(
            data.downcast_ref::<u64>().is_none(),
            "downcasting to the wrong type must fail, not reinterpret the bytes",
        );
    }

    #[test]
    fn set_on_toggle_replaces_rather_than_accumulates() {
        // `OptionSwitchOnToggle` is a single slot; setting twice must leave the *second*
        // callback installed (and must not leak or free the first one's RefAny).
        let first = log_refany();
        let mut s = Switch::create(false);
        s.set_on_toggle(first.clone(), toggle_do_nothing as SwitchOnToggleCallbackType);
        s.set_on_toggle(RefAny::new(1u8), toggle_refresh_all as SwitchOnToggleCallbackType);

        let t = s.switch_state.on_toggle.as_ref().expect("the callback vanished");
        assert_eq!(
            t.callback.cb as *const () as usize,
            toggle_refresh_all as SwitchOnToggleCallbackType as *const () as usize,
            "the second set_on_toggle did not win",
        );
        // The displaced payload is still a valid, readable RefAny (not freed twice).
        assert_eq!(read_log(&first).payload, 0xDEAD_BEEF);
    }

    #[test]
    fn set_on_toggle_does_not_disturb_the_state_or_the_styles() {
        for checked in [false, true] {
            let pristine = Switch::create(checked);
            let mut s = Switch::create(checked);
            s.set_on_toggle(RefAny::new(0u8), toggle_do_nothing as SwitchOnToggleCallbackType);

            assert_eq!(
                s.switch_state.inner.checked, checked,
                "installing a callback flipped the switch",
            );
            assert_eq!(
                properties(&s.track_style),
                properties(&pristine.track_style),
                "installing a callback rewrote the track style",
            );
            assert_eq!(
                properties(&s.knob_style),
                properties(&pristine.knob_style),
                "installing a callback rewrote the knob style",
            );
        }
    }

    #[test]
    fn with_on_toggle_is_exactly_set_on_toggle_in_builder_form() {
        let by_builder = Switch::create(true)
            .with_on_toggle(RefAny::new(7u32), toggle_do_nothing as SwitchOnToggleCallbackType);

        let mut by_setter = Switch::create(true);
        by_setter.set_on_toggle(RefAny::new(7u32), toggle_do_nothing as SwitchOnToggleCallbackType);

        assert_eq!(by_builder.switch_state.inner, by_setter.switch_state.inner);
        assert_eq!(
            properties(&by_builder.track_style),
            properties(&by_setter.track_style),
        );
        assert_eq!(
            properties(&by_builder.knob_style),
            properties(&by_setter.knob_style),
        );

        let a = by_builder.switch_state.on_toggle.as_ref().expect("builder lost the callback");
        let b = by_setter.switch_state.on_toggle.as_ref().expect("setter lost the callback");
        assert_eq!(a.callback.cb as *const () as usize, b.callback.cb as *const () as usize);

        let (mut a, mut b) = (a.refany.clone(), b.refany.clone());
        assert_eq!(
            *a.downcast_ref::<u32>().expect("builder payload changed type"),
            *b.downcast_ref::<u32>().expect("setter payload changed type"),
        );
    }

    #[test]
    fn with_on_toggle_accepts_a_generic_callback_without_mangling_the_pointer() {
        // The `From<Callback>` arm *transmutes* a 2-arg fn pointer into the 3-arg switch
        // slot — this is the FFI (Python/C) path. The pointer must come out bit-identical;
        // a mangled one would be called as a wild jump on the first click.
        let generic = Callback {
            cb: generic_shaped,
            ctx: azul_core::refany::OptionRefAny::None,
        };
        let expected = generic_shaped as *const () as usize;

        let s = Switch::create(false).with_on_toggle(RefAny::new(0u8), generic);
        let t = s.switch_state.on_toggle.as_ref().expect("the generic callback was dropped");
        assert_eq!(
            t.callback.cb as *const () as usize,
            expected,
            "the Callback -> SwitchOnToggleCallback transmute mangled the pointer",
        );
    }

    // ==================================================================
    // Switch::dom
    // ==================================================================

    #[test]
    fn dom_builds_a_focusable_track_with_exactly_one_knob_child() {
        for checked in [false, true] {
            let dom = Switch::create(checked).dom();

            assert!(matches!(dom.root.get_node_type(), NodeType::Div));
            assert_eq!(
                dom.root.flags.get_tab_index(),
                Some(TabIndex::Auto),
                "checked={checked}: the switch is not keyboard-focusable",
            );
            assert_eq!(classes(&dom), vec!["__azul-native-switch".to_string()]);

            let children = dom.children.as_ref();
            assert_eq!(children.len(), 1, "checked={checked}: the switch must have exactly one knob");
            assert_eq!(
                classes(&children[0]),
                vec!["__azul-native-switch-knob".to_string()],
                "checked={checked}: the knob carries the wrong class (external CSS would miss it)",
            );
            assert!(
                children[0].children.as_ref().is_empty(),
                "checked={checked}: the knob grew children",
            );
            assert_eq!(
                children[0].root.flags.get_tab_index(),
                None,
                "checked={checked}: the knob is separately focusable, so Tab stops twice",
            );
        }
    }

    #[test]
    fn dom_puts_the_track_style_on_the_track_and_the_knob_style_on_the_knob() {
        // Swapping the two would style the 16px knob like a 36px pill (and vice versa) —
        // the widget would still render, just wrong.
        for checked in [false, true] {
            let s = Switch::create(checked);
            let track = properties(&s.track_style);
            let knob = properties(&s.knob_style);

            let dom = s.dom();
            assert_eq!(
                inline_properties(&dom),
                track,
                "checked={checked}: the track style did not land on the track",
            );
            assert_eq!(
                inline_properties(&dom.children.as_ref()[0]),
                knob,
                "checked={checked}: the knob style did not land on the knob",
            );
        }
    }

    #[test]
    fn dom_registers_exactly_one_mouse_up_handler_and_it_is_the_widgets_own() {
        for checked in [false, true] {
            let dom = Switch::create(checked).dom();
            let callbacks = dom.root.callbacks.as_ref();

            assert_eq!(callbacks.len(), 1, "checked={checked}: expected exactly one callback");
            assert_eq!(
                callbacks[0].event,
                EventFilter::Hover(HoverEventFilter::MouseUp),
                "checked={checked}: the switch must toggle on mouse-up",
            );
            assert_eq!(
                callbacks[0].callback.cb,
                input::default_on_switch_clicked as usize,
                "checked={checked}: the registered handler is not default_on_switch_clicked",
            );

            // The knob itself must stay inert — a second handler there would toggle twice
            // per click (the event bubbles).
            assert!(
                dom.children.as_ref()[0].root.callbacks.as_ref().is_empty(),
                "checked={checked}: the knob registered a handler of its own",
            );
        }
    }

    #[test]
    fn dom_hands_the_widget_state_to_the_handler_not_the_user_payload() {
        // `dom()` moves `switch_state` (state + on_toggle + user RefAny) into the
        // callback's RefAny. If it stored the *user's* payload instead, the handler's
        // `downcast_mut::<SwitchStateWrapper>()` would fail and every click would be a
        // silent no-op.
        for checked in [false, true] {
            let dom = Switch::create(checked)
                .with_on_toggle(RefAny::new(9u32), toggle_do_nothing as SwitchOnToggleCallbackType)
                .dom();

            let mut state = dom.root.callbacks.as_ref()[0].refany.clone();
            let wrapper = state
                .downcast_ref::<SwitchStateWrapper>()
                .expect("the handler's RefAny is not a SwitchStateWrapper");

            assert_eq!(
                wrapper.inner.checked, checked,
                "the on/off flag was lost on the way into the DOM",
            );
            assert!(
                wrapper.on_toggle.as_ref().is_some(),
                "the user's toggle callback was lost on the way into the DOM",
            );
        }
    }

    #[test]
    fn dom_of_a_callback_less_switch_still_registers_the_toggle_handler() {
        // The switch must always install its own handler: the knob has to slide even
        // with no user callback.
        let dom = Switch::create(false).dom();
        assert_eq!(dom.root.callbacks.as_ref().len(), 1);

        let mut state = dom.root.callbacks.as_ref()[0].refany.clone();
        let wrapper = state.downcast_ref::<SwitchStateWrapper>().expect("wrong RefAny type");
        assert!(wrapper.on_toggle.as_ref().is_none());
    }

    #[test]
    fn from_switch_for_dom_is_the_same_as_calling_dom() {
        for checked in [false, true] {
            let via_from = Dom::from(Switch::create(checked));
            let via_dom = Switch::create(checked).dom();

            assert_eq!(classes(&via_from), classes(&via_dom));
            assert_eq!(inline_properties(&via_from), inline_properties(&via_dom));
            assert_eq!(via_from.children.as_ref().len(), via_dom.children.as_ref().len());
            assert_eq!(
                inline_properties(&via_from.children.as_ref()[0]),
                inline_properties(&via_dom.children.as_ref()[0]),
            );
            assert_eq!(
                via_from.root.callbacks.as_ref().len(),
                via_dom.root.callbacks.as_ref().len(),
            );
            assert_eq!(via_from.root.flags.get_tab_index(), via_dom.root.flags.get_tab_index());
        }
    }

    #[test]
    fn the_rendered_dom_flattens_to_exactly_two_nodes() {
        // `Dom::estimated_total_children` is a *cached* count; if it under-reports, the
        // flatten under-allocates its arenas. Two nodes: track (0), knob (1).
        for checked in [false, true] {
            let styled = StyledDom::create_from_dom(Switch::create(checked).dom());
            assert_eq!(
                styled.node_data.as_ref().len(),
                2,
                "checked={checked}: the switch no longer flattens to a track + a knob",
            );
        }
    }

    // ==================================================================
    // input::default_on_switch_clicked
    // ==================================================================

    #[test]
    fn clicking_an_off_switch_turns_it_on_and_pushes_the_on_visuals() {
        let (styled, state) = laid_out(Switch::create(false));
        assert!(!is_checked(&state));

        let (update, changes) = click(styled, &state, node(TRACK));

        assert!(is_checked(&state), "the click did not turn the switch on");
        assert!(
            matches!(update, Update::DoNothing),
            "with no user callback installed, the handler must report DoNothing",
        );
        assert_eq!(
            pushed_backgrounds(&changes)
                .iter()
                .map(|(n, b)| (*n, solid_colors(b)))
                .collect::<Vec<_>>(),
            vec![(NodeId::new(TRACK), vec![TRACK_ON_COLOR])],
            "turning the switch on did not repaint the *track* green",
        );
        assert_eq!(
            pushed_margins(&changes),
            vec![(NodeId::new(KNOB_NODE), TRAVEL)],
            "turning the switch on did not slide the *knob* right",
        );
    }

    #[test]
    fn clicking_an_on_switch_turns_it_off_and_pushes_the_off_visuals() {
        let (styled, state) = laid_out(Switch::create(true));
        let (_, changes) = click(styled, &state, node(TRACK));

        assert!(!is_checked(&state), "the click did not turn the switch off");
        assert_eq!(
            pushed_backgrounds(&changes)
                .iter()
                .map(|(n, b)| (*n, solid_colors(b)))
                .collect::<Vec<_>>(),
            vec![(NodeId::new(TRACK), vec![TRACK_OFF_COLOR])],
        );
        assert_eq!(pushed_margins(&changes), vec![(NodeId::new(KNOB_NODE), 0.0)]);
    }

    #[test]
    fn a_click_pushes_exactly_what_a_freshly_created_switch_would_render() {
        // The handler re-derives the visuals by hand instead of calling
        // `build_track_style`/`build_knob_style`. That duplication is the bug surface:
        // a colour or a travel distance can drift in one place and not the other, and
        // the switch then renders differently after a click than it did on mount.
        for start in [false, true] {
            let (styled, state) = laid_out(Switch::create(start));
            let (_, changes) = click(styled, &state, node(TRACK));

            let expected = Switch::create(!start);
            assert_eq!(
                pushed_backgrounds(&changes).into_iter().map(|(_, b)| b).collect::<Vec<_>>(),
                vec![background(&expected.track_style).expect("no background")],
                "start={start}: the clicked track colour differs from a freshly built one",
            );
            assert_eq!(
                pushed_margins(&changes).into_iter().map(|(_, m)| m).collect::<Vec<_>>(),
                vec![margin_left_px(&expected.knob_style).expect("no margin")],
                "start={start}: the clicked knob offset differs from a freshly built one",
            );
        }
    }

    #[test]
    fn clicking_twice_returns_to_the_original_state() {
        let (styled, state) = laid_out(Switch::create(false));

        // Two independent deliveries against the same widget state — the styled DOM is
        // rebuilt each time because the harness consumes it, but the RefAny is shared,
        // which is exactly how the real event loop drives it.
        let (styled2, _) = laid_out(Switch::create(false));
        let (_, first) = click(styled, &state, node(TRACK));
        let (_, second) = click(styled2, &state, node(TRACK));

        assert!(!is_checked(&state), "two clicks did not return the switch to its original state");
        assert_eq!(pushed_margins(&first), vec![(NodeId::new(KNOB_NODE), TRAVEL)]);
        assert_eq!(pushed_margins(&second), vec![(NodeId::new(KNOB_NODE), 0.0)]);
    }

    #[test]
    fn clicking_with_a_refany_of_the_wrong_type_is_a_silent_no_op() {
        // The handler downcasts blind; a foreign RefAny must bail out, not reinterpret
        // the bytes as a SwitchStateWrapper.
        let (styled, _) = laid_out(Switch::create(false));
        let foreign = RefAny::new(0xDEAD_BEEF_u32);

        let (update, changes) = click(styled, &foreign, node(TRACK));

        assert!(matches!(update, Update::DoNothing));
        assert!(changes.is_empty(), "the handler wrote to the DOM through a foreign RefAny");

        let mut foreign = foreign;
        assert_eq!(
            *foreign.downcast_ref::<u32>().expect("the foreign payload was reinterpreted"),
            0xDEAD_BEEF,
            "the handler corrupted a RefAny it did not understand",
        );
    }

    #[test]
    fn clicking_the_knob_itself_does_not_half_apply_the_toggle() {
        // The knob is the child that sits under the cursor for most of the track's area,
        // and it has no first child of its own. The handler needs that child to slide the
        // knob, so it must leave the *flag* alone too — a flipped flag with no visual
        // update is a switch that renders the opposite of what it reports.
        let (styled, state) = laid_out(Switch::create(false));

        let (update, changes) = click(styled, &state, node(KNOB_NODE));

        assert!(matches!(update, Update::DoNothing));
        assert!(changes.is_empty(), "a change was pushed for a node the handler could not resolve");
        assert!(
            !is_checked(&state),
            "the flag was flipped even though the knob could not be moved",
        );
    }

    #[test]
    fn stale_or_missing_hit_ids_do_not_panic_or_toggle() {
        // Stale hit ids reach callbacks after a DOM mutation. `set_css_property` *panics*
        // on a None node id, so the handler has to bail out before that point.
        // usize::MAX is unencodable by NodeId's 1-based scheme and would overflow while
        // building this fixture, before `click()` is even called; usize::MAX - 1 is the
        // repo's MAX_ENCODABLE_NODE and still absent from the layout.
        for hit in [node(2), node(99), node(usize::MAX - 1), node_none()] {
            let (styled, state) = laid_out(Switch::create(true));
            let (update, changes) = click(styled, &state, hit);

            assert!(matches!(update, Update::DoNothing), "{hit:?}: a stale hit was acted on");
            assert!(changes.is_empty(), "{hit:?}: a stale hit pushed a DOM change");
            assert!(is_checked(&state), "{hit:?}: a stale hit toggled the switch");
        }
    }

    #[test]
    fn the_toggle_callback_sees_the_new_state_and_its_verdict_is_forwarded() {
        // Order matters: the flag is flipped *before* the user callback runs, so the
        // callback observes the state the user just asked for — not the stale one.
        let probe = log_refany();
        let (styled, state) = laid_out(
            Switch::create(false)
                .with_on_toggle(probe.clone(), record_toggle as SwitchOnToggleCallbackType),
        );

        let (update, changes) = click(styled, &state, node(TRACK));

        assert_eq!(
            read_log(&probe).seen,
            vec![true],
            "the toggle callback was not called exactly once with the NEW state",
        );
        assert!(
            matches!(update, Update::RefreshDom),
            "the user callback's Update was swallowed instead of forwarded",
        );
        // ... and the visual sync still happens *after* the user callback returns.
        assert_eq!(pushed_margins(&changes), vec![(NodeId::new(KNOB_NODE), TRAVEL)]);
    }

    #[test]
    fn the_toggle_callback_receives_the_user_payload_not_the_widget_state() {
        let probe = log_refany();
        let (styled, state) = laid_out(
            Switch::create(true)
                .with_on_toggle(probe.clone(), record_toggle as SwitchOnToggleCallbackType),
        );

        click(styled, &state, node(TRACK));

        let log = read_log(&probe);
        assert_eq!(
            log.payload, 0xDEAD_BEEF,
            "the callback was handed something other than the user's own RefAny",
        );
        // create(true) -> clicked once -> the callback must have seen `false`.
        assert_eq!(log.seen, vec![false]);
    }

    #[test]
    fn a_toggle_callback_that_declines_the_update_still_gets_the_visuals_synced() {
        // A user callback returning DoNothing must not suppress the widget's own visual
        // bookkeeping — otherwise the flag says "on" and the knob stays parked left.
        let (styled, state) = laid_out(
            Switch::create(false)
                .with_on_toggle(RefAny::new(0u8), toggle_do_nothing as SwitchOnToggleCallbackType),
        );

        let (update, changes) = click(styled, &state, node(TRACK));

        assert!(matches!(update, Update::DoNothing));
        assert!(is_checked(&state));
        assert_eq!(
            pushed_margins(&changes),
            vec![(NodeId::new(KNOB_NODE), TRAVEL)],
            "a DoNothing user callback suppressed the knob slide",
        );
    }

    #[test]
    fn a_toggle_callback_on_an_unresolvable_node_is_never_called_at_all() {
        // The bail-out happens before the flip *and* before the user callback: a click
        // that cannot be rendered must not be reported to the app either.
        let probe = log_refany();
        let (styled, state) = laid_out(
            Switch::create(false)
                .with_on_toggle(probe.clone(), record_toggle as SwitchOnToggleCallbackType),
        );

        let (update, changes) = click(styled, &state, node(KNOB_NODE));

        assert!(matches!(update, Update::DoNothing));
        assert!(changes.is_empty());
        assert!(!is_checked(&state));
        assert!(
            read_log(&probe).seen.is_empty(),
            "the user was notified of a toggle that never happened",
        );
    }

    #[test]
    fn many_clicks_leave_the_flag_the_colour_and_the_knob_in_agreement() {
        // 51 clicks starting off -> on. Every push must agree with the flag it
        // accompanies; a drift between the three is exactly the class of bug that makes
        // a switch render inverted after a while.
        let mut expected_on = false;
        let (_, state) = laid_out(Switch::create(false));

        for i in 0..51u32 {
            let (styled, _) = laid_out(Switch::create(false));
            let (_, changes) = click(styled, &state, node(TRACK));
            expected_on = !expected_on;

            let (color, margin) = if expected_on {
                (TRACK_ON_COLOR, TRAVEL)
            } else {
                (TRACK_OFF_COLOR, 0.0)
            };
            assert_eq!(
                pushed_backgrounds(&changes)
                    .iter()
                    .map(|(n, b)| (*n, solid_colors(b)))
                    .collect::<Vec<_>>(),
                vec![(NodeId::new(TRACK), vec![color])],
                "click #{i}: the pushed track colour disagrees with the flag",
            );
            assert_eq!(
                pushed_margins(&changes),
                vec![(NodeId::new(KNOB_NODE), margin)],
                "click #{i}: the pushed knob offset disagrees with the flag",
            );
            assert_eq!(is_checked(&state), expected_on, "click #{i}: the flag drifted");
        }

        assert!(is_checked(&state), "an odd number of clicks left the switch off");
    }

    #[test]
    fn a_click_writes_to_two_distinct_nodes_and_never_to_the_wrong_one() {
        // The track gets the colour, the knob gets the offset — never the other way
        // round, and never both onto the same node.
        let (styled, state) = laid_out(Switch::create(false));
        let (_, changes) = click(styled, &state, node(TRACK));

        let bg_nodes: Vec<_> = pushed_backgrounds(&changes).into_iter().map(|(n, _)| n).collect();
        let margin_nodes: Vec<_> = pushed_margins(&changes).into_iter().map(|(n, _)| n).collect();

        assert_eq!(bg_nodes, vec![NodeId::new(TRACK)]);
        assert_eq!(margin_nodes, vec![NodeId::new(KNOB_NODE)]);
        assert_ne!(
            bg_nodes, margin_nodes,
            "the colour and the knob offset landed on the same node",
        );
    }
}