iced-cube 0.0.6

Themeable application components for the iced GUI library, with Lucide icons
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
//! A text field with a list of suggestions that filters as you type.
//!
//! [`State`] holds the options, the query, the highlighted suggestion and
//! the selected value. The app owns it and passes every [`Event`] to
//! [`State::update`], which returns the value when one is chosen.
//!
//! While the field has focus or the list is open, the combobox handles its
//! own keys through a [`Keymap`] of [`Action`]s: arrows move the highlight,
//! Enter chooses and Escape closes. See [`default_keymap`]. The list is
//! drawn in the shared [menu look](crate::overlay::menu) on the
//! [anchored layer](crate::overlay::anchored), so a click outside closes
//! only the list, even inside a dialog or popover.
//!
//! ```no_run
//! use iced::Element;
//! use iced_cube::forms::combobox::{self, combobox};
//!
//! #[derive(Debug, Clone)]
//! enum Message {
//!     Fruit(combobox::Event),
//! }
//!
//! struct App {
//!     fruit: combobox::State<&'static str>,
//! }
//!
//! impl App {
//!     fn update(&mut self, message: Message) {
//!         let Message::Fruit(event) = message;
//!         if let Some(fruit) = self.fruit.update(event) {
//!             println!("Chose {fruit}");
//!         }
//!     }
//!
//!     fn view(&self) -> Element<'_, Message> {
//!         combobox(&self.fruit)
//!             .placeholder("Search fruit...")
//!             .on_event(Message::Fruit)
//!             .into()
//!     }
//! }
//! ```

use std::fmt::{self, Display};
use std::rc::Rc;

use iced::advanced::layout::{self, Layout};
use iced::advanced::widget::{Operation, Tree, Widget, operation, tree};
use iced::advanced::{Clipboard, Shell, overlay, renderer};
use iced::keyboard::key::Named;
use iced::mouse::{self, ScrollDelta};
use iced::widget::text::LineHeight;
use iced::widget::text_input::{self, Status};
use iced::widget::{self, Column, container, mouse_area, stack};
use iced::{Alignment, Element, Length, Padding, Renderer, Theme};

use crate::icon::{opacity, themed};
use crate::keys::{self, Chord, Keymap};
use crate::overlay::anchored::{Align, Placement, Side, anchored};
use crate::overlay::menu::{self, Parts, RowStatus, Trailing};
use crate::primitives::input::{self, Size};
use crate::theme::{Tokens, space};

/// How many suggestions show at once unless the state sets its own number.
pub const VISIBLE_ROWS: usize = 6;
/// Shown when no option matches the query, unless the builder sets its own.
pub const DEFAULT_EMPTY: &str = "No results found.";

// Matches the line height `input` uses, so the field is the same height.
const LINE_HEIGHT: f32 = 1.3;

/// Everything that changes a combobox.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event {
    /// The query was edited. Opens the list and highlights the first match.
    Input(String),
    Open,
    /// Closes the list and clears the query.
    Close,
    /// Highlights the next match, wrapping round. Opens a closed list.
    Next,
    /// Highlights the previous match, wrapping round. Opens a closed list.
    Previous,
    /// Chooses the highlighted match.
    ActivateHighlighted,
    /// Highlights an option, by its index in [`State::options`].
    Highlight(usize),
    /// Chooses an option, by its index in [`State::options`].
    Activate(usize),
    /// Scrolls the list by a number of rows, down when positive.
    Scroll(i32),
}

/// The options, the query and what is highlighted and selected.
#[derive(Debug, Clone, PartialEq)]
pub struct State<T> {
    options: Vec<T>,
    labels: Vec<String>,
    query: String,
    /// Indices into `options` of the options that match `query`.
    matches: Vec<usize>,
    /// Position in `matches`.
    highlighted: usize,
    /// Position in `matches` of the first visible row.
    offset: usize,
    selected: Option<usize>,
    open: bool,
    rows: usize,
}

impl<T: Display> State<T> {
    /// Creates a closed combobox with nothing selected. Options are
    /// labelled with their `Display` text.
    pub fn new(options: impl IntoIterator<Item = T>) -> Self {
        let options: Vec<T> = options.into_iter().collect();
        let labels = options
            .iter()
            .map(|option| option.to_string().to_lowercase())
            .collect();
        let matches = (0..options.len()).collect();
        Self {
            options,
            labels,
            query: String::new(),
            matches,
            highlighted: 0,
            offset: 0,
            selected: None,
            open: false,
            rows: VISIBLE_ROWS,
        }
    }
}

