revue/utils/selection/types.rs
1//! List selection with viewport scrolling - type definitions
2
3use std::cell::Cell;
4
5/// Wrap-around index navigation with viewport scrolling
6///
7/// Uses `Cell` for offset/visible to allow updates from immutable context (e.g., render).
8#[derive(Clone, Debug)]
9pub struct Selection {
10 /// Currently selected index
11 pub index: usize,
12 /// Total number of items
13 pub len: usize,
14 /// First visible item (scroll offset)
15 pub(crate) offset: Cell<usize>,
16 /// Number of visible items
17 pub(crate) visible: Cell<usize>,
18}
19
20use std::collections::HashMap;
21
22/// Selection state for list view with collapsible sections
23///
24/// Commonly used in views where items are grouped by status, category, etc.
25/// and each group can be collapsed/expanded.
26///
27/// # Example
28///
29/// ```ignore
30/// use revue::utils::SectionedSelection;
31///
32/// let mut sel = SectionedSelection::new();
33///
34/// // Navigate
35/// sel.next(&[5, 3, 2]); // section_sizes = [5 items, 3 items, 2 items]
36/// sel.prev(&[5, 3, 2]);
37///
38/// // Toggle section
39/// sel.toggle_section();
40///
41/// // Check state
42/// if !sel.is_section_collapsed(0) {
43/// // Render section 0 items...
44/// }
45/// ```
46#[derive(Clone, Debug)]
47pub struct SectionedSelection {
48 /// Currently selected section index
49 pub section: usize,
50 /// Currently selected item index within the section
51 pub item: usize,
52 /// Map of section index -> collapsed state
53 pub collapsed: HashMap<usize, bool>,
54}