bevy_feathers 0.20.0

A collection of UI widgets for building editors and utilities in Bevy
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
use std::{f32::consts::PI, ops::RangeInclusive};

use bevy_app::{Plugin, PreUpdate, PropagateOver};
use bevy_color::Color;
use bevy_ecs::{
    change_detection::DetectChanges,
    component::Component,
    entity::Entity,
    event::EntityEvent,
    hierarchy::{ChildOf, Children},
    lifecycle::{Add, Insert, Remove},
    observer::On,
    query::{Changed, Has, Or, With, Without},
    reflect::ReflectComponent,
    resource::Resource,
    schedule::IntoScheduleConfigs,
    system::{Commands, Query, Res},
};
use bevy_input::{
    keyboard::{Key, KeyCode, KeyboardInput},
    ButtonInput,
};
use bevy_input_focus::{FocusGained, FocusLost, FocusedInput, InputFocus, InputFocusSystems};
use bevy_log::{warn, warn_once};
use bevy_math::{ops, Rot2};
use bevy_picking::{
    cursor::EntityCursor,
    events::{
        PointerCancel, PointerDrag, PointerDragEnd, PointerDragStart, PointerPress, PointerRelease,
    },
    hover::Hovered,
    pointer::PointerButton,
    PickingSystems,
};
use bevy_platform::collections::HashMap;
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;
use bevy_scene::prelude::*;
use bevy_text::{
    EditableText, FontSourceTemplate, Justify, LineHeight, TextEdit, TextFont, TextLayout,
    TextReadWriteMode,
};
use bevy_ui::{
    percent, px, widget::Text, AlignItems, AlignSelf, BackgroundGradient, ColorStop, ComputedNode,
    ComputedUiRenderTargetInfo, Display, Gradient, InteractionDisabled, InterpolationColorSpace,
    JustifyContent, LinearGradient, Node, PositionType, UiGlobalTransform, UiRect, UiScale,
    UiTransform,
};
use bevy_ui_widgets::ValueChange;
use smol_str::SmolStr;

use crate::{
    constants::{fonts, icons, size},
    controls::{FeathersSlider, FeathersTextInput, FeathersTextInputContainer},
    display::icon,
    rounded_corners::RoundedCorners,
    theme::{
        SurfaceLevel, ThemeBackgroundColor, ThemeBorderColor, ThemeContext, ThemeTextColor,
        ThemeToken, UiTheme,
    },
    tokens,
};

/// Threshold used to distinguish between a "click" and a "drag" gesture.
const DRAG_THRESHOLD_DISTANCE: f32 = 0.5;

const BASE_DRAG_SPEED: f64 = 0.01f64;

/// Widget that permits text entry of floating-point numbers. This widget implements two-way
/// synchronization:
/// * it emits values (via a [`ValueChange<T>`]) event as the user types or drags.
///   The type of ``T`` will be ``f32``, ``f64``, ``i32``, or ``i64`` depending on the
///   [`NumberInputValue`] component variant.
/// * it listens for the insertion of the [`NumberInputValue`] component, and replaces
///   the contents of the text buffer based on the value in that event.
///
/// This is spawnable by inheriting it as a "scene component" with optional [`FeathersNumberInputProps`].
///
/// To avoid excessive updating, you should only update the number value when there is an actual
/// change, that is, when the new value is different from the current value.
///
/// In most cases, the actual source of truth for the numeric value will be external, that is,
/// some property in an app-specific data structure. It's the responsibility of the app to
/// synchronize this value with the [`FeathersNumberInput`] widget in both directions:
/// * When a [`ValueChange`] event is received, update the app-specific property.
/// * When the app-specific property changes - either in response to a [`ValueChange`] event, or
///   because of some other action, insert a [`NumberInputValue`] component to update the
///   displayed value.
///
/// The `is_final` boolean in [`ValueChange`] is set to false while dragging, however you should
/// still update the widget in response to these events, as otherwise the user won't be able to
/// see the updated value.
///
/// Additional components can be inserted into this widget to customize the behavior: see
/// [`SoftLimit`], [`HardLimit`], [`NumberInputPrecision`], and [`NumberInputStep`].
#[derive(SceneComponent, Default, Clone, Reflect)]
#[scene(FeathersNumberInputProps)]
#[reflect(Component, Default, Clone)]
#[require(NumberInputValue)]
pub struct FeathersNumberInput;

/// Props used to construct a [`FeathersNumberInput`] scene.
pub struct FeathersNumberInputProps {
    /// The "sigil" is a colored strip along the left edge of the input, which is used to
    /// distinguish between different axes. The default is transparent (no sigil).
    pub sigil_color: ThemeToken,
    /// A caption to be placed on the left side of the input, next to the colored stripe.
    /// Usually one of "X", "Y" or "Z".
    pub label_text: Option<&'static str>,
}

impl Default for FeathersNumberInputProps {
    fn default() -> Self {
        Self {
            sigil_color: tokens::TEXT_INPUT_LABEL_BG,
            label_text: None,
        }
    }
}

/// Marks the decrement (left-end) chevron of a number input.
#[derive(Component, Clone, Default, Reflect)]
#[reflect(Component)]
struct NumberInputDecrement;

/// Marks the increment (right-end) chevron of a number input.
#[derive(Component, Clone, Default, Reflect)]
#[reflect(Component)]
struct NumberInputIncrement;

impl FeathersNumberInput {
    fn scene(props: FeathersNumberInputProps) -> impl Scene {
        bsn! {
            @FeathersTextInputContainer
            Node {
                column_gap: px(0),
                border: UiRect {
                    left: px(if props.label_text.is_some() { 4.0 } else { 0.0 }),
                },
                padding: UiRect {
                    left: px(0.0),
                    right: px(0.0),
                },
            }
            ThemeBorderColor({props.sigil_color})
            FeathersNumberInput
            on(number_input_on_insert_value)
            on(number_input_on_insert_disabled)
            on(number_input_on_remove_disabled)
            Children [
                {
                    // Label section
                    props.label_text.map(|text| {
                        bsn! {
                            Node {
                                display: Display::Flex,
                                align_items: AlignItems::Center,
                                align_self: AlignSelf::Stretch,
                                justify_content: JustifyContent::Center,
                                padding: UiRect::axes(px(6), px(0)),
                            }
                            ThemeBackgroundColor(tokens::TEXT_INPUT_LABEL_BG)
                            Children [
                                Text(text)
                                TextFont {
                                    font: FontSourceTemplate::Handle(fonts::REGULAR),
                                    font_size: size::COMPACT_FONT,
                                }
                                PropagateOver<TextFont>
                                ThemeTextColor(tokens::TEXT_INPUT_TEXT)
                            ]
                        }
                    })
                }
                --
                // The editable text entity
                @FeathersTextInput {
                    @max_characters: 30usize, // 20 digits + units
                }
                DragState
                Node {
                    flex_grow: 1.0,
                    align_items: AlignItems::Center,
                    align_self: AlignSelf::Stretch,
                    border_radius: {
                        if props.label_text.is_some() {
                            RoundedCorners::Right.to_border_radius(4.0)
                        } else {
                            RoundedCorners::All.to_border_radius(4.0)
                        }
                    },
                }
                Hovered
                LineHeight::Px(24.0) // TODO: Make const for this
                TextLayout {
                    justify: Justify::Center,
                }
                ThemeTextColor(tokens::TEXT_INPUT_TEXT)
                // Use a gradient to draw the moving bar, this lets us round corners
                BackgroundGradient(vec![Gradient::Linear(LinearGradient {
                    angle: PI * 0.5,
                    stops: vec![
                        ColorStop::new(Color::NONE, percent(0)),
                        ColorStop::new(Color::NONE, percent(50)),
                        ColorStop::new(Color::NONE, percent(50)),
                        ColorStop::new(Color::NONE, percent(100)),
                    ],
                    color_space: InterpolationColorSpace::Srgba,
                })])
                EntityCursor::System(bevy_window::SystemCursorIcon::ColResize)
                on(number_input_init)
                on(number_input_on_enter_key)
                on(number_input_on_focus_gained)
                on(number_input_on_focus_lost)
                on(number_input_hovered)
                on(update_chevron_visibility)
                Children [
                    // Invisible child on top of input field which intercepts drag
                    // events (conditionally) and handles scrubbing gestures.
                    Node {
                        position_type: PositionType::Absolute,
                        left: px(0),
                        top: px(0),
                        bottom: px(0),
                        right: px(0),
                    }
                    on(scrubber_on_press)
                    on(scrubber_on_release)
                    on(scrubber_on_drag_start)
                    on(scrubber_on_drag)
                    on(scrubber_on_drag_end)
                    on(scrubber_on_drag_cancel)
                    --
                    // The decrement chevron of a number input
                    Node {
                        position_type: PositionType::Absolute,
                        display: Display::None,
                        left: px(4),
                    }
                    NumberInputDecrement
                    Children [
                        @icon(icons::CHEVRON_DOWN)
                        UiTransform {
                            rotation: Rot2::radians(std::f32::consts::FRAC_PI_2)
                        }
                    ]
                    --
                    // The increment chevron of a number input
                    Node {
                        position_type: PositionType::Absolute,
                        display: Display::None,
                        right: px(4),
                    }
                    NumberInputIncrement
                    Children [
                        @icon(icons::CHEVRON_RIGHT)
                    ]
                ]
            ]
        }
    }
}

