mobiler-core 0.38.0

Mobiler runtime: the MobilerApp trait + Crux shell adapter over the fixed UI ABI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
//! Mobiler runtime — the developer-facing API.
//!
//! Implement [`MobilerApp`] with your **typed** events, model, and view (built
//! from the [builders](#functions)). Mobiler wraps it in [`MobilerShell`], a
//! Crux app speaking the fixed UI ABI ([`mobiler_ui`]); you never touch the wire
//! protocol. Device APIs are capabilities via [`Cx`].

use std::marker::PhantomData;

pub mod bunny;
pub mod dialog;
pub mod format;
pub mod http;
pub mod i18n;
pub mod transfer;
pub use dialog::{Confirm, Picker};
pub use format::{Currency, Locale, Weekday};
pub use http::{HttpHeader, HttpOutcome};
pub use i18n::{Catalog, negotiate};
pub use transfer::TransferEvent;

use crux_core::{
    App, Command,
    capability::Operation,
    macros::effect,
    render::{RenderOperation, render},
};
use facet::Facet;
use serde::{Deserialize, Serialize, de::DeserializeOwned};

pub use mobiler_ui::{
    A11yRole, Action, BoxAlign, ButtonStyle, Caption, CardStyle, ChartBracket, ChartLegendItem, ChartRefLine, ChartRegion,
    ChartSeries, ChartStyle, ChartTick, Corner, Density, Fab, FieldKind, FontFamily, Icon,
    ImageRatio, ImageShape, InputValue, MapMarker, ProjectColor, Rgb, Segment, Sheet, ShellLabels, Spacing, SwipeButton, Tab,
    TextStyle, Theme, Tone, Widget,
};

// ============================ capabilities ============================

/// Built-in capabilities the generic shell fulfils.
#[effect(facet_typegen)]
#[derive(Debug)]
pub enum Effect {
    Render(RenderOperation),
    /// Fire-and-forget plugin call (shell does not resolve).
    PluginNotify(PluginNotify),
    /// Request/response plugin call (shell resolves with a [`PluginResponse`]).
    Plugin(PluginCall),
    /// Long-lived subscription: the shell starts a native source and resolves
    /// **repeatedly** (a [`PluginResponse`] per event) until it's torn down. Powers
    /// [`Cx::subscribe`]. Stop it with [`Cx::unsubscribe`] (a `stream`/`unsubscribe`
    /// notify keyed by [`PluginStreamCall::key`]).
    PluginStream(PluginStreamCall),
}

#[derive(Facet, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct PluginNotify {
    pub plugin: String,
    pub op: String,
    pub input: String,
}
impl Operation for PluginNotify {
    type Output = ();
}

#[derive(Facet, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct PluginCall {
    pub plugin: String,
    pub op: String,
    pub input: String,
}
impl Operation for PluginCall {
    type Output = PluginResponse;
}

/// A streaming plugin subscription (powers [`Effect::PluginStream`]). Like
/// [`PluginCall`] but carries a caller-chosen `key` so the subscription can be torn
/// down ([`Cx::unsubscribe`]) — the shell registers the native source under `key`.
#[derive(Facet, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct PluginStreamCall {
    pub key: String,
    pub plugin: String,
    pub op: String,
    pub input: String,
}
impl Operation for PluginStreamCall {
    type Output = PluginResponse;
}

/// A plugin's reply. `output` is raw bytes: the HTTP capability puts a bincode
/// [`HttpOutcome`](crate::HttpOutcome) here, while most plugins put UTF-8 text (use
/// [`PluginResponse::text`] to build one and [`as_text`](Self::as_text) to read it).
///
/// Note the asymmetry with [`PluginCall`], whose `input` stays a `String`: changing
/// `output` affects only where a response is *constructed*, whereas changing `input`
/// would affect where it is *parsed* — in every plugin on every shell. Large uploads
/// pass file paths (text), so `input` stays adequate.
#[derive(Facet, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct PluginResponse {
    pub ok: bool,
    pub output: Vec<u8>,
}

impl PluginResponse {
    /// Build a response whose payload is UTF-8 text — what most plugins return.
    pub fn text(ok: bool, s: impl Into<String>) -> Self {
        Self { ok, output: s.into().into_bytes() }
    }

    /// The payload as text, or `None` if it is not valid UTF-8.
    pub fn as_text(&self) -> Option<&str> {
        std::str::from_utf8(&self.output).ok()
    }
}

type Continuation<E> = Box<dyn FnOnce(PluginResponse) -> E + Send>;
/// A streaming continuation — fires once **per event** (so `Fn`, not `FnOnce`).
type StreamContinuation<E> = Box<dyn Fn(PluginResponse) -> E + Send>;

/// Effects an app requests during `update`, generic over the app event type so
/// continuations stay fully typed.
pub struct Cx<E> {
    notifications: Vec<PluginNotify>,
    requests: Vec<(PluginCall, Continuation<E>)>,
    streams: Vec<(PluginStreamCall, StreamContinuation<E>)>,
}

impl<E> Default for Cx<E> {
    fn default() -> Self {
        Self { notifications: Vec::new(), requests: Vec::new(), streams: Vec::new() }
    }
}

impl<E> Cx<E> {
    /// Fire-and-forget call to a native plugin.
    pub fn notify(&mut self, plugin: impl Into<String>, op: impl Into<String>, input: impl Into<String>) {
        self.notifications.push(PluginNotify { plugin: plugin.into(), op: op.into(), input: input.into() });
    }

    /// Request/response call: when the plugin replies, `then(response)` produces
    /// the typed event delivered back to your `update`.
    pub fn plugin(
        &mut self,
        plugin: impl Into<String>,
        op: impl Into<String>,
        input: impl Into<String>,
        then: impl FnOnce(PluginResponse) -> E + Send + 'static,
    ) {
        self.requests
            .push((PluginCall { plugin: plugin.into(), op: op.into(), input: input.into() }, Box::new(then)));
    }

    /// Subscribe to a streaming plugin: the shell starts a native source and delivers
    /// **every** event it produces to `on_event` (which fires repeatedly, once per
    /// event), each producing a typed event into your `update`. `key` is a
    /// caller-chosen id for this subscription — pass the same `key` to
    /// [`unsubscribe`](Self::unsubscribe) to stop it. Call `subscribe` **once** per
    /// key (e.g. in [`init`](MobilerApp::init) or on a connect event); calling it
    /// again with a live key starts a second source.
    pub fn subscribe(
        &mut self,
        key: impl Into<String>,
        plugin: impl Into<String>,
        op: impl Into<String>,
        input: impl Into<String>,
        on_event: impl Fn(PluginResponse) -> E + Send + 'static,
    ) {
        self.streams.push((
            PluginStreamCall { key: key.into(), plugin: plugin.into(), op: op.into(), input: input.into() },
            Box::new(on_event),
        ));
    }

    /// Stop the streaming subscription started under `key` by [`subscribe`](Self::subscribe).
    /// The shell tears down the native source registered under `key`, so it stops
    /// producing events. No-op if `key` isn't subscribed.
    pub fn unsubscribe(&mut self, key: impl Into<String>) {
        self.notify("stream", "unsubscribe", key);
    }

    /// Upload the file at `source` (a path / `content://` / `file://` / `blob:` handle)
    /// as the raw request body. Finish with [`TransferBuilder::start`].
    pub fn upload(&mut self, url: impl Into<String>, source: impl Into<String>) -> crate::transfer::TransferBuilder<'_, E> {
        crate::transfer::TransferBuilder::upload(self, url.into(), source.into())
    }

    /// Download `url` to `dest` (a sandbox path on iOS/Android; a filename hint on web,
    /// which returns a `blob:` handle). Finish with [`TransferBuilder::start`].
    pub fn download(&mut self, url: impl Into<String>, dest: impl Into<String>) -> crate::transfer::TransferBuilder<'_, E> {
        crate::transfer::TransferBuilder::download(self, url.into(), dest.into())
    }

    /// Persist `data` (handed back to [`MobilerApp::restore`] on next startup).
    pub fn save(&mut self, data: impl Into<String>) {
        self.notify("storage", "save", data);
    }

    /// Copy `text` to the system clipboard (built-in `clipboard` capability).
    pub fn copy(&mut self, text: impl Into<String>) {
        self.notify("clipboard", "copy", text);
    }

    /// Open the system share sheet with `text` (built-in `share` capability).
    pub fn share(&mut self, text: impl Into<String>) {
        self.notify("share", "text", text);
    }

    /// Open `url` in the platform browser / default handler (built-in `browser`
    /// capability). Fire-and-forget: the app leaves the foreground.
    pub fn open_url(&mut self, url: impl Into<String>) {
        self.notify("browser", "open", url);
    }

    /// Show a transient toast / snackbar with `text` (built-in `toast` capability).
    pub fn toast(&mut self, text: impl Into<String>) {
        self.notify("toast", "show", text);
    }

    /// Fire a haptic tap (built-in `haptics` capability). `style` is `"light"`,
    /// `"medium"`, or `"heavy"`; unknown styles fall back to medium.
    pub fn haptic(&mut self, style: impl Into<String>) {
        self.notify("haptics", style, "");
    }

    /// Start an HTTP request with full control — headers, and later timeouts and
    /// query params — finished with [`RequestBuilder::send`].
    ///
    /// ```ignore
    /// cx.request("PUT", url)
    ///     .bearer(&token)
    ///     .body(json)
    ///     .send(|outcome| match outcome.status() {
    ///         Some(409) => Event::NeedsRebase,
    ///         Some(s) if outcome.is_success() => Event::Saved,
    ///         Some(s) => Event::ServerError(s),
    ///         None => Event::Offline,
    ///     });
    /// ```
    pub fn request(
        &mut self,
        method: impl Into<String>,
        url: impl Into<String>,
    ) -> crate::http::RequestBuilder<'_, E> {
        crate::http::RequestBuilder::new(self, method.into(), url.into())
    }

    /// `GET url`, delivering the outcome to `then`.
    pub fn get(&mut self, url: impl Into<String>, then: impl FnOnce(HttpOutcome) -> E + Send + 'static) {
        self.request("GET", url).send(then);
    }
    /// `POST url` with `body`, delivering the outcome to `then`.
    pub fn post(&mut self, url: impl Into<String>, body: impl Into<String>, then: impl FnOnce(HttpOutcome) -> E + Send + 'static) {
        self.request("POST", url).body(body).send(then);
    }
    /// `PUT url` with `body`, delivering the outcome to `then`.
    pub fn put(&mut self, url: impl Into<String>, body: impl Into<String>, then: impl FnOnce(HttpOutcome) -> E + Send + 'static) {
        self.request("PUT", url).body(body).send(then);
    }
    /// `PATCH url` with `body`, delivering the outcome to `then`.
    pub fn patch(&mut self, url: impl Into<String>, body: impl Into<String>, then: impl FnOnce(HttpOutcome) -> E + Send + 'static) {
        self.request("PATCH", url).body(body).send(then);
    }
    /// `DELETE url`, delivering the outcome to `then`.
    pub fn delete(&mut self, url: impl Into<String>, then: impl FnOnce(HttpOutcome) -> E + Send + 'static) {
        self.request("DELETE", url).send(then);
    }

    /// Query the device model/name via the built-in `device` capability; the result
    /// (`response.output`, e.g. "Google Pixel 7" / "Apple iPhone (iOS 18.0)") is
    /// delivered to `then`.
    pub fn device_model(&mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
        self.plugin("device", "model", "", then);
    }

    /// Query the device's preferred locale as a BCP-47 language tag (e.g. `"de-CH"`, `"en-US"`)
    /// via the built-in `device` capability; `then` receives it in `response.output`. Pair with
    /// [`Locale::from_tag`](crate::format::Locale::from_tag) to choose the app's language /
    /// formatting locale at startup. Works on iOS, Android, and web.
    pub fn device_locale(&mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
        self.plugin("device", "locale", "", then);
    }

    /// Let the user pick an image (built-in `photo` capability — the system photo
    /// picker, no permission required). `then` receives the result: on success
    /// `response.ok` is `true` and `response.output` is a local image URI you can
    /// hand straight to the `image(...)` widget; on cancel, `ok` is `false`.
    pub fn pick_photo(&mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
        self.plugin("photo", "pick", "", then);
    }

    /// Capture a photo with the device camera (built-in `camera` capability — launches
    /// the system camera). `then` receives the result: on success `response.ok` is
    /// `true` and `response.output` is a local image URI you can hand straight to the
    /// `image(...)` widget; on cancel, `ok` is `false`. iOS requires an
    /// `NSCameraUsageDescription` (the template ships one, opt-in); Android captures via
    /// the system camera app, so no extra runtime permission is needed.
    pub fn capture_photo(&mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
        self.plugin("camera", "capture", "", then);
    }

    /// Ask the user to confirm via a native dialog (built-in `dialog` capability).
    /// `then` receives the choice: `response.ok` is `true` if confirmed, `false` if
    /// cancelled/dismissed. Resolves asynchronously (the user replies whenever).
    pub fn confirm(
        &mut self,
        title: impl Into<String>,
        message: impl Into<String>,
        then: impl FnOnce(PluginResponse) -> E + Send + 'static,
    ) {
        self.confirm_with(Confirm::new(title, message), then);
    }

    /// As [`confirm`](Self::confirm), with the app's own button labels and an optional destructive
    /// style — e.g. `Confirm::new("Cancel booking?", msg).confirm_label("Cancel booking")
    /// .cancel_label("Keep it").destructive()`. Same response: `ok` is `true` only if confirmed.
    pub fn confirm_with(&mut self, dialog: Confirm, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
        self.plugin("dialog", "confirm", dialog.to_input(), then);
    }

    /// Let the user pick a date via the native date picker (built-in `datetime`
    /// capability). On success `response.ok` is `true` and `response.output` is the
    /// chosen date as an ISO `YYYY-MM-DD` string; on cancel/dismiss, `ok` is `false`.
    /// Resolves asynchronously (the user replies whenever).
    pub fn pick_date(&mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
        self.plugin("datetime", "date", "", then);
    }

    /// Let the user pick a time via the native time picker (built-in `datetime`
    /// capability). On success `response.ok` is `true` and `response.output` is the
    /// chosen time as a 24-hour `HH:MM` string; on cancel/dismiss, `ok` is `false`.
    /// Resolves asynchronously (the user replies whenever).
    pub fn pick_time(&mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
        self.plugin("datetime", "time", "", then);
    }

    /// As [`pick_date`](Self::pick_date), with the app's own title and button labels.
    pub fn pick_date_with(&mut self, picker: Picker, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
        self.plugin("datetime", "date", picker.to_input(), then);
    }

    /// As [`pick_time`](Self::pick_time), with the app's own title and button labels.
    pub fn pick_time_with(&mut self, picker: Picker, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
        self.plugin("datetime", "time", picker.to_input(), then);
    }

    /// Read the current local date-time (built-in `datetime` capability). The core is a
    /// pure state machine and can't read the clock itself, so stamping an event with "now"
    /// — a ledger entry, a log line — goes through the shell. `response.output` is the local
    /// date-time as `YYYY-MM-DD HH:MM:SS` (sortable lexicographically; the date is the first
    /// 10 chars). No UI — resolves immediately. Pair with [`pick_date`](Self::pick_date) when
    /// the user should choose a different date instead.
    pub fn now(&mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
        self.plugin("datetime", "now", "", then);
    }
}

