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
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
//! Titlebar widget for custom window chrome (CSD and title-only modes).
//!
//! Key type: [`Titlebar`]

use azul_core::{
    dom::{Dom, DomVec, IdOrClass, IdOrClass::Class, IdOrClass::Id, IdOrClassVec},
    refany::RefAny,
};
#[allow(clippy::wildcard_imports)] // widget/render module pulls in the css property/value types it builds with
use azul_css::{
    dynamic_selector::{CssPropertyWithConditions, CssPropertyWithConditionsVec},
    props::{
        basic::{
            color::ColorU,
            font::{StyleFontFamily, StyleFontFamilyVec},
            *,
        },
        layout::*,
        property::{CssProperty, *},
        style::*,
    },
    system::{SystemFontType, SystemStyle, TitlebarButtonSide, TitlebarButtons, TitlebarMetrics},
    *,
};

// ── Compile-time defaults (used when no SystemStyle is available) ─────────

// Verified: macOS 11 Big Sur – macOS 15 Sequoia (2020–2025)
#[cfg(target_os = "macos")]
const DEFAULT_TITLEBAR_HEIGHT: f32 = 28.0;
#[cfg(target_os = "windows")]
const DEFAULT_TITLEBAR_HEIGHT: f32 = 32.0;
#[cfg(target_os = "linux")]
const DEFAULT_TITLEBAR_HEIGHT: f32 = 30.0;
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
const DEFAULT_TITLEBAR_HEIGHT: f32 = 32.0;

#[cfg(target_os = "macos")]
const DEFAULT_TITLE_FONT_SIZE: f32 = 13.0;
#[cfg(target_os = "windows")]
const DEFAULT_TITLE_FONT_SIZE: f32 = 12.0;
#[cfg(target_os = "linux")]
const DEFAULT_TITLE_FONT_SIZE: f32 = 13.0;
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
const DEFAULT_TITLE_FONT_SIZE: f32 = 13.0;

// Verified: macOS 11–15 traffic-light geometry = 78px including gaps
#[cfg(target_os = "macos")]
const DEFAULT_BUTTON_AREA_WIDTH: f32 = 78.0;
// Windows 10/11: 3 buttons x 46px = 138px
#[cfg(target_os = "windows")]
const DEFAULT_BUTTON_AREA_WIDTH: f32 = 138.0;
#[cfg(target_os = "linux")]
const DEFAULT_BUTTON_AREA_WIDTH: f32 = 100.0;
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
const DEFAULT_BUTTON_AREA_WIDTH: f32 = 100.0;

// macOS: traffic lights on the left.  All others: right.
#[cfg(target_os = "macos")]
const DEFAULT_BUTTON_SIDE_LEFT: bool = true;
#[cfg(not(target_os = "macos"))]
const DEFAULT_BUTTON_SIDE_LEFT: bool = false;

// Default title text color for light / dark fallback
const DEFAULT_TITLE_COLOR_LIGHT: ColorU = ColorU { r: 76, g: 76, b: 76, a: 255 };  // #4c4c4c
const DEFAULT_TITLE_COLOR_DARK: ColorU = ColorU { r: 229, g: 229, b: 229, a: 255 }; // #e5e5e5

// ── Titlebar ─────────────────────────────────────────────────────────────

/// A titlebar widget with optional close / minimize / maximize
/// buttons, drag-to-move, and double-click-to-maximize.
///
/// # Two modes
///
/// 1. **Title-only** ([`Titlebar::dom`], the default for
///    `WindowDecorations::NoTitleAutoInject`):
///    The OS still draws the native window-control buttons (traffic lights on
///    macOS, caption buttons on Windows).  The titlebar reserves
///    `padding_left` / `padding_right` so the title text doesn't overlap them.
///
/// 2. **Full CSD** ([`Titlebar::dom_with_buttons`], used when
///    `WindowDecorations::None` + `has_decorations`):
///    The titlebar renders its own close / minimize / maximize buttons as
///    regular DOM nodes.  Each button carries a plain `MouseDown` callback
///    that calls `CallbackInfo::modify_window_state()` - exactly the same
///    mechanism used for window dragging.  No special event-system hooks.
///
/// Window-control buttons use `Dom::create_icon("close")` etc. so that
/// icons are resolved through the icon provider system (Material Icons
/// by default) and can be swapped out by registering a different icon pack.
///
/// # Button layout
///
/// `button_side` controls where the buttons appear:
/// - `Left` - macOS traffic-light style (buttons before title)
/// - `Right` - Windows / Linux style (title then buttons)
///
/// # Styling
///
/// The DOM uses CSS classes `.csd-titlebar`, `.csd-title`, `.csd-buttons`,
/// `.csd-button`, `.csd-close`, `.csd-minimize`, `.csd-maximize`.
/// These match the output of `SystemStyle::create_csd_stylesheet()`.
#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[repr(C)]
pub struct Titlebar {
    /// The title text to display.
    pub title: AzString,
    /// Height of the titlebar in CSS pixels.
    pub height: f32,
    /// Font size for the title text in CSS pixels.
    pub font_size: f32,
    /// Extra padding on the **left** side (px).
    pub padding_left: f32,
    /// Extra padding on the **right** side (px).
    pub padding_right: f32,
    /// Title text color (resolved from SystemStyle.colors.text or platform default).
    pub title_color: ColorU,
}