/// Used to indicate what format of numbers we are editing. This affects the type
/// of [`ValueChange`] event that is emitted.
#[derive(Default, Clone, Copy, Reflect)]
pub enum NumberFormat {
    /// A 32-bit float
    #[default]
    F32,
    /// A 64-bit float
    F64,
    /// A 32-bit integer
    I32,
    /// A 64-bit integer
    I64,
}

/// Represents numbers in different formats.
#[derive(Component, Debug, PartialEq, Clone, Copy, Reflect)]
#[component(immutable)]
pub enum NumberInputValue {
    /// An `f32` value
    F32(f32),
    /// An `f64` value
    F64(f64),
    /// An `i32` value
    I32(i32),
    /// An `i64` value
    I64(i64),
}

impl core::fmt::Display for NumberInputValue {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            NumberInputValue::F32(v) => write!(f, "{}", v),
            NumberInputValue::F64(v) => write!(f, "{}", v),
            NumberInputValue::I32(v) => write!(f, "{}", v),
            NumberInputValue::I64(v) => write!(f, "{}", v),
        }
    }
}

impl NumberInputValue {
    fn format(&self) -> NumberFormat {
        match self {
            Self::F32(_) => NumberFormat::F32,
            Self::F64(_) => NumberFormat::F64,
            Self::I32(_) => NumberFormat::I32,
            Self::I64(_) => NumberFormat::I64,
        }
    }

    fn parse_from(value: &str, fmt: NumberFormat) -> Result<Self, String> {
        match fmt {
            NumberFormat::F32 => value
                .parse::<f32>()
                .map(NumberInputValue::F32)
                .map_err(|_| format!("Could not parse '{}' as f32", value)),
            NumberFormat::F64 => value
                .parse::<f64>()
                .map(NumberInputValue::F64)
                .map_err(|_| format!("Could not parse '{}' as f64", value)),
            NumberFormat::I32 => value
                .parse::<i32>()
                .map(NumberInputValue::I32)
                .map_err(|_| format!("Could not parse '{}' as i32", value)),
            NumberFormat::I64 => value
                .parse::<i64>()
                .map(NumberInputValue::I64)
                .map_err(|_| format!("Could not parse '{}' as i64", value)),
        }
    }

    /// Offset this value by `delta` (in value units), preserving the variant.
    fn offset_by(self, delta: f64) -> Self {
        match self {
            NumberInputValue::F32(v) => NumberInputValue::F32(v + delta as f32),
            NumberInputValue::F64(v) => NumberInputValue::F64(v + delta),
            NumberInputValue::I32(v) => {
                NumberInputValue::I32(v.saturating_add(delta.round() as i32))
            }
            NumberInputValue::I64(v) => {
                NumberInputValue::I64(v.saturating_add(delta.round() as i64))
            }
        }
    }

    /// Scale this value by `scale` (in value units), preserving the variant.
    fn scale_by(self, scale: f64) -> Self {
        match self {
            NumberInputValue::F32(v) => NumberInputValue::F32((v as f64 * scale) as f32),
            NumberInputValue::F64(v) => NumberInputValue::F64(v * scale),
            NumberInputValue::I32(v) => NumberInputValue::I32((v as f64 * scale).round() as i32),
            NumberInputValue::I64(v) => NumberInputValue::I64((v as f64 * scale).round() as i64),
        }
    }

    fn as_f64(&self) -> f64 {
        match *self {
            NumberInputValue::F32(v) => v as f64,
            NumberInputValue::F64(v) => v,
            NumberInputValue::I32(v) => v as f64,
            NumberInputValue::I64(v) => v as f64,
        }
    }
}

impl Default for NumberInputValue {
    fn default() -> Self {
        Self::F32(0.0)
    }
}

/// Represents numeric limits in different number formats.
#[derive(Debug, PartialEq, Clone, Reflect)]
pub enum NumberInputRange {
    /// An 'f32' range.
    F32(RangeInclusive<f32>),
    /// An 'f64' range.
    F64(RangeInclusive<f64>),
    /// An 'i32' range.
    I32(RangeInclusive<i32>),
    /// An 'i64' range.
    I64(RangeInclusive<i64>),
}

impl NumberInputRange {
    /// Clamp a numeric value of varying type to be within this range.
    pub fn clamp(&self, n: NumberInputValue) -> NumberInputValue {
        match (self, n) {
            (Self::F32(r), NumberInputValue::F32(v)) => {
                NumberInputValue::F32(v.clamp(*r.start(), *r.end()))
            }
            (Self::F64(r), NumberInputValue::F64(v)) => {
                NumberInputValue::F64(v.clamp(*r.start(), *r.end()))
            }
            (Self::I32(r), NumberInputValue::I32(v)) => {
                NumberInputValue::I32(v.clamp(*r.start(), *r.end()))
            }
            (Self::I64(r), NumberInputValue::I64(v)) => {
                NumberInputValue::I64(v.clamp(*r.start(), *r.end()))
            }
            (range, value) => {
                warn_once!("Number input range type mismatch: {range:?} {value:?}");
                n
            }
        }
    }

    /// Wrap a numeric value of varying type to be within this range.
    pub fn wrap(&self, n: NumberInputValue) -> NumberInputValue {
        match (self, n) {
            (Self::F32(r), NumberInputValue::F32(v)) => {
                let range = r.end() - r.start();
                NumberInputValue::F32(r.start() + (v - r.start()).rem_euclid(range))
            }
            (Self::F64(r), NumberInputValue::F64(v)) => {
                let range = r.end() - r.start();
                NumberInputValue::F64(r.start() + (v - r.start()).rem_euclid(range))
            }
            (Self::I32(r), NumberInputValue::I32(v)) => {
                let range = r.end() - r.start();
                NumberInputValue::I32(r.start() + (v - r.start()).rem_euclid(range))
            }
            (Self::I64(r), NumberInputValue::I64(v)) => {
                let range = r.end() - r.start();
                NumberInputValue::I64(r.start() + (v - r.start()).rem_euclid(range))
            }
            (range, value) => {
                warn_once!("Number input range type mismatch: {range:?} {value:?}");
                n
            }
        }
    }

    /// Compute the position of the thumb on the slide bar, as a value between 0 and 1, taking
    /// into account the proportion of the value between the minimum and maximum limits.
    pub fn thumb_position(&self, value: NumberInputValue) -> f32 {
        match (self, value) {
            (Self::F32(range), NumberInputValue::F32(n)) => {
                if range.end() > range.start() {
                    (n - range.start()) / (range.end() - range.start())
                } else {
                    0.5
                }
            }

            (Self::F64(range), NumberInputValue::F64(n)) => {
                if range.end() > range.start() {
                    ((n - range.start()) / (range.end() - range.start())) as f32
                } else {
                    0.5
                }
            }

            (Self::I32(range), NumberInputValue::I32(n)) => {
                if range.end() > range.start() {
                    (n - range.start()) as f32 / (range.end() - range.start()) as f32
                } else {
                    0.5
                }
            }

            (Self::I64(range), NumberInputValue::I64(n)) => {
                if range.end() > range.start() {
                    (n - range.start()) as f32 / (range.end() - range.start()) as f32
                } else {
                    0.5
                }
            }

            (range, value) => {
                warn_once!("Number input range type mismatch: {range:?} {value:?}");
                0.5
            }
        }
    }
}

impl Default for NumberInputRange {
    fn default() -> Self {
        Self::F32(0.0..=0.0)
    }
}

/// A soft limit represents the range of values that can be reached via dragging. Values outside
/// this range can still be entered by typing.
#[derive(Component, Default, Clone, Reflect)]
pub struct SoftLimit(pub NumberInputRange);

impl SoftLimit {
    /// Create a [`SoftLimit`] for `f32` values.
    pub fn f32(range: RangeInclusive<f32>) -> Self {
        Self(NumberInputRange::F32(range))
    }

    /// Create a [`SoftLimit`] for `f64` values.
    pub fn f64(range: RangeInclusive<f64>) -> Self {
        Self(NumberInputRange::F64(range))
    }

    /// Create a [`SoftLimit`] for `i32` values.
    pub fn i32(range: RangeInclusive<i32>) -> Self {
        Self(NumberInputRange::I32(range))
    }

    /// Create a [`SoftLimit`] for `i64` values.
    pub fn i64(range: RangeInclusive<i64>) -> Self {
        Self(NumberInputRange::I64(range))
    }
}

/// A hard limit represents an absolute constraint on the value. Values outside this range will
/// be clamped within the range.
// Note: Similar in concept to `SliderRange`, but the latter only handles f32s.
#[derive(Component, Default, Clone, Reflect)]
pub struct HardLimit(pub NumberInputRange);

