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
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

//! Date-time editor widget.
//!
//! [`DateTimeEdit`] combines the calendar date of
//! [`Date`] with the wall-clock time of [`Time`].
//!
//! # Conventions
//!
//! * The calendar and clock conventions are exactly those of the two component
//!   types: the month and day are **1-based**, the hour is **24-hour**
//!   (`0` = midnight), and a whole second carries into the next minute, hour, or
//!   day.
//! * The widget's range is **inclusive at both ends**, and is compared as a
//!   single [`DateTime`], so `minimum` and `maximum` each constrain the date and
//!   the time together.
//! * [`DateTimeEdit::set_datetime`] rejects an invalid or out-of-range value
//!   silently rather than clamping it.
//! * Ordering is by date first, then by time.
use core::fmt::Write as _;

use crate::core::{Color, HorizontalAlignment, Point, Rect, Size};
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::shortcut::Key;
use crate::signal::{GenericSignal, Signal1};
use crate::undo::{UndoStack, ValueCommand};
use crate::widget::advanced_widgets::calendar::Calendar;
use crate::widget::advanced_widgets::{date_edit::Date, time_edit::Time};
use crate::widget::capability::access::weekday_to_str;
use crate::widget::capability::coercion::{
    expect_bool, expect_datetime, expect_horizontal_alignment, expect_string, expect_weekday,
    horizontal_alignment_to_str,
};
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::input_widgets::shortcut_editor::primary_shortcut_pressed;
use crate::widget::metrics::effective_font;
use crate::widget::{BaseWidget, Draw, Widget, WidgetKind};
use crate::{impl_widget_property_hooks, property_names_of};
use chrono::Weekday;
use std::cell::RefCell;
use std::rc::Rc;

/// Combined date-time value.
///
/// This is a plain pair: the fields are public, so they can be replaced
/// directly and no validation happens on assignment. Call [`DateTime::is_valid`]
/// to check the combination, or use [`DateTimeEdit::set_datetime`], which does
/// that check for you.
///
/// `Ord` compares the date first and the time only when the dates are equal, so
/// the ordering is chronological.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct DateTime {
    /// Calendar part; see [`Date`] for the 1-based month/day convention.
    pub date: Date,
    /// Time-of-day part; see [`Time`] for the 24-hour convention.
    pub time: Time,
}
impl DateTime {
    /// Combines `date` and `time` without validating either.
    pub fn new(date: Date, time: Time) -> Self {
        Self { date, time }
    }
    /// Returns `true` when both the date and the time are individually valid.
    ///
    /// Because [`Time`] clamps on construction, in practice this reduces to
    /// [`Date::is_valid`].
    pub fn is_valid(&self) -> bool {
        self.date.is_valid() && self.time.is_valid()
    }
}
/// Formats the value as `"<date> <time>"`, i.e. `YYYY-MM-DD HH:MM:SS`.
///
/// The two components use their own `Display` implementations, so the fields
/// are zero-padded and the millisecond component is omitted. This is the
/// spelling [`crate::widget::capability::coercion::expect_datetime`] parses
/// back, split on the first space; the round-trip loses any non-zero
/// milliseconds, exactly as the components' own round-trips do.
impl std::fmt::Display for DateTime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} {}", self.date, self.time)
    }
}
/// One editable component of a date-time field.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DateTimeSection {
    /// The year component.
    Year,
    /// The month component.
    Month,
    /// The day component.
    Day,
    /// The hour component.
    Hour,
    /// The minute component.
    Minute,
    /// The second component.
    Second,
}

impl DateTimeSection {
    /// The sections in reading order.
    const ORDER: [Self; 6] =
        [Self::Year, Self::Month, Self::Day, Self::Hour, Self::Minute, Self::Second];

    /// The next section, wrapping.
    pub const fn next(self) -> Self {
        match self {
            Self::Year => Self::Month,
            Self::Month => Self::Day,
            Self::Day => Self::Hour,
            Self::Hour => Self::Minute,
            Self::Minute => Self::Second,
            Self::Second => Self::Year,
        }
    }

    /// The previous section, wrapping.
    pub const fn previous(self) -> Self {
        match self {
            Self::Year => Self::Second,
            Self::Month => Self::Year,
            Self::Day => Self::Month,
            Self::Hour => Self::Day,
            Self::Minute => Self::Hour,
            Self::Second => Self::Minute,
        }
    }

    /// The index in [`Self::ORDER`].
    const fn index(self) -> usize {
        match self {
            Self::Year => 0,
            Self::Month => 1,
            Self::Day => 2,
            Self::Hour => 3,
            Self::Minute => 4,
            Self::Second => 5,
        }
    }

    /// Digits the section collects before the buffer is trimmed.
    const fn digit_capacity(self) -> usize {
        match self {
            Self::Year => 4,
            _ => 2,
        }
    }
}

/// Date-time editor widget.
///
/// Shows a combined date and time and allows them to be changed one second at a
/// time (see [`DateTimeEdit::step_up`] / [`DateTimeEdit::step_down`]), one
/// component at a time via [`DateTimeEdit::set_date`] / [`DateTimeEdit::set_time`],
/// or through the keyboard (Up/Down step, Ctrl+Z/Ctrl+Y undo and redo).
///
/// The accepted range defaults to `1752-09-14 00:00:00.000 ..=
/// 9999-12-31 23:59:59.999` and is inclusive at both ends.
///
/// # Display format
///
/// [`DateTimeEdit::display_format`] holds a format pattern string that is
/// exposed as a property and is interpreted when the widget paints, through the
/// shared `super::date_edit::format_with_pattern_into` substituter. A pattern
/// with no recognised token falls back to `YYYY-MM-DD HH:MM:SS`.
/// `YYYY-MM-DD HH:MM:SS` spelling. The pattern is stored and round-tripped, not
/// yet applied when painting.
pub struct DateTimeEdit {
    base: BaseWidget,
    datetime: DateTime,
    minimum: DateTime,
    maximum: DateTime,
    display_format: String,
    /// Whether the field shows the calendar affordance (a 16 px arrow zone).
    calendar_popup: bool,
    /// Whether the month grid is currently showing.
    calendar_open: bool,
    /// Emitted with the new open state when the calendar grid is shown or hidden.
    pub popup_visibility_changed: Signal1<bool>,
    /// How the field's value is aligned within its own box.
    ///
    /// Horizontal only: the value is centred vertically in the field, so a `top`/`bottom` value
    /// would be one this control could never honour —
    /// [`crate::widget::capability::coercion::expect_horizontal_alignment`] refuses those rather
    /// than accepting a write that does nothing. Defaults to left.
    alignment: crate::core::Alignment,
    /// Emitted with the new value after every accepted change, including changes
    /// produced by [`DateTimeEdit::undo`] and [`DateTimeEdit::redo`]. Not emitted
    /// when a change is rejected or when the value is already the requested one.
    pub datetime_changed: Signal1<DateTime>,
    /// Emitted with the new date part when it changes, so a subscriber interested only in
    /// the date does not have to filter `datetime_changed` payloads.
    pub date_changed: Signal1<Date>,
    /// Emitted with the new time part when it changes, the time counterpart of
    /// [`Self::date_changed`].
    pub time_changed: Signal1<Time>,
    /// Emitted when editing is committed: Enter, Escape, or focus leaving the field.
    pub editing_finished: GenericSignal,
    undo_stack: UndoStack,
    history_target: Rc<RefCell<DateTime>>,
    restoring_history: bool,
    /// The section the user is editing, or `None` for whole-second stepping.
    current_section: Option<DateTimeSection>,
    /// Digits typed into the active section.
    digit_buffer: String,
    /// Whether the value paints a spin-button pair.
    spin_buttons: bool,
    /// The calendar grid's reveal.
    calendar_reveal: crate::widget::menu_toolbar::popup_reveal::PopupReveal,
    /// Text painted instead of the value at the minimum.
    special_value_text: Option<String>,
    /// The embedded [`Calendar`] that owns the popup's month grid.
    calendar: Calendar,
    /// The first column of the popup's month grid.
    calendar_first_day: Weekday,
    /// Emitted after the embedded calendar becomes visible.
    pub calendar_opened: GenericSignal,
    /// Emitted after the embedded calendar is dismissed.
    pub calendar_closed: GenericSignal,
    /// Whether the embedded calendar's geometry and accepted range must be pushed again (K-P02).
    ///
    /// The open popup's event paths used to call `sync_calendar_page` on every pointer move and
    /// key press, re-setting geometry and bounds (each a repaint request) for facts that had not
    /// changed. Only opening the popup, a range change, a first-day change or a field move can
    /// change them, so those mark this flag and the event paths just read it.
    calendar_sync_dirty: bool,
    /// The popup rectangle last pushed into the embedded calendar.
    calendar_sync_rect: Rect,
}
impl DateTimeEdit {
    /// Creates a date-time editor occupying `geometry`.
    ///
    /// The initial value is midnight on [`Date::today`], the accepted range is
    /// `1752-09-14 00:00:00.000 ..= 9999-12-31 23:59:59.999` (inclusive), the
    /// display format is `"yyyy-MM-dd HH:mm:ss"`, and the calendar popup is
    /// disabled.
    pub fn new(geometry: Rect) -> Self {
        let min_dt = DateTime::new(Date::new(1752, 9, 14), Time::new(0, 0, 0, 0));
        let max_dt = DateTime::new(Date::new(9999, 12, 31), Time::new(23, 59, 59, 999));
        let now = DateTime::new(Date::today(), Time::new(0, 0, 0, 0));
        Self {
            base: BaseWidget::new(WidgetKind::DateTimePicker, geometry, "DateTimeEdit"),
            datetime: now,
            minimum: min_dt,
            maximum: max_dt,
            display_format: "yyyy-MM-dd HH:mm:ss".to_string(),
            calendar_popup: false,
            calendar_open: false,
            popup_visibility_changed: Signal1::new(),
            alignment: crate::core::Alignment::Left,
            datetime_changed: Signal1::new(),
            date_changed: Signal1::new(),
            time_changed: Signal1::new(),
            editing_finished: GenericSignal::new(),
            undo_stack: UndoStack::new(),
            history_target: Rc::new(RefCell::new(now)),
            restoring_history: false,
            current_section: None,
            digit_buffer: String::new(),
            spin_buttons: false,
            calendar_reveal: crate::widget::menu_toolbar::popup_reveal::PopupReveal::new(false),
            special_value_text: None,
            calendar: {
                let mut calendar = Calendar::new(super::date_edit::calendar_popup_rect(geometry));
                calendar.set_first_day_of_week(Weekday::Sun);
                calendar
            },
            calendar_first_day: Weekday::Sun,
            calendar_opened: GenericSignal::new(),
            calendar_closed: GenericSignal::new(),
            calendar_sync_dirty: true,
            calendar_sync_rect: Rect::new(0, 0, 0, 0),
        }
    }
    /// Returns the current combined value.
    pub fn datetime(&self) -> DateTime {
        self.datetime
    }
    /// How the field's value is aligned within its own box.
    pub fn alignment(&self) -> crate::core::Alignment {
        self.alignment
    }