impl<T: PartialEq> State<T> {
    /// Starts with `value` selected, when it is one of the options.
    pub fn with_selected(mut self, value: &T) -> Self {
        self.selected = self.options.iter().position(|option| option == value);
        self
    }
}

impl<T> State<T> {
    /// Sets how many suggestions show at once. At least one does.
    pub fn with_visible_rows(mut self, rows: usize) -> Self {
        self.rows = rows.max(1);
        self.reveal();
        self
    }

    pub fn options(&self) -> &[T] {
        &self.options
    }

    pub fn selected(&self) -> Option<&T> {
        self.selected.and_then(|index| self.options.get(index))
    }

    pub fn query(&self) -> &str {
        &self.query
    }

    pub fn is_open(&self) -> bool {
        self.open
    }

    /// The options that match the query, in their original order.
    pub fn matches(&self) -> impl Iterator<Item = &T> {
        self.matches
            .iter()
            .filter_map(|&index| self.options.get(index))
    }

    /// The highlighted option, if any option matches.
    pub fn highlighted(&self) -> Option<&T> {
        self.matches
            .get(self.highlighted)
            .and_then(|&index| self.options.get(index))
    }

    /// The matches in view, with their index in [`State::options`].
    pub fn visible(&self) -> impl Iterator<Item = (usize, &T)> {
        self.matches
            .iter()
            .skip(self.offset)
            .take(self.rows)
            .filter_map(|&index| self.options.get(index).map(|option| (index, option)))
    }

    fn is_highlighted(&self, index: usize) -> bool {
        self.matches.get(self.highlighted) == Some(&index)
    }

    /// Applies an event and returns the chosen value, if one was chosen.
    pub fn update(&mut self, event: Event) -> Option<T>
    where
        T: Clone,
    {
        match event {
            Event::Input(query) => {
                self.query = query;
                self.open = true;
                self.filter();
                self.highlight(0);
            }
            Event::Open => self.open(),
            Event::Close => self.close(),
            Event::Next | Event::Previous if !self.open => self.open(),
            Event::Next => self.step(1),
            Event::Previous => self.step(-1),
            Event::ActivateHighlighted => {
                if !self.open {
                    return None;
                }
                let index = *self.matches.get(self.highlighted)?;
                return self.choose(index);
            }
            Event::Activate(index) => return self.choose(index),
            Event::Highlight(index) => {
                if let Some(position) = self.matches.iter().position(|&i| i == index) {
                    self.highlight(position);
                }
            }
            Event::Scroll(rows) => {
                let last = self.matches.len().saturating_sub(self.rows);
                let offset = (self.offset as i64).saturating_add(rows.into());
                self.offset = offset.clamp(0, last as i64) as usize;
            }
        }
        None
    }

    fn filter(&mut self) {
        let query = self.query.trim().to_lowercase();
        self.matches = self
            .labels
            .iter()
            .enumerate()
            .filter(|(_, label)| label.contains(&query))
            .map(|(index, _)| index)
            .collect();
    }

    fn open(&mut self) {
        self.open = true;
        self.query.clear();
        self.filter();
        let selected = self
            .selected
            .and_then(|index| self.matches.iter().position(|&i| i == index));
        self.offset = 0;
        self.highlight(selected.unwrap_or(0));
    }

    fn close(&mut self) {
        self.open = false;
        self.query.clear();
        self.filter();
        self.offset = 0;
    }

    fn choose(&mut self, index: usize) -> Option<T>
    where
        T: Clone,
    {
        let value = self.options.get(index)?.clone();
        self.selected = Some(index);
        self.close();
        Some(value)
    }

    fn step(&mut self, delta: isize) {
        let len = self.matches.len() as isize;
        if len == 0 {
            return;
        }
        let next = (self.highlighted as isize + delta).rem_euclid(len);
        self.highlight(next as usize);
    }

    fn highlight(&mut self, position: usize) {
        self.highlighted = position.min(self.matches.len().saturating_sub(1));
        self.reveal();
    }

