Skip to main content

vizia_core/views/
list.rs

1use std::{
2    collections::BTreeSet,
3    ops::Deref,
4    rc::Rc,
5    time::{Duration, Instant},
6};
7use vizia_reactive::{Scope, SignalGet, SignalWith, UpdaterEffect};
8
9use crate::prelude::*;
10use crate::{binding::BindingHandler, context::SIGNAL_REBUILDS, context::SignalRebuild};
11
12/// Represents how items can be selected in a list.
13#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
14pub enum Selectable {
15    #[default]
16    /// Items in the list cannot be selected.
17    None,
18    /// A single item in the list can be selected.
19    Single,
20    /// Multiple items in the list can be selected simultaneously.
21    Multi,
22}
23
24impl_res_simple!(Selectable);
25
26/// Events used by the [List] view
27pub enum ListEvent {
28    /// Selects a list item with the given index.
29    Select(usize),
30    /// Selects the focused list item.
31    SelectFocused,
32    /// Moves focus to a specific item index without changing selection.
33    Focus(usize),
34    ///  Moves the focus to the next item in the list.
35    FocusNext,
36    ///  Moves the focus to the previous item in the list.
37    FocusPrev,
38    /// Moves the focus to the first item in the list.
39    FocusFirst,
40    /// Moves the focus to the last item in the list.
41    FocusLast,
42    /// Deselects all items from the list
43    ClearSelection,
44    /// Scrolls the list to the given x and y position.
45    Scroll(f32, f32),
46}
47
48/// A view for creating a list of items from an iterable signal.
49pub struct List {
50    /// The number of items in the list.
51    num_items: usize,
52    /// The set of selected items in the list.
53    selection: Signal<BTreeSet<usize>>,
54    /// Whether the list items are selectable.
55    selectable: Signal<Selectable>,
56    /// The index of the currently focused item in the list.
57    focused: Signal<Option<usize>>,
58    /// Whether the selection should follow the focus.
59    selection_follows_focus: Signal<bool>,
60    /// Whether pressing Space should select the focused item.
61    space_selects_focused: Signal<bool>,
62    /// Minimum number of selected items.
63    min_selected: Signal<usize>,
64    /// Maximum number of selected items.
65    max_selected: Signal<usize>,
66    /// The orientation of the list, either vertical or horizontal.
67    orientation: Signal<Orientation>,
68    /// Whether the scrollview should scroll to the cursor when the scrollbar is pressed.
69    scroll_to_cursor: Signal<bool>,
70    /// Whether the first item should be focused when the list gains focus with no selection.
71    focus_first_item_on_focus_in: Signal<bool>,
72    /// Callback called when a list item is selected.
73    on_select: Option<Box<dyn Fn(&mut EventContext, usize)>>,
74    /// Callback called when the focused list item changes.
75    on_focus: Option<Box<dyn Fn(&mut EventContext, usize)>>,
76    /// Callback called when the scrollview is scrolled.
77    on_scroll: Option<Box<dyn Fn(&mut EventContext, f32, f32) + Send + Sync>>,
78    /// The horizontal scroll position of the list.
79    scroll_x: Signal<f32>,
80    /// The vertical scroll position of the list.
81    scroll_y: Signal<f32>,
82    /// Whether the horizontal scrollbar should be visible.
83    show_horizontal_scrollbar: Signal<bool>,
84    /// Whether the vertical scrollbar should be visible.
85    show_vertical_scrollbar: Signal<bool>,
86    /// Whether focused list items should show focus visibility.
87    focus_visibility: Signal<bool>,
88    /// Returns the searchable text for each item index when type-ahead is enabled.
89    type_ahead_text: Option<Box<dyn Fn(&mut EventContext, usize) -> Option<String>>>,
90    /// Buffered type-ahead query built from rapid character input.
91    type_ahead_buffer: String,
92    /// Timestamp of the last accepted type-ahead character.
93    type_ahead_last_input: Option<Instant>,
94    /// Maximum elapsed time before resetting type-ahead buffer.
95    type_ahead_timeout: Duration,
96}
97
98/// A binding handler that manages list item entities for a [List].
99///
100/// The user provides `Vec<T>` wrapped in an outer signal.
101/// This handler creates internal signals for each item and maintains them.
102/// Value changes to existing items update their internal signals (zero entity rebuilds).
103/// Structural changes (add/remove/reorder) are handled by diffing values and rebuilding from the first changed position.
104struct ListItemsBinding<T: 'static> {
105    entity: Entity,
106    list_entity: Entity,
107    get_fn: Box<dyn Fn() -> Vec<T>>,
108    item_content: Rc<dyn Fn(&mut Context, usize, Signal<T>)>,
109    selection: Signal<BTreeSet<usize>>,
110    focused: Signal<Option<usize>>,
111    focus_visibility: Signal<bool>,
112    /// Internal signals for each list item.
113    item_signals: Vec<Signal<T>>,
114    /// Entity IDs of the ListItem views.
115    item_entities: Vec<Entity>,
116    /// Previous values, used for value-based diffing.
117    prev_values: Vec<T>,
118    scope: Scope,
119}
120
121/// A binding handler that manages caller-provided list item entities for a [List].
122///
123/// This variant keeps list diffing and metadata updates but does not wrap items in [ListItem],
124/// allowing callers to provide their own semantics and interaction behavior.
125struct CustomListItemsBinding<T: 'static> {
126    entity: Entity,
127    list_entity: Entity,
128    get_fn: Box<dyn Fn() -> Vec<T>>,
129    item_content: Rc<dyn for<'a> Fn(&'a mut Context, usize, Signal<T>, Memo<bool>) -> Entity>,
130    selection: Signal<BTreeSet<usize>>,
131    /// Internal signals for each list item.
132    item_signals: Vec<Signal<T>>,
133    /// Entity IDs of caller-built item views.
134    item_entities: Vec<Entity>,
135    /// Previous values, used for value-based diffing.
136    prev_values: Vec<T>,
137    scope: Scope,
138}
139
140impl<T: PartialEq + Clone + 'static> ListItemsBinding<T> {
141    fn create<S, V>(
142        cx: &mut Context,
143        list_entity: Entity,
144        list: S,
145        selection: Signal<BTreeSet<usize>>,
146        focused: Signal<Option<usize>>,
147        focus_visibility: Signal<bool>,
148        item_content: Rc<dyn Fn(&mut Context, usize, Signal<T>)>,
149    ) where
150        S: SignalGet<V> + SignalWith<V> + Copy + 'static,
151        V: Deref<Target = [T]> + Clone + 'static,
152    {
153        let entity = cx.entity_manager.create();
154        let context_id = cx.context_id;
155        cx.tree.add(entity, cx.current()).expect("Failed to add to tree");
156        cx.tree.set_ignored(entity, true);
157
158        let scope = Scope::new();
159        let initial_values: Vec<T> = scope.enter(|| {
160            UpdaterEffect::new(
161                move || list.with(|list| list.deref().to_vec()),
162                move |_new_value| {
163                    SIGNAL_REBUILDS.with_borrow_mut(|set| {
164                        set.insert(SignalRebuild { context_id, entity });
165                    });
166                },
167            )
168        });
169
170        let mut binding = Self {
171            entity,
172            list_entity,
173            get_fn: Box::new(move || list.with_untracked(|list| list.deref().to_vec())),
174            item_content,
175            selection,
176            focused,
177            focus_visibility,
178            item_signals: Vec::new(),
179            item_entities: Vec::new(),
180            prev_values: Vec::new(),
181            scope,
182        };
183
184        // Build initial items.
185        for (index, value) in initial_values.iter().enumerate() {
186            let signal = Signal::new(value.clone());
187            let entity = binding.create_item_entity(cx, index, signal);
188            binding.item_signals.push(signal);
189            binding.item_entities.push(entity);
190            binding.prev_values.push(value.clone());
191        }
192        binding.update_list_metadata(cx, initial_values.len());
193
194        cx.bindings.insert(entity, Box::new(binding));
195
196        let _: Handle<Self> =
197            Handle { current: entity, entity, p: Default::default(), cx }.ignore();
198    }
199
200    fn update_list_metadata(&self, cx: &mut Context, len: usize) {
201        if let Some(view) = cx.views.get_mut(&self.list_entity)
202            && let Some(list) = view.downcast_mut::<List>()
203        {
204            list.num_items = len;
205            list.normalize_selection_state();
206        }
207    }
208
209    fn create_item_entity(&self, cx: &mut Context, index: usize, signal: Signal<T>) -> Entity {
210        let mut created = Entity::null();
211        let item_content = self.item_content.clone();
212        let selection = self.selection;
213        let focused = self.focused;
214        let focus_visibility = self.focus_visibility;
215
216        cx.with_current(self.entity, |cx| {
217            created = ListItem::new(cx, index, signal, selection, focused, focus_visibility, {
218                let item_content = item_content.clone();
219                move |cx, index, item| (item_content)(cx, index, item)
220            })
221            .entity();
222        });
223
224        created
225    }
226}
227
228impl<T: PartialEq + Clone + 'static> BindingHandler for ListItemsBinding<T> {
229    fn update(&mut self, cx: &mut Context) {
230        let new_values = (self.get_fn)();
231        let new_len = new_values.len();
232
233        // Find the first position where values differ.
234        let first_diff = self
235            .prev_values
236            .iter()
237            .zip(new_values.iter())
238            .position(|(old, new)| old != new)
239            .unwrap_or(self.prev_values.len().min(new_len));
240
241        // Remove all entities from first_diff onward.
242        for entity in self.item_entities.drain(first_diff..) {
243            cx.remove(entity);
244        }
245        self.item_signals.truncate(first_diff);
246
247        // Update existing signals or create new items from first_diff onward.
248        for (i, value) in new_values[first_diff..].iter().enumerate() {
249            let index = first_diff + i;
250            if index < self.item_signals.len() {
251                // Update existing signal
252                self.item_signals[index].set(value.clone());
253            } else {
254                // Create new signal and item
255                let signal = Signal::new(value.clone());
256                let entity = self.create_item_entity(cx, index, signal);
257                self.item_signals.push(signal);
258                self.item_entities.push(entity);
259            }
260        }
261
262        self.prev_values = new_values;
263        self.update_list_metadata(cx, new_len);
264    }
265
266    fn remove(&self, _cx: &mut Context) {
267        self.scope.dispose();
268    }
269
270    fn debug(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
271        f.write_str("ListItemsBinding")
272    }
273}
274
275impl<T: PartialEq + Clone + 'static> CustomListItemsBinding<T> {
276    fn create<S, V>(
277        cx: &mut Context,
278        list_entity: Entity,
279        list: S,
280        selection: Signal<BTreeSet<usize>>,
281        item_content: Rc<dyn for<'a> Fn(&'a mut Context, usize, Signal<T>, Memo<bool>) -> Entity>,
282    ) where
283        S: SignalGet<V> + SignalWith<V> + Copy + 'static,
284        V: Deref<Target = [T]> + Clone + 'static,
285    {
286        let entity = cx.entity_manager.create();
287        let context_id = cx.context_id;
288        cx.tree.add(entity, cx.current()).expect("Failed to add to tree");
289        cx.tree.set_ignored(entity, true);
290
291        let scope = Scope::new();
292        let initial_values: Vec<T> = scope.enter(|| {
293            UpdaterEffect::new(
294                move || list.with(|list| list.deref().to_vec()),
295                move |_new_value| {
296                    SIGNAL_REBUILDS.with_borrow_mut(|set| {
297                        set.insert(SignalRebuild { context_id, entity });
298                    });
299                },
300            )
301        });
302
303        let mut binding = Self {
304            entity,
305            list_entity,
306            get_fn: Box::new(move || list.with_untracked(|list| list.deref().to_vec())),
307            item_content,
308            selection,
309            item_signals: Vec::new(),
310            item_entities: Vec::new(),
311            prev_values: Vec::new(),
312            scope,
313        };
314
315        // Build initial items.
316        for (index, value) in initial_values.iter().enumerate() {
317            let signal = Signal::new(value.clone());
318            let entity = binding.create_item_entity(cx, index, signal);
319            binding.item_signals.push(signal);
320            binding.item_entities.push(entity);
321            binding.prev_values.push(value.clone());
322        }
323        binding.update_list_metadata(cx, initial_values.len());
324
325        cx.bindings.insert(entity, Box::new(binding));
326
327        let _: Handle<Self> =
328            Handle { current: entity, entity, p: Default::default(), cx }.ignore();
329    }
330
331    fn update_list_metadata(&self, cx: &mut Context, len: usize) {
332        if let Some(view) = cx.views.get_mut(&self.list_entity)
333            && let Some(list) = view.downcast_mut::<List>()
334        {
335            list.num_items = len;
336            list.normalize_selection_state();
337        }
338    }
339
340    fn create_item_entity(&self, cx: &mut Context, index: usize, signal: Signal<T>) -> Entity {
341        let item_content = self.item_content.clone();
342        let selection = self.selection;
343        let mut created = Entity::null();
344
345        cx.with_current(self.entity, |cx| {
346            let is_selected = selection.map(move |selection| selection.contains(&index));
347            created = (item_content)(cx, index, signal, is_selected);
348        });
349
350        created
351    }
352}
353
354impl<T: PartialEq + Clone + 'static> BindingHandler for CustomListItemsBinding<T> {
355    fn update(&mut self, cx: &mut Context) {
356        let new_values = (self.get_fn)();
357        let new_len = new_values.len();
358
359        // Find the first position where values differ.
360        let first_diff = self
361            .prev_values
362            .iter()
363            .zip(new_values.iter())
364            .position(|(old, new)| old != new)
365            .unwrap_or(self.prev_values.len().min(new_len));
366
367        // Remove all entities from first_diff onward.
368        for entity in self.item_entities.drain(first_diff..) {
369            cx.remove(entity);
370        }
371        self.item_signals.truncate(first_diff);
372
373        // Update existing signals or create new items from first_diff onward.
374        for (i, value) in new_values[first_diff..].iter().enumerate() {
375            let index = first_diff + i;
376            if index < self.item_signals.len() {
377                self.item_signals[index].set(value.clone());
378            } else {
379                let signal = Signal::new(value.clone());
380                let entity = self.create_item_entity(cx, index, signal);
381                self.item_signals.push(signal);
382                self.item_entities.push(entity);
383            }
384        }
385
386        self.prev_values = new_values;
387        self.update_list_metadata(cx, new_len);
388    }
389
390    fn remove(&self, _cx: &mut Context) {
391        self.scope.dispose();
392    }
393
394    fn debug(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
395        f.write_str("CustomListItemsBinding")
396    }
397}
398
399impl List {
400    fn set_focused_with_callback(&mut self, cx: &mut EventContext, focused: Option<usize>) {
401        let previous = self.focused.get();
402        self.focused.set(focused);
403
404        if previous != focused
405            && let (Some(index), Some(callback)) = (focused, self.on_focus.as_ref())
406        {
407            callback(cx, index);
408        }
409    }
410
411    fn find_type_ahead_match(
412        &self,
413        cx: &mut EventContext,
414        query: &str,
415        start_index: usize,
416    ) -> Option<usize> {
417        let get_text = self.type_ahead_text.as_ref()?;
418        if self.num_items == 0 {
419            return None;
420        }
421
422        for offset in 0..self.num_items {
423            let index = (start_index + offset) % self.num_items;
424            let item_text = get_text(cx, index)
425                .map(|text| text.trim_start().to_lowercase())
426                .unwrap_or_default();
427
428            if !item_text.is_empty() && item_text.starts_with(query) {
429                return Some(index);
430            }
431        }
432
433        None
434    }
435
436    fn try_type_ahead(&mut self, cx: &mut EventContext, typed: char) -> bool {
437        if self.type_ahead_text.is_none() || self.num_items == 0 {
438            return false;
439        }
440
441        if typed.is_control() || typed.is_whitespace() {
442            return false;
443        }
444
445        let now = Instant::now();
446        let within_timeout = self
447            .type_ahead_last_input
448            .is_some_and(|last| now.saturating_duration_since(last) <= self.type_ahead_timeout);
449
450        let ch = typed.to_lowercase().collect::<String>();
451        let query = if within_timeout {
452            let repeated_char_cycle = !self.type_ahead_buffer.is_empty()
453                && self.type_ahead_buffer.chars().all(|c| c == typed.to_ascii_lowercase());
454
455            if repeated_char_cycle {
456                ch.clone()
457            } else {
458                format!("{}{}", self.type_ahead_buffer, ch)
459            }
460        } else {
461            ch.clone()
462        };
463
464        let start_index =
465            self.focused.get().map(|focused| (focused + 1) % self.num_items).unwrap_or(0);
466
467        if let Some(index) = self.find_type_ahead_match(cx, &query, start_index) {
468            self.type_ahead_buffer = query;
469            self.type_ahead_last_input = Some(now);
470            self.focus_visibility.set(true);
471            self.set_focused_with_callback(cx, Some(index));
472
473            if self.selection_follows_focus.get() {
474                cx.emit(ListEvent::SelectFocused);
475            }
476
477            true
478        } else {
479            self.type_ahead_buffer.clear();
480            self.type_ahead_last_input = Some(now);
481            false
482        }
483    }
484
485    fn selection_limits(&self) -> (usize, usize) {
486        let mut min_selected = self.min_selected.get();
487        let mut max_selected = self.max_selected.get();
488
489        match self.selectable.get() {
490            Selectable::None => {
491                min_selected = 0;
492                max_selected = 0;
493            }
494
495            Selectable::Single => {
496                min_selected = min_selected.min(1);
497                max_selected = 1;
498            }
499
500            Selectable::Multi => {}
501        }
502
503        max_selected = max_selected.min(self.num_items);
504        min_selected = min_selected.min(max_selected);
505
506        (min_selected, max_selected)
507    }
508
509    fn normalize_selection_state(&mut self) {
510        let (min_selected, max_selected) = self.selection_limits();
511
512        let mut selection = self.selection.get();
513        selection.retain(|index| *index < self.num_items);
514
515        while selection.len() > max_selected {
516            if let Some(last) = selection.iter().next_back().copied() {
517                selection.remove(&last);
518            } else {
519                break;
520            }
521        }
522
523        if selection.len() < min_selected {
524            for index in 0..self.num_items {
525                selection.insert(index);
526                if selection.len() >= min_selected {
527                    break;
528                }
529            }
530        }
531
532        let mut focused = self.focused.get();
533        if focused.is_some_and(|index| index >= self.num_items) {
534            focused = self.num_items.checked_sub(1);
535        }
536
537        self.selection.set(selection);
538        self.focused.set(focused);
539    }
540
541    /// Creates a new [List] view from a reactive or static list of values.
542    ///
543    /// `list` accepts any [`Res<V>`] source where `V` derefs to `[T]` — for example a
544    /// `Signal<Vec<T>>` for a reactive list, or a plain `Vec<T>` for a static list.
545    /// The list creates and manages internal signals for each item automatically.
546    /// Value changes to existing items update their internal signals with zero entity rebuilds.
547    /// Structural changes (add/remove/reorder) are handled by diffing values and rebuilding from the first changed position.
548    pub fn new<S, V, T>(
549        cx: &mut Context,
550        list: S,
551        item_content: impl 'static + Fn(&mut Context, usize, Signal<T>),
552    ) -> Handle<Self>
553    where
554        S: Res<V> + 'static,
555        V: Deref<Target = [T]> + Clone + 'static,
556        T: PartialEq + Clone + 'static,
557    {
558        let content: Rc<dyn Fn(&mut Context, usize, Signal<T>)> = Rc::new(item_content);
559        let selection = Signal::new(BTreeSet::default());
560        let selectable = Signal::new(Selectable::None);
561        let focused = Signal::new(None);
562        let min_selected = Signal::new(0);
563        let max_selected = Signal::new(usize::MAX);
564        let orientation = Signal::new(Orientation::Vertical);
565        let scroll_to_cursor = Signal::new(false);
566        let focus_first_item_on_focus_in = Signal::new(true);
567        let scroll_x = Signal::new(0.0);
568        let scroll_y = Signal::new(0.0);
569        let show_horizontal_scrollbar = Signal::new(true);
570        let show_vertical_scrollbar = Signal::new(true);
571        let focus_visibility = Signal::new(false);
572
573        Self {
574            num_items: 0,
575            selection,
576            selectable,
577            focused,
578            selection_follows_focus: Signal::new(false),
579            space_selects_focused: Signal::new(true),
580            min_selected,
581            max_selected,
582            orientation,
583            scroll_to_cursor,
584            focus_first_item_on_focus_in,
585            on_select: None,
586            on_focus: None,
587            on_scroll: None,
588            scroll_x,
589            scroll_y,
590            show_horizontal_scrollbar,
591            show_vertical_scrollbar,
592            focus_visibility,
593            type_ahead_text: None,
594            type_ahead_buffer: String::new(),
595            type_ahead_last_input: None,
596            type_ahead_timeout: Duration::from_millis(1000),
597        }
598        .build(cx, move |cx| {
599            let list_entity = cx.current();
600
601            Keymap::from(vec![
602                (
603                    KeyChord::new(Modifiers::empty(), Code::ArrowDown),
604                    KeymapEntry::new("Focus Next", |cx| cx.emit(ListEvent::FocusNext)),
605                ),
606                (
607                    KeyChord::new(Modifiers::empty(), Code::ArrowUp),
608                    KeymapEntry::new("Focus Previous", |cx| cx.emit(ListEvent::FocusPrev)),
609                ),
610                (
611                    KeyChord::new(Modifiers::empty(), Code::Home),
612                    KeymapEntry::new("Focus First", |cx| cx.emit(ListEvent::FocusFirst)),
613                ),
614                (
615                    KeyChord::new(Modifiers::empty(), Code::End),
616                    KeymapEntry::new("Focus Last", |cx| cx.emit(ListEvent::FocusLast)),
617                ),
618                (
619                    KeyChord::new(Modifiers::empty(), Code::Enter),
620                    KeymapEntry::new("Select Focused", |cx| cx.emit(ListEvent::SelectFocused)),
621                ),
622            ])
623            .build(cx);
624
625            Binding::new(cx, orientation, move |cx| {
626                let orientation = orientation.get();
627                if orientation == Orientation::Horizontal {
628                    cx.emit(KeymapEvent::RemoveAction(
629                        KeyChord::new(Modifiers::empty(), Code::ArrowDown),
630                        "Focus Next",
631                    ));
632
633                    cx.emit(KeymapEvent::RemoveAction(
634                        KeyChord::new(Modifiers::empty(), Code::ArrowUp),
635                        "Focus Previous",
636                    ));
637
638                    cx.emit(KeymapEvent::InsertAction(
639                        KeyChord::new(Modifiers::empty(), Code::ArrowRight),
640                        KeymapEntry::new("Focus Next", |cx| cx.emit(ListEvent::FocusNext)),
641                    ));
642
643                    cx.emit(KeymapEvent::InsertAction(
644                        KeyChord::new(Modifiers::empty(), Code::ArrowLeft),
645                        KeymapEntry::new("Focus Previous", |cx| cx.emit(ListEvent::FocusPrev)),
646                    ));
647                } else {
648                    cx.emit(KeymapEvent::RemoveAction(
649                        KeyChord::new(Modifiers::empty(), Code::ArrowRight),
650                        "Focus Next",
651                    ));
652
653                    cx.emit(KeymapEvent::RemoveAction(
654                        KeyChord::new(Modifiers::empty(), Code::ArrowLeft),
655                        "Focus Previous",
656                    ));
657
658                    cx.emit(KeymapEvent::InsertAction(
659                        KeyChord::new(Modifiers::empty(), Code::ArrowDown),
660                        KeymapEntry::new("Focus Next", |cx| cx.emit(ListEvent::FocusNext)),
661                    ));
662
663                    cx.emit(KeymapEvent::InsertAction(
664                        KeyChord::new(Modifiers::empty(), Code::ArrowUp),
665                        KeymapEntry::new("Focus Previous", |cx| cx.emit(ListEvent::FocusPrev)),
666                    ));
667                }
668            });
669
670            let list_signal = list.to_signal(cx);
671            ScrollView::new(cx, move |cx| {
672                ListItemsBinding::create(
673                    cx,
674                    list_entity,
675                    list_signal,
676                    selection,
677                    focused,
678                    focus_visibility,
679                    content.clone(),
680                );
681            })
682            .show_horizontal_scrollbar(show_horizontal_scrollbar)
683            .show_vertical_scrollbar(show_vertical_scrollbar)
684            .scroll_to_cursor(scroll_to_cursor)
685            .scroll_x(scroll_x)
686            .scroll_y(scroll_y)
687            .on_scroll(|cx, x, y| {
688                if y.is_finite() {
689                    cx.emit(ListEvent::Scroll(x, y));
690                }
691            });
692        })
693        .toggle_class("selectable", selectable.map(|s| *s != Selectable::None))
694        .multiselectable(selectable.map(|s| *s == Selectable::Multi))
695        .orientation(orientation)
696        .navigable(true)
697        .role(Role::ListBox)
698    }
699
700    /// Creates a new [List] view from a reactive or static list of values using caller-provided
701    /// item views directly, without wrapping each item in a [ListItem].
702    ///
703    /// This keeps the list's diffing, keyboard, focus, and scrolling behavior while allowing
704    /// custom item semantics.
705    pub fn new_custom_items<S, V, T, H>(
706        cx: &mut Context,
707        list: S,
708        item_content: impl 'static + for<'a> Fn(&'a mut Context, usize, Signal<T>) -> Handle<'a, H>,
709    ) -> Handle<Self>
710    where
711        S: Res<V> + 'static,
712        V: Deref<Target = [T]> + Clone + 'static,
713        T: PartialEq + Clone + 'static,
714        H: View,
715    {
716        Self::new_custom_items_with_selection(cx, list, move |cx, index, item, _is_selected| {
717            item_content(cx, index, item)
718        })
719    }
720
721    /// Creates a new [List] view from a reactive or static list of values using caller-provided
722    /// item views directly, without wrapping each item in a [ListItem], and provides each item
723    /// with a memo of whether it is currently selected in this list.
724    pub fn new_custom_items_with_selection<S, V, T, H>(
725        cx: &mut Context,
726        list: S,
727        item_content: impl 'static
728        + for<'a> Fn(&'a mut Context, usize, Signal<T>, Memo<bool>) -> Handle<'a, H>,
729    ) -> Handle<Self>
730    where
731        S: Res<V> + 'static,
732        V: Deref<Target = [T]> + Clone + 'static,
733        T: PartialEq + Clone + 'static,
734        H: View,
735    {
736        let selection = Signal::new(BTreeSet::default());
737        let selectable = Signal::new(Selectable::None);
738        let focused = Signal::new(None);
739        let min_selected = Signal::new(0);
740        let max_selected = Signal::new(usize::MAX);
741        let orientation = Signal::new(Orientation::Vertical);
742        let scroll_to_cursor = Signal::new(false);
743        let focus_first_item_on_focus_in = Signal::new(true);
744        let scroll_x = Signal::new(0.0);
745        let scroll_y = Signal::new(0.0);
746        let show_horizontal_scrollbar = Signal::new(true);
747        let show_vertical_scrollbar = Signal::new(true);
748        let focus_visibility = Signal::new(false);
749
750        let content: Rc<dyn for<'a> Fn(&'a mut Context, usize, Signal<T>, Memo<bool>) -> Entity> =
751            Rc::new(move |cx, index, item, is_selected| {
752                let is_focused = focused.map(move |focused| focused.is_some_and(|f| f == index));
753                item_content(cx, index, item, is_selected)
754                    .focusable(true)
755                    .navigable(false)
756                    .focused_with_visibility(is_focused, focus_visibility)
757                    .entity()
758            });
759
760        Self {
761            num_items: 0,
762            selection,
763            selectable,
764            focused,
765            selection_follows_focus: Signal::new(false),
766            space_selects_focused: Signal::new(true),
767            min_selected,
768            max_selected,
769            orientation,
770            scroll_to_cursor,
771            focus_first_item_on_focus_in,
772            on_select: None,
773            on_focus: None,
774            on_scroll: None,
775            scroll_x,
776            scroll_y,
777            show_horizontal_scrollbar,
778            show_vertical_scrollbar,
779            focus_visibility,
780            type_ahead_text: None,
781            type_ahead_buffer: String::new(),
782            type_ahead_last_input: None,
783            type_ahead_timeout: Duration::from_millis(1000),
784        }
785        .build(cx, move |cx| {
786            let list_entity = cx.current();
787
788            Keymap::from(vec![
789                (
790                    KeyChord::new(Modifiers::empty(), Code::ArrowDown),
791                    KeymapEntry::new("Focus Next", |cx| cx.emit(ListEvent::FocusNext)),
792                ),
793                (
794                    KeyChord::new(Modifiers::empty(), Code::ArrowUp),
795                    KeymapEntry::new("Focus Previous", |cx| cx.emit(ListEvent::FocusPrev)),
796                ),
797                (
798                    KeyChord::new(Modifiers::empty(), Code::Home),
799                    KeymapEntry::new("Focus First", |cx| cx.emit(ListEvent::FocusFirst)),
800                ),
801                (
802                    KeyChord::new(Modifiers::empty(), Code::End),
803                    KeymapEntry::new("Focus Last", |cx| cx.emit(ListEvent::FocusLast)),
804                ),
805                (
806                    KeyChord::new(Modifiers::empty(), Code::Enter),
807                    KeymapEntry::new("Select Focused", |cx| cx.emit(ListEvent::SelectFocused)),
808                ),
809            ])
810            .build(cx);
811
812            Binding::new(cx, orientation, move |cx| {
813                let orientation = orientation.get();
814                if orientation == Orientation::Horizontal {
815                    cx.emit(KeymapEvent::RemoveAction(
816                        KeyChord::new(Modifiers::empty(), Code::ArrowDown),
817                        "Focus Next",
818                    ));
819
820                    cx.emit(KeymapEvent::RemoveAction(
821                        KeyChord::new(Modifiers::empty(), Code::ArrowUp),
822                        "Focus Previous",
823                    ));
824
825                    cx.emit(KeymapEvent::InsertAction(
826                        KeyChord::new(Modifiers::empty(), Code::ArrowRight),
827                        KeymapEntry::new("Focus Next", |cx| cx.emit(ListEvent::FocusNext)),
828                    ));
829
830                    cx.emit(KeymapEvent::InsertAction(
831                        KeyChord::new(Modifiers::empty(), Code::ArrowLeft),
832                        KeymapEntry::new("Focus Previous", |cx| cx.emit(ListEvent::FocusPrev)),
833                    ));
834                } else {
835                    cx.emit(KeymapEvent::RemoveAction(
836                        KeyChord::new(Modifiers::empty(), Code::ArrowRight),
837                        "Focus Next",
838                    ));
839
840                    cx.emit(KeymapEvent::RemoveAction(
841                        KeyChord::new(Modifiers::empty(), Code::ArrowLeft),
842                        "Focus Previous",
843                    ));
844
845                    cx.emit(KeymapEvent::InsertAction(
846                        KeyChord::new(Modifiers::empty(), Code::ArrowDown),
847                        KeymapEntry::new("Focus Next", |cx| cx.emit(ListEvent::FocusNext)),
848                    ));
849
850                    cx.emit(KeymapEvent::InsertAction(
851                        KeyChord::new(Modifiers::empty(), Code::ArrowUp),
852                        KeymapEntry::new("Focus Previous", |cx| cx.emit(ListEvent::FocusPrev)),
853                    ));
854                }
855            });
856
857            let list_signal = list.to_signal(cx);
858            ScrollView::new(cx, move |cx| {
859                CustomListItemsBinding::create(
860                    cx,
861                    list_entity,
862                    list_signal,
863                    selection,
864                    content.clone(),
865                );
866            })
867            .show_horizontal_scrollbar(show_horizontal_scrollbar)
868            .show_vertical_scrollbar(show_vertical_scrollbar)
869            .scroll_to_cursor(scroll_to_cursor)
870            .scroll_x(scroll_x)
871            .scroll_y(scroll_y)
872            .on_scroll(|cx, x, y| {
873                if y.is_finite() {
874                    cx.emit(ListEvent::Scroll(x, y));
875                }
876            });
877        })
878        .toggle_class("selectable", selectable.map(|s| *s != Selectable::None))
879        .multiselectable(selectable.map(|s| *s == Selectable::Multi))
880        .orientation(orientation)
881        .navigable(true)
882    }
883}
884
885impl View for List {
886    fn element(&self) -> Option<&'static str> {
887        Some("list")
888    }
889
890    fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
891        event.map(|window_event, meta| {
892            match window_event {
893                WindowEvent::Press { mouse } => {
894                    self.focus_visibility.set(!*mouse);
895                }
896
897                WindowEvent::FocusIn if meta.target == cx.current() => {
898                    // Focus events originating at root are generated by keyboard navigation
899                    // (e.g. Tab/Shift+Tab), so preserve visible focus in that case.
900                    if meta.origin == Entity::root() {
901                        self.focus_visibility.set(true);
902                    }
903
904                    let next_focused = focus_index_on_focus_in(
905                        &self.selection.get(),
906                        self.num_items,
907                        self.focus_first_item_on_focus_in.get(),
908                    );
909
910                    if let Some(index) = next_focused {
911                        // Force a transition so focused_with_visibility re-applies focus
912                        // to the list item when focus enters the list container.
913                        if self.focused.get() == Some(index) {
914                            self.focused.set(None);
915                        }
916                        self.set_focused_with_callback(cx, Some(index));
917                    }
918                }
919
920                WindowEvent::CharInput(c) => {
921                    if *c == ' ' && meta.target == cx.current() && self.space_selects_focused.get()
922                    {
923                        cx.emit(ListEvent::SelectFocused);
924                        meta.consume();
925                    } else if self.try_type_ahead(cx, *c) {
926                        meta.consume();
927                    }
928                }
929
930                _ => {}
931            }
932        });
933
934        event.take(|list_event, meta| match list_event {
935            ListEvent::Select(index) => {
936                let selectable = self.selectable.get();
937                let (min_selected, max_selected) = self.selection_limits();
938                let mut selection = self.selection.get();
939                let mut focused = self.focused.get();
940                match selectable {
941                    Selectable::Single => {
942                        if selection.contains(&index) {
943                            if min_selected == 0 {
944                                selection.clear();
945                                focused = None;
946                            }
947                        } else {
948                            selection.clear();
949                            selection.insert(index);
950                            focused = Some(index);
951                            if let Some(on_select) = &self.on_select {
952                                on_select(cx, index);
953                            }
954                        }
955                    }
956
957                    Selectable::Multi => {
958                        // In multi-select mode, clicking an item should move focus to that item
959                        // regardless of whether the click selects or deselects it.
960                        focused = Some(index);
961
962                        if selection.contains(&index) {
963                            if selection.len() > min_selected {
964                                selection.remove(&index);
965                                if let Some(on_select) = &self.on_select {
966                                    on_select(cx, index);
967                                }
968                            }
969                        } else if selection.len() < max_selected {
970                            selection.insert(index);
971                            if let Some(on_select) = &self.on_select {
972                                on_select(cx, index);
973                            }
974                        }
975                    }
976
977                    Selectable::None => {}
978                }
979
980                self.selection.set(selection);
981                self.set_focused_with_callback(cx, focused);
982
983                meta.consume();
984            }
985
986            ListEvent::SelectFocused => {
987                if let Some(focused) = self.focused.get() {
988                    self.focus_visibility.set(true);
989                    cx.emit(ListEvent::Select(focused))
990                }
991                meta.consume();
992            }
993
994            ListEvent::Focus(index) => {
995                if index < self.num_items {
996                    self.focus_visibility.set(true);
997                    self.set_focused_with_callback(cx, Some(index));
998                }
999
1000                meta.consume();
1001            }
1002
1003            ListEvent::ClearSelection => {
1004                let (min_selected, _) = self.selection_limits();
1005                if min_selected == 0 {
1006                    self.selection.set(BTreeSet::default());
1007                }
1008                meta.consume();
1009            }
1010
1011            ListEvent::FocusNext => {
1012                let mut focused = self.focused.get();
1013                let mut moved_focus = false;
1014                if let Some(f) = &mut focused {
1015                    if *f < self.num_items.saturating_sub(1) {
1016                        *f = f.saturating_add(1);
1017                        moved_focus = true;
1018                        if self.selection_follows_focus.get() {
1019                            cx.emit(ListEvent::SelectFocused);
1020                        }
1021                    }
1022                } else if self.num_items > 0 {
1023                    focused = Some(0);
1024                    moved_focus = true;
1025                    if self.selection_follows_focus.get() {
1026                        cx.emit(ListEvent::SelectFocused);
1027                    }
1028                }
1029
1030                if moved_focus {
1031                    self.focus_visibility.set(true);
1032                }
1033
1034                self.set_focused_with_callback(cx, focused);
1035
1036                meta.consume();
1037            }
1038
1039            ListEvent::FocusPrev => {
1040                let mut focused = self.focused.get();
1041                let mut moved_focus = false;
1042                if let Some(f) = &mut focused {
1043                    if *f > 0 {
1044                        *f = f.saturating_sub(1);
1045                        moved_focus = true;
1046                        if self.selection_follows_focus.get() {
1047                            cx.emit(ListEvent::SelectFocused);
1048                        }
1049                    }
1050                } else if self.num_items > 0 {
1051                    focused = Some(self.num_items.saturating_sub(1));
1052                    moved_focus = true;
1053                    if self.selection_follows_focus.get() {
1054                        cx.emit(ListEvent::SelectFocused);
1055                    }
1056                }
1057
1058                if moved_focus {
1059                    self.focus_visibility.set(true);
1060                }
1061
1062                self.set_focused_with_callback(cx, focused);
1063
1064                meta.consume();
1065            }
1066
1067            ListEvent::FocusFirst => {
1068                if self.num_items > 0 {
1069                    self.focus_visibility.set(true);
1070                    self.set_focused_with_callback(cx, Some(0));
1071                    if self.selection_follows_focus.get() {
1072                        cx.emit(ListEvent::SelectFocused);
1073                    }
1074                }
1075
1076                meta.consume();
1077            }
1078
1079            ListEvent::FocusLast => {
1080                if self.num_items > 0 {
1081                    self.focus_visibility.set(true);
1082                    self.set_focused_with_callback(cx, Some(self.num_items.saturating_sub(1)));
1083                    if self.selection_follows_focus.get() {
1084                        cx.emit(ListEvent::SelectFocused);
1085                    }
1086                }
1087
1088                meta.consume();
1089            }
1090
1091            ListEvent::Scroll(x, y) => {
1092                self.scroll_x.set(x);
1093                self.scroll_y.set(y);
1094                if let Some(callback) = &self.on_scroll {
1095                    (callback)(cx, x, y);
1096                }
1097
1098                meta.consume();
1099            }
1100        })
1101    }
1102}
1103
1104/// Modifiers for changing the behavior and selection state of a [List].
1105pub trait ListModifiers: Sized {
1106    /// Sets the selected items of the list from signal of type indices.
1107    fn selection<R>(self, selection: impl Res<R> + 'static) -> Self
1108    where
1109        R: Deref<Target = [usize]> + Clone + 'static;
1110
1111    /// Sets the focused item of the list from a signal of an optional index.
1112    fn focused_index(self, focused: impl Res<Option<usize>> + 'static) -> Self;
1113
1114    /// Sets the callback triggered when a [ListItem] is selected.
1115    fn on_select<F>(self, callback: F) -> Self
1116    where
1117        F: 'static + Fn(&mut EventContext, usize);
1118
1119    /// Sets the callback triggered when a [ListItem] receives focus.
1120    fn on_focus<F>(self, callback: F) -> Self
1121    where
1122        F: 'static + Fn(&mut EventContext, usize);
1123
1124    /// Set the selectable state of the [List].
1125    fn selectable<U: Into<Selectable> + Clone + 'static>(
1126        self,
1127        selectable: impl Res<U> + 'static,
1128    ) -> Self;
1129
1130    /// Sets the minimum number of selected items.
1131    fn min_selected(self, min_selected: impl Res<usize> + 'static) -> Self;
1132
1133    /// Sets the maximum number of selected items.
1134    fn max_selected(self, max_selected: impl Res<usize> + 'static) -> Self;
1135
1136    /// Sets whether the selection should follow the focus.
1137    fn selection_follows_focus<U: Into<bool> + Clone + 'static>(
1138        self,
1139        flag: impl Res<U> + 'static,
1140    ) -> Self;
1141
1142    /// Sets whether pressing Space should select the currently focused item.
1143    fn space_selects_focused<U: Into<bool> + Clone + 'static>(
1144        self,
1145        flag: impl Res<U> + 'static,
1146    ) -> Self;
1147
1148    /// Sets the orientation of the list.
1149    fn horizontal<U: Into<bool> + Clone + 'static>(self, horizontal: impl Res<U> + 'static)
1150    -> Self;
1151
1152    /// Sets whether the scrollbar should move to the cursor when pressed.
1153    fn scroll_to_cursor(self, flag: bool) -> Self;
1154
1155    /// Sets whether the first item should be focused when the list gains focus with no selection.
1156    fn focus_first_item_on_focus_in(self, flag: impl Res<bool> + 'static) -> Self;
1157
1158    /// Sets a callback which will be called when a scrollview is scrolled, either with the mouse wheel, touchpad, or using the scroll bars.
1159    fn on_scroll(
1160        self,
1161        callback: impl Fn(&mut EventContext, f32, f32) + 'static + Send + Sync,
1162    ) -> Self;
1163
1164    /// Set the horizontal scroll position of the [ScrollView]. Accepts a value or signal of type an `f32` between 0 and 1.
1165    fn scroll_x(self, scrollx: impl Res<f32> + 'static) -> Self;
1166
1167    /// Set the vertical scroll position of the [ScrollView]. Accepts a value or signal of type an `f32` between 0 and 1.
1168    fn scroll_y(self, scrolly: impl Res<f32> + 'static) -> Self;
1169
1170    /// Sets whether the horizontal scrollbar should be visible.
1171    fn show_horizontal_scrollbar(self, flag: impl Res<bool> + 'static) -> Self;
1172
1173    /// Sets whether the vertical scrollbar should be visible.
1174    fn show_vertical_scrollbar(self, flag: impl Res<bool> + 'static) -> Self;
1175
1176    /// Enables type-ahead navigation by providing searchable text per item index.
1177    fn type_ahead_text<F>(self, callback: F) -> Self
1178    where
1179        F: 'static + Fn(&mut EventContext, usize) -> Option<String>;
1180}
1181
1182impl ListModifiers for Handle<'_, List> {
1183    fn selection<R>(self, selection: impl Res<R> + 'static) -> Self
1184    where
1185        R: Deref<Target = [usize]> + Clone + 'static,
1186    {
1187        let selection = selection.to_signal(self.cx);
1188        self.bind(selection, move |handle| {
1189            selection.with(|selected_indices| {
1190                handle.modify(|list| {
1191                    let previous_focused = list.focused.get();
1192                    let mut selection = BTreeSet::default();
1193                    for idx in selected_indices.deref().iter().copied() {
1194                        selection.insert(idx);
1195                    }
1196
1197                    let focused = previous_focused
1198                        .filter(|idx| *idx < list.num_items)
1199                        .or_else(|| selection.iter().next_back().copied());
1200
1201                    list.selection.set(selection);
1202                    list.focused.set(focused);
1203                    list.normalize_selection_state();
1204                });
1205            });
1206        })
1207    }
1208
1209    fn focused_index(self, focused: impl Res<Option<usize>> + 'static) -> Self {
1210        let focused = focused.to_signal(self.cx);
1211        self.bind(focused, move |handle| {
1212            let focused = focused.get();
1213            handle.modify(|list| {
1214                list.focused.set(normalize_focused_index(focused, list.num_items));
1215            });
1216        })
1217    }
1218
1219    fn on_select<F>(self, callback: F) -> Self
1220    where
1221        F: 'static + Fn(&mut EventContext, usize),
1222    {
1223        self.modify(|list: &mut List| list.on_select = Some(Box::new(callback)))
1224    }
1225
1226    fn on_focus<F>(self, callback: F) -> Self
1227    where
1228        F: 'static + Fn(&mut EventContext, usize),
1229    {
1230        self.modify(|list: &mut List| list.on_focus = Some(Box::new(callback)))
1231    }
1232
1233    fn selectable<U: Into<Selectable> + Clone + 'static>(
1234        self,
1235        selectable: impl Res<U> + 'static,
1236    ) -> Self {
1237        let selectable = selectable.to_signal(self.cx);
1238        self.bind(selectable, move |handle| {
1239            let selectable = selectable.get();
1240            let s = selectable.into();
1241            handle.modify(|list: &mut List| {
1242                list.selectable.set(s);
1243                list.normalize_selection_state();
1244            });
1245        })
1246    }
1247
1248    fn min_selected(self, min_selected: impl Res<usize> + 'static) -> Self {
1249        let min_selected = min_selected.to_signal(self.cx);
1250        self.bind(min_selected, move |handle| {
1251            let min_selected = min_selected.get();
1252            handle.modify(|list: &mut List| {
1253                list.min_selected.set(min_selected);
1254                list.normalize_selection_state();
1255            });
1256        })
1257    }
1258
1259    fn max_selected(self, max_selected: impl Res<usize> + 'static) -> Self {
1260        let max_selected = max_selected.to_signal(self.cx);
1261        self.bind(max_selected, move |handle| {
1262            let max_selected = max_selected.get();
1263            handle.modify(|list: &mut List| {
1264                list.max_selected.set(max_selected);
1265                list.normalize_selection_state();
1266            });
1267        })
1268    }
1269
1270    fn selection_follows_focus<U: Into<bool> + Clone + 'static>(
1271        self,
1272        flag: impl Res<U> + 'static,
1273    ) -> Self {
1274        let flag = flag.to_signal(self.cx);
1275        self.bind(flag, move |handle| {
1276            let selection_follows_focus = flag.get();
1277            let s = selection_follows_focus.into();
1278            handle.modify(|list: &mut List| list.selection_follows_focus.set(s));
1279        })
1280    }
1281
1282    fn space_selects_focused<U: Into<bool> + Clone + 'static>(
1283        self,
1284        flag: impl Res<U> + 'static,
1285    ) -> Self {
1286        let flag = flag.to_signal(self.cx);
1287        self.bind(flag, move |handle| {
1288            let space_selects_focused = flag.get();
1289            let s = space_selects_focused.into();
1290            handle.modify(|list: &mut List| list.space_selects_focused.set(s));
1291        })
1292    }
1293
1294    fn horizontal<U: Into<bool> + Clone + 'static>(
1295        self,
1296        horizontal: impl Res<U> + 'static,
1297    ) -> Self {
1298        let horizontal = horizontal.to_signal(self.cx);
1299        self.bind(horizontal, move |handle| {
1300            let horizontal = horizontal.get();
1301            let horizontal = horizontal.into();
1302            handle.modify(|list: &mut List| {
1303                list.orientation.set(if horizontal {
1304                    Orientation::Horizontal
1305                } else {
1306                    Orientation::Vertical
1307                });
1308            });
1309        })
1310    }
1311
1312    fn scroll_to_cursor(self, flag: bool) -> Self {
1313        self.modify(|list| {
1314            list.scroll_to_cursor.set(flag);
1315        })
1316    }
1317
1318    fn focus_first_item_on_focus_in(self, flag: impl Res<bool> + 'static) -> Self {
1319        let flag = flag.to_signal(self.cx);
1320        self.bind(flag, move |handle| {
1321            let focus_first_item_on_focus_in = flag.get();
1322            handle.modify(|list: &mut List| {
1323                list.focus_first_item_on_focus_in.set(focus_first_item_on_focus_in);
1324            });
1325        })
1326    }
1327
1328    fn on_scroll(
1329        self,
1330        callback: impl Fn(&mut EventContext, f32, f32) + 'static + Send + Sync,
1331    ) -> Self {
1332        self.modify(|list: &mut List| list.on_scroll = Some(Box::new(callback)))
1333    }
1334
1335    fn scroll_x(self, scrollx: impl Res<f32> + 'static) -> Self {
1336        let scrollx = scrollx.to_signal(self.cx);
1337        self.bind(scrollx, move |handle| {
1338            let scrollx = scrollx.get();
1339            handle.modify(|list| {
1340                list.scroll_x.set(scrollx);
1341            });
1342        })
1343    }
1344
1345    fn scroll_y(self, scrolly: impl Res<f32> + 'static) -> Self {
1346        let scrolly = scrolly.to_signal(self.cx);
1347        self.bind(scrolly, move |handle| {
1348            let scrolly = scrolly.get();
1349            handle.modify(|list| {
1350                list.scroll_y.set(scrolly);
1351            });
1352        })
1353    }
1354
1355    fn show_horizontal_scrollbar(self, flag: impl Res<bool> + 'static) -> Self {
1356        let flag = flag.to_signal(self.cx);
1357        self.bind(flag, move |handle| {
1358            let show_scrollbar = flag.get();
1359            handle.modify(|list| {
1360                list.show_horizontal_scrollbar.set(show_scrollbar);
1361            });
1362        })
1363    }
1364
1365    fn show_vertical_scrollbar(self, flag: impl Res<bool> + 'static) -> Self {
1366        let flag = flag.to_signal(self.cx);
1367        self.bind(flag, move |handle| {
1368            let show_scrollbar = flag.get();
1369            handle.modify(|list| {
1370                list.show_vertical_scrollbar.set(show_scrollbar);
1371            });
1372        })
1373    }
1374
1375    fn type_ahead_text<F>(self, callback: F) -> Self
1376    where
1377        F: 'static + Fn(&mut EventContext, usize) -> Option<String>,
1378    {
1379        self.modify(|list: &mut List| list.type_ahead_text = Some(Box::new(callback)))
1380    }
1381}
1382
1383/// A view which represents a selectable item within a list.
1384pub struct ListItem {
1385    selected: Memo<bool>,
1386}
1387
1388impl ListItem {
1389    /// Create a new [ListItem] view.
1390    pub fn new<'a, T: Clone + 'static, M: SignalGet<T> + 'static>(
1391        cx: &'a mut Context,
1392        index: usize,
1393        item: M,
1394        selection: impl SignalMap<BTreeSet<usize>> + SignalGet<BTreeSet<usize>>,
1395        focused: impl SignalMap<Option<usize>>,
1396        focus_visibility: impl Res<bool> + Copy + 'static,
1397        item_content: impl 'static + Fn(&mut Context, usize, M),
1398    ) -> Handle<'a, Self> {
1399        let is_focused =
1400            focused.map(move |focused| focused.as_ref().is_some_and(|f| *f == index)).get();
1401        let focused_signal =
1402            focused.map(move |focused| focused.as_ref().is_some_and(|f| *f == index));
1403        let is_selected = selection.map(move |selection| selection.contains(&index));
1404        Self { selected: is_selected }
1405            .build(cx, move |cx| {
1406                item_content(cx, index, item);
1407            })
1408            .role(Role::ListBoxOption)
1409            .focusable(true)
1410            .navigable(false)
1411            .toggle_class("focused", focused_signal)
1412            .focused_with_visibility(focused_signal, focus_visibility)
1413            .checked(selection.map(move |selection| selection.contains(&index)))
1414            .bind(focused_signal, move |handle| {
1415                let focused = focused_signal.get();
1416                if focused != is_focused {
1417                    handle.cx.emit(ScrollEvent::ScrollToView(handle.entity()));
1418                }
1419            })
1420            .on_press(move |cx| cx.emit(ListEvent::Select(index)))
1421    }
1422}
1423
1424impl View for ListItem {
1425    fn element(&self) -> Option<&'static str> {
1426        Some("list-item")
1427    }
1428
1429    fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
1430        event.map(|window_event, _| match window_event {
1431            WindowEvent::GeometryChanged(geo) => {
1432                if self.selected.get() && geo.contains(GeoChanged::HEIGHT_CHANGED) {
1433                    cx.emit(ScrollEvent::ScrollToView(cx.current()));
1434                }
1435            }
1436            _ => {}
1437        });
1438    }
1439}
1440
1441fn focus_index_on_focus_in(
1442    selection: &BTreeSet<usize>,
1443    num_items: usize,
1444    focus_first_item_on_focus_in: bool,
1445) -> Option<usize> {
1446    selection
1447        .iter()
1448        .copied()
1449        .find(|index| *index < num_items)
1450        .or_else(|| focus_first_item_on_focus_in.then_some(0).filter(|_| num_items > 0))
1451}
1452
1453fn normalize_focused_index(focused: Option<usize>, num_items: usize) -> Option<usize> {
1454    focused.filter(|index| *index < num_items)
1455}
1456
1457#[cfg(test)]
1458mod tests {
1459    use super::*;
1460
1461    #[test]
1462    fn focuses_selected_item_on_focus_in() {
1463        let mut selection = BTreeSet::new();
1464        selection.insert(3);
1465
1466        assert_eq!(focus_index_on_focus_in(&selection, 5, false), Some(3));
1467    }
1468
1469    #[test]
1470    fn can_leave_focus_empty_when_nothing_is_selected() {
1471        let selection = BTreeSet::new();
1472
1473        assert_eq!(focus_index_on_focus_in(&selection, 5, false), None);
1474    }
1475
1476    #[test]
1477    fn falls_back_to_first_item_when_enabled() {
1478        let selection = BTreeSet::new();
1479
1480        assert_eq!(focus_index_on_focus_in(&selection, 5, true), Some(0));
1481    }
1482
1483    #[test]
1484    fn keeps_focused_index_when_in_range() {
1485        assert_eq!(normalize_focused_index(Some(3), 5), Some(3));
1486    }
1487
1488    #[test]
1489    fn clears_focused_index_when_out_of_range() {
1490        assert_eq!(normalize_focused_index(Some(5), 5), None);
1491    }
1492
1493    /// Sends `events` to a fresh list of `num_items` items and returns the indices passed to
1494    /// `on_focus` and `on_select`, in order.
1495    fn focus_and_select_callbacks(
1496        num_items: u32,
1497        events: impl IntoIterator<Item = ListEvent>,
1498    ) -> (Vec<usize>, Vec<usize>) {
1499        use crate::context::WindowState;
1500        use crate::events::EventManager;
1501        use std::cell::RefCell;
1502
1503        let mut cx = Context::default();
1504        cx.windows.insert(Entity::root(), WindowState::default());
1505        let focused = Rc::new(RefCell::new(Vec::new()));
1506        let selected = Rc::new(RefCell::new(Vec::new()));
1507        let (focused_in, selected_in) = (focused.clone(), selected.clone());
1508
1509        let list = Signal::new((0..num_items).collect::<Vec<_>>());
1510        let list_entity = List::new(&mut cx, list, |cx, _, item| {
1511            Label::new(cx, item);
1512        })
1513        .on_focus(move |_, index| focused_in.borrow_mut().push(index))
1514        .on_select(move |_, index| selected_in.borrow_mut().push(index))
1515        .selectable(Selectable::Single)
1516        .selection_follows_focus(true)
1517        .entity();
1518
1519        for event in events {
1520            cx.emit_to(list_entity, event);
1521            EventManager::new().flush_events(&mut cx, |_| {});
1522        }
1523
1524        (focused.take(), selected.take())
1525    }
1526
1527    #[test]
1528    fn focus_next_on_an_empty_list_focuses_nothing() {
1529        assert_eq!(focus_and_select_callbacks(0, [ListEvent::FocusNext]), (vec![], vec![]));
1530    }
1531
1532    #[test]
1533    fn focus_prev_on_an_empty_list_focuses_nothing() {
1534        assert_eq!(focus_and_select_callbacks(0, [ListEvent::FocusPrev]), (vec![], vec![]));
1535    }
1536
1537    #[test]
1538    fn focus_next_without_focus_starts_at_the_first_item() {
1539        assert_eq!(
1540            focus_and_select_callbacks(3, [ListEvent::FocusNext, ListEvent::FocusNext]),
1541            (vec![0, 1], vec![0, 1])
1542        );
1543    }
1544
1545    #[test]
1546    fn focus_prev_without_focus_starts_at_the_last_item() {
1547        assert_eq!(
1548            focus_and_select_callbacks(3, [ListEvent::FocusPrev, ListEvent::FocusPrev]),
1549            (vec![2, 1], vec![2, 1])
1550        );
1551    }
1552}