impl HardLimit {
    /// Create a [`HardLimit`] for `f32` values.
    pub fn f32(range: RangeInclusive<f32>) -> Self {
        Self(NumberInputRange::F32(range))
    }

    /// Create a [`HardLimit`] for `f64` values.
    pub fn f64(range: RangeInclusive<f64>) -> Self {
        Self(NumberInputRange::F64(range))
    }

    /// Create a [`HardLimit`] for `i32` values.
    pub fn i32(range: RangeInclusive<i32>) -> Self {
        Self(NumberInputRange::I32(range))
    }

    /// Create a [`HardLimit`] for `i64` values.
    pub fn i64(range: RangeInclusive<i64>) -> Self {
        Self(NumberInputRange::I64(range))
    }
}

/// A component which controls the rounding of the number value during dragging. This is also used
/// as a heuristic to determine drag speed when there is no soft limit or step size specified.
///
/// Stepping is not affected, although presumably the step size will be an integer multiple of the
/// rounding factor. This also doesn't prevent the edited value from being set to non-rounded values
/// by other means, such as manually entering digits via a numeric input field.
///
/// The value in this component represents the number of decimal places of desired precision, so a
/// value of 2 would round to the nearest 1/100th. A value of -3 would round to the nearest
/// thousand.
#[derive(Component, Debug, Clone, Copy, Reflect)]
#[reflect(Component, Default)]
pub struct NumberInputPrecision(pub i32);

impl NumberInputPrecision {
    fn round_f32(&self, value: f32) -> f32 {
        let factor = ops::powf(10.0_f32, self.0 as f32);
        (value * factor).round() / factor
    }

    fn round_f64(&self, value: f64) -> f64 {
        let factor = f64::powf(10.0_f64, self.0 as f64);
        (value * factor).round() / factor
    }

    fn round(&self, value: NumberInputValue) -> NumberInputValue {
        match value {
            NumberInputValue::F32(v) => NumberInputValue::F32(self.round_f32(v)),
            NumberInputValue::F64(v) => NumberInputValue::F64(self.round_f64(v)),
            // Decimal-place rounding only affects integers at negative precision
            // (round to 10/100/...); left as identity for now.
            other => other,
        }
    }
}

impl Default for NumberInputPrecision {
    fn default() -> Self {
        Self(2)
    }
}

/// A component which controls the step size when incrementing or decrementing the value.
/// This also is used as a heuristic to determine drag speed when there is no soft limit present.
#[derive(Component, Debug, Clone, Copy, Reflect)]
#[reflect(Component, Default)]
pub struct NumberInputStep(pub f64);

impl Default for NumberInputStep {
    fn default() -> Self {
        Self(1.0f64)
    }
}

/// Indicates whether the number input should wrap around the min/max value.
/// This component only applies to [`HardLimit`].
#[derive(Component, Default, Debug, PartialEq, Clone, Copy, Reflect)]
#[reflect(Component, Default)]
pub enum NumberInputWrap {
    /// The number input will not wrap around the min/max value.
    #[default]
    NoWrap,
    /// The number input will wrap around the min/max value.
    Wrap,
}

/// Indicates the type of quantity (length, angle, time, etc.) being edited, as
/// well as the preferred unit (meters, degrees, seconds, etc.).
///
/// The enclosed string should be the id of a [`UnitsFormat`] that has previously been registered,
/// such as ``length_meters`` or ``angle_degrees``.
///
/// Note on serialization: it intended that this component, like most feathers-related
/// component be serializable via reflection, so that it can be edited in the planned Bevy scene
/// editor. The objects pointed to by this id, however, are static and not meant to be serialized.
#[derive(Component, Default, Debug, Clone, Reflect)]
#[reflect(Component, Default)]
pub struct NumberInputUnits(pub SmolStr);

impl NumberInputUnits {
    /// Construct a [`NumberInputUnits`] instance from a static string slice. The string should
    /// be the id of a previously-registered [`UnitsFormat`].
    pub fn new_static(text: &'static str) -> Self {
        Self(SmolStr::new_static(text))
    }

    /// Construct a [`NumberInputUnits`] instance from a string slice. The string should
    /// be the id of a previously-registered [`UnitsFormat`].
    pub fn new_inline(text: &str) -> Self {
        Self(SmolStr::new_inline(text))
    }

    /// Construct a [`NumberInputUnits`] given a [`UnitsFormat`] reference, which must have been
    /// previously registered.
    pub fn new(format: &dyn UnitsFormat) -> Self {
        Self(SmolStr::new_static(format.id()))
    }
}

/// Manage state transitions between scrubbing and dragging modes
#[derive(Default, Clone, Reflect, PartialEq, Debug)]
enum EditMode {
    /// Neither scrubbing nor dragging (unfocused)
    #[default]
    Idle,
    /// Drag value via scrubbing
    Scrubbing,
    /// Edit value by typing
    Editing,
}

/// Component used to manage the state of a number during dragging ("scrubbing"). This component
/// lives on the text edit entity, not the widget root.
///
/// State transitions:
///
/// if not editing, then a click puts us into "dragging" mode. Once the drag is finished
/// we check max movement, if it's less than the threshold, we transition to "editing" mode.
///
/// Loss of focus, or hitting the return key, cancels "editing" mode.
///
/// Changing [`EditMode`] affects:
/// * Cursor shape
/// * [`TextReadWriteMode`]
/// * How drag events are handled
/// * Background and text color (not yet implemented).
#[derive(Component, Default, Clone, Reflect)]
#[reflect(Component)]
struct DragState {
    /// Whether the input is currently being dragged.
    mode: EditMode,

    /// Conversion factor from pixels dragged to value
    drag_speed: f64,

    /// Similar to drag distance in the drag event, but includes scaling caused by modifier keys.
    value_offset: f64,

    /// The maximum absolute distance during the drag - used to detect click vs drag gesture.
    max_distance: f32,

    /// The value of the input when dragging started.
    base_value: NumberInputValue,
}

/// Observer which sets the text content of the field when the number value component changes.
fn number_input_on_insert_value(
    update: On<Insert<NumberInputValue>>,
    q_children: Query<&Children>,
    q_number_input: Query<
        (
            &NumberInputValue,
            Option<&SoftLimit>,
            Option<&HardLimit>,
            Option<&NumberInputUnits>,
        ),
        With<FeathersNumberInput>,
    >,
    mut q_text_input: Query<(&mut EditableText, &mut BackgroundGradient, &DragState)>,
    units_registry: Res<UnitsRegistry>,
) {
    let text_input_id = q_children
        .iter_descendants(update.event_target())
        .find(|e| q_text_input.contains(*e));

    if let Ok((&input_value, soft_limit, hard_limit, units)) =
        q_number_input.get(update.event_target())
        && let Some(text_id) = text_input_id
    {
        let clamped_value = match hard_limit {
            Some(limit) => limit.0.clamp(input_value),
            None => input_value,
        };
        let (mut editable_text, mut gradient, drag_state) = q_text_input.get_mut(text_id).unwrap();
        let new_digits = units_registry
            .resolve(units)
            .format(clamped_value, drag_state.mode == EditMode::Editing);
        if editable_text.value() != &new_digits {
            editable_text.queue_edit(TextEdit::SelectAll);
            editable_text.queue_edit(TextEdit::Insert(new_digits.into()));
        }

        update_slider_pos(&clamped_value, soft_limit, &mut gradient);
    }
}

/// Observer changes the colors based on disabled status.
fn number_input_on_insert_disabled(
    insert: On<Insert<InteractionDisabled>>,
    q_children: Query<&Children>,
    q_number_input: Query<
        (Has<InteractionDisabled>, Option<&ThemeContext>),
        With<FeathersNumberInput>,
    >,
    mut q_text_input: Query<(&Hovered, &mut BackgroundGradient)>,
    theme: Res<UiTheme>,
    input_focus: Res<InputFocus>,
    mut commands: Commands,
) {
    let text_input_id = q_children
        .iter_descendants(insert.event_target())
        .find(|e| q_text_input.contains(*e));

    if let Some(text_id) = text_input_id
        && let Ok((&Hovered(hovered), mut gradient)) = q_text_input.get_mut(text_id)
        && let Ok((is_disabled, theme_context)) = q_number_input.get(insert.event_target())
    {
        set_slidebar_styles(
            text_id,
            &theme,
            theme_context.map(|tc| tc.0).unwrap_or(SurfaceLevel::Base),
            is_disabled,
            false,
            hovered,
            input_focus.get() == Some(text_id),
            &mut gradient,
            &mut commands,
        );
        commands
            .entity(text_id)
            .try_insert(TextReadWriteMode::ReadOnly);
    }
}