    /// Moves the window so the highlighted row is in view.
    fn reveal(&mut self) {
        if self.highlighted < self.offset {
            self.offset = self.highlighted;
        } else if self.highlighted >= self.offset + self.rows {
            self.offset = self.highlighted + 1 - self.rows;
        }
        self.offset = self
            .offset
            .min(self.matches.len().saturating_sub(self.rows));
    }
}

/// What a combobox keyboard shortcut does.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Action {
    Next,
    Previous,
    Activate,
    Close,
}

impl Action {
    /// The [`Event`] this action sends to `state`, or `None` when it would
    /// do nothing, so the key can reach the text field instead.
    pub fn event<T>(self, state: &State<T>) -> Option<Event> {
        match self {
            Action::Next => Some(Event::Next),
            Action::Previous => Some(Event::Previous),
            Action::Activate => state.open.then_some(Event::ActivateHighlighted),
            Action::Close => state.open.then_some(Event::Close),
        }
    }
}

impl keys::Action for Action {
    const ALL: &'static [Self] = &[
        Action::Next,
        Action::Previous,
        Action::Activate,
        Action::Close,
    ];

    fn defaults() -> Keymap<Self> {
        default_keymap()
    }

    fn name(self) -> &'static str {
        match self {
            Action::Next => "Next",
            Action::Previous => "Previous",
            Action::Activate => "Activate",
            Action::Close => "Close",
        }
    }

    fn description(self) -> &'static str {
        match self {
            Action::Next => "Highlights the next suggestion, opening the list if it is closed.",
            Action::Previous => {
                "Highlights the previous suggestion, opening the list if it is closed."
            }
            Action::Activate => "Chooses the highlighted suggestion and closes the list.",
            Action::Close => "Closes the list and clears the query.",
        }
    }
}

/// The default combobox shortcuts:
///
/// | Keys | Action |
/// | --- | --- |
/// | `ArrowDown` | [`Action::Next`] |
/// | `ArrowUp` | [`Action::Previous`] |
/// | `Enter` | [`Action::Activate`] |
/// | `Escape` | [`Action::Close`] |
///
/// The combobox handles these itself while its field has focus or its list
/// is open, so the app does not need to subscribe to key presses. Pass a
/// changed keymap with [`Combobox::keymap`].
pub fn default_keymap() -> Keymap<Action> {
    Keymap::new()
        .bind(Chord::named(Named::ArrowDown), Action::Next)
        .bind(Chord::named(Named::ArrowUp), Action::Previous)
        .bind(Chord::named(Named::Enter), Action::Activate)
        .bind(Chord::named(Named::Escape), Action::Close)
}

/// A combobox builder. Convert it into an [`Element`] to render.
///
/// A combobox without [`on_event`](Combobox::on_event) is rendered disabled.
pub struct Combobox<'a, T, Message> {
    state: &'a State<T>,
    placeholder: String,
    empty: String,
    width: Length,
    size: Size,
    id: Option<widget::Id>,
    invalid: bool,
    keymap: Keymap<Action>,
    on_event: Option<Box<dyn Fn(Event) -> Message + 'a>>,
}

/// Renders the field and, while it is open, the suggestions of `state`.
pub fn combobox<'a, T, Message>(state: &'a State<T>) -> Combobox<'a, T, Message> {
    Combobox {
        state,
        placeholder: String::new(),
        empty: DEFAULT_EMPTY.to_owned(),
        width: Length::Fixed(240.0),
        size: Size::default(),
        id: None,
        invalid: false,
        keymap: default_keymap(),
        on_event: None,
    }
}

impl<T: fmt::Debug, Message> fmt::Debug for Combobox<'_, T, Message> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Combobox")
            .field("state", self.state)
            .field("placeholder", &self.placeholder)
            .field("empty", &self.empty)
            .field("width", &self.width)
            .field("size", &self.size)
            .field("invalid", &self.invalid)
            .field("enabled", &self.on_event.is_some())
            .finish_non_exhaustive()
    }
}

