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
use alloc::sync::Arc;

use bevy_app::{Plugin, PostUpdate};
use bevy_camera::visibility::Visibility;
use bevy_color::{Alpha, Color, Hsla, Srgba};
use bevy_ecs::{
    change_detection::{DetectChanges, DetectChangesMut},
    component::Component,
    entity::Entity,
    event::EntityEvent,
    hierarchy::{ChildOf, Children},
    lifecycle::Despawn,
    observer::On,
    query::{Changed, With},
    reflect::ReflectComponent,
    relationship::Relationship,
    resource::Resource,
    schedule::IntoScheduleConfigs,
    system::{Commands, Query, Res, ResMut},
    template::FromTemplate,
    world::Mut,
};
use bevy_input::keyboard::{KeyCode, KeyboardInput};
use bevy_input_focus::{
    tab_navigation::TabIndex, AutoFocus, FocusCause, FocusLost, FocusedInput, InputFocus,
};
use bevy_log::warn;
use bevy_math::{UVec2, Vec2, Vec3};
use bevy_reflect::{prelude::ReflectDefault, Reflect};
use bevy_scene::{prelude::*, Ready};
use bevy_text::{EditableText, FontSize, Justify, LineHeight, TextEdit, TextLayout};
use bevy_ui::{
    prelude::AccessibleLabel, px, AlignItems, AlignSelf, Display, FlexDirection, GridPlacement,
    GridTrack, JustifySelf, Node, RepeatedGridTrack,
};
use bevy_ui_widgets::{
    popover::{Popover, PopoverAlign, PopoverPlacement, PopoverSide},
    Activate, ActivateOnPress, SliderValue, ValueChange,
};

use crate::{
    constants::fonts,
    controls::{
        ButtonVariant, ColorChannel, ColorPlaneValue, ColorSlider, ColorSwatchGridUpdate,
        ColorSwatchValue, ColorWheelValue, FeathersButton, FeathersColorPlane, FeathersColorSlider,
        FeathersColorSwatch, FeathersColorSwatchGrid, FeathersColorWheel, FeathersLazyMenu,
        FeathersMenuPopup, FeathersMenuToolButton, FeathersNumberInput, FeathersTextInput,
        FeathersTextInputContainer, HardLimit, NumberInputPrecision, NumberInputRange,
        NumberInputStep, NumberInputValue, SliderBaseColor,
    },
    display::{caption, label},
    font_styles::InheritableFont,
    rounded_corners::RoundedCorners,
};

const RECENT_COLORS_COLUMNS: u32 = 12;
const RECENT_COLORS_ROWS: u32 = 2;
const RECENT_COLORS_COUNT: usize = (RECENT_COLORS_COLUMNS * RECENT_COLORS_ROWS) as usize;

/// Component that contains the value of the color input.
#[derive(Component, Default, Clone, Reflect)]
#[reflect(Component, Clone, Default)]
#[component(immutable)]
pub struct ColorInputValue(pub Color);

/// Supported color editing modes
#[derive(Default, Clone, Copy, Reflect, PartialEq)]
pub enum ColorInputMode {
    /// Hue/saturation/lightness mode with color wheel
    #[default]
    Wheel,
    /// Red/green/blue mode with R/G plane
    RGPlane,
    /// Hue/saturation/lightness mode with H/S plane
    HSPlane,
}

/// Resource that contains user preferences for the color input.
/// This is global (shared between all picker instances), because it's
/// a user preference.
///
/// This means that the choice of color space is "sticky": when the user chooses RGB or HSL modes,
/// the next time the picker opens, even if it's editing a different entity or color attribute,
/// the picker will be in that mode. The assumption is that artists have a preferred mode and will
/// generally stick with it. (Also, we have no way to store the user's preference on a
/// per-attribute basis.)
#[derive(Resource, Default)]
// #[derive(SettingsGroup)] // TODO
pub struct ColorInputSettings {
    /// Which color space we're editing
    pub mode: ColorInputMode,
    /// List of recently edited colors
    pub recent_colors: Vec<Color>,
}

impl ColorInputSettings {
    /// Push the new color onto the head of the list, and remove any duplicate entries.
    /// Also trim the list size to 24 items, which is the size of the grid.
    pub fn add_recent_color(&mut self, color: Color) {
        if let Some(index) = self
            .recent_colors
            .iter()
            .position(|existing| *existing == color)
        {
            self.recent_colors.remove(index);
        }

        self.recent_colors.insert(0, color);
        self.recent_colors.truncate(RECENT_COLORS_COUNT);
    }
}

/// A button which displays a color swatch; when clicked, it displays a popup containing
/// a color picker.
///
/// This is spawnable by inheriting it as a "scene component" with optional
/// [`FeathersColorInputProps`].
///
/// The picker contains a number of user preferences which are sticky, and which
/// are stored on the [`ColorInputSettings`] resource. If the bevy settings plugin in installed,
/// these user preferences will be saved along with the user's settings.
#[derive(SceneComponent, Default, Clone, Reflect)]
#[reflect(Component, Clone, Default)]
#[scene(FeathersColorInputProps)]
#[require(ColorInputValue)]
pub struct FeathersColorInput;

/// Props used to construct a [`FeathersColorInput`] scene.
#[derive(Default)]
pub struct FeathersColorInputProps {
    /// Set a percentage of the swatch to display the opaque version of the
    /// current color.
    pub opaque_color_percentage: f32,
}

