Skip to main content

slt/
widgets.rs

1//! Widget state types passed to [`Context`](crate::Context) widget methods.
2//!
3//! Each interactive widget (text input, list, tabs, table, etc.) has a
4//! corresponding state struct defined here. Create the state once, then pass
5//! a `&mut` reference each frame.
6
7use std::collections::HashSet;
8use unicode_width::UnicodeWidthStr;
9
10type FormValidator = fn(&str) -> Result<(), String>;
11
12/// State for a single-line text input widget.
13///
14/// Pass a mutable reference to `Context::text_input` each frame. The widget
15/// handles all keyboard events when focused.
16///
17/// # Example
18///
19/// ```no_run
20/// # use slt::widgets::TextInputState;
21/// # slt::run(|ui: &mut slt::Context| {
22/// let mut input = TextInputState::with_placeholder("Type here...");
23/// ui.text_input(&mut input);
24/// println!("{}", input.value);
25/// # });
26/// ```
27pub struct TextInputState {
28    /// The current input text.
29    pub value: String,
30    /// Cursor position as a character index into `value`.
31    pub cursor: usize,
32    /// Placeholder text shown when `value` is empty.
33    pub placeholder: String,
34    /// Maximum character count. Input is rejected beyond this limit.
35    pub max_length: Option<usize>,
36    /// The most recent validation error message, if any.
37    pub validation_error: Option<String>,
38    /// When `true`, input is displayed as `•` characters (for passwords).
39    pub masked: bool,
40}
41
42impl TextInputState {
43    /// Create an empty text input state.
44    pub fn new() -> Self {
45        Self {
46            value: String::new(),
47            cursor: 0,
48            placeholder: String::new(),
49            max_length: None,
50            validation_error: None,
51            masked: false,
52        }
53    }
54
55    /// Create a text input with placeholder text shown when the value is empty.
56    pub fn with_placeholder(p: impl Into<String>) -> Self {
57        Self {
58            placeholder: p.into(),
59            ..Self::new()
60        }
61    }
62
63    /// Set the maximum allowed character count.
64    pub fn max_length(mut self, len: usize) -> Self {
65        self.max_length = Some(len);
66        self
67    }
68
69    /// Validate the current value and store the latest error message.
70    ///
71    /// Sets [`TextInputState::validation_error`] to `None` when validation
72    /// succeeds, or to `Some(error)` when validation fails.
73    pub fn validate(&mut self, validator: impl Fn(&str) -> Result<(), String>) {
74        self.validation_error = validator(&self.value).err();
75    }
76}
77
78impl Default for TextInputState {
79    fn default() -> Self {
80        Self::new()
81    }
82}
83
84/// A single form field with label and validation.
85pub struct FormField {
86    /// Field label shown above the input.
87    pub label: String,
88    /// Text input state for this field.
89    pub input: TextInputState,
90    /// Validation error shown below the input when present.
91    pub error: Option<String>,
92}
93
94impl FormField {
95    /// Create a new form field with the given label.
96    pub fn new(label: impl Into<String>) -> Self {
97        Self {
98            label: label.into(),
99            input: TextInputState::new(),
100            error: None,
101        }
102    }
103
104    /// Set placeholder text for this field's input.
105    pub fn placeholder(mut self, p: impl Into<String>) -> Self {
106        self.input.placeholder = p.into();
107        self
108    }
109}
110
111/// State for a form with multiple fields.
112pub struct FormState {
113    /// Ordered list of form fields.
114    pub fields: Vec<FormField>,
115    /// Whether the form has been successfully submitted.
116    pub submitted: bool,
117}
118
119impl FormState {
120    /// Create an empty form state.
121    pub fn new() -> Self {
122        Self {
123            fields: Vec::new(),
124            submitted: false,
125        }
126    }
127
128    /// Add a field and return the updated form for chaining.
129    pub fn field(mut self, field: FormField) -> Self {
130        self.fields.push(field);
131        self
132    }
133
134    /// Validate all fields with the given validators.
135    ///
136    /// Returns `true` when all validations pass.
137    pub fn validate(&mut self, validators: &[FormValidator]) -> bool {
138        let mut all_valid = true;
139        for (i, field) in self.fields.iter_mut().enumerate() {
140            if let Some(validator) = validators.get(i) {
141                match validator(&field.input.value) {
142                    Ok(()) => field.error = None,
143                    Err(msg) => {
144                        field.error = Some(msg);
145                        all_valid = false;
146                    }
147                }
148            }
149        }
150        all_valid
151    }
152
153    /// Get field value by index.
154    pub fn value(&self, index: usize) -> &str {
155        self.fields
156            .get(index)
157            .map(|f| f.input.value.as_str())
158            .unwrap_or("")
159    }
160}
161
162impl Default for FormState {
163    fn default() -> Self {
164        Self::new()
165    }
166}
167
168/// State for toast notification display.
169///
170/// Add messages with [`ToastState::info`], [`ToastState::success`],
171/// [`ToastState::warning`], or [`ToastState::error`], then pass the state to
172/// `Context::toast` each frame. Expired messages are removed automatically.
173pub struct ToastState {
174    /// Active toast messages, ordered oldest-first.
175    pub messages: Vec<ToastMessage>,
176}
177
178/// A single toast notification message.
179pub struct ToastMessage {
180    /// The text content of the notification.
181    pub text: String,
182    /// Severity level, used to choose the display color.
183    pub level: ToastLevel,
184    /// The tick at which this message was created.
185    pub created_tick: u64,
186    /// How many ticks the message remains visible.
187    pub duration_ticks: u64,
188}
189
190/// Severity level for a [`ToastMessage`].
191pub enum ToastLevel {
192    /// Informational message (primary color).
193    Info,
194    /// Success message (success color).
195    Success,
196    /// Warning message (warning color).
197    Warning,
198    /// Error message (error color).
199    Error,
200}
201
202impl ToastState {
203    /// Create an empty toast state with no messages.
204    pub fn new() -> Self {
205        Self {
206            messages: Vec::new(),
207        }
208    }
209
210    /// Push an informational toast visible for 30 ticks.
211    pub fn info(&mut self, text: impl Into<String>, tick: u64) {
212        self.push(text, ToastLevel::Info, tick, 30);
213    }
214
215    /// Push a success toast visible for 30 ticks.
216    pub fn success(&mut self, text: impl Into<String>, tick: u64) {
217        self.push(text, ToastLevel::Success, tick, 30);
218    }
219
220    /// Push a warning toast visible for 50 ticks.
221    pub fn warning(&mut self, text: impl Into<String>, tick: u64) {
222        self.push(text, ToastLevel::Warning, tick, 50);
223    }
224
225    /// Push an error toast visible for 80 ticks.
226    pub fn error(&mut self, text: impl Into<String>, tick: u64) {
227        self.push(text, ToastLevel::Error, tick, 80);
228    }
229
230    /// Push a toast with a custom level and duration.
231    pub fn push(
232        &mut self,
233        text: impl Into<String>,
234        level: ToastLevel,
235        tick: u64,
236        duration_ticks: u64,
237    ) {
238        self.messages.push(ToastMessage {
239            text: text.into(),
240            level,
241            created_tick: tick,
242            duration_ticks,
243        });
244    }
245
246    /// Remove all messages whose display duration has elapsed.
247    ///
248    /// Called automatically by `Context::toast` before rendering.
249    pub fn cleanup(&mut self, current_tick: u64) {
250        self.messages.retain(|message| {
251            current_tick < message.created_tick.saturating_add(message.duration_ticks)
252        });
253    }
254}
255
256impl Default for ToastState {
257    fn default() -> Self {
258        Self::new()
259    }
260}
261
262/// State for a multi-line text area widget.
263///
264/// Pass a mutable reference to `Context::textarea` each frame along with the
265/// number of visible rows. The widget handles all keyboard events when focused.
266pub struct TextareaState {
267    /// The lines of text, one entry per line.
268    pub lines: Vec<String>,
269    /// Row index of the cursor (0-based, logical line).
270    pub cursor_row: usize,
271    /// Column index of the cursor within the current row (character index).
272    pub cursor_col: usize,
273    /// Maximum total character count across all lines.
274    pub max_length: Option<usize>,
275    /// When set, lines longer than this display-column width are soft-wrapped.
276    pub wrap_width: Option<u32>,
277    /// First visible visual line (managed internally by `textarea()`).
278    pub scroll_offset: usize,
279}
280
281impl TextareaState {
282    /// Create an empty text area state with one blank line.
283    pub fn new() -> Self {
284        Self {
285            lines: vec![String::new()],
286            cursor_row: 0,
287            cursor_col: 0,
288            max_length: None,
289            wrap_width: None,
290            scroll_offset: 0,
291        }
292    }
293
294    /// Return all lines joined with newline characters.
295    pub fn value(&self) -> String {
296        self.lines.join("\n")
297    }
298
299    /// Replace the content with the given text, splitting on newlines.
300    ///
301    /// Resets the cursor to the beginning of the first line.
302    pub fn set_value(&mut self, text: impl Into<String>) {
303        let value = text.into();
304        self.lines = value.split('\n').map(str::to_string).collect();
305        if self.lines.is_empty() {
306            self.lines.push(String::new());
307        }
308        self.cursor_row = 0;
309        self.cursor_col = 0;
310        self.scroll_offset = 0;
311    }
312
313    /// Set the maximum allowed total character count.
314    pub fn max_length(mut self, len: usize) -> Self {
315        self.max_length = Some(len);
316        self
317    }
318
319    /// Enable soft word-wrap at the given display-column width.
320    pub fn word_wrap(mut self, width: u32) -> Self {
321        self.wrap_width = Some(width);
322        self
323    }
324}
325
326impl Default for TextareaState {
327    fn default() -> Self {
328        Self::new()
329    }
330}
331
332/// State for an animated spinner widget.
333///
334/// Create with [`SpinnerState::dots`] or [`SpinnerState::line`], then pass to
335/// `Context::spinner` each frame. The frame advances automatically with the
336/// tick counter.
337pub struct SpinnerState {
338    chars: Vec<char>,
339}
340
341impl SpinnerState {
342    /// Create a dots-style spinner using braille characters.
343    ///
344    /// Cycles through: `⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏`
345    pub fn dots() -> Self {
346        Self {
347            chars: vec!['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'],
348        }
349    }
350
351    /// Create a line-style spinner using ASCII characters.
352    ///
353    /// Cycles through: `| / - \`
354    pub fn line() -> Self {
355        Self {
356            chars: vec!['|', '/', '-', '\\'],
357        }
358    }
359
360    /// Return the spinner character for the given tick.
361    pub fn frame(&self, tick: u64) -> char {
362        if self.chars.is_empty() {
363            return ' ';
364        }
365        self.chars[tick as usize % self.chars.len()]
366    }
367}
368
369impl Default for SpinnerState {
370    fn default() -> Self {
371        Self::dots()
372    }
373}
374
375/// State for a selectable list widget.
376///
377/// Pass a mutable reference to `Context::list` each frame. Up/Down arrow
378/// keys (and `k`/`j`) move the selection when the widget is focused.
379pub struct ListState {
380    /// The list items as display strings.
381    pub items: Vec<String>,
382    /// Index of the currently selected item.
383    pub selected: usize,
384}
385
386impl ListState {
387    /// Create a list with the given items. The first item is selected initially.
388    pub fn new(items: Vec<impl Into<String>>) -> Self {
389        Self {
390            items: items.into_iter().map(Into::into).collect(),
391            selected: 0,
392        }
393    }
394
395    /// Get the currently selected item text, or `None` if the list is empty.
396    pub fn selected_item(&self) -> Option<&str> {
397        self.items.get(self.selected).map(String::as_str)
398    }
399}
400
401/// State for a tab navigation widget.
402///
403/// Pass a mutable reference to `Context::tabs` each frame. Left/Right arrow
404/// keys cycle through tabs when the widget is focused.
405pub struct TabsState {
406    /// The tab labels displayed in the bar.
407    pub labels: Vec<String>,
408    /// Index of the currently active tab.
409    pub selected: usize,
410}
411
412impl TabsState {
413    /// Create tabs with the given labels. The first tab is active initially.
414    pub fn new(labels: Vec<impl Into<String>>) -> Self {
415        Self {
416            labels: labels.into_iter().map(Into::into).collect(),
417            selected: 0,
418        }
419    }
420
421    /// Get the currently selected tab label, or `None` if there are no tabs.
422    pub fn selected_label(&self) -> Option<&str> {
423        self.labels.get(self.selected).map(String::as_str)
424    }
425}
426
427/// State for a data table widget.
428///
429/// Pass a mutable reference to `Context::table` each frame. Up/Down arrow
430/// keys move the row selection when the widget is focused. Column widths are
431/// computed automatically from header and cell content.
432pub struct TableState {
433    /// Column header labels.
434    pub headers: Vec<String>,
435    /// Table rows, each a `Vec` of cell strings.
436    pub rows: Vec<Vec<String>>,
437    /// Index of the currently selected row.
438    pub selected: usize,
439    column_widths: Vec<u32>,
440    dirty: bool,
441    /// Sorted column index (`None` means no sorting).
442    pub sort_column: Option<usize>,
443    /// Sort direction (`true` for ascending).
444    pub sort_ascending: bool,
445    /// Case-insensitive substring filter applied across all cells.
446    pub filter: String,
447    /// Current page (0-based) when pagination is enabled.
448    pub page: usize,
449    /// Rows per page (`0` disables pagination).
450    pub page_size: usize,
451    view_indices: Vec<usize>,
452}
453
454impl TableState {
455    /// Create a table with headers and rows. Column widths are computed immediately.
456    pub fn new(headers: Vec<impl Into<String>>, rows: Vec<Vec<impl Into<String>>>) -> Self {
457        let headers: Vec<String> = headers.into_iter().map(Into::into).collect();
458        let rows: Vec<Vec<String>> = rows
459            .into_iter()
460            .map(|r| r.into_iter().map(Into::into).collect())
461            .collect();
462        let mut state = Self {
463            headers,
464            rows,
465            selected: 0,
466            column_widths: Vec::new(),
467            dirty: true,
468            sort_column: None,
469            sort_ascending: true,
470            filter: String::new(),
471            page: 0,
472            page_size: 0,
473            view_indices: Vec::new(),
474        };
475        state.rebuild_view();
476        state.recompute_widths();
477        state
478    }
479
480    /// Replace all rows, preserving the selection index if possible.
481    ///
482    /// If the current selection is beyond the new row count, it is clamped to
483    /// the last row.
484    pub fn set_rows(&mut self, rows: Vec<Vec<impl Into<String>>>) {
485        self.rows = rows
486            .into_iter()
487            .map(|r| r.into_iter().map(Into::into).collect())
488            .collect();
489        self.rebuild_view();
490    }
491
492    /// Sort by a specific column index. If already sorted by this column, toggles direction.
493    pub fn toggle_sort(&mut self, column: usize) {
494        if self.sort_column == Some(column) {
495            self.sort_ascending = !self.sort_ascending;
496        } else {
497            self.sort_column = Some(column);
498            self.sort_ascending = true;
499        }
500        self.rebuild_view();
501    }
502
503    /// Sort by column without toggling (always sets to ascending first).
504    pub fn sort_by(&mut self, column: usize) {
505        self.sort_column = Some(column);
506        self.sort_ascending = true;
507        self.rebuild_view();
508    }
509
510    /// Set the filter string. Empty string disables filtering.
511    pub fn set_filter(&mut self, filter: impl Into<String>) {
512        self.filter = filter.into();
513        self.page = 0;
514        self.rebuild_view();
515    }
516
517    /// Clear sorting.
518    pub fn clear_sort(&mut self) {
519        self.sort_column = None;
520        self.sort_ascending = true;
521        self.rebuild_view();
522    }
523
524    /// Move to the next page. Does nothing if already on the last page.
525    pub fn next_page(&mut self) {
526        if self.page_size == 0 {
527            return;
528        }
529        let last_page = self.total_pages().saturating_sub(1);
530        self.page = (self.page + 1).min(last_page);
531    }
532
533    /// Move to the previous page. Does nothing if already on page 0.
534    pub fn prev_page(&mut self) {
535        self.page = self.page.saturating_sub(1);
536    }
537
538    /// Total number of pages based on filtered rows and page_size. Returns 1 if page_size is 0.
539    pub fn total_pages(&self) -> usize {
540        if self.page_size == 0 {
541            return 1;
542        }
543
544        let len = self.view_indices.len();
545        if len == 0 {
546            1
547        } else {
548            len.div_ceil(self.page_size)
549        }
550    }
551
552    /// Get the visible row indices after filtering and sorting (used internally by table()).
553    pub fn visible_indices(&self) -> &[usize] {
554        &self.view_indices
555    }
556
557    /// Get the currently selected row data, or `None` if the table is empty.
558    pub fn selected_row(&self) -> Option<&[String]> {
559        if self.view_indices.is_empty() {
560            return None;
561        }
562        let data_idx = self.view_indices.get(self.selected)?;
563        self.rows.get(*data_idx).map(|r| r.as_slice())
564    }
565
566    /// Recompute view_indices based on current sort + filter settings.
567    fn rebuild_view(&mut self) {
568        let mut indices: Vec<usize> = (0..self.rows.len()).collect();
569
570        let tokens: Vec<String> = self
571            .filter
572            .split_whitespace()
573            .map(|t| t.to_lowercase())
574            .collect();
575        if !tokens.is_empty() {
576            indices.retain(|&idx| {
577                let row = match self.rows.get(idx) {
578                    Some(r) => r,
579                    None => return false,
580                };
581                tokens.iter().all(|token| {
582                    row.iter()
583                        .any(|cell| cell.to_lowercase().contains(token.as_str()))
584                })
585            });
586        }
587
588        if let Some(column) = self.sort_column {
589            indices.sort_by(|a, b| {
590                let left = self
591                    .rows
592                    .get(*a)
593                    .and_then(|row| row.get(column))
594                    .map(String::as_str)
595                    .unwrap_or("");
596                let right = self
597                    .rows
598                    .get(*b)
599                    .and_then(|row| row.get(column))
600                    .map(String::as_str)
601                    .unwrap_or("");
602
603                match (left.parse::<f64>(), right.parse::<f64>()) {
604                    (Ok(l), Ok(r)) => l.partial_cmp(&r).unwrap_or(std::cmp::Ordering::Equal),
605                    _ => left.to_lowercase().cmp(&right.to_lowercase()),
606                }
607            });
608
609            if !self.sort_ascending {
610                indices.reverse();
611            }
612        }
613
614        self.view_indices = indices;
615
616        if self.page_size > 0 {
617            self.page = self.page.min(self.total_pages().saturating_sub(1));
618        } else {
619            self.page = 0;
620        }
621
622        self.selected = self.selected.min(self.view_indices.len().saturating_sub(1));
623        self.dirty = true;
624    }
625
626    pub(crate) fn recompute_widths(&mut self) {
627        let col_count = self.headers.len();
628        self.column_widths = vec![0u32; col_count];
629        for (i, header) in self.headers.iter().enumerate() {
630            let mut width = UnicodeWidthStr::width(header.as_str()) as u32;
631            if self.sort_column == Some(i) {
632                width += 2;
633            }
634            self.column_widths[i] = width;
635        }
636        for row in &self.rows {
637            for (i, cell) in row.iter().enumerate() {
638                if i < col_count {
639                    let w = UnicodeWidthStr::width(cell.as_str()) as u32;
640                    self.column_widths[i] = self.column_widths[i].max(w);
641                }
642            }
643        }
644        self.dirty = false;
645    }
646
647    pub(crate) fn column_widths(&self) -> &[u32] {
648        &self.column_widths
649    }
650
651    pub(crate) fn is_dirty(&self) -> bool {
652        self.dirty
653    }
654}
655
656/// State for a scrollable container.
657///
658/// Pass a mutable reference to `Context::scrollable` each frame. The context
659/// updates `offset` and the internal bounds automatically based on mouse wheel
660/// and drag events.
661pub struct ScrollState {
662    /// Current vertical scroll offset in rows.
663    pub offset: usize,
664    content_height: u32,
665    viewport_height: u32,
666}
667
668impl ScrollState {
669    /// Create scroll state starting at offset 0.
670    pub fn new() -> Self {
671        Self {
672            offset: 0,
673            content_height: 0,
674            viewport_height: 0,
675        }
676    }
677
678    /// Check if scrolling upward is possible (offset is greater than 0).
679    pub fn can_scroll_up(&self) -> bool {
680        self.offset > 0
681    }
682
683    /// Check if scrolling downward is possible (content extends below the viewport).
684    pub fn can_scroll_down(&self) -> bool {
685        (self.offset as u32) + self.viewport_height < self.content_height
686    }
687
688    /// Get the total content height in rows.
689    pub fn content_height(&self) -> u32 {
690        self.content_height
691    }
692
693    /// Get the viewport height in rows.
694    pub fn viewport_height(&self) -> u32 {
695        self.viewport_height
696    }
697
698    /// Get the scroll progress as a ratio in [0.0, 1.0].
699    pub fn progress(&self) -> f32 {
700        let max = self.content_height.saturating_sub(self.viewport_height);
701        if max == 0 {
702            0.0
703        } else {
704            self.offset as f32 / max as f32
705        }
706    }
707
708    /// Scroll up by the given number of rows, clamped to 0.
709    pub fn scroll_up(&mut self, amount: usize) {
710        self.offset = self.offset.saturating_sub(amount);
711    }
712
713    /// Scroll down by the given number of rows, clamped to the maximum offset.
714    pub fn scroll_down(&mut self, amount: usize) {
715        let max_offset = self.content_height.saturating_sub(self.viewport_height) as usize;
716        self.offset = (self.offset + amount).min(max_offset);
717    }
718
719    pub(crate) fn set_bounds(&mut self, content_height: u32, viewport_height: u32) {
720        self.content_height = content_height;
721        self.viewport_height = viewport_height;
722    }
723}
724
725impl Default for ScrollState {
726    fn default() -> Self {
727        Self::new()
728    }
729}
730
731/// Visual variant for buttons.
732///
733/// Controls the color scheme used when rendering a button. Pass to
734/// [`crate::Context::button_with`] to create styled button variants.
735///
736/// - `Default` — theme text color, primary when focused (same as `button()`)
737/// - `Primary` — primary color background with contrasting text
738/// - `Danger` — error/red color for destructive actions
739/// - `Outline` — bordered appearance without fill
740#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
741pub enum ButtonVariant {
742    /// Standard button style.
743    #[default]
744    Default,
745    /// Filled button with primary background color.
746    Primary,
747    /// Filled button with error/danger background color.
748    Danger,
749    /// Bordered button without background fill.
750    Outline,
751}
752
753// ── Select / Dropdown ─────────────────────────────────────────────────
754
755/// State for a dropdown select widget.
756///
757/// Renders as a single-line button showing the selected option. When activated,
758/// expands into a vertical list overlay for picking an option.
759pub struct SelectState {
760    pub items: Vec<String>,
761    pub selected: usize,
762    pub open: bool,
763    pub placeholder: String,
764    cursor: usize,
765}
766
767impl SelectState {
768    pub fn new(items: Vec<impl Into<String>>) -> Self {
769        Self {
770            items: items.into_iter().map(Into::into).collect(),
771            selected: 0,
772            open: false,
773            placeholder: String::new(),
774            cursor: 0,
775        }
776    }
777
778    pub fn placeholder(mut self, p: impl Into<String>) -> Self {
779        self.placeholder = p.into();
780        self
781    }
782
783    pub fn selected_item(&self) -> Option<&str> {
784        self.items.get(self.selected).map(String::as_str)
785    }
786
787    pub(crate) fn cursor(&self) -> usize {
788        self.cursor
789    }
790
791    pub(crate) fn set_cursor(&mut self, c: usize) {
792        self.cursor = c;
793    }
794}
795
796// ── Radio ─────────────────────────────────────────────────────────────
797
798/// State for a radio button group.
799///
800/// Renders a vertical list of mutually-exclusive options with `●`/`○` markers.
801pub struct RadioState {
802    pub items: Vec<String>,
803    pub selected: usize,
804}
805
806impl RadioState {
807    pub fn new(items: Vec<impl Into<String>>) -> Self {
808        Self {
809            items: items.into_iter().map(Into::into).collect(),
810            selected: 0,
811        }
812    }
813
814    pub fn selected_item(&self) -> Option<&str> {
815        self.items.get(self.selected).map(String::as_str)
816    }
817}
818
819// ── Multi-Select ──────────────────────────────────────────────────────
820
821/// State for a multi-select list.
822///
823/// Like [`ListState`] but allows toggling multiple items with Space.
824pub struct MultiSelectState {
825    pub items: Vec<String>,
826    pub cursor: usize,
827    pub selected: HashSet<usize>,
828}
829
830impl MultiSelectState {
831    pub fn new(items: Vec<impl Into<String>>) -> Self {
832        Self {
833            items: items.into_iter().map(Into::into).collect(),
834            cursor: 0,
835            selected: HashSet::new(),
836        }
837    }
838
839    pub fn selected_items(&self) -> Vec<&str> {
840        let mut indices: Vec<usize> = self.selected.iter().copied().collect();
841        indices.sort();
842        indices
843            .iter()
844            .filter_map(|&i| self.items.get(i).map(String::as_str))
845            .collect()
846    }
847
848    pub fn toggle(&mut self, index: usize) {
849        if self.selected.contains(&index) {
850            self.selected.remove(&index);
851        } else {
852            self.selected.insert(index);
853        }
854    }
855}
856
857// ── Tree ──────────────────────────────────────────────────────────────
858
859/// A node in a tree view.
860pub struct TreeNode {
861    pub label: String,
862    pub children: Vec<TreeNode>,
863    pub expanded: bool,
864}
865
866impl TreeNode {
867    pub fn new(label: impl Into<String>) -> Self {
868        Self {
869            label: label.into(),
870            children: Vec::new(),
871            expanded: false,
872        }
873    }
874
875    pub fn expanded(mut self) -> Self {
876        self.expanded = true;
877        self
878    }
879
880    pub fn children(mut self, children: Vec<TreeNode>) -> Self {
881        self.children = children;
882        self
883    }
884
885    pub fn is_leaf(&self) -> bool {
886        self.children.is_empty()
887    }
888
889    fn flatten(&self, depth: usize, out: &mut Vec<FlatTreeEntry>) {
890        out.push(FlatTreeEntry {
891            depth,
892            label: self.label.clone(),
893            is_leaf: self.is_leaf(),
894            expanded: self.expanded,
895        });
896        if self.expanded {
897            for child in &self.children {
898                child.flatten(depth + 1, out);
899            }
900        }
901    }
902}
903
904pub(crate) struct FlatTreeEntry {
905    pub depth: usize,
906    pub label: String,
907    pub is_leaf: bool,
908    pub expanded: bool,
909}
910
911/// State for a hierarchical tree view widget.
912pub struct TreeState {
913    pub nodes: Vec<TreeNode>,
914    pub selected: usize,
915}
916
917impl TreeState {
918    pub fn new(nodes: Vec<TreeNode>) -> Self {
919        Self { nodes, selected: 0 }
920    }
921
922    pub(crate) fn flatten(&self) -> Vec<FlatTreeEntry> {
923        let mut entries = Vec::new();
924        for node in &self.nodes {
925            node.flatten(0, &mut entries);
926        }
927        entries
928    }
929
930    pub(crate) fn toggle_at(&mut self, flat_index: usize) {
931        let mut counter = 0usize;
932        Self::toggle_recursive(&mut self.nodes, flat_index, &mut counter);
933    }
934
935    fn toggle_recursive(nodes: &mut [TreeNode], target: usize, counter: &mut usize) -> bool {
936        for node in nodes.iter_mut() {
937            if *counter == target {
938                if !node.is_leaf() {
939                    node.expanded = !node.expanded;
940                }
941                return true;
942            }
943            *counter += 1;
944            if node.expanded && Self::toggle_recursive(&mut node.children, target, counter) {
945                return true;
946            }
947        }
948        false
949    }
950}
951
952// ── Command Palette ───────────────────────────────────────────────────
953
954/// A single command entry in the palette.
955pub struct PaletteCommand {
956    pub label: String,
957    pub description: String,
958    pub shortcut: Option<String>,
959}
960
961impl PaletteCommand {
962    pub fn new(label: impl Into<String>, description: impl Into<String>) -> Self {
963        Self {
964            label: label.into(),
965            description: description.into(),
966            shortcut: None,
967        }
968    }
969
970    pub fn shortcut(mut self, s: impl Into<String>) -> Self {
971        self.shortcut = Some(s.into());
972        self
973    }
974}
975
976/// State for a command palette overlay.
977///
978/// Renders as a modal with a search input and filtered command list.
979pub struct CommandPaletteState {
980    pub commands: Vec<PaletteCommand>,
981    pub input: String,
982    pub cursor: usize,
983    pub open: bool,
984    selected: usize,
985}
986
987impl CommandPaletteState {
988    pub fn new(commands: Vec<PaletteCommand>) -> Self {
989        Self {
990            commands,
991            input: String::new(),
992            cursor: 0,
993            open: false,
994            selected: 0,
995        }
996    }
997
998    pub fn toggle(&mut self) {
999        self.open = !self.open;
1000        if self.open {
1001            self.input.clear();
1002            self.cursor = 0;
1003            self.selected = 0;
1004        }
1005    }
1006
1007    pub(crate) fn filtered_indices(&self) -> Vec<usize> {
1008        if self.input.is_empty() {
1009            return (0..self.commands.len()).collect();
1010        }
1011        let query = self.input.to_lowercase();
1012        self.commands
1013            .iter()
1014            .enumerate()
1015            .filter(|(_, cmd)| {
1016                cmd.label.to_lowercase().contains(&query)
1017                    || cmd.description.to_lowercase().contains(&query)
1018            })
1019            .map(|(i, _)| i)
1020            .collect()
1021    }
1022
1023    pub(crate) fn selected(&self) -> usize {
1024        self.selected
1025    }
1026
1027    pub(crate) fn set_selected(&mut self, s: usize) {
1028        self.selected = s;
1029    }
1030}
1031
1032/// State for a streaming text display.
1033///
1034/// Accumulates text chunks as they arrive from an LLM stream.
1035/// Pass to [`Context::streaming_text`](crate::Context::streaming_text) each frame.
1036pub struct StreamingTextState {
1037    /// The accumulated text content.
1038    pub content: String,
1039    /// Whether the stream is still receiving data.
1040    pub streaming: bool,
1041    /// Cursor blink state (for the typing indicator).
1042    pub(crate) cursor_visible: bool,
1043    pub(crate) cursor_tick: u64,
1044}
1045
1046impl StreamingTextState {
1047    /// Create a new empty streaming text state.
1048    pub fn new() -> Self {
1049        Self {
1050            content: String::new(),
1051            streaming: false,
1052            cursor_visible: true,
1053            cursor_tick: 0,
1054        }
1055    }
1056
1057    /// Append a chunk of text (e.g., from an LLM stream delta).
1058    pub fn push(&mut self, chunk: &str) {
1059        self.content.push_str(chunk);
1060    }
1061
1062    /// Mark the stream as complete (hides the typing cursor).
1063    pub fn finish(&mut self) {
1064        self.streaming = false;
1065    }
1066
1067    /// Start a new streaming session, clearing previous content.
1068    pub fn start(&mut self) {
1069        self.content.clear();
1070        self.streaming = true;
1071        self.cursor_visible = true;
1072        self.cursor_tick = 0;
1073    }
1074
1075    /// Clear all content and reset state.
1076    pub fn clear(&mut self) {
1077        self.content.clear();
1078        self.streaming = false;
1079        self.cursor_visible = true;
1080        self.cursor_tick = 0;
1081    }
1082}
1083
1084impl Default for StreamingTextState {
1085    fn default() -> Self {
1086        Self::new()
1087    }
1088}
1089
1090/// Approval state for a tool call.
1091#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1092pub enum ApprovalAction {
1093    /// No action taken yet.
1094    Pending,
1095    /// User approved the tool call.
1096    Approved,
1097    /// User rejected the tool call.
1098    Rejected,
1099}
1100
1101/// State for a tool approval widget.
1102///
1103/// Displays a tool call with approve/reject buttons for human-in-the-loop
1104/// AI workflows. Pass to [`Context::tool_approval`](crate::Context::tool_approval)
1105/// each frame.
1106pub struct ToolApprovalState {
1107    /// The name of the tool being invoked.
1108    pub tool_name: String,
1109    /// A human-readable description of what the tool will do.
1110    pub description: String,
1111    /// The current approval status.
1112    pub action: ApprovalAction,
1113}
1114
1115impl ToolApprovalState {
1116    /// Create a new tool approval prompt.
1117    pub fn new(tool_name: impl Into<String>, description: impl Into<String>) -> Self {
1118        Self {
1119            tool_name: tool_name.into(),
1120            description: description.into(),
1121            action: ApprovalAction::Pending,
1122        }
1123    }
1124
1125    /// Reset to pending state.
1126    pub fn reset(&mut self) {
1127        self.action = ApprovalAction::Pending;
1128    }
1129}
1130
1131/// Item in a context bar showing active context sources.
1132#[derive(Debug, Clone)]
1133pub struct ContextItem {
1134    /// Display label for this context source.
1135    pub label: String,
1136    /// Token count or size indicator.
1137    pub tokens: usize,
1138}
1139
1140impl ContextItem {
1141    /// Create a new context item with a label and token count.
1142    pub fn new(label: impl Into<String>, tokens: usize) -> Self {
1143        Self {
1144            label: label.into(),
1145            tokens,
1146        }
1147    }
1148}