rust_widgets 2.9.2

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 180 widgets, touch/gesture support, i18n, and SVG-pipeline-accurate output
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
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

//! DateRangePicker widget — a calendar-based date range selection widget.
//!
//! The DateRangePicker displays a calendar month grid where users click to
//! select a start date and then click again to select an end date, forming
//! a date range. The range between the two dates is visually highlighted.
//! A `range_changed` signal is emitted whenever the selection changes.

use super::date_utils::{days_in_month, parse_iso_date, DAY_NAMES, MONTH_NAMES};
use crate::core::{Color, HorizontalAlignment, Point, Rect};
use crate::event::{Event, EventHandler};
use crate::render::{RenderContext, TEXT_FIT_MARGIN};
use crate::signal::Signal1;
use crate::widget::capability::coercion::{expect_string, expect_string_list};
use crate::widget::capability::properties_trait::{base_property_get, base_property_set};
use crate::widget::capability::types::{CapabilityAccessError, CapabilityValue};
use crate::widget::capability::WidgetProperties;
use crate::widget::metrics::role_font;
use crate::widget::{BaseWidget, Draw, IconName, Widget, WidgetKind};
use crate::{impl_widget_property_hooks, property_names_of};

use chrono::Datelike;

/// A named shortcut range offered above the calendar grid.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DatePreset {
    /// The chip's caption.
    pub label: String,
    /// Inclusive first day, or `None` when the preset only sets an anchor.
    pub start: Option<(i32, u32, u32)>,
    /// Inclusive last day, or `None`.
    pub end: Option<(i32, u32, u32)>,
}

impl DatePreset {
    /// Creates a preset from its label and endpoints.
    pub fn new(label: impl Into<String>, start: (i32, u32, u32), end: (i32, u32, u32)) -> Self {
        Self { label: label.into(), start: Some(start), end: Some(end) }
    }
}

/// A selected date range: `(start_date, end_date)` each as `(year, month, day)`.
pub(crate) type DateRange = (Option<(i32, u32, u32)>, Option<(i32, u32, u32)>);

/// Returns the day of week (0=Sunday, 1=Monday, ..., 6=Saturday)
/// for the given date using Zeller-like / Tomohiko Sakamoto's algorithm.
fn day_of_week(year: i32, month: u32, day: u32) -> u32 {
    let (y, m) = if month < 3 { (year - 1, month + 12) } else { (year, month) };
    let c = y / 100;
    let y = y % 100;
    let d = day as i32;
    let w = (d + (13 * (m as i32 + 1)) / 5 + y + y / 4 + c / 4 - 2 * c) % 7;
    // Zeller's congruence numbers the week **Saturday = 0**, while `DAY_NAMES` and the
    // leading-blank count are Sunday-first. Returning the raw value shifted the whole grid
    // one column: January 2025 (whose 1st is a Wednesday) drew its 1 under "Th", and every
    // click then selected the date one day after the cell under the pointer. Adding 6 and
    // reducing modulo 7 converts Saturday-based to Sunday-based (Wed 4 → 3); `rem_euclid`
    // keeps proleptic years with a negative weekday from producing a negative column.
    (w + 6).rem_euclid(7) as u32
}

/// DateRangePicker widget — select a date range by clicking start/end dates.
///
/// Displays a calendar for a given month/year. The user clicks once to set
/// the start date, and clicks again to set the end date. The range is
/// visually highlighted. Clicking outside the grid (or an already-selected
/// start) with no end date clears the selection.
pub struct DateRangePicker {
    base: BaseWidget,
    display_year: i32,
    display_month: u32,
    start_date: Option<(i32, u32, u32)>,
    end_date: Option<(i32, u32, u32)>,
    hover_date: Option<(i32, u32, u32)>,
    /// Preset chips offered above the grid.
    presets: Vec<DatePreset>,
    /// Whether a second consecutive month is painted beside the first.
    double_month: bool,
    /// Minimum nights between the endpoints; `None` for no minimum.
    min_nights: Option<u32>,
    /// Maximum nights between the endpoints; `None` for no maximum.
    max_nights: Option<u32>,
    /// Emitted when the range changes, with `(start_date, end_date)`.
    pub range_changed: Signal1<DateRange>,
    /// Emitted with the preset index when a preset chip is chosen.
    pub preset_selected: Signal1<usize>,
    /// The earliest selectable day, inclusive; `None` for no lower bound.
    minimum_date: Option<(i32, u32, u32)>,
    /// The latest selectable day, inclusive; `None` for no upper bound.
    maximum_date: Option<(i32, u32, u32)>,
    /// The day the keyboard is on, for the grid focus the arrow keys move.
    ///
    /// `None` until the keyboard or a click establishes it; Enter then applies it through the same
    /// path a click does, so the two ways of choosing cannot diverge.
    focus_date: Option<(i32, u32, u32)>,
    /// Cached numeral widths and header line height (K-P12).
    digit_metrics: crate::compat::RefCell<DigitMetrics>,
}

/// A font's identity, as far as text measurement is concerned.
type FontKey = (u32, u16, u64);

/// The digit and header measurements one font pair produced (K-P12).
///
/// `draw_month_panel` measured each of its 42 day numerals with `measure_text`, and re-measured
/// `"M"` for the weekday header, on every frame. The numerals are the fixed strings `"1"..="31"`
/// and the height is the font's own line height, so the whole table depends only on the two fonts.
#[derive(Default)]
struct DigitMetrics {
    /// The day font the line height was measured with.
    day_key: Option<FontKey>,
    /// The line height of the day font, which every numeral shares.
    day_height: u32,
    /// The weekday-header font the height was measured with.
    header_key: Option<FontKey>,
    /// The line height of the weekday-header font.
    header_height: u32,
}

/// `(size bits, weight, family hash)` for a font.
fn font_key(font: &crate::core::Font) -> FontKey {
    let mut family_hash: u64 = 0xcbf2_9ce4_8422_2325;
    for byte in font.family().as_bytes() {
        family_hash ^= u64::from(*byte);
        family_hash = family_hash.wrapping_mul(0x0000_0100_0000_01b3);
    }
    (font.size().to_bits(), font.weight(), family_hash)
}

impl DateRangePicker {
    /// Creates a new DateRangePicker widget with the given geometry.
    ///
    /// The displayed month is the current month.
    pub fn new(geometry: Rect) -> Self {
        let today = chrono::Local::now().date_naive();
        Self {
            base: BaseWidget::new(WidgetKind::DateRangePicker, geometry, "DateRangePicker"),
            display_year: today.year(),
            display_month: today.month(),
            start_date: None,
            end_date: None,
            hover_date: None,
            presets: Vec::new(),
            double_month: false,
            min_nights: None,
            max_nights: None,
            range_changed: Signal1::new(),
            preset_selected: Signal1::new(),
            minimum_date: None,
            maximum_date: None,
            focus_date: None,
            digit_metrics: crate::compat::RefCell::new(DigitMetrics::default()),
        }
    }

    /// Points the grid at the current month without changing the selection.
    pub fn set_current_month(&mut self) {
        let today = chrono::Local::now().date_naive();
        self.display_year = today.year();
        self.display_month = today.month();
        self.base.request_redraw();
    }

    /// Moves the grid to an explicit year/month without changing the selection.
    ///
    /// The public peer of the arrow buttons: a host that restores a saved view, or jumps to a
    /// reservation's month, states the page instead of stepping to it.
    pub fn set_display_page(&mut self, year: i32, month: u32) {
        let month = month.clamp(1, 12);
        if self.display_year == year && self.display_month == month {
            return;
        }
        self.display_year = year;
        self.display_month = month;
        self.base.request_redraw();
    }

    /// Moves the grid to the current month and selects today as a single-day range.
    pub fn go_to_today(&mut self) {
        let today = chrono::Local::now().date_naive();
        self.display_year = today.year();
        self.display_month = today.month();
        let date = (today.year(), today.month(), today.day());
        self.start_date = Some(date);
        self.end_date = Some(date);
        self.emit_range_changed();
        self.base.request_redraw();
    }

    /// The preset chips.
    pub fn presets(&self) -> &[DatePreset] {
        &self.presets
    }

    /// Replaces the preset chips.
    pub fn set_presets(&mut self, presets: Vec<DatePreset>) {
        if self.presets == presets {
            return;
        }
        self.presets = presets;
        self.base.request_redraw();
    }

    /// Whether a second consecutive month is painted.
    pub fn double_month(&self) -> bool {
        self.double_month
    }

    /// Enables or disables the side-by-side second month.
    pub fn set_double_month(&mut self, enabled: bool) {
        if self.double_month == enabled {
            return;
        }
        self.double_month = enabled;
        self.base.request_redraw();
    }

    /// The minimum nights constraint, if any.
    pub fn min_nights(&self) -> Option<u32> {
        self.min_nights
    }

    /// Sets the minimum nights constraint (`0` clears it).
    pub fn set_min_nights(&mut self, nights: Option<u32>) {
        let nights = nights.filter(|n| *n > 0);
        if self.min_nights == nights {
            return;
        }
        self.min_nights = nights;
        self.base.request_redraw();
    }

    /// The maximum nights constraint, if any.
    pub fn max_nights(&self) -> Option<u32> {
        self.max_nights
    }

    /// Sets the maximum nights constraint (`0` clears it).
    pub fn set_max_nights(&mut self, nights: Option<u32>) {
        let nights = nights.filter(|n| *n > 0);
        if self.max_nights == nights {
            return;
        }
        self.max_nights = nights;
        self.base.request_redraw();
    }