/// Observer changes the colors based on disabled status.
fn number_input_on_remove_disabled(
    remove: On<Remove<InteractionDisabled>>,
    q_children: Query<&Children>,
    q_number_input: Query<
        (Has<InteractionDisabled>, Option<&ThemeContext>),
        With<FeathersNumberInput>,
    >,
    mut q_text_input: Query<(&Hovered, &mut BackgroundGradient)>,
    theme: Res<UiTheme>,
    input_focus: Res<InputFocus>,
    mut commands: Commands,
) {
    let text_input_id = q_children
        .iter_descendants(remove.event_target())
        .find(|e| q_text_input.contains(*e));

    if let Some(text_id) = text_input_id
        && let Ok((&Hovered(hovered), mut gradient)) = q_text_input.get_mut(text_id)
        && let Ok((is_disabled, theme_context)) = q_number_input.get(remove.event_target())
    {
        set_slidebar_styles(
            text_id,
            &theme,
            theme_context.map(|tc| tc.0).unwrap_or(SurfaceLevel::Base),
            is_disabled,
            false,
            hovered,
            input_focus.get() == Some(text_id),
            &mut gradient,
            &mut commands,
        );
        commands
            .entity(text_id)
            .try_insert(TextReadWriteMode::Editable);
    }
}

/// Observer which initializes the text edit once it has completed spawning.
fn number_input_init(
    insert: On<Add<EditableText>>,
    q_parent: Query<&ChildOf>,
    q_number_input: Query<
        (
            &NumberInputValue,
            Option<&SoftLimit>,
            Has<InteractionDisabled>,
            Option<&ThemeContext>,
            Option<&NumberInputUnits>,
        ),
        With<FeathersNumberInput>,
    >,
    mut q_text_input: Query<(
        &mut EditableText,
        &Hovered,
        &mut BackgroundGradient,
        &DragState,
    )>,
    theme: Res<UiTheme>,
    units_registry: Res<UnitsRegistry>,
    input_focus: Res<InputFocus>,
    mut commands: Commands,
) {
    let text_id = insert.event_target();
    if let Ok((mut editable_text, &Hovered(hovered), mut gradient, drag_state)) =
        q_text_input.get_mut(text_id)
        && let Ok(&ChildOf(root_id)) = q_parent.get(text_id)
        && let Ok((input_value, limit, is_disabled, theme_context, units)) =
            q_number_input.get(root_id)
    {
        let new_digits = units_registry
            .resolve(units)
            .format(*input_value, drag_state.mode == EditMode::Editing);
        let old_digits = editable_text.value().to_string();
        if old_digits != new_digits {
            editable_text.queue_edit(TextEdit::SelectAll);
            editable_text.queue_edit(TextEdit::Insert(new_digits.into()));
        }

        update_slider_pos(input_value, limit, &mut gradient);
        set_slidebar_styles(
            text_id,
            &theme,
            theme_context.map(|tc| tc.0).unwrap_or(SurfaceLevel::Base),
            is_disabled,
            false,
            hovered,
            input_focus.get() == Some(text_id),
            &mut gradient,
            &mut commands,
        );
        if is_disabled {
            commands.entity(text_id).insert(TextReadWriteMode::ReadOnly);
        }
    }
}

/// Observer which looks for changes in the hover state.
fn number_input_hovered(
    insert: On<Insert<Hovered>>,
    q_parent: Query<&ChildOf>,
    q_number_input: Query<
        (Has<InteractionDisabled>, Option<&ThemeContext>),
        With<FeathersNumberInput>,
    >,
    mut q_text_input: Query<(&Hovered, &mut BackgroundGradient, &DragState)>,
    theme: Res<UiTheme>,
    input_focus: Res<InputFocus>,
    mut commands: Commands,
) {
    let text_id = insert.event_target();
    if let Ok((&Hovered(hovered), mut gradient, drag_state)) = q_text_input.get_mut(text_id)
        && let Ok(&ChildOf(root_id)) = q_parent.get(text_id)
        && let Ok((is_disabled, theme_context)) = q_number_input.get(root_id)
    {
        set_slidebar_styles(
            text_id,
            &theme,
            theme_context.map(|tc| tc.0).unwrap_or(SurfaceLevel::Base),
            is_disabled,
            drag_state.mode == EditMode::Scrubbing,
            hovered,
            input_focus.get() == Some(text_id),
            &mut gradient,
            &mut commands,
        );
    }
}

fn number_input_on_enter_key(
    key_input: On<FocusedInput<KeyboardInput>>,
    q_parent: Query<&ChildOf>,
    q_number_input: Query<
        (
            &NumberInputValue,
            Option<&HardLimit>,
            Option<&NumberInputWrap>,
            Option<&NumberInputUnits>,
        ),
        With<FeathersNumberInput>,
    >,
    mut q_text_input: Query<(&EditableText, &mut DragState)>,
    units_registry: Res<UnitsRegistry>,
    mut commands: Commands,
) {
    if key_input.input.key_code != KeyCode::Enter {
        return;
    }

    let text_id = key_input.event_target();
    if let Ok(&ChildOf(root)) = q_parent.get(text_id)
        && let Ok((input_value, hard_limit, wrap, units)) = q_number_input.get(root)
        && let Ok((editable_text, mut drag_state)) = q_text_input.get_mut(text_id)
    {
        let text_value = editable_text.value().to_string();
        drag_state.mode = EditMode::Idle; // Boot us out of editing mode
        commands
            .entity(text_id)
            .insert(TextReadWriteMode::Static) // Hide selection
            .insert(EntityCursor::System(
                bevy_window::SystemCursorIcon::ColResize,
            ));
        emit_value_change(
            text_value,
            input_value.format(),
            units_registry.resolve(units),
            root,
            hard_limit,
            wrap,
            &mut commands,
            true,
        );
    }
}

fn number_input_on_focus_gained(focus_gained: On<FocusGained>, mut commands: Commands) {
    // Change cursor to I-Beam.
    let editable_text_id = focus_gained.event_target();
    commands
        .entity(editable_text_id)
        .insert(EntityCursor::System(bevy_window::SystemCursorIcon::Text));
}

fn number_input_on_focus_lost(
    focus_lost: On<FocusLost>,
    q_parent: Query<&ChildOf>,
    q_number_input: Query<
        (
            &NumberInputValue,
            Has<InteractionDisabled>,
            Option<&HardLimit>,
            Option<&NumberInputWrap>,
            Option<&NumberInputUnits>,
        ),
        With<FeathersNumberInput>,
    >,
    mut q_text_input: Query<(&EditableText, &mut DragState)>,
    units_registry: Res<UnitsRegistry>,
    mut commands: Commands,
) {
    let editable_text_id = focus_lost.event_target();

    if let Ok(&ChildOf(root)) = q_parent.get(editable_text_id)
        && let Ok((input_value, disabled, hard_limit, wrap, units)) = q_number_input.get(root)
        && let Ok((editable_text, mut drag_state)) = q_text_input.get_mut(editable_text_id)
    {
        let text_value = editable_text.value().to_string();
        emit_value_change(
            text_value,
            input_value.format(),
            units_registry.resolve(units),
            root,
            hard_limit,
            wrap,
            &mut commands,
            true,
        );

        drag_state.mode = EditMode::Idle;

        // Restore cursor and rwmode back to normal.
        commands
            .entity(editable_text_id)
            .insert(EntityCursor::System(
                bevy_window::SystemCursorIcon::ColResize,
            ))
            .insert(if disabled {
                TextReadWriteMode::ReadOnly
            } else {
                TextReadWriteMode::Static
            });
    }
}

fn scrubber_on_press(
    mut press: On<PointerPress>,
    mut q_text_input: Query<&mut DragState>,
    q_number_input: Query<Has<InteractionDisabled>, With<FeathersNumberInput>>,
    q_parent: Query<&ChildOf>,
    mut commands: Commands,
) {
    if let Ok(&ChildOf(text_id)) = q_parent.get(press.event_target())
        && let Ok(&ChildOf(root_id)) = q_parent.get(text_id)
        && let Ok(disabled) = q_number_input.get(root_id)
        && let Ok(mut drag_state) = q_text_input.get_mut(text_id)
    {
        drag_state.max_distance = 0.0;
        if disabled {
            drag_state.mode = EditMode::Idle;
            commands.entity(text_id).insert(TextReadWriteMode::ReadOnly);
            press.propagate(false);
        } else if drag_state.mode == EditMode::Editing {
            // Let events propagate to text edit if editing mode.
            commands.entity(text_id).insert(TextReadWriteMode::Editable);
        } else {
            drag_state.mode = EditMode::Scrubbing;
            commands.entity(text_id).insert(TextReadWriteMode::Static);
            press.propagate(false);
        }
    }
}