impl Titlebar {
    /// Create a titlebar with compile-time platform defaults.
    ///
    /// Use [`Titlebar::from_system_style`] when you have a
    /// `SystemStyle` available for pixel-perfect metrics.
    #[inline]
    #[must_use] pub fn new(title: AzString) -> Self {
        // Equal padding on both sides keeps text-align:center at the window midpoint.
        // The button-side half prevents overlap; the opposite half balances it.
        let half = DEFAULT_BUTTON_AREA_WIDTH / 2.0;
        let (padding_left, padding_right) = (half, half);
        Self {
            title,
            height: DEFAULT_TITLEBAR_HEIGHT,
            font_size: DEFAULT_TITLE_FONT_SIZE,
            padding_left,
            padding_right,
            title_color: DEFAULT_TITLE_COLOR_LIGHT,
        }
    }

    /// FFI-compatible alias for [`Titlebar::new`].
    #[inline]
    #[must_use] pub fn create(title: AzString) -> Self {
        Self::new(title)
    }

    /// Create a titlebar with a custom height.
    #[inline]
    #[must_use] pub fn with_height(title: AzString, height: f32) -> Self {
        let mut tb = Self::new(title);
        tb.height = height;
        tb
    }

    /// Set the titlebar height.
    #[inline]
    pub const fn set_height(&mut self, height: f32) {
        self.height = height;
    }

    /// Set the title text.
    #[inline]
    pub fn set_title(&mut self, title: AzString) {
        self.title = title;
    }

    /// Swap this titlebar with a default instance, returning the old value.
    #[inline]
    #[must_use]
    pub fn swap_with_default(&mut self) -> Self {
        let mut s = Self::new(AzString::from_const_str(""));
        core::mem::swap(&mut s, self);
        s
    }

    /// Create from a live [`SystemStyle`] (for title-only mode, padding
    /// reserves space for OS-drawn buttons).
    #[must_use] pub fn from_system_style(title: AzString, system_style: &SystemStyle) -> Self {
        let tm = &system_style.metrics.titlebar;
        let height = tm.height.as_ref()
            .map_or(DEFAULT_TITLEBAR_HEIGHT, |pv| pv.to_pixels_internal(0.0, 0.0, 0.0));
        let font_size = tm.title_font_size
            .into_option()
            .unwrap_or(DEFAULT_TITLE_FONT_SIZE);
        let button_area = tm.button_area_width.as_ref()
            .map_or(DEFAULT_BUTTON_AREA_WIDTH, |pv| pv.to_pixels_internal(0.0, 0.0, 0.0));
        let safe_left = tm.safe_area.left.as_ref()
            .map_or(0.0, |pv| pv.to_pixels_internal(0.0, 0.0, 0.0));
        let safe_right = tm.safe_area.right.as_ref()
            .map_or(0.0, |pv| pv.to_pixels_internal(0.0, 0.0, 0.0));
        // Apply padding_horizontal from TitlebarMetrics
        let pad_h = tm.padding_horizontal.as_ref()
            .map_or(0.0, |pv| pv.to_pixels_internal(0.0, 0.0, 0.0));

        // Equal padding on both sides so text-align:center stays at the window midpoint.
        // button_area/2 on each side: the button-side half clears the traffic-lights/caption
        // buttons, the opposite half balances the centering offset.
        let half_btn = button_area / 2.0;
        let (padding_left, padding_right) = (
            half_btn + safe_left + pad_h,
            half_btn + safe_right + pad_h,
        );

        // Resolve title color from system style, with dark/light fallback
        let title_color = system_style.colors.text.into_option().unwrap_or(
            match system_style.theme {
                system::Theme::Dark => DEFAULT_TITLE_COLOR_DARK,
                system::Theme::Light => DEFAULT_TITLE_COLOR_LIGHT,
            }
        );

        Self { title, height, font_size, padding_left, padding_right, title_color }
    }

    /// Create from [`SystemStyle`] for **full CSD** mode (no padding - the
    /// buttons are rendered as DOM children).
    #[must_use] pub fn from_system_style_csd(title: AzString, system_style: &SystemStyle) -> Self {
        let tm = &system_style.metrics.titlebar;
        let height = tm.height.as_ref()
            .map_or(DEFAULT_TITLEBAR_HEIGHT, |pv| pv.to_pixels_internal(0.0, 0.0, 0.0));
        let font_size = tm.title_font_size
            .into_option()
            .unwrap_or(DEFAULT_TITLE_FONT_SIZE);
        let title_color = system_style.colors.text.into_option().unwrap_or(
            match system_style.theme {
                system::Theme::Dark => DEFAULT_TITLE_COLOR_DARK,
                system::Theme::Light => DEFAULT_TITLE_COLOR_LIGHT,
            }
        );
        Self { title, height, font_size, padding_left: 0.0, padding_right: 0.0, title_color }
    }

    /// Build inline CSS for the container div.
    #[allow(clippy::cast_possible_truncation)] // bounded layout/render numeric cast
    fn build_container_style(&self, show_buttons: bool) -> CssPropertyWithConditionsVec {
        let mut props = Vec::with_capacity(8);
        if show_buttons {
            // CSD mode: flex layout to place buttons + title side by side
            props.push(CssPropertyWithConditions::simple(
                CssProperty::const_display(LayoutDisplay::Flex),
            ));
            props.push(CssPropertyWithConditions::simple(
                CssProperty::const_flex_direction(LayoutFlexDirection::Row),
            ));
            props.push(CssPropertyWithConditions::simple(
                CssProperty::const_align_items(LayoutAlignItems::Center),
            ));
        } else {
            // Title-only mode: block layout — title fills width automatically.
            // Avoids flex-grow complexity; text centers via text-align.
            props.push(CssPropertyWithConditions::simple(
                CssProperty::const_display(LayoutDisplay::Block),
            ));
        }
        props.push(CssPropertyWithConditions::simple(
            CssProperty::const_height(LayoutHeight::const_px(self.height as isize)),
        ));
        // Titlebar should show grab cursor and prevent text selection
        props.push(CssPropertyWithConditions::simple(
            CssProperty::const_cursor(StyleCursor::Grab),
        ));
        props.push(CssPropertyWithConditions::simple(
            CssProperty::user_select(StyleUserSelect::None),
        ));
        if self.padding_left > 0.0 {
            props.push(CssPropertyWithConditions::simple(
                CssProperty::const_padding_left(LayoutPaddingLeft::const_px(
                    self.padding_left as isize,
                )),
            ));
        }
        if self.padding_right > 0.0 {
            props.push(CssPropertyWithConditions::simple(
                CssProperty::const_padding_right(LayoutPaddingRight::const_px(
                    self.padding_right as isize,
                )),
            ));
        }
        CssPropertyWithConditionsVec::from_vec(props)
    }

    /// Build inline CSS for the title text node.
    #[allow(clippy::cast_possible_truncation)] // bounded layout/render numeric cast
    fn build_title_style(&self, show_buttons: bool) -> CssPropertyWithConditionsVec {
        let font_family = StyleFontFamilyVec::from_vec(vec![
            StyleFontFamily::SystemType(SystemFontType::TitleBold),
        ]);
        let mut props = Vec::with_capacity(10);
        props.push(CssPropertyWithConditions::simple(
            CssProperty::const_font_size(StyleFontSize::const_px(self.font_size as isize)),
        ));
        props.push(CssPropertyWithConditions::simple(
            CssProperty::const_font_family(font_family),
        ));
        // Use resolved title color from SystemStyle (adapts to dark mode)
        props.push(CssPropertyWithConditions::simple(
            CssProperty::const_text_color(StyleTextColor { inner: self.title_color }),
        ));
        // In CSD mode (flex container), title must grow to fill remaining space
        if show_buttons {
            props.push(CssPropertyWithConditions::simple(
                CssProperty::const_flex_grow(LayoutFlexGrow::const_new(1)),
            ));
            props.push(CssPropertyWithConditions::simple(
                CssProperty::const_min_width(LayoutMinWidth::const_px(0)),
            ));
        }
        props.push(CssPropertyWithConditions::simple(
            CssProperty::const_text_align(StyleTextAlign::Center),
        ));
        props.push(CssPropertyWithConditions::simple(
            CssProperty::WhiteSpace(StyleWhiteSpaceValue::Exact(StyleWhiteSpace::Nowrap)),
        ));
        props.push(CssPropertyWithConditions::simple(
            CssProperty::const_overflow_x(LayoutOverflow::Hidden),
        ));
        // Vertically center the text: pad from top by (height - font_size) / 2
        let v_pad = ((self.height - self.font_size) / 2.0).max(0.0);
        if v_pad > 0.0 {
            props.push(CssPropertyWithConditions::simple(
                CssProperty::const_padding_top(LayoutPaddingTop::const_px(v_pad as isize)),
            ));
        }
        CssPropertyWithConditionsVec::from_vec(props)
    }

    /// Title-only DOM (for `NoTitleAutoInject`).
    ///
    /// The OS draws the native window-control buttons; this just renders
    /// a centred title with drag support.
    #[inline]
    #[must_use] pub fn dom(self) -> Dom {
        self.dom_inner(false, &TitlebarButtons::default(), TitlebarButtonSide::Right)
    }

    /// Full-CSD DOM with close / minimize / maximize buttons.
    ///
    /// Each button is a div with a `MouseDown` callback that calls
    /// `modify_window_state()` - no special hooks needed.
    #[must_use] pub fn dom_with_buttons(
        self,
        buttons: &TitlebarButtons,
        button_side: TitlebarButtonSide,
    ) -> Dom {
        self.dom_inner(true, buttons, button_side)
    }

    /// Inner builder for both modes.
    #[allow(clippy::trivially_copy_pass_by_ref)] // <=8B Copy param kept by-ref intentionally (hot pixel/coord path or to avoid churning call sites for a perf-neutral change)
    fn dom_inner(
        self,
        show_buttons: bool,
        buttons: &TitlebarButtons,
        button_side: TitlebarButtonSide,
    ) -> Dom {
        use azul_core::{
            callbacks::{CoreCallback, CoreCallbackData},
            dom::{EventFilter, HoverEventFilter},
        };

        #[derive(Debug, Clone, Copy)]
        struct DragMarker;

        // Build styles BEFORE moving self.title
        let title_style = self.build_title_style(show_buttons);
        let container_style = self.build_container_style(show_buttons);

        // ── Title node with drag callbacks ──
        let title_classes = IdOrClassVec::from_vec(vec![Class("csd-title".into())]);

        let title_node = Dom::create_div()
            .with_ids_and_classes(title_classes)
            .with_css_props(title_style)
            .with_child(Dom::create_text(self.title)) // moves self.title
            .with_callbacks(vec![
                CoreCallbackData {
                    event: EventFilter::Hover(HoverEventFilter::DragStart),
                    callback: CoreCallback {
                        cb: callbacks::titlebar_drag_start as usize,
                        ctx: azul_core::refany::OptionRefAny::None,
                    },
                    refany: RefAny::new(DragMarker),
                },
                CoreCallbackData {
                    event: EventFilter::Hover(HoverEventFilter::Drag),
                    callback: CoreCallback {
                        cb: callbacks::titlebar_drag as usize,
                        ctx: azul_core::refany::OptionRefAny::None,
                    },
                    refany: RefAny::new(DragMarker),
                },
                CoreCallbackData {
                    event: EventFilter::Hover(HoverEventFilter::DoubleClick),
                    callback: CoreCallback {
                        cb: callbacks::titlebar_double_click as usize,
                        ctx: azul_core::refany::OptionRefAny::None,
                    },
                    refany: RefAny::new(DragMarker),
                },
            ].into());

        // ── Button container (CSD mode only) ──
        let button_container = if show_buttons {
            Some(build_button_container(buttons))
        } else {
            None
        };

        // ── Root ──
        let container_classes = IdOrClassVec::from_vec(vec![
            Class("csd-titlebar".into()),
            Class("__azul-native-titlebar".into()),
        ]);
        let mut root = Dom::create_div()
            .with_ids_and_classes(container_classes)
            .with_css_props(container_style);

        // Button side determines child order:
        //   Left  (macOS):   [buttons] [title]
        //   Right (Win/Lin): [title] [buttons]
        match button_side {
            TitlebarButtonSide::Left => {
                if let Some(btn) = button_container { root = root.with_child(btn); }
                root = root.with_child(title_node);
            }
            TitlebarButtonSide::Right => {
                root = root.with_child(title_node);
                if let Some(btn) = button_container { root = root.with_child(btn); }
            }
        }

        root
    }
}

/// Build the `.csd-buttons` container with close/min/max button DOM nodes.
#[allow(clippy::trivially_copy_pass_by_ref)] // <=8B Copy param kept by-ref intentionally (hot pixel/coord path or to avoid churning call sites for a perf-neutral change)
fn build_button_container(buttons: &TitlebarButtons) -> Dom {
    use azul_core::{
        callbacks::{CoreCallback, CoreCallbackData},
        dom::{EventFilter, HoverEventFilter},
    };

    let mut children = Vec::new();

    if buttons.has_minimize {
        let classes = IdOrClassVec::from_vec(vec![
            Id("csd-button-minimize".into()),
            Class("csd-button".into()),
            Class("csd-minimize".into()),
        ]);
        children.push(Dom::create_div()
            .with_ids_and_classes(classes)
            .with_child(Dom::create_icon("minimize"))
            .with_callbacks(vec![CoreCallbackData {
                event: EventFilter::Hover(HoverEventFilter::MouseDown),
                callback: CoreCallback {
                    cb: callbacks::csd_minimize as usize,
                    ctx: azul_core::refany::OptionRefAny::None,
                },
                refany: RefAny::new(()),
            }].into()));
    }

    if buttons.has_maximize {
        let classes = IdOrClassVec::from_vec(vec![
            Id("csd-button-maximize".into()),
            Class("csd-button".into()),
            Class("csd-maximize".into()),
        ]);
        children.push(Dom::create_div()
            .with_ids_and_classes(classes)
            .with_child(Dom::create_icon("maximize"))
            .with_callbacks(vec![CoreCallbackData {
                event: EventFilter::Hover(HoverEventFilter::MouseDown),
                callback: CoreCallback {
                    cb: callbacks::csd_maximize as usize,
                    ctx: azul_core::refany::OptionRefAny::None,
                },
                refany: RefAny::new(()),
            }].into()));
    }

    if buttons.has_close {
        let classes = IdOrClassVec::from_vec(vec![
            Id("csd-button-close".into()),
            Class("csd-button".into()),
            Class("csd-close".into()),
        ]);
        children.push(Dom::create_div()
            .with_ids_and_classes(classes)
            .with_child(Dom::create_icon("close"))
            .with_callbacks(vec![CoreCallbackData {
                event: EventFilter::Hover(HoverEventFilter::MouseDown),
                callback: CoreCallback {
                    cb: callbacks::csd_close as usize,
                    ctx: azul_core::refany::OptionRefAny::None,
                },
                refany: RefAny::new(()),
            }].into()));
    }

    let classes = IdOrClassVec::from_vec(vec![Class("csd-buttons".into())]);
    Dom::create_div()
        .with_ids_and_classes(classes)
        .with_children(DomVec::from_vec(children))
}

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

impl Default for Titlebar {
    fn default() -> Self {
        Self::new(AzString::from_const_str(""))
    }
}

// ── Titlebar callbacks ───────────────────────────────────────────────────

/// All titlebar callbacks: drag, double-click, close, minimize, maximize.
///
/// Every callback is a plain `extern "C"` function that uses
/// `CallbackInfo::modify_window_state()`.  No special hooks needed.
pub(crate) mod callbacks {
    use azul_core::callbacks::Update;
    use azul_core::refany::RefAny;
    use crate::callbacks::CallbackInfo;

    /// `DragStart` - on Wayland, initiate compositor-managed move immediately.
    /// On other platforms, just acknowledge (movement happens in `titlebar_drag`).
    pub(super) extern "C" fn titlebar_drag_start(
        _data: RefAny, mut info: CallbackInfo,
    ) -> Update {
        // On Wayland, window position is Uninitialized (compositor hides it).
        // We must use xdg_toplevel_move via begin_interactive_move().
        // MWA-B9 (D2): macOS ALSO takes the native path — the backend maps
        // begin_interactive_move to performWindowDragWithEvent:, which is
        // OS-smooth / snap-aware / multi-monitor-correct; the manual
        // per-event position loop below remains for X11/Windows and as the
        // programmatic fallback.
        let ws = info.get_current_window_state().clone();
        let native_move = matches!(ws.position, azul_core::window::WindowPosition::Uninitialized)
            || cfg!(target_os = "macos");
        if native_move {
            info.begin_interactive_move();
        } else {
            // MWA-C-csd: reset the fractional-residual accumulator for the
            // manual move loop (see titlebar_drag).
            RESIDUAL_X_BITS.store(0f32.to_bits(), core::sync::atomic::Ordering::Relaxed);
            RESIDUAL_Y_BITS.store(0f32.to_bits(), core::sync::atomic::Ordering::Relaxed);
            // MWA-C-csd: dragging a maximized window restores it first —
            // the native paths get this from the OS drag loop, but the
            // manual loop moved the still-maximized frame around.
            if ws.flags.frame == azul_core::window::WindowFrame::Maximized {
                let mut s = ws;
                s.flags.frame = azul_core::window::WindowFrame::Normal;
                info.modify_window_state(s);
            }
        }
        Update::DoNothing
    }