    /// Sets how the field's value is aligned within its own box.
    ///
    /// Horizontal only. A `top`/`bottom` alignment is **ignored**, because the value is centred
    /// vertically in the field by the field's own layout — the property route refuses it through
    /// [`crate::widget::capability::coercion::expect_horizontal_alignment`], and this setter
    /// matching that keeps the two entry points from disagreeing.
    pub fn set_alignment(&mut self, alignment: crate::core::Alignment) {
        if alignment.to_horizontal().is_none() || self.alignment == alignment {
            return;
        }
        self.alignment = alignment;
        self.base.request_redraw();
    }
    /// Returns the date part of the current value.
    pub fn date(&self) -> Date {
        self.datetime.date
    }
    /// Returns the time part of the current value.
    pub fn time(&self) -> Time {
        self.datetime.time
    }
    /// Returns the inclusive lower bound accepted by
    /// [`DateTimeEdit::set_datetime`].
    ///
    /// Defaults to `1752-09-14 00:00:00.000`.
    pub fn minimum_datetime(&self) -> DateTime {
        self.minimum
    }
    /// Returns the inclusive upper bound accepted by
    /// [`DateTimeEdit::set_datetime`].
    ///
    /// Defaults to `9999-12-31 23:59:59.999`.
    pub fn maximum_datetime(&self) -> DateTime {
        self.maximum
    }
    /// Returns the stored display-format pattern.
    ///
    /// The pattern is applied by the next paint; see
    /// `super::date_edit::format_with_pattern_into` for the token table.
    pub fn display_format(&self) -> &str {
        &self.display_format
    }
    /// Returns whether the calendar affordance is enabled.
    ///
    /// Defaults to `false`. This is the affordance flag, not the open state; see
    /// [`DateTimeEdit::calendar_open`].
    pub fn calendar_popup(&self) -> bool {
        self.calendar_popup
    }
    /// Returns whether the month grid is currently showing.
    pub fn calendar_open(&self) -> bool {
        self.calendar_open
    }
    /// Sets the combined date-time, subject to validity and the accepted range.
    ///
    /// The assignment happens only when `dt` satisfies [`DateTime::is_valid`],
    /// falls within `minimum ..= maximum` (both inclusive), and differs from the
    /// current value; otherwise the call is a no-op and the previous value is
    /// kept. Out-of-range input is therefore **rejected, not clamped**.
    ///
    /// # Side effects
    ///
    /// On a successful change, an undo entry is pushed (unless this call comes
    /// from [`DateTimeEdit::undo`] / [`DateTimeEdit::redo`]), `datetime_changed`
    /// is emitted with the new value, and a redraw is requested.
    pub fn set_datetime(&mut self, dt: DateTime) {
        if dt.is_valid() && dt >= self.minimum && dt <= self.maximum && self.datetime != dt {
            let before = self.datetime;
            self.datetime = dt;
            if !self.restoring_history {
                *self.history_target.borrow_mut() = self.datetime;
                self.undo_stack.push(Box::new(ValueCommand::new(
                    self.history_target.clone(),
                    before,
                    self.datetime,
                    "Edit date-time",
                    "date_time_edit",
                )));
            }
            self.datetime_changed.emit(dt);
            if before.date != dt.date {
                self.date_changed.emit(dt.date);
            }
            if before.time != dt.time {
                self.time_changed.emit(dt.time);
            }
            self.base.request_redraw();
        }
    }
    /// Replaces only the date, keeping the current time part.
    ///
    /// Delegates to [`DateTimeEdit::set_datetime`], so the combined value is
    /// validated against the accepted range as a whole: a date that is in range
    /// on its own may still be rejected when the retained time pushes the
    /// combination outside `minimum ..= maximum`.
    pub fn set_date(&mut self, date: Date) {
        self.set_datetime(DateTime::new(date, self.datetime.time));
    }
    /// Replaces only the time, keeping the current date part.
    ///
    /// Delegates to [`DateTimeEdit::set_datetime`] and is subject to the same
    /// whole-value range check; see [`DateTimeEdit::set_date`].
    pub fn set_time(&mut self, time: Time) {
        self.set_datetime(DateTime::new(self.datetime.date, time));
    }
    /// Sets the inclusive lower bound for accepted values.
    ///
    /// The current value is clamped up into the new range, so the widget never holds
    /// a value below its own minimum — which would make every later
    /// [`DateTimeEdit::set_datetime`] call fail silently.
    pub fn set_minimum_datetime(&mut self, dt: DateTime) {
        if self.minimum == dt {
            return;
        }
        self.minimum = dt;
        self.calendar_sync_dirty = true;
        self.clamp_to_range();
        self.base.request_redraw();
    }
    /// Sets the inclusive upper bound for accepted values.
    ///
    /// The current value is clamped down into the new range, like
    /// [`DateTimeEdit::set_minimum_datetime`] clamps up.
    pub fn set_maximum_datetime(&mut self, dt: DateTime) {
        if self.maximum == dt {
            return;
        }
        self.maximum = dt;
        self.calendar_sync_dirty = true;
        self.clamp_to_range();
        self.base.request_redraw();
    }
    /// Moves the current value inside `minimum..=maximum` if it fell outside.
    ///
    /// Called by both bound setters so "the value is within the range" holds after
    /// any sequence of calls. See [`clamp_ordered_range`] for why an inverted range
    /// resolves to `minimum` — it keeps the widget usable instead of wedged at a
    /// value no write can replace.
    fn clamp_to_range(&mut self) {
        self.datetime = super::clamp_ordered_range(self.datetime, self.minimum, self.maximum);
    }
    /// Stores the display-format pattern.
    ///
    /// The pattern is applied by the next paint; see
    /// `super::date_edit::format_with_pattern_into` for the token table.
    pub fn set_display_format(&mut self, fmt: String) {
        if self.display_format == fmt {
            return;
        }
        self.display_format = fmt;
        self.base.request_redraw();
    }
    /// Enables or disables the calendar affordance.
    ///
    /// With it set, the field paints a 16 px arrow zone at its trailing edge; the month grid is
    /// painted only while [`DateTimeEdit::calendar_open`] is true. Disabling the affordance also
    /// closes an open grid.
    pub fn set_calendar_popup(&mut self, popup: bool) {
        if self.calendar_popup == popup {
            return;
        }
        self.calendar_popup = popup;
        if !popup {
            self.set_calendar_open(false);
        }
        self.base.request_redraw();
    }

    /// Shows or hides the month grid.
    ///
    /// Opening requires the affordance to be enabled. The new state is announced through
    /// `popup_visibility_changed` so a host can close its other popups, and a redraw is
    /// requested.
    pub fn set_calendar_open(&mut self, open: bool) {
        let open = open && self.calendar_popup;
        if self.calendar_open == open {
            return;
        }
        if open {
            self.sync_calendar_open_state();
        }
        self.calendar_open = open;
        self.calendar_reveal.set_open(open);
        self.popup_visibility_changed.emit(open);
        if open {
            self.calendar_opened.emit();
        } else {
            self.calendar_closed.emit();
        }
        self.base.request_redraw();
    }

