tui-lipan 0.1.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets on top of ratatui.
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
//! Combo box widget.

use std::sync::Arc;

use crate::callback::{Callback, KeyHandler};
use crate::core::element::Element;
use crate::core::event::{KeyCode, KeyEvent};
use crate::style::{BorderStyle, Length, Padding, ScrollbarConfig, Style, StyleSlot};
use crate::widgets::{
    Input, InputEvent, List, ListConfig, ListEvent, ListItem, Popover, PopoverOffset,
    PopoverPlacement,
};

/// Commit event emitted by [`ComboBox`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ComboBoxCommitEvent {
    /// Index in the source `items` list when an existing item is committed.
    pub index: Option<usize>,
    /// Committed value.
    pub value: Arc<str>,
    /// `true` when the committed value comes from free-form query text.
    pub from_custom_value: bool,
}

/// A controlled input + dropdown list widget.
#[derive(Clone)]
pub struct ComboBox {
    items: Vec<Arc<str>>,
    query: Arc<str>,
    placeholder: Option<Arc<str>>,
    open: bool,
    active_index: Option<usize>,
    selected: Option<usize>,
    allow_custom_value: bool,
    width: Length,
    list_width: Option<Length>,
    list_height: Length,
    match_input_width: bool,
    disabled: bool,
    placement: PopoverPlacement,
    offset: PopoverOffset,
    clamp: bool,
    auto_flip: bool,
    input_style: Style,
    input_hover_style: StyleSlot,
    input_focus_style: StyleSlot,
    input_focus_content_style: Style,
    input_disabled_style: Style,
    input_hover_border_style: Option<BorderStyle>,
    input_placeholder_style: Style,
    input_focus_placeholder_style: Style,
    input_suffix_open: Arc<str>,
    input_suffix_closed: Arc<str>,
    input_suffix_style: Style,
    input_focus_suffix_style: Style,
    list_config: ListConfig,
    empty_text: Option<Arc<str>>,
    on_query_change: Option<Callback<Arc<str>>>,
    on_open_change: Option<Callback<bool>>,
    on_active_index_change: Option<Callback<Option<usize>>>,
    on_commit: Option<Callback<ComboBoxCommitEvent>>,
}

impl Default for ComboBox {
    fn default() -> Self {
        Self {
            items: Vec::new(),
            query: Arc::from(""),
            placeholder: Some("Type to filter...".into()),
            open: false,
            active_index: None,
            selected: None,
            allow_custom_value: false,
            width: Length::Flex(1),
            list_width: None,
            list_height: Length::Px(8),
            match_input_width: false,
            disabled: false,
            placement: PopoverPlacement::BelowStart,
            offset: PopoverOffset::ZERO,
            clamp: true,
            auto_flip: true,
            input_style: Style::default(),
            input_hover_style: StyleSlot::Inherit,
            input_focus_style: StyleSlot::Inherit,
            input_focus_content_style: Style::default(),
            input_disabled_style: Style::default(),
            input_hover_border_style: None,
            input_placeholder_style: Style::default(),
            input_focus_placeholder_style: Style::default(),
            input_suffix_open: " â–²".into(),
            input_suffix_closed: " â–¼".into(),
            input_suffix_style: Style::default(),
            input_focus_suffix_style: Style::default(),
            list_config: ListConfig {
                border: true,
                border_style: BorderStyle::Plain,
                padding: Padding::default(),
                style: Style::default(),
                selection_style: StyleSlot::Inherit,
                unfocused_selection_style: StyleSlot::Inherit,
                selection_full_width: false,
                selection_symbol: None,
                selection_symbol_right: None,
                selection_symbol_style: None,
                unfocused_selection_symbol_style: None,
                symbol_column: true,
                gutter_gap: 0,
                gutter_for_non_selectable: false,
                item_horizontal_padding: Padding::default(),
                header_horizontal_padding: Padding::default(),
                empty_text_style: Style::default(),
                item_hover_style: None,
                scrollbar: false,
                scrollbar_config: ScrollbarConfig::default(),
            },
            empty_text: Some("No matches".into()),
            on_query_change: None,
            on_open_change: None,
            on_active_index_change: None,
            on_commit: None,
        }
    }
}

