stipple 0.0.3

Stipple: a cross-platform, self-drawn UI toolkit in Rust.
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
//! Stipple's self-drawn widget library.
//!
//! Every widget is composed from the `stipple::core` [`Element`] IR and drawn by
//! `oxideav-raster` — there are no native controls. The builders here read
//! their colors and metrics from a [`Theme`], so a single theme swap re-skins
//! the whole UI.
//!
//! Available widgets: structure (`column`, `row`, `grid`, `spacer`, `panel`), content
//! (`label`, `heading`, `swatch`, `divider`), and interactive controls —
//! `button_variant` (Primary/Secondary/Ghost/Danger) / `button_labeled`
//! ([`Element::on_tap`]), `text_field` / `text_editor` ([`Element::on_key`]; the
//! latter is caret-aware via [`EditBuffer`]), `checkbox`, `switch`, and `slider`
//! ([`Element::on_drag`]). All colors and metrics come from the [`Theme`], so
//! swapping or customizing the theme reskins everything.

#![forbid(unsafe_code)]

pub(crate) mod menus;
#[cfg(test)]
mod menus_tests;
mod number;
mod preedit;
mod select;
mod split;
mod toast;
mod toolbar;

pub use number::{NumberInput, number_input};
pub use preedit::DisplayText;
pub use select::{Select, select};
pub use split::{SplitPane, split_pane};
pub use toast::{Toast, ToastId, ToastKind, Toasts, toasts};
pub use toolbar::{tool_button, tool_icon_button, tool_separator, tool_toggle, toolbar};

use crate::core::{
    Align, Anchor, Axis, BoxStyle, Cx, Element, KeyInput, OverlaySpec, Role, Track, ViewportId,
};
use crate::geometry::{Insets, Point};
use crate::platform::native::{self, NativeControl};
use crate::render::{Color, ImageSource, Svg, TextDirection};
use crate::style::Theme;
use std::sync::Arc;

mod table;
mod tree;
mod virtual_list;

pub use table::{Column, SortOrder, TableSpec, TableState, table};
pub use tree::{TreeModel, TreeRow, TreeSpec, TreeState, tree};
pub use virtual_list::{
    DEFAULT_OVERSCAN, ListState, RowHeight, RowInfo, SelectionMode, VirtualList, virtual_list,
};

/// A vertical container.
/// Apply the size the OS gives `control`.
fn native_size(mut el: Element, control: NativeControl) -> Element {
    let (width, height) = native::size_hint(control);
    if let Some(width) = width {
        el = el.width(width);
    }
    if let Some(height) = height {
        el = el.height(height);
    }
    el
}

impl Element {
    /// Under the native theme, have the OS draw this text field's frame.
    fn native_text_frame(self, theme: &Theme) -> Self {
        match theme.native_deco(NativeControl::TextField) {
            Some(deco) => self.native(deco),
            None => self,
        }
    }
}

pub fn column(children: Vec<Element>) -> Element {
    Element::stack(Axis::Vertical, children)
}

/// A horizontal container.
pub fn row(children: Vec<Element>) -> Element {
    Element::stack(Axis::Horizontal, children)
}

/// A grid placing `children` in order, row by row, across `columns` tracks:
/// [`Track::px`] fixed, [`Track::fr`] fractional, or [`Track::Auto`]
/// (content-sized). Rows size to their content unless set with
/// [`Element::rows`]; cells stretch to fill by default.
///
/// ```
/// use stipple::prelude::*;
/// let form = grid(
///     vec![Track::Auto, Track::fr(1.0)],
///     vec![
///         Element::boxed(BoxStyle::default()).width(60.0).height(20.0),
///         Element::boxed(BoxStyle::default()).height(20.0),
///     ],
/// )
/// .gap(8.0);
/// # let _ = form;
/// ```
pub fn grid(columns: Vec<Track>, children: Vec<Element>) -> Element {
    Element::grid(columns, children)
}

/// `key` as a widget laid out left to right should read it: in a mirrored UI
/// (`rtl`) Left and Right swap, so they still point towards the end and the
/// start of the layout: Left expands a node in a right-to-left tree, and a
/// split divider still moves the way the arrow points.
pub(crate) fn logical_key(rtl: bool, key: &KeyInput) -> KeyInput {
    match key {
        KeyInput::Left if rtl => KeyInput::Right,
        KeyInput::Right if rtl => KeyInput::Left,
        _ => key.clone(),
    }
}

/// A flexible gap that pushes siblings apart (grow = 1).
pub fn spacer() -> Element {
    Element::stack(Axis::Horizontal, Vec::new()).grow(1.0)
}

/// A fixed-height vertical scroll viewport around `content`. Content taller than
/// `height` overflows and is clipped to the viewport; wheel events over it
/// scroll the app's offset (re-clamped each frame). Register the container once
/// per frame via `cx` so its scroll position is stable across rebuilds.
pub fn scroll<S>(cx: &mut Cx<S>, height: f64, content: Element) -> Element {
    let id = cx.register_scroll();
    Element::stack(Axis::Vertical, vec![content])
        .height(height)
        .scrollable(id)
        .align(Align::Start, Align::Stretch)
}

/// An embedded-content viewport of `width`×`height` logical pixels for the
/// given [`ViewportId`], framed with a hairline themed border. The app fills the
/// reserved rect with externally-rendered pixels (a browser page, video, or a
/// sandboxed content process's GPU surface) by locating it via
/// [`collect_viewports`](crate::core::collect_viewports); until then a neutral
/// placeholder shows. The `id` is caller-chosen so it stays stable across frames
/// and correlates with the content source.
pub fn viewport(theme: &Theme, id: ViewportId, width: f64, height: f64) -> Element {
    Element::viewport(id)
        .width(width)
        .height(height)
        .border(theme.palette.border, 1.0)
}

/// An image: a decoded bitmap (see [`Pixmap::decode`]) or an [`Svg`], shared
/// by `Arc`. Sized to the image's natural size in logical pixels; override with
/// [`width`](Element::width)/[`height`](Element::height) (one alone keeps the
/// aspect ratio) and choose placement with [`fit`](Element::fit) (default
/// [`ImageFit::Contain`]).
///
/// Keep the `Arc` in your state or a `static` rather than decoding in the view:
/// images compare by identity, so a fresh `Arc` each frame repaints each frame.
///
/// [`Pixmap::decode`]: crate::render::Pixmap::decode
/// [`ImageFit::Contain`]: crate::render::ImageFit::Contain
pub fn image(source: impl Into<ImageSource>) -> Element {
    Element::image(source)
}

/// An SVG document, rasterized at the physical size it's shown at so it stays
/// sharp at any scale. Sized like [`image`].
pub fn svg(doc: &Arc<Svg>) -> Element {
    Element::image(doc)
}

/// A raised surface card: themed fill, hairline border, rounded corners, and
/// comfortable padding. Lays its `children` vertically.
pub fn panel(theme: &Theme, children: Vec<Element>) -> Element {
    Element::stack(Axis::Vertical, children)
        .fill(theme.palette.surface)
        .radius(theme.radius)
        .border(theme.palette.border, 1.0)
        .padding(Insets::uniform(theme.spacing.lg))
        .gap(theme.spacing.md)
        // Stretch children across the card width so rows and dividers fill it.
        .align(Align::Start, Align::Stretch)
}

/// A primary action surface (the visual basis of an icon-only button). Fixed
/// size; for a text button use [`button_labeled`].
pub fn button(theme: &Theme) -> Element {
    Element::boxed(BoxStyle {
        fill: Some(theme.palette.primary),
        radius: theme.radius,
        border: None,
    })
    .width(96.0)
    .height(36.0)
}

/// A single line of text in the theme's default text color and body size.
pub fn label(theme: &Theme, text: impl Into<String>) -> Element {
    Element::text(text, theme.typography.body, theme.palette.text)
}

/// A larger heading in the theme's heading size.
pub fn heading(theme: &Theme, text: impl Into<String>) -> Element {
    Element::text(text, theme.typography.heading, theme.palette.text)
}

/// A block of body text that word-wraps to its container width across as many
/// lines as needed. Place it in a cross-stretch container (like [`panel`]) so it
/// takes the full width.
pub fn paragraph(theme: &Theme, text: impl Into<String>) -> Element {
    Element::text(text, theme.typography.body, theme.palette.text).wrap()
}

/// Visual emphasis level for a button.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Variant {
    /// Filled accent — the primary action.
    Primary,
    /// Outlined surface — a secondary action.
    Secondary,
    /// Text-only, no fill — a low-emphasis action.
    Ghost,
    /// Filled danger color — a destructive action.
    Danger,
}