fn scrubber_on_release(
    mut release: On<PointerRelease>,
    mut q_text: Query<(
        &mut EditableText,
        &mut DragState,
        &ComputedNode,
        &ComputedUiRenderTargetInfo,
        &UiGlobalTransform,
    )>,
    q_parent: Query<&ChildOf>,
    q_units: Query<(&NumberInputValue, Option<&NumberInputUnits>), Without<InteractionDisabled>>,
    ui_scale: Res<UiScale>,
    units_registry: Res<UnitsRegistry>,
    mut commands: Commands,
) {
    if let Ok(&ChildOf(text_id)) = q_parent.get(release.event_target())
        && let Ok((mut editable_text, mut drag_state, node, target, transform)) =
            q_text.get_mut(text_id)
        && let Ok(&ChildOf(root_id)) = q_parent.get(text_id)
        && let Ok((value, units)) = q_units.get(root_id)
    {
        // If we're in editing mode, let event propagate so that text input can handle it.
        if drag_state.mode == EditMode::Editing {
            return;
        }

        release.propagate(false);

        // Copy of logic from EditableText / text_input, but done on pointer up instead of down.
        if drag_state.max_distance <= DRAG_THRESHOLD_DISTANCE {
            if release.button != PointerButton::Primary {
                return;
            }

            if editable_text.is_composing() {
                // The IME is active; all input needs to be routed there, including pointer presses.
                return;
            }

            let Some(local_pos) = transform.try_inverse().map(|inverse| {
                inverse
                    .transform_point2(release.pointer.position * target.scale_factor() / ui_scale.0)
                    - node.content_box().min
                    + editable_text.viewport.offset
            }) else {
                return;
            };

            drag_state.mode = EditMode::Editing;
            commands
                .entity(text_id)
                .insert(TextReadWriteMode::Editable)
                .insert(EntityCursor::System(bevy_window::SystemCursorIcon::Text));

            // Replace the text before editing; this let's us change the degree symbol (°), which
            // is hard to type, into `d`, which is easier.
            let editable_digits = units_registry.resolve(units).format(*value, true);
            let old_digits = editable_text.value().to_string();
            if old_digits != editable_digits {
                editable_text.queue_edit(TextEdit::SelectAll);
                editable_text.queue_edit(TextEdit::Insert(editable_digits.into()));
            }
            editable_text.queue_edit(TextEdit::MoveToPoint(local_pos));
        }
    }
}

fn scrubber_on_drag_start(
    mut drag_start: On<PointerDragStart>,
    q_root: Query<(
        &NumberInputValue,
        Option<&SoftLimit>,
        Option<&NumberInputPrecision>,
        Option<&NumberInputStep>,
        Has<InteractionDisabled>,
        Option<&ThemeContext>,
    )>,
    mut q_text_input: Query<(&mut BackgroundGradient, &mut DragState)>,
    mut q_scrubber: Query<&ComputedNode>,
    q_parent: Query<&ChildOf>,
    input_focus: Res<InputFocus>,
    theme: Res<UiTheme>,
    mut commands: Commands,
) {
    if let Ok(&ChildOf(text_id)) = q_parent.get(drag_start.event_target())
        && let Ok(&ChildOf(root_id)) = q_parent.get(text_id)
        && let Ok((input_value, soft_limit, precision, step, disabled, theme_context)) =
            q_root.get(root_id)
        && let Ok((mut gradient, mut drag)) = q_text_input.get_mut(text_id)
        && !disabled
        && let Ok(node) = q_scrubber.get_mut(drag_start.event_target())
        && drag.mode == EditMode::Scrubbing
    {
        let slider_size = (node.size().x * node.inverse_scale_factor).max(1.0) as f64;
        drag_start.propagate(false);
        drag.base_value = *input_value;
        drag.max_distance = 0.0;
        drag.value_offset = 0.0f64;
        // Use various heuristics to determine drag speed based on which components are present.
        drag.drag_speed = if let Some(SoftLimit(nrange)) = soft_limit {
            match nrange {
                NumberInputRange::F32(range) => (range.end() - range.start()) as f64 / slider_size,
                NumberInputRange::F64(range) => (range.end() - range.start()) / slider_size,
                NumberInputRange::I32(range) => (range.end() - range.start()) as f64 / slider_size,
                NumberInputRange::I64(range) => (range.end() - range.start()) as f64 / slider_size,
            }
        } else if let Some(NumberInputStep(step)) = step {
            *step * BASE_DRAG_SPEED
        } else if matches!(input_value.format(), NumberFormat::I32 | NumberFormat::I64) {
            // Treat integers as having a step size of 1
            BASE_DRAG_SPEED
        } else if let Some(prec) = precision {
            // Derive from precision
            10.0_f64.powf(-(prec.0 as f64))
        } else {
            // No clues present, so we'll have to guess. Use an adaptive algorithm based on
            // present value; this determines the nearest power of 10 to the current magnitude.
            let m = input_value.as_f64().abs();
            let decade = if m >= 1.0 { m.log10().floor() } else { 0.0 };
            BASE_DRAG_SPEED * 10f64.powf(decade)
        };

        set_slidebar_styles(
            text_id,
            &theme,
            theme_context.map(|tc| tc.0).unwrap_or(SurfaceLevel::Base),
            disabled,
            true,
            false,
            input_focus.get() == Some(text_id),
            &mut gradient,
            &mut commands,
        );
    }
}

fn scrubber_on_drag(
    mut drag: On<PointerDrag>,
    q_root: Query<(
        Option<&SoftLimit>,
        Option<&HardLimit>,
        Option<&NumberInputPrecision>,
        Has<InteractionDisabled>,
        Option<&NumberInputWrap>,
    )>,
    mut q_text_input: Query<&mut DragState>,
    mut q_scrubber: Query<&UiGlobalTransform>,
    q_parent: Query<&ChildOf>,
    mut commands: Commands,
    ui_scale: Res<UiScale>,
    keys: Res<ButtonInput<Key>>,
) {
    if let Ok(&ChildOf(text_id)) = q_parent.get(drag.event_target())
        && let Ok(&ChildOf(root_id)) = q_parent.get(text_id)
        && let Ok((soft_limit, hard_limit, precision, disabled, wrap)) = q_root.get(root_id)
        && let Ok(mut drag_state) = q_text_input.get_mut(text_id)
        && let Ok(transform) = q_scrubber.get_mut(drag.entity)
        && drag_state.mode == EditMode::Scrubbing
    {
        drag_state.max_distance = drag_state.max_distance.max(drag.distance.length());
        drag.propagate(false);
        if !disabled && drag_state.max_distance > DRAG_THRESHOLD_DISTANCE {
            let drag_delta = transform.transform_vector2(drag.delta / ui_scale.0).x;
            let mut delta = drag_delta as f64 * drag_state.drag_speed;
            if keys.pressed(Key::Shift) {
                delta *= 0.1;
            }
            drag_state.value_offset += delta;
            emit_drag_value_change(
                &mut commands,
                root_id,
                soft_limit,
                hard_limit,
                precision,
                wrap,
                &mut drag_state,
                false,
            );
        }
    }
}

fn scrubber_on_drag_end(
    mut drag_end: On<PointerDragEnd>,
    q_root: Query<(
        Option<&SoftLimit>,
        Option<&HardLimit>,
        Option<&NumberInputPrecision>,
        Has<InteractionDisabled>,
        Option<&NumberInputWrap>,
        Option<&ThemeContext>,
    )>,
    mut q_text_input: Query<(&Hovered, &mut BackgroundGradient, &mut DragState)>,
    q_parent: Query<&ChildOf>,
    mut commands: Commands,
    theme: Res<UiTheme>,
) {
    if let Ok(&ChildOf(text_id)) = q_parent.get(drag_end.event_target())
        && let Ok(&ChildOf(root_id)) = q_parent.get(text_id)
        && let Ok((soft_limit, hard_limit, precision, disabled, wrap, theme_context)) =
            q_root.get(root_id)
        && let Ok((&Hovered(hovered), mut gradient, mut drag_state)) = q_text_input.get_mut(text_id)
        && drag_state.mode == EditMode::Scrubbing
    {
        drag_end.propagate(false);
        if !disabled {
            emit_drag_value_change(
                &mut commands,
                root_id,
                soft_limit,
                hard_limit,
                precision,
                wrap,
                &mut drag_state,
                true,
            );
        }
        set_slidebar_styles(
            text_id,
            &theme,
            theme_context.map(|tc| tc.0).unwrap_or(SurfaceLevel::Base),
            disabled,
            false,
            hovered,
            false,
            &mut gradient,
            &mut commands,
        );
    }
}

fn scrubber_on_drag_cancel(
    mut drag_cancel: On<PointerCancel>,
    q_parent: Query<&ChildOf>,
    mut q_text_input: Query<(
        &Hovered,
        &mut BackgroundGradient,
        &mut DragState,
        Option<&ThemeContext>,
    )>,
    theme: Res<UiTheme>,
    input_focus: Res<InputFocus>,
    mut commands: Commands,
) {
    if let Ok(&ChildOf(text_id)) = q_parent.get(drag_cancel.event_target())
        && let Ok((&Hovered(hovered), mut gradient, mut drag_state, theme_context)) =
            q_text_input.get_mut(text_id)
    {
        set_slidebar_styles(
            text_id,
            &theme,
            theme_context.map(|tc| tc.0).unwrap_or(SurfaceLevel::Base),
            false,
            false,
            hovered,
            input_focus.get() == Some(text_id),
            &mut gradient,
            &mut commands,
        );
        drag_cancel.propagate(false);
        drag_state.mode = EditMode::Idle;
    }
}