    /// Opens the calendar if it is closed, closes it if it is open.
    ///
    /// Does nothing while the affordance is disabled.
    pub fn toggle_calendar_popup(&mut self) {
        if self.calendar_popup {
            self.set_calendar_open(!self.calendar_open);
        }
    }

    /// The embedded calendar instance that draws the popup's month grid.
    pub fn calendar(&self) -> &Calendar {
        &self.calendar
    }

    /// Mutably borrows the embedded calendar.
    ///
    /// The field still owns the selected date: the next open re-syncs the calendar's selection,
    /// range and displayed page from this field.
    pub fn calendar_mut(&mut self) -> &mut Calendar {
        &mut self.calendar
    }

    /// Replaces the embedded calendar instance.
    ///
    /// The instance is positioned and synchronised on the next open or draw, so a replacement
    /// does not have to know the field's geometry. Exposing the instance as a *method* rather
    /// than a property is deliberate: `CapabilityValue` has no variant that can carry a widget.
    pub fn set_calendar(&mut self, mut calendar: Calendar) {
        calendar.set_first_day_of_week(self.calendar_first_day);
        self.calendar = calendar;
        self.calendar_sync_dirty = true;
        if self.calendar_open {
            self.sync_calendar_open_state();
        }
        self.base.request_redraw();
    }

    /// The first column of the popup's month grid.
    pub fn calendar_first_day(&self) -> Weekday {
        self.calendar_first_day
    }

    /// Sets the first column of the popup's month grid.
    pub fn set_calendar_first_day(&mut self, weekday: Weekday) {
        if self.calendar_first_day == weekday {
            return;
        }
        self.calendar_first_day = weekday;
        self.calendar.set_first_day_of_week(weekday);
        self.calendar_sync_dirty = true;
        self.base.request_redraw();
    }

    /// Opens the calendar popup, synchronising the embedded calendar first.
    pub fn open_calendar(&mut self) {
        if !self.calendar_popup || self.calendar_open {
            return;
        }
        self.set_calendar_open(true);
    }

    /// Dismisses the calendar popup without changing the value.
    pub fn close_calendar(&mut self) {
        if self.calendar_open {
            self.set_calendar_open(false);
        }
    }

    /// Copies the field's date facts into the embedded calendar.
    fn sync_calendar_open_state(&mut self) {
        self.sync_calendar_page();
        self.calendar.set_first_day_of_week(self.calendar_first_day);
        if let Some(date) = super::date_edit::date_to_naive(self.datetime.date) {
            self.calendar.set_display_month(date);
            self.calendar.set_selected_date(date);
        }
    }

    /// Keeps the embedded calendar's geometry and range in step without moving its page.
    fn sync_calendar_page(&mut self) {
        let popup = super::date_edit::calendar_popup_rect(self.geometry());
        // Nothing that sync writes can have changed since the last push, so the event paths can
        // call this on every pointer move and key press for free (K-P02).
        if !self.calendar_sync_dirty && self.calendar_sync_rect == popup {
            return;
        }
        self.calendar.set_geometry(popup);
        if let Some(minimum) = super::date_edit::date_to_naive(self.minimum.date) {
            self.calendar.set_minimum_date(minimum);
        }
        if let Some(maximum) = super::date_edit::date_to_naive(self.maximum.date) {
            self.calendar.set_maximum_date(maximum);
        }
        self.calendar_sync_rect = popup;
        self.calendar_sync_dirty = false;
    }

    /// Takes the calendar's selected date as this field's date part and dismisses the popup.
    fn commit_calendar_selection(&mut self) {
        let selected = super::date_edit::naive_to_date(self.calendar.selected_date());
        if selected != self.datetime.date {
            self.set_date(selected);
        }
        self.close_calendar();
    }

    /// The section the user is editing, or `None` for whole-second stepping.
    pub fn current_section(&self) -> Option<DateTimeSection> {
        self.current_section
    }

    /// Selects the section Up/Down and typed digits apply to.
    pub fn set_current_section(&mut self, section: Option<DateTimeSection>) {
        if self.current_section == section {
            return;
        }
        self.current_section = section;
        self.digit_buffer.clear();
        self.base.request_redraw();
    }

    /// The box one section occupies inside the value text.
    ///
    /// The six zones are equal sixths of the text run. A custom `display_format` can
    /// order the components differently and this control does not parse the pattern to
    /// recover their positions, so the zones are the documented approximation; the
    /// underline and the pointer target still agree with each other.
    pub fn section_zone(&self, section: DateTimeSection) -> Rect {
        let rect = self.geometry();
        let trailing =
            if self.calendar_popup { 16u32 } else { 0 } + if self.spin_buttons { 24 } else { 0 };
        let width = rect.width.saturating_sub(12 + trailing);
        let part = width / 6;
        Rect::new(rect.x + 6 + section.index() as i32 * part as i32, rect.y, part, rect.height)
    }

    /// The section containing `pos`, when the point is on the value text.
    fn section_at(&self, pos: crate::core::Point) -> Option<DateTimeSection> {
        if !self.geometry().contains_point(pos) {
            return None;
        }
        DateTimeSection::ORDER
            .into_iter()
            .find(|section| self.section_zone(*section).contains_point(pos))
    }

    /// Shifts a date by whole days, rolling months and years.
    fn shift_date(date: &mut Date, days: i32) {
        for _ in 0..days.unsigned_abs() {
            if days > 0 {
                if date.day() < date.days_in_month() {
                    date.set_day(date.day() + 1);
                } else {
                    date.set_day(1);
                    if date.month() < 12 {
                        date.set_month(date.month() + 1);
                    } else {
                        date.set_month(1);
                        date.set_year(date.year() + 1);
                    }
                }
            } else if date.day() > 1 {
                date.set_day(date.day() - 1);
            } else {
                if date.month() > 1 {
                    date.set_month(date.month() - 1);
                } else {
                    date.set_month(12);
                    date.set_year(date.year() - 1);
                }
                date.set_day(date.days_in_month());
            }
        }
    }

    /// Steps the active section, or one whole second when no section is active.
    pub fn step_current_section(&mut self, delta: i32) {
        let Some(section) = self.current_section else {
            if delta >= 0 {
                self.step_up();
            } else {
                self.step_down();
            }
            return;
        };
        if delta == 0 {
            return;
        }
        let mut next = self.datetime;
        match section {
            DateTimeSection::Year => next.date.set_year(next.date.year() + delta),
            DateTimeSection::Month => {
                let total = next.date.year() * 12 + next.date.month() as i32 - 1 + delta;
                next.date.set_year(total.div_euclid(12));
                next.date.set_month((total.rem_euclid(12) + 1) as u8);
            }
            DateTimeSection::Day => Self::shift_date(&mut next.date, delta),
            DateTimeSection::Hour => {
                let total = next.time.hour() as i32 + delta;
                let days = total.div_euclid(24);
                next.time.set_hour(total.rem_euclid(24) as u8);
                Self::shift_date(&mut next.date, days);
            }
            DateTimeSection::Minute => {
                let total = next.time.minute() as i32 + delta;
                let carry = total.div_euclid(60);
                next.time.set_minute(total.rem_euclid(60) as u8);
                let hour_total = next.time.hour() as i32 + carry;
                let days = hour_total.div_euclid(24);
                next.time.set_hour(hour_total.rem_euclid(24) as u8);
                Self::shift_date(&mut next.date, days);
            }
            DateTimeSection::Second => {
                let total = next.time.second() as i32 + delta;
                let carry = total.div_euclid(60);
                next.time.set_second(total.rem_euclid(60) as u8);
                let minute_total = next.time.minute() as i32 + carry;
                let hour_carry = minute_total.div_euclid(60);
                next.time.set_minute(minute_total.rem_euclid(60) as u8);
                let hour_total = next.time.hour() as i32 + hour_carry;
                let days = hour_total.div_euclid(24);
                next.time.set_hour(hour_total.rem_euclid(24) as u8);
                Self::shift_date(&mut next.date, days);
            }
        }
        self.set_datetime(next);
    }

    /// Types one digit into the active section.
    pub fn type_digit(&mut self, digit: char) -> bool {
        if !digit.is_ascii_digit() {
            return false;
        }
        let Some(section) = self.current_section else {
            return false;
        };
        self.digit_buffer.push(digit);
        while self.digit_buffer.len() > section.digit_capacity() {
            self.digit_buffer.remove(0);
        }
        let Ok(value) = self.digit_buffer.parse::<i32>() else {
            return false;
        };
        let mut next = self.datetime;
        match section {
            DateTimeSection::Year => {
                if value < 1000 {
                    return true;
                }
                next.date.set_year(value);
            }
            DateTimeSection::Month => next.date.set_month(value as u8),
            DateTimeSection::Day => next.date.set_day(value as u8),
            DateTimeSection::Hour => next.time.set_hour(value as u8),
            DateTimeSection::Minute => next.time.set_minute(value as u8),
            DateTimeSection::Second => next.time.set_second(value as u8),
        }
        self.set_datetime(next);
        true
    }