/// A text button styled per [`Variant`], reading colors from the theme.
pub fn button_variant(theme: &Theme, label: impl Into<String>, variant: Variant) -> Element {
    let p = &theme.palette;
    // The OS's primary and regular buttons; it has no equivalent of the others.
    let control = match variant {
        Variant::Primary => Some(NativeControl::Button { primary: true }),
        Variant::Secondary => Some(NativeControl::Button { primary: false }),
        Variant::Ghost | Variant::Danger => None,
    };
    if let Some(control) = control
        && let Some(deco) = theme.native_deco(control)
    {
        let ink = if matches!(variant, Variant::Primary) && native::primary_label_is_on_accent() {
            p.on_primary
        } else {
            p.text
        };
        let el = Element::stack(
            Axis::Horizontal,
            vec![Element::text(label, theme.typography.body, ink)],
        )
        .fill(p.surface)
        .border(p.border, 1.0)
        .radius(theme.radius)
        .padding(Insets::symmetric(theme.spacing.lg, 0.0))
        .align(Align::Center, Align::Center)
        .native(deco);
        return native_size(el, control);
    }
    let (fill, ink, border): (Option<Color>, Color, Option<Color>) = match variant {
        Variant::Primary => (Some(p.primary), p.on_primary, None),
        Variant::Secondary => (Some(p.surface), p.text, Some(p.border)),
        Variant::Ghost => (None, p.primary, None),
        Variant::Danger => (Some(p.danger), p.danger.on_color(), None),
    };
    let mut el = Element::stack(
        Axis::Horizontal,
        vec![Element::text(label, theme.typography.body, ink)],
    )
    .radius(theme.radius)
    .padding(Insets::symmetric(theme.spacing.lg, theme.spacing.sm))
    .align(Align::Center, Align::Center);
    if let Some(fill) = fill {
        el = el.fill(fill);
    }
    if let Some(border) = border {
        el = el.border(border, 1.0);
    }
    el
}

/// A primary button with a centered text `label` (shorthand for
/// [`button_variant`] with [`Variant::Primary`]). Sizes to the label plus
/// padding (intrinsic sizing via the active font's measurement).
pub fn button_labeled(theme: &Theme, label: impl Into<String>) -> Element {
    button_variant(theme, label, Variant::Primary)
}

/// Apply a [`KeyInput`] to an editable string: append committed text, or
/// remove the last character on backspace. Navigation keys are ignored (no
/// caret model). For caret-aware editing use an [`EditBuffer`] with
/// [`text_editor`] instead.
pub fn edit_string(value: &mut String, input: &KeyInput) {
    match input {
        KeyInput::Text(t) => value.push_str(t),
        KeyInput::Backspace => {
            value.pop();
        }
        _ => {}
    }
}

/// A single-line text buffer with a caret, for in-place editing.
///
/// The caret is a byte index into the text, always kept on a UTF-8 char
/// boundary. Text is inserted and deleted at the caret; the arrow / Home / End
/// keys move it. Drive it by feeding [`KeyInput`]s to [`EditBuffer::apply`], and
/// render it (with a positioned caret) via [`text_editor`].
///
/// Edits are recorded for [`undo`](Self::undo) / [`redo`](Self::redo).
/// Consecutive typing (and consecutive Backspace or Delete presses) coalesce
/// into one undo step; a caret move, a selection change, or a different kind of
/// edit ends the step. Equality compares the text, caret and selection only,
/// not the undo history.
#[derive(Clone, Debug, Default)]
pub struct EditBuffer {
    text: String,
    caret: usize,
    /// The fixed end of an active selection (the caret is the moving end);
    /// `None` when nothing is selected.
    anchor: Option<usize>,
    history: History,
    /// The paragraph direction, for drawing and for which way Left and Right
    /// move the caret.
    direction: TextDirection,
    /// Input-method composition shown at the caret (not part of `text`).
    preedit: Option<preedit::Preedit>,
}

impl PartialEq for EditBuffer {
    fn eq(&self, other: &Self) -> bool {
        self.text == other.text
            && self.caret == other.caret
            && self.anchor == other.anchor
            && self.preedit == other.preedit
    }
}

impl Eq for EditBuffer {}

/// The most undo steps an [`EditBuffer`] keeps; older steps are dropped.
const UNDO_LIMIT: usize = 1000;

/// What kind of edit an undo step records, deciding whether the next edit may
/// join it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum EditKind {
    /// Typed text: consecutive typing coalesces.
    Typing,
    /// Backspace: consecutive presses coalesce.
    Backspace,
    /// Delete: consecutive presses coalesce.
    Delete,
    /// Anything else (paste, cut, newline, word delete, typing over a
    /// selection): always its own step.
    Other,
}

/// One primitive replacement: at byte `at`, `removed` was replaced by
/// `inserted`.
#[derive(Clone, Debug)]
struct Edit {
    at: usize,
    removed: String,
    inserted: String,
}

/// One undo step: a run of primitive edits plus the caret/anchor before and
/// after, so undo and redo restore the selection too.
#[derive(Clone, Debug)]
struct UndoStep {
    kind: EditKind,
    edits: Vec<Edit>,
    before: (usize, Option<usize>),
    after: (usize, Option<usize>),
}

/// The undo and redo stacks of an [`EditBuffer`].
#[derive(Clone, Debug, Default)]
struct History {
    undo: Vec<UndoStep>,
    redo: Vec<UndoStep>,
    /// Whether the top undo step may still absorb the next edit of its kind.
    /// Cleared by caret moves, selection changes, and undo/redo.
    open: bool,
}

/// A character class for word-wise movement and double-click selection.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CharClass {
    /// Line break: never part of a double-click selection.
    Newline,
    /// Other whitespace.
    Space,
    /// Letters and digits (any script) and `_`.
    Word,
    /// Everything else (punctuation, symbols).
    Punct,
}

fn char_class(c: char) -> CharClass {
    if c == '\n' {
        CharClass::Newline
    } else if c.is_whitespace() {
        CharClass::Space
    } else if c.is_alphanumeric() || c == '_' {
        CharClass::Word
    } else {
        CharClass::Punct
    }
}

fn is_blank(class: CharClass) -> bool {
    matches!(class, CharClass::Newline | CharClass::Space)
}

impl EditBuffer {
    /// An empty buffer with the caret at the start.
    pub fn new() -> Self {
        Self::default()
    }

    /// A buffer holding `text`, caret placed at the end.
    pub fn from_text(text: impl Into<String>) -> Self {
        let text = text.into();
        let caret = text.len();
        Self {
            text,
            caret,
            anchor: None,
            history: History::default(),
            direction: TextDirection::Auto,
            preedit: None,
        }
    }

    /// This buffer with its paragraph direction set (see
    /// [`set_direction`](Self::set_direction)).
    pub fn with_direction(mut self, direction: TextDirection) -> Self {
        self.direction = direction;
        self
    }

    /// Set the paragraph direction: [`TextDirection::Auto`] (the default) takes
    /// each line's direction from its first strong character. [`text_editor`]
    /// and [`text_area`] draw the text with it, and Left and Right move the
    /// caret visually along the line it gives.
    pub fn set_direction(&mut self, direction: TextDirection) {
        self.direction = direction;
    }

    /// The paragraph direction.
    pub fn direction(&self) -> TextDirection {
        self.direction
    }

    /// The current text.
    pub fn text(&self) -> &str {
        &self.text
    }

    /// The caret position as a byte index into [`text`](Self::text).
    pub fn caret(&self) -> usize {
        self.caret
    }

    /// `true` if there is no text.
    pub fn is_empty(&self) -> bool {
        self.text.is_empty()
    }

    /// The selected byte range `[start, end)`, or `None` when nothing is
    /// selected (the anchor is unset or collapsed onto the caret).
    pub fn selection(&self) -> Option<(usize, usize)> {
        match self.anchor {
            Some(a) if a != self.caret => Some((a.min(self.caret), a.max(self.caret))),
            _ => None,
        }
    }

    /// Replace `range` with `s`, leave the caret after the insertion with no
    /// selection, and record the change for undo as an edit of `kind`.
    fn replace(&mut self, range: std::ops::Range<usize>, s: &str, kind: EditKind) {
        if range.is_empty() && s.is_empty() {
            return;
        }
        let before = (self.caret, self.anchor);
        let removed = self.text[range.clone()].to_string();
        self.text.replace_range(range.clone(), s);
        self.caret = range.start + s.len();
        self.anchor = None;
        let edit = Edit {
            at: range.start,
            removed,
            inserted: s.to_string(),
        };
        let after = (self.caret, None);
        let h = &mut self.history;
        h.redo.clear();
        match h.undo.last_mut() {
            Some(step) if h.open && kind != EditKind::Other && step.kind == kind => {
                step.edits.push(edit);
                step.after = after;
            }
            _ => {
                h.undo.push(UndoStep {
                    kind,
                    edits: vec![edit],
                    before,
                    after,
                });
                if h.undo.len() > UNDO_LIMIT {
                    h.undo.remove(0);
                }
            }
        }
        h.open = kind != EditKind::Other;
    }

    /// End the current undo step, so the next edit starts a new one. Called on
    /// every caret move and selection change.
    fn seal(&mut self) {
        self.history.open = false;
    }

    /// Delete the active selection (if any) as its own undo step, leaving the
    /// caret at its start. Returns `true` if something was removed.
    fn delete_selection(&mut self) -> bool {
        if let Some((s, e)) = self.selection() {
            self.replace(s..e, "", EditKind::Other);
            true
        } else {
            self.anchor = None;
            false
        }
    }

    /// Insert `s` as typed text, replacing the selection first, and advance the
    /// caret past it. Consecutive typing coalesces into one undo step.
    pub fn insert(&mut self, s: &str) {
        self.insert_as(s, EditKind::Typing);
    }