fn update_slider_pos(
    input_value: &NumberInputValue,
    limit: Option<&SoftLimit>,
    gradient: &mut BackgroundGradient,
) {
    if let [Gradient::Linear(linear_gradient)] = &mut gradient.0[..] {
        let percent_value = if let Some(SoftLimit(range)) = limit {
            (range.thumb_position(*input_value) * 100.0).clamp(0.0, 100.0)
        } else {
            // If there's no soft limit, then don't show the slide bar.
            0.0
        };
        linear_gradient.stops[1].point = percent(percent_value);
        linear_gradient.stops[2].point = percent(percent_value);
    }
}

fn set_slidebar_styles(
    slidebar_id: Entity,
    theme: &UiTheme,
    context: SurfaceLevel,
    disabled: bool,
    pressed: bool,
    hovered: bool,
    focused: bool,
    gradient: &mut BackgroundGradient,
    commands: &mut Commands,
) {
    let bar_color = theme.context_color(
        &if disabled {
            tokens::SLIDER_BAR_DISABLED
        } else if pressed {
            tokens::SLIDER_BAR_PRESSED
        } else if hovered {
            tokens::SLIDER_BAR_HOVER
        } else {
            tokens::SLIDER_BAR
        },
        context,
    );

    let bg_color = theme.context_color(
        &if focused {
            tokens::TEXT_INPUT_BG
        } else if disabled {
            tokens::SLIDER_BG_DISABLED
        } else if pressed {
            tokens::SLIDER_BG_PRESSED
        } else if hovered {
            tokens::SLIDER_BG_HOVER
        } else {
            tokens::SLIDER_BG
        },
        context,
    );

    let font_color_token = match disabled {
        true => tokens::TEXT_INPUT_TEXT_DISABLED,
        false => tokens::TEXT_INPUT_TEXT,
    };

    let cursor_shape = match disabled {
        true => bevy_window::SystemCursorIcon::NotAllowed,
        false => bevy_window::SystemCursorIcon::ColResize,
    };

    if let [Gradient::Linear(linear_gradient)] = &mut gradient.0[..] {
        linear_gradient.stops[0].color = bar_color;
        linear_gradient.stops[1].color = bar_color;
        linear_gradient.stops[2].color = bg_color;
        linear_gradient.stops[3].color = bg_color;
    }

    // Change cursor shape and text color
    commands
        .entity(slidebar_id)
        .try_insert(EntityCursor::System(cursor_shape))
        .try_insert(ThemeTextColor(font_color_token));
}

fn emit_drag_value_change(
    commands: &mut Commands,
    source: Entity,
    soft_limit: Option<&SoftLimit>,
    hard_limit: Option<&HardLimit>,
    precision: Option<&NumberInputPrecision>,
    wrap: Option<&NumberInputWrap>,
    drag_state: &mut DragState,
    is_final: bool,
) {
    // Relative scrub: always measured from the value at drag start.
    let mut value = drag_state.base_value.offset_by(drag_state.value_offset);

    // Dragging is confined to the soft range; typing can still exceed it.
    if let Some(SoftLimit(range)) = soft_limit {
        value = range.clamp(value);
    }
    if let Some(precision) = precision {
        value = precision.round(value);
    }
    // Hard limit is absolute and always applied last.
    let value = match (wrap, hard_limit) {
        (Some(NumberInputWrap::Wrap), Some(HardLimit(range))) => range.wrap(value),
        (Some(NumberInputWrap::Wrap), None) => {
            warn_once!("NumberInputWrap::Wrap specified without a HardLimit; ignoring wrap");
            value
        }
        (_, Some(HardLimit(range))) => range.clamp(value),
        (_, None) => value,
    };

    trigger_value_change(commands, value, source, is_final);
}

fn emit_value_change(
    text_value: String,
    format: NumberFormat,
    units: &'static dyn UnitsFormat,
    source: Entity,
    hard_limit: Option<&HardLimit>,
    wrap: Option<&NumberInputWrap>,
    commands: &mut Commands,
    is_final: bool,
) {
    let text_value = text_value.trim();
    if text_value.is_empty() {
        return;
    }

    let Ok(new_value) = units.parse(text_value.to_owned(), format) else {
        // TODO: should handle errors better than this
        warn!("number input parsing failed, invalid format");
        return;
    };

    let value = match (wrap, hard_limit) {
        (Some(NumberInputWrap::Wrap), Some(HardLimit(range))) => range.wrap(new_value),
        (Some(NumberInputWrap::Wrap), None) => {
            warn_once!("NumberInputWrap::Wrap specified without a HardLimit; ignoring wrap");
            new_value
        }
        (_, Some(HardLimit(range))) => range.clamp(new_value),
        (_, None) => new_value,
    };

    trigger_value_change(commands, value, source, is_final);
}

/// Decompose the input value enum and trigger a [`ValueChange`] with the appropriate generic
/// parameter type based on the enum variant.
fn trigger_value_change(
    commands: &mut Commands,
    value: NumberInputValue,
    source: Entity,
    is_final: bool,
) {
    match value {
        NumberInputValue::F32(value) => commands.trigger(ValueChange {
            source,
            value,
            is_final,
        }),
        NumberInputValue::F64(value) => commands.trigger(ValueChange {
            source,
            value,
            is_final,
        }),
        NumberInputValue::I32(value) => commands.trigger(ValueChange {
            source,
            value,
            is_final,
        }),
        NumberInputValue::I64(value) => commands.trigger(ValueChange {
            source,
            value,
            is_final,
        }),
    }
}

/// Re-apply the slidebar gradient colors for every number input when the theme changes.
fn update_slidebar_styles_theme(
    q_children: Query<&Children>,
    q_number_input: Query<
        (Entity, Has<InteractionDisabled>, Option<&ThemeContext>),
        With<FeathersNumberInput>,
    >,
    // Without<FeathersSlider> to avoid ambiguity with FeathersSlider systems.
    mut q_text_input: Query<
        (&Hovered, &mut BackgroundGradient),
        (With<FeathersTextInput>, Without<FeathersSlider>),
    >,
    theme: Res<UiTheme>,
    input_focus: Res<InputFocus>,
    mut commands: Commands,
) {
    if !theme.is_changed() {
        return;
    }
    for (root_entity, is_disabled, theme_context) in q_number_input.iter() {
        let Some(text_entity) = q_children
            .iter_descendants(root_entity)
            .find(|e| q_text_input.contains(*e))
        else {
            continue;
        };
        if let Ok((&Hovered(hovered), mut gradient)) = q_text_input.get_mut(text_entity) {
            set_slidebar_styles(
                text_entity,
                &theme,
                theme_context.map(|tc| tc.0).unwrap_or(SurfaceLevel::Base),
                is_disabled,
                false,
                hovered,
                input_focus.get() == Some(text_entity),
                &mut gradient,
                &mut commands,
            );
        }
    }
}

/// Re-apply the slidebar gradient colors for every number input when the theme context changes.
fn update_slidebar_styles_context(
    q_children: Query<&Children>,
    q_number_input: Query<
        (Entity, Has<InteractionDisabled>, Option<&ThemeContext>),
        (With<FeathersNumberInput>, Changed<ThemeContext>),
    >,
    // Without<FeathersSlider> to avoid ambiguity with FeathersSlider systems.
    mut q_text_input: Query<
        (&Hovered, &mut BackgroundGradient),
        (With<FeathersTextInput>, Without<FeathersSlider>),
    >,
    theme: Res<UiTheme>,
    input_focus: Res<InputFocus>,
    mut commands: Commands,
) {
    for (root_entity, is_disabled, theme_context) in q_number_input.iter() {
        let Some(text_entity) = q_children
            .iter_descendants(root_entity)
            .find(|e| q_text_input.contains(*e))
        else {
            continue;
        };
        if let Ok((&Hovered(hovered), mut gradient)) = q_text_input.get_mut(text_entity) {
            set_slidebar_styles(
                text_entity,
                &theme,
                theme_context.map(|tc| tc.0).unwrap_or(SurfaceLevel::Base),
                is_disabled,
                false,
                hovered,
                input_focus.get() == Some(text_entity),
                &mut gradient,
                &mut commands,
            );
        }
    }
}

// Updates the visibility of the chevron buttons based on the hover state and value limits.
fn update_chevron_visibility(
    hover: On<Insert<Hovered>>,
    q_hovered: Query<&Hovered>,
    q_parent: Query<&ChildOf>,
    q_number_input: Query<
        (&NumberInputValue, Option<&HardLimit>, Has<SoftLimit>),
        With<FeathersNumberInput>,
    >,
    q_children: Query<&Children>,
    mut q_chevron: Query<
        (
            &mut Node,
            Has<NumberInputDecrement>,
            Has<NumberInputIncrement>,
        ),
        Or<(With<NumberInputDecrement>, With<NumberInputIncrement>)>,
    >,
) {
    let text_input = hover.entity;
    let Ok(hovered) = q_hovered.get(text_input) else {
        return;
    };
    let Ok(&ChildOf(root)) = q_parent.get(text_input) else {
        return;
    };
    let Ok((value, hard_limit, has_soft_limit)) = q_number_input.get(root) else {
        return;
    };
    let base_visible = hovered.0 && !has_soft_limit;

    let (is_at_min, is_at_max) = if let Some(HardLimit(range)) = hard_limit {
        (is_at_limit_min(value, range), is_at_limit_max(value, range))
    } else {
        (false, false)
    };

    for child in q_children.iter_descendants(text_input) {
        if let Ok((mut node, is_dec, is_inc)) = q_chevron.get_mut(child) {
            let disabled_by_limit = (is_at_min && is_dec) || (is_at_max && is_inc);
            node.display = if base_visible && !disabled_by_limit {
                Display::Block
            } else {
                Display::None
            };
        }
    }
}