    /// The height the preset strip reserves.
    fn preset_height(&self) -> u32 {
        if self.presets.is_empty() {
            0
        } else {
            PRESET_HEIGHT
        }
    }

    /// The box of preset `index`, when presets are shown.
    pub fn preset_rect(&self, index: usize) -> Option<Rect> {
        if index >= self.presets.len() {
            return None;
        }
        let rect = self.geometry();
        let width = rect.width / self.presets.len() as u32;
        Some(Rect::new(
            rect.x + (index as u32 * width) as i32,
            rect.y + 2,
            width.saturating_sub(2),
            PRESET_HEIGHT.saturating_sub(4),
        ))
    }

    /// Chooses preset `index`, applying its dates and reporting the choice.
    pub fn select_preset(&mut self, index: usize) {
        let Some(preset) = self.presets.get(index).cloned() else {
            return;
        };
        self.start_date = preset.start;
        self.end_date = preset.end;
        if let Some((year, month, _)) = preset.start.or(preset.end) {
            self.display_year = year;
            self.display_month = month;
        }
        self.emit_range_changed();
        self.preset_selected.emit(index);
        self.base.request_redraw();
    }

    /// Applies `preset` to the selection.
    ///
    /// When the preset's label matches one already configured through
    /// [`DateRangePicker::set_presets`], the existing selection path runs and `preset_selected`
    /// carries that index — that is the wiring between a [`RangePresetBar`] and this picker. A
    /// preset that is not in the picker's own list is still applied (the range is the useful part)
    /// but no index can honestly be reported, so the return value is `None`.
    pub fn apply_preset(&mut self, preset: &RangePreset) -> Option<usize> {
        if let Some(index) = self.presets.iter().position(|own| own.label == preset.label) {
            self.select_preset(index);
            return Some(index);
        }
        self.start_date = Some(preset.start);
        self.end_date = Some(preset.end);
        self.display_year = preset.start.0;
        self.display_month = preset.start.1;
        self.emit_range_changed();
        self.base.request_redraw();
        None
    }

    /// The `(year, month)` shown by panel `panel` (`0` is the primary month).
    fn panel_month(&self, panel: usize) -> (i32, u32) {
        if panel == 0 {
            return (self.display_year, self.display_month);
        }
        if self.display_month == 12 {
            (self.display_year + 1, 1)
        } else {
            (self.display_year, self.display_month + 1)
        }
    }

    /// Number of month panels painted.
    fn panel_count(&self) -> usize {
        if self.double_month {
            2
        } else {
            1
        }
    }

    /// Whether a completed range satisfies the night constraints.
    fn nights_allowed(&self, start: (i32, u32, u32), end: (i32, u32, u32)) -> bool {
        let Some(nights) = Self::nights_between(start, end) else {
            return false;
        };
        if self.min_nights.is_some_and(|minimum| nights < minimum) {
            return false;
        }
        if self.max_nights.is_some_and(|maximum| nights > maximum) {
            return false;
        }
        true
    }

    /// The night count between two dates, when both are real calendar days.
    fn nights_between(start: (i32, u32, u32), end: (i32, u32, u32)) -> Option<u32> {
        let a = chrono::NaiveDate::from_ymd_opt(start.0, start.1, start.2)?;
        let b = chrono::NaiveDate::from_ymd_opt(end.0, end.1, end.2)?;
        let nights = (b - a).num_days();
        u32::try_from(nights).ok()
    }

    /// Returns the current start date, if set.
    pub fn start_date(&self) -> Option<(i32, u32, u32)> {
        self.start_date
    }

    /// Sets the start date and emits `range_changed` if it changes.
    pub fn set_start_date(&mut self, date: Option<(i32, u32, u32)>) {
        if self.start_date != date {
            self.start_date = date;
            self.emit_range_changed();
            self.base.request_redraw();
        }
    }

    /// Returns the current end date, if set.
    pub fn end_date(&self) -> Option<(i32, u32, u32)> {
        self.end_date
    }

    /// Sets the end date and emits `range_changed` if it changes.
    pub fn set_end_date(&mut self, date: Option<(i32, u32, u32)>) {
        if self.end_date != date {
            self.end_date = date;
            self.emit_range_changed();
            self.base.request_redraw();
        }
    }

    /// The earliest selectable day, inclusive.
    pub fn minimum_date(&self) -> Option<(i32, u32, u32)> {
        self.minimum_date
    }

    /// Sets the earliest selectable day; `None` removes the bound.
    ///
    /// The bound is enforced on **both** selection paths (pointer and keyboard) through
    /// [`Self::accepts_date`], and a cell outside it paints in the disabled ink, so a user can see
    /// why a day is not selectable instead of clicking an inert cell.
    pub fn set_minimum_date(&mut self, date: Option<(i32, u32, u32)>) {
        if self.minimum_date == date {
            return;
        }
        self.minimum_date = date;
        self.base.request_redraw();
    }

    /// The latest selectable day, inclusive.
    pub fn maximum_date(&self) -> Option<(i32, u32, u32)> {
        self.maximum_date
    }

    /// Sets the latest selectable day; `None` removes the bound.
    pub fn set_maximum_date(&mut self, date: Option<(i32, u32, u32)>) {
        if self.maximum_date == date {
            return;
        }
        self.maximum_date = date;
        self.base.request_redraw();
    }