    /// Insert `s` at the caret (replacing any selection) as an edit of `kind`.
    /// Typing over a selection is always its own undo step.
    fn insert_as(&mut self, s: &str, kind: EditKind) {
        let (range, kind) = match self.selection() {
            Some((a, b)) => (a..b, EditKind::Other),
            None => (self.caret..self.caret, kind),
        };
        self.replace(range, s, kind);
    }

    /// Delete the selection, or the character before the caret (Backspace).
    pub fn backspace(&mut self) {
        if self.delete_selection() {
            return;
        }
        if let Some(prev) = self.prev_boundary(self.caret) {
            self.replace(prev..self.caret, "", EditKind::Backspace);
        }
    }

    /// Delete the selection, or the character at the caret (Delete).
    pub fn delete(&mut self) {
        if self.delete_selection() {
            return;
        }
        if let Some(next) = self.next_boundary(self.caret) {
            self.replace(self.caret..next, "", EditKind::Delete);
        }
    }

    /// Delete the selection, or from the previous word start to the caret
    /// (Ctrl+Backspace; Alt+Backspace on macOS).
    pub fn delete_word_backward(&mut self) {
        if self.delete_selection() {
            return;
        }
        let start = self.word_left_of(self.caret);
        self.replace(start..self.caret, "", EditKind::Other);
    }

    /// Delete the selection, or from the caret to the next word end
    /// (Ctrl+Delete; Alt+Delete on macOS).
    pub fn delete_word_forward(&mut self) {
        if self.delete_selection() {
            return;
        }
        let end = self.word_right_of(self.caret);
        self.replace(self.caret..end, "", EditKind::Other);
    }

    /// `true` if there is an edit to [`undo`](Self::undo).
    pub fn can_undo(&self) -> bool {
        !self.history.undo.is_empty()
    }

    /// `true` if there is an undone edit to [`redo`](Self::redo).
    pub fn can_redo(&self) -> bool {
        !self.history.redo.is_empty()
    }

    /// Revert the last undo step, restoring the text, caret and selection as
    /// they were before it. Returns `false` if there was nothing to undo.
    pub fn undo(&mut self) -> bool {
        let Some(step) = self.history.undo.pop() else {
            return false;
        };
        for e in step.edits.iter().rev() {
            self.text
                .replace_range(e.at..e.at + e.inserted.len(), &e.removed);
        }
        (self.caret, self.anchor) = step.before;
        self.history.redo.push(step);
        self.seal();
        true
    }

    /// Re-apply the last undone step. Returns `false` if there was nothing to
    /// redo. Any new edit clears the redo stack.
    pub fn redo(&mut self) -> bool {
        let Some(step) = self.history.redo.pop() else {
            return false;
        };
        for e in &step.edits {
            self.text
                .replace_range(e.at..e.at + e.removed.len(), &e.inserted);
        }
        (self.caret, self.anchor) = step.after;
        self.history.undo.push(step);
        self.seal();
        true
    }

    /// Forget all undo and redo steps (e.g. after loading a new document).
    pub fn clear_history(&mut self) {
        self.history = History::default();
    }

    /// The class of the char starting at byte `i`, or `None` at the end.
    fn class_after(&self, i: usize) -> Option<CharClass> {
        self.text[i..].chars().next().map(char_class)
    }

    /// The class of the char ending at byte `i`, or `None` at the start.
    fn class_before(&self, i: usize) -> Option<CharClass> {
        self.text[..i].chars().next_back().map(char_class)
    }

    /// The start of the word at or before `i`: skip whitespace backwards, then
    /// the run of same-class chars (a word, or a run of punctuation).
    fn word_left_of(&self, mut i: usize) -> usize {
        while self.class_before(i).is_some_and(is_blank) {
            i = self.prev_boundary(i).unwrap_or(0);
        }
        if let Some(class) = self.class_before(i) {
            while self.class_before(i) == Some(class) {
                i = self.prev_boundary(i).unwrap_or(0);
            }
        }
        i
    }

    /// The end of the word at or after `i`: skip whitespace forwards, then the
    /// run of same-class chars.
    fn word_right_of(&self, mut i: usize) -> usize {
        while self.class_after(i).is_some_and(is_blank) {
            i = self.next_boundary(i).unwrap_or(self.text.len());
        }
        if let Some(class) = self.class_after(i) {
            while self.class_after(i) == Some(class) {
                i = self.next_boundary(i).unwrap_or(self.text.len());
            }
        }
        i
    }

    /// The byte range `[start, end)` of the word (or run of punctuation, or of
    /// spaces) at byte `i`. A word touching `i` on either side wins over
    /// punctuation, and punctuation over spaces, so a click just past a word's
    /// last letter still picks the word. Never spans a line break; empty when
    /// `i` sits between two line breaks.
    pub fn word_range_at(&self, i: usize) -> (usize, usize) {
        let i = self.snap(i);
        let sides = [self.class_after(i), self.class_before(i)];
        let Some(class) = [CharClass::Word, CharClass::Punct, CharClass::Space]
            .into_iter()
            .find(|c| sides.contains(&Some(*c)))
        else {
            return (i, i);
        };
        let mut start = i;
        while self.class_before(start) == Some(class) {
            start = self.prev_boundary(start).unwrap_or(0);
        }
        let mut end = i;
        while self.class_after(end) == Some(class) {
            end = self.next_boundary(end).unwrap_or(self.text.len());
        }
        (start, end)
    }

    /// Move the caret to the previous word start, clearing the selection.
    pub fn word_left(&mut self) {
        self.caret = self.word_left_of(self.caret);
        self.anchor = None;
        self.seal();
    }

    /// Move the caret to the next word end, clearing the selection.
    pub fn word_right(&mut self) {
        self.caret = self.word_right_of(self.caret);
        self.anchor = None;
        self.seal();
    }

    /// Extend the selection to the previous word start.
    pub fn select_word_left(&mut self) {
        self.extend(|b| b.caret = b.word_left_of(b.caret));
    }

    /// Extend the selection to the next word end.
    pub fn select_word_right(&mut self) {
        self.extend(|b| b.caret = b.word_right_of(b.caret));
    }

    /// Select the word at the caret (see [`word_range_at`](Self::word_range_at)),
    /// as a double-click does.
    pub fn select_word(&mut self) {
        let (s, e) = self.word_range_at(self.caret);
        self.anchor = Some(s);
        self.caret = e;
        self.seal();
    }

    /// Select the line containing the caret (without its line break), as a
    /// triple-click does.
    pub fn select_line(&mut self) {
        self.anchor = Some(self.line_start(self.caret));
        self.caret = self.line_end(self.caret);
        self.seal();
    }

    /// Move the caret one character left on screen, collapsing any selection
    /// to its start (its end on a right-to-left line).
    ///
    /// The movement is visual: inside right-to-left text, Left moves forward
    /// through the text. See [`visual_neighbor`](Self::visual_neighbor).
    pub fn move_left(&mut self) {
        self.move_visually(false);
    }

    /// Move the caret one character right on screen, collapsing any selection
    /// to its end (its start on a right-to-left line).
    pub fn move_right(&mut self) {
        self.move_visually(true);
    }

    fn move_visually(&mut self, right: bool) {
        if let Some((s, e)) = self.selection() {
            let rtl = self.line_is_rtl(self.caret);
            self.caret = if right != rtl { e } else { s };
        } else if let Some(next) = self.visual_neighbor(self.caret, right) {
            self.caret = next;
        }
        self.anchor = None;
        self.seal();
    }

    /// Whether the line holding byte `i` runs right to left.
    fn line_is_rtl(&self, i: usize) -> bool {
        let line = &self.text[self.line_start(i)..self.line_end(i)];
        self.direction.is_rtl_for(line)
    }

    /// The caret position one character left (`right == false`) or right of
    /// byte `i` on screen, or `None` when there's nowhere to go.
    ///
    /// Each line is run through the Unicode Bidirectional Algorithm and the
    /// caret steps between the visual edges of its characters, so it moves
    /// left to right across mixed text even where the logical order jumps.
    /// At the start or end of a line it crosses to the previous or next line.
    pub fn visual_neighbor(&self, i: usize, right: bool) -> Option<usize> {
        let (ls, le) = (self.line_start(i), self.line_end(i));
        let line = &self.text[ls..le];
        if let Some(b) = crate::render::bidi::visual_step_in(line, i - ls, self.direction, right) {
            return Some(ls + b);
        }
        // At the line's visual edge: Right goes forward in a left-to-right
        // line and back in a right-to-left one, onto the neighboring line.
        let forward = right != self.direction.is_rtl_for(line);
        if forward && i == le {
            self.next_boundary(i)
        } else if !forward && i == ls {
            self.prev_boundary(i)
        } else {
            None
        }
    }

    /// Move the caret to the start of the current line, clearing the selection.
    pub fn home(&mut self) {
        self.caret = self.line_start(self.caret);
        self.anchor = None;
        self.seal();
    }

    /// Move the caret to the end of the current line, clearing the selection.
    pub fn end(&mut self) {
        self.caret = self.line_end(self.caret);
        self.anchor = None;
        self.seal();
    }

    /// Move the caret up one line (same byte column), clearing the selection.
    pub fn up(&mut self) {
        self.caret = self.caret_up(self.caret);
        self.anchor = None;
        self.seal();
    }