    /// MWA-C-csd: fractional-residual carry for the manual drag loop -
    /// rounding alone still loses up to half a pixel per event in a
    /// consistent direction, so very slow trackpad drags crawled. Only one
    /// interactive drag exists at a time and callbacks run on the UI
    /// thread; atomics keep this no_std-friendly (f32 stored as bits).
    static RESIDUAL_X_BITS: core::sync::atomic::AtomicU32 =
        core::sync::atomic::AtomicU32::new(0);
    static RESIDUAL_Y_BITS: core::sync::atomic::AtomicU32 =
        core::sync::atomic::AtomicU32::new(0);

    /// Drag - apply incremental screen-space delta to the CURRENT window position.
    ///
    /// Uses `get_drag_delta_screen_incremental()` (frame-to-frame delta) instead of
    /// `get_drag_delta_screen()` (total delta since drag start). Combined with
    /// the current window position from the OS, this approach is robust against
    /// external position changes during the drag (DPI change, OS clamping,
    /// compositor resize).
    ///
    /// On Wayland: this is a no-op because the compositor manages the move
    /// (initiated by `begin_interactive_move()` in `titlebar_drag_start`).
    #[allow(clippy::cast_possible_truncation)] // bounded layout/render numeric cast
    pub(super) extern "C" fn titlebar_drag(
        _data: RefAny, mut info: CallbackInfo,
    ) -> Update {
        use azul_core::window::WindowPosition;
        use azul_core::geom::PhysicalPositionI32;

        let delta = info.get_drag_delta_screen_incremental();
        let current_pos = info.get_current_window_state().position;

        if let (azul_core::geom::OptionDragDelta::Some(d), WindowPosition::Initialized(pos)) = (delta, current_pos) {
            use core::sync::atomic::Ordering;
            // MWA-C-csd: full fractional-residual carry (upgrades MWA-B9's
            // round-only fix). Each event applies the integer part of
            // delta + residual and carries the remainder, so arbitrarily
            // slow drags advance losslessly.
            let total_x = d.dx + f32::from_bits(RESIDUAL_X_BITS.load(Ordering::Relaxed));
            let total_y = d.dy + f32::from_bits(RESIDUAL_Y_BITS.load(Ordering::Relaxed));
            let apply_x = total_x.round();
            let apply_y = total_y.round();
            RESIDUAL_X_BITS.store((total_x - apply_x).to_bits(), Ordering::Relaxed);
            RESIDUAL_Y_BITS.store((total_y - apply_y).to_bits(), Ordering::Relaxed);
            let new_pos = WindowPosition::Initialized(PhysicalPositionI32::new(
                pos.x + apply_x as i32,
                pos.y + apply_y as i32,
            ));
            let mut ws = info.get_current_window_state().clone();
            ws.position = new_pos;
            info.modify_window_state(ws);
        }
        // On Wayland: current_pos is Uninitialized, so the if-let doesn't match → no-op.
        Update::DoNothing
    }

    /// `DoubleClick` - toggle Maximized ↔ Normal.
    pub(super) extern "C" fn titlebar_double_click(
        _data: RefAny, mut info: CallbackInfo,
    ) -> Update {
        use azul_core::window::WindowFrame;
        let mut s = info.get_current_window_state().clone();
        s.flags.frame = if s.flags.frame == WindowFrame::Maximized {
            WindowFrame::Normal } else { WindowFrame::Maximized };
        info.modify_window_state(s);
        Update::DoNothing
    }

    /// Close button - `close_requested = true`.
    pub(super) extern "C" fn csd_close(
        _data: RefAny, mut info: CallbackInfo,
    ) -> Update {
        let mut s = info.get_current_window_state().clone();
        s.flags.close_requested = true;
        info.modify_window_state(s);
        Update::DoNothing
    }

    /// Minimize button - `frame = Minimized`.
    pub(super) extern "C" fn csd_minimize(
        _data: RefAny, mut info: CallbackInfo,
    ) -> Update {
        use azul_core::window::WindowFrame;
        let mut s = info.get_current_window_state().clone();
        s.flags.frame = WindowFrame::Minimized;
        info.modify_window_state(s);
        Update::DoNothing
    }

    /// Maximize button - toggle Maximized ↔ Normal.
    pub(super) extern "C" fn csd_maximize(
        _data: RefAny, mut info: CallbackInfo,
    ) -> Update {
        use azul_core::window::WindowFrame;
        let mut s = info.get_current_window_state().clone();
        s.flags.frame = if s.flags.frame == WindowFrame::Maximized {
            WindowFrame::Normal } else { WindowFrame::Maximized };
        info.modify_window_state(s);
        Update::DoNothing
    }
}

#[cfg(test)]
#[allow(
    clippy::float_cmp,
    clippy::cast_possible_truncation,
    clippy::cast_precision_loss,
    clippy::too_many_lines,
    clippy::unreadable_literal
)]
mod autotest_generated {
    use std::{
        collections::BTreeMap,
        sync::{Arc, Mutex},
    };

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

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

    // ==================================================================
    // Helpers
    // ==================================================================

    /// Titles a caller can realistically hand to a titlebar. The widget never
    /// parses, trims or normalises its title, so every one of these has to reach
    /// the DOM byte-for-byte — `AzString` is length-based, so an embedded NUL
    /// must not truncate, and a ZWJ emoji cluster must not be split.
    const ADVERSARIAL_TITLES: [&str; 10] = [
        "",
        " ",
        "My Window",
        "a\0b",
        "\0",
        "e\u{0301}\u{0301}\u{0301}",
        "\u{1F469}\u{200D}\u{1F469}\u{200D}\u{1F467}",
        "\u{202E}gnirts desrever\u{202C}",
        "\u{FFFD}\u{FEFF}\t\n",
        "\u{200B}",
    ];

    /// Every `f32` the numeric surface (`height` / `font_size`) has to survive
    /// *without* tipping the fixed-point encoding over — see
    /// `heights_outside_the_encodable_range_are_not_saturated` for the ones that do.
    const TAME_FLOATS: [f32; 14] = [
        0.0,
        -0.0,
        1.0,
        -1.0,
        0.5,
        -0.5,
        30.0,
        1000.0,
        -1000.0,
        0.999,
        f32::EPSILON,
        f32::MIN_POSITIVE,
        -f32::MIN_POSITIVE,
        f32::NAN,
    ];

    /// The magnitudes that overflow `PixelValue`'s `value * 1000` encoding on
    /// every pointer width: `as isize` saturates them to `isize::MIN`/`MAX`.
    const UNENCODABLE_FLOATS: [f32; 4] =
        [f32::INFINITY, f32::NEG_INFINITY, f32::MAX, f32::MIN];

    /// Every `TitlebarButtonSide`.
    const BOTH_SIDES: [TitlebarButtonSide; 2] =
        [TitlebarButtonSide::Left, TitlebarButtonSide::Right];

    /// Every `WindowFrame` a titlebar callback can be invoked against.
    const ALL_FRAMES: [WindowFrame; 4] = [
        WindowFrame::Normal,
        WindowFrame::Minimized,
        WindowFrame::Maximized,
        WindowFrame::Fullscreen,
    ];

    fn tb(title: &str) -> Titlebar {
        Titlebar::new(AzString::from(title))
    }

    /// The declared properties of a style vec, in declaration order.
    fn properties(v: &CssPropertyWithConditionsVec) -> Vec<CssProperty> {
        v.as_ref().iter().map(|p| p.property.clone()).collect()
    }

    /// Every declaration must be unconditional: a titlebar built with a
    /// `@media`/`:hover` guard would silently not apply.
    fn all_unconditional(v: &CssPropertyWithConditionsVec) -> bool {
        v.as_ref().iter().all(|p| p.apply_if.as_ref().is_empty())
    }

    /// The `f32` of a `PixelValue`, asserting it is an absolute `px` length. An
    /// `em`/`%` height would resolve against the parent font/box instead of the
    /// fixed chrome geometry the titlebar is supposed to reserve.
    fn px(pv: &PixelValue) -> f32 {
        assert_eq!(
            pv.metric,
            SizeMetric::Px,
            "titlebar geometry must be absolute px, got {:?}",
            pv.metric
        );
        pv.number.get()
    }

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

    fn padding_left_px(v: &CssPropertyWithConditionsVec) -> Option<f32> {
        v.as_ref().iter().find_map(|p| match &p.property {
            CssProperty::PaddingLeft(x) => x.get_property().map(|x| px(&x.inner)),
            _ => None,
        })
    }

    fn padding_right_px(v: &CssPropertyWithConditionsVec) -> Option<f32> {
        v.as_ref().iter().find_map(|p| match &p.property {
            CssProperty::PaddingRight(x) => x.get_property().map(|x| px(&x.inner)),
            _ => None,
        })
    }

    fn padding_top_px(v: &CssPropertyWithConditionsVec) -> Option<f32> {
        v.as_ref().iter().find_map(|p| match &p.property {
            CssProperty::PaddingTop(x) => x.get_property().map(|x| px(&x.inner)),
            _ => None,
        })
    }

    fn font_size_px(v: &CssPropertyWithConditionsVec) -> Option<f32> {
        v.as_ref().iter().find_map(|p| match &p.property {
            CssProperty::FontSize(f) => f.get_property().map(|f| px(&f.inner)),
            _ => None,
        })
    }

    fn text_color(v: &CssPropertyWithConditionsVec) -> Option<ColorU> {
        v.as_ref().iter().find_map(|p| match &p.property {
            CssProperty::TextColor(c) => c.get_property().map(|c| c.inner),
            _ => None,
        })
    }

    /// The exact container declarations the widget documents, for a given mode.
    fn expected_container(t: &Titlebar, show_buttons: bool) -> Vec<CssProperty> {
        let mut v = Vec::new();
        if show_buttons {
            v.push(CssProperty::const_display(LayoutDisplay::Flex));
            v.push(CssProperty::const_flex_direction(LayoutFlexDirection::Row));
            v.push(CssProperty::const_align_items(LayoutAlignItems::Center));
        } else {
            v.push(CssProperty::const_display(LayoutDisplay::Block));
        }
        v.push(CssProperty::const_height(LayoutHeight::const_px(t.height as isize)));
        v.push(CssProperty::const_cursor(StyleCursor::Grab));
        v.push(CssProperty::user_select(StyleUserSelect::None));
        if t.padding_left > 0.0 {
            v.push(CssProperty::const_padding_left(LayoutPaddingLeft::const_px(
                t.padding_left as isize,
            )));
        }
        if t.padding_right > 0.0 {
            v.push(CssProperty::const_padding_right(LayoutPaddingRight::const_px(
                t.padding_right as isize,
            )));
        }
        v
    }