// ============================ the app trait ============================

/// What a Mobiler app implements. Write typed domain events; Mobiler serializes
/// them into opaque tokens behind the scenes.
pub trait MobilerApp: Default {
    type Event: Serialize + DeserializeOwned + Send + 'static;
    type Model: Default;

    fn update(&self, event: Self::Event, model: &mut Self::Model, cx: &mut Cx<Self::Event>);

    fn input(&self, id: &str, value: InputValue, model: &mut Self::Model, cx: &mut Cx<Self::Event>) {
        let _ = (id, value, model, cx);
    }

    /// Restore persisted state on startup. `data` is whatever you last passed to
    /// `cx.save` (or empty if nothing was saved). Default: ignore.
    fn restore(&self, data: &str, model: &mut Self::Model) {
        let _ = (data, model);
    }

    /// Run once on startup, after [`restore`](Self::restore). The place to kick
    /// off initial effects — e.g. fetch data with `cx.get`. Default: nothing.
    fn init(&self, model: &mut Self::Model, cx: &mut Cx<Self::Event>) {
        let _ = (model, cx);
    }

    fn view(&self, model: &Self::Model) -> Widget;
}

/// Crux adapter: turns a [`MobilerApp`] into an app speaking the fixed ABI.
pub struct MobilerShell<A>(PhantomData<fn() -> A>);

impl<A> Default for MobilerShell<A> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<A: MobilerApp> App for MobilerShell<A> {
    type Event = Action;
    type Model = A::Model;
    type ViewModel = Widget;
    type Effect = Effect;

    fn update(&self, action: Action, model: &mut Self::Model) -> Command<Effect, Action> {
        let app = A::default();
        let mut cx = Cx::<A::Event>::default();
        match action {
            Action::Fired { token } => {
                if let Ok(event) = serde_json::from_str::<A::Event>(&token) {
                    app.update(event, model, &mut cx);
                }
            }
            Action::Input { id, value } => app.input(&id, value, model, &mut cx),
            Action::Restore { data } => app.restore(&data, model),
            Action::Start => app.init(model, &mut cx),
        }
        // Render first: the model is final once the app's handler returns, so the first frame
        // must show it. A shell that awaits a request before looking at later effects (the
        // Android shell once did) would otherwise show the change a full round trip late —
        // a controlled text field then reverts keystrokes. Replies re-render via their own
        // `Fired` update.
        let mut commands: Vec<Command<Effect, Action>> = vec![render()];
        for op in cx.notifications {
            commands.push(Command::notify_shell(op).build());
        }
        for (op, then) in cx.requests {
            commands.push(Command::request_from_shell(op).then_send(move |response: PluginResponse| {
                Action::Fired { token: serde_json::to_string(&then(response)).expect("serialize event") }
            }));
        }
        for (op, then) in cx.streams {
            // A long-lived shell stream: `then_send` fires `then` once per emitted
            // event (it's `Fn`), each re-entering `update` as a `Fired` action.
            commands.push(Command::stream_from_shell(op).then_send(move |response: PluginResponse| {
                Action::Fired { token: serde_json::to_string(&then(response)).expect("serialize event") }
            }));
        }
        Command::all(commands)
    }

    fn view(&self, model: &Self::Model) -> Widget {
        A::default().view(model)
    }
}

// ============================ navigation ============================

/// A navigation stack the app holds in its `Model`. The **core owns the stack**
/// (single source of truth); the framework reads its `route`/`depth` to drive
/// the shell's push/pop transitions and back button.
///
/// `R` is your screen-route type (typically a small enum). Hold it in the model,
/// mutate it in `update` (`push`/`pop`/`reset`), match `current()` in `view`, and
/// build the shell with [`nav_scaffold`]. Wire a `Msg::Back` (or similar) event to
/// `pop` so the back affordance works.
///
/// ```ignore
/// #[derive(Clone, Serialize)] enum Route { List, Detail(u32) }
/// // model.nav: Nav<Route> = Nav::new(Route::List);
/// // update: Msg::Open(id) => model.nav.push(Route::Detail(id)),
/// //         Msg::Back      => model.nav.pop(),
/// // view:   nav_scaffold(title, dark, tabs, body, &model.nav, Msg::Back)
/// ```
#[derive(Clone, Debug)]
pub struct Nav<R> {
    stack: Vec<R>,
}

impl<R: Clone + Serialize> Nav<R> {
    /// A stack containing a single root route.
    #[must_use]
    pub fn new(root: R) -> Self {
        Self { stack: vec![root] }
    }
    /// Push a new screen onto the stack.
    pub fn push(&mut self, route: R) {
        self.stack.push(route);
    }
    /// Pop the top screen (no-op at the root).
    pub fn pop(&mut self) {
        if self.stack.len() > 1 {
            self.stack.pop();
        }
    }
    /// Replace the whole stack with a fresh root (e.g. switching bottom-nav tabs).
    pub fn reset(&mut self, root: R) {
        self.stack = vec![root];
    }
    /// The current (top) route — what `view` should render.
    #[must_use]
    pub fn current(&self) -> &R {
        self.stack.last().expect("nav stack is never empty")
    }
    /// Stack depth (root = 1).
    #[must_use]
    pub fn depth(&self) -> u32 {
        self.stack.len() as u32
    }
    /// Whether there is a screen to pop back to.
    #[must_use]
    pub fn can_go_back(&self) -> bool {
        self.stack.len() > 1
    }
    /// Stable identity of the current route (its serialization), used by the shell
    /// to decide when to animate a transition.
    fn route_key(&self) -> String {
        serde_json::to_string(self.current()).expect("serialize route")
    }
}

// ============================ widget builders ============================
// Action-carrying builders take a TYPED event and serialize it into a token.

fn tok<E: Serialize>(event: E) -> String {
    serde_json::to_string(&event).expect("serialize event")
}

#[must_use]
pub fn styled(content: impl Into<String>, style: TextStyle) -> Widget {
    Widget::Text { content: content.into(), style }
}
#[must_use]
pub fn text(content: impl Into<String>) -> Widget { styled(content, TextStyle::Body) }
#[must_use]
pub fn title(content: impl Into<String>) -> Widget { styled(content, TextStyle::Title) }
#[must_use]
pub fn subtitle(content: impl Into<String>) -> Widget { styled(content, TextStyle::Subtitle) }
#[must_use]
pub fn caption(content: impl Into<String>) -> Widget { styled(content, TextStyle::Caption) }
#[must_use]
pub fn emphasis(content: impl Into<String>) -> Widget { styled(content, TextStyle::Emphasis) }

#[must_use]
pub fn image(source: impl Into<String>, shape: ImageShape, ratio: ImageRatio) -> Widget {
    Widget::Image { source: source.into(), shape, ratio }
}
#[must_use]
pub fn badge(label: impl Into<String>, tone: Tone) -> Widget {
    Widget::Badge { label: label.into(), tone }
}
/// A small colored identity dot.
#[must_use]
pub fn color_dot(color: ProjectColor) -> Widget {
    Widget::ColorDot { color }
}
#[must_use]
pub fn divider() -> Widget { Widget::Divider }
/// A progress bar (`Some(0.0..=1.0)`) or an indeterminate spinner (`None`).
#[must_use]
pub fn progress(value: Option<f32>) -> Widget { Widget::Progress { value } }
/// A shimmer placeholder shown while content loads.
#[must_use]
pub fn skeleton() -> Widget { Widget::Skeleton }
/// An in-app PDF viewer for the document at `url` (remote https URL or local file URI) — rendered
/// natively per platform (PDFKit / `PdfRenderer` / `<iframe>`). The app just supplies the URL, e.g.
/// a backend-generated report. Give it room (place in a sized container or a scroller).
#[must_use]
pub fn pdf_view(url: impl Into<String>) -> Widget { Widget::PdfView { url: url.into() } }
/// A controllable native video player for `url` (remote MP4/HLS or a local file URI), rendered with the
/// native player per platform (AVPlayer / Media3 ExoPlayer / `<video>`). `id` routes the ~once-per-second
/// position into `input(id, InputValue::Int(position_ms))`; build it fresh each render with the current
/// `playing` (play/pause) + `seek_to_ms` (the shell jumps when this CHANGES; `-1` = no seek). `on_ended`
/// fires when the clip finishes. Defaults: controls shown, not looping/muted, no poster, no resume
/// offset, no captions, rate 1.0, full volume, single clip (no playlist), PiP off — tune with
/// [`with_loop`]/[`with_muted`]/[`without_controls`]/[`with_poster`]/[`with_start_at`]/[`with_captions`]/
/// [`with_rate`]/[`with_volume`]/[`with_pip`] (or [`video_playlist`] for a queue). Give it room.
#[must_use]
pub fn video_player<E: Serialize>(id: impl Into<String>, url: impl Into<String>, playing: bool, seek_to_ms: i64, on_ended: E) -> Widget {
    Widget::Video {
        url: url.into(),
        id: id.into(),
        playing,
        seek_to_ms,
        controls: true,
        looping: false,
        muted: false,
        on_ended: Some(tok(on_ended)),
        poster: None,
        start_at_ms: -1,
        captions: Vec::new(),
        rate: 1.0,
        volume: 1.0,
        urls: Vec::new(),
        start_index: 0,
        seek_index: -1,
        allow_pip: false,
    }
}
/// A controllable native video player over a **playlist** of `urls` (auto-advances gaplessly; the
/// shell reports the current track via `input("{id}.index", InputValue::Int(i))`). `start_index` is
/// the first clip; build it fresh each render with the current `playing`. Force-jump to a track by
/// pairing this with [`with_seek_index`]. `on_ended` fires when the LAST clip finishes. Same cosmetic
/// modifiers as [`video_player`]. Empty `urls` renders nothing useful — use [`video_player`] for one clip.
#[must_use]
pub fn video_playlist<E: Serialize>(id: impl Into<String>, urls: Vec<String>, start_index: i64, playing: bool, on_ended: E) -> Widget {
    Widget::Video {
        url: urls.first().cloned().unwrap_or_default(),
        id: id.into(),
        playing,
        seek_to_ms: -1,
        controls: true,
        looping: false,
        muted: false,
        on_ended: Some(tok(on_ended)),
        poster: None,
        start_at_ms: -1,
        captions: Vec::new(),
        rate: 1.0,
        volume: 1.0,
        urls,
        start_index,
        seek_index: -1,
        allow_pip: false,
    }
}
/// Apply a mutation to a [`Widget::Video`]'s fields, passing other widgets through unchanged. Keeps
/// the `with_*` video modifiers from each having to spell out all of `Video`'s fields.
fn map_video(widget: Widget, f: impl FnOnce(&mut VideoFields)) -> Widget {
    match widget {
        Widget::Video { url, id, playing, seek_to_ms, controls, looping, muted, on_ended,
            poster, start_at_ms, captions, rate, volume, urls, start_index, seek_index, allow_pip } => {
            let mut v = VideoFields { url, id, playing, seek_to_ms, controls, looping, muted, on_ended,
                poster, start_at_ms, captions, rate, volume, urls, start_index, seek_index, allow_pip };
            f(&mut v);
            Widget::Video { url: v.url, id: v.id, playing: v.playing, seek_to_ms: v.seek_to_ms,
                controls: v.controls, looping: v.looping, muted: v.muted, on_ended: v.on_ended,
                poster: v.poster, start_at_ms: v.start_at_ms, captions: v.captions, rate: v.rate,
                volume: v.volume, urls: v.urls, start_index: v.start_index, seek_index: v.seek_index,
                allow_pip: v.allow_pip }
        }
        other => other,
    }
}
struct VideoFields {
    url: String, id: String, playing: bool, seek_to_ms: i64, controls: bool, looping: bool,
    muted: bool, on_ended: Option<String>, poster: Option<String>, start_at_ms: i64,
    captions: Vec<Caption>, rate: f32, volume: f32, urls: Vec<String>, start_index: i64,
    seek_index: i64, allow_pip: bool,
}
/// Loop a [`video_player`] (restart on end). No-op on non-Video widgets.
#[must_use]
pub fn with_loop(widget: Widget) -> Widget { map_video(widget, |v| v.looping = true) }
/// Start a [`video_player`] muted (needed for reliable autoplay). No-op on non-Video widgets.
#[must_use]
pub fn with_muted(widget: Widget) -> Widget { map_video(widget, |v| v.muted = true) }
/// Hide the native transport controls on a [`video_player`] (the app drives it). No-op otherwise.
#[must_use]
pub fn without_controls(widget: Widget) -> Widget { map_video(widget, |v| v.controls = false) }
/// Show `poster` (an image URL) before the first play / while idle. No-op on non-Video widgets.
#[must_use]
pub fn with_poster(widget: Widget, poster: impl Into<String>) -> Widget {
    let poster = poster.into();
    map_video(widget, move |v| v.poster = Some(poster))
}
/// Resume a [`video_player`] at `start_at_ms` (applied once on load). No-op on non-Video widgets.
#[must_use]
pub fn with_start_at(widget: Widget, start_at_ms: i64) -> Widget {
    map_video(widget, move |v| v.start_at_ms = start_at_ms)
}
/// Attach subtitle/caption tracks to a [`video_player`] (see [`Caption`]). No-op on non-Video widgets.
#[must_use]
pub fn with_captions(widget: Widget, captions: Vec<Caption>) -> Widget {
    map_video(widget, move |v| v.captions = captions)
}
/// Set playback speed (`1.0` = normal) on a [`video_player`]. No-op on non-Video widgets.
#[must_use]
pub fn with_rate(widget: Widget, rate: f32) -> Widget { map_video(widget, move |v| v.rate = rate) }
/// Set the volume (`0.0`–`1.0`) on a [`video_player`]. No-op on non-Video widgets.
#[must_use]
pub fn with_volume(widget: Widget, volume: f32) -> Widget {
    map_video(widget, move |v| v.volume = volume.clamp(0.0, 1.0))
}
/// Force a playlist [`video_playlist`] to jump to track `index` when this CHANGES. No-op otherwise.
#[must_use]
pub fn with_seek_index(widget: Widget, index: i64) -> Widget {
    map_video(widget, move |v| v.seek_index = index)
}
/// Enable Picture-in-Picture on a [`video_player`] (the shell adds a PiP affordance). No-op otherwise.
#[must_use]
pub fn with_pip(widget: Widget) -> Widget { map_video(widget, |v| v.allow_pip = true) }
/// A native web view showing the page / embedded player at `url` (`WKWebView` / Android `WebView` /
/// `<iframe>`). General-purpose: docs, dashboards, or a hosted player embed (e.g. a Bunny.net /
/// YouTube embed URL). NOT the default video player — use [`video_player`] for that. Give it room
/// (a sized container or a card).
#[must_use]
pub fn web_view(url: impl Into<String>) -> Widget { Widget::WebView { url: url.into() } }