    /// Move the caret down one line (same byte column), clearing the selection.
    pub fn down(&mut self) {
        self.caret = self.caret_down(self.caret);
        self.anchor = None;
        self.seal();
    }

    /// Byte index of the start of the line containing `byte` (after the
    /// preceding `\n`, or 0).
    fn line_start(&self, byte: usize) -> usize {
        self.text[..byte].rfind('\n').map(|i| i + 1).unwrap_or(0)
    }

    /// Byte index of the end of the line containing `byte` (before the next
    /// `\n`, or the text end).
    fn line_end(&self, byte: usize) -> usize {
        self.text[byte..]
            .find('\n')
            .map(|i| byte + i)
            .unwrap_or(self.text.len())
    }

    /// The caret position one line above `byte`, keeping the byte column.
    fn caret_up(&self, byte: usize) -> usize {
        let start = self.line_start(byte);
        if start == 0 {
            return 0; // already on the first line
        }
        let col = byte - start;
        let prev_start = self.line_start(start - 1);
        let prev_end = start - 1; // the '\n' ending the previous line
        self.snap((prev_start + col).min(prev_end))
    }

    /// The caret position one line below `byte`, keeping the byte column.
    fn caret_down(&self, byte: usize) -> usize {
        let end = self.line_end(byte);
        if end == self.text.len() {
            return self.text.len(); // already on the last line
        }
        let col = byte - self.line_start(byte);
        let next_start = end + 1;
        let next_end = self.line_end(next_start);
        self.snap((next_start + col).min(next_end))
    }

    /// Begin or extend a selection: anchor at the current caret if not already
    /// selecting, then run `motion` to move the caret (the moving end).
    fn extend(&mut self, motion: impl FnOnce(&mut Self)) {
        if self.anchor.is_none() {
            self.anchor = Some(self.caret);
        }
        let anchor = self.anchor;
        motion(self);
        self.anchor = anchor; // motion's `move_*` clears it; restore.
        self.seal();
    }

    /// Extend the selection one character left on screen.
    pub fn select_left(&mut self) {
        self.extend(|b| {
            if let Some(prev) = b.visual_neighbor(b.caret, false) {
                b.caret = prev;
            }
        });
    }

    /// Extend the selection one character right on screen.
    pub fn select_right(&mut self) {
        self.extend(|b| {
            if let Some(next) = b.visual_neighbor(b.caret, true) {
                b.caret = next;
            }
        });
    }

    /// Extend the selection to the start of the current line.
    pub fn select_home(&mut self) {
        self.extend(|b| b.caret = b.line_start(b.caret));
    }

    /// Extend the selection to the end of the current line.
    pub fn select_end(&mut self) {
        self.extend(|b| b.caret = b.line_end(b.caret));
    }

    /// Select the entire text.
    pub fn select_all(&mut self) {
        self.anchor = Some(0);
        self.caret = self.text.len();
        self.seal();
    }

    /// Place the caret at byte index `i` (snapped to a char boundary) and clear
    /// any selection. For click-to-position from a pointer hit-test.
    pub fn place_caret(&mut self, i: usize) {
        self.caret = self.snap(i);
        self.anchor = None;
        self.seal();
    }

    /// Move the caret (the selection's moving end) to byte index `i`, anchoring
    /// the selection at the prior caret if not already selecting. For
    /// drag-to-select from a pointer hit-test.
    pub fn extend_to(&mut self, i: usize) {
        if self.anchor.is_none() {
            self.anchor = Some(self.caret);
        }
        self.caret = self.snap(i);
        self.seal();
    }

    /// Snap a byte index to the nearest char boundary at or below it, clamped to
    /// the text length.
    fn snap(&self, i: usize) -> usize {
        let mut i = i.min(self.text.len());
        while !self.text.is_char_boundary(i) {
            i -= 1;
        }
        i
    }

    /// Apply a [`KeyInput`]: insert/replace text, delete, or move/extend the
    /// caret. Enter/Escape are ignored (single-line).
    pub fn apply(&mut self, input: &KeyInput) {
        match input {
            KeyInput::Text(t) => self.commit_text(t),
            KeyInput::Preedit { text, cursor } => self.set_preedit(text, *cursor),
            KeyInput::DeleteSurrounding { before, after } => {
                self.delete_surrounding(*before, *after)
            }
            KeyInput::Backspace => self.backspace(),
            KeyInput::Delete => self.delete(),
            KeyInput::Left => self.move_left(),
            KeyInput::Right => self.move_right(),
            KeyInput::Up => self.up(),
            KeyInput::Down => self.down(),
            KeyInput::Home => self.home(),
            KeyInput::End => self.end(),
            KeyInput::SelectLeft => self.select_left(),
            KeyInput::SelectRight => self.select_right(),
            KeyInput::SelectUp => self.extend(|b| b.caret = b.caret_up(b.caret)),
            KeyInput::SelectDown => self.extend(|b| b.caret = b.caret_down(b.caret)),
            KeyInput::SelectHome => self.select_home(),
            KeyInput::SelectEnd => self.select_end(),
            KeyInput::SelectAll => self.select_all(),
            KeyInput::WordLeft => self.word_left(),
            KeyInput::WordRight => self.word_right(),
            KeyInput::SelectWordLeft => self.select_word_left(),
            KeyInput::SelectWordRight => self.select_word_right(),
            KeyInput::DeleteWordBackward => self.delete_word_backward(),
            KeyInput::DeleteWordForward => self.delete_word_forward(),
            KeyInput::SelectWord => self.select_word(),
            KeyInput::SelectLine => self.select_line(),
            KeyInput::Undo => {
                self.undo();
            }
            KeyInput::Redo => {
                self.redo();
            }
            // Clipboard: copy/cut write the selection to the process clipboard
            // mirror (the app syncs it to the OS); paste inserts the mirror text.
            KeyInput::Copy => {
                if let Some(sel) = self.selected_text() {
                    crate::core::set_clipboard_text(&sel);
                }
            }
            KeyInput::Cut => {
                if let Some(sel) = self.selected_text() {
                    crate::core::set_clipboard_text(&sel);
                    self.delete_selection();
                }
            }
            KeyInput::Paste => {
                let text = crate::core::clipboard_text();
                if !text.is_empty() {
                    self.insert_as(&text, EditKind::Other);
                }
            }
            // Enter inserts a newline (the buffer is multi-line capable), as
            // its own undo step.
            KeyInput::Enter => self.insert_as("\n", EditKind::Other),
            KeyInput::Escape => {}
        }
    }

    /// The currently selected text, or `None` if there is no selection.
    pub fn selected_text(&self) -> Option<String> {
        self.selection().map(|(s, e)| self.text[s..e].to_string())
    }

    /// The char boundary strictly before `i`, or `None` at the start.
    fn prev_boundary(&self, i: usize) -> Option<usize> {
        if i == 0 {
            return None;
        }
        let mut p = i - 1;
        while !self.text.is_char_boundary(p) {
            p -= 1;
        }
        Some(p)
    }

    /// The char boundary strictly after `i`, or `None` at the end.
    fn next_boundary(&self, i: usize) -> Option<usize> {
        if i >= self.text.len() {
            return None;
        }
        let mut n = i + 1;
        while n < self.text.len() && !self.text.is_char_boundary(n) {
            n += 1;
        }
        Some(n)
    }
}

/// A single-line editable text field showing `value` and routing keyboard
/// input to `on_key` while focused (pair with [`edit_string`]). Default width
/// 200 logical px; override with `.width(..)` on the returned element.
pub fn text_field<S>(
    cx: &mut Cx<S>,
    theme: &Theme,
    value: &str,
    on_key: impl FnMut(&mut S, &KeyInput) + 'static,
) -> Element {
    // A leading space keeps an empty field from collapsing to zero height.
    let shown = if value.is_empty() {
        String::from(" ")
    } else {
        value.to_string()
    };
    Element::stack(
        Axis::Horizontal,
        vec![Element::text(shown, theme.font_size, theme.palette.text)],
    )
    .fill(theme.palette.surface)
    .radius(theme.radius)
    .border(theme.palette.border, 1.0)
    .padding(Insets::symmetric(theme.spacing.md, theme.spacing.sm))
    .align(Align::Start, Align::Center)
    .width(200.0)
    .native_text_frame(theme)
    .cursor(crate::platform::CursorIcon::Text)
    .on_key(cx, on_key)
}