/// Component which stores references to the button swatch entity.
#[derive(Component, Clone, FromTemplate, Debug)]
struct ButtonEntityRefs(Entity);

/// Component which stores references to all the various internal widgets so that we don't have
/// to trawl the hierarchy looking for them.
#[derive(Component, Clone, Debug, FromTemplate)]
struct PopupEntityRefs {
    mode_wheel: Entity,
    mode_rgb: Entity,
    mode_hsl: Entity,
    wheel: Entity,
    rg_plane: Entity,
    hs_plane: Entity,

    rgb_group: Entity,
    hsl_group: Entity,

    r_slider: Entity,
    r_input: Entity,
    g_slider: Entity,
    g_input: Entity,
    b_slider: Entity,
    b_input: Entity,

    h_slider: Entity,
    h_input: Entity,
    s_slider: Entity,
    s_input: Entity,
    l_slider: Entity,
    l_input: Entity,

    a_slider: Entity,
    a_input: Entity,

    hex_input_container: Entity,
    hex_input: Entity,
    swatch: Entity,
    recent: Entity,
}

/// Which color model is the current source of truth
#[derive(Default, Clone, Copy, Debug, PartialEq)]
enum SourceColorSpace {
    /// RGB
    #[default]
    Rgb,
    /// HSL
    Hsl,
}

/// Used to track the current color value and which color space we are editing. We don't use
/// the [`Color`] enum here for reasons explained below.
#[derive(Component, Clone, Debug, FromTemplate)]
struct ColorInputState {
    // Which color space is the source of truth
    source: SourceColorSpace,

    /// The current RGB color
    rgb: Srgba,

    /// The current HSL color
    hsl: Hsla,
}

impl ColorInputState {
    /// Synchronize the colors when switching color spaces.
    ///
    /// The color source is determined by which panel is shown, whether it's an RGB mode panel,
    /// an HSL mode panel, or any additional supported color sources that may be added later.
    /// When controls are interacted with, only the current source is updated; when we switch
    /// color sources, the new source is updated from the previous source.
    ///
    /// This maintains separate color values for each color space, rather than a single union, so
    /// that we can prevent lossy conversions when switching color spaces. For example, an RGB color
    /// that has no saturation (black, white or gray) has an indeterminate hue. We do this by only
    /// overwriting a color model's channels when they're well-defined in the source color,
    /// preserving the previous value otherwise.
    fn change_source(&mut self, next_source: SourceColorSpace) {
        if next_source != self.source {
            match self.source {
                SourceColorSpace::Rgb => {
                    let hsl = Hsla::from(self.rgb);

                    // Copy `hsl` into `self.hsl`, but leave indeterminate channels unchanged from
                    // the previous value. We only adopt a channel when the channels it depends on
                    // are well-defined; otherwise we keep the old values.
                    const EPSILON: f32 = 1e-6;

                    // Lightness and alpha are always well-defined.
                    self.hsl.lightness = hsl.lightness;
                    self.hsl.alpha = hsl.alpha;

                    // Saturation is indeterminate for pure black and pure white.
                    if hsl.lightness > EPSILON && hsl.lightness < 1.0 - EPSILON {
                        self.hsl.saturation = hsl.saturation;

                        // Hue is indeterminate for grays (zero saturation).
                        if hsl.saturation > EPSILON {
                            self.hsl.hue = hsl.hue;
                        }
                    }
                }

                SourceColorSpace::Hsl => {
                    // Convert HSL to RGB
                    self.rgb = Srgba::from(self.hsl);
                }
            }
            self.source = next_source;
        }
    }

    /// Set a single channel, returning the resulting color in the current source space.
    fn set_channel(&mut self, channel: ColorChannel, value: f32) -> Color {
        match channel {
            ColorChannel::Red => {
                self.rgb.red = value;
                self.rgb.into()
            }
            ColorChannel::Green => {
                self.rgb.green = value;
                self.rgb.into()
            }
            ColorChannel::Blue => {
                self.rgb.blue = value;
                self.rgb.into()
            }
            ColorChannel::HslHue => {
                self.hsl.hue = value;
                self.hsl.into()
            }
            ColorChannel::HslSaturation => {
                self.hsl.saturation = value;
                self.hsl.into()
            }
            ColorChannel::HslLightness => {
                self.hsl.lightness = value;
                self.hsl.into()
            }
            ColorChannel::Alpha => match self.source {
                SourceColorSpace::Rgb => {
                    self.rgb.alpha = value;
                    self.rgb.into()
                }
                SourceColorSpace::Hsl => {
                    self.hsl.alpha = value;
                    self.hsl.into()
                }
            },
            _ => unreachable!("Unsupported color plane type"),
        }
    }

    /// Return a [`Color`] representing the current source color.
    fn to_color(&self) -> Color {
        match self.source {
            SourceColorSpace::Rgb => Color::from(self.rgb),
            SourceColorSpace::Hsl => Color::from(self.hsl),
        }
    }

    /// Return the alpha component of the current source color
    fn to_alpha(&self) -> f32 {
        match self.source {
            SourceColorSpace::Rgb => self.rgb.alpha,
            SourceColorSpace::Hsl => self.hsl.alpha,
        }
    }
}