/// An interactive map centered at (`center_lat`, `center_lng`) with the given `zoom` (≈ MapLibre/Google
/// zoom levels: ~2 world, ~14 city, ~17 street). iOS MapKit / Android MapLibre / web MapLibre-GL — no
/// API key. Add pins with [`with_markers`], a vector style with [`with_map_style`]. Taps arrive in
/// [`MobilerApp::input`] as `Input { id: "{id}.tap", Text("lat,lng") }` / `{ "{id}.marker", Text(id) }`.
/// Give it a height (a sized container or card).
#[must_use]
pub fn map(id: impl Into<String>, center_lat: f64, center_lng: f64, zoom: f64) -> Widget {
    Widget::Map {
        id: id.into(),
        center_lat,
        center_lng,
        zoom,
        markers: Vec::new(),
        style_url: None,
        interactive: true,
    }
}
/// Add/replace the pins on a [`map`] (no-op on any other widget).
#[must_use]
pub fn with_markers(widget: Widget, markers: Vec<MapMarker>) -> Widget {
    match widget {
        Widget::Map { id, center_lat, center_lng, zoom, style_url, interactive, .. } =>
            Widget::Map { id, center_lat, center_lng, zoom, markers, style_url, interactive },
        other => other,
    }
}
/// Set the MapLibre vector-style URL (Android + web; iOS MapKit ignores it). None → a free default.
#[must_use]
pub fn with_map_style(widget: Widget, url: impl Into<String>) -> Widget {
    match widget {
        Widget::Map { id, center_lat, center_lng, zoom, markers, interactive, .. } =>
            Widget::Map { id, center_lat, center_lng, zoom, markers, style_url: Some(url.into()), interactive },
        other => other,
    }
}
/// A map pin at (`lat`, `lng`); `id` is echoed on tap. Add a title with [`marker_titled`].
#[must_use]
pub fn marker(id: impl Into<String>, lat: f64, lng: f64) -> MapMarker {
    MapMarker { id: id.into(), lat, lng, title: None }
}
/// A titled map pin (the title shows in the marker's callout/popup).
#[must_use]
pub fn marker_titled(id: impl Into<String>, lat: f64, lng: f64, title: impl Into<String>) -> MapMarker {
    MapMarker { id: id.into(), lat, lng, title: Some(title.into()) }
}
/// A single unnamed series wrapping `values` — the back-compat shape for `bar_chart`/`line_chart`.
fn one_series(values: Vec<f32>) -> Vec<ChartSeries> {
    vec![ChartSeries { name: String::new(), values, color: None, goal: None }]
}

/// A bar chart of `values` (normalized to the max), with optional per-value `labels`.
/// Single-series, no axis or legend — for richer charts use [`chart`].
#[must_use]
pub fn bar_chart(values: Vec<f32>, labels: Vec<String>) -> Widget {
    Widget::Chart { series: one_series(values), labels, style: ChartStyle::Bar, axis: false, legend: false }
}
/// A line chart of `values` (normalized to the max), with optional per-value `labels`.
/// Single-series, no axis or legend — for richer charts use [`chart`].
#[must_use]
pub fn line_chart(values: Vec<f32>, labels: Vec<String>) -> Widget {
    Widget::Chart { series: one_series(values), labels, style: ChartStyle::Line, axis: false, legend: false }
}
/// A multi-series chart in the given `style`, with optional x-axis `labels`, y-`axis` gridlines/
/// ticks (cartesian styles), and a series `legend`. The general builder behind the convenience
/// constructors below.
#[must_use]
pub fn chart(series: Vec<ChartSeries>, labels: Vec<String>, style: ChartStyle, axis: bool, legend: bool) -> Widget {
    Widget::Chart { series, labels, style, axis, legend }
}
/// Bars stacked to a total per x-slot. Axis + legend on by default.
#[must_use]
pub fn stacked_bar_chart(series: Vec<ChartSeries>, labels: Vec<String>) -> Widget {
    chart(series, labels, ChartStyle::StackedBar, true, true)
}
/// Bars where each x-slot fills to 100% — series as proportions. Legend on, no value axis.
#[must_use]
pub fn pct_stacked_bar_chart(series: Vec<ChartSeries>, labels: Vec<String>) -> Widget {
    chart(series, labels, ChartStyle::StackedBar100, false, true)
}
/// A pie chart — each series is one wedge sized by its magnitude. Legend on.
#[must_use]
pub fn pie_chart(series: Vec<ChartSeries>) -> Widget {
    chart(series, vec![], ChartStyle::Pie, false, true)
}
/// A donut chart (pie with a center hole). Legend on.
#[must_use]
pub fn donut_chart(series: Vec<ChartSeries>) -> Widget {
    chart(series, vec![], ChartStyle::Donut, false, true)
}
/// Concentric progress rings — one per series, swept by `sum(values) / goal`. Legend on.
/// Give each series a goal via [`ChartSeries::with_goal`].
#[must_use]
pub fn rings_chart(series: Vec<ChartSeries>) -> Widget {
    chart(series, vec![], ChartStyle::Rings, false, true)
}
/// A single radial gauge — the first series' `value / goal` with the number in the center.
#[must_use]
pub fn gauge_chart(series: ChartSeries) -> Widget {
    chart(vec![series], vec![], ChartStyle::Gauge, false, false)
}

/// A variable-width stacked-region ("coverage-gap" / Marimekko) chart. `regions` are rectangles in
/// the `[0, x_max] × [0, y_max]` plane (build with [`ChartRegion::new`]); `ticks` label the
/// irregular x-axis; `ref_lines` are horizontal target/max lines ([`ChartRefLine::target`]/`::max`);
/// `legend` names the colors. Add a right-side bracket annotation with [`with_bracket`].
#[must_use]
pub fn region_chart(
    regions: Vec<ChartRegion>,
    ticks: Vec<ChartTick>,
    x_max: f32,
    y_max: f32,
    ref_lines: Vec<ChartRefLine>,
    legend: Vec<ChartLegendItem>,
) -> Widget {
    Widget::RegionChart { regions, ticks, x_max, y_max, ref_lines, bracket: None, legend }
}

/// Attach a right-side bracket annotation to a [`region_chart`] (no-op on any other widget).
#[must_use]
pub fn with_bracket(widget: Widget, bracket: ChartBracket) -> Widget {
    match widget {
        Widget::RegionChart { regions, ticks, x_max, y_max, ref_lines, legend, .. } => {
            Widget::RegionChart { regions, ticks, x_max, y_max, ref_lines, bracket: Some(bracket), legend }
        }
        other => other,
    }
}

/// Days in `month` (1–12) of `year`, leap-year aware.
fn days_in_month(year: u32, month: u8) -> u8 {
    match month {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 => if (year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400) { 29 } else { 28 },
        _ => 30,
    }
}

/// Weekday of `year-month-day` as 0=Sunday..6=Saturday (Sakamoto's algorithm).
fn weekday(year: u32, month: u8, day: u8) -> u8 {
    const T: [u32; 12] = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
    let y = if month < 3 { year - 1 } else { year };
    let m = month as usize - 1;
    ((y + y / 4 - y / 100 + y / 400 + T[m] + u32::from(day)) % 7) as u8
}

/// An inline month calendar for `year`/`month` (1–12), US English (Sunday-first). `on_day(d)`
/// builds the tap event for each day `d`; `selected` highlights a day. See [`calendar_in`] for a
/// localized calendar with per-day markers.
#[must_use]
pub fn calendar<E: Serialize>(year: u32, month: u8, selected: Option<u8>, on_day: impl Fn(u8) -> E) -> Widget {
    calendar_in(Locale::EnUs, year, month, selected, &[], on_day)
}

/// A localized inline month calendar: the title, weekday header and week start follow `locale`
/// (e.g. [`Locale::SrLatn`] → "Septembar 2026", Monday-first `P U S Č P S N`). `markers` is one
/// busy-level per day (`markers[d-1]`, `0..=3`, drawn as that many dots; `0` = none) — pass `&[]`
/// for no markers. Shorter slices pad with `0`; levels above 3 clamp to 3.
#[must_use]
pub fn calendar_in<E: Serialize>(
    locale: Locale,
    year: u32,
    month: u8,
    selected: Option<u8>,
    markers: &[u8],
    on_day: impl Fn(u8) -> E,
) -> Widget {
    let n = days_in_month(year, month);
    let start = locale.week_start().sun0();
    let weekday_labels = (0..7).map(|i| format::weekday_short(start + i, locale).to_string()).collect();
    let leading_blanks = (weekday(year, month, 1) + 7 - start) % 7;
    let markers = if markers.is_empty() {
        Vec::new()
    } else {
        (0..usize::from(n)).map(|i| markers.get(i).copied().unwrap_or(0).min(3)).collect()
    };
    Widget::Calendar {
        year,
        month,
        title: format::month_year(year, u32::from(month), locale),
        weekday_labels,
        leading_blanks,
        selected,
        on_day: (1..=n).map(|d| tok(on_day(d))).collect(),
        markers,
    }
}