/// A single-line editable field backed by an [`EditBuffer`], rendering the text
/// with the caret and selection at the buffer's positions (shown while focused).
///
/// `field` is an accessor that returns the `&mut EditBuffer` inside the app
/// state; the field wires both keyboard input (`EditBuffer::apply`) and pointer
/// interaction (click to place the caret, drag to select) to it. Default width
/// 200 logical px; override with `.width(..)` on the returned element.
///
/// ```
/// # use stipple::widgets::{text_editor, EditBuffer};
/// # use stipple::core::runtime::Cx;
/// # use stipple::style::Theme;
/// struct App { name: EditBuffer }
/// let theme = Theme::light();
/// let mut cx = Cx::new(&theme);
/// let state = App { name: EditBuffer::from_text("hi") };
/// let field = text_editor(&mut cx, &theme, &state.name, |s: &mut App| &mut s.name);
/// assert!(field.focus.is_some() && field.text_pos.is_some());
/// ```
pub fn text_editor<S>(
    cx: &mut Cx<S>,
    theme: &Theme,
    buffer: &EditBuffer,
    field: impl Fn(&mut S) -> &mut EditBuffer + Copy + 'static,
) -> Element {
    let shown = buffer.display();
    // The text fills the field so right-to-left lines can sit at its right edge.
    let text = Element::text(placeholder(shown.text), theme.font_size, theme.palette.text)
        .direction(buffer.direction())
        .grow(1.0)
        .caret(shown.caret)
        .selection(shown.selection)
        .preedit(shown.preedit);
    Element::stack(Axis::Horizontal, vec![text])
        .fill(theme.palette.surface)
        .radius(theme.radius)
        .border(theme.palette.border, 1.0)
        .padding(Insets::symmetric(theme.spacing.md, theme.spacing.sm))
        .align(Align::Start, Align::Center)
        .width(200.0)
        .native_text_frame(theme)
        .on_key(cx, move |s, k| match k {
            // Single-line: a triple-click selects the whole field.
            KeyInput::SelectLine => field(s).select_all(),
            _ => field(s).apply(k),
        })
        .on_text_pos(cx, move |s, index, extend| {
            let b = field(s);
            let index = b.display_to_text(index);
            if extend {
                b.extend_to(index);
            } else {
                b.place_caret(index);
            }
        })
}

/// The text a field shows: a lone space for an empty one, which keeps it from
/// collapsing to zero height (the caret then sits at index 0 of it).
fn placeholder(text: String) -> String {
    if text.is_empty() {
        String::from(" ")
    } else {
        text
    }
}

/// A multi-line editable text area backed by an [`EditBuffer`]: the same
/// keyboard + pointer wiring as [`text_editor`], but top-aligned so wrapped and
/// `\n`-separated lines stack from the top. Enter inserts a newline, and
/// Up/Down move between lines. Default size 280×120 logical px; override with
/// `.width(..)`/`.height(..)`.
pub fn text_area<S>(
    cx: &mut Cx<S>,
    theme: &Theme,
    buffer: &EditBuffer,
    field: impl Fn(&mut S) -> &mut EditBuffer + Copy + 'static,
) -> Element {
    let shown = buffer.display();
    // The text fills the field so right-to-left lines can sit at its right edge.
    let text = Element::text(placeholder(shown.text), theme.font_size, theme.palette.text)
        .direction(buffer.direction())
        .grow(1.0)
        .caret(shown.caret)
        .selection(shown.selection)
        .preedit(shown.preedit);
    Element::stack(Axis::Horizontal, vec![text])
        .fill(theme.palette.surface)
        .radius(theme.radius)
        .border(theme.palette.border, 1.0)
        .padding(Insets::symmetric(theme.spacing.md, theme.spacing.sm))
        .align(Align::Start, Align::Start)
        .width(280.0)
        .height(120.0)
        .native_text_frame(theme)
        .on_key(cx, move |s, k| field(s).apply(k))
        .on_text_pos(cx, move |s, index, extend| {
            let b = field(s);
            let index = b.display_to_text(index);
            if extend {
                b.extend_to(index);
            } else {
                b.place_caret(index);
            }
        })
}

/// A toggleable checkbox: a small square showing a check mark when `checked`,
/// calling `on_toggle` when tapped.
pub fn checkbox<S>(
    cx: &mut Cx<S>,
    theme: &Theme,
    checked: bool,
    on_toggle: impl FnMut(&mut S) + 'static,
) -> Element {
    let control = NativeControl::Checkbox { checked };
    if let Some(deco) = theme.native_deco(control) {
        return native_size(Element::boxed(BoxStyle::default()).native(deco), control)
            .on_tap(cx, on_toggle)
            .checked(checked);
    }
    let mark = if checked {
        vec![Element::text(
            "✓",
            theme.font_size,
            theme.palette.on_primary,
        )]
    } else {
        Vec::new()
    };
    Element::stack(Axis::Horizontal, mark)
        .fill(if checked {
            theme.palette.primary
        } else {
            theme.palette.surface
        })
        .border(theme.palette.border, 1.0)
        .radius(theme.radius / 2.0)
        .width(22.0)
        .height(22.0)
        .align(Align::Center, Align::Center)
        .on_tap(cx, on_toggle)
        .checked(checked)
        // Not named by its check mark; the label next to it names it.
        .a11y_label("")
}

/// An on/off switch: a pill track with a knob that sits right when `on`
/// (track tinted with the primary color) and left when off. Tapping calls
/// `on_toggle`.
pub fn switch<S>(
    cx: &mut Cx<S>,
    theme: &Theme,
    on: bool,
    on_toggle: impl FnMut(&mut S) + 'static,
) -> Element {
    let control = NativeControl::Switch { on };
    if let Some(deco) = theme.native_deco(control) {
        return native_size(Element::boxed(BoxStyle::default()).native(deco), control)
            .on_tap(cx, on_toggle)
            .role(Role::Switch)
            .checked(on);
    }
    let knob = Element::boxed(BoxStyle {
        fill: Some(Color::WHITE),
        radius: 8.0,
        border: None,
    })
    .width(16.0)
    .height(16.0);
    let children = if on {
        vec![spacer(), knob]
    } else {
        vec![knob, spacer()]
    };
    Element::stack(Axis::Horizontal, children)
        .fill(if on {
            theme.palette.primary
        } else {
            theme.palette.border
        })
        .radius(11.0)
        .width(40.0)
        .height(22.0)
        .padding(Insets::uniform(3.0))
        .align(Align::Start, Align::Center)
        .on_tap(cx, on_toggle)
        .role(Role::Switch)
        .checked(on)
}

/// A horizontal slider with `value` in 0..=1. Dragging anywhere on the track
/// calls `on_change` with the new fractional position. Default width 160 px;
/// override with `.width(..)`.
pub fn slider<S>(
    cx: &mut Cx<S>,
    theme: &Theme,
    value: f64,
    on_change: impl FnMut(&mut S, f64) + 'static,
) -> Element {
    let v = value.clamp(0.0, 1.0);
    let control = NativeControl::Slider {
        value: (v * 1000.0).round() as u16,
    };
    if let Some(deco) = theme.native_deco(control) {
        return native_size(Element::boxed(BoxStyle::default()).native(deco), control)
            .on_drag(cx, on_change);
    }
    let knob = Element::boxed(BoxStyle {
        fill: Some(theme.palette.primary),
        radius: 8.0,
        border: None,
    })
    .width(16.0)
    .height(16.0);
    Element::stack(
        Axis::Horizontal,
        vec![spacer().grow(v), knob, spacer().grow(1.0 - v)],
    )
    .fill(theme.palette.border)
    .radius(11.0)
    .width(160.0)
    .height(22.0)
    .padding(Insets::symmetric(3.0, 3.0))
    .align(Align::Start, Align::Center)
    .on_drag(cx, on_change)
}

/// A 1px themed divider line spanning the cross axis.
pub fn divider(theme: &Theme) -> Element {
    Element::boxed(BoxStyle {
        fill: Some(theme.palette.border),
        radius: 0.0,
        border: None,
    })
    .height(1.0)
}

/// A small square color sample with rounded corners.
pub fn swatch(color: Color, size: f64, radius: f64) -> Element {
    Element::boxed(BoxStyle {
        fill: Some(color),
        radius,
        border: None,
    })
    .width(size)
    .height(size)
}

/// A horizontal row with a leading swatch, a flexible spacer, and a trailing
/// button — a compact "setting row" used to show real composition.
pub fn setting_row(theme: &Theme, accent: Color, action: impl Into<String>) -> Element {
    row(vec![
        swatch(accent, 24.0, theme.radius / 2.0),
        spacer(),
        button_variant(theme, action, Variant::Secondary),
    ])
    .gap(theme.spacing.md)
    .align(Align::Start, Align::Center)
    .height(40.0)
}

/// A radio button: a circle filled with the primary color when `selected`.
/// Tapping calls `on_select`.
pub fn radio<S>(
    cx: &mut Cx<S>,
    theme: &Theme,
    selected: bool,
    on_select: impl FnMut(&mut S) + 'static,
) -> Element {
    let control = NativeControl::Radio { selected };
    if let Some(deco) = theme.native_deco(control) {
        return native_size(Element::boxed(BoxStyle::default()).native(deco), control)
            .on_tap(cx, on_select);
    }
    let dot = if selected {
        vec![
            Element::boxed(BoxStyle {
                fill: Some(theme.palette.primary),
                radius: 5.0,
                border: None,
            })
            .width(10.0)
            .height(10.0),
        ]
    } else {
        Vec::new()
    };
    Element::stack(Axis::Horizontal, dot)
        .fill(theme.palette.surface)
        .border(theme.palette.border, 1.0)
        .radius(11.0) // half the size → a circle
        .width(22.0)
        .height(22.0)
        .align(Align::Center, Align::Center)
        .on_tap(cx, on_select)
}