/// Marks the number input so that we know what channel it's editing.
#[derive(Component, Default, Clone)]
struct NumberInputChannel(ColorChannel);

impl FeathersColorInput {
    fn scene(props: FeathersColorInputProps) -> impl Scene {
        let popup: Arc<dyn Fn() -> Box<dyn Scene> + Sync + Send> = Arc::new(color_input_popup);
        bsn! {
            @FeathersLazyMenu { popup }
            ButtonEntityRefs(#swatch)
            ColorInputState
            Children [
                @FeathersMenuToolButton {
                    @caption: bsn! {
                        #swatch
                        @FeathersColorSwatch {
                            @opaque_color_percentage: {props.opaque_color_percentage},
                            @corners: RoundedCorners::None,
                        }
                        Node {
                            width: px(18),
                            min_width: px(18),
                            height: px(18),
                            flex_grow: 0.0,
                            align_self: AlignSelf::Center,
                            justify_self: JustifySelf::Start,
                        }
                        }
                }
            ]
        }
    }
}

// Lazily-constructed menu popup
fn color_input_popup() -> Box<dyn Scene> {
    Box::new(bsn! {
        @FeathersMenuPopup
        PopupEntityRefs {
            mode_wheel: #mode_wheel,
            mode_rgb: #mode_rgb,
            mode_hsl: #mode_hsl,

            wheel: #wheel,
            rg_plane: #rg_plane,
            hs_plane: #hs_plane,

            rgb_group: #rgb_group,
            hsl_group: #hsl_group,

            r_slider: #r_slider,
            r_input: #r_input,
            g_slider: #g_slider,
            g_input: #g_input,
            b_slider: #b_slider,
            b_input: #b_input,

            h_slider: #h_slider,
            h_input: #h_input,
            s_slider: #s_slider,
            s_input: #s_input,
            l_slider: #l_slider,
            l_input: #l_input,

            a_slider: #a_slider,
            a_input: #a_input,

            hex_input_container: #hex_input_container,
            hex_input: #hex_input,
            swatch: #swatch,
            recent: #recent,
        }
        Node {
            width: px(256 + 18), // room for 256 px wide plane widgets + padding/border
            row_gap: px(4),
            padding: px(4)
        }
        TabIndex
        on(popup_ready)
        on(popup_despawn)
        Popover {
            positions: vec![
                PopoverPlacement {
                    side: PopoverSide::Bottom,
                    align: PopoverAlign::End,
                    gap: 2.0,
                },
                PopoverPlacement {
                    side: PopoverSide::Bottom,
                    align: PopoverAlign::Start,
                    gap: 2.0,
                },
                PopoverPlacement {
                    side: PopoverSide::Right,
                    align: PopoverAlign::Center,
                    gap: 2.0,
                },
                PopoverPlacement {
                    side: PopoverSide::Left,
                    align: PopoverAlign::Center,
                    gap: 2.0,
                },
                PopoverPlacement {
                    side: PopoverSide::Top,
                    align: PopoverAlign::End,
                    gap: 2.0,
                },
                PopoverPlacement {
                    side: PopoverSide::Top,
                    align: PopoverAlign::Start,
                    gap: 2.0,
                },
            ],
            window_margin: 10.0,
        }
        Children [
            Node {
                display: Display::Flex,
                flex_direction: FlexDirection::Row,
                column_gap: px(1),
            }
            Children [
                #mode_wheel
                @FeathersButton {
                    @caption: bsn! {
                        @caption("\u{1F7D5}")
                        Node {
                            top: px(3), // adjust to be visually centered vertically
                        }
                    },
                    @corners: RoundedCorners::Left,
                }
                InheritableFont {
                    font: "embedded://bevy_feathers/assets/fonts/NotoSansSymbols2-U+1F7D5.ttf",
                    font_size: FontSize::Px(17.0),
                }
                Node {
                    flex_grow: 1.0,
                }
                ActivateOnPress
                AccessibleLabel("Wheel")
                on(|_activate: On<Activate>, mut settings: ResMut<ColorInputSettings>| {
                    settings.mode = ColorInputMode::Wheel;
                })
                --
                #mode_rgb
                @FeathersButton {
                    @caption: bsn! { @caption("RGB") },
                    @corners: RoundedCorners::None,
                }
                Node {
                    flex_grow: 1.0,
                }
                ActivateOnPress
                AccessibleLabel("RGB")
                on(|_activate: On<Activate>, mut settings: ResMut<ColorInputSettings>| {
                    settings.mode = ColorInputMode::RGPlane;
                })
                AutoFocus
                --
                #mode_hsl
                @FeathersButton {
                    @caption: bsn! { @caption("HSL") },
                    @corners: RoundedCorners::Right,
                }
                Node {
                    flex_grow: 1.0,
                }
                ActivateOnPress
                AccessibleLabel("HSL")
                on(|_activate: On<Activate>, mut settings: ResMut<ColorInputSettings>| {
                    settings.mode = ColorInputMode::HSPlane;
                })
            ]
            --
            #wheel
            @FeathersColorWheel
            Node {
                width: px(256 + 8),
                height: px(256 + 8),
            }
            on(color_wheel_value_change)
            --
            #rg_plane
            @FeathersColorPlane::RedGreen
            Node {
                width: px(256 + 8),
                height: px(256 + 8),
            }
            on(rg_color_plane_value_change)
            --
            #hs_plane
            @FeathersColorPlane::HueSaturation
            Node {
                width: px(256 + 8),
                height: px(256 + 8),
            }
            on(hs_color_plane_value_change)
            --
            #rgb_group
            Node {
                display: Display::Grid,
                column_gap: px(4),
                row_gap: px(4),
                grid_template_columns: vec![
                    RepeatedGridTrack::auto(1),
                    RepeatedGridTrack::flex(1, 1.),
                    RepeatedGridTrack::px(1, 50.),
                ],
                grid_auto_rows: vec![GridTrack::auto()],
                align_items: AlignItems::Center,
            }
            Children [
                @label("R")
                --
                #r_slider
                @FeathersColorSlider {
                    @value: 0.5,
                    @channel: ColorChannel::Red
                }
                AccessibleLabel("Red Channel")
                on(color_slider_value_change)
                --
                #r_input
                @FeathersNumberInput
                NumberInputValue::F32(0.0)
                HardLimit(NumberInputRange::F32(0.0..=255.0))
                NumberInputPrecision(1)
                NumberInputStep(20.0)
                NumberInputChannel(ColorChannel::Red)
                on(number_input_value_change)
                --
                @label("G")
                --
                #g_slider
                @FeathersColorSlider {
                    @value: 0.5,
                    @channel: ColorChannel::Green
                }
                AccessibleLabel("Green Channel")
                on(color_slider_value_change)
                --
                #g_input
                @FeathersNumberInput
                NumberInputValue::F32(0.0)
                HardLimit(NumberInputRange::F32(0.0..=255.0))
                NumberInputPrecision(1)
                NumberInputStep(20.0)
                NumberInputChannel(ColorChannel::Green)
                on(number_input_value_change)
                --
                @label("B")
                --
                #b_slider
                @FeathersColorSlider {
                    @value: 0.5,
                    @channel: ColorChannel::Blue
                }
                AccessibleLabel("Blue Channel")
                on(color_slider_value_change)
                --
                #b_input
                @FeathersNumberInput
                NumberInputValue::F32(0.0)
                HardLimit(NumberInputRange::F32(0.0..=255.0))
                NumberInputPrecision(1)
                NumberInputStep(20.0)
                NumberInputChannel(ColorChannel::Blue)
                on(number_input_value_change)
            ]
            --
            #hsl_group
            Node {
                display: Display::Grid,
                column_gap: px(4),
                row_gap: px(4),
                grid_template_columns: vec![
                    RepeatedGridTrack::auto(1),
                    RepeatedGridTrack::flex(1, 1.),
                    RepeatedGridTrack::px(1, 50.),
                ],
                grid_auto_rows: vec![GridTrack::auto()],
                align_items: AlignItems::Center,
            }
            Children [
                @label("H")
                --
                #h_slider
                @FeathersColorSlider {
                    @value: 0.5,
                    @channel: ColorChannel::HslHue
                }
                AccessibleLabel("Hue Channel")
                on(color_slider_value_change)
                --
                #h_input
                @FeathersNumberInput
                NumberInputValue::F32(0.0)
                HardLimit(NumberInputRange::F32(0.0..=360.0))
                NumberInputPrecision(1)
                NumberInputStep(30.0)
                NumberInputChannel(ColorChannel::HslHue)
                Node {
                    flex_grow: 1.0,
                }
                on(number_input_value_change)
                --
                @label("S")
                --
                #s_slider
                @FeathersColorSlider {
                    @value: 0.5,
                    @channel: ColorChannel::HslSaturation
                }
                AccessibleLabel("Saturation Channel")
                on(color_slider_value_change)
                --
                #s_input
                @FeathersNumberInput
                NumberInputValue::F32(0.0)
                HardLimit(NumberInputRange::F32(0.0..=100.0))
                NumberInputPrecision(1)
                NumberInputStep(10.0)
                NumberInputChannel(ColorChannel::HslSaturation)
                Node {
                    flex_grow: 1.0,
                }
                on(number_input_value_change)
                --
                @label("L")
                --
                #l_slider
                @FeathersColorSlider {
                    @value: 0.5,
                    @channel: ColorChannel::HslLightness
                }
                AccessibleLabel("Lightness Channel")
                on(color_slider_value_change)
                --
                #l_input
                @FeathersNumberInput
                NumberInputValue::F32(0.0)
                HardLimit(NumberInputRange::F32(0.0..=100.0))
                NumberInputPrecision(1)
                NumberInputStep(10.0)
                NumberInputChannel(ColorChannel::HslLightness)
                Node {
                    flex_grow: 1.0,
                }
                on(number_input_value_change)
            ]
            --
            Node {
                display: Display::Grid,
                column_gap: px(4),
                row_gap: px(4),
                grid_template_columns: vec![
                    RepeatedGridTrack::auto(1),
                    RepeatedGridTrack::flex(1, 1.),
                    RepeatedGridTrack::px(1, 50.),
                ],
                grid_auto_rows: vec![GridTrack::auto()],
                align_items: AlignItems::Center,
            }
            Children [
                @label("A")
                --
                #a_slider
                @FeathersColorSlider {
                    @value: 0.5,
                    @channel: ColorChannel::Alpha
                }
                AccessibleLabel("Alpha Channel")
                on(color_slider_value_change)
                --
                #a_input
                @FeathersNumberInput
                NumberInputValue::F32(0.0)
                HardLimit(NumberInputRange::F32(0.0..=255.0))
                NumberInputPrecision(1)
                NumberInputStep(10.0)
                NumberInputChannel(ColorChannel::Alpha)
                on(number_input_value_change)
                --
                #hex_input_container
                @FeathersTextInputContainer
                Node {
                    flex_grow: 0.
                    padding: { px(4).left() },
                    grid_column: GridPlacement::span(2),
                }
                Children [
                    #hex_input
                    @FeathersTextInput {
                        @max_characters: 9usize,
                    }
                    TextLayout {
                        justify: Justify::Center,
                    }
                    InheritableFont {
                        font: fonts::MONO
                    }
                    LineHeight::Px(24.0) // TODO: Make const for this
                    on(hex_input_on_enter_key)
                    on(hex_input_on_focus_loss)
                ]
                --
                #swatch
                @FeathersColorSwatch {
                    @opaque_color_percentage: 50.0,
                }
                Node {
                    flex_grow: 0.0,
                    align_self: AlignSelf::Stretch,
                }
            ]
            --
            // Recent colors
            #recent
            @FeathersColorSwatchGrid {
                size: UVec2::new(RECENT_COLORS_COLUMNS, RECENT_COLORS_ROWS),
                opaque_color_percentage: 50.0,
            }
            on(recent_color_selected)
        ]
    })
}

fn color_wheel_value_change(
    change: On<ValueChange<ColorWheelValue>>,
    q_parent: Query<&ChildOf>,
    mut q_state: Query<&mut ColorInputState>,
    mut commands: Commands,
) {
    if let Some((root_id, mut state)) = color_input_state(&q_parent, &mut q_state, change.source) {
        let alpha = state.hsl.alpha;
        state.hsl = change.value.to_hsla().with_alpha(alpha);
        let value: Color = state.hsl.into();
        commands.trigger(ValueChange {
            source: root_id,
            value,
            is_final: change.is_final,
        });
    }
}

fn rg_color_plane_value_change(
    change: On<ValueChange<Vec2>>,
    q_parent: Query<&ChildOf>,
    mut q_state: Query<&mut ColorInputState>,
    mut commands: Commands,
) {
    if let Some((root_id, mut state)) = color_input_state(&q_parent, &mut q_state, change.source) {
        state.rgb.red = change.value.x;
        state.rgb.green = change.value.y;
        let value: Color = state.rgb.into();
        commands.trigger(ValueChange {
            source: root_id,
            value,
            is_final: change.is_final,
        });
    }
}

fn hs_color_plane_value_change(
    change: On<ValueChange<Vec2>>,
    q_parent: Query<&ChildOf>,
    mut q_state: Query<&mut ColorInputState>,
    mut commands: Commands,
) {
    if let Some((root_id, mut state)) = color_input_state(&q_parent, &mut q_state, change.source) {
        state.hsl.hue = change.value.x * 360.0;
        state.hsl.saturation = change.value.y;
        let value: Color = state.hsl.into();
        commands.trigger(ValueChange {
            source: root_id,
            value,
            is_final: change.is_final,
        });
    }
}

fn color_slider_value_change(
    change: On<ValueChange<f32>>,
    q_slider: Query<&ColorSlider>,
    q_parent: Query<&ChildOf>,
    mut q_state: Query<&mut ColorInputState>,
    mut commands: Commands,
) {
    let Ok(slider) = q_slider.get(change.source) else {
        return;
    };

    if let Some((root_id, mut state)) = color_input_state(&q_parent, &mut q_state, change.source) {
        let value = state.set_channel(slider.channel, change.value);
        commands.trigger(ValueChange {
            source: root_id,
            value,
            is_final: change.is_final,
        });
    }
}

fn number_input_value_change(
    change: On<ValueChange<f32>>,
    q_input: Query<&NumberInputChannel>,
    q_parent: Query<&ChildOf>,
    mut q_state: Query<&mut ColorInputState>,
    mut commands: Commands,
) {
    let Ok(channel) = q_input.get(change.source) else {
        return;
    };

    if let Some((root_id, mut state)) = color_input_state(&q_parent, &mut q_state, change.source) {
        let value = state.set_channel(channel.0, change.value / display_scale(channel.0));
        commands.trigger(ValueChange {
            source: root_id,
            value,
            is_final: change.is_final,
        });
    }
}

fn hex_input_on_enter_key(
    key_input: On<FocusedInput<KeyboardInput>>,
    q_text_input: Query<&EditableText>,
    q_parent: Query<&ChildOf>,
    mut q_state: Query<&mut ColorInputState>,
    mut commands: Commands,
) {
    if key_input.input.key_code != KeyCode::Enter {
        return;
    }

    let Ok(editable_text) = q_text_input.get(key_input.event_target()) else {
        return;
    };

    if let Some((root_id, mut state)) =
        color_input_state(&q_parent, &mut q_state, key_input.event_target())
    {
        hex_input_value_change(root_id, &mut state, editable_text, &mut commands);
    }
}

fn hex_input_on_focus_loss(
    focus_lost: On<FocusLost>,
    q_text_input: Query<&EditableText>,
    q_parent: Query<&ChildOf>,
    mut q_state: Query<&mut ColorInputState>,
    mut commands: Commands,
) {
    let Ok(editable_text) = q_text_input.get(focus_lost.event_target()) else {
        return;
    };

    if let Some((root_id, mut state)) =
        color_input_state(&q_parent, &mut q_state, focus_lost.event_target())
    {
        hex_input_value_change(root_id, &mut state, editable_text, &mut commands);
    }
}

fn hex_input_value_change(
    root_id: Entity,
    state: &mut ColorInputState,
    editable_text: &EditableText,
    commands: &mut Commands,
) {
    if let Ok(new_rgb_color) = Srgba::hex(editable_text.value().to_string())
        && state.rgb != new_rgb_color
    {
        state.rgb = new_rgb_color;
        commands.trigger(ValueChange {
            source: root_id,
            value: Color::from(new_rgb_color),
            is_final: true,
        });
    }
    // TODO: We currently have no way to report errors, so silently
    // fail for now.
}

fn color_input_value_change(
    mut q_input: Query<
        (
            &ColorInputValue,
            &mut ColorInputState,
            &ButtonEntityRefs,
            &Children,
        ),
        Changed<ColorInputValue>,
    >,
    q_popup: Query<&PopupEntityRefs, With<FeathersMenuPopup>>,
    mut q_color_wheel: Query<&mut ColorWheelValue>,
    mut q_color_plane: Query<&mut ColorPlaneValue>,
    mut q_editable_text: Query<&mut EditableText>,
    mut grid_update: ColorSwatchGridUpdate,
    settings: Res<ColorInputSettings>,
    mut commands: Commands,
) {
    for (&ColorInputValue(value), mut state, refs, children) in q_input.iter_mut() {
        commands.entity(refs.0).insert(ColorSwatchValue(value));

        // Update either the RGB or HSL value depending on what mode we are in.
        match state.source {
            SourceColorSpace::Rgb => state.rgb = value.into(),
            SourceColorSpace::Hsl => state.hsl = value.into(),
        }

        if let Some(refs) = children
            .iter()
            .find_map(|child_id| q_popup.get(*child_id).ok())
        {
            update_controls(
                &mut q_color_wheel,
                &mut q_color_plane,
                &mut q_editable_text,
                &mut commands,
                refs,
                &state,
            );

            grid_update.update(refs.recent, &settings.recent_colors, Some(value));
        }
    }
}

/// Re-emit a recent color selection from the color input itself, so that it's indistinguishable
/// from any other edit.
fn recent_color_selected(
    change: On<ValueChange<Color>>,
    q_parent: Query<&ChildOf>,
    q_state: Query<&ColorInputState>,
    mut commands: Commands,
) {
    if let Some(root_id) = q_parent
        .iter_ancestors(change.source)
        .find(|e| q_state.contains(*e))
    {
        commands.trigger(ValueChange {
            source: root_id,
            value: change.value,
            is_final: true,
        });
    }
}

fn update_mode_selector(
    q_refs: Query<(Entity, &PopupEntityRefs)>,
    q_parent: Query<&ChildOf>,
    mut q_state: Query<&mut ColorInputState>,
    mut q_button: Query<&mut ButtonVariant>,
    mut q_node: Query<&mut Node>,
    mut q_color_wheel: Query<&mut ColorWheelValue>,
    mut q_color_plane: Query<&mut ColorPlaneValue>,
    mut q_editable_text: Query<&mut EditableText>,
    settings: Res<ColorInputSettings>,
    mut focus: ResMut<InputFocus>,
    mut commands: Commands,
) {
    for (popup_id, refs) in q_refs.iter() {
        set_mode_selector(refs, &mut q_button, settings.mode);
        set_pane_visible(refs, &mut q_node, settings.mode, &mut commands);

        if settings.is_changed()
            && let Some((_, mut state)) = color_input_state(&q_parent, &mut q_state, popup_id)
        {
            // Make sure that the right color model is designated as the source.
            // Also, ensure that focus moves to a widget that is not about to be hidden,
            // as this will auto-close the popup.
            match settings.mode {
                ColorInputMode::Wheel => {
                    state.change_source(SourceColorSpace::Hsl);
                    focus.set(refs.mode_wheel, FocusCause::Auto);
                }
                ColorInputMode::RGPlane => {
                    state.change_source(SourceColorSpace::Rgb);
                    focus.set(refs.mode_rgb, FocusCause::Auto);
                }
                ColorInputMode::HSPlane => {
                    state.change_source(SourceColorSpace::Hsl);
                    focus.set(refs.mode_hsl, FocusCause::Auto);
                }
            }

            update_controls(
                &mut q_color_wheel,
                &mut q_color_plane,
                &mut q_editable_text,
                &mut commands,
                refs,
                &state,
            );
        }
    }
}

fn set_mode_selector(
    refs: &PopupEntityRefs,
    q_button: &mut Query<&mut ButtonVariant>,
    mode: ColorInputMode,
) {
    if let Ok(mut wheel_variant) = q_button.get_mut(refs.mode_wheel) {
        wheel_variant.set_if_neq(ButtonVariant::selected(mode == ColorInputMode::Wheel));
    }

    if let Ok(mut rgb_variant) = q_button.get_mut(refs.mode_rgb) {
        rgb_variant.set_if_neq(ButtonVariant::selected(mode == ColorInputMode::RGPlane));
    }

    if let Ok(mut hsl_variant) = q_button.get_mut(refs.mode_hsl) {
        hsl_variant.set_if_neq(ButtonVariant::selected(mode == ColorInputMode::HSPlane));
    }
}

fn set_pane_visible(
    refs: &PopupEntityRefs,
    q_node: &mut Query<&mut Node>,
    mode: ColorInputMode,
    commands: &mut Commands,
) {
    set_node_visible(
        q_node,
        refs.wheel,
        if mode == ColorInputMode::Wheel {
            Display::Flex
        } else {
            Display::None
        },
        commands,
    );
    set_node_visible(
        q_node,
        refs.rg_plane,
        if mode == ColorInputMode::RGPlane {
            Display::Flex
        } else {
            Display::None
        },
        commands,
    );
    set_node_visible(
        q_node,
        refs.rgb_group,
        if mode == ColorInputMode::RGPlane {
            Display::Grid
        } else {
            Display::None
        },
        commands,
    );
    set_node_visible(
        q_node,
        refs.hex_input_container,
        if mode == ColorInputMode::RGPlane {
            Display::Flex
        } else {
            Display::None
        },
        commands,
    );
    // The swatch fills the row the hex input vacates.
    if let Ok(mut swatch_node) = q_node.get_mut(refs.swatch) {
        let span = if mode == ColorInputMode::RGPlane {
            GridPlacement::auto()
        } else {
            GridPlacement::span(3)
        };
        if swatch_node.grid_column != span {
            swatch_node.grid_column = span;
        }
    }
    set_node_visible(
        q_node,
        refs.hs_plane,
        if mode == ColorInputMode::HSPlane {
            Display::Flex
        } else {
            Display::None
        },
        commands,
    );
    // HSL sliders are shared by the H/S plane and the wheel.
    set_node_visible(
        q_node,
        refs.hsl_group,
        if mode == ColorInputMode::Wheel || mode == ColorInputMode::HSPlane {
            Display::Grid
        } else {
            Display::None
        },
        commands,
    );
}

fn set_node_visible(
    q_node: &mut Query<&mut Node>,
    id: Entity,
    display: Display,
    commands: &mut Commands,
) {
    if let Ok(mut node) = q_node.get_mut(id)
        && node.display != display
    {
        node.display = display;

        // Also set visibility to prevent tab navigation
        commands.entity(id).insert(if display == Display::None {
            Visibility::Hidden
        } else {
            Visibility::Inherited
        });
    }
}

/// Record the edited color in the recent colors palette when the popup closes.
///
/// The popup is despawned by every path which closes it, including ESC, so this is also the
/// commit point for a cancelled edit. That's deliberate: the picker has no commit button, and
/// the recent colors list is a convenience rather than part of the edited document.
fn popup_despawn(
    despawn: On<Despawn<PopupEntityRefs>>,
    q_popup: Query<&ChildOf, With<PopupEntityRefs>>,
    q_color_input: Query<&ColorInputValue>,
    mut settings: ResMut<ColorInputSettings>,
) {
    let Ok(parent) = q_popup.get(despawn.entity) else {
        return;
    };

    // The color input is gone too if the whole widget is being torn down.
    let Ok(ColorInputValue(value)) = q_color_input.get(parent.get()) else {
        return;
    };

    settings.add_recent_color(*value);
}

fn popup_ready(
    ready: On<Ready>,
    q_popup: Query<(&PopupEntityRefs, &ChildOf)>,
    mut q_color_input: Query<(&ColorInputValue, &mut ColorInputState)>,
    mut q_editable_text: Query<&mut EditableText>,
    mut q_color_plane: Query<&mut ColorPlaneValue>,
    mut q_color_wheel: Query<&mut ColorWheelValue>,
    mut grid_update: ColorSwatchGridUpdate,
    settings: Res<ColorInputSettings>,
    mut commands: Commands,
) {
    let Ok((refs, parent)) = q_popup.get(ready.entity) else {
        return;
    };

    let Ok((ColorInputValue(value), mut state)) = q_color_input.get_mut(parent.get()) else {
        warn!("Could not locate popup parent");
        return;
    };

    match settings.mode {
        ColorInputMode::Wheel | ColorInputMode::HSPlane => {
            state.source = SourceColorSpace::Hsl;
            state.hsl = (*value).into();
        }
        ColorInputMode::RGPlane => {
            state.source = SourceColorSpace::Rgb;
            state.rgb = (*value).into();
        }
    }

    update_controls(
        &mut q_color_wheel,
        &mut q_color_plane,
        &mut q_editable_text,
        &mut commands,
        refs,
        &state,
    );

    grid_update.update(refs.recent, &settings.recent_colors, Some(*value));
}

fn update_controls(
    q_color_wheel: &mut Query<'_, '_, &mut ColorWheelValue>,
    q_color_plane: &mut Query<'_, '_, &mut ColorPlaneValue>,
    q_editable_text: &mut Query<'_, '_, &mut EditableText>,
    commands: &mut Commands<'_, '_>,
    refs: &PopupEntityRefs,
    state: &ColorInputState,
) {
    let color = state.to_color();

    if let Ok(mut color_wheel_value) = q_color_wheel.get_mut(refs.wheel) {
        color_wheel_value.set_if_neq(ColorWheelValue::from_hsla(state.hsl));
    }

    if let Ok(mut color_plane_value) = q_color_plane.get_mut(refs.rg_plane) {
        color_plane_value.set_if_neq(ColorPlaneValue(Vec3::new(
            state.rgb.red,
            state.rgb.green,
            state.rgb.blue,
        )));
    }

    if let Ok(mut color_plane_value) = q_color_plane.get_mut(refs.hs_plane) {
        color_plane_value.set_if_neq(ColorPlaneValue(Vec3::new(
            state.hsl.hue / 360.0,
            state.hsl.saturation,
            0.5,
        )));
    }

    commands
        .entity(refs.r_slider)
        .insert(SliderValue(state.rgb.red))
        .insert(SliderBaseColor(color));
    commands
        .entity(refs.g_slider)
        .insert(SliderValue(state.rgb.green))
        .insert(SliderBaseColor(color));
    commands
        .entity(refs.b_slider)
        .insert(SliderValue(state.rgb.blue))
        .insert(SliderBaseColor(color));
    commands
        .entity(refs.a_slider)
        .insert(SliderValue(state.to_alpha()))
        .insert(SliderBaseColor(color));
    commands
        .entity(refs.h_slider)
        .insert(SliderValue(state.hsl.hue))
        .insert(SliderBaseColor(color));
    commands
        .entity(refs.s_slider)
        .insert(SliderValue(state.hsl.saturation))
        .insert(SliderBaseColor(color));
    commands
        .entity(refs.l_slider)
        .insert(SliderValue(state.hsl.lightness))
        .insert(SliderBaseColor(color));

    // Round to nearest tenth, so that the string of digits
    // won't be too long to display in the limited space.
    fn scaled_number_input_value(channel: ColorChannel, native: f32) -> NumberInputValue {
        NumberInputValue::F32((native * display_scale(channel) * 10.0).round() / 10.0)
    }

    commands
        .entity(refs.r_input)
        .insert(scaled_number_input_value(ColorChannel::Red, state.rgb.red));
    commands
        .entity(refs.g_input)
        .insert(scaled_number_input_value(
            ColorChannel::Green,
            state.rgb.green,
        ));
    commands
        .entity(refs.b_input)
        .insert(scaled_number_input_value(
            ColorChannel::Blue,
            state.rgb.blue,
        ));

    commands
        .entity(refs.h_input)
        .insert(scaled_number_input_value(
            ColorChannel::HslHue,
            state.hsl.hue,
        ));
    commands
        .entity(refs.s_input)
        .insert(scaled_number_input_value(
            ColorChannel::HslSaturation,
            state.hsl.saturation,
        ));
    commands
        .entity(refs.l_input)
        .insert(scaled_number_input_value(
            ColorChannel::HslLightness,
            state.hsl.lightness,
        ));

    commands
        .entity(refs.a_input)
        .insert(scaled_number_input_value(
            ColorChannel::Alpha,
            state.to_alpha(),
        ));

    // Update the color swatch
    commands.entity(refs.swatch).insert(ColorSwatchValue(color));

    // Update the hex input
    if let Ok(mut editable_text) = q_editable_text.get_mut(refs.hex_input) {
        let hex_value = state.rgb.to_hex();
        if editable_text.value() != hex_value.as_str() {
            editable_text.queue_edit(TextEdit::SelectAll);
            editable_text.queue_edit(TextEdit::Insert(hex_value.into()));
        }
    }
}

/// Helper function used to find the [`ColorInputState`] component in the ancestor chain.
fn color_input_state<'a>(
    q_parent: &Query<&ChildOf>,
    q_state: &'a mut Query<&mut ColorInputState>,
    from: Entity,
) -> Option<(Entity, Mut<'a, ColorInputState>)> {
    let root_id = q_parent
        .iter_ancestors(from)
        .find(|e| q_state.contains(*e))?;
    let state = q_state.get_mut(root_id).ok()?;
    Some((root_id, state))
}

/// Scale between a number input's display units and the channel's native range.
///
/// Note that RGBA sliders go from 0..=255 (inclusive). The multiplier
/// is arbitrary since the representation is f32, but CSS users are familiar
/// with 8-bit colors.
fn display_scale(channel: ColorChannel) -> f32 {
    match channel {
        ColorChannel::Red | ColorChannel::Green | ColorChannel::Blue | ColorChannel::Alpha => 255.0,
        ColorChannel::HslSaturation | ColorChannel::HslLightness => 100.0,
        ColorChannel::HslHue => 1.0,
        _ => unreachable!("Unsupported color plane type"),
    }
}

/// Plugin which registers the observers for updating the swatch color.
pub struct ColorInputPlugin;

impl Plugin for ColorInputPlugin {
    fn build(&self, app: &mut bevy_app::App) {
        app.init_resource::<ColorInputSettings>();
        app.add_systems(
            PostUpdate,
            (update_mode_selector, color_input_value_change).chain(),
        );
    }
}

/// Observer function which updates the color input value in response to a [`ValueChange`] event.
pub fn color_input_self_update(value_change: On<ValueChange<Color>>, mut commands: Commands) {
    commands
        .entity(value_change.source)
        .insert(ColorInputValue(value_change.value));
}