impl ComboBox {
    /// Create a new combo box.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set source items.
    pub fn items(mut self, items: impl IntoIterator<Item = impl Into<Arc<str>>>) -> Self {
        self.items = items.into_iter().map(Into::into).collect();
        self
    }

    /// Set current query.
    pub fn query(mut self, query: impl Into<Arc<str>>) -> Self {
        self.query = query.into();
        self
    }

    /// Set placeholder text.
    pub fn placeholder(mut self, placeholder: impl Into<Arc<str>>) -> Self {
        self.placeholder = Some(placeholder.into());
        self
    }

    /// Set controlled open state.
    pub fn open(mut self, open: bool) -> Self {
        self.open = open;
        self
    }

    /// Set currently active_index source index.
    pub fn active_index(mut self, active_index: Option<usize>) -> Self {
        self.active_index = active_index;
        self
    }

    /// Set selected source index.
    pub fn selected(mut self, selected: Option<usize>) -> Self {
        self.selected = selected;
        self
    }

    /// Allow Enter to commit free-form query text when no item is chosen.
    pub fn allow_custom_value(mut self, allow_custom_value: bool) -> Self {
        self.allow_custom_value = allow_custom_value;
        self
    }

    /// Set width.
    pub fn width(mut self, width: Length) -> Self {
        self.width = width;
        self
    }

    /// Set dropdown width override.
    pub fn list_width(mut self, width: Length) -> Self {
        self.list_width = Some(width);
        self
    }

    /// Set dropdown height.
    pub fn list_height(mut self, height: Length) -> Self {
        self.list_height = height;
        self
    }

    /// Force dropdown width to exactly match rendered input width.
    pub fn match_input_width(mut self, match_input_width: bool) -> Self {
        self.match_input_width = match_input_width;
        self
    }

    /// Set disabled state.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Set popover placement.
    pub fn placement(mut self, placement: PopoverPlacement) -> Self {
        self.placement = placement;
        self
    }

    /// Set popover offset.
    pub fn offset(mut self, offset: impl Into<PopoverOffset>) -> Self {
        self.offset = offset.into();
        self
    }

    /// Clamp dropdown to viewport bounds.
    pub fn clamp(mut self, clamp: bool) -> Self {
        self.clamp = clamp;
        self
    }

    /// Auto-flip dropdown placement when overflowing viewport.
    pub fn auto_flip(mut self, auto_flip: bool) -> Self {
        self.auto_flip = auto_flip;
        self
    }

    /// Set input base style.
    pub fn input_style(mut self, style: Style) -> Self {
        self.input_style = style;
        self
    }