/// A list row that reveals trailing `actions` (label, tone, event) on horizontal swipe; each is
/// tappable. On web the actions render inline (no gesture).
#[must_use]
pub fn swipe_action<S: Into<String>, E: Serialize>(child: Widget, actions: Vec<(S, Tone, E)>) -> Widget {
    Widget::SwipeAction {
        child: Box::new(child),
        actions: actions
            .into_iter()
            .map(|(label, tone, ev)| SwipeButton { label: label.into(), tone, on_tap: tok(ev) })
            .collect(),
    }
}
#[must_use]
pub fn spacer(size: Spacing) -> Widget { Widget::Spacer { size } }

#[must_use]
pub fn row(children: Vec<Widget>) -> Widget { Widget::Row { children } }
#[must_use]
pub fn column(children: Vec<Widget>) -> Widget { Widget::Column { children } }
#[must_use]
pub fn card(child: Widget, style: CardStyle) -> Widget {
    Widget::Card { child: Box::new(child), style, on_press: None, on_long_press: None }
}
/// A tappable card carrying a typed press event.
#[must_use]
pub fn card_button<E: Serialize>(child: Widget, style: CardStyle, on_press: E) -> Widget {
    Widget::Card { child: Box::new(child), style, on_press: Some(tok(on_press)), on_long_press: None }
}
/// Attach a long-press (press-and-hold) event to a `Card`. No-op on any other widget.
/// Combines with `card` / `card_button` — a card can carry both a tap and a long-press.
#[must_use]
pub fn with_long_press<E: Serialize>(widget: Widget, on_long_press: E) -> Widget {
    match widget {
        Widget::Card { child, style, on_press, .. } => Widget::Card {
            child,
            style,
            on_press,
            on_long_press: Some(tok(on_long_press)),
        },
        other => other,
    }
}
/// Z-stack/overlay (the `Box` widget). With `scrim`, the first child is a
/// darkened background and the rest render on top.
#[must_use]
pub fn stack(align: BoxAlign, scrim: bool, children: Vec<Widget>) -> Widget {
    Widget::Box { children, align, scrim }
}
#[must_use]
pub fn grid(children: Vec<Widget>) -> Widget { Widget::Grid { children } }
/// A two-pane master-detail layout ([`Widget::Split`]). Side-by-side on a wide screen (tablet /
/// landscape); one pane on a phone — `primary` until `show_detail` (the app sets it on selection),
/// then `detail` with a back chevron firing `on_back`. On wide, `detail` should show a placeholder
/// until a row is selected.
#[must_use]
pub fn split<E: Serialize>(primary: Widget, detail: Widget, show_detail: bool, on_back: E) -> Widget {
    Widget::Split { primary: Box::new(primary), detail: Box::new(detail), show_detail, on_back: Some(tok(on_back)) }
}
/// Horizontally scrolling row of children (a carousel / chip rail).
#[must_use]
pub fn scroller(children: Vec<Widget>) -> Widget { Widget::Scroller { children, edge_fade: false } }
/// A [`scroller`] whose trailing edge fades out — a hint that it scrolls.
#[must_use]
pub fn scroller_hinted(children: Vec<Widget>) -> Widget { Widget::Scroller { children, edge_fade: true } }
/// A circular avatar image.
#[must_use]
pub fn avatar(source: impl Into<String>) -> Widget { Widget::Avatar { source: source.into(), status: None } }
/// A circular avatar image with a colored status dot.
#[must_use]
pub fn avatar_status(source: impl Into<String>, status: Tone) -> Widget {
    Widget::Avatar { source: source.into(), status: Some(status) }
}
/// A read-only star rating. `value` is in tenths (e.g. `48` = 4.8 of `max` stars).
#[must_use]
pub fn rating(value: u32, max: u8) -> Widget { Widget::Rating { value, max, on_rate: None } }
/// A tappable star rating — `on_rate` carries one event per star (star *i* fires `on_rate[i]`).
#[must_use]
pub fn rating_input<E: Serialize>(value: u32, max: u8, on_rate: Vec<E>) -> Widget {
    Widget::Rating { value, max, on_rate: Some(on_rate.into_iter().map(tok).collect()) }
}

/// Extra options for [`button_with`]; `ButtonOpts::default()` is a plain [`button`].
///
/// ```
/// use mobiler_core::{ButtonOpts, Icon, Tone};
/// let danger_wide = ButtonOpts::default().tone(Tone::Danger).icon(Icon::Close).wide();
/// assert!(danger_wide.wide);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ButtonOpts {
    pub tone: Tone,
    pub icon: Option<Icon>,
    pub wide: bool,
}

impl Default for ButtonOpts {
    fn default() -> Self {
        Self { tone: Tone::Neutral, icon: None, wide: false }
    }
}

impl ButtonOpts {
    /// Recolor the button (`Tone::Danger` for destructive actions).
    #[must_use]
    pub const fn tone(mut self, tone: Tone) -> Self {
        self.tone = tone;
        self
    }
    /// A leading icon.
    #[must_use]
    pub const fn icon(mut self, icon: Icon) -> Self {
        self.icon = Some(icon);
        self
    }
    /// Stretch to the available width (a screen's main action).
    #[must_use]
    pub const fn wide(mut self) -> Self {
        self.wide = true;
        self
    }
}

#[must_use]
pub fn button<E: Serialize>(label: impl Into<String>, style: ButtonStyle, on_press: E) -> Widget {
    button_with(label, style, on_press, ButtonOpts::default())
}

/// A button with a [`ButtonOpts`] tone, leading icon, and/or full width.
#[must_use]
pub fn button_with<E: Serialize>(label: impl Into<String>, style: ButtonStyle, on_press: E, opts: ButtonOpts) -> Widget {
    Widget::Button { label: label.into(), style, on_press: tok(on_press), tone: opts.tone, icon: opts.icon, wide: opts.wide }
}
#[must_use]
pub fn icon_button<E: Serialize>(icon: Icon, on_press: E) -> Widget {
    Widget::IconButton { icon, on_press: tok(on_press) }
}
#[must_use]
pub fn chip<E: Serialize>(label: impl Into<String>, selected: bool, on_press: E) -> Widget {
    Widget::Chip { label: label.into(), selected, on_press: tok(on_press) }
}
#[must_use]
pub fn text_field(id: impl Into<String>, placeholder: impl Into<String>, value: impl Into<String>) -> Widget {
    Widget::TextField { id: id.into(), placeholder: placeholder.into(), value: value.into(), kind: FieldKind::Text, error: None }
}
/// A text field with full control over [`FieldKind`] and an optional inline
/// validation `error`. The kind-specific helpers below ([`secure_field`],
/// [`email_field`], …) wrap this for the common cases.
#[must_use]
pub fn field(id: impl Into<String>, placeholder: impl Into<String>, value: impl Into<String>, kind: FieldKind, error: Option<String>) -> Widget {
    Widget::TextField { id: id.into(), placeholder: placeholder.into(), value: value.into(), kind, error }
}
/// A masked password field ([`FieldKind::Secure`]).
#[must_use]
pub fn secure_field(id: impl Into<String>, placeholder: impl Into<String>, value: impl Into<String>) -> Widget {
    field(id, placeholder, value, FieldKind::Secure, None)
}
/// An email-keyboard field ([`FieldKind::Email`]).
#[must_use]
pub fn email_field(id: impl Into<String>, placeholder: impl Into<String>, value: impl Into<String>) -> Widget {
    field(id, placeholder, value, FieldKind::Email, None)
}
/// A whole-number keypad field ([`FieldKind::Number`]).
#[must_use]
pub fn number_field(id: impl Into<String>, placeholder: impl Into<String>, value: impl Into<String>) -> Widget {
    field(id, placeholder, value, FieldKind::Number, None)
}
/// A decimal keypad field ([`FieldKind::Decimal`]).
#[must_use]
pub fn decimal_field(id: impl Into<String>, placeholder: impl Into<String>, value: impl Into<String>) -> Widget {
    field(id, placeholder, value, FieldKind::Decimal, None)
}
/// A phone-keypad field ([`FieldKind::Phone`]).
#[must_use]
pub fn phone_field(id: impl Into<String>, placeholder: impl Into<String>, value: impl Into<String>) -> Widget {
    field(id, placeholder, value, FieldKind::Phone, None)
}
/// A URL-keyboard field ([`FieldKind::Url`]).
#[must_use]
pub fn url_field(id: impl Into<String>, placeholder: impl Into<String>, value: impl Into<String>) -> Widget {
    field(id, placeholder, value, FieldKind::Url, None)
}
/// A growable multi-line text area ([`FieldKind::Multiline`]).
#[must_use]
pub fn multiline_field(id: impl Into<String>, placeholder: impl Into<String>, value: impl Into<String>) -> Widget {
    field(id, placeholder, value, FieldKind::Multiline, None)
}
/// Attach an inline validation message to a [`Widget::TextField`], marking it
/// invalid. No-op on any other widget.
#[must_use]
pub fn with_error(widget: Widget, message: impl Into<String>) -> Widget {
    match widget {
        Widget::TextField { id, placeholder, value, kind, .. } =>
            Widget::TextField { id, placeholder, value, kind, error: Some(message.into()) },
        other => other,
    }
}

/// Wrap `child` so a screen reader (VoiceOver / TalkBack) announces the subtree as ONE element named
/// `label` — gives an unlabeled `icon_button`/`image` a name, or groups a card's children into one
/// announced element. Add `with_a11y_hint` / `with_a11y_role` for the activation hint + control type.
#[must_use]
pub fn a11y(child: Widget, label: impl Into<String>) -> Widget {
    Widget::A11y { child: Box::new(child), label: label.into(), hint: None, role: None }
}
/// Set the accessibility activation hint (e.g. "Opens your bookings"); wraps `widget` if it isn't an
/// [`a11y`] wrapper yet.
#[must_use]
pub fn with_a11y_hint(widget: Widget, hint: impl Into<String>) -> Widget {
    match widget {
        Widget::A11y { child, label, role, .. } =>
            Widget::A11y { child, label, hint: Some(hint.into()), role },
        other => Widget::A11y { child: Box::new(other), label: String::new(), hint: Some(hint.into()), role: None },
    }
}
/// Set the accessibility role / control type; wraps `widget` if it isn't an [`a11y`] wrapper yet.
#[must_use]
pub fn with_a11y_role(widget: Widget, role: A11yRole) -> Widget {
    match widget {
        Widget::A11y { child, label, hint, .. } =>
            Widget::A11y { child, label, hint, role: Some(role) },
        other => Widget::A11y { child: Box::new(other), label: String::new(), hint: None, role: Some(role) },
    }
}
/// A search input (leading magnifier, pill); emits `Input { id, Text }` like [`text_field`].
#[must_use]
pub fn search_field(id: impl Into<String>, placeholder: impl Into<String>, value: impl Into<String>) -> Widget {
    Widget::SearchField { id: id.into(), placeholder: placeholder.into(), value: value.into() }
}
/// One option in a [`segmented`] control, carrying a typed selection event.
#[must_use]
pub fn segment<E: Serialize>(label: impl Into<String>, selected: bool, on_select: E) -> Segment {
    Segment { label: label.into(), selected, on_select: tok(on_select) }
}
/// A single-choice segmented control (exclusive options in a pill).
#[must_use]
pub fn segmented(segments: Vec<Segment>) -> Widget {
    Widget::Segmented { segments }
}
#[must_use]
pub fn toggle(id: impl Into<String>, label: impl Into<String>, value: bool) -> Widget {
    Widget::Toggle { id: id.into(), label: label.into(), value }
}
#[must_use]
pub fn checkbox(id: impl Into<String>, label: impl Into<String>, value: bool) -> Widget {
    Widget::Checkbox { id: id.into(), label: label.into(), value }
}
#[must_use]
pub fn slider(id: impl Into<String>, value: i32, max: i32) -> Widget {
    Widget::Slider { id: id.into(), value, max }
}
#[must_use]
pub fn stepper<E: Serialize>(value: i32, on_decrement: E, on_increment: E) -> Widget {
    Widget::Stepper { value, on_decrement: tok(on_decrement), on_increment: tok(on_increment) }
}

/// A bottom-nav tab carrying a typed selection event (label-only).
#[must_use]
pub fn tab<E: Serialize>(label: impl Into<String>, selected: bool, on_select: E) -> Tab {
    Tab { label: label.into(), selected, on_select: tok(on_select), icon: None }
}

/// A bottom-nav tab with a leading icon (icon tab bar).
#[must_use]
pub fn tab_icon<E: Serialize>(label: impl Into<String>, icon: Icon, selected: bool, on_select: E) -> Tab {
    Tab { label: label.into(), selected, on_select: tok(on_select), icon: Some(icon) }
}

/// App shell: top bar + bottom-nav `tabs` + scrollable `body`. `dark_mode` is
/// theme-as-data (the shell themes the whole app from it).
#[must_use]
pub fn scaffold(title: impl Into<String>, dark_mode: bool, tabs: Vec<Tab>, body: Widget) -> Widget {
    let title = title.into();
    // route defaults to the title; root depth = 1.
    Widget::Scaffold { route: title.clone(), title, body: Box::new(body), tabs, back: None, dark_mode, theme: None, fab: None, sheet: None, on_refresh: None, refreshing: false, depth: 1, labels: None }
}