    /// Whether the spin-button pair is painted.
    pub fn spin_buttons(&self) -> bool {
        self.spin_buttons
    }

    /// Shows or hides the spin-button pair. Off by default.
    pub fn set_spin_buttons(&mut self, visible: bool) {
        if self.spin_buttons == visible {
            return;
        }
        self.spin_buttons = visible;
        self.base.request_redraw();
    }

    /// The up and down spin buttons, when shown.
    pub fn spin_button_rects(&self) -> Option<(Rect, Rect)> {
        if !self.spin_buttons {
            return None;
        }
        let rect = self.geometry();
        let width = 24u32.min(rect.width);
        let x =
            rect.x + rect.width as i32 - width as i32 - if self.calendar_popup { 16 } else { 0 };
        let half = rect.height / 2;
        Some((
            Rect::new(x, rect.y, width, half),
            Rect::new(x, rect.y + half as i32, width, rect.height - half),
        ))
    }

    /// Text painted instead of the value when the value equals the minimum.
    pub fn special_value_text(&self) -> Option<&str> {
        self.special_value_text.as_deref()
    }

    /// Sets the special value text, or clears it with `None`.
    pub fn set_special_value_text(&mut self, text: Option<String>) {
        if self.special_value_text == text {
            return;
        }
        self.special_value_text = text;
        self.base.request_redraw();
    }
    /// Advances the value by one second, carrying into minutes, hours, and days.
    ///
    /// The millisecond component is preserved. The day carries into the next
    /// month or year as needed, using the length of the month being left. The
    /// result goes through [`DateTimeEdit::set_datetime`], so advancing beyond
    /// [`DateTimeEdit::maximum_datetime`] is rejected and leaves the value
    /// unchanged rather than wrapping around. A successful step is undoable.
    pub fn step_up(&mut self) {
        let mut t = self.datetime.time;
        let new_sec = t.second() + 1;
        if new_sec >= 60 {
            t.set_second(0);
            let new_min = t.minute() + 1;
            if new_min >= 60 {
                t.set_minute(0);
                let new_hour = t.hour() + 1;
                if new_hour >= 24 {
                    t.set_hour(0);
                    let mut d = self.datetime.date;
                    let next_day = d.day() as u16 + 1;
                    if next_day > d.days_in_month() as u16 {
                        d.set_day(1);
                        let next_month = d.month() + 1;
                        if next_month > 12 {
                            d.set_month(1);
                            d.set_year(d.year() + 1);
                        } else {
                            d.set_month(next_month);
                        }
                    } else {
                        d.set_day(next_day as u8);
                    }
                    self.set_datetime(DateTime::new(d, t));
                    return;
                } else {
                    t.set_hour(new_hour);
                }
            } else {
                t.set_minute(new_min);
            }
        } else {
            t.set_second(new_sec);
        }
        self.set_time(t);
    }
    /// Moves the value back by one second, borrowing from minutes, hours, and
    /// days.
    ///
    /// The millisecond component is preserved. The day borrows into the previous
    /// month or year as needed, using the length of the month arrived at, so
    /// `2024-03-01 00:00:00` steps back to `2024-02-29 23:59:59`. The change is
    /// applied through [`DateTimeEdit::set_datetime`], so going below
    /// [`DateTimeEdit::minimum_datetime`] is rejected and leaves the value
    /// unchanged rather than wrapping around. A successful step is undoable.
    pub fn step_down(&mut self) {
        let mut t = self.datetime.time;
        if t.second() > 0 {
            t.set_second(t.second() - 1);
        } else {
            t.set_second(59);
            if t.minute() > 0 {
                t.set_minute(t.minute() - 1);
            } else {
                t.set_minute(59);
                if t.hour() > 0 {
                    t.set_hour(t.hour() - 1);
                } else {
                    t.set_hour(23);
                    let mut d = self.datetime.date;
                    if d.day() > 1 {
                        d.set_day(d.day() - 1);
                    } else {
                        if d.month() > 1 {
                            d.set_month(d.month() - 1);
                        } else {
                            d.set_month(12);
                            d.set_year(d.year() - 1);
                        }
                        d.set_day(d.days_in_month());
                    }
                    self.set_datetime(DateTime::new(d, t));
                    return;
                }
            }
        }
        self.set_time(t);
    }
    /// Reverts the most recent value change.
    ///
    /// Returns `true` if a change was undone, `false` when the undo stack is
    /// empty. Undoing emits `datetime_changed` with the restored value and
    /// requests a redraw, but does not push a new undo entry.
    pub fn undo(&mut self) -> bool {
        if self.undo_stack.undo().is_err() {
            return false;
        }
        self.restore_history_datetime();
        true
    }
    /// Re-applies the most recently undone value change.
    ///
    /// Returns `true` if a change was redone, `false` when there is nothing to
    /// redo. Like [`DateTimeEdit::undo`], it emits `datetime_changed` and
    /// requests a redraw without recording a new undo entry.
    pub fn redo(&mut self) -> bool {
        if self.undo_stack.redo().is_err() {
            return false;
        }
        self.restore_history_datetime();
        true
    }
    /// Returns `true` when there is at least one value change to undo.
    pub fn can_undo(&self) -> bool {
        self.undo_stack.can_undo()
    }
    /// Returns `true` when there is at least one undone value change to redo.
    pub fn can_redo(&self) -> bool {
        self.undo_stack.can_redo()
    }
    fn restore_history_datetime(&mut self) {
        self.restoring_history = true;
        self.datetime = *self.history_target.borrow();
        self.restoring_history = false;
        self.datetime_changed.emit(self.datetime);
        self.base.request_redraw();
    }
}
impl Widget for DateTimeEdit {
    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) -> Size {
        crate::core::Size::new(160, 28)
    }

    /// Advances the calendar popup's reveal; `true` while it owes frames.
    fn tick(&mut self, delta_ms: u32) -> bool {
        self.calendar_reveal.tick(delta_ms)
    }

    /// Whether the calendar popup is mid-reveal.
    fn is_animating(&self) -> bool {
        self.calendar_reveal.is_animating()
    }
    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 {
            "datetime_changed" => {
                Some(EventSignalRef::mapped("datetime_changed", &self.datetime_changed, |v| {
                    CapabilityValue::String(format!("{v:?}"))
                }))
            }
            "date_changed" => {
                Some(EventSignalRef::mapped("date_changed", &self.date_changed, |v| {
                    CapabilityValue::String(format!("{v:?}"))
                }))
            }
            "time_changed" => {
                Some(EventSignalRef::mapped("time_changed", &self.time_changed, |v| {
                    CapabilityValue::String(format!("{v:?}"))
                }))
            }
            "editing_finished" => {
                Some(EventSignalRef::unit("editing_finished", &self.editing_finished))
            }
            "popup_visibility_changed" => Some(EventSignalRef::mapped(
                "popup_visibility_changed",
                &self.popup_visibility_changed,
                |v| CapabilityValue::Bool(*v),
            )),
            "calendar_opened" => {
                Some(EventSignalRef::unit("calendar_opened", &self.calendar_opened))
            }
            "calendar_closed" => {
                Some(EventSignalRef::unit("calendar_closed", &self.calendar_closed))
            }
            _ => None,
        }
    }
}

/// `DateTimePicker`'s property contract (the control is `DateTimeEdit`;
/// `DateTimePicker` is a type alias for it, so this single impl covers both
/// names).
///
/// The read path matches the old dispatch exactly: `datetime` is published
/// through the value's `Display`, and the schema declares all three properties
/// non-writable, so only the base helpers answer on write.
impl WidgetProperties for DateTimeEdit {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "datetime" => Ok(CapabilityValue::String(self.datetime().to_string())),
            "display_format" => Ok(CapabilityValue::String(self.display_format().to_string())),
            "calendar_popup" => Ok(CapabilityValue::Bool(self.calendar_popup())),
            "calendar_first_day" => {
                Ok(CapabilityValue::String(weekday_to_str(self.calendar_first_day()).to_string()))
            }
            "alignment" => Ok(CapabilityValue::String(
                horizontal_alignment_to_str(self.alignment()).to_string(),
            )),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "datetime" => {
                self.set_datetime(expect_datetime(value)?);
                Ok(())
            }
            "display_format" => {
                self.set_display_format(expect_string(value)?);
                Ok(())
            }
            "calendar_popup" => {
                self.set_calendar_popup(expect_bool(value)?);
                Ok(())
            }
            "calendar_first_day" => {
                self.set_calendar_first_day(expect_weekday(value)?);
                Ok(())
            }
            "alignment" => {
                self.set_alignment(expect_horizontal_alignment(value)?);
                Ok(())
            }
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        property_names_of![
            "datetime",
            "display_format",
            "calendar_popup",
            "calendar_first_day",
            "alignment",
            BASE_PROPERTY_NAMES
        ]
    }
}