    /// The cached digit widths and line heights for the panel's two fonts (K-P12).
    ///
    /// The table costs 31 measurements when a font changes and nothing afterwards; the draw path
    /// then reads it instead of measuring each numeral.
    fn digit_metrics(
        &self,
        dow_font: &crate::core::Font,
        day_font: &crate::core::Font,
        context: &RenderContext,
    ) -> core::cell::RefMut<'_, DigitMetrics> {
        let day_key = font_key(day_font);
        let header_key = font_key(dow_font);
        let mut cache = self.digit_metrics.borrow_mut();
        if cache.day_key != Some(day_key) {
            // Every numeral's line height is the font's own, so the 42 per-cell measurements only
            // ever answered this one question.
            cache.day_height = context.measure_text("M", day_font).height;
            cache.day_key = Some(day_key);
        }
        if cache.header_key != Some(header_key) {
            cache.header_height = context.measure_text("M", dow_font).height;
            cache.header_key = Some(header_key);
        }
        core::cell::RefMut::map(cache, |cache| cache)
    }

    /// Whether `date` lies inside the configured bounds.
    pub fn accepts_date(&self, date: (i32, u32, u32)) -> bool {
        if self.minimum_date.is_some_and(|minimum| date_to_ordinal(date) < date_to_ordinal(minimum))
        {
            return false;
        }
        if self.maximum_date.is_some_and(|maximum| date_to_ordinal(date) > date_to_ordinal(maximum))
        {
            return false;
        }
        true
    }

    /// The day the keyboard grid focus is on, if any.
    pub fn focus_date(&self) -> Option<(i32, u32, u32)> {
        self.focus_date
    }

    /// Moves the keyboard focus by `days` and `weeks`, flipping the page when the new day is in
    /// another month.
    fn move_focus(&mut self, days: i64, weeks: i64) {
        let Some(current) = self.focus_date.or(self.start_date) else {
            // No focus and no selection: start on the first selectable day of the visible month.
            if let Some(day) = self.first_selectable_day(self.display_year, self.display_month) {
                self.focus_date = Some(day);
                self.base.request_redraw();
            }
            return;
        };
        let Some(mut naive) = chrono::NaiveDate::from_ymd_opt(current.0, current.1, current.2)
        else {
            return;
        };
        let delta = days + weeks * 7;
        if delta > 0 {
            for _ in 0..delta {
                naive = naive.succ_opt().unwrap_or(naive);
            }
        } else {
            for _ in 0..(-delta) {
                naive = naive.pred_opt().unwrap_or(naive);
            }
        }
        let next = (naive.year(), naive.month(), naive.day());
        if self.focus_date != Some(next) {
            self.focus_date = Some(next);
            // Keep the focused day on screen: the grid only shows the displayed month (or two).
            if next.0 != self.display_year || next.1 != self.display_month {
                self.display_year = next.0;
                self.display_month = next.1;
            }
            self.base.request_redraw();
        }
    }

    /// The first day of `year`/`month` that the bounds accept.
    fn first_selectable_day(&self, year: i32, month: u32) -> Option<(i32, u32, u32)> {
        for day in 1..=31 {
            let date = (year, month, day);
            if chrono::NaiveDate::from_ymd_opt(year, month, day).is_none() {
                break;
            }
            if self.accepts_date(date) {
                return Some(date);
            }
        }
        None
    }

    /// Applies a day through the same three-state path a pointer click uses.
    ///
    /// This is the shared commit the keyboard path runs, so Enter cannot mean something the click
    /// does not: first pick sets the start, second completes (or restarts) the range, third
    /// restarts it.
    fn apply_day(&mut self, clicked_date: (i32, u32, u32)) {
        if !self.accepts_date(clicked_date) {
            return;
        }
        match (self.start_date, self.end_date) {
            (None, _) => {
                self.start_date = Some(clicked_date);
                self.emit_range_changed();
                self.base.request_redraw();
            }
            (Some(start), None) => {
                let start_ord = date_to_ordinal(start);
                let click_ord = date_to_ordinal(clicked_date);
                let (new_start, new_end) = if click_ord > start_ord {
                    (start, clicked_date)
                } else if click_ord < start_ord {
                    (clicked_date, start)
                } else {
                    self.clear_selection();
                    return;
                };
                if !self.nights_allowed(new_start, new_end) {
                    return;
                }
                self.start_date = Some(new_start);
                self.end_date = Some(new_end);
                self.emit_range_changed();
                self.base.request_redraw();
            }
            (Some(_), Some(_)) => {
                self.start_date = Some(clicked_date);
                self.end_date = None;
                self.emit_range_changed();
                self.base.request_redraw();
            }
        }
    }

    /// Clears both start and end dates.
    pub fn clear_selection(&mut self) {
        if self.start_date.is_some() || self.end_date.is_some() {
            self.start_date = None;
            self.end_date = None;
            self.emit_range_changed();
            self.base.request_redraw();
        }
    }

    /// Navigates to the previous month.
    pub fn previous_month(&mut self) {
        if self.display_month == 1 {
            self.display_year -= 1;
            self.display_month = 12;
        } else {
            self.display_month -= 1;
        }
        self.base.request_redraw();
    }

    /// Navigates to the next month.
    pub fn next_month(&mut self) {
        if self.display_month == 12 {
            self.display_year += 1;
            self.display_month = 1;
        } else {
            self.display_month += 1;
        }
        self.base.request_redraw();
    }

    /// Returns the currently displayed year.
    pub fn display_year(&self) -> i32 {
        self.display_year
    }

    /// Returns the currently displayed month.
    pub fn display_month(&self) -> u32 {
        self.display_month
    }

    /// Emits the `range_changed` signal with current start/end.
    fn emit_range_changed(&self) {
        self.range_changed.emit((self.start_date, self.end_date));
    }

    /// Returns the number of days in panel `panel`'s month.
    fn days_in_panel_month(&self, panel: usize) -> u32 {
        let (year, month) = self.panel_month(panel);
        days_in_month(year, month)
    }

    /// Returns the day of week for the 1st of the displayed month.
    #[cfg(test)]
    fn first_day_of_week(&self) -> u32 {
        day_of_week(self.display_year, self.display_month, 1)
    }

    /// Returns the day of week for the 1st of panel `panel`'s month.
    fn first_day_of_panel(&self, panel: usize) -> u32 {
        let (year, month) = self.panel_month(panel);
        day_of_week(year, month, 1)
    }

    /// Returns the day, month, year for a cell in panel `panel`'s grid.
    fn date_at_panel_cell(&self, panel: usize, cell_index: usize) -> Option<(i32, u32, u32)> {
        let first_dow = self.first_day_of_panel(panel) as usize;
        let total_days = self.days_in_panel_month(panel) as usize;
        if cell_index < first_dow || cell_index >= first_dow + total_days {
            return None;
        }
        let day = (cell_index - first_dow + 1) as u32;
        let (year, month) = self.panel_month(panel);
        Some((year, month, day))
    }

    /// Returns the day, month, year for a given cell position in the primary grid.
    #[cfg(test)]
    fn date_at_cell(&self, cell_index: usize) -> Option<(i32, u32, u32)> {
        self.date_at_panel_cell(0, cell_index)
    }

    /// Returns the grid cell index under `pos`, or `None` when the point is outside the
    /// calendar grid.
    ///
    /// Shared by the press and hover paths so a pointer can never highlight a different
    /// cell from the one a click would select. The returned index is `(panel, cell)`.
    fn cell_at_position(&self, pos: Point) -> Option<(usize, usize)> {
        for panel in 0..self.panel_count() {
            let layout = self.grid_layout_for(panel);
            let rel_x = pos.x - layout.grid_left;
            let rel_y = pos.y - layout.grid_top;
            if rel_x < 0 || rel_y < 0 {
                continue;
            }
            let col = (rel_x as u32) / layout.column_pitch;
            let row = (rel_y as u32) / layout.row_pitch;
            if col >= 7 || row >= 6 {
                continue;
            }
            let cell_idx = (row as usize) * 7 + (col as usize);
            if cell_idx < layout.total_cells {
                return Some((panel, cell_idx));
            }
        }
        None
    }

    /// The date under `pos`, if the point is on a day cell.
    fn date_at_position(&self, pos: Point) -> Option<(i32, u32, u32)> {
        self.cell_at_position(pos).and_then(|(panel, cell)| self.date_at_panel_cell(panel, cell))
    }

    /// Checks if a date falls within the current selection range.
    fn is_in_range(&self, date: (i32, u32, u32)) -> bool {
        let Some(start) = self.start_date else {
            return false;
        };
        if date_to_ordinal(date) < date_to_ordinal(start) {
            return false;
        }
        if let Some(end) = self.end_date {
            date_to_ordinal(date) <= date_to_ordinal(end)
        } else {
            // If no end date, highlight just the start
            date == start
        }
    }

    /// The calendar grid's geometry for the current month and rectangle.
    ///
    /// Shared by the draw and the hit test, because a click must land in the cell that is
    /// visibly under it: the two used to derive the grid independently from the same
    /// literals, so any change to one silently desynchronised the other.
    ///
    /// The cell size is derived from the room the grid actually has rather than being a
    /// fixed 28 px. A month can need five or six week rows, so a fixed cell walked the last
    /// row — and the labels in it — past the control's bottom edge, where the raster
    /// backends clipped it and the SVG snapshot showed it leaving the picture. The nominal
    /// size is kept as the *maximum*, so a roomy control is unchanged.
    #[cfg(test)]
    fn grid_layout(&self) -> GridLayout {
        self.grid_layout_for(0)
    }

    /// The grid geometry for one month panel.
    fn grid_layout_for(&self, panel: usize) -> GridLayout {
        let rect = self.geometry();
        let panels = self.panel_count() as u32;
        let panel_width = rect.width / panels.max(1);
        let panel_x = rect.x + (panel as u32 * panel_width) as i32;
        let total_days = self.days_in_panel_month(panel) as usize;
        let first_dow = self.first_day_of_panel(panel) as usize;
        let rows = ((first_dow + total_days).div_ceil(7) as u32).max(1);
        let grid_left = panel_x + GRID_LEFT_INSET;
        let preset = self.preset_height() as i32;
        let grid_top = rect.y + preset + HEADER_HEIGHT as i32 + DAY_HEADER_HEIGHT as i32;
        let available_height = (rect.y + rect.height as i32 - grid_top).max(1) as u32;
        let gaps = CELL_SPACING * (rows - 1);
        let cell_size = (available_height.saturating_sub(gaps) / rows).clamp(1, NOMINAL_CELL);
        let available_width =
            (panel_x + panel_width as i32 - grid_left - GRID_RIGHT_INSET).max(1) as u32;
        let column_gaps = CELL_SPACING * 6;
        let cell_width = (available_width.saturating_sub(column_gaps) / 7).clamp(1, NOMINAL_CELL);
        GridLayout {
            grid_left,
            grid_top,
            cell_size,
            cell_width,
            row_pitch: cell_size + CELL_SPACING,
            column_pitch: cell_width + CELL_SPACING,
            total_cells: first_dow + total_days,
        }
    }
}

/// Reserved height of the preset chip strip.
const PRESET_HEIGHT: u32 = 22;
/// Reserved height of the month/year header band.
const HEADER_HEIGHT: u32 = 40;
/// Reserved height of the weekday row.
const DAY_HEADER_HEIGHT: u32 = 20;
/// Left inset of the grid from the control's edge.
const GRID_LEFT_INSET: i32 = 4;
/// Right inset of the grid from the control's edge.
const GRID_RIGHT_INSET: i32 = 4;
/// Gap between calendar cells.
const CELL_SPACING: u32 = 2;
/// The largest a calendar cell is allowed to be.
const NOMINAL_CELL: u32 = 28;

/// The month name for `month` (`1`–`12`), clamped into the table.
fn month_name_for(month: u32) -> &'static str {
    let idx = (month as usize).saturating_sub(1);
    MONTH_NAMES[idx.min(11)]
}

/// The calendar grid's resolved geometry.
struct GridLayout {
    /// Absolute x of the first column.
    grid_left: i32,
    /// Absolute y of the first week row.
    grid_top: i32,
    /// Edge length of one day cell (the row height limit).
    cell_size: u32,
    /// Width of one day cell, from the grid's width.
    cell_width: u32,
    /// Row pitch: the cell height plus its gap.
    row_pitch: u32,
    /// Column pitch: the cell width plus its gap.
    column_pitch: u32,
    /// Cells to draw, including the leading blanks of the first week.
    total_cells: usize,
}

/// Converts a date to a single ordinal number for easy comparison.
fn date_to_ordinal(date: (i32, u32, u32)) -> i64 {
    date.0 as i64 * 10000 + date.1 as i64 * 100 + date.2 as i64
}

/// Formats a date as an ISO `YYYY-MM-DD` string.
///
/// Thin wrapper over [`date_utils::format_iso_date`] so this module can keep
/// passing its `(year, month, day)` tuples around as one value.
fn format_iso_date(date: (i32, u32, u32)) -> String {
    super::date_utils::format_iso_date(date.0, date.1, date.2)
}

impl Widget for DateRangePicker {
    fn base(&self) -> &BaseWidget {
        &self.base
    }
    fn set_state_theme_hook(&mut self) {
        crate::style::reapply_active_theme_state(self);
    }

    fn base_mut(&mut self) -> &mut BaseWidget {
        &mut self.base
    }