/// A horizontal tab strip (segmented control): one cell per label, the
/// `selected` one filled with the accent color and the rest on the surface.
/// Tapping a tab runs `on_select` with that tab's index. Stateless — store the
/// selected index in your app state and render the matching content yourself.
///
/// `on_select` must be `Clone` because it is stamped onto every tab (each with
/// its own index); a plain closure capturing `Copy`/`Clone` data satisfies this.
pub fn tabs<S>(
    cx: &mut Cx<S>,
    theme: &Theme,
    labels: &[&str],
    selected: usize,
    on_select: impl Fn(&mut S, usize) + Clone + 'static,
) -> Element {
    let p = &theme.palette;
    let mut cells = Vec::with_capacity(labels.len());
    for (i, text) in labels.iter().enumerate() {
        let active = i == selected;
        let (fill, ink) = if active {
            (p.primary, p.on_primary)
        } else {
            (p.surface, p.text)
        };
        let on = on_select.clone();
        let cell = Element::stack(
            Axis::Horizontal,
            vec![Element::text(*text, theme.typography.body, ink)],
        )
        .fill(fill)
        .border(p.border, 1.0)
        .radius(theme.radius)
        .padding(Insets::symmetric(theme.spacing.md, theme.spacing.sm))
        .align(Align::Center, Align::Center)
        .grow(1.0)
        .on_tap(cx, move |s: &mut S| on(s, i));
        cells.push(cell);
    }
    Element::stack(Axis::Horizontal, cells)
        .gap(theme.spacing.xs)
        .align(Align::Start, Align::Stretch)
}

/// A progress bar: a track with a primary-filled portion for `fraction` (0..=1).
/// Default width 200 px; override with `.width(..)`.
pub fn progress_bar(theme: &Theme, fraction: f64) -> Element {
    let f = fraction.clamp(0.0, 1.0);
    let control = NativeControl::Progress {
        value: (f * 1000.0).round() as u16,
    };
    if let Some(deco) = theme.native_deco(control) {
        return native_size(Element::boxed(BoxStyle::default()).native(deco), control);
    }
    let fill = Element::boxed(BoxStyle {
        fill: Some(theme.palette.primary),
        radius: 4.0,
        border: None,
    })
    .grow(f);
    Element::stack(Axis::Horizontal, vec![fill, spacer().grow(1.0 - f)])
        .fill(theme.palette.border)
        .radius(4.0)
        .width(200.0)
        .height(8.0)
        .align(Align::Start, Align::Stretch)
}

mod spinner;
pub use spinner::spinner;

/// A tappable menu row showing `text`, running `on_select` when clicked. Stretch
/// it across a [`menu`] panel (the panel stretches its children).
pub fn menu_item<S>(
    cx: &mut Cx<S>,
    theme: &Theme,
    text: impl Into<String>,
    on_select: impl FnMut(&mut S) + 'static,
) -> Element {
    Element::stack(Axis::Horizontal, vec![label(theme, text)])
        .radius(theme.radius / 2.0)
        .padding(Insets::symmetric(theme.spacing.md, theme.spacing.sm))
        .align(Align::Start, Align::Center)
        .on_tap(cx, on_select)
}

/// A floating vertical menu panel (surface, border, padding) holding `items`
/// (build them with [`menu_item`]). The visual body of a dropdown — show it via
/// [`open_menu`] or `cx.overlay`.
pub fn menu(theme: &Theme, items: Vec<Element>) -> Element {
    Element::stack(Axis::Vertical, items)
        .fill(theme.palette.surface)
        .border(theme.palette.border, 1.0)
        .radius(theme.radius)
        .padding(Insets::uniform(theme.spacing.xs))
        .gap(2.0)
        .align(Align::Start, Align::Stretch)
        .width(180.0)
}

/// Declare a non-modal dropdown overlay anchored with its top-left at `at`,
/// containing `items` (from [`menu_item`]). `on_dismiss` fires on a press
/// outside the menu. Call when the menu should be open.
pub fn open_menu<S>(
    cx: &mut Cx<S>,
    theme: &Theme,
    at: Point,
    items: Vec<Element>,
    on_dismiss: impl FnMut(&mut S) + 'static,
) {
    let content = menu(theme, items);
    let dismiss = cx.register(on_dismiss);
    cx.overlay(OverlaySpec {
        content,
        anchor: Anchor::At(at),
        modal: false,
        dismiss: Some(dismiss),
    });
}

/// Declare a modal dialog overlay: a centered panel with `title`, a `body`
/// element, and a right-aligned row of `actions` (buttons). A dark scrim behind
/// it blocks the tree; pressing the scrim runs `on_dismiss`.
pub fn open_dialog<S>(
    cx: &mut Cx<S>,
    theme: &Theme,
    title: impl Into<String>,
    body: Element,
    actions: Vec<Element>,
    on_dismiss: impl FnMut(&mut S) + 'static,
) {
    let dismiss = cx.register(on_dismiss);
    let content = panel(
        theme,
        vec![
            heading(theme, title),
            body,
            row(actions)
                .gap(theme.spacing.sm)
                .align(Align::End, Align::Center),
        ],
    )
    .width(340.0);
    cx.overlay(OverlaySpec {
        content,
        anchor: Anchor::Center,
        modal: true,
        dismiss: Some(dismiss),
    });
}