/// Like [`scaffold`], but the top bar (and the system back button) navigate back
/// via `back` — e.g. a detail screen pushed over a tab (treated as depth 2).
/// For multi-level stacks, drive navigation with [`Nav`] + [`nav_scaffold`].
#[must_use]
pub fn scaffold_back<E: Serialize>(title: impl Into<String>, dark_mode: bool, tabs: Vec<Tab>, body: Widget, back: E) -> Widget {
    let title = title.into();
    Widget::Scaffold { route: title.clone(), title, body: Box::new(body), tabs, back: Some(tok(back)), dark_mode, theme: None, fab: None, sheet: None, on_refresh: None, refreshing: false, depth: 2, labels: None }
}

/// Scaffold driven by a [`Nav`] stack: fills `route` (from the current route's
/// serialization) and `depth` (stack depth) so the shell animates transitions,
/// and shows a back affordance (top-bar arrow + system back button) firing
/// `on_back` whenever the stack can pop.
#[must_use]
pub fn nav_scaffold<R, E>(
    title: impl Into<String>,
    dark_mode: bool,
    tabs: Vec<Tab>,
    body: Widget,
    nav: &Nav<R>,
    on_back: E,
) -> Widget
where
    R: Clone + Serialize,
    E: Serialize,
{
    Widget::Scaffold {
        title: title.into(),
        body: Box::new(body),
        tabs,
        back: if nav.can_go_back() { Some(tok(on_back)) } else { None },
        dark_mode,
        theme: None,
        fab: None,
        sheet: None,
        on_refresh: None,
        refreshing: false,
        route: nav.route_key(),
        depth: nav.depth(),
        labels: None,
    }
}

/// Apply a [`Theme`] to a scaffold (brand color, corner, density, font). No-op on any
/// other widget. Lets an app brand its UI without new scaffold builder overloads:
/// `with_theme(nav_scaffold(...), Theme { seed, ..Default::default() })`.
pub fn with_theme(widget: Widget, theme: Theme) -> Widget {
    match widget {
        Widget::Scaffold { title, body, tabs, back, dark_mode, fab, sheet, on_refresh, refreshing, route, depth, labels, .. } => Widget::Scaffold {
            title,
            body,
            tabs,
            back,
            dark_mode,
            theme: Some(theme),
            fab,
            sheet,
            on_refresh,
            refreshing,
            route,
            depth,
            labels,
        },
        other => other,
    }
}

/// Anchor a floating action button over a scaffold's body (the raised primary action).
/// No-op on any other widget: `with_fab(scaffold(...), Icon::Add, Msg::New)`.
pub fn with_fab<E: Serialize>(widget: Widget, icon: Icon, on_press: E) -> Widget {
    match widget {
        Widget::Scaffold { title, body, tabs, back, dark_mode, theme, sheet, on_refresh, refreshing, route, depth, labels, .. } => Widget::Scaffold {
            title,
            body,
            tabs,
            back,
            dark_mode,
            theme,
            fab: Some(Fab { icon, on_press: tok(on_press) }),
            sheet,
            on_refresh,
            refreshing,
            route,
            depth,
            labels,
        },
        other => other,
    }
}

/// Open a modal bottom sheet over a scaffold's body. No-op on any other widget — drive it from
/// the model: `with_sheet(scaffold(...), title, sheet_body, Msg::CloseSheet)`.
pub fn with_sheet<E: Serialize>(widget: Widget, title: impl Into<String>, child: Widget, on_dismiss: E) -> Widget {
    match widget {
        Widget::Scaffold { title: t, body, tabs, back, dark_mode, theme, fab, on_refresh, refreshing, route, depth, labels, .. } => Widget::Scaffold {
            title: t,
            body,
            tabs,
            back,
            dark_mode,
            theme,
            fab,
            sheet: Some(Sheet { title: title.into(), child: Box::new(child), on_dismiss: tok(on_dismiss) }),
            on_refresh,
            refreshing,
            route,
            depth,
            labels,
        },
        other => other,
    }
}

/// Enable pull-to-refresh on a scaffold's body: the body becomes pull-refreshable and fires
/// `on_refresh` on pull. `refreshing` is app-owned — set it true when the pull fires and clear it
/// when the async reload completes (the shell shows a spinner while true). No-op on other widgets.
pub fn with_refresh<E: Serialize>(widget: Widget, refreshing: bool, on_refresh: E) -> Widget {
    match widget {
        Widget::Scaffold { title, body, tabs, back, dark_mode, theme, fab, sheet, route, depth, labels, .. } => Widget::Scaffold {
            title,
            body,
            tabs,
            back,
            dark_mode,
            theme,
            fab,
            sheet,
            on_refresh: Some(tok(on_refresh)),
            refreshing,
            route,
            depth,
            labels,
        },
        // Pull-to-refresh on a LazyList's top — same API as on a Scaffold. Leaves the load-more
        // fields intact.
        Widget::LazyList { children, on_load_more, loading, has_more, end_label, fill, .. } => Widget::LazyList {
            children,
            on_load_more,
            loading,
            has_more,
            on_refresh: Some(tok(on_refresh)),
            refreshing,
            end_label,
            fill,
        },
        other => other,
    }
}

/// Set the app-wide shell text (see [`ShellLabels`]) on a scaffold — e.g.
/// `with_labels(root, ShellLabels::new().back("Nazad").ok("U redu").cancel("Otkaži"))` also puts
/// those words on a plain `cx.confirm`. Combines with the other scaffold helpers in any order.
/// No-op on other widgets.
#[must_use]
pub fn with_labels(widget: Widget, labels: ShellLabels) -> Widget {
    match widget {
        Widget::Scaffold { title, body, tabs, back, dark_mode, theme, fab, sheet, on_refresh, refreshing, route, depth, .. } => {
            Widget::Scaffold { title, body, tabs, back, dark_mode, theme, fab, sheet, on_refresh, refreshing, route, depth, labels: Some(labels) }
        }
        other => other,
    }
}

/// A scrollable list for long/paged feeds that fires `on_load_more` when the user scrolls near the
/// end. The app owns the state: append to `children` on each load-more event, set `loading` true
/// while the page loads (the shell shows a spinner and won't re-fire), and `has_more=false` when
/// the feed is exhausted. Add pull-to-refresh at the top with [`with_refresh`]. Give it room — a
/// `LazyList` nested in a scrollable body needs a bounded height to scroll on its own.
#[must_use]
pub fn lazy_list<E: Serialize>(children: Vec<Widget>, loading: bool, has_more: bool, on_load_more: E) -> Widget {
    Widget::LazyList {
        children,
        on_load_more: Some(tok(on_load_more)),
        loading,
        has_more,
        on_refresh: None,
        refreshing: false,
        end_label: None,
        fill: false,
    }
}

/// A scrollable list with no load-more and no refresh — a plain virtualized list of `children`.
#[must_use]
pub fn lazy_list_static(children: Vec<Widget>) -> Widget {
    Widget::LazyList { children, on_load_more: None, loading: false, has_more: false, on_refresh: None, refreshing: false, end_label: None, fill: false }
}

/// Text shown at the end of an exhausted paged list (a [`lazy_list`] whose `has_more` is false) —
/// e.g. "You're all caught up", in the app's language. Without it nothing is shown there. Combines
/// with [`with_refresh`] in either order. No-op on other widgets.
#[must_use]
pub fn with_end_label(widget: Widget, label: impl Into<String>) -> Widget {
    match widget {
        Widget::LazyList { children, on_load_more, loading, has_more, on_refresh, refreshing, fill, .. } => {
            Widget::LazyList { children, on_load_more, loading, has_more, on_refresh, refreshing, end_label: Some(label.into()), fill }
        }
        other => other,
    }
}