    fn size_hint(&self) -> crate::core::Size {
        crate::core::Size::new(500, 300)
    }
    impl_draw_bridge!();
    impl_widget_property_hooks!();
    /// Resolves a published event name to the control's own signal.
    ///
    /// # Why this is explicit
    ///
    /// `connect_event` validates a name against the capability table and registers a hub slot; only
    /// `event_signal_dyn` joins that name to the signal the control actually **emits**. Without an
    /// arm a published name is valid and inert, which is the silent failure
    /// `tools/check_event_signal_dyn.sh` exists to make impossible: the arm set is compared against
    /// the capability's published names, so the two cannot drift.
    ///
    /// # Payload mapping
    ///
    /// A hub name carries no value, so a `Signal1<T>` needs a conversion. The one used here is the
    /// same spelling the property side uses, so a designer reads one representation per kind: a
    /// number as `Int`, a float as `Float`, a flag as `Bool`, text as `String`, and a structured
    /// payload as its debug spelling. A `None` maps to `Null`, which is the same value an absent
    /// optional property uses — so "no selection" is distinguishable from "selection 0".
    fn event_signal_dyn(&self, name: &str) -> Option<crate::signal::EventSignalRef> {
        use crate::signal::EventSignalRef;
        match name {
            "range_changed" => {
                Some(EventSignalRef::mapped("range_changed", &self.range_changed, |v| {
                    CapabilityValue::String(format!("{v:?}"))
                }))
            }
            "preset_selected" => {
                Some(EventSignalRef::mapped("preset_selected", &self.preset_selected, |v| {
                    CapabilityValue::UInt(*v as u64)
                }))
            }
            _ => None,
        }
    }
}

/// `DateRangePicker`'s property contract.
///
/// Read/write semantics are carried over unchanged from the centralised
/// `access_read_dialog.in.rs` / `access_write_dialog.in.rs` dispatch, so callers see
/// the same coercions and the same errors as before. Each endpoint reports the
/// widget's real date as an ISO `YYYY-MM-DD` string, or `Null` while that endpoint
/// is unset, rather than the legacy hardcoded blank. A write parses the same format;
/// malformed text is reported as [`CapabilityAccessError::TypeMismatch`] instead of
/// quietly ignoring the write, and `Null` clears the endpoint.
impl WidgetProperties for DateRangePicker {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "start_date" => Ok(match self.start_date() {
                Some(date) => CapabilityValue::String(format_iso_date(date)),
                None => CapabilityValue::Null,
            }),
            "end_date" => Ok(match self.end_date() {
                Some(date) => CapabilityValue::String(format_iso_date(date)),
                None => CapabilityValue::Null,
            }),
            "presets" => Ok(CapabilityValue::Tuple(
                self.presets
                    .iter()
                    .map(|preset| {
                        CapabilityValue::String(format!(
                            "{}|{}|{}",
                            preset.label,
                            preset.start.map(format_iso_date).unwrap_or_default(),
                            preset.end.map(format_iso_date).unwrap_or_default()
                        ))
                    })
                    .collect(),
            )),
            "double_month" => Ok(CapabilityValue::Bool(self.double_month)),
            "min_nights" => Ok(CapabilityValue::UInt(self.min_nights.unwrap_or(0) as u64)),
            "max_nights" => Ok(CapabilityValue::UInt(self.max_nights.unwrap_or(0) as u64)),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "start_date" => {
                self.set_start_date(expect_optional_iso_date(value)?);
                Ok(())
            }
            "end_date" => {
                self.set_end_date(expect_optional_iso_date(value)?);
                Ok(())
            }
            "presets" => {
                let items = expect_string_list(value)?;
                let mut presets = Vec::new();
                for entry in items {
                    let mut parts = entry.splitn(3, '|');
                    let label = parts.next().unwrap_or_default().to_string();
                    let start =
                        parts.next().filter(|text| !text.is_empty()).and_then(parse_iso_date);
                    let end = parts.next().filter(|text| !text.is_empty()).and_then(parse_iso_date);
                    presets.push(DatePreset { label, start, end });
                }
                self.set_presets(presets);
                Ok(())
            }
            "double_month" => {
                let enabled = match value {
                    CapabilityValue::Bool(flag) => flag,
                    _ => return Err(CapabilityAccessError::TypeMismatch),
                };
                self.set_double_month(enabled);
                Ok(())
            }
            "min_nights" => {
                let nights = match value {
                    CapabilityValue::UInt(value) => value,
                    CapabilityValue::Int(value) if value >= 0 => value as u64,
                    _ => return Err(CapabilityAccessError::TypeMismatch),
                };
                self.set_min_nights(u32::try_from(nights).ok());
                Ok(())
            }
            "max_nights" => {
                let nights = match value {
                    CapabilityValue::UInt(value) => value,
                    CapabilityValue::Int(value) if value >= 0 => value as u64,
                    _ => return Err(CapabilityAccessError::TypeMismatch),
                };
                self.set_max_nights(u32::try_from(nights).ok());
                Ok(())
            }
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        property_names_of![
            "start_date",
            "end_date",
            "presets",
            "double_month",
            "min_nights",
            "max_nights",
            BASE_PROPERTY_NAMES
        ]
    }
}

/// Reads an optional ISO date: `Null` clears, a string parses, anything else is a
/// type mismatch.
fn expect_optional_iso_date(
    value: CapabilityValue,
) -> Result<Option<(i32, u32, u32)>, CapabilityAccessError> {
    match value {
        CapabilityValue::Null => Ok(None),
        other => {
            let text = expect_string(other)?;
            parse_iso_date(&text).map(Some).ok_or(CapabilityAccessError::TypeMismatch)
        }
    }
}

impl Draw for DateRangePicker {
    fn draw(&mut self, context: &mut RenderContext) {
        let rect = self.geometry();
        if rect.width == 0 || rect.height == 0 {
            return;
        }

        let is_enabled = self.base.is_enabled();

        // Chrome colours resolve explicit style first, then the theme's resolved
        // style for this control, and only then a literal. The theme step is what
        // makes an appearance switch visible; previously the calendar surface, its
        // text, and the range highlight were all hardcoded, so light and dark
        // rendered identically.
        //
        // `resolved_theme_style` takes and releases the global manager's lock
        // internally, so no guard is held across the draw (the mutex is not
        // re-entrant).
        let style = self.base.style();
        let theme = crate::style::resolved_theme_style_shared("date_range_picker");
        let surface = style
            .background_color
            .or_else(|| theme.as_ref().and_then(|t| t.background_color))
            .unwrap_or(Color::WHITE);
        let text_color = style
            .text_color
            .or_else(|| theme.as_ref().and_then(|t| t.text_color))
            .unwrap_or(Color::BLACK);
        // A disabled calendar is the same surface, faded: derived from the resolved
        // colour rather than a second literal so it still follows the appearance.
        let bg_color =
            if is_enabled { surface.with_alpha(255) } else { surface.blend(&text_color, 0.1) };
        // The selected range is a state, so it reads the theme's selection accent
        // rather than a fixed blue. `primary` is the theme's brand/action colour.
        let accent = theme
            .as_ref()
            .and_then(|_| crate::style::semantic_color(crate::style::SemanticColor::Info))
            .unwrap_or(Color::BLUE);

        context.face_with_gradient(
            rect,
            bg_color,
            self.style().background_gradient.as_ref(),
            self.style().surface.unwrap_or_default(),
            self.style().border_radius.unwrap_or(0),
            Color::BLACK,
        );

        // ── Preset chips ──
        let preset_font = role_font(style, 10.0);
        if !self.presets.is_empty() {
            for index in 0..self.presets.len() {
                let Some(chip) = self.preset_rect(index) else {
                    continue;
                };
                context.fill_rounded_rect(chip, 4, surface.blend(&text_color, 0.08));
                context.draw_text_fitted_cow(
                    chip,
                    self.presets[index].label.as_str(),
                    &preset_font,
                    text_color,
                    HorizontalAlignment::Center,
                );
            }
        }

        // ── Month/Year headers ──
        let header_top = rect.y + self.preset_height() as i32;
        let header_font = role_font(style, 14.0).with_bold(true);
        let panels = self.panel_count();
        let panel_width = rect.width / panels.max(1) as u32;
        for panel in 0..panels {
            let panel_x = rect.x + (panel as u32 * panel_width) as i32;
            let (year, month) = self.panel_month(panel);
            let header_band =
                Rect::new(panel_x + 20, header_top + 6, panel_width.saturating_sub(40), 18);
            let header_text = format!("{} {year}", month_name_for(month));
            context.draw_text_fitted_cow(
                header_band,
                &header_text,
                &header_font,
                text_color,
                HorizontalAlignment::Center,
            );
        }

        // Navigation arrows, each in its own strip at the band's ends so they cannot collide
        // with a long month name. Two icon outlines rather than triangle glyphs: no bundled face
        // covers U+25C0/U+25B6, so those drew as 8x8 bitmap blocks.
        let nav_font = role_font(style, 12.0).with_bold(true);
        let nav_height = context.measure_text("M", &nav_font).height;
        let arrow_side = nav_height.max(1);
        crate::widget::draw_icon_at(
            context,
            Rect::new(rect.x + 6, header_top + 8, arrow_side, arrow_side),
            text_color,
            IconName::ChevronLeft,
        );
        crate::widget::draw_icon_at(
            context,
            Rect::new(rect.x + rect.width as i32 - 20, header_top + 8, arrow_side, arrow_side),
            text_color,
            IconName::ChevronRight,
        );

        // ── Day-of-week headers and grids, one block per month panel ──
        let dow_font = role_font(style, 9.0);
        let day_font = role_font(style, 10.0);
        for panel in 0..panels {
            draw_month_panel(
                self, context, panel, &dow_font, &day_font, surface, text_color, accent, is_enabled,
            );
        }
    }
}