    /// The exact title declarations the widget documents, for a given mode.
    fn expected_title(t: &Titlebar, show_buttons: bool) -> Vec<CssProperty> {
        let font_family = StyleFontFamilyVec::from_vec(vec![StyleFontFamily::SystemType(
            SystemFontType::TitleBold,
        )]);
        let mut v = vec![
            CssProperty::const_font_size(StyleFontSize::const_px(t.font_size as isize)),
            CssProperty::const_font_family(font_family),
            CssProperty::const_text_color(StyleTextColor { inner: t.title_color }),
        ];
        if show_buttons {
            v.push(CssProperty::const_flex_grow(LayoutFlexGrow::const_new(1)));
            v.push(CssProperty::const_min_width(LayoutMinWidth::const_px(0)));
        }
        v.push(CssProperty::const_text_align(StyleTextAlign::Center));
        v.push(CssProperty::WhiteSpace(StyleWhiteSpaceValue::Exact(
            StyleWhiteSpace::Nowrap,
        )));
        v.push(CssProperty::const_overflow_x(LayoutOverflow::Hidden));
        let v_pad = ((t.height - t.font_size) / 2.0).max(0.0);
        if v_pad > 0.0 {
            v.push(CssProperty::const_padding_top(LayoutPaddingTop::const_px(
                v_pad as isize,
            )));
        }
        v
    }

    /// True if `node` carries the CSS class `name`.
    fn has_class(node: &Dom, name: &str) -> bool {
        node.root
            .get_ids_and_classes()
            .as_ref()
            .iter()
            .any(|c| matches!(c, IdOrClass::Class(s) if s.as_str() == name))
    }

    /// Every id declared on `node`, in order.
    fn ids(node: &Dom) -> Vec<String> {
        node.root
            .get_ids_and_classes()
            .as_ref()
            .iter()
            .filter_map(|c| match c {
                IdOrClass::Id(s) => Some(s.as_str().to_string()),
                IdOrClass::Class(_) => None,
            })
            .collect()
    }

    /// Every class declared on `node`, in order.
    fn classes(node: &Dom) -> Vec<String> {
        node.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 text of a `NodeType::Text` node (`None` for any other node type).
    fn text_of(node: &Dom) -> Option<&str> {
        match node.root.get_node_type() {
            NodeType::Text(s) => Some(s.as_ref().as_str()),
            _ => None,
        }
    }

    /// The icon name of a `NodeType::Icon` node.
    fn icon_of(node: &Dom) -> Option<&str> {
        match node.root.get_node_type() {
            NodeType::Icon(s) => Some(s.as_ref().as_str()),
            _ => None,
        }
    }

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

    /// `(event, callback fn address)` for every callback on `node`, in order.
    fn callbacks_of(node: &Dom) -> Vec<(EventFilter, usize)> {
        node.root
            .get_callbacks()
            .as_ref()
            .iter()
            .map(|c| (c.event, c.callback.cb))
            .collect()
    }

    /// The recursive descendant count. `Dom::estimated_total_children` is a
    /// *cached* value that, if too small, makes `convert_dom_into_compact_dom`
    /// under-allocate its arenas and panic on out-of-bounds writes — so it has to
    /// match this exactly for every button combination.
    fn count_descendants(dom: &Dom) -> usize {
        dom.children.as_ref().iter().map(|c| 1 + count_descendants(c)).sum()
    }

    /// A pre-order, structural fingerprint of a DOM: node type, ids/classes,
    /// `(event, fn address)` per callback and inline declarations. Used instead of
    /// `Dom: PartialEq` because the drag callbacks carry freshly allocated
    /// `RefAny`s, which compare by pointer and so are never equal across builds.
    fn fingerprint(dom: &Dom) -> Vec<String> {
        fn walk(d: &Dom, depth: usize, out: &mut Vec<String>) {
            out.push(format!(
                "{depth}|{:?}|{:?}|{:?}|{:?}|{:?}",
                d.root.get_node_type(),
                ids(d),
                classes(d),
                callbacks_of(d),
                inline_props(d),
            ));
            for c in d.children.as_ref() {
                walk(c, depth + 1, out);
            }
        }
        let mut out = Vec::new();
        walk(dom, 0, &mut out);
        out
    }

    /// The title node of a rendered titlebar (the `.csd-title` div).
    fn title_node(dom: &Dom) -> &Dom {
        dom.children
            .as_ref()
            .iter()
            .find(|c| has_class(c, "csd-title"))
            .expect("every titlebar must render a .csd-title node")
    }

    /// The `.csd-buttons` node of a rendered titlebar, if there is one.
    fn buttons_node(dom: &Dom) -> Option<&Dom> {
        dom.children.as_ref().iter().find(|c| has_class(c, "csd-buttons"))
    }

    fn all_button_combinations() -> Vec<TitlebarButtons> {
        let mut out = Vec::new();
        for &close in &[false, true] {
            for &min in &[false, true] {
                for &max in &[false, true] {
                    for &full in &[false, true] {
                        out.push(TitlebarButtons {
                            has_close: close,
                            has_minimize: min,
                            has_maximize: max,
                            has_fullscreen: full,
                        });
                    }
                }
            }
        }
        out
    }

    /// A `SystemStyle` whose titlebar metrics are all "not detected" — the state
    /// `SystemStyle::default()` ships and the one the fallbacks exist for.
    fn blank_system_style() -> SystemStyle {
        SystemStyle::default()
    }

    /// Runs `f` against a `CallbackInfo` backed by `state`, returning `f`'s result
    /// plus every recorded `CallbackChange`. No layout result is inserted: none of
    /// the titlebar callbacks walk the DOM.
    fn with_callback_info<R>(
        state: FullWindowState,
        f: impl FnOnce(CallbackInfo) -> R,
    ) -> (R, Vec<CallbackChange>) {
        let layout_window =
            LayoutWindow::new(FcFontCache::default()).expect("LayoutWindow::new failed");

        let renderer_resources = RendererResources::default();
        let previous_window_state: Option<FullWindowState> = None;
        let current_window_state = state;
        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(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 info = CallbackInfo::new(
            &ref_data,
            &changes,
            DomNodeId {
                dom: DomId::ROOT_ID,
                node: NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(0))),
            },
            OptionLogicalPosition::None,
            OptionLogicalPosition::None,
        );

        let out = f(info);
        let recorded = core::mem::take(&mut *changes.lock().expect("change log poisoned"));
        (out, recorded)
    }

    /// The window states pushed through `modify_window_state`, in order.
    fn state_writes(changes: &[CallbackChange]) -> Vec<FullWindowState> {
        changes
            .iter()
            .filter_map(|c| match c {
                CallbackChange::ModifyWindowState { state } => Some(state.clone()),
                _ => None,
            })
            .collect()
    }

    fn interactive_moves(changes: &[CallbackChange]) -> usize {
        changes
            .iter()
            .filter(|c| matches!(c, CallbackChange::BeginInteractiveMove))
            .count()
    }

    fn state_with(frame: WindowFrame, position: WindowPosition) -> FullWindowState {
        let mut s = FullWindowState::default();
        s.flags.frame = frame;
        s.position = position;
        s
    }

    // ==================================================================
    // Titlebar::new / Titlebar::create / Default
    // ==================================================================

    #[test]
    fn new_uses_the_compile_time_platform_defaults() {
        let t = tb("hello");

        assert_eq!(t.title.as_str(), "hello");
        assert_eq!(t.height, DEFAULT_TITLEBAR_HEIGHT);
        assert_eq!(t.font_size, DEFAULT_TITLE_FONT_SIZE);
        assert_eq!(t.title_color, DEFAULT_TITLE_COLOR_LIGHT);
        assert_eq!(t.padding_left, DEFAULT_BUTTON_AREA_WIDTH / 2.0);
        assert_eq!(t.padding_right, DEFAULT_BUTTON_AREA_WIDTH / 2.0);
    }

    #[test]
    fn new_pads_both_sides_equally_so_centering_lands_at_the_window_midpoint() {
        // The doc comment is explicit: the button-side half clears the OS buttons,
        // the opposite half balances `text-align: center`. Asymmetric padding would
        // push the title off the window midpoint.
        let t = tb("x");
        assert_eq!(
            t.padding_left, t.padding_right,
            "title-only padding must stay symmetric",
        );
        assert!(t.padding_left >= 0.0, "negative reserved space is meaningless");
        assert!(t.height > 0.0 && t.height.is_finite());
        assert!(t.font_size > 0.0 && t.font_size.is_finite());
        assert!(
            t.font_size < t.height,
            "the default font must fit inside the default titlebar height",
        );
    }

    #[test]
    fn new_stores_pathological_titles_byte_for_byte() {
        for title in ADVERSARIAL_TITLES {
            let t = tb(title);
            assert_eq!(t.title.as_str(), title, "the title was mangled or normalised");
            assert_eq!(
                t.title.as_str().len(),
                title.len(),
                "the title was truncated (an embedded NUL must not terminate it)",
            );
        }
    }

    #[test]
    fn new_accepts_a_title_far_longer_than_any_real_window_caption() {
        let huge = "a".repeat(1_000_000);
        let t = Titlebar::new(AzString::from(huge.clone()));
        assert_eq!(t.title.as_str().len(), 1_000_000);
        // ... and it survives the trip into the DOM without being re-encoded.
        let dom = t.dom();
        assert_eq!(text_of(&title_node(&dom).children.as_ref()[0]), Some(huge.as_str()));
    }

    #[test]
    fn create_is_indistinguishable_from_new() {
        for title in ADVERSARIAL_TITLES {
            assert_eq!(
                Titlebar::create(AzString::from(title)),
                Titlebar::new(AzString::from(title)),
                "the FFI alias drifted away from Titlebar::new",
            );
        }
    }

    #[test]
    fn default_is_new_with_an_empty_title() {
        let d = Titlebar::default();
        assert_eq!(d, tb(""));
        assert_eq!(d.title.as_str(), "");
    }

    // ==================================================================
    // Titlebar::with_height / Titlebar::set_height
    // ==================================================================

    #[test]
    fn with_height_stores_every_float_bit_exactly_and_touches_nothing_else() {
        let base = tb("t");
        for h in TAME_FLOATS.into_iter().chain(UNENCODABLE_FLOATS) {
            let t = Titlebar::with_height(AzString::from("t"), h);

            // to_bits, not `==`: NaN != NaN, and -0.0 == 0.0 would hide a sign flip.
            assert_eq!(
                t.height.to_bits(),
                h.to_bits(),
                "with_height({h}) did not store the value verbatim",
            );
            assert_eq!(t.title.as_str(), "t");
            assert_eq!(t.font_size, base.font_size, "with_height({h}) moved the font size");
            assert_eq!(t.padding_left, base.padding_left, "with_height({h}) moved the padding");
            assert_eq!(t.padding_right, base.padding_right, "with_height({h}) moved the padding");
            assert_eq!(t.title_color, base.title_color, "with_height({h}) moved the colour");
        }
    }