/// Let a paged list fill the rest of the screen: when it is the scaffold body (or a direct child
/// of the body's column — the first such list), the body stops scrolling as a page and the list
/// takes the remaining height, scrolling itself. Anywhere else it keeps its normal bounded height.
/// Combines with [`with_refresh`] and [`with_end_label`] in any order. No-op on other widgets.
#[must_use]
pub fn with_fill(widget: Widget) -> Widget {
    match widget {
        Widget::LazyList { children, on_load_more, loading, has_more, on_refresh, refreshing, end_label, .. } => {
            Widget::LazyList { children, on_load_more, loading, has_more, on_refresh, refreshing, end_label, fill: true }
        }
        other => other,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::Serialize;

    #[derive(Clone, Copy, Serialize, PartialEq, Debug)]
    enum Route {
        Home,
        Detail(u32),
    }

    #[derive(Serialize)]
    enum Ev {
        Tap,
        Open(u32),
    }

    // ---- PluginResponse ----

    #[test]
    fn plugin_response_carries_bytes_and_converts_text() {
        let r = PluginResponse::text(true, "hello");
        assert!(r.ok);
        assert_eq!(r.output, b"hello".to_vec());
        assert_eq!(r.as_text(), Some("hello"));

        let binary = PluginResponse { ok: true, output: vec![0xff, 0xfe] };
        assert_eq!(binary.as_text(), None, "invalid UTF-8 must not panic");
    }

    // ---- Nav ----

    #[test]
    fn nav_push_pop_depth() {
        let mut nav = Nav::new(Route::Home);
        assert_eq!(nav.depth(), 1);
        assert!(!nav.can_go_back());

        nav.push(Route::Detail(7));
        assert_eq!(nav.depth(), 2);
        assert!(nav.can_go_back());
        assert!(matches!(nav.current(), Route::Detail(7)));

        nav.pop();
        assert_eq!(nav.depth(), 1);
        assert!(matches!(nav.current(), Route::Home));

        nav.pop(); // no-op at the root
        assert_eq!(nav.depth(), 1);
    }

    #[test]
    fn nav_reset_replaces_stack() {
        let mut nav = Nav::new(Route::Home);
        nav.push(Route::Detail(1));
        nav.push(Route::Detail(2));
        nav.reset(Route::Detail(9));
        assert_eq!(nav.depth(), 1);
        assert!(matches!(nav.current(), Route::Detail(9)));
    }

    #[test]
    fn nav_route_key_is_serialization() {
        let nav = Nav::new(Route::Detail(3));
        assert_eq!(nav.route_key(), serde_json::to_string(&Route::Detail(3)).unwrap());
    }

    // ---- builders ----

    #[test]
    fn scaffold_sets_route_depth_and_no_back() {
        match scaffold("Home", false, vec![], text("x")) {
            Widget::Scaffold { route, depth, back, dark_mode, .. } => {
                assert_eq!(route, "Home");
                assert_eq!(depth, 1);
                assert!(back.is_none());
                assert!(!dark_mode);
            }
            other => panic!("expected Scaffold, got {other:?}"),
        }
    }

    #[test]
    fn scaffold_back_is_depth_2_with_back() {
        match scaffold_back("Detail", true, vec![], text("x"), Ev::Tap) {
            Widget::Scaffold { depth, back, dark_mode, .. } => {
                assert_eq!(depth, 2);
                assert_eq!(back, Some(serde_json::to_string(&Ev::Tap).unwrap()));
                assert!(dark_mode);
            }
            other => panic!("expected Scaffold, got {other:?}"),
        }
    }

    #[test]
    fn nav_scaffold_shows_back_only_when_poppable() {
        let mut nav = Nav::new(Route::Home);
        // at the root: no back, depth 1, route = serialized current route
        match nav_scaffold("T", false, vec![], text("x"), &nav, Ev::Tap) {
            Widget::Scaffold { back, depth, route, .. } => {
                assert!(back.is_none());
                assert_eq!(depth, 1);
                assert_eq!(route, serde_json::to_string(&Route::Home).unwrap());
            }
            other => panic!("expected Scaffold, got {other:?}"),
        }
        // after a push: back present, depth 2
        nav.push(Route::Detail(2));
        match nav_scaffold("T", false, vec![], text("x"), &nav, Ev::Tap) {
            Widget::Scaffold { back, depth, .. } => {
                assert_eq!(back, Some(serde_json::to_string(&Ev::Tap).unwrap()));
                assert_eq!(depth, 2);
            }
            other => panic!("expected Scaffold, got {other:?}"),
        }
    }

    #[test]
    fn with_labels_sets_scaffold_labels_and_combinators_keep_them() {
        let l = ShellLabels::new().back("Nazad").ok("U redu");
        assert!(matches!(scaffold("T", false, vec![], text("b")), Widget::Scaffold { labels: None, .. }));
        let s = with_labels(scaffold("T", false, vec![], text("b")), l.clone());
        assert!(matches!(&s, Widget::Scaffold { labels: Some(x), .. } if *x == l));
        // Every scaffold combinator must carry labels through, in either order.
        let themed = with_theme(s.clone(), Theme { seed: Rgb::new(1, 2, 3), ..Default::default() });
        assert!(matches!(&themed, Widget::Scaffold { labels: Some(x), .. } if *x == l));
        let refreshed = with_refresh(s.clone(), false, Ev::Tap);
        assert!(matches!(&refreshed, Widget::Scaffold { labels: Some(x), .. } if *x == l));
        let fabbed = with_fab(s.clone(), Icon::Calendar, Ev::Tap);
        assert!(matches!(&fabbed, Widget::Scaffold { labels: Some(x), .. } if *x == l));
        let sheeted = with_sheet(s, "Sheet", text("c"), Ev::Tap);
        assert!(matches!(&sheeted, Widget::Scaffold { labels: Some(x), .. } if *x == l));
        // No-op elsewhere.
        assert!(matches!(with_labels(text("x"), ShellLabels::new()), Widget::Text { .. }));
        // Builder fills only what was set.
        assert_eq!(ShellLabels::new().done("Gotovo"), ShellLabels { done: Some("Gotovo".into()), ..ShellLabels::default() });
        // nav_scaffold also defaults labels to None.
        let nav = Nav::new(Route::Home);
        assert!(matches!(nav_scaffold("T", false, vec![], text("x"), &nav, Ev::Tap), Widget::Scaffold { labels: None, .. }));
    }

    #[test]
    fn button_with_carries_tone_icon_and_width() {
        assert!(matches!(
            button("Go", ButtonStyle::Filled, Ev::Tap),
            Widget::Button { style: ButtonStyle::Filled, tone: Tone::Neutral, icon: None, wide: false, .. }
        ));
        assert!(matches!(
            button_with("Cancel", ButtonStyle::Tonal, Ev::Tap, ButtonOpts::default().tone(Tone::Danger).icon(Icon::Close).wide()),
            Widget::Button { style: ButtonStyle::Tonal, tone: Tone::Danger, icon: Some(Icon::Close), wide: true, .. }
        ));
    }

    #[test]
    fn buttons_carry_serialized_event_tokens() {
        match button("Go", ButtonStyle::Filled, Ev::Open(5)) {
            Widget::Button { label, on_press, .. } => {
                assert_eq!(label, "Go");
                assert_eq!(on_press, serde_json::to_string(&Ev::Open(5)).unwrap());
            }
            other => panic!("expected Button, got {other:?}"),
        }
        match card_button(text("c"), CardStyle::Elevated, Ev::Tap) {
            Widget::Card { on_press, .. } => {
                assert_eq!(on_press, Some(serde_json::to_string(&Ev::Tap).unwrap()));
            }
            other => panic!("expected Card, got {other:?}"),
        }
        // a plain card is not tappable
        match card(text("c"), CardStyle::Elevated) {
            Widget::Card { on_press, on_long_press, .. } => {
                assert!(on_press.is_none());
                assert!(on_long_press.is_none());
            }
            other => panic!("expected Card, got {other:?}"),
        }
        // with_long_press attaches a long-press, keeping any existing tap
        match with_long_press(card_button(text("c"), CardStyle::Filled, Ev::Tap), Ev::Open(7)) {
            Widget::Card { on_press, on_long_press, .. } => {
                assert_eq!(on_press, Some(serde_json::to_string(&Ev::Tap).unwrap()));
                assert_eq!(on_long_press, Some(serde_json::to_string(&Ev::Open(7)).unwrap()));
            }
            other => panic!("expected Card, got {other:?}"),
        }
        // with_long_press on a non-Card is a no-op
        assert!(matches!(with_long_press(text("x"), Ev::Tap), Widget::Text { .. }));
    }

    // ---- Cx capabilities ----

    #[test]
    fn cx_notify_and_save_enqueue_notifications() {
        let mut cx = Cx::<Ev>::default();
        cx.notify("toast", "show", "hi");
        cx.save("blob");
        assert_eq!(cx.notifications.len(), 2);
        assert_eq!(cx.notifications[0], PluginNotify { plugin: "toast".into(), op: "show".into(), input: "hi".into() });
        assert_eq!(cx.notifications[1], PluginNotify { plugin: "storage".into(), op: "save".into(), input: "blob".into() });
        assert!(cx.requests.is_empty());
    }

    #[test]
    fn cx_http_helpers_build_requests() {
        let mut cx = Cx::<Ev>::default();
        cx.get("http://h/x", |_| Ev::Tap);
        cx.post("http://h/y", "hello", |_| Ev::Tap);
        cx.put("http://h/p", "putbody", |_| Ev::Tap);
        cx.patch("http://h/z", "patch", |_| Ev::Tap);
        cx.delete("http://h/d", |_| Ev::Tap);

        let methods: Vec<&str> = cx.requests.iter().map(|(c, _)| c.op.as_str()).collect();
        assert_eq!(methods, ["GET", "POST", "PUT", "PATCH", "DELETE"]);
        assert!(cx.requests.iter().all(|(c, _)| c.plugin == "http"));

        let get_input: serde_json::Value = serde_json::from_str(&cx.requests[0].0.input).unwrap();
        assert_eq!(get_input["url"], "http://h/x");
        assert!(get_input["body"].is_null());

        let put_input: serde_json::Value = serde_json::from_str(&cx.requests[2].0.input).unwrap();
        assert_eq!(put_input["url"], "http://h/p");
        assert_eq!(put_input["body"], "putbody");
    }

    #[test]
    fn request_builder_emits_headers_in_order() {
        let mut cx = Cx::<Ev>::default();
        cx.request("PUT", "http://h/access-key")
            .bearer("tok123")
            .header("X-Trace-Id", "abc")
            .body("{}")
            .send(|_| Ev::Tap);

        assert_eq!(cx.requests.len(), 1);
        let (call, _) = &cx.requests[0];
        assert_eq!(call.plugin, "http");
        assert_eq!(call.op, "PUT");

        let input: serde_json::Value = serde_json::from_str(&call.input).unwrap();
        assert_eq!(input["url"], "http://h/access-key");
        assert_eq!(input["body"], "{}");
        assert_eq!(input["headers"][0]["name"], "Authorization");
        assert_eq!(input["headers"][0]["value"], "Bearer tok123");
        assert_eq!(input["headers"][1]["name"], "X-Trace-Id");
        assert_eq!(input["headers"][1]["value"], "abc");
    }

    #[test]
    fn helpers_emit_no_headers_field_content() {
        let mut cx = Cx::<Ev>::default();
        cx.get("http://h/x", |_| Ev::Tap);
        let input: serde_json::Value = serde_json::from_str(&cx.requests[0].0.input).unwrap();
        assert_eq!(input["headers"].as_array().unwrap().len(), 0);
    }

    #[test]
    fn continuation_receives_decoded_outcome() {
        #[derive(Debug, PartialEq)]
        enum Got { Conflict, Offline, Other }

        let classify = |r: PluginResponse| -> Got {
            match HttpOutcome::decode(&r.output).unwrap() {
                HttpOutcome::Response { status: 409, .. } => Got::Conflict,
                HttpOutcome::TransportError { .. } => Got::Offline,
                _ => Got::Other,
            }
        };

        let conflict = HttpOutcome::Response { status: 409, headers: vec![], body: b"c".to_vec() };
        assert_eq!(classify(PluginResponse { ok: false, output: conflict.encode() }), Got::Conflict);

        let offline = HttpOutcome::TransportError { message: "refused".into() };
        assert_eq!(classify(PluginResponse { ok: false, output: offline.encode() }), Got::Offline);
    }

    #[test]
    fn decode_failure_in_continuation_surfaces_as_transport_error() {
        // Drives the actual `send()` callback path (not just `HttpOutcome::decode`
        // directly): stores a continuation via `cx.request(...).send(...)`, then
        // invokes it with a `PluginResponse` whose `output` is malformed bytes, the
        // way the shell would if it returned something undecodable.
        let mut cx = Cx::<Ev>::default();

        cx.request("GET", "http://h/x").send(|outcome| {
            match outcome {
                HttpOutcome::TransportError { message } => {
                    assert!(
                        message.contains("malformed http response"),
                        "unexpected message: {message}"
                    );
                }
                HttpOutcome::Response { .. } => {
                    panic!("garbage bytes must not decode as a Response")
                }
            }
            Ev::Tap
        });

        assert_eq!(cx.requests.len(), 1);
        let (_, continuation) = cx.requests.remove(0);
        // Must not panic: a malformed `output` has to surface as `TransportError`,
        // asserted inside the callback above.
        continuation(PluginResponse { ok: true, output: vec![0xff, 0xff, 0xff] });
    }

    #[test]
    fn cx_pick_and_capture_photo_request_the_right_plugin() {
        let mut cx = Cx::<Ev>::default();
        cx.pick_photo(|_| Ev::Tap);
        cx.capture_photo(|_| Ev::Tap);
        assert_eq!(cx.requests.len(), 2);
        // photo picker = `photo`/`pick`; camera capture = `camera`/`capture`. Both
        // carry empty input (the shell needs no parameters to launch picker/camera).
        assert_eq!((cx.requests[0].0.plugin.as_str(), cx.requests[0].0.op.as_str(), cx.requests[0].0.input.as_str()), ("photo", "pick", ""));
        assert_eq!((cx.requests[1].0.plugin.as_str(), cx.requests[1].0.op.as_str(), cx.requests[1].0.input.as_str()), ("camera", "capture", ""));
    }

    #[test]
    fn cx_capture_photo_routes_success_and_cancel() {
        // Happy path: ok=true delivers the URI to the success branch.
        let mut cx = Cx::<Ev>::default();
        cx.capture_photo(|r| if r.ok { Ev::Open(7) } else { Ev::Tap });
        let (_, then) = cx.requests.pop().unwrap();
        assert!(matches!(then(PluginResponse { ok: true, output: "file:///tmp/shot.jpg".into() }), Ev::Open(7)));

        // Sad path: ok=false (user cancelled / permission denied) takes the else branch.
        let mut cx = Cx::<Ev>::default();
        cx.capture_photo(|r| if r.ok { Ev::Open(7) } else { Ev::Tap });
        let (_, then) = cx.requests.pop().unwrap();
        assert!(matches!(then(PluginResponse { ok: false, output: Vec::new() }), Ev::Tap));
    }

    #[test]
    fn cx_notify_capabilities_map_to_the_right_plugin_and_op() {
        let mut cx = Cx::<Ev>::default();
        cx.copy("c");
        cx.share("s");
        cx.open_url("u");
        cx.toast("t");
        cx.haptic("heavy");
        let got: Vec<(&str, &str, &str)> = cx
            .notifications
            .iter()
            .map(|n| (n.plugin.as_str(), n.op.as_str(), n.input.as_str()))
            .collect();
        assert_eq!(
            got,
            vec![
                ("clipboard", "copy", "c"),
                ("share", "text", "s"),
                ("browser", "open", "u"),
                ("toast", "show", "t"),
                ("haptics", "heavy", ""), // haptic style is the op, input empty
            ]
        );
        assert!(cx.requests.is_empty());
    }

    #[test]
    fn cx_device_model_is_a_request_not_a_notification() {
        let mut cx = Cx::<Ev>::default();
        cx.device_model(|_| Ev::Tap);
        assert!(cx.notifications.is_empty());
        assert_eq!(cx.requests.len(), 1);
        let (call, _) = &cx.requests[0];
        assert_eq!((call.plugin.as_str(), call.op.as_str(), call.input.as_str()), ("device", "model", ""));
    }

    #[test]
    fn cx_device_locale_requests_the_device_locale_op() {
        let mut cx = Cx::<Ev>::default();
        cx.device_locale(|_| Ev::Tap);
        assert!(cx.notifications.is_empty());
        assert_eq!(cx.requests.len(), 1);
        let (call, _) = &cx.requests[0];
        assert_eq!((call.plugin.as_str(), call.op.as_str(), call.input.as_str()), ("device", "locale", ""));
    }

    #[test]
    fn cx_now_requests_the_datetime_now_op() {
        let mut cx = Cx::<Ev>::default();
        cx.now(|_| Ev::Tap);
        assert_eq!(cx.requests.len(), 1);
        let (call, _) = &cx.requests[0];
        assert_eq!((call.plugin.as_str(), call.op.as_str(), call.input.as_str()), ("datetime", "now", ""));
    }

    #[test]
    fn cx_subscribe_enqueues_a_keyed_stream_and_maps_each_event() {
        let mut cx = Cx::<Ev>::default();
        cx.subscribe("ws", "websocket", "stream", "wss://h/x", |r| if r.ok { Ev::Tap } else { Ev::Open(0) });
        // It's a stream, not a one-shot request or a notification.
        assert!(cx.notifications.is_empty());
        assert!(cx.requests.is_empty());
        assert_eq!(cx.streams.len(), 1);
        let (call, on_event) = &cx.streams[0];
        assert_eq!(
            (call.key.as_str(), call.plugin.as_str(), call.op.as_str(), call.input.as_str()),
            ("ws", "websocket", "stream", "wss://h/x")
        );
        // The continuation is `Fn` — it can map MANY events, not just one.
        assert!(matches!(on_event(PluginResponse { ok: true, output: "frame1".into() }), Ev::Tap));
        assert!(matches!(on_event(PluginResponse { ok: true, output: "frame2".into() }), Ev::Tap));
        assert!(matches!(on_event(PluginResponse { ok: false, output: "closed".into() }), Ev::Open(0)));
    }

    #[test]
    fn cx_unsubscribe_enqueues_the_teardown_notify_keyed_by_subscription() {
        let mut cx = Cx::<Ev>::default();
        cx.unsubscribe("ws");
        assert!(cx.streams.is_empty());
        assert_eq!(cx.notifications.len(), 1);
        // The shell tears down the native source registered under this key.
        assert_eq!(
            cx.notifications[0],
            PluginNotify { plugin: "stream".into(), op: "unsubscribe".into(), input: "ws".into() }
        );
    }

    #[test]
    fn cx_confirm_serializes_title_message_and_routes_ok() {
        let mut cx = Cx::<Ev>::default();
        cx.confirm("Delete?", "This cannot be undone.", |r| if r.ok { Ev::Tap } else { Ev::Open(0) });
        let (call, then) = cx.requests.pop().unwrap();
        assert_eq!((call.plugin.as_str(), call.op.as_str()), ("dialog", "confirm"));
        let v: serde_json::Value = serde_json::from_str(&call.input).unwrap();
        assert_eq!(v["title"], "Delete?");
        assert_eq!(v["message"], "This cannot be undone.");
        // ok=true → confirmed branch; ok=false would take the else branch.
        assert!(matches!(then(PluginResponse { ok: true, output: "ok".into() }), Ev::Tap));
    }

    #[test]
    fn cx_confirm_stays_byte_identical_on_the_wire() {
        let mut cx = Cx::<Ev>::default();
        cx.confirm("Delete?", "This cannot be undone.", |_| Ev::Tap);
        let (call, _) = cx.requests.pop().unwrap();
        assert_eq!(call.input, r#"{"title":"Delete?","message":"This cannot be undone."}"#);
    }

    #[test]
    fn cx_confirm_with_sends_labels_and_destructive() {
        let mut cx = Cx::<Ev>::default();
        cx.confirm_with(
            Confirm::new("Otkazati termin?", "Klijent dobija obaveštenje.")
                .confirm_label("Otkaži termin")
                .cancel_label("Ne, vrati se")
                .destructive(),
            |r| if r.ok { Ev::Tap } else { Ev::Open(0) },
        );
        let (call, then) = cx.requests.pop().unwrap();
        assert_eq!((call.plugin.as_str(), call.op.as_str()), ("dialog", "confirm"));
        let v: serde_json::Value = serde_json::from_str(&call.input).unwrap();
        assert_eq!(v["title"], "Otkazati termin?");
        assert_eq!(v["message"], "Klijent dobija obaveštenje.");
        assert_eq!(v["confirm_label"], "Otkaži termin");
        assert_eq!(v["cancel_label"], "Ne, vrati se");
        assert_eq!(v["destructive"], true);
        assert!(matches!(then(PluginResponse::text(true, "ok")), Ev::Tap));
        // Unset labels are omitted, not sent as null.
        let mut cx = Cx::<Ev>::default();
        cx.confirm_with(Confirm::new("T", "M").confirm_label("Go"), |_| Ev::Tap);
        let v: serde_json::Value = serde_json::from_str(&cx.requests.pop().unwrap().0.input).unwrap();
        assert!(v.get("cancel_label").is_none() && v.get("destructive").is_none());
    }

    #[test]
    fn cx_pickers_keep_empty_input_and_with_variants_send_labels() {
        let mut cx = Cx::<Ev>::default();
        cx.pick_date(|_| Ev::Tap);
        cx.pick_time(|_| Ev::Tap);
        assert!(cx.requests.iter().all(|(c, _)| c.input.is_empty()));

        let mut cx = Cx::<Ev>::default();
        cx.pick_date_with(Picker::new().title("Izaberi datum").confirm_label("Izaberi").cancel_label("Otkaži"), |_| Ev::Tap);
        cx.pick_time_with(Picker::new(), |_| Ev::Tap);
        let (date, _) = &cx.requests[0];
        assert_eq!((date.plugin.as_str(), date.op.as_str()), ("datetime", "date"));
        let v: serde_json::Value = serde_json::from_str(&date.input).unwrap();
        assert_eq!((v["title"].as_str(), v["confirm_label"].as_str(), v["cancel_label"].as_str()), (Some("Izaberi datum"), Some("Izaberi"), Some("Otkaži")));
        let (time, _) = &cx.requests[1];
        assert_eq!((time.op.as_str(), time.input.as_str()), ("time", "{}"));
    }

    // ---- widget builders ----

    #[test]
    fn text_builders_carry_their_style() {
        assert!(matches!(text("b"), Widget::Text { style: TextStyle::Body, .. }));
        assert!(matches!(title("t"), Widget::Text { style: TextStyle::Title, .. }));
        assert!(matches!(subtitle("s"), Widget::Text { style: TextStyle::Subtitle, .. }));
        assert!(matches!(caption("c"), Widget::Text { style: TextStyle::Caption, .. }));
        assert!(matches!(emphasis("e"), Widget::Text { style: TextStyle::Emphasis, .. }));
    }

    #[test]
    fn layout_and_content_builders_produce_their_variants() {
        assert!(matches!(row(vec![text("a")]), Widget::Row { children } if children.len() == 1));
        assert!(matches!(column(vec![]), Widget::Column { children } if children.is_empty()));
        assert!(matches!(grid(vec![text("a"), text("b")]), Widget::Grid { children } if children.len() == 2));
        assert!(matches!(divider(), Widget::Divider));
        assert!(matches!(bar_chart(vec![1.0, 2.0], vec![]), Widget::Chart { style: ChartStyle::Bar, series, .. } if series[0].values.len() == 2));
        assert!(matches!(line_chart(vec![1.0], vec![]), Widget::Chart { style: ChartStyle::Line, .. }));
        assert!(matches!(donut_chart(vec![ChartSeries::new("a", vec![1.0])]), Widget::Chart { style: ChartStyle::Donut, legend: true, .. }));
        assert!(matches!(gauge_chart(ChartSeries::new("g", vec![3.0]).with_goal(5.0)), Widget::Chart { style: ChartStyle::Gauge, series, .. } if series[0].goal == Some(5.0)));
        let rc = with_bracket(
            region_chart(
                vec![ChartRegion::new(0.0, 3.0, 0.0, 80.0, "80%").vertical()],
                vec![ChartTick::new(3.0, "3 Mt.")],
                65.0, 80.0,
                vec![ChartRefLine::target(80.0, "CHF 80'000"), ChartRefLine::max(90.0, "CHF 90'000")],
                vec![ChartLegendItem::new("Gap", Rgb::new(0x5A, 0x7D, 0x9A))],
            ),
            ChartBracket::new(60.0, 80.0, "Ceiling").with_info(),
        );
        assert!(matches!(rc, Widget::RegionChart { bracket: Some(b), regions, ref_lines, .. } if regions[0].vertical && ref_lines[1].dashed && b.info));
        // June 2026 has 30 days and starts on a Monday; US English is Sunday-first → 1 blank.
        assert!(matches!(
            calendar(2026, 6, Some(3), |d| Ev::Open(u32::from(d))),
            Widget::Calendar { leading_blanks: 1, selected: Some(3), ref on_day, ref title, ref markers, .. }
                if on_day.len() == 30 && title == "June 2026" && markers.is_empty()
        ));
        assert!(matches!(
            swipe_action(text("row"), vec![("Delete", Tone::Danger, Ev::Tap)]),
            Widget::SwipeAction { actions, .. } if actions.len() == 1
        ));
        // lazy_list carries the load-more token + app-owned flags; no refresh by default.
        assert!(matches!(
            lazy_list(vec![text("a"), text("b")], false, true, Ev::Tap),
            Widget::LazyList { children, on_load_more: Some(t), loading: false, has_more: true, on_refresh: None, refreshing: false, end_label: None, fill: false }
                if children.len() == 2 && t == serde_json::to_string(&Ev::Tap).unwrap()
        ));
        assert!(matches!(lazy_list_static(vec![text("a")]), Widget::LazyList { on_load_more: None, on_refresh: None, .. }));
        // with_refresh adds pull-to-refresh to a LazyList without disturbing the load-more fields.
        assert!(matches!(
            with_refresh(lazy_list(vec![text("a")], true, false, Ev::Tap), true, Ev::Open(9)),
            Widget::LazyList { on_load_more: Some(_), loading: true, has_more: false, on_refresh: Some(r), refreshing: true, .. }
                if r == serde_json::to_string(&Ev::Open(9)).unwrap()
        ));
        // with_end_label sets the end text; nothing else changes. Default is None (render nothing).
        assert!(matches!(lazy_list_static(vec![text("a")]), Widget::LazyList { end_label: None, .. }));
        assert!(matches!(
            with_end_label(lazy_list(vec![text("a")], false, false, Ev::Tap), "Kraj liste"),
            Widget::LazyList { on_load_more: Some(_), has_more: false, end_label: Some(l), .. } if l == "Kraj liste"
        ));
        // with_refresh keeps an end label set before it, and with_end_label keeps refresh.
        assert!(matches!(
            with_refresh(with_end_label(lazy_list(vec![], false, false, Ev::Tap), "End"), false, Ev::Open(1)),
            Widget::LazyList { on_refresh: Some(_), end_label: Some(l), .. } if l == "End"
        ));
        assert!(matches!(
            with_end_label(with_refresh(lazy_list(vec![], false, false, Ev::Tap), false, Ev::Open(1)), "End"),
            Widget::LazyList { on_refresh: Some(_), end_label: Some(l), .. } if l == "End"
        ));
        // No-op on other widgets.
        assert!(matches!(with_end_label(text("x"), "End"), Widget::Text { .. }));
        assert!(matches!(spacer(Spacing::Lg), Widget::Spacer { .. }));
        assert!(matches!(image("u", ImageShape::Circle, ImageRatio::Square), Widget::Image { .. }));
        assert!(matches!(badge("new", Tone::Success), Widget::Badge { .. }));
        assert!(matches!(color_dot(ProjectColor::Teal), Widget::ColorDot { .. }));
        assert!(matches!(card(text("x"), CardStyle::Filled), Widget::Card { on_press: None, .. }));
        // a scrim z-stack keeps its align + scrim flag
        assert!(matches!(stack(BoxAlign::Center, true, vec![]), Widget::Box { scrim: true, .. }));
        // split: children boxed, show_detail + on_back carried.
        assert!(matches!(split(text("list"), text("detail"), true, Ev::Tap),
            Widget::Split { show_detail: true, on_back: Some(_), .. }));
    }

    #[test]
    fn with_fill_marks_a_lazy_list_and_keeps_its_other_fields() {
        assert!(matches!(lazy_list(vec![], false, true, Ev::Tap), Widget::LazyList { fill: false, .. }));
        assert!(matches!(lazy_list_static(vec![]), Widget::LazyList { fill: false, .. }));
        let l = with_fill(with_end_label(with_refresh(lazy_list(vec![text("a")], false, true, Ev::Tap), false, Ev::Open(1)), "End"));
        assert!(matches!(&l, Widget::LazyList { fill: true, on_refresh: Some(_), end_label: Some(e), on_load_more: Some(_), .. } if e == "End"));
        // Other orders keep fill too.
        let l2 = with_end_label(with_refresh(with_fill(lazy_list(vec![], false, true, Ev::Tap)), false, Ev::Tap), "E");
        assert!(matches!(l2, Widget::LazyList { fill: true, .. }));
        assert!(matches!(with_fill(text("x")), Widget::Text { .. }));
        assert_eq!(ShellLabels::new().pdf_error("x").pdf_error, Some("x".to_string()));
    }

    #[test]
    fn input_builders_carry_ids_values_and_event_tokens() {
        assert!(matches!(text_field("id", "ph", "v"), Widget::TextField { kind: FieldKind::Text, error: None, .. }));
        assert!(matches!(pdf_view("https://x/report.pdf"), Widget::PdfView { url } if url == "https://x/report.pdf"));
        assert!(matches!(web_view("https://iframe.mediadelivery.net/embed/1/abc"), Widget::WebView { url } if url == "https://iframe.mediadelivery.net/embed/1/abc"));
        // video_player defaults + the cosmetic modifiers (match-and-rebind like with_refresh).
        assert!(matches!(video_player("v", "https://x/c.mp4", false, -1, Ev::Tap),
            Widget::Video { id, playing: false, seek_to_ms: -1, controls: true, looping: false, muted: false, on_ended: Some(_), .. } if id == "v"));
        assert!(matches!(without_controls(with_muted(with_loop(video_player("v", "u", true, 0, Ev::Tap)))),
            Widget::Video { playing: true, controls: false, looping: true, muted: true, .. }));
        // v2 defaults + modifiers.
        assert!(matches!(video_player("v", "u", false, -1, Ev::Tap),
            Widget::Video { poster: None, start_at_ms: -1, rate, volume, allow_pip: false, .. }
                if (rate - 1.0).abs() < f32::EPSILON && (volume - 1.0).abs() < f32::EPSILON));
        let tuned = with_pip(with_volume(with_rate(with_start_at(with_poster(
            with_captions(video_player("v", "u", true, -1, Ev::Tap),
                vec![Caption { url: "e.vtt".into(), label: "EN".into(), language: "en".into(), default_on: true }]),
            "p.jpg"), 9000), 1.5), 0.5));
        assert!(matches!(tuned,
            Widget::Video { poster: Some(p), start_at_ms: 9000, rate, volume, allow_pip: true, captions, .. }
                if p == "p.jpg" && (rate - 1.5).abs() < f32::EPSILON && (volume - 0.5).abs() < f32::EPSILON && captions.len() == 1));
        // playlist builder: url defaults to the first clip; urls/start_index carried; seek_index jumps.
        assert!(matches!(with_seek_index(video_playlist("pl", vec!["a.mp4".into(), "b.mp4".into()], 1, true, Ev::Tap), 0),
            Widget::Video { url, urls, start_index: 1, seek_index: 0, .. } if url == "a.mp4" && urls.len() == 2));
        // modifiers are no-ops on non-Video widgets.
        assert!(matches!(with_pip(divider()), Widget::Divider));
        assert!(matches!(secure_field("pw", "Password", ""), Widget::TextField { kind: FieldKind::Secure, .. }));
        assert!(matches!(email_field("e", "", ""), Widget::TextField { kind: FieldKind::Email, .. }));
        assert!(matches!(multiline_field("note", "", ""), Widget::TextField { kind: FieldKind::Multiline, .. }));
        assert!(matches!(with_error(email_field("e", "", "x"), "Invalid"), Widget::TextField { error: Some(m), kind: FieldKind::Email, .. } if m == "Invalid"));
        assert!(matches!(with_error(divider(), "ignored"), Widget::Divider));
        assert!(matches!(toggle("t", "l", true), Widget::Toggle { value: true, .. }));
        assert!(matches!(checkbox("c", "l", false), Widget::Checkbox { value: false, .. }));
        assert!(matches!(slider("s", 3, 10), Widget::Slider { value: 3, max: 10, .. }));

        match chip("Latte", true, Ev::Open(2)) {
            Widget::Chip { selected, on_press, .. } => {
                assert!(selected);
                assert_eq!(on_press, serde_json::to_string(&Ev::Open(2)).unwrap());
            }
            other => panic!("expected Chip, got {other:?}"),
        }
        match stepper(5, Ev::Tap, Ev::Open(1)) {
            Widget::Stepper { value, on_decrement, on_increment } => {
                assert_eq!(value, 5);
                assert_eq!(on_decrement, serde_json::to_string(&Ev::Tap).unwrap());
                assert_eq!(on_increment, serde_json::to_string(&Ev::Open(1)).unwrap());
            }
            other => panic!("expected Stepper, got {other:?}"),
        }
        let t = tab("Home", true, Ev::Tap);
        assert_eq!(t.label, "Home");
        assert!(t.selected);
        assert_eq!(t.on_select, serde_json::to_string(&Ev::Tap).unwrap());
    }

    // ---- ABI serialization round-trips (structural stability of the wire types) ----

    #[test]
    fn widget_tree_round_trips_through_serde() {
        let tree = scaffold(
            "Home",
            true,
            vec![tab("A", true, Ev::Tap)],
            column(vec![
                title("Hi"),
                row(vec![button("Go", ButtonStyle::Filled, Ev::Open(3)), chip("x", false, Ev::Tap)]),
                image("u", ImageShape::Rounded, ImageRatio::Wide),
                slider("s", 2, 5),
            ]),
        );
        let s = serde_json::to_string(&tree).unwrap();
        let back: Widget = serde_json::from_str(&s).unwrap();
        assert_eq!(s, serde_json::to_string(&back).unwrap());
    }

    #[test]
    fn actions_and_input_values_round_trip() {
        let actions = vec![
            Action::Fired { token: serde_json::to_string(&Ev::Open(1)).unwrap() },
            Action::Input { id: "n".into(), value: InputValue::Int(7) },
            Action::Input { id: "n".into(), value: InputValue::Text("hi".into()) },
            Action::Input { id: "n".into(), value: InputValue::Bool(true) },
            Action::Restore { data: "blob".into() },
            Action::Start,
        ];
        for a in actions {
            let s = serde_json::to_string(&a).unwrap();
            let back: Action = serde_json::from_str(&s).unwrap();
            assert_eq!(s, serde_json::to_string(&back).unwrap());
        }
    }

    // ---- MobilerShell: the fixed-ABI action dispatch ----

    #[derive(Default)]
    struct CounterModel {
        count: i32,
        restored: String,
        started: bool,
        last_input: String,
    }

    #[derive(serde::Serialize, serde::Deserialize)]
    enum CounterEv {
        Inc,
        Add(i32),
    }

    #[derive(Default)]
    struct CounterApp;

    impl MobilerApp for CounterApp {
        type Event = CounterEv;
        type Model = CounterModel;
        fn update(&self, ev: CounterEv, model: &mut CounterModel, _cx: &mut Cx<CounterEv>) {
            match ev {
                CounterEv::Inc => model.count += 1,
                CounterEv::Add(n) => model.count += n,
            }
        }
        fn input(&self, id: &str, value: InputValue, model: &mut CounterModel, _cx: &mut Cx<CounterEv>) {
            if let InputValue::Text(t) = value {
                model.last_input = format!("{id}={t}");
            }
        }
        fn restore(&self, data: &str, model: &mut CounterModel) {
            model.restored = data.to_string();
        }
        fn init(&self, model: &mut CounterModel, _cx: &mut Cx<CounterEv>) {
            model.started = true;
        }
        fn view(&self, model: &CounterModel) -> Widget {
            text(format!("{}", model.count))
        }
    }

    #[test]
    fn shell_dispatches_fired_input_restore_and_start() {
        use crux_core::App as _;
        let shell = MobilerShell::<CounterApp>::default();
        let mut m = CounterModel::default();

        // Fired with a valid token → the typed event reaches app.update.
        let _ = shell.update(Action::Fired { token: serde_json::to_string(&CounterEv::Add(5)).unwrap() }, &mut m);
        assert_eq!(m.count, 5);
        // Input → app.input.
        let _ = shell.update(Action::Input { id: "name".into(), value: InputValue::Text("bob".into()) }, &mut m);
        assert_eq!(m.last_input, "name=bob");
        // Restore → app.restore.
        let _ = shell.update(Action::Restore { data: "saved".into() }, &mut m);
        assert_eq!(m.restored, "saved");
        // Start → app.init.
        let _ = shell.update(Action::Start, &mut m);
        assert!(m.started);
        // view renders the (mutated) model through the ABI.
        assert!(matches!(shell.view(&m), Widget::Text { .. }));
    }

    #[test]
    fn shell_ignores_a_malformed_fired_token() {
        use crux_core::App as _;
        let shell = MobilerShell::<CounterApp>::default();
        let mut m = CounterModel::default();
        // A token that doesn't deserialize to the app's event type is dropped — no
        // panic, model untouched (the `if let Ok(event)` guard in MobilerShell::update).
        let _ = shell.update(Action::Fired { token: "not a valid token".into() }, &mut m);
        assert_eq!(m.count, 0);
    }

    // An app whose one event makes a notify, a request and a subscription, in that
    // call order — used to pin the effect order `MobilerShell::update` emits.
    #[derive(Default)]
    struct EffectsApp;

    impl MobilerApp for EffectsApp {
        type Event = CounterEv;
        type Model = CounterModel;
        fn update(&self, _ev: CounterEv, model: &mut CounterModel, cx: &mut Cx<CounterEv>) {
            model.count += 1;
            cx.plugin("http", "get", "{}", |_r| CounterEv::Inc);
            cx.notify("toast", "show", "hi");
            cx.subscribe("tick", "ticker", "start", "", |_r| CounterEv::Inc);
            cx.plugin("device", "model", "", |_r| CounterEv::Inc);
        }
        fn view(&self, model: &CounterModel) -> Widget {
            text(format!("{}", model.count))
        }
    }

    #[test]
    fn shell_renders_before_requests_notifications_and_streams() {
        use crux_core::App as _;
        let shell = MobilerShell::<EffectsApp>::default();
        let mut m = CounterModel::default();
        let mut cmd = shell.update(Action::Fired { token: serde_json::to_string(&CounterEv::Inc).unwrap() }, &mut m);
        let kinds: Vec<String> = cmd
            .effects()
            .map(|e| match e {
                Effect::Render(_) => "render".to_string(),
                Effect::PluginNotify(r) => format!("notify:{}", r.operation.plugin),
                Effect::Plugin(r) => format!("plugin:{}", r.operation.plugin),
                Effect::PluginStream(r) => format!("stream:{}", r.operation.plugin),
            })
            .collect();
        // Render first (the model is final when update returns, so the first frame shows it);
        // then notifications, requests (in call order), streams — the order shells already rely on.
        assert_eq!(kinds, ["render", "notify:toast", "plugin:http", "plugin:device", "stream:ticker"]);
    }

    // ---- transfer builders (cx.upload / cx.download) ----

    #[test]
    fn upload_builder_emits_transfer_stream_call() {
        let mut cx = Cx::<Ev>::default();
        let key = cx
            .upload("https://h/put", "file:///tmp/a.enc")
            .bearer("tok")
            .header("Content-Type", "application/octet-stream")
            .start("up-1", |_ev| Ev::Tap);

        assert_eq!(key, "up-1");
        assert_eq!(cx.streams.len(), 1);
        let (call, _) = &cx.streams[0];
        assert_eq!(call.key, "up-1");
        assert_eq!(call.plugin, "transfer");
        assert_eq!(call.op, "upload");

        let v: serde_json::Value = serde_json::from_str(&call.input).unwrap();
        assert_eq!(v["url"], "https://h/put");
        assert_eq!(v["source"], "file:///tmp/a.enc");
        assert_eq!(v["method"], "PUT"); // default
        assert_eq!(v["headers"][0]["name"], "Authorization");
        assert_eq!(v["headers"][0]["value"], "Bearer tok");
        assert_eq!(v["headers"][1]["name"], "Content-Type");
    }

    #[test]
    fn download_builder_uses_dest_and_no_default_method() {
        let mut cx = Cx::<Ev>::default();
        cx.download("https://h/get", "/data/att-9.enc").start("dl-1", |_| Ev::Tap);
        let (call, _) = &cx.streams[0];
        assert_eq!(call.op, "download");
        let v: serde_json::Value = serde_json::from_str(&call.input).unwrap();
        assert_eq!(v["dest"], "/data/att-9.enc");
        assert!(v.get("source").is_none());
    }

    #[test]
    fn start_continuation_decodes_progress_and_done() {
        use crate::http::HttpOutcome;

        // Local to this test (not module-scope `Ev`, which isn't `PartialEq`) so the
        // continuation's return type can be compared with `assert_eq!`.
        #[derive(Debug, PartialEq)]
        enum Got {
            Prog(u64),
            Done(u16),
            Bad,
        }
        #[derive(Debug, PartialEq)]
        struct GotEv(Got);

        let mut cx = Cx::<GotEv>::default();
        cx.download("https://h/get", "/d").start("k", |ev| match ev {
            TransferEvent::Progress { transferred, .. } => GotEv(Got::Prog(transferred)),
            TransferEvent::Done { outcome, .. } => GotEv(match outcome.status() {
                Some(s) => Got::Done(s),
                None => Got::Bad,
            }),
        });
        let (_, cont) = &cx.streams[0];

        let prog = TransferEvent::Progress { transferred: 512, total: Some(1024) };
        assert_eq!(cont(PluginResponse { ok: true, output: prog.encode() }), GotEv(Got::Prog(512)));

        let done = TransferEvent::Done {
            outcome: HttpOutcome::Response { status: 201, headers: vec![], body: vec![] },
            handle: Some("/d".into()),
        };
        assert_eq!(cont(PluginResponse { ok: true, output: done.encode() }), GotEv(Got::Done(201)));
    }

    #[test]
    fn calendar_in_localizes_layout_and_clamps_markers() {
        // 1 September 2026 is a Tuesday; Serbian weeks start Monday → 1 leading blank, "U" 2nd column.
        let w = calendar_in(Locale::SrLatn, 2026, 9, None, &[1, 2, 3, 9], |d| Ev::Open(u32::from(d)));
        let Widget::Calendar { title, weekday_labels, leading_blanks, on_day, markers, .. } = w else { panic!("not a calendar") };
        assert_eq!(title, "Septembar 2026");
        assert_eq!(weekday_labels, ["P", "U", "S", "Č", "P", "S", "N"]);
        assert_eq!(leading_blanks, 1);
        assert_eq!(on_day.len(), 30);
        assert_eq!(markers.len(), 30, "padded to one level per day");
        assert_eq!(&markers[..5], &[1, 2, 3, 3, 0], "clamped to 3, missing days = 0");
        // Same month, US English (Sunday-first) → 2 leading blanks.
        assert!(matches!(
            calendar_in(Locale::EnUs, 2026, 9, None, &[], |_| Ev::Tap),
            Widget::Calendar { leading_blanks: 2, ref markers, .. } if markers.is_empty()
        ));
    }

    #[test]
    fn scroller_hint_is_opt_in() {
        assert!(matches!(scroller(vec![text("a")]), Widget::Scroller { edge_fade: false, .. }));
        assert!(matches!(scroller_hinted(vec![text("a")]), Widget::Scroller { edge_fade: true, ref children } if children.len() == 1));
    }
}