// Checks if the value is at the minimum limit of the range.
fn is_at_limit_min(value: &NumberInputValue, range: &NumberInputRange) -> bool {
    match (range, value) {
        (NumberInputRange::F32(r), NumberInputValue::F32(v)) => v <= r.start(),
        (NumberInputRange::F64(r), NumberInputValue::F64(v)) => v <= r.start(),
        (NumberInputRange::I32(r), NumberInputValue::I32(v)) => v <= r.start(),
        (NumberInputRange::I64(r), NumberInputValue::I64(v)) => v <= r.start(),
        _ => false,
    }
}

// Checks if the value is at the maximum limit of the range.
fn is_at_limit_max(value: &NumberInputValue, range: &NumberInputRange) -> bool {
    match (range, value) {
        (NumberInputRange::F32(r), NumberInputValue::F32(v)) => v >= r.end(),
        (NumberInputRange::F64(r), NumberInputValue::F64(v)) => v >= r.end(),
        (NumberInputRange::I32(r), NumberInputValue::I32(v)) => v >= r.end(),
        (NumberInputRange::I64(r), NumberInputValue::I64(v)) => v >= r.end(),
        _ => false,
    }
}

/// Trait that represents numerical units such as meters, seconds, degrees, etc.
/// [`UnitsFormat`] objects are static constants; a reference to this object can be associated
/// with a number input widget to allow display and conversion of number values with units.
/// The units are not actually stored with the numeric quantity, and are only used for display
/// and editing.
///
/// A [`UnitsFormat`] must be added to the [`UnitsRegistry`] resource before it can be used.
/// Widgets reference the units indirectly by id, allowing them to be serialized.
pub trait UnitsFormat: Send + Sync {
    /// Unique id of this format, e.g. ``"length_meters"``. This will be stored in the
    /// [`NumberInputUnits`] component and used to locate this object in the units registry.
    ///
    ///  The naming convention is `<dimension>_<preferred_display_unit>`, so examples are
    /// `length_meters`, `time_seconds`, or `angle_degrees`.
    fn id(&self) -> &'static str;

    /// Format the value as a string, with the default units. The `editing` parameter allows us
    /// to format the string differently when the user is editing than when displaying or scrubbing.
    /// This can be used, for example, to transform the unicode degree symbol, (°), which is hard
    /// to type, into something easier.
    fn format(&self, value: NumberInputValue, editing: bool) -> String;

    /// Parse the input value into a numerical quantity. If the string includes a units signifier,
    /// convert the value into the canonical units.
    fn parse(&self, value: String, fmt: NumberFormat) -> Result<NumberInputValue, String>;
}

/// A [`UnitsFormat`] meaning "no units", which is the default units. This just does straight-up
/// number parsing and formatting with no suffix.
pub struct Dimensionless;

impl UnitsFormat for Dimensionless {
    fn id(&self) -> &'static str {
        "none"
    }

    fn format(&self, value: NumberInputValue, _editing: bool) -> String {
        value.to_string()
    }

    fn parse(&self, value: String, fmt: NumberFormat) -> Result<NumberInputValue, String> {
        NumberInputValue::parse_from(value.as_str(), fmt)
    }
}

/// Contains the definition of a "standard metrical unit". The [`StandardUnitKind`] struct maintains
/// a table of these items.
pub struct StandardUnitItem {
    /// e.g. ["km"], or ["cm"], first entry is canonical
    pub suffixes: &'static [&'static str],
    /// multiply a value in this unit to get the base unit (meters, radians, kg...)
    pub scale: f64,
}

/// A trait that provides an implementation of standard metrical units like meters or seconds.
///  The purpose of this struct is to provide an easy and convenient implementation of the
/// [`UnitsFormat`] trait. Whereas [`UnitsFormat`] allows arbitrary transformations between the
/// canonical units and the display units (including advanced use cases like log-scale conversion),
/// the [`StandardUnitKind`] just maintains a fixed, flat table of units, with an associated scale
/// factor for each one.
pub trait StandardUnitKind: Send + Sync {
    /// Lookup key for this unit type. The naming convention is `<dimension>_<preferred_display_unit>`,
    /// so examples are `length_meters`, `time_seconds`, or `angle_degrees`.
    ///
    /// This lookup key gets stored in the [`NumberInputUnits`] component.
    const ID: &'static str;

    /// Returns a reference to the table of units. Each entry in the table represents a single
    /// unit. For each unit, there's a scaling factor and a list of possible suffixes.
    fn units(&self) -> &'static [StandardUnitItem];

    /// Identifies the index of the table entry representing the canonical unit. This is the unit
    /// that is used for the value's internal representation, which is contained in the
    /// [`ValueChange`] event. For example, the canonical unit of [`LengthMeters`] is meters,
    /// suffix "m", which is table index 0.
    fn canonical_index(&self) -> usize {
        0
    }

    /// Returns the table index of the preferred display unit (as an index in the units table).
    /// Most of the time this will be the same as the canonical unit; however in some cases
    /// (such as angles), an alternate unit may be more user-friendly (e.g. degrees instead of
    /// radians).
    ///
    /// For example, the display unit for [`AngleDegrees`] is degrees, even though the canonical
    /// unit is radians.
    fn display_index(&self) -> usize {
        self.canonical_index()
    }

    /// Returns the table index of the preferred editing unit (as an index in the units table).
    /// Most of the time this will be the same as the canonical unit, or the display unit;
    /// however, in cases where the display unit is hard to type (like ° or μs), this lets us
    /// choose a different suffix.
    fn editing_index(&self) -> usize {
        self.display_index()
    }
}

/// Blanket impl of [`UnitsFormat`] for [`StandardUnitKind`]
impl<S: StandardUnitKind> UnitsFormat for S {
    fn id(&self) -> &'static str {
        Self::ID
    }

    fn format(&self, value: NumberInputValue, editing: bool) -> String {
        let units_table = self.units();
        let display_unit = &units_table[if editing {
            self.editing_index()
        } else {
            self.display_index()
        }];
        let canonical_unit = &units_table[self.canonical_index()];

        // Convert from canonical units to display units. This is the inverse of the scaling
        // applied in `parse`, which multiplies by `editing.scale / canonical.scale`.
        let scale = canonical_unit.scale / display_unit.scale;

        // Format at the value's native precision.
        let display_value = match value {
            NumberInputValue::F32(v) => format!("{}", (v as f64 * scale) as f32),
            NumberInputValue::F64(v) => format!("{}", v * scale),
            NumberInputValue::I32(v) => format!("{}", v as f64 * scale),
            NumberInputValue::I64(v) => format!("{}", v as f64 * scale),
        };

        // Append the display suffix
        format!("{}{}", display_value, display_unit.suffixes[0])
    }

    fn parse(&self, value: String, fmt: NumberFormat) -> Result<NumberInputValue, String> {
        // Parse the input string, isolate the units token, find matching unit in table.
        // This will eventually be a parser to support complex exprs.
        let trimmed = value.trim();

        // Find where the numeric part ends (after the last digit, decimal point, or exponent)
        let mut magnitude_end = 0;
        let mut has_dot = false;
        let mut has_exp = false;

        // TODO: replace with a better parser, once we settle on a standard parser for Bevy.
        for (i, c) in trimmed.chars().enumerate() {
            match c {
                '0'..='9' | '+' | '-' => {
                    magnitude_end = i + 1;
                }
                '.' if !has_dot && !has_exp => {
                    has_dot = true;
                    magnitude_end = i + 1;
                }
                'e' | 'E' if !has_exp => {
                    has_exp = true;
                    magnitude_end = i + 1;
                }
                ' ' | '\t' => {
                    // Allow whitespace, but don't extend magnitude_end
                }
                _ => {
                    // First non-numeric character found
                    break;
                }
            }
        }

        let magnitude_str = trimmed[..magnitude_end].trim();
        let suffix_str = trimmed[magnitude_end..].trim();

        // Parse the magnitude
        let magnitude = NumberInputValue::parse_from(magnitude_str, fmt)?;

        // Find matching unit suffix
        let units_table = self.units();
        let canonical_unit = &units_table[self.canonical_index()];

        let editing_unit = if suffix_str.is_empty() {
            // If no suffix provided, use the default display unit
            &units_table[self.display_index()]
        } else {
            // Search for matching suffix in any unit
            units_table
                .iter()
                .find(|unit| unit.suffixes.contains(&suffix_str))
                .ok_or_else(|| format!("Unknown unit suffix: '{}'", suffix_str))?
        };

        // Scale by editing units
        let scale_factor = editing_unit.scale / canonical_unit.scale;
        Ok(magnitude.scale_by(scale_factor))
    }
}