impl<'a, T, Message> Combobox<'a, T, Message> {
    /// Text shown while the field is empty and nothing is selected.
    pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
        self.placeholder = placeholder.into();
        self
    }

    /// Text shown in the list when no option matches the query.
    pub fn empty(mut self, empty: impl Into<String>) -> Self {
        self.empty = empty.into();
        self
    }

    /// Overrides the width. Defaults to 240 pixels.
    pub fn width(mut self, width: impl Into<Length>) -> Self {
        self.width = width.into();
        self
    }

    /// The field's height, padding and text size, shared with
    /// [`input`](fn@crate::primitives::input). Defaults to [`Size::Md`].
    pub fn size(mut self, size: Size) -> Self {
        self.size = size;
        self
    }

    /// Sets the widget id of the text field, to focus it or find it in tests.
    pub fn id(mut self, id: impl Into<widget::Id>) -> Self {
        self.id = Some(id.into());
        self
    }

    /// Draws the field with the destructive border, as
    /// [`input`](fn@crate::primitives::input) does, for example when a required
    /// value is missing.
    pub fn invalid(mut self, invalid: bool) -> Self {
        self.invalid = invalid;
        self
    }

    /// Replaces the [`default_keymap`].
    pub fn keymap(mut self, keymap: Keymap<Action>) -> Self {
        self.keymap = keymap;
        self
    }

    /// Maps combobox events to your message. Without it the combobox is
    /// disabled.
    pub fn on_event(mut self, on_event: impl Fn(Event) -> Message + 'a) -> Self {
        self.on_event = Some(Box::new(on_event));
        self
    }

    pub fn is_enabled(&self) -> bool {
        self.on_event.is_some()
    }
}

impl<'a, T, Message> From<Combobox<'a, T, Message>> for Element<'a, Message>
where
    T: Display + 'a,
    Message: Clone + 'a,
{
    fn from(combobox: Combobox<'a, T, Message>) -> Self {
        let Combobox {
            state,
            placeholder,
            empty,
            width,
            size,
            id,
            invalid,
            keymap,
            on_event,
        } = combobox;
        let selected = state.selected().map(ToString::to_string);

        let Some(on_event) = on_event else {
            let value = selected.unwrap_or_default();
            return container(field(&placeholder, &value, size, id, None, false, invalid))
                .width(width)
                .into();
        };
        let on_event: Rc<dyn Fn(Event) -> Message + 'a> = Rc::from(on_event);

        let open = state.open;
        let (value, hint) = if open {
            (state.query.clone(), selected.unwrap_or(placeholder))
        } else {
            (selected.unwrap_or_default(), placeholder)
        };

        let list = open.then(|| suggestions(state, &empty, &*on_event));
        let on_anchor_press = (!open).then(|| on_event(Event::Open));
        let on_dismiss = open.then(|| on_event(Event::Close));
        let on_input: OnInput<'a, Message> = {
            let on_event = Rc::clone(&on_event);
            Box::new(move |query| on_event(Event::Input(query)))
        };
        let field = container(field(
            &hint,
            &value,
            size,
            id,
            Some(on_input),
            open,
            invalid,
        ))
        .width(width);
        let field = CaretToEnd {
            content: field.into(),
            watch: (open, state.selected),
        };

        anchored(Element::new(field))
            .content(list)
            .placement(Placement::new(Side::Bottom, Align::Start))
            .match_width(true)
            .dismiss_keys([])
            .dismiss_on_blur(true)
            .on_dismiss_maybe(on_dismiss)
            .on_anchor_press_maybe(on_anchor_press)
            .on_key(move |key| {
                keymap
                    .resolve_event(key)
                    .and_then(|action| action.event(state))
                    .map(&*on_event)
            })
            .into()
    }
}

type OnInput<'a, Message> = Box<dyn Fn(String) -> Message + 'a>;

fn field<'a, Message: Clone + 'a>(
    placeholder: &str,
    value: &str,
    size: Size,
    id: Option<widget::Id>,
    on_input: Option<OnInput<'a, Message>>,
    open: bool,
    invalid: bool,
) -> Element<'a, Message> {
    let enabled = on_input.is_some();
    let metrics = size.metrics();
    let mut padding = metrics.padding(false);
    padding.right += metrics.icon + space::SM;

    let mut input = widget::text_input(placeholder, value)
        .size(metrics.text)
        .line_height(LineHeight::Relative(LINE_HEIGHT))
        .padding(padding)
        .width(Length::Fill)
        .style(move |theme, status| style(&Tokens::of(theme), status, open, invalid));
    if let Some(id) = id {
        input = input.id(id);
    }
    if let Some(on_input) = on_input {
        input = input.on_input(on_input);
    }

    let chevron = themed(
        crate::lucide!(ChevronsUpDown),
        metrics.icon,
        opacity(enabled),
        |theme| Tokens::of(theme).muted_foreground,
    );

    stack![
        input,
        container(chevron)
            .width(Length::Fill)
            .height(Length::Fill)
            .align_x(Alignment::End)
            .align_y(Alignment::Center)
            .padding(Padding::ZERO.right(metrics.padding_x)),
    ]
    .into()
}