    /// Set input hover style.
    pub fn input_hover_style(mut self, style: Style) -> Self {
        self.input_hover_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the themed input hover style.
    pub fn extend_input_hover_style(mut self, style: Style) -> Self {
        self.input_hover_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the themed input hover style.
    pub fn inherit_input_hover_style(mut self) -> Self {
        self.input_hover_style = StyleSlot::Inherit;
        self
    }

    /// Set input hover style slot directly for composite forwarding.
    pub fn input_hover_style_slot(mut self, slot: StyleSlot) -> Self {
        self.input_hover_style = slot;
        self
    }

    /// Set input focus chrome style.
    pub fn input_focus_style(mut self, style: Style) -> Self {
        self.input_focus_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the themed input focus style.
    pub fn extend_input_focus_style(mut self, style: Style) -> Self {
        self.input_focus_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the themed input focus style.
    pub fn inherit_input_focus_style(mut self) -> Self {
        self.input_focus_style = StyleSlot::Inherit;
        self
    }

    /// Set input focus style slot directly for composite forwarding.
    pub fn input_focus_style_slot(mut self, slot: StyleSlot) -> Self {
        self.input_focus_style = slot;
        self
    }

    /// Set focused input content text style.
    pub fn input_focus_content_style(mut self, style: Style) -> Self {
        self.input_focus_content_style = style;
        self
    }

    /// Set input disabled style.
    pub fn input_disabled_style(mut self, style: Style) -> Self {
        self.input_disabled_style = style;
        self
    }

    /// Set input border style while hovered.
    pub fn input_hover_border_style(mut self, border_style: BorderStyle) -> Self {
        self.input_hover_border_style = Some(border_style);
        self
    }

    /// Set input placeholder style.
    pub fn input_placeholder_style(mut self, style: Style) -> Self {
        self.input_placeholder_style = style;
        self
    }

    /// Set input placeholder style when focused.
    pub fn input_focus_placeholder_style(mut self, style: Style) -> Self {
        self.input_focus_placeholder_style = style;
        self
    }

    /// Set suffix displayed when dropdown is open.
    pub fn input_open_suffix(mut self, suffix: impl Into<Arc<str>>) -> Self {
        self.input_suffix_open = suffix.into();
        self
    }

    /// Set suffix displayed when dropdown is closed.
    pub fn input_closed_suffix(mut self, suffix: impl Into<Arc<str>>) -> Self {
        self.input_suffix_closed = suffix.into();
        self
    }

    /// Set input suffix style.
    pub fn input_suffix_style(mut self, style: Style) -> Self {
        self.input_suffix_style = style;
        self
    }

    /// Set input suffix style when focused.
    pub fn input_focus_suffix_style(mut self, style: Style) -> Self {
        self.input_focus_suffix_style = style;
        self
    }

    /// Set list config.
    pub fn list_config(mut self, config: ListConfig) -> Self {
        self.list_config = config;
        self
    }

    /// Set dropdown border visibility.
    pub fn list_border(mut self, list_border: bool) -> Self {
        self.list_config.border = list_border;
        self
    }

    /// Set dropdown border style.
    pub fn list_border_style(mut self, border_style: BorderStyle) -> Self {
        self.list_config.border_style = border_style;
        self
    }

    /// Set dropdown padding.
    pub fn list_padding(mut self, padding: impl Into<Padding>) -> Self {
        self.list_config.padding = padding.into();
        self
    }

    /// Set dropdown base style.
    pub fn list_style(mut self, style: Style) -> Self {
        self.list_config.style = style;
        self
    }

    /// Set dropdown active_index-item style.
    pub fn list_selection_style(mut self, style: Style) -> Self {
        self.list_config.selection_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the themed dropdown active_index-item style.
    pub fn extend_list_selection_style(mut self, style: Style) -> Self {
        self.list_config.selection_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the themed dropdown active_index-item style.
    pub fn inherit_list_selection_style(mut self) -> Self {
        self.list_config.selection_style = StyleSlot::Inherit;
        self
    }

    /// Set dropdown active_index-item style slot directly for composite forwarding.
    pub fn list_selection_style_slot(mut self, slot: StyleSlot) -> Self {
        self.list_config.selection_style = slot;
        self
    }

    /// Set dropdown active_index-item style while the list is not focused.
    pub fn list_unfocused_selection_style(mut self, style: Style) -> Self {
        self.list_config.unfocused_selection_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the themed dropdown active_index-item style while the list is not focused.
    pub fn extend_list_unfocused_selection_style(mut self, style: Style) -> Self {
        self.list_config.unfocused_selection_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the themed dropdown active_index-item style while the list is not focused.
    pub fn inherit_list_unfocused_selection_style(mut self) -> Self {
        self.list_config.unfocused_selection_style = StyleSlot::Inherit;
        self
    }

    /// Set dropdown unfocused active_index-item style slot directly for composite forwarding.
    pub fn list_unfocused_selection_style_slot(mut self, slot: StyleSlot) -> Self {
        self.list_config.unfocused_selection_style = slot;
        self
    }

    /// Set dropdown hovered-item style.
    pub fn list_hover_style(mut self, style: Style) -> Self {
        self.list_config.item_hover_style = Some(StyleSlot::Replace(style));
        self
    }

    /// Extend the themed dropdown hovered-item style.
    pub fn extend_list_hover_style(mut self, style: Style) -> Self {
        self.list_config.item_hover_style = Some(StyleSlot::Extend(style));
        self
    }

    /// Inherit the themed dropdown hovered-item style.
    pub fn inherit_list_hover_style(mut self) -> Self {
        self.list_config.item_hover_style = Some(StyleSlot::Inherit);
        self
    }

    /// Set dropdown hovered-item style slot directly for composite forwarding.
    pub fn list_hover_style_slot(mut self, slot: StyleSlot) -> Self {
        self.list_config.item_hover_style = Some(slot);
        self
    }

    /// Set dropdown active_index-item symbol.
    pub fn list_selection_symbol(mut self, symbol: Option<impl Into<Arc<str>>>) -> Self {
        self.list_config.selection_symbol = symbol.map(Into::into);
        self
    }

    /// Set the trailing dropdown selection symbol (right "pill" cap). Pairs with
    /// [`Self::list_selection_symbol`] and shares the selection symbol style.
    pub fn list_selection_symbol_right(mut self, symbol: Option<impl Into<Arc<str>>>) -> Self {
        self.list_config.selection_symbol_right = symbol.map(Into::into);
        self
    }

    /// Set dropdown active_index-item symbol style.
    pub fn list_selection_symbol_style(mut self, style: Style) -> Self {
        self.list_config.selection_symbol_style = Some(style);
        self
    }

    /// Set dropdown active_index-item symbol style while the list is not focused.
    pub fn list_unfocused_selection_symbol_style(mut self, style: Style) -> Self {
        self.list_config.unfocused_selection_symbol_style = Some(style);
        self
    }

    /// Enable dropdown scrollbar.
    pub fn list_scrollbar(mut self, scrollbar: bool) -> Self {
        self.list_config.scrollbar = scrollbar;
        self
    }

    /// Set dropdown scrollbar configuration.
    pub fn list_scrollbar_config(mut self, config: ScrollbarConfig) -> Self {
        self.list_config.scrollbar_config = config;
        self
    }

    /// Set empty-list text.
    pub fn empty_text(mut self, text: impl Into<Arc<str>>) -> Self {
        self.empty_text = Some(text.into());
        self
    }

    /// Set empty-list text style.
    pub fn empty_text_style(mut self, style: Style) -> Self {
        self.list_config.empty_text_style = style;
        self
    }

    /// Callback fired when query changes.
    pub fn on_query_change(mut self, cb: Callback<Arc<str>>) -> Self {
        self.on_query_change = Some(cb);
        self
    }

    /// Callback fired when open state should change.
    pub fn on_open_change(mut self, cb: Callback<bool>) -> Self {
        self.on_open_change = Some(cb);
        self
    }

    /// Callback fired when active_index source index changes.
    pub fn on_active_index_change(mut self, cb: Callback<Option<usize>>) -> Self {
        self.on_active_index_change = Some(cb);
        self
    }

    /// Callback fired when an item or custom value is committed.
    pub fn on_commit(mut self, cb: Callback<ComboBoxCommitEvent>) -> Self {
        self.on_commit = Some(cb);
        self
    }
}

impl From<ComboBox> for Element {
    fn from(combo: ComboBox) -> Self {
        let filtered_indices = filtered_item_indices(&combo.items, combo.query.as_ref());
        let effective_highlight = normalized_active_index(
            combo.active_index,
            combo.selected,
            filtered_indices.as_slice(),
        );
        let list_selected = effective_highlight
            .and_then(|source_index| {
                filtered_indices
                    .iter()
                    .position(|&candidate| candidate == source_index)
            })
            .unwrap_or(0);

        let mut input = Input::new(combo.query.clone())
            .width(combo.width)
            .style(combo.input_style)
            .hover_style_slot(combo.input_hover_style)
            .focus_style_slot(combo.input_focus_style)
            .focus_content_style(combo.input_focus_content_style)
            .disabled_style(combo.input_disabled_style)
            .placeholder_style(combo.input_placeholder_style)
            .focus_placeholder_style(combo.input_focus_placeholder_style)
            .suffix(if combo.open {
                combo.input_suffix_open.clone()
            } else {
                combo.input_suffix_closed.clone()
            })
            .suffix_style(combo.input_suffix_style)
            .focus_suffix_style(combo.input_focus_suffix_style)
            .read_only(combo.disabled)
            .disabled(combo.disabled);

        if let Some(hover_border_style) = combo.input_hover_border_style {
            input = input.hover_border_style(hover_border_style);
        }

        if let Some(placeholder) = combo.placeholder.clone() {
            input = input.placeholder(placeholder);
        }

        if combo.on_query_change.is_some() || combo.on_open_change.is_some() {
            let on_query_change = combo.on_query_change.clone();
            let on_open_change = combo.on_open_change.clone();
            input = input.on_change(Callback::new(move |event: InputEvent| {
                if let Some(cb) = on_query_change.as_ref() {
                    cb.emit(event.value.clone());
                }
                if let Some(cb) = on_open_change.as_ref() {
                    cb.emit(true);
                }
            }));
        }

        {
            let filtered_indices = filtered_indices.clone();
            let items = combo.items.clone();
            let query = combo.query.clone();
            let selected = combo.selected;
            let allow_custom_value = combo.allow_custom_value;
            let open = combo.open;
            let on_open_change = combo.on_open_change.clone();
            let on_active_index_change = combo.on_active_index_change.clone();
            let on_commit = combo.on_commit.clone();
            input = input.on_key(KeyHandler::new(move |key: KeyEvent| match key.code {
                KeyCode::Esc if open => {
                    if let Some(cb) = on_open_change.as_ref() {
                        cb.emit(false);
                        true
                    } else {
                        false
                    }
                }
                KeyCode::Down | KeyCode::Up => {
                    if filtered_indices.is_empty() {
                        return true;
                    }

                    let current_pos = effective_highlight
                        .and_then(|source_index| {
                            filtered_indices
                                .iter()
                                .position(|&candidate| candidate == source_index)
                        })
                        .unwrap_or(0);
                    let next_pos = if key.code == KeyCode::Down {
                        (current_pos + 1).min(filtered_indices.len().saturating_sub(1))
                    } else {
                        current_pos.saturating_sub(1)
                    };
                    if let Some(cb) = on_active_index_change.as_ref() {
                        cb.emit(Some(filtered_indices[next_pos]));
                    }
                    if let Some(cb) = on_open_change.as_ref() {
                        cb.emit(true);
                    }
                    true
                }
                KeyCode::Enter => {
                    let mut handled = false;

                    let active_index = effective_highlight
                        .filter(|source_index| filtered_indices.contains(source_index));
                    let selected =
                        selected.filter(|source_index| filtered_indices.contains(source_index));
                    let picked_index = active_index.or(selected);

                    if let Some(cb) = on_commit.as_ref() {
                        if let Some(index) = picked_index {
                            cb.emit(ComboBoxCommitEvent {
                                index: Some(index),
                                value: items[index].clone(),
                                from_custom_value: false,
                            });
                            handled = true;
                        } else if allow_custom_value && !query.is_empty() {
                            cb.emit(ComboBoxCommitEvent {
                                index: None,
                                value: query.clone(),
                                from_custom_value: true,
                            });
                            handled = true;
                        }
                    }

                    if open && let Some(cb) = on_open_change.as_ref() {
                        cb.emit(false);
                        handled = true;
                    }

                    handled
                }
                _ => false,
            }));
        }

        let mut list = List::new()
            .items(
                filtered_indices
                    .iter()
                    .map(|&index| ListItem::new(combo.items[index].clone())),
            )
            .selected(list_selected)
            .border(combo.list_config.border)
            .border_style(combo.list_config.border_style)
            .padding(combo.list_config.padding)
            .style(combo.list_config.style)
            .selection_symbol(combo.list_config.selection_symbol)
            .selection_symbol_right(combo.list_config.selection_symbol_right)
            .selection_symbol_style(
                combo.list_config.selection_symbol_style.unwrap_or(
                    combo
                        .list_config
                        .selection_style
                        .explicit_style()
                        .unwrap_or_default(),
                ),
            )
            .unfocused_selection_symbol_style(
                combo
                    .list_config
                    .unfocused_selection_symbol_style
                    .or_else(|| combo.list_config.unfocused_selection_style.explicit_style())
                    .unwrap_or(
                        combo
                            .list_config
                            .selection_style
                            .explicit_style()
                            .unwrap_or_default(),
                    ),
            )
            .symbol_column(combo.list_config.symbol_column)
            .gutter_gap(combo.list_config.gutter_gap)
            .gutter_for_non_selectable(combo.list_config.gutter_for_non_selectable)
            .scrollbar(combo.list_config.scrollbar)
            .scrollbar_config(combo.list_config.scrollbar_config)
            .width(combo.list_width.unwrap_or(combo.width))
            .height(combo.list_height)
            .disabled(combo.disabled)
            .item_horizontal_padding(combo.list_config.item_horizontal_padding)
            .header_horizontal_padding(combo.list_config.header_horizontal_padding)
            .empty_text_style(combo.list_config.empty_text_style);
        list = list
            .selection_style_slot(combo.list_config.selection_style)
            .unfocused_selection_style_slot(combo.list_config.unfocused_selection_style)
            .item_hover_style_slot(
                combo
                    .list_config
                    .item_hover_style
                    .unwrap_or(combo.list_config.selection_style),
            );

        let fit_trigger_width = combo.match_input_width && combo.list_width.is_none();

        if let Some(empty_text) = combo.empty_text {
            list = list.empty_text(empty_text);
        }

        if let Some(cb) = combo.on_active_index_change.clone() {
            let filtered_indices = filtered_indices.clone();
            list = list.on_select(Callback::new(move |event: ListEvent| {
                if let Some(source_index) = filtered_indices.get(event.index).copied() {
                    cb.emit(Some(source_index));
                }
            }));
        }

        if combo.on_commit.is_some() || combo.on_open_change.is_some() {
            let filtered_indices = filtered_indices.clone();
            let on_commit = combo.on_commit.clone();
            let on_open_change = combo.on_open_change.clone();
            let items = combo.items.clone();
            list = list.on_activate(Callback::new(move |event: ListEvent| {
                if let Some(source_index) = filtered_indices.get(event.index).copied()
                    && let Some(cb) = on_commit.as_ref()
                {
                    cb.emit(ComboBoxCommitEvent {
                        index: Some(source_index),
                        value: items[source_index].clone(),
                        from_custom_value: false,
                    });
                }
                if let Some(cb) = on_open_change.as_ref() {
                    cb.emit(false);
                }
            }));
        }

        let on_close = combo
            .on_open_change
            .unwrap_or_else(|| Callback::new(|_| {}));

        Popover::new()
            .trigger(input)
            .content(list)
            .open(combo.open && !combo.disabled)
            .placement(combo.placement)
            .offset(combo.offset)
            .clamp(combo.clamp)
            .auto_flip(combo.auto_flip)
            .fit_trigger_width(fit_trigger_width)
            .min_trigger_width(false)
            .on_close(Callback::new(move |_| on_close.emit(false)))
            .into()
    }
}

fn filtered_item_indices(items: &[Arc<str>], query: &str) -> Vec<usize> {
    if query.is_empty() {
        return (0..items.len()).collect();
    }

    let query = query.to_ascii_lowercase();
    items
        .iter()
        .enumerate()
        .filter_map(|(index, item)| {
            item.to_ascii_lowercase()
                .contains(query.as_str())
                .then_some(index)
        })
        .collect()
}

fn normalized_active_index(
    active_index: Option<usize>,
    selected: Option<usize>,
    filtered_indices: &[usize],
) -> Option<usize> {
    if filtered_indices.is_empty() {
        return None;
    }

    active_index
        .filter(|index| filtered_indices.contains(index))
        .or_else(|| selected.filter(|index| filtered_indices.contains(index)))
        .or_else(|| filtered_indices.first().copied())
}

#[cfg(test)]
mod tests {
    use super::{filtered_item_indices, normalized_active_index};

    #[test]
    fn filter_returns_all_for_empty_query() {
        let items = ["Alpha".into(), "Beta".into(), "Gamma".into()];
        let filtered = filtered_item_indices(&items, "");
        assert_eq!(filtered, vec![0, 1, 2]);
    }

    #[test]
    fn filter_is_case_insensitive() {
        let items = ["Alpha".into(), "Beta".into(), "Gamma".into()];
        let filtered = filtered_item_indices(&items, "AL");
        assert_eq!(filtered, vec![0]);
    }

    #[test]
    fn normalized_active_index_prefers_explicit_highlight() {
        let filtered = vec![2, 4, 6];
        let resolved = normalized_active_index(Some(4), Some(2), filtered.as_slice());
        assert_eq!(resolved, Some(4));
    }

    #[test]
    fn normalized_active_index_falls_back_to_selected_then_first() {
        let filtered = vec![1, 3, 5];
        let resolved = normalized_active_index(Some(2), Some(3), filtered.as_slice());
        assert_eq!(resolved, Some(3));

        let resolved = normalized_active_index(Some(2), Some(4), filtered.as_slice());
        assert_eq!(resolved, Some(1));
    }
}