/// Unit representing length, where the preferred unit is meters.
pub struct LengthMeters;

impl StandardUnitKind for LengthMeters {
    const ID: &'static str = "length_meters";

    fn units(&self) -> &'static [StandardUnitItem] {
        &[
            // Metric
            StandardUnitItem {
                suffixes: &["m"],
                scale: 1.0,
            },
            StandardUnitItem {
                suffixes: &["km"],
                scale: 1000.0,
            },
            StandardUnitItem {
                suffixes: &["cm"],
                scale: 0.01,
            },
            StandardUnitItem {
                suffixes: &["mm"],
                scale: 0.001,
            },
            // Imperial
            StandardUnitItem {
                suffixes: &["ft"],
                scale: 0.3048,
            },
            StandardUnitItem {
                suffixes: &["in"],
                scale: 0.0254,
            },
            StandardUnitItem {
                suffixes: &["mi"],
                scale: 1609.34,
            },
        ]
    }
}

/// Unit representing time, where the preferred unit is seconds.
pub struct TimeSeconds;

impl StandardUnitKind for TimeSeconds {
    const ID: &'static str = "time_seconds";

    fn units(&self) -> &'static [StandardUnitItem] {
        &[
            StandardUnitItem {
                suffixes: &["s"],
                scale: 1.0,
            },
            StandardUnitItem {
                suffixes: &["ks"],
                scale: 1000.0,
            },
            StandardUnitItem {
                suffixes: &["cs"],
                scale: 0.01,
            },
            StandardUnitItem {
                suffixes: &["ms"],
                scale: 0.001,
            },
        ]
    }
}

/// Unit representing an angle. Internally this is represented as radians, but the preferred
/// display format is degrees. This will display the degree symbol (°) except when in editing
/// mode, in which case it will use the suffix "d" which is easier to type.
pub struct AngleDegrees;

impl StandardUnitKind for AngleDegrees {
    const ID: &'static str = "angle_degrees";

    fn units(&self) -> &'static [StandardUnitItem] {
        &[
            StandardUnitItem {
                suffixes: &["rad"],
                scale: 1.0,
            },
            StandardUnitItem {
                suffixes: &["°"],
                scale: std::f64::consts::PI / 180.0f64,
            },
            StandardUnitItem {
                suffixes: &["d", "deg"],
                scale: std::f64::consts::PI / 180.0f64,
            },
        ]
    }

    /// Override display to show degrees, not radians
    fn display_index(&self) -> usize {
        1
    }

    /// When editing, show "d" rather than degree symbol.
    fn editing_index(&self) -> usize {
        2
    }
}

/// Resource which contains registry of various units, which allows unit definitions to be serialized
/// and references in the editor.
#[derive(Resource, Default)]
pub struct UnitsRegistry(HashMap<&'static str, &'static dyn UnitsFormat>);

impl UnitsRegistry {
    /// Register a new [`UnitsFormat`].
    pub fn insert(&mut self, unit_format: &'static dyn UnitsFormat) -> &mut Self {
        self.0.insert(unit_format.id(), unit_format);
        self
    }

    /// Look up a [`UnitsFormat`] by name, defaulting to [`Dimensionless`] if it cannot be found,
    /// or if the name is not present.
    pub fn resolve(&self, units: Option<&NumberInputUnits>) -> &'static dyn UnitsFormat {
        match units {
            None => &Dimensionless,
            Some(NumberInputUnits(name)) => {
                self.0.get(name.as_str()).copied().unwrap_or_else(|| {
                    warn_once!(
                        "Unit format '{}' not found in registry, using Dimensionless",
                        name
                    );
                    &Dimensionless
                })
            }
        }
    }
}

/// Plugin which keeps number-input slidebar colors in sync with the theme.
pub struct NumberInputPlugin;

impl Plugin for NumberInputPlugin {
    fn build(&self, app: &mut bevy_app::App) {
        let mut registry = UnitsRegistry::default();
        registry.insert(&Dimensionless);
        registry.insert(&LengthMeters);
        registry.insert(&TimeSeconds);
        registry.insert(&AngleDegrees);
        app.insert_resource(registry);

        app.add_systems(
            PreUpdate,
            (update_slidebar_styles_context, update_slidebar_styles_theme)
                .chain()
                .in_set(PickingSystems::Last)
                // After Dispatch systems so that these systems use the most updated `InputFocus`.
                .after(InputFocusSystems::Dispatch),
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bevy_ecs::world::World;

    #[test]
    fn test_length_meters_format() {
        let length = LengthMeters;
        let value = NumberInputValue::F64(100.0);
        let formatted = length.format(value, false);
        assert_eq!(formatted, "100m");
    }

    #[test]
    fn test_length_meters_parse() {
        let length = LengthMeters;
        let result = length.parse("100m".to_string(), NumberFormat::F64);
        match result.unwrap() {
            NumberInputValue::F64(val) => assert!((val - 100.0).abs() < 1e-6),
            _ => panic!("Expected F64 variant"),
        }
    }

    #[test]
    fn test_length_meters_convert_km_to_m() {
        let length = LengthMeters;
        let result = length.parse("1km".to_string(), NumberFormat::F64);
        assert!(result.is_ok());
        match result.unwrap() {
            NumberInputValue::F64(val) => assert!((val - 1000.0).abs() < 1e-6),
            _ => panic!("Expected F64 variant"),
        }
    }

    #[test]
    fn test_length_meters_convert_mm_to_m() {
        let length = LengthMeters;
        let result = length.parse("1mm".to_string(), NumberFormat::F64);
        match result.unwrap() {
            NumberInputValue::F64(val) => assert!((val - 0.001).abs() < 1e-6),
            _ => panic!("Expected F64 variant"),
        }
    }

    #[test]
    fn test_angle_degrees_format() {
        let angle = AngleDegrees;
        let value = NumberInputValue::F64(std::f64::consts::PI / 4.0); // 45 degrees in radians
        let formatted = angle.format(value, false);
        assert_eq!(formatted, "45°");

        let formatted = angle.format(value, true);
        assert_eq!(formatted, "45d");
    }

    #[test]
    fn test_angle_degrees_parse() {
        let angle = AngleDegrees;
        let result = angle.parse("45d".to_string(), NumberFormat::F64);
        match result.unwrap() {
            NumberInputValue::F64(val) => assert!((val - std::f64::consts::PI / 4.0).abs() < 1e-6),
            _ => panic!("Expected F64 variant"),
        }
    }

    #[test]
    fn test_angle_degrees_convert_rad_to_deg() {
        let angle = AngleDegrees;
        let result = angle.parse("1rad".to_string(), NumberFormat::F64);
        match result.unwrap() {
            NumberInputValue::F64(val) => assert!((val - 1.0).abs() < 1e-6),
            _ => panic!("Expected F64 variant"),
        }
    }

    #[test]
    fn test_angle_degrees_parse_deg_suffix() {
        let angle = AngleDegrees;
        let result = angle.parse("90deg".to_string(), NumberFormat::F64);
        match result.unwrap() {
            NumberInputValue::F64(val) => assert!((val - std::f64::consts::PI / 2.0).abs() < 1e-6),
            _ => panic!("Expected F64 variant"),
        }
    }

    #[test]
    fn test_parse_negative_numbers() {
        let length = LengthMeters;
        let result = length.parse("-100".to_string(), NumberFormat::F64);
        match result.unwrap() {
            NumberInputValue::F64(val) => assert!((val - (-100.0)).abs() < 1e-6),
            _ => panic!("Expected F64 variant"),
        }

        let result = length.parse("-50.5cm".to_string(), NumberFormat::F64);
        match result.unwrap() {
            NumberInputValue::F64(val) => assert!((val - (-0.505)).abs() < 1e-6),
            _ => panic!("Expected F64 variant"),
        }
    }

    #[test]
    fn despawn_disabled_number_input_does_not_panic() {
        let mut world = World::new();
        world.init_resource::<UiTheme>();
        world.init_resource::<InputFocus>();

        let text_id = world
            .spawn((
                Hovered::default(),
                BackgroundGradient(vec![Gradient::Linear(LinearGradient {
                    angle: 0.0,
                    stops: vec![
                        ColorStop::new(Color::NONE, percent(0)),
                        ColorStop::new(Color::NONE, percent(50)),
                        ColorStop::new(Color::NONE, percent(50)),
                        ColorStop::new(Color::NONE, percent(100)),
                    ],
                    color_space: InterpolationColorSpace::Srgba,
                })]),
            ))
            .id();

        let number_input = world
            .spawn((FeathersNumberInput, InteractionDisabled))
            .add_child(text_id)
            .id();

        world
            .entity_mut(number_input)
            .observe(number_input_on_insert_disabled)
            .observe(number_input_on_remove_disabled);

        world.despawn(number_input);
        world.flush();
    }
}