impl EventHandler for DateTimeEdit {
    fn handle_event(&mut self, event: &Event) {
        self.base.handle_event(event);
        if !self.base.is_enabled() {
            return;
        }
        // Pointer tracking inside the open popup belongs to the embedded calendar.
        if self.calendar_open && matches!(event, Event::MouseMove { .. } | Event::MouseLeave { .. })
        {
            self.sync_calendar_page();
            self.calendar.handle_event(event);
        }
        match event {
            Event::MousePress { pos, button, .. } if *button == 1 => {
                let field = self.geometry();
                if let Some((up, down)) = self.spin_button_rects() {
                    if up.contains_point(*pos) {
                        self.step_current_section(1);
                        return;
                    }
                    if down.contains_point(*pos) {
                        self.step_current_section(-1);
                        return;
                    }
                }
                // The arrow is inside the field's own box, so it is checked before anything
                // else that treats the field as the value's display area.
                if self.calendar_popup
                    && super::date_edit::calendar_arrow_rect(field).contains_point(*pos)
                {
                    self.toggle_calendar_popup();
                } else if self.calendar_open {
                    // The popup is the embedded calendar's own rectangle; a press inside it is
                    // forwarded there. `set_date` keeps the current time part, so a date pick
                    // cannot silently reset the user's time.
                    let popup = super::date_edit::calendar_popup_rect(field);
                    if popup.contains_point(*pos) {
                        self.sync_calendar_page();
                        let before = self.calendar.selected_date();
                        self.calendar.handle_event(event);
                        let after = self.calendar.selected_date();
                        if after != before {
                            self.set_date(super::date_edit::naive_to_date(after));
                            self.close_calendar();
                        }
                    } else {
                        self.close_calendar();
                    }
                } else if let Some(section) = self.section_at(*pos) {
                    self.set_current_section(Some(section));
                }
            }
            Event::MouseDoubleClick { pos, .. } if self.calendar_open => {
                if super::date_edit::calendar_popup_rect(self.geometry()).contains_point(*pos) {
                    self.sync_calendar_page();
                    self.calendar.handle_event(event);
                    self.commit_calendar_selection();
                }
            }
            Event::Wheel { delta, .. } => {
                if delta.y > 0 {
                    self.step_current_section(1);
                } else if delta.y < 0 {
                    self.step_current_section(-1);
                }
            }
            Event::FocusLost => {
                self.editing_finished.emit();
            }
            Event::KeyPress { key, .. } if self.calendar_open => {
                // Escape closes the popup before any value navigation runs. Every other key
                // belongs to the embedded calendar while it is open: arrows move its selection,
                // Page Up/Down move its page, and Enter/Space commit.
                if *key == 27 {
                    self.close_calendar();
                } else {
                    self.sync_calendar_page();
                    self.calendar.handle_event(event);
                    if *key == 13 || *key == 32 {
                        self.commit_calendar_selection();
                    }
                }
            }
            Event::KeyPress { key, modifiers } => match *key {
                // Escape closes an open grid first, before any value navigation runs.
                27 if self.calendar_open => self.set_calendar_open(false),
                13 => {
                    // Enter commits the edited value, which is where a field's edit ends.
                    self.editing_finished.emit();
                }
                // Undo and redo are the platform primary chord: Control+Z/Y on
                // Windows and Linux, Command+Z/Y on macOS. A shifted chord is a
                // different binding and stays unbound here.
                _ if primary_shortcut_pressed(*key, *modifiers, Key::Z) => {
                    let _ = self.undo();
                }
                _ if primary_shortcut_pressed(*key, *modifiers, Key::Y) => {
                    let _ = self.redo();
                }
                38 => self.step_current_section(1),
                40 => self.step_current_section(-1),
                37 => {
                    if let Some(section) = self.current_section {
                        let previous = section.previous();
                        self.set_current_section(Some(previous));
                    }
                }
                39 => {
                    if let Some(section) = self.current_section {
                        let next = section.next();
                        self.set_current_section(Some(next));
                    }
                }
                48..=57 if self.current_section.is_some() => {
                    if let Some(digit) = char::from_u32(*key) {
                        self.type_digit(digit);
                    }
                }
                _ => { /* Other keys are not relevant */ }
            },
            _ => { /* Other events are not relevant */ }
        }
    }
}
impl Draw for DateTimeEdit {
    fn draw(&mut self, context: &mut RenderContext) {
        let rect = self.geometry();

        // Chrome colours resolve explicit style first, then the theme's resolved style
        // for this control, and only then fall back to a literal. Without the theme step
        // a light/dark switch changed nothing on screen: the field, its border and its
        // text were all hardcoded, which the rendering census reported as theme-blind.
        //
        // The style lookups are the cached, revision-validated forms: `resolved_theme_style_shared`
        // is a refcount bump on a hit and `palette_shared` answers both colours this draw needs in
        // one read, so neither takes the manager lock (K-P03/K-P17). `date_time_edit` classifies as
        // `Input`, whose resolved style supplies `text_color` and `border_color` but no
        // `background_color` (ThemeRole::Input resolves that to `None`), so the interior is derived
        // below from the theme's own background rather than left unset.
        let style = self.base.style();
        let theme = crate::style::resolved_theme_style_shared("date_time_edit");
        let (window_fill, foreground) = match crate::style::palette_shared() {
            Some(colors) => (colors.background, colors.foreground),
            None => (Color::rgb(240, 240, 240), Color::BLACK),
        };

        let ink = style
            .text_color
            .or_else(|| theme.as_ref().and_then(|t| t.text_color))
            .unwrap_or(foreground);
        // An editable field's interior: one step from the window fill toward the text
        // colour, so it reads as a field on a light theme and on a dark one, and is never
        // byte-identical to the window behind it.
        let field = window_fill.blend(&ink, 0.08);
        // The filter is on the **resolved** value, not only on the theme's: the active
        // theme is applied to every control before it is drawn, so `style.background_color`
        // already holds the resolved fill and letting it through unfiltered is exactly the
        // invisible-field defect this guards against. A caller's own colour still wins.
        let surface = match style.background_color {
            Some(resolved) if resolved != window_fill => resolved,
            _ => field,
        };
        let border = style
            .border_color
            .or_else(|| theme.as_ref().and_then(|t| t.border_color))
            .filter(|resolved| *resolved != surface)
            .unwrap_or_else(|| surface.blend(&ink, 0.35));

        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,
        );
        // Rounded to the face's radius: a square outline left the four corners unpainted.
        crate::widget::draw::stroke_surface_border(
            context,
            rect,
            self.style().border_radius.unwrap_or(0),
            border,
            1,
        );
        // The value is spelled by the caller's `display_format` when that pattern is one this control
        // can render, and by `DateTime`'s own spelling otherwise — the same rule and the same shared
        // substituter as `date_edit` and `time_edit`, so the same pattern cannot render three ways.
        // Into the buffer directly: the allocating wrapper built the result plus one string per token.
        let mut text = String::new();
        let special = self.special_value_text.as_deref().filter(|_| self.datetime == self.minimum);
        if let Some(special) = special {
            text.push_str(special);
        } else if !super::date_edit::format_with_pattern_into(
            &self.display_format,
            super::date_edit::DateTimeComponents {
                year: Some(self.datetime.date.year()),
                month: Some(self.datetime.date.month()),
                day: Some(self.datetime.date.day()),
                hour: Some(self.datetime.time.hour()),
                minute: Some(self.datetime.time.minute()),
                second: Some(self.datetime.time.second()),
                millisecond: Some(self.datetime.time.msec()),
            },
            &mut text,
        ) {
            text.clear();
            let _ = write!(text, "{}", self.datetime);
        }
        // Vertically centred through the shared primitive: `rect.y + height / 2` puts the
        // glyph box's top edge on the field's middle line, so the value sat half a line low.
        // The **effective font** — the resolved theme/caller font — so the value honours the theme
        // body font and the user's text scale (D09-STYLE-01).
        let font = effective_font(style);
        let line = context.text_line(rect, font);
        // The arrow zone is part of the field's chrome, so the value must not run under it.
        let text_width = rect
            .width
            .saturating_sub(12)
            .saturating_sub(if self.calendar_popup {
                super::date_edit::CALENDAR_ARROW_W
            } else {
                0
            })
            .saturating_sub(if self.spin_buttons { 24 } else { 0 });
        context.draw_text_fitted_cow(
            Rect { x: rect.x + 6, y: line.y, width: text_width, height: line.height },
            text.as_str(),
            font,
            ink,
            self.alignment.to_horizontal().unwrap_or(HorizontalAlignment::Left),
        );

        if let Some(section) = self.current_section {
            let zone = self.section_zone(section);
            let y = line.y + line.height as i32;
            context.draw_line(
                Point::new(zone.x + 1, y),
                Point::new(zone.x + zone.width as i32 - 1, y),
                ink,
            );
        }