    #[test]
    fn set_height_is_a_bit_exact_last_write_wins_store() {
        let mut t = tb("t");
        let base = tb("t");

        for h in TAME_FLOATS.into_iter().chain(UNENCODABLE_FLOATS) {
            t.set_height(h);
            assert_eq!(t.height.to_bits(), h.to_bits(), "set_height({h}) was not verbatim");
            assert_eq!(t.font_size, base.font_size);
            assert_eq!(t.padding_left, base.padding_left);
            assert_eq!(t.padding_right, base.padding_right);
            assert_eq!(t.title.as_str(), "t", "set_height({h}) disturbed the title");
        }

        // The last write is the one that survives; nothing accumulates.
        t.set_height(7.5);
        t.set_height(9.25);
        assert_eq!(t.height, 9.25);
    }

    #[test]
    fn set_height_zero_and_negative_are_stored_not_clamped() {
        // The setter is documented as a plain store — it is `build_container_style`
        // that has to survive the result, not the setter.
        let mut t = tb("t");

        t.set_height(0.0);
        assert_eq!(t.height.to_bits(), 0_f32.to_bits(), "0.0 must stay +0.0");

        t.set_height(-0.0);
        assert_eq!(t.height.to_bits(), (-0.0_f32).to_bits(), "-0.0 must not be normalised");

        t.set_height(-42.0);
        assert_eq!(t.height, -42.0, "a negative height must not be clamped by the setter");
    }

    // ==================================================================
    // Titlebar::set_title
    // ==================================================================

    #[test]
    fn set_title_replaces_the_title_and_leaves_the_geometry_alone() {
        let mut t = Titlebar::with_height(AzString::from("first"), 44.0);
        for title in ADVERSARIAL_TITLES {
            t.set_title(AzString::from(title));
            assert_eq!(t.title.as_str(), title);
            assert_eq!(t.title.as_str().len(), title.len());
            assert_eq!(t.height, 44.0, "set_title moved the height");
            assert_eq!(t.padding_left, tb("").padding_left, "set_title moved the padding");
        }
    }

    // ==================================================================
    // Titlebar::swap_with_default
    // ==================================================================

    #[test]
    fn swap_with_default_hands_back_the_old_value_and_leaves_a_default() {
        let mut t = Titlebar::with_height(AzString::from("payload"), 99.5);
        t.title_color = ColorU { r: 1, g: 2, b: 3, a: 4 };

        let taken = t.swap_with_default();

        assert_eq!(taken.title.as_str(), "payload", "the title did not travel out");
        assert_eq!(taken.height, 99.5, "the height did not travel out");
        assert_eq!(taken.title_color, ColorU { r: 1, g: 2, b: 3, a: 4 });

        assert_eq!(t, Titlebar::default(), "what was left behind is not a default titlebar");
        assert_eq!(t.title.as_str(), "");
    }

    #[test]
    fn swap_with_default_moves_a_nan_height_out_without_losing_its_bits() {
        // `Titlebar` derives PartialEq, so a NaN height makes the struct
        // self-unequal — the swap still has to move the exact bit pattern.
        let mut t = tb("x");
        t.set_height(f32::NAN);

        let taken = t.swap_with_default();

        assert!(taken.height.is_nan(), "the NaN height did not travel out");
        assert_eq!(t.height, DEFAULT_TITLEBAR_HEIGHT, "the leftover kept the NaN");
        assert_eq!(t, Titlebar::default());
    }

    #[test]
    fn repeated_swap_with_default_never_accumulates_state() {
        let mut t = Titlebar::with_height(AzString::from("x"), 1.0);
        let _first = t.swap_with_default();

        for i in 0..8 {
            let taken = t.swap_with_default();
            assert_eq!(taken, Titlebar::default(), "swap #{i} handed back a non-default");
            assert_eq!(t, Titlebar::default(), "swap #{i} left a non-default behind");
        }

        // The drained titlebar still renders a well-formed DOM.
        let dom = t.dom();
        assert_eq!(dom.children.as_ref().len(), 1);
        assert_eq!(text_of(&title_node(&dom).children.as_ref()[0]), Some(""));
    }

    // ==================================================================
    // Titlebar::from_system_style
    // ==================================================================

    #[test]
    fn from_system_style_falls_back_to_the_compile_time_defaults_when_nothing_is_detected() {
        let ss = blank_system_style();
        let t = Titlebar::from_system_style(AzString::from("sys"), &ss);

        assert_eq!(t.title.as_str(), "sys");
        assert_eq!(t.height, DEFAULT_TITLEBAR_HEIGHT, "an undetected height must fall back");
        // `TitlebarMetrics::default()` *does* carry a font size (13.0), so the
        // compile-time default is only reachable when it is explicitly None.
        assert_eq!(t.font_size, 13.0);
        assert_eq!(t.padding_left, DEFAULT_BUTTON_AREA_WIDTH / 2.0);
        assert_eq!(t.padding_right, DEFAULT_BUTTON_AREA_WIDTH / 2.0);
        assert_eq!(t.title_color, DEFAULT_TITLE_COLOR_LIGHT);
    }

    #[test]
    fn from_system_style_with_no_font_size_falls_back_to_the_platform_constant() {
        let mut ss = blank_system_style();
        ss.metrics.titlebar.title_font_size = OptionF32::None;
        let t = Titlebar::from_system_style(AzString::from("x"), &ss);
        assert_eq!(t.font_size, DEFAULT_TITLE_FONT_SIZE);
    }

    #[test]
    fn from_system_style_adds_the_safe_area_and_padding_to_each_side_separately() {
        let mut ss = blank_system_style();
        ss.metrics.titlebar.button_area_width = OptionPixelValue::Some(PixelValue::px(100.0));
        ss.metrics.titlebar.padding_horizontal = OptionPixelValue::Some(PixelValue::px(5.0));
        ss.metrics.titlebar.safe_area = SafeAreaInsets {
            top: OptionPixelValue::None,
            bottom: OptionPixelValue::None,
            left: OptionPixelValue::Some(PixelValue::px(10.0)),
            right: OptionPixelValue::Some(PixelValue::px(20.0)),
        };

        let t = Titlebar::from_system_style(AzString::from("x"), &ss);

        assert_eq!(t.padding_left, 50.0 + 10.0 + 5.0);
        assert_eq!(t.padding_right, 50.0 + 20.0 + 5.0);
        // A notch on one side is exactly the documented case where the padding is
        // deliberately *not* symmetric.
        assert_ne!(t.padding_left, t.padding_right);
    }

    #[test]
    fn from_system_style_reads_the_height_and_font_size_it_was_given() {
        let mut ss = blank_system_style();
        ss.metrics.titlebar.height = OptionPixelValue::Some(PixelValue::px(41.0));
        ss.metrics.titlebar.title_font_size = OptionF32::Some(17.5);

        let t = Titlebar::from_system_style(AzString::from("x"), &ss);

        assert_eq!(t.height, 41.0);
        assert_eq!(t.font_size, 17.5);
    }

    #[test]
    fn from_system_style_converts_absolute_units_but_collapses_relative_ones_to_zero() {
        // `to_pixels_internal(0.0, 0.0, 0.0)` is called with *zero* resolution
        // bases, so anything relative (em/rem/%/vw) silently resolves to 0px —
        // a titlebar height declared in `em` collapses the whole chrome.
        let cases: [(PixelValue, f32); 6] = [
            (PixelValue::px(30.0), 30.0),
            (PixelValue::pt(30.0), 30.0 * (96.0 / 72.0)),
            (PixelValue::em(2.0), 0.0),
            (PixelValue::rem(2.0), 0.0),
            (PixelValue::percent(50.0), 0.0),
            (PixelValue::const_from_metric(SizeMetric::Vh, 50), 0.0),
        ];

        for (pv, expected) in cases {
            let mut ss = blank_system_style();
            ss.metrics.titlebar.height = OptionPixelValue::Some(pv);
            let t = Titlebar::from_system_style(AzString::from("x"), &ss);
            assert!(
                (t.height - expected).abs() < 0.01,
                "{pv:?} resolved to {} px, expected {expected} px",
                t.height,
            );
        }
    }

    #[test]
    fn from_system_style_saturates_a_non_finite_metric_instead_of_propagating_it() {
        // `PixelValue::px(inf)` encodes as `f32_to_isize(inf * 1000) == isize::MAX`,
        // so what comes back out is huge but *finite*: an infinity reaching the
        // layout solver would poison every downstream size computation.
        for bogus in [f32::INFINITY, f32::NEG_INFINITY, f32::MAX, f32::MIN, f32::NAN] {
            let mut ss = blank_system_style();
            ss.metrics.titlebar.height = OptionPixelValue::Some(PixelValue::px(bogus));
            ss.metrics.titlebar.button_area_width = OptionPixelValue::Some(PixelValue::px(bogus));

            let t = Titlebar::from_system_style(AzString::from("x"), &ss);

            assert!(t.height.is_finite(), "{bogus} produced a non-finite height {}", t.height);
            assert!(
                t.padding_left.is_finite() && t.padding_right.is_finite(),
                "{bogus} produced non-finite padding",
            );
        }

        // NaN specifically collapses to zero rather than staying NaN.
        let mut ss = blank_system_style();
        ss.metrics.titlebar.height = OptionPixelValue::Some(PixelValue::px(f32::NAN));
        assert_eq!(Titlebar::from_system_style(AzString::from("x"), &ss).height, 0.0);
    }

    #[test]
    fn from_system_style_prefers_the_detected_text_colour_over_both_theme_fallbacks() {
        let detected = ColorU { r: 9, g: 8, b: 7, a: 6 };
        for theme in [system::Theme::Light, system::Theme::Dark] {
            let mut ss = blank_system_style();
            ss.theme = theme;
            ss.colors.text = OptionColorU::Some(detected);
            assert_eq!(
                Titlebar::from_system_style(AzString::from("x"), &ss).title_color,
                detected,
                "{theme:?}: the detected system text colour must win",
            );
        }
    }

    #[test]
    fn from_system_style_picks_the_theme_appropriate_fallback_colour() {
        let mut light = blank_system_style();
        light.theme = system::Theme::Light;
        light.colors.text = OptionColorU::None;
        assert_eq!(
            Titlebar::from_system_style(AzString::from("x"), &light).title_color,
            DEFAULT_TITLE_COLOR_LIGHT,
        );

        let mut dark = blank_system_style();
        dark.theme = system::Theme::Dark;
        dark.colors.text = OptionColorU::None;
        assert_eq!(
            Titlebar::from_system_style(AzString::from("x"), &dark).title_color,
            DEFAULT_TITLE_COLOR_DARK,
        );

        // The two fallbacks must actually differ, or dark mode renders unreadably.
        assert_ne!(DEFAULT_TITLE_COLOR_LIGHT, DEFAULT_TITLE_COLOR_DARK);
    }

    #[test]
    fn from_system_style_carries_pathological_titles_through_untouched() {
        let ss = blank_system_style();
        for title in ADVERSARIAL_TITLES {
            let t = Titlebar::from_system_style(AzString::from(title), &ss);
            assert_eq!(t.title.as_str(), title);
            let csd = Titlebar::from_system_style_csd(AzString::from(title), &ss);
            assert_eq!(csd.title.as_str(), title);
        }
    }

    // ==================================================================
    // Titlebar::from_system_style_csd
    // ==================================================================