/// A small tooltip bubble showing `text` (a dark rounded label). Show it via
/// `cx.overlay` anchored near the hovered element (non-modal, no dismiss).
pub fn tooltip(theme: &Theme, text: impl Into<String>) -> Element {
    Element::stack(
        Axis::Horizontal,
        vec![Element::text(
            text.into(),
            theme.typography.caption,
            theme.palette.on_primary,
        )],
    )
    .fill(theme.palette.text)
    .radius(theme.radius / 2.0)
    .padding(Insets::symmetric(theme.spacing.sm, theme.spacing.xs))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::{ElementKind, render_view};
    use crate::geometry::Size;

    #[test]
    fn panel_is_a_decorated_vertical_stack() {
        let p = panel(&Theme::light(), vec![divider(&Theme::light())]);
        assert!(p.decoration.fill.is_some());
        assert!(matches!(
            p.kind,
            ElementKind::Stack {
                axis: Axis::Vertical,
                ..
            }
        ));
    }

    #[test]
    fn setting_row_paints_swatch_and_button() {
        // Swatch fill, plus the button's fill and border; the spacer is empty
        // and the label needs a font.
        let theme = Theme::light();
        let scene = render_view(
            &setting_row(&theme, Color::rgb(200, 80, 80), "Edit"),
            Size::new(300.0, 40.0),
            &theme,
            None,
        );
        assert_eq!(scene.len(), 3);
    }

    #[test]
    fn edit_buffer_inserts_and_moves_caret() {
        let mut b = EditBuffer::new();
        b.insert("ab");
        assert_eq!((b.text(), b.caret()), ("ab", 2));
        b.move_left();
        assert_eq!(b.caret(), 1);
        b.insert("X"); // insert mid-string at the caret
        assert_eq!((b.text(), b.caret()), ("aXb", 2));
        b.home();
        assert_eq!(b.caret(), 0);
        b.end();
        assert_eq!(b.caret(), 3);
    }

    #[test]
    fn edit_buffer_backspace_and_delete_at_caret() {
        let mut b = EditBuffer::from_text("abc");
        assert_eq!(b.caret(), 3); // from_text places caret at end
        b.backspace();
        assert_eq!((b.text(), b.caret()), ("ab", 2));
        b.home();
        b.delete();
        assert_eq!((b.text(), b.caret()), ("b", 0));
        // Deletes/backspaces at the edges are no-ops.
        b.backspace();
        assert_eq!(b.text(), "b");
        b.end();
        b.delete();
        assert_eq!(b.text(), "b");
    }

    #[test]
    fn edit_buffer_respects_utf8_boundaries() {
        // "é" and "🦀" are multi-byte; caret motion must land on boundaries.
        let mut b = EditBuffer::from_text("é🦀");
        assert_eq!(b.caret(), "é🦀".len());
        b.move_left(); // skip the whole crab
        assert_eq!(b.caret(), "é".len());
        b.move_left();
        assert_eq!(b.caret(), 0);
        b.delete(); // removes "é", not a partial byte
        assert_eq!(b.text(), "🦀");
    }

    #[test]
    fn edit_buffer_apply_dispatches_keys() {
        let mut b = EditBuffer::new();
        for k in [
            KeyInput::Text("hi".into()),
            KeyInput::Left,
            KeyInput::Text("X".into()),
        ] {
            b.apply(&k);
        }
        assert_eq!((b.text(), b.caret()), ("hXi", 2));
    }

    #[test]
    fn edit_buffer_shift_select_and_replace() {
        let mut b = EditBuffer::from_text("hello");
        b.home(); // caret at 0, no selection
        assert_eq!(b.selection(), None);
        b.select_right();
        b.select_right();
        b.select_right(); // select "hel"
        assert_eq!(b.selection(), Some((0, 3)));
        // Typing replaces the selection.
        b.insert("X");
        assert_eq!((b.text(), b.caret(), b.selection()), ("Xlo", 1, None));
    }

    #[test]
    fn edit_buffer_select_all_then_backspace_clears() {
        let mut b = EditBuffer::from_text("abc");
        b.select_all();
        assert_eq!(b.selection(), Some((0, 3)));
        b.backspace(); // deletes the whole selection
        assert!(b.is_empty());
        assert_eq!(b.selection(), None);
    }

    #[test]
    fn edit_buffer_plain_motion_collapses_selection() {
        let mut b = EditBuffer::from_text("abcd");
        b.home();
        b.select_right();
        b.select_right(); // select "ab", caret at 2
        assert_eq!(b.selection(), Some((0, 2)));
        b.move_left(); // collapse to selection start
        assert_eq!((b.caret(), b.selection()), (0, None));
    }

    #[test]
    fn edit_buffer_select_respects_utf8() {
        let mut b = EditBuffer::from_text("é🦀");
        b.home();
        b.select_right(); // selects "é" (2 bytes), not a partial byte
        assert_eq!(b.selection(), Some((0, "é".len())));
        b.select_right(); // extends over "🦀"
        assert_eq!(b.selection(), Some((0, "é🦀".len())));
    }

    #[test]
    fn edit_buffer_place_caret_and_extend() {
        let mut b = EditBuffer::from_text("hello");
        b.place_caret(2); // click between "he" and "llo"
        assert_eq!((b.caret(), b.selection()), (2, None));
        b.extend_to(4); // drag right to select "ll"
        assert_eq!(b.selection(), Some((2, 4)));
        b.extend_to(0); // drag left past the anchor
        assert_eq!(b.selection(), Some((0, 2)));
        // Out-of-range / mid-codepoint indices snap safely.
        let mut u = EditBuffer::from_text("é🦀");
        u.place_caret(999);
        assert_eq!(u.caret(), "é🦀".len());
        u.place_caret(1); // inside "é" -> snaps down to 0
        assert_eq!(u.caret(), 0);
    }

    #[test]
    fn edit_buffer_copy_paste_round_trips_through_the_clipboard() {
        let mut b = EditBuffer::from_text("Clip");
        b.apply(&KeyInput::SelectAll);
        b.apply(&KeyInput::Copy); // clipboard = "Clip"
        b.apply(&KeyInput::End); // collapse selection, caret at end
        b.apply(&KeyInput::Paste); // insert "Clip" → "ClipClip"
        assert_eq!(b.text(), "ClipClip");
        assert_eq!(crate::core::clipboard_text(), "Clip");

        // Cut removes the selection and leaves it on the clipboard.
        b.apply(&KeyInput::SelectAll);
        b.apply(&KeyInput::Cut);
        assert_eq!(b.text(), "");
        assert_eq!(crate::core::clipboard_text(), "ClipClip");
    }

    #[test]
    fn edit_buffer_enter_and_line_navigation() {
        let mut b = EditBuffer::new();
        b.apply(&KeyInput::Text("ab".into()));
        b.apply(&KeyInput::Enter); // newline → "ab\n", caret at 3
        b.apply(&KeyInput::Text("cde".into())); // "ab\ncde", caret at end (6)
        assert_eq!((b.text(), b.caret()), ("ab\ncde", 6));
        // Up keeps the byte column (3 → clamped to line 0 length 2).
        b.up();
        assert_eq!(b.caret(), 2);
        // Home/End are line-aware.
        b.home();
        assert_eq!(b.caret(), 0);
        b.end();
        assert_eq!(b.caret(), 2); // end of line 0, before '\n'
        // Down returns to line 1 at the same column.
        b.down();
        assert_eq!(b.caret(), 5); // "ab\ncd|e"
    }

    #[test]
    fn edit_buffer_shift_down_selects_across_lines() {
        let mut b = EditBuffer::from_text("ab\ncd");
        b.home(); // caret to start of line 1 ("cd")
        b.up(); // line 0
        b.home(); // caret 0
        b.apply(&KeyInput::SelectDown); // extend down one line
        let (s, e) = b.selection().expect("selection");
        assert_eq!(s, 0);
        assert!(e >= 3, "selection should reach into line 1, got {e}");
    }

    #[test]
    fn text_editor_carries_caret_on_its_text_leaf() {
        let theme = Theme::light();
        let mut cx = Cx::new(&theme);
        let buf = EditBuffer::from_text("hello");
        let field = text_editor(&mut cx, &theme, &buf, |s: &mut EditBuffer| s);
        // The inner text leaf carries the caret byte index for the focus overlay;
        // the field itself carries focus + text-pointer handles.
        assert!(field.focus.is_some());
        assert!(field.text_pos.is_some());
        let ElementKind::Stack { children, .. } = &field.kind else {
            panic!("text_editor should be a stack");
        };
        assert_eq!(children[0].caret, Some(5));
    }

    /// Feed each key to `b` in order.
    fn keys(b: &mut EditBuffer, ks: &[KeyInput]) {
        for k in ks {
            b.apply(k);
        }
    }

    fn typed(b: &mut EditBuffer, s: &str) {
        for c in s.chars() {
            b.apply(&KeyInput::Text(c.to_string()));
        }
    }

    #[test]
    fn undo_coalesces_consecutive_typing() {
        let mut b = EditBuffer::new();
        assert!(!b.can_undo());
        typed(&mut b, "hello");
        assert!(b.can_undo());
        b.apply(&KeyInput::Undo);
        assert_eq!(b.text(), "");
        assert_eq!(b.caret(), 0);
        assert!(!b.can_undo());
        b.apply(&KeyInput::Redo);
        assert_eq!(b.text(), "hello");
        assert_eq!(b.caret(), 5);
    }

    #[test]
    fn caret_move_ends_the_typing_group() {
        let mut b = EditBuffer::new();
        typed(&mut b, "ab");
        // Left then Right returns to the same spot, but still ends the group.
        keys(&mut b, &[KeyInput::Left, KeyInput::Right]);
        typed(&mut b, "cd");
        assert_eq!(b.text(), "abcd");
        b.undo();
        assert_eq!(b.text(), "ab");
        assert_eq!(b.caret(), 2);
        b.undo();
        assert_eq!(b.text(), "");
        assert!(!b.undo());
    }

    #[test]
    fn different_edit_kind_ends_the_group() {
        let mut b = EditBuffer::new();
        typed(&mut b, "abc");
        keys(&mut b, &[KeyInput::Backspace, KeyInput::Backspace]);
        typed(&mut b, "XY");
        assert_eq!(b.text(), "aXY");
        b.undo(); // typing "XY"
        assert_eq!(b.text(), "a");
        b.undo(); // both backspaces, as one step
        assert_eq!(b.text(), "abc");
        assert_eq!(b.caret(), 3);
        b.undo(); // typing "abc"
        assert_eq!(b.text(), "");
    }

    #[test]
    fn delete_presses_coalesce() {
        let mut b = EditBuffer::from_text("abcdef");
        b.place_caret(1);
        keys(
            &mut b,
            &[KeyInput::Delete, KeyInput::Delete, KeyInput::Delete],
        );
        assert_eq!(b.text(), "aef");
        b.undo();
        assert_eq!(b.text(), "abcdef");
        assert_eq!(b.caret(), 1);
    }

    #[test]
    fn newline_and_paste_are_their_own_steps() {
        let mut b = EditBuffer::new();
        typed(&mut b, "ab");
        b.apply(&KeyInput::Enter);
        typed(&mut b, "cd");
        assert_eq!(b.text(), "ab\ncd");
        b.undo();
        assert_eq!(b.text(), "ab\n");
        b.undo();
        assert_eq!(b.text(), "ab");
        b.undo();
        assert_eq!(b.text(), "");
    }

    #[test]
    fn undo_restores_a_replaced_selection() {
        let mut b = EditBuffer::from_text("hello world");
        b.place_caret(6);
        b.extend_to(11);
        typed(&mut b, "there");
        assert_eq!(b.text(), "hello there");
        // Replacing the selection plus the typing that followed it.
        b.undo();
        assert_eq!(b.text(), "hello t");
        b.undo();
        assert_eq!(b.text(), "hello world");
        assert_eq!(b.selection(), Some((6, 11)));
        b.redo();
        assert_eq!(b.text(), "hello t");
        assert_eq!(b.selection(), None);
        assert_eq!(b.caret(), 7);
    }

    #[test]
    fn new_edit_clears_redo() {
        let mut b = EditBuffer::new();
        typed(&mut b, "ab");
        b.undo();
        assert!(b.can_redo());
        typed(&mut b, "x");
        assert!(!b.can_redo());
        assert!(!b.redo());
        assert_eq!(b.text(), "x");
    }

    #[test]
    fn undo_redo_round_trip_multibyte() {
        let mut b = EditBuffer::from_text("é🦀z");
        b.place_caret("é🦀".len());
        b.backspace(); // removes the crab
        b.apply(&KeyInput::Left);
        typed(&mut b, "日本");
        assert_eq!(b.text(), "日本éz");
        b.undo();
        assert_eq!(b.text(), "éz");
        b.undo();
        assert_eq!(b.text(), "é🦀z");
        assert_eq!(b.caret(), "é🦀".len());
        b.redo();
        b.redo();
        assert_eq!(b.text(), "日本éz");
        assert_eq!(b.caret(), "日本".len());
        b.clear_history();
        assert!(!b.can_undo() && !b.can_redo());
    }

    #[test]
    fn equality_ignores_history() {
        let mut a = EditBuffer::new();
        typed(&mut a, "hi");
        assert_eq!(a, EditBuffer::from_text("hi"));
    }

    #[test]
    fn word_movement_stops_at_word_and_punctuation_runs() {
        let text = "foo bar_baz, qux";
        let mut b = EditBuffer::from_text(text);
        b.place_caret(0);
        let mut stops = Vec::new();
        for _ in 0..5 {
            b.apply(&KeyInput::WordRight);
            stops.push(b.caret());
        }
        // foo| bar_baz|,| qux| (then stays at the end)
        assert_eq!(stops, vec![3, 11, 12, 16, 16]);
        let mut back = Vec::new();
        for _ in 0..5 {
            b.apply(&KeyInput::WordLeft);
            back.push(b.caret());
        }
        assert_eq!(back, vec![13, 11, 4, 0, 0]);
    }

    #[test]
    fn word_movement_handles_unicode() {
        // Accented and CJK letters are word chars; the em dash is punctuation.
        let text = "café — 日本語 x";
        let mut b = EditBuffer::from_text(text);
        b.place_caret(0);
        b.word_right();
        assert_eq!(&text[..b.caret()], "café");
        b.word_right();
        assert_eq!(&text[..b.caret()], "café —");
        b.word_right();
        assert_eq!(&text[..b.caret()], "café — 日本語");
        b.word_left();
        assert_eq!(&text[..b.caret()], "café — ");
        // Every stop lands on a char boundary.
        b.place_caret(text.len());
        while b.caret() > 0 {
            b.word_left();
            assert!(text.is_char_boundary(b.caret()));
        }
    }

    #[test]
    fn word_movement_crosses_lines() {
        let mut b = EditBuffer::from_text("one\n  two");
        b.word_left();
        assert_eq!(b.caret(), 6);
        b.word_left();
        assert_eq!(b.caret(), 0);
        b.word_right();
        assert_eq!(b.caret(), 3);
        b.word_right();
        assert_eq!(b.caret(), 9);
    }

    #[test]
    fn shift_word_movement_extends_selection() {
        let mut b = EditBuffer::from_text("alpha beta gamma");
        b.place_caret(6);
        keys(
            &mut b,
            &[KeyInput::SelectWordRight, KeyInput::SelectWordRight],
        );
        assert_eq!(b.selection(), Some((6, 16)));
        assert_eq!(b.selected_text().as_deref(), Some("beta gamma"));
        // Back past the anchor flips the selection to the other side.
        keys(
            &mut b,
            &[
                KeyInput::SelectWordLeft,
                KeyInput::SelectWordLeft,
                KeyInput::SelectWordLeft,
            ],
        );
        assert_eq!(b.selection(), Some((0, 6)));
        // A plain word move collapses it.
        b.apply(&KeyInput::WordRight);
        assert_eq!(b.selection(), None);
        assert_eq!(b.caret(), 5);
    }

    #[test]
    fn word_delete_backward_and_forward() {
        let mut b = EditBuffer::from_text("let x = foo.bar;");
        b.apply(&KeyInput::DeleteWordBackward); // ";"
        assert_eq!(b.text(), "let x = foo.bar");
        b.apply(&KeyInput::DeleteWordBackward); // "bar"
        assert_eq!(b.text(), "let x = foo.");
        b.place_caret(3);
        b.apply(&KeyInput::DeleteWordForward); // " x"
        assert_eq!(b.text(), "let = foo.");
        assert_eq!(b.caret(), 3);
        // Each word delete is its own undo step.
        b.undo();
        assert_eq!(b.text(), "let x = foo.");
        b.undo();
        assert_eq!(b.text(), "let x = foo.bar");
        // At the edges word deletes are no-ops (and record nothing).
        let mut e = EditBuffer::from_text("ab");
        e.place_caret(0);
        e.delete_word_backward();
        e.place_caret(2);
        e.delete_word_forward();
        assert_eq!(e.text(), "ab");
        assert!(!e.can_undo());
    }

    #[test]
    fn word_delete_removes_selection_first() {
        let mut b = EditBuffer::from_text("one two three");
        b.place_caret(4);
        b.extend_to(6);
        b.apply(&KeyInput::DeleteWordBackward);
        assert_eq!(b.text(), "one o three");
        assert_eq!(b.caret(), 4);
    }

    #[test]
    fn word_delete_multibyte() {
        let mut b = EditBuffer::from_text("naïve 🦀🦀");
        b.delete_word_backward(); // the crab pair is one punctuation run
        assert_eq!(b.text(), "naïve ");
        b.delete_word_backward();
        assert_eq!(b.text(), "");
    }

    #[test]
    fn word_range_prefers_the_word_next_to_the_click() {
        let b = EditBuffer::from_text("hello, world  x");
        // Inside a word.
        assert_eq!(b.word_range_at(2), (0, 5));
        // Just past the word's last letter (clicked its right half).
        assert_eq!(b.word_range_at(5), (0, 5));
        // Between ',' and ' ': punctuation beats space.
        assert_eq!(b.word_range_at(6), (5, 6));
        // Inside a run of spaces.
        assert_eq!(b.word_range_at(13), (12, 14));
        // Multibyte word; an index inside a char snaps down to its boundary.
        let m = EditBuffer::from_text("über 日本語");
        assert_eq!(m.word_range_at(1), (0, "über".len()));
        assert_eq!(m.word_range_at(7), ("über ".len(), m.text().len()));
        // Between two line breaks there is nothing to select.
        let n = EditBuffer::from_text("a\n\nb");
        assert_eq!(n.word_range_at(2), (2, 2));
    }

    #[test]
    fn select_word_and_line() {
        let mut b = EditBuffer::from_text("first line\nsecond line here");
        b.place_caret(14);
        b.apply(&KeyInput::SelectWord);
        assert_eq!(b.selected_text().as_deref(), Some("second"));
        b.apply(&KeyInput::SelectLine);
        assert_eq!(b.selected_text().as_deref(), Some("second line here"));
        b.place_caret(3);
        b.select_line();
        assert_eq!(b.selected_text().as_deref(), Some("first line"));
        // Selecting doesn't record an edit, but ends a typing group.
        let mut t = EditBuffer::new();
        typed(&mut t, "ab");
        t.select_word();
        typed(&mut t, "c");
        assert_eq!(t.text(), "c");
        t.undo();
        assert_eq!(t.text(), "ab");
        assert_eq!(t.selection(), Some((0, 2)));
    }

    #[test]
    fn text_editor_treats_select_line_as_select_all() {
        struct St {
            b: EditBuffer,
        }
        let theme = Theme::light();
        let mut st = St {
            b: EditBuffer::from_text("a\nb"),
        };
        let mut cx = Cx::new(&theme);
        let field = text_editor(&mut cx, &theme, &st.b, |s: &mut St| &mut s.b);
        let mut handlers = cx.into_handlers();
        st.b.place_caret(0);
        assert!(handlers.dispatch_key(field.focus.unwrap(), &KeyInput::SelectLine, &mut st));
        assert_eq!(st.b.selection(), Some((0, 3)));
    }

    #[test]
    fn arrows_move_visually_through_right_to_left_text() {
        // "abc אבג": a0 b1 c2 ' '3, then א at byte 4, ב 6, ג 8; 10 bytes.
        let mut b = EditBuffer::from_text("abc אבג");
        b.place_caret(3);
        b.move_right(); // after the space: the Hebrew's visual left end...
        assert_eq!(b.caret(), 4 + 6, "...which is the end of the text");
        b.move_right();
        assert_eq!(b.caret(), 8, "then back through it, to before ג");
        b.move_right();
        b.move_right();
        assert_eq!(b.caret(), 4, "the visual right end is before א");
        b.move_right();
        assert_eq!(b.caret(), 4, "and that's the end of the line");
        b.move_left();
        assert_eq!(b.caret(), 6);

        // A right-to-left line: Left moves forward through the text.
        let mut h = EditBuffer::from_text("שלום");
        h.place_caret(0);
        h.move_left();
        assert_eq!(h.caret(), 2);
        h.move_right();
        assert_eq!(h.caret(), 0);
        // Shift+Left extends forward too, and the selection stays logical.
        h.select_left();
        h.select_left();
        assert_eq!(h.selection(), Some((0, 4)));
        // Left collapses a right-to-left selection to its far (logical) end.
        h.move_left();
        assert_eq!(h.caret(), 4);
    }

    #[test]
    fn deletion_stays_logical_in_mixed_text() {
        let mut b = EditBuffer::from_text("abc אבג");
        b.backspace();
        assert_eq!(
            b.text(),
            "abc אב",
            "Backspace removes the last letter typed"
        );
        b.place_caret(4);
        b.delete();
        assert_eq!(b.text(), "abc ב");
    }

    #[test]
    fn direction_can_be_forced() {
        let mut b = EditBuffer::from_text("abc").with_direction(TextDirection::Rtl);
        assert_eq!(b.direction(), TextDirection::Rtl);
        // Latin letters keep their order inside a right-to-left line: the
        // caret before "a" is at the line's visual left edge.
        b.place_caret(0);
        b.move_left();
        assert_eq!(b.caret(), 0);
        b.move_right();
        assert_eq!(b.caret(), 1);
        // At the end of a left-to-right line, Right goes on to the next line.
        let mut m = EditBuffer::from_text("ab\nאב");
        m.place_caret(2);
        m.move_right();
        assert_eq!(m.caret(), 3);
    }
}