/// Paints one month panel's weekday header and day grid.
#[allow(clippy::too_many_arguments)]
fn draw_month_panel(
    picker: &DateRangePicker,
    context: &mut RenderContext,
    panel: usize,
    dow_font: &crate::core::Font,
    day_font: &crate::core::Font,
    surface: Color,
    text_color: Color,
    accent: Color,
    is_enabled: bool,
) {
    let layout = picker.grid_layout_for(panel);
    let grid_left = layout.grid_left;
    let grid_top = layout.grid_top;
    let cell_size = layout.cell_size;
    let cell_width = layout.cell_width;
    let row_pitch = layout.row_pitch;
    let column_pitch = layout.column_pitch;
    let dow_color = surface.blend(&text_color, 0.55);
    // Both measurements come from the cached table: the header line height and the widths of
    // `"1"..="31"` depend only on the fonts (K-P12), and the panel measured them per frame — 42
    // day numerals plus the header, twice over for a two-month layout.
    let metrics = picker.digit_metrics(dow_font, day_font, context);
    let dow_height = metrics.header_height;
    let margin = TEXT_FIT_MARGIN as i32;
    for (i, day_name) in DAY_NAMES.iter().enumerate() {
        let cell_x = grid_left + (i as u32 * column_pitch) as i32;
        let cell_y = grid_top - DAY_HEADER_HEIGHT as i32;
        context.draw_text_fitted_cow(
            Rect::new(cell_x - margin, cell_y + 4, cell_width + 2 * margin as u32, dow_height),
            day_name,
            dow_font,
            dow_color,
            HorizontalAlignment::Center,
        );
    }

    let mut day_buf = [0u8; 2];
    let focus_date = picker.focus_date;
    for cell_idx in 0..layout.total_cells {
        let Some(date) = picker.date_at_panel_cell(panel, cell_idx) else {
            continue;
        };
        let day = date.2;
        let day_text = if day < 10 {
            day_buf[0] = b'0' + day as u8;
            core::str::from_utf8(&day_buf[..1]).unwrap_or("")
        } else {
            day_buf[0] = b'0' + (day / 10) as u8;
            day_buf[1] = b'0' + (day % 10) as u8;
            core::str::from_utf8(&day_buf[..2]).unwrap_or("")
        };
        let row = cell_idx / 7;
        let col = cell_idx % 7;
        let cell_x = grid_left + (col as u32 * column_pitch) as i32;
        let cell_y = grid_top + (row as u32 * row_pitch) as i32;
        let cell_rect = Rect::new(cell_x, cell_y, cell_width, cell_size);

        let is_start = picker.start_date == Some(date);
        let is_end = picker.end_date == Some(date);
        let is_today = is_start || is_end;
        let in_range = picker.is_in_range(date);
        let is_hover = picker.hover_date == Some(date);
        let is_focused = focus_date == Some(date);
        // A day outside the configured bounds is not selectable, and it says so in the picture
        // (MISC-08): the fill is skipped and the ink is the disabled tone, so a user can see why
        // the click would do nothing.
        let allowed = picker.accepts_date(date);

        if allowed && in_range && is_enabled {
            let range_color =
                if is_start || is_end { accent.with_alpha(200) } else { accent.with_alpha(60) };
            context.fill_rounded_rect(cell_rect, 4, range_color);
        } else if allowed && is_hover && is_enabled {
            context.fill_rounded_rect(cell_rect, 4, surface.blend(&text_color, 0.16));
        } else if allowed && is_today && is_enabled {
            let today_color = if is_start || is_end {
                accent.with_alpha(200)
            } else {
                surface.blend(&text_color, 0.14)
            };
            context.fill_rounded_rect(cell_rect, 4, today_color);
        }

        // The keyboard focus is a ring inside the cell, so it cannot be confused with the range
        // fill it may sit on top of.
        if is_focused && is_enabled {
            context.draw_rect(
                Rect::new(
                    cell_x + 1,
                    cell_y + 1,
                    cell_width.saturating_sub(2),
                    cell_size.saturating_sub(2),
                ),
                accent,
            );
        }

        let day_height = metrics.day_height;
        let day_y = cell_y + (cell_size as i32 - day_height as i32) / 2;
        let day_color = if !is_enabled || !allowed {
            text_color.blend(&surface, 0.55)
        } else if is_start || is_end {
            surface
        } else if in_range {
            accent
        } else {
            text_color
        };
        context.draw_text_fitted_cow(
            Rect::new(
                cell_x - TEXT_FIT_MARGIN as i32,
                day_y,
                cell_width + 2 * TEXT_FIT_MARGIN,
                day_height,
            ),
            day_text,
            day_font,
            day_color,
            HorizontalAlignment::Center,
        );
    }
}

impl EventHandler for DateRangePicker {
    fn handle_event(&mut self, event: &Event) {
        self.base.handle_event(event);
        if !self.base.is_enabled() {
            return;
        }

        match event {
            // Hovering a day cell previews the range the click would complete. Before this
            // arm `hover_date` was only ever `None`, so the draw's highlight branch was
            // unreachable and the control gave no pointer feedback at all.
            Event::MouseMove { pos } => {
                let hovered = self.date_at_position(*pos);
                if self.hover_date != hovered {
                    self.hover_date = hovered;
                    self.base.request_redraw();
                }
                return;
            }
            Event::MouseLeave { .. } => {
                if self.hover_date.is_some() {
                    self.hover_date = None;
                    self.base.request_redraw();
                }
                return;
            }
            Event::KeyPress { key, modifiers } => {
                // Month navigation from the keyboard: PageUp/PageDown step the visible
                // month, and Ctrl+Left/Right do the same for a keyboard user coming from a
                // text-field convention. Neither arm existed, so the grid could only be
                // navigated with the pointer.
                let pressed = crate::shortcut::Key::from_key_code(*key);
                let modifiers = crate::shortcut::Modifiers::from_event_bits(*modifiers);
                match pressed {
                    Some(crate::shortcut::Key::PageUp) => {
                        self.previous_month();
                        return;
                    }
                    Some(crate::shortcut::Key::PageDown) => {
                        self.next_month();
                        return;
                    }
                    Some(crate::shortcut::Key::Left)
                        if modifiers.contains(crate::shortcut::Modifiers::CTRL) =>
                    {
                        self.previous_month();
                        return;
                    }
                    Some(crate::shortcut::Key::Right)
                        if modifiers.contains(crate::shortcut::Modifiers::CTRL) =>
                    {
                        self.next_month();
                        return;
                    }
                    // The grid's own keyboard path (MISC-07): arrows move a focus day (crossing
                    // a month flips the page), Enter applies it through the click's own commit,
                    // and Escape cancels an in-progress range.
                    Some(crate::shortcut::Key::Left) => {
                        self.move_focus(-1, 0);
                        return;
                    }
                    Some(crate::shortcut::Key::Right) => {
                        self.move_focus(1, 0);
                        return;
                    }
                    Some(crate::shortcut::Key::Up) => {
                        self.move_focus(0, -1);
                        return;
                    }
                    Some(crate::shortcut::Key::Down) => {
                        self.move_focus(0, 1);
                        return;
                    }
                    Some(crate::shortcut::Key::Enter) | Some(crate::shortcut::Key::Space) => {
                        if let Some(day) = self.focus_date {
                            self.apply_day(day);
                        }
                        return;
                    }
                    Some(crate::shortcut::Key::Escape) => {
                        // An in-progress selection (start with no end) is what Escape cancels;
                        // a completed range is left alone, because losing a confirmed range to a
                        // stray key is not what "cancel" means to a user.
                        if self.start_date.is_some() && self.end_date.is_none() {
                            self.clear_selection();
                        }
                        return;
                    }
                    _ => {}
                }
            }
            _ => {}
        }

        if let Event::MousePress { pos, button: 1, .. } = event {
            let rect = self.geometry();
            if !rect.contains_point(*pos) {
                return;
            }

            // Preset chips take the press before the calendar behind them.
            for index in 0..self.presets.len() {
                if self.preset_rect(index).is_some_and(|chip| chip.contains_point(*pos)) {
                    self.select_preset(index);
                    return;
                }
            }

            // The header strips move down by the preset row, matching the paint.
            let header_top = rect.y + self.preset_height() as i32;
            let header_rect_left = Rect::new(rect.x + 4, header_top + 4, 20, HEADER_HEIGHT);
            let header_rect_right =
                Rect::new(rect.x + rect.width as i32 - 24, header_top + 4, 20, HEADER_HEIGHT);
            if header_rect_left.contains_point(*pos) {
                self.previous_month();
                return;
            }
            if header_rect_right.contains_point(*pos) {
                self.next_month();
                return;
            }

            let Some((panel, cell_idx)) = self.cell_at_position(*pos) else {
                // Click outside the grid — clear if only start is selected.
                if self.start_date.is_some() && self.end_date.is_none() {
                    self.clear_selection();
                }
                return;
            };
            let Some(clicked_date) = self.date_at_panel_cell(panel, cell_idx) else {
                // A click in the blank tail of the last week belongs to no date; treating it
                // as a selection would invent one.
                return;
            };

            // The click also becomes the keyboard focus, so Enter after a click repeats it and the
            // arrows continue from where the pointer left off.
            self.focus_date = Some(clicked_date);
            self.apply_day(clicked_date);
        }
    }
}

/// A calendar date as `(year, month, day)`, the tuple every date API in this module uses.
pub type Date = (i32, u32, u32);