    #[test]
    fn from_system_style_csd_zeroes_the_padding_and_keeps_everything_else() {
        let mut ss = blank_system_style();
        ss.metrics.titlebar.height = OptionPixelValue::Some(PixelValue::px(41.0));
        ss.metrics.titlebar.title_font_size = OptionF32::Some(17.5);
        ss.theme = system::Theme::Dark;

        let title_only = Titlebar::from_system_style(AzString::from("x"), &ss);
        let csd = Titlebar::from_system_style_csd(AzString::from("x"), &ss);

        assert_eq!(csd.height, title_only.height);
        assert_eq!(csd.font_size, title_only.font_size);
        assert_eq!(csd.title_color, title_only.title_color);
        assert_eq!(csd.title_color, DEFAULT_TITLE_COLOR_DARK);

        // The buttons are DOM children in CSD mode, so no space is reserved.
        assert_eq!(csd.padding_left.to_bits(), 0_f32.to_bits());
        assert_eq!(csd.padding_right.to_bits(), 0_f32.to_bits());
    }

    #[test]
    fn from_system_style_csd_ignores_the_button_area_and_safe_area_entirely() {
        let mut ss = blank_system_style();
        ss.metrics.titlebar.button_area_width = OptionPixelValue::Some(PixelValue::px(500.0));
        ss.metrics.titlebar.padding_horizontal = OptionPixelValue::Some(PixelValue::px(77.0));
        ss.metrics.titlebar.safe_area = SafeAreaInsets {
            top: OptionPixelValue::Some(PixelValue::px(1.0)),
            bottom: OptionPixelValue::Some(PixelValue::px(2.0)),
            left: OptionPixelValue::Some(PixelValue::px(3.0)),
            right: OptionPixelValue::Some(PixelValue::px(4.0)),
        };

        let csd = Titlebar::from_system_style_csd(AzString::from("x"), &ss);
        assert_eq!(csd.padding_left, 0.0);
        assert_eq!(csd.padding_right, 0.0);
    }

    // ==================================================================
    // Titlebar::build_container_style
    // ==================================================================

    #[test]
    fn build_container_style_emits_the_documented_declarations_in_both_modes() {
        let t = tb("x");
        for show_buttons in [false, true] {
            let style = t.build_container_style(show_buttons);
            assert_eq!(
                properties(&style),
                expected_container(&t, show_buttons),
                "container declarations drifted (show_buttons = {show_buttons})",
            );
            assert!(all_unconditional(&style), "a container declaration became conditional");
        }
    }

    #[test]
    fn build_container_style_switches_flex_only_for_the_csd_mode() {
        let t = tb("x");

        let block = t.build_container_style(false);
        let flex = t.build_container_style(true);

        assert!(properties(&block).contains(&CssProperty::const_display(LayoutDisplay::Block)));
        assert!(properties(&flex).contains(&CssProperty::const_display(LayoutDisplay::Flex)));
        // Title-only mode must *not* declare flex layout — the doc comment says it
        // deliberately avoids flex-grow complexity.
        assert!(
            !properties(&block)
                .iter()
                .any(|p| matches!(p, CssProperty::FlexDirection(_) | CssProperty::AlignItems(_))),
            "title-only mode leaked flex declarations",
        );
        // Everything else is identical.
        assert_eq!(height_px(&block), height_px(&flex));
        assert_eq!(padding_left_px(&block), padding_left_px(&flex));
        assert_eq!(padding_right_px(&block), padding_right_px(&flex));
    }

    #[test]
    fn build_container_style_always_declares_the_grab_cursor_and_disables_selection() {
        // Without these a drag selects the title text instead of moving the window.
        for show_buttons in [false, true] {
            let style = tb("x").build_container_style(show_buttons);
            let props = properties(&style);
            assert!(props.contains(&CssProperty::const_cursor(StyleCursor::Grab)));
            assert!(props.contains(&CssProperty::user_select(StyleUserSelect::None)));
        }
    }

    #[test]
    fn build_container_style_truncates_the_height_toward_zero() {
        // `height as isize` truncates; a 30.9px titlebar is encoded as 30px.
        for (h, expected) in [
            (30.0_f32, 30.0_f32),
            (30.9, 30.0),
            (-30.9, -30.0),
            (0.0, 0.0),
            (-0.0, 0.0),
            (0.5, 0.0),
            (-0.5, 0.0),
            (0.999, 0.0),
        ] {
            let mut t = tb("x");
            t.set_height(h);
            assert_eq!(
                height_px(&t.build_container_style(false)),
                Some(expected),
                "height {h} encoded wrongly",
            );
        }
    }

    #[test]
    fn build_container_style_encodes_a_nan_height_as_zero_pixels() {
        // `NaN as isize` saturates to 0, so the encoding is defined rather than
        // propagating NaN into the layout solver.
        let mut t = tb("x");
        t.set_height(f32::NAN);
        assert_eq!(height_px(&t.build_container_style(false)), Some(0.0));
        assert_eq!(height_px(&t.build_container_style(true)), Some(0.0));
    }

    #[test]
    fn build_container_style_omits_padding_that_is_not_strictly_positive() {
        for pad in [0.0_f32, -0.0, -1.0, -1e30, f32::NAN, f32::NEG_INFINITY] {
            let mut t = tb("x");
            t.padding_left = pad;
            t.padding_right = pad;
            let style = t.build_container_style(false);
            assert_eq!(
                padding_left_px(&style),
                None,
                "padding-left {pad} must not be declared at all",
            );
            assert_eq!(padding_right_px(&style), None, "padding-right {pad} was declared");
        }
    }

    #[test]
    fn build_container_style_emits_sub_pixel_padding_as_a_zero_px_declaration() {
        // The `> 0.0` gate lets 0.4px through, and `as isize` then truncates it to
        // 0px: the declaration exists but reserves nothing.
        let mut t = tb("x");
        t.padding_left = 0.4;
        t.padding_right = 0.6;
        let style = t.build_container_style(false);
        assert_eq!(padding_left_px(&style), Some(0.0));
        assert_eq!(padding_right_px(&style), Some(0.0));
    }

    #[test]
    fn build_container_style_keeps_the_two_paddings_independent() {
        let mut t = tb("x");
        t.padding_left = 12.0;
        t.padding_right = 0.0;
        let style = t.build_container_style(true);
        assert_eq!(padding_left_px(&style), Some(12.0));
        assert_eq!(padding_right_px(&style), None);
    }

    // ==================================================================
    // Titlebar::build_title_style
    // ==================================================================

    #[test]
    fn build_title_style_emits_the_documented_declarations_in_both_modes() {
        let t = tb("x");
        for show_buttons in [false, true] {
            let style = t.build_title_style(show_buttons);
            assert_eq!(
                properties(&style),
                expected_title(&t, show_buttons),
                "title declarations drifted (show_buttons = {show_buttons})",
            );
            assert!(all_unconditional(&style), "a title declaration became conditional");
        }
    }

    #[test]
    fn build_title_style_only_grows_the_title_in_csd_mode() {
        // In the flex container the title must claim the space left by the buttons,
        // and `min-width: 0` is what lets it actually shrink below its text width.
        let t = tb("x");
        let flex = properties(&t.build_title_style(true));
        assert!(flex.contains(&CssProperty::const_flex_grow(LayoutFlexGrow::const_new(1))));
        assert!(flex.contains(&CssProperty::const_min_width(LayoutMinWidth::const_px(0))));

        let block = properties(&t.build_title_style(false));
        assert!(
            !block
                .iter()
                .any(|p| matches!(p, CssProperty::FlexGrow(_) | CssProperty::MinWidth(_))),
            "title-only mode leaked flex-grow / min-width",
        );
    }

    #[test]
    fn build_title_style_always_centres_clips_and_never_wraps() {
        for show_buttons in [false, true] {
            let props = properties(&tb("x").build_title_style(show_buttons));
            assert!(props.contains(&CssProperty::const_text_align(StyleTextAlign::Center)));
            assert!(props.contains(&CssProperty::WhiteSpace(StyleWhiteSpaceValue::Exact(
                StyleWhiteSpace::Nowrap
            ))));
            assert!(props.contains(&CssProperty::const_overflow_x(LayoutOverflow::Hidden)));
        }
    }

    #[test]
    fn build_title_style_forwards_the_resolved_title_colour_verbatim() {
        for c in [
            ColorU { r: 0, g: 0, b: 0, a: 0 },
            ColorU { r: 255, g: 255, b: 255, a: 255 },
            ColorU { r: 1, g: 2, b: 3, a: 4 },
            DEFAULT_TITLE_COLOR_DARK,
        ] {
            let mut t = tb("x");
            t.title_color = c;
            assert_eq!(text_color(&t.build_title_style(false)), Some(c));
            assert_eq!(text_color(&t.build_title_style(true)), Some(c));
        }
    }

    #[test]
    fn build_title_style_centres_vertically_with_half_the_leftover_height() {
        for (h, fs, expected) in [
            (30.0_f32, 13.0_f32, Some(8.0_f32)), // (30-13)/2 = 8.5 -> 8px
            (32.0, 12.0, Some(10.0)),
            (40.0, 20.0, Some(10.0)),
            (14.0, 13.0, Some(0.0)), // 0.5 -> declared, but 0px
        ] {
            let mut t = tb("x");
            t.set_height(h);
            t.font_size = fs;
            assert_eq!(
                padding_top_px(&t.build_title_style(false)),
                expected,
                "h={h} fs={fs} produced the wrong vertical padding",
            );
        }
    }

    #[test]
    fn build_title_style_omits_the_vertical_padding_when_the_text_does_not_fit() {
        // `.max(0.0)` must swallow the negative gap: a negative padding-top would
        // push the title above the titlebar.
        for (h, fs) in [
            (13.0_f32, 13.0_f32),
            (10.0, 20.0),
            (0.0, 13.0),
            (-100.0, 13.0),
            (f32::NEG_INFINITY, 13.0),
            (f32::NAN, 13.0),
            (13.0, f32::NAN),
        ] {
            let mut t = tb("x");
            t.set_height(h);
            t.font_size = fs;
            assert_eq!(
                padding_top_px(&t.build_title_style(false)),
                None,
                "h={h} fs={fs} declared a vertical padding it should have clamped away",
            );
        }
    }

    #[test]
    fn build_title_style_encodes_a_nan_font_size_as_zero_pixels() {
        let mut t = tb("x");
        t.font_size = f32::NAN;
        assert_eq!(font_size_px(&t.build_title_style(false)), Some(0.0));
    }

    #[test]
    fn build_title_style_truncates_the_font_size_toward_zero() {
        for (fs, expected) in [(13.0_f32, 13.0_f32), (13.9, 13.0), (0.5, 0.0), (-13.9, -13.0)] {
            let mut t = tb("x");
            t.font_size = fs;
            assert_eq!(font_size_px(&t.build_title_style(false)), Some(expected));
        }
    }

    // ==================================================================
    // The fixed-point encoding boundary
    // ==================================================================