        if self.calendar_popup {
            super::date_edit::draw_calendar_arrow(context, rect, ink);
        }
        if let Some((up, down)) = self.spin_button_rects() {
            let fill = surface.blend(&ink, 0.08);
            context.fill_rect(up, fill);
            context.fill_rect(down, fill);
            context.draw_rect(up, border);
            context.draw_rect(down, border);
            let side = up.width.min(up.height).min(10);
            crate::widget::draw_icon_centered(
                context,
                up,
                side,
                ink,
                crate::widget::IconName::ChevronUp,
            );
            crate::widget::draw_icon_centered(
                context,
                down,
                side,
                ink,
                crate::widget::IconName::ChevronDown,
            );
        }
        if self.calendar_open || !self.calendar_reveal.is_closed() {
            let popup = super::date_edit::calendar_popup_rect(rect);
            let revealed = self.calendar_reveal.revealed(
                popup,
                crate::widget::menu_toolbar::popup_reveal::RevealDirection::Down,
                crate::widget::menu_toolbar::popup_reveal::PopupReveal::travel_cap_for(Size::new(
                    popup.width,
                    popup.height,
                )),
            );
            let clipped = !self.calendar_reveal.is_closed();
            if clipped {
                context.push_clip(revealed.x, revealed.y, revealed.width, revealed.height);
            }
            // One month-grid implementation: the embedded calendar paints the popup.
            self.sync_calendar_page();
            self.calendar.draw(context);
            if clipped {
                context.pop_clip();
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::Point;
    use crate::widget::input_widgets::shortcut_editor::{
        non_primary_modifier_event_bits, primary_modifier_event_bits,
    };
    use crate::widget::svg::render_to_svg;
    use std::sync::{Arc, Mutex};

    // ─── DateTime helper tests ───────────────────────────────────

    #[test]
    fn date_time_creation_and_accessors() {
        let d = Date::new(2026, 6, 8);
        let t = Time::new(14, 30, 15, 0);
        let dt = DateTime::new(d, t);
        assert_eq!(dt.date, d);
        assert_eq!(dt.time, t);
        assert!(dt.is_valid());
    }

    #[test]
    fn date_time_is_valid() {
        let valid = DateTime::new(Date::new(2026, 6, 8), Time::new(12, 0, 0, 0));
        assert!(valid.is_valid());

        let bad_date = DateTime::new(Date::new(2026, 13, 1), Time::new(12, 0, 0, 0));
        assert!(!bad_date.is_valid());

        let bad_time = DateTime::new(Date::new(2026, 6, 8), Time::new(25, 0, 0, 0));
        // Time::new clamps, so it's actually valid
        assert!(bad_time.is_valid());
    }

    #[test]
    fn date_time_display() {
        let dt = DateTime::new(Date::new(2026, 6, 8), Time::new(9, 5, 3, 0));
        assert_eq!(dt.to_string(), "2026-06-08 09:05:03");
    }

    #[test]
    fn date_time_ordering() {
        let early = DateTime::new(Date::new(2026, 6, 8), Time::new(8, 0, 0, 0));
        let late = DateTime::new(Date::new(2026, 6, 8), Time::new(10, 0, 0, 0));
        assert!(early < late);
        assert!(late > early);
        assert_eq!(early, DateTime::new(Date::new(2026, 6, 8), Time::new(8, 0, 0, 0)));
    }

    // ─── DateTimeEdit widget tests ───────────────────────────────

    #[test]
    fn date_time_edit_creation_defaults() {
        let editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        assert_eq!(editor.date(), Date::today());
        assert_eq!(editor.time(), Time::new(0, 0, 0, 0));
        assert_eq!(editor.display_format(), "yyyy-MM-dd HH:mm:ss");
        assert!(!editor.calendar_popup());
        assert!(editor.is_visible());
        assert!(editor.is_enabled());
    }

    #[test]
    fn date_time_edit_set_and_get_datetime() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        let dt = DateTime::new(Date::new(2026, 12, 25), Time::new(10, 30, 0, 0));
        editor.set_datetime(dt);
        assert_eq!(editor.datetime(), dt);
        assert_eq!(editor.date(), Date::new(2026, 12, 25));
        assert_eq!(editor.time(), Time::new(10, 30, 0, 0));
    }

    #[test]
    fn date_time_edit_set_date_part() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        let new_date = Date::new(2027, 4, 15);
        editor.set_date(new_date);
        assert_eq!(editor.date(), new_date);
        // Time part should be unchanged
        assert_eq!(editor.time(), Time::new(0, 0, 0, 0));
    }