/// One named shortcut range offered by a [`RangePresetBar`].
///
/// Unlike [`DatePreset`] — which the picker owns and may use as a partial anchor — a range preset
/// is always a complete range, because a bar's whole job is to apply both ends at once.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RangePreset {
    /// The chip's caption, and the key [`RangePresetBar::select`] matches.
    pub label: String,
    /// Inclusive first day.
    pub start: Date,
    /// Inclusive last day.
    pub end: Date,
}

impl RangePreset {
    /// Creates a preset from its caption and endpoints.
    pub fn new(label: impl Into<String>, start: Date, end: Date) -> Self {
        Self { label: label.into(), start, end }
    }

    /// Converts a picker-owned [`DatePreset`] that names both ends.
    ///
    /// Returns `None` for a partial preset: a bar chip cannot stand for "start only", and
    /// inventing an end would make the chip's caption a lie.
    pub fn from_date_preset(preset: &DatePreset) -> Option<Self> {
        Some(Self { label: preset.label.clone(), start: preset.start?, end: preset.end? })
    }

    /// The published property spelling: `label|start|end` with ISO dates.
    pub fn to_token(&self) -> String {
        format!("{}|{}|{}", self.label, format_iso_date(self.start), format_iso_date(self.end))
    }

    /// Parses [`Self::to_token`].
    pub fn from_token(token: &str) -> Option<Self> {
        let mut parts = token.splitn(3, '|');
        let label = parts.next()?.to_string();
        let start = parse_iso_date(parts.next()?)?;
        let end = parse_iso_date(parts.next()?)?;
        Some(Self { label, start, end })
    }
}

/// Whether a preset bar lays its chips out across or down.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum PresetBarOrientation {
    /// Chips share one row; the default.
    #[default]
    Horizontal,
    /// Chips share one column.
    Vertical,
}

impl PresetBarOrientation {
    /// The token spelling published as the `orientation` property.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Horizontal => "horizontal",
            Self::Vertical => "vertical",
        }
    }

    /// Parses a published token spelling.
    pub fn from_token(token: &str) -> Option<Self> {
        match token {
            "horizontal" | "row" => Some(Self::Horizontal),
            "vertical" | "column" => Some(Self::Vertical),
            _ => None,
        }
    }
}

/// Height of one horizontal chip, and of a compact one.
const CHIP_HEIGHT: u32 = 24;
const COMPACT_CHIP_HEIGHT: u32 = 18;

/// A strip of named range shortcuts that reports the range a reader picked.
///
/// The bar is the companion [`DateRangePicker`] lacked rather than a second calendar: it paints
/// the presets, owns which one is active, and emits the range as a `(start, end)` pair. A host
/// feeds that pair to [`DateRangePicker::apply_preset`], which is the same value type, so the two
/// controls cannot disagree about what "last 7 days" means.
pub struct RangePresetBar {
    base: BaseWidget,
    presets: Vec<RangePreset>,
    orientation: PresetBarOrientation,
    compact: bool,
    /// Index of the active chip, once one has been chosen.
    selected: Option<usize>,
    /// Index of the chip the pointer is over, for the hover wash.
    hovered: Option<usize>,
    /// Index keyboard navigation points at, whether or not it is selected yet.
    cursor: usize,
    /// Emitted with the applied range whenever a chip is chosen.
    pub preset_selected: Signal1<(Date, Date)>,
}

impl RangePresetBar {
    /// Creates an empty horizontal bar.
    pub fn new(geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(WidgetKind::RangePresetBar, geometry, "RangePresetBar"),
            presets: Vec::new(),
            orientation: PresetBarOrientation::Horizontal,
            compact: false,
            selected: None,
            hovered: None,
            cursor: 0,
            preset_selected: Signal1::new(),
        }
    }

    /// The configured presets, in display order.
    pub fn presets(&self) -> &[RangePreset] {
        &self.presets
    }

    /// Replaces the presets.
    ///
    /// An active chip is kept when its label still exists, so a refreshed preset list (the
    /// classic "last 7 days" sliding forward overnight) keeps the reader's choice rather than
    /// clearing it.
    pub fn set_presets(&mut self, presets: Vec<RangePreset>) {
        let active_label = self
            .selected
            .and_then(|index| self.presets.get(index))
            .map(|preset| preset.label.clone());
        self.presets = presets;
        self.selected = active_label
            .and_then(|label| self.presets.iter().position(|preset| preset.label == label));
        self.cursor = self.cursor.min(self.presets.len().saturating_sub(1));
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// The layout direction.
    pub fn orientation(&self) -> PresetBarOrientation {
        self.orientation
    }

    /// Sets the layout direction.
    pub fn set_orientation(&mut self, orientation: PresetBarOrientation) {
        if self.orientation == orientation {
            return;
        }
        self.orientation = orientation;
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Whether chips are drawn in their compact size.
    pub fn compact(&self) -> bool {
        self.compact
    }

    /// Enables or disables the compact chip size.
    pub fn set_compact(&mut self, compact: bool) {
        if self.compact == compact {
            return;
        }
        self.compact = compact;
        self.base.request_redraw();
    }

    /// The active preset's label, when one has been chosen.
    pub fn selected_label(&self) -> Option<&str> {
        self.selected.and_then(|index| self.presets.get(index)).map(|preset| preset.label.as_str())
    }

    /// The active preset's range, when one has been chosen.
    pub fn selected_range(&self) -> Option<(Date, Date)> {
        self.selected
            .and_then(|index| self.presets.get(index))
            .map(|preset| (preset.start, preset.end))
    }

    /// Chooses the preset whose label is `label`, emitting its range.
    ///
    /// Returns `false` for an unknown label instead of silently choosing the first chip: a host
    /// that mistyped a caption must find out, not discover later that the picker was showing a
    /// different range from the one it asked for.
    pub fn select(&mut self, label: &str) -> bool {
        let Some(index) = self.presets.iter().position(|preset| preset.label == label) else {
            return false;
        };
        self.select_index(index);
        true
    }

    /// Chooses preset `index`, emitting its range.
    pub fn select_index(&mut self, index: usize) {
        let Some(preset) = self.presets.get(index) else {
            return;
        };
        let range = (preset.start, preset.end);
        let changed = self.selected != Some(index);
        self.selected = Some(index);
        self.cursor = index;
        if changed {
            self.preset_selected.emit(range);
        }
        self.base.request_redraw();
    }

    /// The painted box of chip `index`.
    ///
    /// Draw and hit-testing both call this, so a click can never choose a chip the reader is not
    /// pointing at.
    pub fn chip_rect(&self, index: usize) -> Option<Rect> {
        if index >= self.presets.len() {
            return None;
        }
        let rect = self.geometry();
        let count = self.presets.len() as u32;
        match self.orientation {
            PresetBarOrientation::Horizontal => {
                let width = rect.width / count.max(1);
                let height = if self.compact {
                    COMPACT_CHIP_HEIGHT.min(rect.height)
                } else {
                    CHIP_HEIGHT.min(rect.height)
                };
                Some(Rect::new(
                    rect.x + (index as u32 * width) as i32,
                    rect.y,
                    width.saturating_sub(2),
                    height,
                ))
            }
            PresetBarOrientation::Vertical => {
                let height = rect.height / count.max(1);
                Some(Rect::new(
                    rect.x,
                    rect.y + (index as u32 * height) as i32,
                    rect.width,
                    height.saturating_sub(2),
                ))
            }
        }
    }

    /// The chip under `pos`.
    fn chip_at(&self, pos: Point) -> Option<usize> {
        (0..self.presets.len())
            .find(|index| self.chip_rect(*index).is_some_and(|chip| chip.contains_point(pos)))
    }

    /// Moves keyboard navigation by `delta` and selects the reached chip.
    fn move_cursor(&mut self, delta: isize) {
        if self.presets.is_empty() {
            return;
        }
        let last = self.presets.len() - 1;
        let next = (self.cursor as isize + delta).clamp(0, last as isize) as usize;
        self.select_index(next);
    }
}

impl Widget for RangePresetBar {
    fn base(&self) -> &BaseWidget {
        &self.base
    }
    fn set_state_theme_hook(&mut self) {
        crate::style::reapply_active_theme_state(self);
    }
    fn base_mut(&mut self) -> &mut BaseWidget {
        &mut self.base
    }
    fn size_hint(&self) -> crate::core::Size {
        match self.orientation {
            PresetBarOrientation::Horizontal => crate::core::Size::new(320, CHIP_HEIGHT),
            PresetBarOrientation::Vertical => crate::core::Size::new(160, 160),
        }
    }
    impl_draw_bridge!();
    impl_widget_property_hooks!();
    /// Resolves `preset_selected`; see `DiffViewer`'s note for why the arms are explicit.
    fn event_signal_dyn(&self, name: &str) -> Option<crate::signal::EventSignalRef> {
        use crate::signal::EventSignalRef;
        match name {
            "preset_selected" => {
                Some(EventSignalRef::mapped("preset_selected", &self.preset_selected, |range| {
                    CapabilityValue::Tuple(crate::compat::Vec::from([
                        CapabilityValue::String(format_iso_date(range.0)),
                        CapabilityValue::String(format_iso_date(range.1)),
                    ]))
                }))
            }
            _ => None,
        }
    }
}

impl WidgetProperties for RangePresetBar {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "presets" => Ok(CapabilityValue::Tuple(
                self.presets
                    .iter()
                    .map(|preset| CapabilityValue::String(preset.to_token()))
                    .collect(),
            )),
            "orientation" => Ok(CapabilityValue::String(self.orientation.as_str().to_string())),
            "compact" => Ok(CapabilityValue::Bool(self.compact)),
            "selected_label" => Ok(match self.selected_label() {
                Some(label) => CapabilityValue::String(label.to_string()),
                None => CapabilityValue::Null,
            }),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "presets" => {
                let items = expect_string_list(value)?;
                let mut presets = Vec::with_capacity(items.len());
                for item in items {
                    let preset = RangePreset::from_token(&item)
                        .ok_or(CapabilityAccessError::TypeMismatch)?;
                    presets.push(preset);
                }
                self.set_presets(presets);
                Ok(())
            }
            "orientation" => {
                let token = expect_string(value)?;
                let orientation = PresetBarOrientation::from_token(&token)
                    .ok_or(CapabilityAccessError::TypeMismatch)?;
                self.set_orientation(orientation);
                Ok(())
            }
            "compact" => {
                let compact = match value {
                    CapabilityValue::Bool(flag) => flag,
                    _ => return Err(CapabilityAccessError::TypeMismatch),
                };
                self.set_compact(compact);
                Ok(())
            }
            "selected_label" => Err(CapabilityAccessError::ReadOnlyProperty),
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        property_names_of![
            "presets",
            "orientation",
            "compact",
            "selected_label",
            BASE_PROPERTY_NAMES
        ]
    }
}