fn suggestions<'a, T: Display, Message: Clone + 'a>(
    state: &'a State<T>,
    empty: &str,
    on_event: &dyn Fn(Event) -> Message,
) -> Element<'a, Message> {
    let rows: Vec<Element<'a, Message>> = state
        .visible()
        .map(|(index, option)| {
            let status = if state.is_highlighted(index) {
                RowStatus::Highlighted
            } else {
                RowStatus::Idle
            };
            let label = option.to_string();
            let trailing = if state.selected == Some(index) {
                Trailing::Check
            } else {
                Trailing::None
            };
            let parts = Parts {
                trailing,
                ..Parts::label(&label)
            };
            let messages = (
                on_event(Event::Highlight(index)),
                on_event(Event::Activate(index)),
            );
            menu::item(parts, status, false, Some(messages))
        })
        .collect();

    let body: Element<'a, Message> = if rows.is_empty() {
        menu::empty(empty)
    } else {
        Column::from_vec(rows).width(Length::Fill).into()
    };

    let down = on_event(Event::Scroll(1));
    let up = on_event(Event::Scroll(-1));
    mouse_area(menu::surface(body).width(Length::Fill))
        .on_scroll(move |delta| match delta {
            ScrollDelta::Lines { y, .. } | ScrollDelta::Pixels { y, .. } if y < 0.0 => down.clone(),
            _ => up.clone(),
        })
        .into()
}

/// The field style: the input style, drawn focused while the list is open
/// and with the destructive border while `invalid`.
pub fn style(tokens: &Tokens, status: Status, open: bool, invalid: bool) -> text_input::Style {
    let status = match status {
        Status::Active if open => Status::Focused { is_hovered: false },
        Status::Hovered if open => Status::Focused { is_hovered: true },
        status => status,
    };
    input::style(tokens, status, invalid)
}

/// Wraps the field and moves its caret to the end whenever the list closes
/// or the selection changes, so a picked value reads from its end rather
/// than from wherever the caret was in the query.
struct CaretToEnd<'a, Message> {
    content: Element<'a, Message>,
    /// Whether the list is open, and the selected option.
    watch: (bool, Option<usize>),
}

/// The caret goes to the end once the list is closed, if it just closed or
/// the selection changed.
fn moves_caret(before: Option<(bool, Option<usize>)>, now: (bool, Option<usize>)) -> bool {
    let Some(before) = before else {
        return false;
    };
    let (open, selected) = now;
    !open && (before.0 || before.1 != selected)
}

struct MoveCaretToEnd;

impl Operation for MoveCaretToEnd {
    fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation)) {
        operate(self);
    }

    fn text_input(
        &mut self,
        _id: Option<&widget::Id>,
        _bounds: iced::Rectangle,
        state: &mut dyn operation::TextInput,
    ) {
        state.move_cursor_to_end();
    }
}