    #[cfg(panic = "unwind")]
    #[test]
    fn heights_outside_the_encodable_range_are_not_saturated() {
        use std::{
            hint::black_box,
            panic::{catch_unwind, AssertUnwindSafe},
        };

        // LATENT BUG, pinned: `PixelValue::const_px` multiplies by 1000 with a
        // plain `*`, so any height/font-size whose `as isize` truncation exceeds
        // `isize::MAX / 1000` either panics (overflow checks on: a debug build
        // dies) or wraps to a garbage length (checks off) — it never saturates.
        // Asserted against a probe of the *current* profile so the test is
        // profile-independent; adding saturation flips it loudly.
        let profile_traps_overflow = catch_unwind(AssertUnwindSafe(|| {
            let big = black_box(isize::MAX);
            let _ = black_box(big * black_box(1000_isize));
        }))
        .is_err();

        for bogus in UNENCODABLE_FLOATS {
            let mut t = tb("x");
            t.set_height(bogus);
            let panicked =
                catch_unwind(AssertUnwindSafe(|| drop(t.build_container_style(false)))).is_err();
            assert_eq!(
                panicked, profile_traps_overflow,
                "height {bogus}: the fixed-point encoding no longer behaves like a raw multiply",
            );

            let mut f = tb("x");
            f.font_size = bogus;
            let panicked =
                catch_unwind(AssertUnwindSafe(|| drop(f.build_title_style(false)))).is_err();
            assert_eq!(
                panicked, profile_traps_overflow,
                "font size {bogus}: the fixed-point encoding no longer behaves like a raw multiply",
            );
        }
    }

    #[cfg(panic = "unwind")]
    #[test]
    fn an_unencodable_vertical_gap_reaches_the_padding_encoder_unclamped() {
        use std::{
            hint::black_box,
            panic::{catch_unwind, AssertUnwindSafe},
        };

        let profile_traps_overflow = catch_unwind(AssertUnwindSafe(|| {
            let big = black_box(isize::MAX);
            let _ = black_box(big * black_box(1000_isize));
        }))
        .is_err();

        // A *positive* unencodable height also blows up through `padding-top`,
        // because `(h - fs) / 2` is still unencodable. The negative ones are
        // clamped away by `.max(0.0)` and are therefore safe — asserted here so
        // the asymmetry is not mistaken for full coverage.
        for bogus in [f32::INFINITY, f32::MAX] {
            let mut t = tb("x");
            t.set_height(bogus);
            let panicked =
                catch_unwind(AssertUnwindSafe(|| drop(t.build_title_style(false)))).is_err();
            assert_eq!(panicked, profile_traps_overflow, "height {bogus} via padding-top");
        }

        for safe in [f32::NEG_INFINITY, f32::MIN] {
            let mut t = tb("x");
            t.set_height(safe);
            assert_eq!(
                padding_top_px(&t.build_title_style(false)),
                None,
                "height {safe} must be clamped away by .max(0.0)",
            );
        }
    }

    // ==================================================================
    // Titlebar::dom (title-only)
    // ==================================================================

    #[test]
    fn dom_builds_the_documented_title_only_tree() {
        let dom = tb("caption").dom();

        assert_eq!(classes(&dom), vec!["csd-titlebar", "__azul-native-titlebar"]);
        assert!(ids(&dom).is_empty(), "the container must not claim an id");
        assert_eq!(dom.children.as_ref().len(), 1, "title-only mode has exactly one child");

        let title = title_node(&dom);
        assert_eq!(classes(title), vec!["csd-title"]);
        assert_eq!(title.children.as_ref().len(), 1);
        assert_eq!(text_of(&title.children.as_ref()[0]), Some("caption"));
        assert!(buttons_node(&dom).is_none(), "title-only mode must render no buttons");
    }

    #[test]
    fn dom_puts_the_container_and_title_styles_on_the_right_nodes() {
        let t = tb("caption");
        let dom = t.clone().dom();

        assert_eq!(inline_props(&dom), expected_container(&t, false));
        assert_eq!(inline_props(title_node(&dom)), expected_title(&t, false));
        // The text node itself carries no styling of its own.
        assert!(inline_props(&title_node(&dom).children.as_ref()[0]).is_empty());
    }

    #[test]
    fn dom_registers_exactly_the_three_drag_callbacks_on_the_title_node() {
        let dom = tb("x").dom();

        assert!(
            callbacks_of(&dom).is_empty(),
            "the container must carry no callbacks — the title node owns the drag",
        );
        assert_eq!(
            callbacks_of(title_node(&dom)),
            vec![
                (
                    EventFilter::Hover(HoverEventFilter::DragStart),
                    callbacks::titlebar_drag_start as usize,
                ),
                (EventFilter::Hover(HoverEventFilter::Drag), callbacks::titlebar_drag as usize),
                (
                    EventFilter::Hover(HoverEventFilter::DoubleClick),
                    callbacks::titlebar_double_click as usize,
                ),
            ],
        );
    }

    #[test]
    fn dom_carries_pathological_titles_into_the_text_node_verbatim() {
        for title in ADVERSARIAL_TITLES {
            let dom = tb(title).dom();
            let text = &title_node(&dom).children.as_ref()[0];
            assert_eq!(text_of(text), Some(title), "the title was mangled on the way in");
            // Even an empty title still gets a text node, so the drag target exists.
            assert_eq!(title_node(&dom).children.as_ref().len(), 1);
        }
    }

    #[test]
    fn dom_keeps_the_cached_child_count_in_sync() {
        // A too-small `estimated_total_children` makes `convert_dom_into_compact_dom`
        // under-allocate and panic on an out-of-bounds write.
        let dom = tb("x").dom();
        assert_eq!(dom.estimated_total_children, count_descendants(&dom));
        assert_eq!(dom.estimated_total_children, 2, "title div + text node");
    }

    #[test]
    fn the_dom_conversion_is_exactly_dom() {
        for title in ADVERSARIAL_TITLES {
            let via_from: Dom = tb(title).into();
            assert_eq!(
                fingerprint(&via_from),
                fingerprint(&tb(title).dom()),
                "From<Titlebar> for Dom drifted away from Titlebar::dom",
            );
        }
    }

    // ==================================================================
    // Titlebar::dom_with_buttons / build_button_container
    // ==================================================================

    #[test]
    fn dom_with_buttons_orders_the_children_by_button_side() {
        let buttons = TitlebarButtons::default();

        let left = tb("x").dom_with_buttons(&buttons, TitlebarButtonSide::Left);
        let left_kids = left.children.as_ref();
        assert_eq!(left_kids.len(), 2);
        assert!(has_class(&left_kids[0], "csd-buttons"), "macOS puts the buttons first");
        assert!(has_class(&left_kids[1], "csd-title"));

        let right = tb("x").dom_with_buttons(&buttons, TitlebarButtonSide::Right);
        let right_kids = right.children.as_ref();
        assert_eq!(right_kids.len(), 2);
        assert!(has_class(&right_kids[0], "csd-title"), "Windows/Linux put the title first");
        assert!(has_class(&right_kids[1], "csd-buttons"));
    }

    #[test]
    fn dom_with_buttons_emits_one_node_per_enabled_button_in_minimize_maximize_close_order() {
        for buttons in all_button_combinations() {
            for side in BOTH_SIDES {
                let dom = tb("x").dom_with_buttons(&buttons, side);
                let container = buttons_node(&dom).expect("the CSD button container is mandatory");

                let mut expected: Vec<&str> = Vec::new();
                if buttons.has_minimize {
                    expected.push("csd-button-minimize");
                }
                if buttons.has_maximize {
                    expected.push("csd-button-maximize");
                }
                if buttons.has_close {
                    expected.push("csd-button-close");
                }

                let actual: Vec<String> = container
                    .children
                    .as_ref()
                    .iter()
                    .flat_map(ids)
                    .collect();
                assert_eq!(actual, expected, "{buttons:?} on {side:?} produced the wrong buttons");
            }
        }
    }

    #[test]
    fn has_fullscreen_is_never_rendered() {
        // The flag exists in `TitlebarButtons` but the widget has no fullscreen
        // button; toggling it must not change a single node.
        for &(close, min, max) in &[(true, true, true), (false, false, false), (true, false, true)]
        {
            let off = TitlebarButtons {
                has_close: close,
                has_minimize: min,
                has_maximize: max,
                has_fullscreen: false,
            };
            let on = TitlebarButtons { has_fullscreen: true, ..off };
            assert_eq!(
                fingerprint(&build_button_container(&off)),
                fingerprint(&build_button_container(&on)),
                "has_fullscreen changed the rendered buttons",
            );
        }
    }

    #[test]
    fn all_buttons_disabled_still_emits_an_empty_button_container() {
        let none = TitlebarButtons {
            has_close: false,
            has_minimize: false,
            has_maximize: false,
            has_fullscreen: false,
        };
        let container = build_button_container(&none);

        assert_eq!(classes(&container), vec!["csd-buttons"]);
        assert!(container.children.as_ref().is_empty());
        assert_eq!(container.estimated_total_children, 0);

        // ... and the full DOM still has both children in the documented order.
        let dom = tb("x").dom_with_buttons(&none, TitlebarButtonSide::Right);
        assert_eq!(dom.children.as_ref().len(), 2);
        assert!(buttons_node(&dom).is_some());
    }

    #[test]
    fn every_button_carries_one_mousedown_callback_and_the_matching_icon() {
        let expected: [(&str, &str, usize); 3] = [
            ("csd-button-minimize", "minimize", callbacks::csd_minimize as usize),
            ("csd-button-maximize", "maximize", callbacks::csd_maximize as usize),
            ("csd-button-close", "close", callbacks::csd_close as usize),
        ];

        let container = build_button_container(&TitlebarButtons::default());
        let kids = container.children.as_ref();
        assert_eq!(kids.len(), 3);

        for (node, (id, icon, cb)) in kids.iter().zip(expected) {
            assert_eq!(ids(node), vec![id]);
            assert_eq!(
                callbacks_of(node),
                vec![(EventFilter::Hover(HoverEventFilter::MouseDown), cb)],
                "{id} must carry exactly one MouseDown callback",
            );
            assert_eq!(node.children.as_ref().len(), 1);
            assert_eq!(
                icon_of(&node.children.as_ref()[0]),
                Some(icon),
                "{id} rendered the wrong icon",
            );
        }
    }

    #[test]
    fn every_button_carries_the_shared_and_the_specific_class() {
        let container = build_button_container(&TitlebarButtons::default());
        for (node, specific) in container
            .children
            .as_ref()
            .iter()
            .zip(["csd-minimize", "csd-maximize", "csd-close"])
        {
            assert_eq!(
                classes(node),
                vec!["csd-button".to_string(), specific.to_string()],
                "the stylesheet hooks documented on Titlebar are missing",
            );
        }
    }

    #[test]
    fn dom_with_buttons_keeps_the_cached_child_count_in_sync_for_every_combination() {
        for buttons in all_button_combinations() {
            for side in BOTH_SIDES {
                let dom = tb("x").dom_with_buttons(&buttons, side);
                assert_eq!(
                    dom.estimated_total_children,
                    count_descendants(&dom),
                    "{buttons:?} on {side:?} desynced the cached child count",
                );

                let enabled = usize::from(buttons.has_close)
                    + usize::from(buttons.has_minimize)
                    + usize::from(buttons.has_maximize);
                // title + text + button container + 2 nodes per enabled button
                assert_eq!(dom.estimated_total_children, 3 + 2 * enabled);
            }
        }
    }