impl EventHandler for RangePresetBar {
    fn handle_event(&mut self, event: &Event) {
        self.base.handle_event(event);
        if !self.base.is_enabled() {
            return;
        }
        match event {
            Event::MousePress { pos, button: 1, .. } => {
                if let Some(index) = self.chip_at(*pos) {
                    self.select_index(index);
                }
            }
            Event::MouseMove { pos } => {
                let hovered = self.chip_at(*pos);
                if hovered != self.hovered {
                    self.hovered = hovered;
                    self.base.request_redraw();
                }
            }
            Event::KeyPress { key, .. } => match *key {
                37 | 38 => self.move_cursor(-1),
                39 | 40 => self.move_cursor(1),
                13 if !self.presets.is_empty() => {
                    self.select_index(self.cursor.min(self.presets.len() - 1));
                }
                _ => { /* Other keys are not relevant */ }
            },
            _ => {}
        }
    }
}

impl Draw for RangePresetBar {
    fn draw(&mut self, context: &mut RenderContext) {
        let rect = self.geometry();
        if rect.width == 0 || rect.height == 0 {
            return;
        }
        let style = self.base.style();
        let theme = crate::style::resolved_theme_style_shared("date_range_picker");
        let surface = style
            .background_color
            .or_else(|| theme.as_ref().and_then(|t| t.background_color))
            .unwrap_or(Color::WHITE);
        let text_color = style
            .text_color
            .or_else(|| theme.as_ref().and_then(|t| t.text_color))
            .unwrap_or(Color::BLACK);
        let accent =
            crate::style::semantic_color(crate::style::SemanticColor::Info).unwrap_or(Color::BLUE);
        let font = role_font(style, 10.0);

        context.face_with_gradient(
            rect,
            surface,
            self.style().background_gradient.as_ref(),
            self.style().surface.unwrap_or_default(),
            self.style().border_radius.unwrap_or(0),
            Color::BLACK,
        );

        for index in 0..self.presets.len() {
            let Some(chip) = self.chip_rect(index) else {
                continue;
            };
            let active = self.selected == Some(index);
            let fill = if active {
                accent.blend(&surface, 0.25)
            } else if self.hovered == Some(index) {
                surface.blend(&text_color, 0.08)
            } else {
                surface.blend(&text_color, 0.03)
            };
            context.fill_rounded_rect(chip, 4, fill);
            if active {
                crate::widget::draw::stroke_surface_border(context, chip, 4, accent, 1);
            }
            let label = crate::widget::text_utils::elide_end(
                &self.presets[index].label,
                (chip.width as usize / 7).max(1),
            );
            context.draw_text(
                Point::new(chip.x + 6, chip.y + 4),
                &label,
                &font,
                if active { text_color } else { text_color.blend(&surface, 0.25) },
                HorizontalAlignment::Left,
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::widget::svg::render_to_svg;

    fn make_picker() -> DateRangePicker {
        let mut picker = DateRangePicker::new(Rect::new(0, 0, 240, 260));
        // The default now follows the wall clock (MISC-02); the geometry tests below pin the
        // month so their cell arithmetic stays deterministic.
        picker.display_year = 2025;
        picker.display_month = 1;
        picker
    }

    #[test]
    fn date_range_picker_default_creation() {
        let picker = DateRangePicker::new(Rect::new(0, 0, 240, 260));
        let today = chrono::Local::now().date_naive();
        assert_eq!(picker.kind(), WidgetKind::DateRangePicker);
        assert!(picker.start_date().is_none());
        assert!(picker.end_date().is_none());
        assert_eq!(picker.display_year(), today.year());
        assert_eq!(picker.display_month(), today.month());
    }

    #[test]
    fn date_range_picker_set_start_end() {
        let mut picker = make_picker();
        picker.set_start_date(Some((2025, 1, 10)));
        assert_eq!(picker.start_date(), Some((2025, 1, 10)));

        picker.set_end_date(Some((2025, 1, 20)));
        assert_eq!(picker.end_date(), Some((2025, 1, 20)));
    }

    #[test]
    fn date_range_picker_clear_selection() {
        let mut picker = make_picker();
        picker.set_start_date(Some((2025, 1, 10)));
        picker.set_end_date(Some((2025, 1, 20)));
        assert!(picker.start_date().is_some());
        assert!(picker.end_date().is_some());

        picker.clear_selection();
        assert!(picker.start_date().is_none());
        assert!(picker.end_date().is_none());
    }

    #[test]
    fn date_range_picker_navigation() {
        let mut picker = make_picker();
        picker.set_current_month();
        let today = chrono::Local::now().date_naive();
        assert_eq!(picker.display_month(), today.month());

        picker.previous_month();
        let (year, month) = if today.month() == 1 {
            (today.year() - 1, 12)
        } else {
            (today.year(), today.month() - 1)
        };
        assert_eq!(picker.display_month(), month);
        assert_eq!(picker.display_year(), year);

        picker.next_month();
        assert_eq!(picker.display_month(), today.month());
        assert_eq!(picker.display_year(), today.year());
    }

    #[test]
    fn date_range_picker_svg_output() {
        let mut picker = make_picker();
        picker.set_start_date(Some((2025, 1, 5)));
        picker.set_end_date(Some((2025, 1, 15)));
        let svg = render_to_svg(&mut picker);
        assert!(svg.starts_with("<svg"));
        assert!(svg.ends_with("</svg>"));
    }

    /// Centre of the cell at `col`/`row` in the picker's own grid geometry.
    fn cell_centre(picker: &DateRangePicker, col: u32, row: u32) -> Point {
        let layout = picker.grid_layout();
        Point::new(
            layout.grid_left + (col * layout.column_pitch) as i32 + layout.cell_width as i32 / 2,
            layout.grid_top + (row * layout.row_pitch) as i32 + layout.cell_size as i32 / 2,
        )
    }

    #[test]
    fn january_2025_starts_in_the_wednesday_column() {
        // The 1st of January 2025 is a Wednesday. Sunday-first numbering therefore puts it in
        // column 3, with three leading blanks in the first week. The grid used to start it in
        // column 4 (Zeller's Saturday-based value used verbatim), shifting every day one cell
        // to the right of the weekday printed above it.
        assert_eq!(day_of_week(2025, 1, 1), 3);
        let picker = make_picker();
        assert_eq!(picker.first_day_of_week(), 3);
        assert_eq!(picker.date_at_cell(2), None);
        assert_eq!(picker.date_at_cell(3), Some((2025, 1, 1)));
        assert_eq!(picker.date_at_cell(9), Some((2025, 1, 7)));
    }

    #[test]
    fn a_click_selects_the_day_painted_in_that_cell() {
        // The draw places day `d` at `col = (offset + d - 1) % 7`; the hit test must resolve a
        // press at that cell's centre back to the same `d`. Before the geometry fix the two
        // agreed with each other but disagreed with the painted weekday, because the offset
        // itself was a day off.
        let picker = make_picker();
        let offset = picker.first_day_of_week();
        for day in [1u32, 7, 15, 22, 31] {
            let index = offset + day - 1;
            let (col, row) = (index % 7, index / 7);

            let mut clicked = make_picker();
            clicked.handle_event(&Event::MousePress {
                pos: cell_centre(&picker, col, row),
                button: 1,
                modifiers: 0,
            });
            assert_eq!(
                clicked.start_date(),
                Some((2025, 1, day)),
                "day {day} drawn at column {col}, row {row} was hit-tested as {:?}",
                clicked.start_date()
            );
            assert!(clicked.end_date().is_none());
        }
    }

    #[test]
    fn hover_tracks_the_day_under_the_pointer_and_clears_on_leave() {
        let mut picker = make_picker();
        let offset = picker.first_day_of_week();
        let index = offset + 14; // day 15
        let (col, row) = (index % 7, index / 7);

        picker.handle_event(&Event::MouseMove { pos: cell_centre(&picker, col, row) });
        assert_eq!(
            picker.hover_date,
            Some((2025, 1, 15)),
            "the cell under the pointer must be the highlighted date"
        );

        picker.handle_event(&Event::MouseLeave { pos: Point::new(0, 0) });
        assert_eq!(picker.hover_date, None, "leaving the control clears the preview");
    }

    #[test]
    fn date_range_picker_mouse_press_sets_start() {
        let mut picker = make_picker();
        // Jan 1, 2025 is Wednesday -> column 3 (Sunday-based). Day 15 is therefore cell
        // index 3 + 14 = 17 => row 2, column 3; the click below is that cell's centre.
        picker.handle_event(&Event::MousePress {
            pos: cell_centre(&picker, 3, 2),
            button: 1,
            modifiers: 0,
        });
        assert_eq!(picker.start_date(), Some((2025, 1, 15)));
        assert!(picker.end_date().is_none());
    }

    #[test]
    fn a_click_in_a_leading_blank_does_not_invent_a_date() {
        let mut picker = make_picker();
        // Column 0..2 of the first week precede January 1st and own no date.
        picker.handle_event(&Event::MousePress {
            pos: cell_centre(&picker, 0, 0),
            button: 1,
            modifiers: 0,
        });
        assert_eq!(picker.start_date(), None);
    }

    #[test]
    fn date_range_picker_mouse_press_clears_existing() {
        let mut picker = make_picker();
        picker.set_start_date(Some((2025, 1, 10)));

        // Click outside the grid area (row >= 6)
        picker.handle_event(&Event::MousePress {
            pos: Point::new(4, 250), // bottom of widget
            button: 1,
            modifiers: 0,
        });
        assert!(picker.start_date().is_none());
    }

    #[test]
    fn presets_double_month_and_night_bounds_are_live() {
        use std::sync::atomic::{AtomicUsize, Ordering};
        use std::sync::Arc;

        let mut picker = make_picker();
        picker.set_presets(vec![DatePreset::new("Week", (2025, 1, 1), (2025, 1, 7))]);
        let hits = Arc::new(AtomicUsize::new(0));
        let sink = Arc::clone(&hits);
        picker.preset_selected.connect(move |_| {
            sink.fetch_add(1, Ordering::SeqCst);
        });
        let chip = picker.preset_rect(0).expect("the preset chip exists");
        picker.handle_event(&Event::MousePress {
            pos: Point::new(chip.x + 2, chip.y + 2),
            button: 1,
            modifiers: 0,
        });
        assert_eq!(picker.start_date(), Some((2025, 1, 1)));
        assert_eq!(picker.end_date(), Some((2025, 1, 7)));
        assert_eq!(hits.load(Ordering::SeqCst), 1);

        picker.set_double_month(true);
        assert!(picker.double_month());
        let svg = render_to_svg(&mut picker);
        assert!(svg.starts_with("<svg"));

        assert!(picker.nights_allowed((2025, 1, 1), (2025, 1, 2)));
        picker.set_min_nights(Some(2));
        assert!(!picker.nights_allowed((2025, 1, 1), (2025, 1, 2)));
        assert!(picker.nights_allowed((2025, 1, 1), (2025, 1, 3)));
        picker.set_max_nights(Some(3));
        assert!(!picker.nights_allowed((2025, 1, 1), (2025, 1, 5)));
    }

    /// A preset bar's range applies to the picker, and the picker reports the matching index.
    #[test]
    fn a_range_preset_applies_to_the_picker() {
        let mut picker = make_picker();
        picker.set_presets(vec![DatePreset::new("Week", (2025, 1, 1), (2025, 1, 7))]);
        let reported = std::sync::Arc::new(std::sync::Mutex::new(Vec::<usize>::new()));
        let sink = reported.clone();
        picker.preset_selected.connect(move |index| {
            if let Ok(mut guard) = sink.lock() {
                guard.push(*index);
            }
        });

        let known = RangePreset::new("Week", (2025, 1, 1), (2025, 1, 7));
        assert_eq!(picker.apply_preset(&known), Some(0));
        assert_eq!(picker.start_date(), Some((2025, 1, 1)));
        assert_eq!(picker.end_date(), Some((2025, 1, 7)));
        assert_eq!(*reported.lock().expect("reported lock"), vec![0]);

        let unknown = RangePreset::new("Quarter", (2025, 4, 1), (2025, 6, 30));
        assert_eq!(picker.apply_preset(&unknown), None);
        assert_eq!(picker.start_date(), Some((2025, 4, 1)));
        assert_eq!(picker.end_date(), Some((2025, 6, 30)));
    }

    #[test]
    fn preset_bar_emits_the_range_it_applies() {
        let mut bar = RangePresetBar::new(Rect::new(0, 0, 300, 24));
        bar.set_presets(vec![
            RangePreset::new("Today", (2025, 1, 1), (2025, 1, 1)),
            RangePreset::new("Week", (2025, 1, 1), (2025, 1, 7)),
        ]);
        let ranges = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let sink = ranges.clone();
        bar.preset_selected.connect(move |range| {
            if let Ok(mut guard) = sink.lock() {
                guard.push(*range);
            }
        });

        assert!(bar.select("Week"));
        assert!(!bar.select("Missing"));
        assert_eq!(bar.selected_label(), Some("Week"));
        assert_eq!(bar.selected_range(), Some(((2025, 1, 1), (2025, 1, 7))));
        assert_eq!(*ranges.lock().expect("ranges lock"), vec![((2025, 1, 1), (2025, 1, 7))]);

        // A press on the first chip chooses it, and the chip boxes are the hit targets.
        let chip = bar.chip_rect(0).expect("chip 0");
        bar.handle_event(&Event::mouse_press(chip.x + 2, chip.y + 2, 1));
        assert_eq!(bar.selected_label(), Some("Today"));
    }

    #[test]
    fn preset_bar_properties_drive_layout_and_round_trip() {
        let mut bar = RangePresetBar::new(Rect::new(0, 0, 300, 100));
        let presets = CapabilityValue::Tuple(vec![
            CapabilityValue::String("Today|2025-01-01|2025-01-01".to_string()),
            CapabilityValue::String("Week|2025-01-01|2025-01-07".to_string()),
        ]);
        bar.set("presets", presets.clone()).expect("presets writable");
        assert_eq!(bar.get("presets"), Ok(presets));

        bar.set("orientation", CapabilityValue::String("vertical".to_string()))
            .expect("orientation writable");
        assert_eq!(bar.orientation(), PresetBarOrientation::Vertical);
        assert_eq!(bar.chip_rect(1).expect("chip 1").y, 50);

        bar.set("compact", CapabilityValue::Bool(true)).expect("compact writable");
        assert!(bar.compact());
        assert_eq!(bar.get("selected_label"), Ok(CapabilityValue::Null));
        assert_eq!(
            bar.set("selected_label", CapabilityValue::String("Week".to_string())),
            Err(CapabilityAccessError::ReadOnlyProperty)
        );
    }

    #[test]
    fn go_to_today_selects_the_wall_clock_day() {
        let mut picker = make_picker();
        picker.go_to_today();
        let today = chrono::Local::now().date_naive();
        assert_eq!(picker.display_year(), today.year());
        assert_eq!(picker.display_month(), today.month());
        assert_eq!(picker.start_date(), Some((today.year(), today.month(), today.day())));
    }

    /// A bounded picker refuses a day outside its range on every path (MISC-08).
    #[test]
    fn the_date_bounds_block_a_selection() {
        let mut picker = DateRangePicker::new(Rect::new(0, 0, 240, 260));
        picker.set_minimum_date(Some((2025, 6, 10)));
        picker.set_maximum_date(Some((2025, 6, 20)));
        assert!(!picker.accepts_date((2025, 6, 9)));
        assert!(picker.accepts_date((2025, 6, 10)));
        assert!(picker.accepts_date((2025, 6, 20)));
        assert!(!picker.accepts_date((2025, 6, 21)));

        // The shared commit refuses the out-of-range day without touching the selection.
        picker.apply_day((2025, 6, 5));
        assert_eq!(picker.start_date(), None);
        picker.apply_day((2025, 6, 12));
        assert_eq!(picker.start_date(), Some((2025, 6, 12)));
    }

    /// Arrows move a focus day across months; Enter applies it; Escape cancels a partial range
    /// (MISC-07).
    #[test]
    fn the_keyboard_walks_the_grid_and_commits() {
        let mut picker = DateRangePicker::new(Rect::new(0, 0, 240, 260));
        picker.set_display_page(2025, 6);
        picker.set_start_date(Some((2025, 6, 10)));

        picker.handle_event(&Event::key_press(crate::event::key_codes::RIGHT, 0));
        assert_eq!(picker.focus_date(), Some((2025, 6, 11)));
        picker.handle_event(&Event::key_press(crate::event::key_codes::DOWN, 0));
        assert_eq!(picker.focus_date(), Some((2025, 6, 18)));

        // Enter completes the range through the click path.
        picker.handle_event(&Event::key_press(crate::event::key_codes::ENTER, 0));
        assert_eq!(picker.end_date(), Some((2025, 6, 18)));

        // Escape clears an in-progress (start-only) selection.
        picker.set_end_date(None);
        picker.handle_event(&Event::key_press(crate::event::key_codes::ESCAPE, 0));
        assert_eq!(picker.start_date(), None);
    }

    /// Walking past the end of a month flips the page the grid shows.
    #[test]
    fn the_keyboard_flips_the_page_across_a_month_boundary() {
        let mut picker = DateRangePicker::new(Rect::new(0, 0, 240, 260));
        picker.set_display_page(2025, 6);
        picker.set_start_date(Some((2025, 6, 30)));
        picker.handle_event(&Event::key_press(crate::event::key_codes::RIGHT, 0));
        assert_eq!(picker.focus_date(), Some((2025, 7, 1)));
        assert_eq!(picker.display_month(), 7);
    }
}