    #[test]
    fn date_time_edit_set_time_part() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_date(Date::new(2026, 6, 8));
        let new_time = Time::new(18, 45, 30, 0);
        editor.set_time(new_time);
        assert_eq!(editor.time(), new_time);
        assert_eq!(editor.date(), Date::new(2026, 6, 8));
    }

    #[test]
    fn date_time_edit_set_display_format() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_display_format("dd/MM/yyyy HH:mm".to_string());
        assert_eq!(editor.display_format(), "dd/MM/yyyy HH:mm");
    }

    #[test]
    fn date_time_edit_set_minimum_maximum() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        let min = DateTime::new(Date::new(2025, 1, 1), Time::new(0, 0, 0, 0));
        let max = DateTime::new(Date::new(2030, 12, 31), Time::new(23, 59, 59, 999));
        editor.set_minimum_datetime(min);
        editor.set_maximum_datetime(max);
        assert_eq!(editor.minimum_datetime(), min);
        assert_eq!(editor.maximum_datetime(), max);
    }

    #[test]
    fn date_time_edit_set_datetime_clamps_to_range() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        let min = DateTime::new(Date::new(2025, 6, 1), Time::new(0, 0, 0, 0));
        let max = DateTime::new(Date::new(2025, 6, 30), Time::new(23, 59, 59, 999));
        editor.set_minimum_datetime(min);
        editor.set_maximum_datetime(max);

        // Set within range — should work
        let within = DateTime::new(Date::new(2025, 6, 15), Time::new(12, 0, 0, 0));
        editor.set_datetime(within);
        assert_eq!(editor.datetime(), within);

        // Set below minimum — should be rejected
        let before = DateTime::new(Date::new(2025, 5, 1), Time::new(0, 0, 0, 0));
        editor.set_datetime(before);
        assert_eq!(editor.datetime(), within);

        // Set above maximum — should be rejected
        let after = DateTime::new(Date::new(2025, 7, 1), Time::new(0, 0, 0, 0));
        editor.set_datetime(after);
        assert_eq!(editor.datetime(), within);
    }

    #[test]
    fn date_time_edit_date_time_changed_signal_emits() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        let captured =
            Arc::new(Mutex::new(DateTime::new(Date::new(2000, 1, 1), Time::new(0, 0, 0, 0))));
        let sink = captured.clone();
        editor.datetime_changed.connect(move |dt| {
            if let Ok(mut c) = sink.lock() {
                *c = *dt;
            }
        });

        let expected = DateTime::new(Date::new(2026, 12, 25), Time::new(10, 30, 0, 0));
        editor.set_datetime(expected);
        let got = *captured.lock().unwrap();
        assert_eq!(got, expected);
    }

    #[test]
    fn date_time_edit_date_time_changed_not_emitted_for_unchanged() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        let hits = Arc::new(Mutex::new(0usize));
        let hits_clone = hits.clone();
        editor.datetime_changed.connect(move |_| {
            if let Ok(mut h) = hits_clone.lock() {
                *h += 1;
            }
        });

        // Setting same value should NOT emit
        let default_dt = DateTime::new(Date::today(), Time::new(0, 0, 0, 0));
        editor.set_datetime(default_dt);
        assert_eq!(*hits.lock().unwrap(), 0);
    }

    #[test]
    fn date_time_edit_calendar_popup_visibility() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        assert!(!editor.calendar_popup());
        editor.set_calendar_popup(true);
        assert!(editor.calendar_popup());
        editor.set_calendar_popup(false);
        assert!(!editor.calendar_popup());
    }

    #[test]
    fn date_time_edit_geometry_delegation() {
        let rect = Rect::new(10, 20, 280, 30);
        let editor = DateTimeEdit::new(rect);
        assert_eq!(editor.geometry(), rect);
        assert_eq!(editor.position(), Point::new(10, 20));
        assert_eq!(editor.size(), crate::core::Size::new(280, 30));
    }

    #[test]
    fn date_time_edit_widget_kind() {
        let editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        assert_eq!(editor.kind(), WidgetKind::DateTimePicker);
    }

    #[test]
    fn date_time_edit_ids_are_unique() {
        let a = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        let b = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        assert_ne!(a.id(), b.id());
    }

    #[test]
    fn date_time_edit_svg_output() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_datetime(DateTime::new(Date::new(2026, 6, 8), Time::new(12, 30, 45, 0)));
        let svg = render_to_svg(&mut editor);
        assert!(svg.starts_with("<svg"));
        assert!(svg.ends_with("</svg>"));
        assert!(svg.contains("width=\"280\""));
        assert!(svg.contains("height=\"30\""));
        assert!(svg.contains("2026-06-08 12:30:45") || svg.contains("fill="));
    }

    /// The calendar popup is **painted**, not merely stored.
    ///
    /// # The defect this pins
    ///
    /// `calendar_popup` was stored, published, settable, and read by nothing -- the exact defect
    /// `date_edit` had, in its sibling control. The getter and setter and the schema row all existed,
    /// and setting it redrew the same field.
    ///
    /// The assertion reads the **document**, not the accessor: a model-only round-trip (`set` then
    /// `get`) is exactly what the broken version already passed. And it checks that the ink is the
    /// *month's days*, because "the documents differ" alone is satisfied by any extra element.
    #[test]
    fn the_calendar_popup_is_actually_painted() {
        use crate::widget::svg::{render_to_svg, text_subpath_count};
        let _theme_guard = crate::style::theme_test_guard();

        let make = |popup: bool| {
            let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
            editor.set_datetime(DateTime::new(Date::new(2026, 6, 8), Time::new(12, 30, 45, 0)));
            editor.set_calendar_popup(popup);
            if popup {
                editor.set_calendar_open(true);
            }
            editor
        };

        let mut closed = make(false);
        let closed_svg = render_to_svg(&mut closed);
        let mut open = make(true);
        let open_svg = render_to_svg(&mut open);

        assert_ne!(closed_svg, open_svg, "opening the popup must change the picture");
        // The grid is the weekday row plus the month's days: 7 + 30 for June, against the single
        // `2026-06-08 12:30:45` run the closed field paints. The bound is stated as "at least the
        // month's day count" rather than as a ratio, so it names the thing that must be there.
        let closed_ink = text_subpath_count(&closed_svg);
        let open_ink = text_subpath_count(&open_svg);
        assert!(
            open_ink > closed_ink,
            "the popup must add ink: closed={closed_ink} open={open_ink}"
        );
        assert!(
            open_ink >= 30,
            "the popup must paint the month's days, not a stub: open={open_ink}"
        );
        assert!(
            open_svg.matches("<rect").count() > closed_svg.matches("<rect").count(),
            "the popup paints its own plate: {open_svg}"
        );
    }

    /// The affordance flag alone must not paint the grid.
    #[test]
    fn calendar_popup_alone_does_not_paint_the_grid() {
        use crate::widget::svg::{render_to_svg, text_subpath_count};
        let _theme_guard = crate::style::theme_test_guard();

        let mut plain = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        plain.set_datetime(DateTime::new(Date::new(2026, 6, 8), Time::new(12, 30, 45, 0)));
        let mut affordance = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        affordance.set_datetime(DateTime::new(Date::new(2026, 6, 8), Time::new(12, 30, 45, 0)));
        affordance.set_calendar_popup(true);

        assert!(!affordance.calendar_open(), "the flag alone must not open the grid");
        let plain_svg = render_to_svg(&mut plain);
        let affordance_svg = render_to_svg(&mut affordance);
        assert_ne!(plain_svg, affordance_svg, "the arrow affordance itself must be painted");
        assert_eq!(
            text_subpath_count(&affordance_svg),
            text_subpath_count(&plain_svg),
            "no weekday/day runs may appear until the grid is open"
        );
    }

    /// Clicking the arrow opens the grid and announces the new state.
    #[test]
    fn arrow_click_emits_popup_visibility_changed_and_paints_the_grid() {
        use crate::widget::svg::{render_to_svg, text_subpath_count};
        let _theme_guard = crate::style::theme_test_guard();

        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_datetime(DateTime::new(Date::new(2026, 6, 8), Time::new(12, 30, 45, 0)));
        editor.set_calendar_popup(true);

        let seen = Arc::new(Mutex::new(Vec::<bool>::new()));
        let sink = Arc::clone(&seen);
        editor.popup_visibility_changed.connect(move |open| {
            if let Ok(mut v) = sink.lock() {
                v.push(*open);
            }
        });

        let closed_svg = render_to_svg(&mut editor);
        let arrow = super::super::date_edit::calendar_arrow_rect(editor.geometry()).center();
        editor.handle_event(&Event::mouse_press(arrow.x, arrow.y, 1));

        assert!(editor.calendar_open(), "the arrow press must open the grid");
        assert_eq!(*seen.lock().expect("visibility lock"), vec![true]);
        let open_svg = render_to_svg(&mut editor);
        assert!(
            text_subpath_count(&open_svg) > text_subpath_count(&closed_svg),
            "the open grid must add the month's day runs"
        );
    }

    /// The centre of a day cell in the embedded calendar, derived from the calendar's documented
    /// layout and the popup rectangle both the draw pass and the hit test use. Test-only.
    fn calendar_day_center(editor: &DateTimeEdit, day: u32) -> Point {
        use chrono::Datelike;
        let popup = super::super::date_edit::calendar_popup_rect(editor.geometry());
        let nav_h = 30_i32;
        let header_h = 24_i32;
        let grid_y = popup.y + nav_h + header_h;
        let grid_h = popup.height as i32 - nav_h - header_h;
        let cell_w = (popup.width / 7).max(1) as i32;
        let cell_h = (grid_h / 6).max(1);
        let date = editor.datetime.date;
        let first = chrono::NaiveDate::from_ymd_opt(date.year(), date.month() as u32, 1)
            .expect("the field's month always has a first day");
        let slot = first.weekday().num_days_from_sunday() + day - 1;
        Point::new(
            popup.x + (slot % 7) as i32 * cell_w + cell_w / 2,
            grid_y + (slot / 7) as i32 * cell_h + cell_h / 2,
        )
    }

    /// Choosing a day updates the date part and preserves the time part.
    #[test]
    fn selecting_a_day_updates_the_date_and_keeps_the_time() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_datetime(DateTime::new(Date::new(2026, 6, 8), Time::new(14, 30, 15, 0)));
        editor.set_calendar_popup(true);
        editor.set_calendar_open(true);

        let cell = calendar_day_center(&editor, 15);
        editor.handle_event(&Event::mouse_press(cell.x, cell.y, 1));

        assert_eq!(editor.date(), Date::new(2026, 6, 15));
        assert_eq!(editor.time(), Time::new(14, 30, 15, 0), "the time part must survive");
        assert!(!editor.calendar_open(), "picking a day finishes the interaction");
    }

    /// The popup is the embedded `Calendar`, and the two new signals bracket its visibility.
    #[test]
    fn calendar_open_and_close_signals_bracket_the_embedded_calendar() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_datetime(DateTime::new(Date::new(2026, 6, 8), Time::new(14, 30, 15, 0)));
        editor.set_calendar_popup(true);

        let opened = Arc::new(Mutex::new(0_usize));
        let closed = Arc::new(Mutex::new(0_usize));
        let opened_sink = Arc::clone(&opened);
        let closed_sink = Arc::clone(&closed);
        editor.calendar_opened.connect(move || {
            if let Ok(mut count) = opened_sink.lock() {
                *count += 1;
            }
        });
        editor.calendar_closed.connect(move || {
            if let Ok(mut count) = closed_sink.lock() {
                *count += 1;
            }
        });

        editor.open_calendar();
        assert!(editor.calendar_open());
        assert_eq!(
            editor.calendar().selected_date(),
            chrono::NaiveDate::from_ymd_opt(2026, 6, 8).unwrap()
        );
        editor.close_calendar();
        assert!(!editor.calendar_open());
        assert_eq!(*opened.lock().expect("opened lock"), 1);
        assert_eq!(*closed.lock().expect("closed lock"), 1);
    }

    /// Escape closes an open grid before any value navigation runs.
    #[test]
    fn escape_closes_the_calendar_first() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        let value = DateTime::new(Date::new(2026, 6, 8), Time::new(14, 30, 15, 0));
        editor.set_datetime(value);
        editor.set_calendar_popup(true);
        editor.set_calendar_open(true);

        editor.handle_event(&Event::KeyPress { key: 27, modifiers: 0 });

        assert!(!editor.calendar_open());
        assert_eq!(editor.datetime(), value, "closing must not change the value");
    }

    #[test]
    fn date_time_edit_disabled_state_blocks_changes() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_datetime(DateTime::new(Date::new(2026, 6, 8), Time::new(10, 0, 0, 0)));
        editor.set_enabled(false);
        assert!(!editor.is_enabled());

        // Via handle_event, disabled widgets should reject keyboard input
        editor.handle_event(&Event::KeyPress { key: 38, modifiers: 0 });
        assert_eq!(editor.datetime(), DateTime::new(Date::new(2026, 6, 8), Time::new(10, 0, 0, 0)));
    }

    #[test]
    fn date_time_edit_date_selection_updates_datetime() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        let new_date = Date::new(2027, 12, 25);
        editor.set_date(new_date);
        assert_eq!(editor.date(), new_date);
        // Time should remain the default (00:00:00)
        assert_eq!(editor.time(), Time::new(0, 0, 0, 0));
    }

    #[test]
    fn date_time_edit_time_part_updates_independently() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_datetime(DateTime::new(Date::new(2026, 6, 8), Time::new(14, 30, 0, 0)));

        // Update time part
        editor.set_time(Time::new(20, 15, 45, 0));
        assert_eq!(editor.time(), Time::new(20, 15, 45, 0));
        assert_eq!(editor.date(), Date::new(2026, 6, 8));
    }

    #[test]
    fn date_time_edit_step_up_increments_time() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_datetime(DateTime::new(Date::new(2026, 6, 8), Time::new(10, 30, 15, 0)));
        editor.step_up();
        assert_eq!(editor.time(), Time::new(10, 30, 16, 0));
        assert_eq!(editor.date(), Date::new(2026, 6, 8));
    }

    #[test]
    fn date_time_edit_step_down_decrements_time() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_datetime(DateTime::new(Date::new(2026, 6, 8), Time::new(10, 30, 15, 0)));
        editor.step_down();
        assert_eq!(editor.time(), Time::new(10, 30, 14, 0));
        assert_eq!(editor.date(), Date::new(2026, 6, 8));
    }

    #[test]
    fn date_time_edit_step_up_rolls_to_next_day() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_datetime(DateTime::new(Date::new(2026, 6, 8), Time::new(23, 59, 59, 0)));
        editor.step_up();
        // Rolls to next day 00:00:00
        assert_eq!(editor.time(), Time::new(0, 0, 0, 0));
        assert_eq!(editor.date(), Date::new(2026, 6, 9));
    }

    #[test]
    fn date_time_edit_step_down_rolls_to_prev_day() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_datetime(DateTime::new(Date::new(2026, 6, 8), Time::new(0, 0, 0, 0)));
        editor.step_down();
        // Rolls to previous day 23:59:59
        assert_eq!(editor.time(), Time::new(23, 59, 59, 0));
        assert_eq!(editor.date(), Date::new(2026, 6, 7));
    }

    #[test]
    fn date_time_edit_undo_redo_restores_datetime() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        let first = DateTime::new(Date::new(2026, 6, 8), Time::new(10, 0, 0, 0));
        let second = DateTime::new(Date::new(2026, 6, 8), Time::new(10, 0, 1, 0));
        editor.set_datetime(first);
        editor.set_datetime(second);

        assert!(editor.can_undo());
        assert!(editor.undo());
        assert_eq!(editor.datetime(), first);
        assert!(editor.can_redo());
        assert!(editor.redo());
        assert_eq!(editor.datetime(), second);
    }

    #[test]
    fn date_time_edit_keyboard_shortcuts_drive_history() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        let first = DateTime::new(Date::new(2026, 6, 8), Time::new(10, 0, 0, 0));
        let second = DateTime::new(Date::new(2026, 6, 9), Time::new(11, 0, 0, 0));
        editor.set_datetime(first);
        editor.set_datetime(second);

        editor.handle_event(&Event::KeyPress { key: 90, modifiers: primary_modifier_event_bits() });
        assert_eq!(editor.datetime(), first);
        editor.handle_event(&Event::KeyPress { key: 89, modifiers: primary_modifier_event_bits() });
        assert_eq!(editor.datetime(), second);
    }

    #[test]
    fn date_time_edit_primary_shortcut_and_shifted_chord() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        let first = DateTime::new(Date::new(2026, 6, 8), Time::new(10, 0, 0, 0));
        let second = DateTime::new(Date::new(2026, 6, 9), Time::new(11, 0, 0, 0));
        editor.set_datetime(first);
        editor.set_datetime(second);

        let primary = primary_modifier_event_bits();
        // Shift+Primary+Z is a different chord, so it is not plain undo.
        editor.handle_event(&Event::key_press(90, primary | 0b0001));
        assert_eq!(editor.datetime(), second);

        // Primary+Z / Primary+Y are undo / redo on this host.
        editor.handle_event(&Event::key_press(90, primary));
        assert_eq!(editor.datetime(), first);
        editor.handle_event(&Event::key_press(89, primary));
        assert_eq!(editor.datetime(), second);

        // The accelerator that is not primary on this host does not undo.
        editor.handle_event(&Event::key_press(90, non_primary_modifier_event_bits()));
        assert_eq!(editor.datetime(), second);
    }

    #[test]
    fn date_time_edit_keyboard_navigation() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_datetime(DateTime::new(Date::new(2026, 6, 8), Time::new(10, 0, 0, 0)));

        // Up arrow (key 38) = step_up
        editor.handle_event(&Event::KeyPress { key: 38, modifiers: 0 });
        assert_eq!(editor.time(), Time::new(10, 0, 1, 0));

        // Down arrow (key 40) = step_down
        editor.handle_event(&Event::KeyPress { key: 40, modifiers: 0 });
        assert_eq!(editor.time(), Time::new(10, 0, 0, 0));
    }

    #[test]
    fn date_time_edit_visibility_toggle() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        assert!(editor.is_visible());
        editor.set_visible(false);
        assert!(!editor.is_visible());
        editor.set_visible(true);
        assert!(editor.is_visible());
    }

    /// Sections cover every component; typing edits the active one and stepping wraps it.
    #[test]
    fn date_time_sections_edit_each_component() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_datetime(DateTime::new(Date::new(2026, 6, 15), Time::new(10, 0, 0, 0)));
        assert!(!editor.type_digit('2'), "no section, no typing");

        editor.set_current_section(Some(DateTimeSection::Hour));
        assert!(editor.type_digit('2'));
        assert!(editor.type_digit('3'));
        assert_eq!(editor.time().hour(), 23);
        editor.step_current_section(1);
        assert_eq!(editor.time().hour(), 0, "the hour wraps");
        assert_eq!(editor.date(), Date::new(2026, 6, 16), "and carries the day");

        editor.set_current_section(Some(DateTimeSection::Month));
        editor.step_current_section(1);
        assert_eq!(editor.date().month(), 7);
        editor.set_current_section(Some(DateTimeSection::Second));
        editor.step_current_section(60);
        assert_eq!(editor.time().minute(), 1, "seconds carry into minutes");
    }

    /// Spin buttons and the wheel are opt-in and step the value.
    #[test]
    fn date_time_spin_buttons_and_wheel_step_the_value() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_datetime(DateTime::new(Date::new(2026, 6, 15), Time::new(10, 0, 0, 0)));
        assert!(editor.spin_button_rects().is_none());
        editor.set_spin_buttons(true);
        let (up, _) = editor.spin_button_rects().expect("the pair exists");
        editor.handle_event(&Event::mouse_press(up.x + 2, up.y + 2, 1));
        assert_eq!(editor.time().second(), 1);

        editor.handle_event(&Event::wheel(0, 1, 0));
        assert_eq!(editor.time().second(), 2);
        editor.handle_event(&Event::wheel(0, -1, 0));
        assert_eq!(editor.time().second(), 1);
    }

    /// Clicking a zone picks its section, and the special value text is opt-in.
    #[test]
    fn date_time_zones_and_special_text() {
        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        let minute = editor.section_zone(DateTimeSection::Minute);
        editor.handle_event(&Event::mouse_press(minute.x + 1, minute.y + 2, 1));
        assert_eq!(editor.current_section(), Some(DateTimeSection::Minute));

        let before = render_to_svg(&mut editor);
        editor.set_datetime(editor.minimum_datetime());
        let at_minimum = render_to_svg(&mut editor);
        editor.set_special_value_text(Some("Unset".to_string()));
        let after = render_to_svg(&mut editor);
        assert_ne!(at_minimum, after, "the special text must replace the value at the minimum");
        assert_ne!(before, after);
    }

    /// The calendar reveal animates and settles.
    #[test]
    fn date_time_calendar_reveal_settles() {
        use crate::widget::Widget;

        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        editor.set_calendar_popup(true);
        editor.set_calendar_open(true);
        assert!(editor.is_animating());
        for _ in 0..120 {
            editor.tick(16);
        }
        assert!(!editor.is_animating());
    }

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

        let mut editor = DateTimeEdit::new(Rect::new(0, 0, 280, 30));
        let dates = Arc::new(AtomicUsize::new(0));
        let times = Arc::new(AtomicUsize::new(0));
        let fin = Arc::new(AtomicUsize::new(0));
        let (d, t, f) = (Arc::clone(&dates), Arc::clone(&times), Arc::clone(&fin));
        editor.date_changed.connect(move |_| {
            d.fetch_add(1, Ordering::SeqCst);
        });
        editor.time_changed.connect(move |_| {
            t.fetch_add(1, Ordering::SeqCst);
        });
        editor.editing_finished.connect(move || {
            f.fetch_add(1, Ordering::SeqCst);
        });

        editor.set_date(Date::new(2026, 7, 4));
        assert_eq!(dates.load(Ordering::SeqCst), 1);
        assert_eq!(times.load(Ordering::SeqCst), 0);
        editor.set_time(Time::new(12, 0, 0, 0));
        assert_eq!(times.load(Ordering::SeqCst), 1);
        editor.handle_event(&Event::key_press(13, 0));
        assert_eq!(fin.load(Ordering::SeqCst), 1);
        assert!(editor.event_signal_dyn("date_changed").is_some());
        assert!(editor.event_signal_dyn("editing_finished").is_some());
    }
}