impl<Message> Widget<Message, Theme, Renderer> for CaretToEnd<'_, Message> {
    fn size(&self) -> iced::Size<Length> {
        self.content.as_widget().size()
    }

    fn size_hint(&self) -> iced::Size<Length> {
        self.content.as_widget().size_hint()
    }

    fn tag(&self) -> tree::Tag {
        tree::Tag::of::<Option<(bool, Option<usize>)>>()
    }

    fn state(&self) -> tree::State {
        tree::State::new(None::<(bool, Option<usize>)>)
    }

    fn children(&self) -> Vec<Tree> {
        vec![Tree::new(&self.content)]
    }

    fn diff(&self, tree: &mut Tree) {
        tree.diff_children(std::slice::from_ref(&self.content));
    }

    // Layout runs after every change to the view, before the next draw, so
    // the caret moves without waiting for another event.
    fn layout(
        &mut self,
        tree: &mut Tree,
        renderer: &Renderer,
        limits: &layout::Limits,
    ) -> layout::Node {
        let node = self
            .content
            .as_widget_mut()
            .layout(&mut tree.children[0], renderer, limits);
        let seen = tree.state.downcast_mut::<Option<(bool, Option<usize>)>>();
        let before = seen.replace(self.watch);
        if moves_caret(before, self.watch) {
            self.content.as_widget_mut().operate(
                &mut tree.children[0],
                Layout::new(&node),
                renderer,
                &mut MoveCaretToEnd,
            );
        }
        node
    }

    fn draw(
        &self,
        tree: &Tree,
        renderer: &mut Renderer,
        theme: &Theme,
        style: &renderer::Style,
        layout: Layout<'_>,
        cursor: mouse::Cursor,
        viewport: &iced::Rectangle,
    ) {
        self.content.as_widget().draw(
            &tree.children[0],
            renderer,
            theme,
            style,
            layout,
            cursor,
            viewport,
        );
    }

    fn operate(
        &mut self,
        tree: &mut Tree,
        layout: Layout<'_>,
        renderer: &Renderer,
        operation: &mut dyn Operation,
    ) {
        self.content
            .as_widget_mut()
            .operate(&mut tree.children[0], layout, renderer, operation);
    }

    fn update(
        &mut self,
        tree: &mut Tree,
        event: &iced::Event,
        layout: Layout<'_>,
        cursor: mouse::Cursor,
        renderer: &Renderer,
        clipboard: &mut dyn Clipboard,
        shell: &mut Shell<'_, Message>,
        viewport: &iced::Rectangle,
    ) {
        self.content.as_widget_mut().update(
            &mut tree.children[0],
            event,
            layout,
            cursor,
            renderer,
            clipboard,
            shell,
            viewport,
        );
    }

    fn mouse_interaction(
        &self,
        tree: &Tree,
        layout: Layout<'_>,
        cursor: mouse::Cursor,
        viewport: &iced::Rectangle,
        renderer: &Renderer,
    ) -> mouse::Interaction {
        self.content.as_widget().mouse_interaction(
            &tree.children[0],
            layout,
            cursor,
            viewport,
            renderer,
        )
    }

    fn overlay<'b>(
        &'b mut self,
        tree: &'b mut Tree,
        layout: Layout<'b>,
        renderer: &Renderer,
        viewport: &iced::Rectangle,
        translation: iced::Vector,
    ) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
        self.content.as_widget_mut().overlay(
            &mut tree.children[0],
            layout,
            renderer,
            viewport,
            translation,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::theme::{dark, light};

    const FRUITS: [&str; 5] = ["Apple", "Banana", "Blueberry", "Cherry", "Grape"];

    fn fruits() -> State<&'static str> {
        State::new(FRUITS)
    }

    fn matches(state: &State<&'static str>) -> Vec<&'static str> {
        state.matches().copied().collect()
    }

    fn press(keymap: &Keymap<Action>, chord: &str) -> Option<Action> {
        let chord: Chord = chord.parse().unwrap();
        keymap.resolve(chord.key(), chord.modifiers())
    }

    #[test]
    fn new_state_is_closed_with_every_option() {
        let state = fruits();
        assert!(!state.is_open());
        assert!(state.selected().is_none());
        assert_eq!(state.query(), "");
        assert_eq!(matches(&state), FRUITS);
        assert_eq!(state.visible().count(), 5);
    }

    #[test]
    fn with_selected_ignores_unknown_values() {
        assert_eq!(
            fruits().with_selected(&"Cherry").selected(),
            Some(&"Cherry")
        );
        assert!(fruits().with_selected(&"Kiwi").selected().is_none());
    }

    #[test]
    fn typing_filters_case_insensitively_and_keeps_order() {
        let mut state = fruits();
        assert!(state.update(Event::Input("B".into())).is_none());
        assert!(state.is_open());
        assert_eq!(matches(&state), ["Banana", "Blueberry"]);
        assert_eq!(state.highlighted(), Some(&"Banana"));

        let _ = state.update(Event::Input("  ERR ".into()));
        assert_eq!(matches(&state), ["Blueberry", "Cherry"]);
    }

    #[test]
    fn no_match_leaves_nothing_highlighted() {
        let mut state = fruits();
        let _ = state.update(Event::Input("kiwi".into()));
        assert!(state.matches().next().is_none());
        assert!(state.highlighted().is_none());
        assert!(state.update(Event::ActivateHighlighted).is_none());
        let _ = state.update(Event::Next);
        assert!(state.highlighted().is_none());
    }

    #[test]
    fn arrows_open_then_wrap_round() {
        let mut state = fruits();
        let _ = state.update(Event::Next);
        assert!(state.is_open());
        assert_eq!(state.highlighted(), Some(&"Apple"));

        let _ = state.update(Event::Previous);
        assert_eq!(state.highlighted(), Some(&"Grape"));
        let _ = state.update(Event::Next);
        assert_eq!(state.highlighted(), Some(&"Apple"));
    }

    #[test]
    fn opening_highlights_the_selection() {
        let mut state = fruits().with_selected(&"Cherry");
        let _ = state.update(Event::Open);
        assert_eq!(state.highlighted(), Some(&"Cherry"));
    }

    #[test]
    fn confirm_chooses_closes_and_clears_the_query() {
        let mut state = fruits();
        let _ = state.update(Event::Input("an".into()));
        assert_eq!(state.update(Event::ActivateHighlighted), Some("Banana"));
        assert!(!state.is_open());
        assert_eq!(state.query(), "");
        assert_eq!(state.selected(), Some(&"Banana"));
        assert_eq!(matches(&state).len(), FRUITS.len());
    }

    #[test]
    fn confirm_does_nothing_while_closed() {
        let mut state = fruits();
        assert!(state.update(Event::ActivateHighlighted).is_none());
        assert!(state.selected().is_none());
    }

    #[test]
    fn pick_and_highlight_use_option_indices() {
        let mut state = fruits();
        let _ = state.update(Event::Input("berry".into()));
        let _ = state.update(Event::Highlight(2));
        assert_eq!(state.highlighted(), Some(&"Blueberry"));
        // Highlighting an option that does not match is ignored.
        let _ = state.update(Event::Highlight(0));
        assert_eq!(state.highlighted(), Some(&"Blueberry"));

        assert_eq!(state.update(Event::Activate(3)), Some("Cherry"));
        assert!(state.update(Event::Activate(99)).is_none());
        assert_eq!(state.selected(), Some(&"Cherry"));
    }

    #[test]
    fn close_keeps_the_selection() {
        let mut state = fruits().with_selected(&"Apple");
        let _ = state.update(Event::Input("gr".into()));
        let _ = state.update(Event::Close);
        assert!(!state.is_open());
        assert_eq!(state.query(), "");
        assert_eq!(state.selected(), Some(&"Apple"));
    }

    #[test]
    fn the_window_follows_the_highlight() {
        let mut state = fruits().with_visible_rows(2);
        let _ = state.update(Event::Open);
        let visible = |state: &State<&'static str>| -> Vec<&str> {
            state.visible().map(|(_, fruit)| *fruit).collect()
        };
        assert_eq!(visible(&state), ["Apple", "Banana"]);

        let _ = state.update(Event::Next);
        let _ = state.update(Event::Next);
        assert_eq!(state.highlighted(), Some(&"Blueberry"));
        assert_eq!(visible(&state), ["Banana", "Blueberry"]);

        let _ = state.update(Event::Previous);
        let _ = state.update(Event::Previous);
        let _ = state.update(Event::Previous);
        assert_eq!(state.highlighted(), Some(&"Grape"));
        assert_eq!(visible(&state), ["Cherry", "Grape"]);
    }

    #[test]
    fn scrolling_stays_within_the_matches() {
        let mut state = fruits().with_visible_rows(2);
        let _ = state.update(Event::Open);
        let _ = state.update(Event::Scroll(10));
        assert_eq!(state.visible().map(|(i, _)| i).collect::<Vec<_>>(), [3, 4]);
        let _ = state.update(Event::Scroll(-10));
        assert_eq!(state.visible().map(|(i, _)| i).collect::<Vec<_>>(), [0, 1]);
        assert_eq!(State::<&str>::new([]).with_visible_rows(0).rows, 1);
    }

    #[test]
    fn empty_options_are_safe() {
        let mut state = State::<&str>::new([]);
        let _ = state.update(Event::Next);
        let _ = state.update(Event::Previous);
        let _ = state.update(Event::Scroll(3));
        assert!(state.update(Event::ActivateHighlighted).is_none());
        assert!(state.highlighted().is_none());
    }

    #[test]
    fn default_keymap_and_overrides() {
        let keymap = default_keymap();
        assert_eq!(press(&keymap, "ArrowDown"), Some(Action::Next));
        assert_eq!(press(&keymap, "ArrowUp"), Some(Action::Previous));
        assert_eq!(press(&keymap, "Enter"), Some(Action::Activate));
        assert_eq!(press(&keymap, "Escape"), Some(Action::Close));

        let custom = keymap
            .bind("Ctrl+N".parse().unwrap(), Action::Next)
            .unbind(&Chord::named(Named::Escape));
        assert_eq!(press(&custom, "ctrl+n"), Some(Action::Next));
        assert_eq!(press(&custom, "Escape"), None);
    }

    #[test]
    fn actions_map_to_events_only_when_they_apply() {
        let mut state = fruits();
        assert_eq!(Action::Next.event(&state), Some(Event::Next));
        assert_eq!(Action::Previous.event(&state), Some(Event::Previous));
        assert_eq!(Action::Activate.event(&state), None);
        assert_eq!(Action::Close.event(&state), None);

        let _ = state.update(Event::Open);
        assert_eq!(
            Action::Activate.event(&state),
            Some(Event::ActivateHighlighted)
        );
        assert_eq!(Action::Close.event(&state), Some(Event::Close));
    }

    #[test]
    fn default_builder_is_disabled_with_the_default_empty_text() {
        let state = fruits();
        let c: Combobox<'_, &str, Event> = combobox(&state);
        assert!(!c.is_enabled());
        assert_eq!(c.empty, DEFAULT_EMPTY);
        assert_eq!(c.width, Length::Fixed(240.0));
        assert_eq!(c.size, Size::Md);
        assert_eq!(c.keymap, default_keymap());
        assert!(!c.invalid);
        let c = c
            .placeholder("Pick")
            .empty("None")
            .size(Size::Sm)
            .invalid(true)
            .on_event(|e| e);
        assert_eq!(c.size, Size::Sm);
        assert!(c.invalid);
        assert!(c.is_enabled());
        assert_eq!((c.placeholder.as_str(), c.empty.as_str()), ("Pick", "None"));
    }

    const STATES: [Status; 5] = [
        Status::Active,
        Status::Hovered,
        Status::Focused { is_hovered: false },
        Status::Focused { is_hovered: true },
        Status::Disabled,
    ];

    #[test]
    fn open_field_draws_the_focused_border() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            let focused = input::style(&tokens, Status::Focused { is_hovered: false }, false);
            for status in STATES {
                let closed = style(&tokens, status, false, false);
                assert_eq!(closed, input::style(&tokens, status, false), "{status:?}");
                let open = style(&tokens, status, true, false);
                if status != Status::Disabled {
                    assert_eq!(open.border.color, focused.border.color, "{status:?}");
                }
            }
        }
    }

    #[test]
    fn disabled_field_uses_the_shared_disabled_background() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            for open in [false, true] {
                let disabled = style(&tokens, Status::Disabled, open, false);
                assert_eq!(
                    disabled.background,
                    iced::Background::Color(tokens.disabled_field())
                );
            }
        }
    }

    #[test]
    fn invalid_field_uses_the_input_invalid_style() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            for status in STATES {
                assert_eq!(
                    style(&tokens, status, false, true),
                    input::style(&tokens, status, true),
                    "{status:?}"
                );
                let valid = style(&tokens, status, false, false);
                if status != Status::Disabled {
                    assert_ne!(
                        style(&tokens, status, false, true).border.color,
                        valid.border.color,
                        "{status:?}"
                    );
                }
            }
        }
    }

    #[test]
    fn the_caret_moves_to_the_end_when_the_list_closes_or_the_pick_changes() {
        assert!(
            !moves_caret(None, (false, None)),
            "nothing to move on first layout"
        );
        assert!(moves_caret(Some((true, None)), (false, Some(2))), "a pick");
        assert!(
            moves_caret(Some((true, Some(2))), (false, Some(2))),
            "Escape"
        );
        assert!(
            moves_caret(Some((false, Some(1))), (false, Some(2))),
            "set by the app"
        );
        assert!(!moves_caret(Some((false, Some(2))), (false, Some(2))));
        assert!(!moves_caret(Some((false, None)), (true, None)), "opening");
        assert!(!moves_caret(Some((true, None)), (true, None)), "typing");
    }
}