    #[test]
    fn dom_with_buttons_uses_the_csd_container_and_title_styles() {
        let t = tb("x");
        let dom = t.clone().dom_with_buttons(&TitlebarButtons::default(), TitlebarButtonSide::Right);

        assert_eq!(inline_props(&dom), expected_container(&t, true));
        assert_eq!(inline_props(title_node(&dom)), expected_title(&t, true));
        // The button container is styled entirely from the stylesheet.
        assert!(inline_props(buttons_node(&dom).unwrap()).is_empty());
    }

    #[test]
    fn the_button_side_changes_only_the_child_order() {
        let buttons = TitlebarButtons::default();
        let left = tb("x").dom_with_buttons(&buttons, TitlebarButtonSide::Left);
        let right = tb("x").dom_with_buttons(&buttons, TitlebarButtonSide::Right);

        assert_eq!(inline_props(&left), inline_props(&right));
        assert_eq!(classes(&left), classes(&right));
        assert_eq!(
            fingerprint(title_node(&left)),
            fingerprint(title_node(&right)),
            "the title node must not depend on the button side",
        );
        assert_eq!(
            fingerprint(buttons_node(&left).unwrap()),
            fingerprint(buttons_node(&right).unwrap()),
            "the button container must not depend on the button side",
        );
    }

    #[test]
    fn dom_with_buttons_carries_pathological_titles_verbatim() {
        for title in ADVERSARIAL_TITLES {
            let dom = tb(title).dom_with_buttons(&TitlebarButtons::default(), TitlebarButtonSide::Left);
            let text = &title_node(&dom).children.as_ref()[0];
            assert_eq!(text_of(text), Some(title));
        }
    }

    // ==================================================================
    // callbacks::titlebar_drag_start
    // ==================================================================

    #[test]
    fn drag_start_with_an_unknown_position_hands_the_move_to_the_compositor() {
        // Wayland hides the window position, so the *only* way to move is
        // `xdg_toplevel_move` — the manual loop would be a silent no-op there.
        let (update, changes) = with_callback_info(
            state_with(WindowFrame::Normal, WindowPosition::Uninitialized),
            |info| callbacks::titlebar_drag_start(RefAny::new(()), info),
        );

        assert_eq!(update, Update::DoNothing);
        assert_eq!(interactive_moves(&changes), 1, "the compositor move was not requested");
        assert!(state_writes(&changes).is_empty(), "the native path must not write state");
    }

    #[test]
    fn drag_start_with_a_known_position_takes_the_platform_appropriate_path() {
        // macOS is documented to take the native path even with a known position
        // (performWindowDragWithEvent: is snap- and multi-monitor-aware); X11 and
        // Windows fall through to the manual per-event loop. `cfg!` rather than
        // `#[cfg]` so both branches keep type-checking on every target.
        let (update, changes) = with_callback_info(
            state_with(
                WindowFrame::Normal,
                WindowPosition::Initialized(PhysicalPositionI32::new(100, 200)),
            ),
            |info| callbacks::titlebar_drag_start(RefAny::new(()), info),
        );

        assert_eq!(update, Update::DoNothing);
        if cfg!(target_os = "macos") {
            assert_eq!(interactive_moves(&changes), 1);
        } else {
            assert_eq!(interactive_moves(&changes), 0, "the manual path must not ask the OS");
            assert!(
                changes.is_empty(),
                "a normal-frame manual drag start must record nothing at all",
            );
        }
    }

    #[test]
    fn drag_start_restores_a_maximized_window_before_the_manual_move() {
        // Dragging a maximized window has to un-maximize first, otherwise the
        // manual loop slides the still-maximized frame around the screen.
        let before = state_with(
            WindowFrame::Maximized,
            WindowPosition::Initialized(PhysicalPositionI32::new(0, 0)),
        );
        let (update, changes) = with_callback_info(before.clone(), |info| {
            callbacks::titlebar_drag_start(RefAny::new(()), info)
        });

        assert_eq!(update, Update::DoNothing);
        if cfg!(target_os = "macos") {
            assert_eq!(interactive_moves(&changes), 1);
            assert!(state_writes(&changes).is_empty());
        } else {
            let writes = state_writes(&changes);
            assert_eq!(writes.len(), 1, "the un-maximize write is missing");
            assert_eq!(writes[0].flags.frame, WindowFrame::Normal);

            // Nothing else may be touched on the way through.
            let mut expected = before;
            expected.flags.frame = WindowFrame::Normal;
            assert_eq!(writes[0], expected, "drag start changed more than the frame");
        }
    }

    #[test]
    fn drag_start_leaves_a_fullscreen_or_minimized_frame_alone() {
        for frame in [WindowFrame::Fullscreen, WindowFrame::Minimized, WindowFrame::Normal] {
            let (_, changes) = with_callback_info(
                state_with(frame, WindowPosition::Initialized(PhysicalPositionI32::new(1, 1))),
                |info| callbacks::titlebar_drag_start(RefAny::new(()), info),
            );
            if !cfg!(target_os = "macos") {
                assert!(
                    state_writes(&changes).is_empty(),
                    "{frame:?} must not be rewritten — only Maximized is restored",
                );
            }
        }
    }

    // ==================================================================
    // callbacks::titlebar_drag
    // ==================================================================

    #[test]
    fn drag_without_an_active_gesture_is_a_no_op() {
        // No drag is in flight, so `get_drag_delta_screen_incremental()` is None and
        // the if-let must not match — a callback that moved the window anyway would
        // teleport it on the first stray Drag event.
        for position in [
            WindowPosition::Uninitialized,
            WindowPosition::Initialized(PhysicalPositionI32::new(-5, 7)),
        ] {
            let (update, changes) =
                with_callback_info(state_with(WindowFrame::Normal, position), |info| {
                    callbacks::titlebar_drag(RefAny::new(()), info)
                });
            assert_eq!(update, Update::DoNothing);
            assert!(changes.is_empty(), "{position:?}: a no-delta drag recorded a change");
        }
    }

    #[test]
    fn drag_is_idempotent_when_repeated_without_a_gesture() {
        for _ in 0..4 {
            let (update, changes) = with_callback_info(
                state_with(
                    WindowFrame::Maximized,
                    WindowPosition::Initialized(PhysicalPositionI32::new(i32::MAX, i32::MIN)),
                ),
                |info| callbacks::titlebar_drag(RefAny::new(()), info),
            );
            assert_eq!(update, Update::DoNothing);
            // Extreme coordinates must not tempt the callback into arithmetic it
            // was never asked to do.
            assert!(changes.is_empty());
        }
    }

    // ==================================================================
    // callbacks::titlebar_double_click / csd_maximize
    // ==================================================================

    #[test]
    fn double_click_toggles_maximized_and_normalises_every_other_frame() {
        for frame in ALL_FRAMES {
            let before = state_with(frame, WindowPosition::Uninitialized);
            let (update, changes) = with_callback_info(before.clone(), |info| {
                callbacks::titlebar_double_click(RefAny::new(()), info)
            });

            assert_eq!(update, Update::DoNothing);
            let writes = state_writes(&changes);
            assert_eq!(writes.len(), 1, "{frame:?}: exactly one state write expected");

            let expected_frame = if frame == WindowFrame::Maximized {
                WindowFrame::Normal
            } else {
                WindowFrame::Maximized
            };
            assert_eq!(writes[0].flags.frame, expected_frame, "{frame:?} toggled wrongly");

            let mut expected = before;
            expected.flags.frame = expected_frame;
            assert_eq!(writes[0], expected, "{frame:?}: more than the frame changed");
        }
    }

    #[test]
    fn double_clicking_twice_returns_to_the_original_frame() {
        let (_, changes) = with_callback_info(
            state_with(WindowFrame::Normal, WindowPosition::Uninitialized),
            |info| callbacks::titlebar_double_click(RefAny::new(()), info),
        );
        let once = state_writes(&changes).remove(0);
        assert_eq!(once.flags.frame, WindowFrame::Maximized);

        let (_, changes) = with_callback_info(once, |info| {
            callbacks::titlebar_double_click(RefAny::new(()), info)
        });
        assert_eq!(state_writes(&changes)[0].flags.frame, WindowFrame::Normal);
    }

    #[test]
    fn the_maximize_button_agrees_with_the_double_click_for_every_frame() {
        for frame in ALL_FRAMES {
            let before = state_with(frame, WindowPosition::Uninitialized);
            let (_, via_button) = with_callback_info(before.clone(), |info| {
                callbacks::csd_maximize(RefAny::new(()), info)
            });
            let (_, via_double) = with_callback_info(before, |info| {
                callbacks::titlebar_double_click(RefAny::new(()), info)
            });
            assert_eq!(
                state_writes(&via_button),
                state_writes(&via_double),
                "{frame:?}: the maximize button and the double-click diverged",
            );
        }
    }

    // ==================================================================
    // callbacks::csd_close / csd_minimize
    // ==================================================================

    #[test]
    fn close_sets_close_requested_and_nothing_else() {
        for frame in ALL_FRAMES {
            let before = state_with(frame, WindowPosition::Uninitialized);
            assert!(!before.flags.close_requested, "fixture must start un-closed");

            let (update, changes) =
                with_callback_info(before.clone(), |info| callbacks::csd_close(RefAny::new(()), info));

            assert_eq!(update, Update::DoNothing);
            let writes = state_writes(&changes);
            assert_eq!(writes.len(), 1);
            assert!(writes[0].flags.close_requested);
            assert_eq!(writes[0].flags.frame, frame, "close must not move the frame");

            let mut expected = before;
            expected.flags.close_requested = true;
            assert_eq!(writes[0], expected, "close changed more than close_requested");
        }
    }

    #[test]
    fn close_is_idempotent_on_an_already_closing_window() {
        let mut before = state_with(WindowFrame::Normal, WindowPosition::Uninitialized);
        before.flags.close_requested = true;

        let (_, changes) =
            with_callback_info(before.clone(), |info| callbacks::csd_close(RefAny::new(()), info));

        assert_eq!(state_writes(&changes), vec![before], "a second close must be a re-assert");
    }

    #[test]
    fn minimize_always_minimizes_regardless_of_the_current_frame() {
        for frame in ALL_FRAMES {
            let before = state_with(frame, WindowPosition::Uninitialized);
            let (update, changes) = with_callback_info(before.clone(), |info| {
                callbacks::csd_minimize(RefAny::new(()), info)
            });

            assert_eq!(update, Update::DoNothing);
            let writes = state_writes(&changes);
            assert_eq!(writes.len(), 1);
            assert_eq!(writes[0].flags.frame, WindowFrame::Minimized, "{frame:?} was not minimized");

            let mut expected = before;
            expected.flags.frame = WindowFrame::Minimized;
            assert_eq!(writes[0], expected, "{frame:?}: minimize changed more than the frame");
        }
    }

    #[test]
    fn minimize_never_requests_a_close() {
        let (_, changes) = with_callback_info(
            state_with(WindowFrame::Normal, WindowPosition::Uninitialized),
            |info| callbacks::csd_minimize(RefAny::new(()), info),
        );
        assert!(!state_writes(&changes)[0].flags.close_requested);
        assert_eq!(interactive_moves(&changes), 0);
    }
}