Skip to main content

tui/widgets/
mod.rs

1pub mod amount;
2pub mod date;
3pub mod editor;
4pub mod grid;
5mod hints;
6pub mod rows;
7pub mod select;
8pub mod splits;
9pub mod table_panel;
10
11pub use amount::AmountWidget;
12pub use date::DateWidget;
13pub use editor::{Editor, TEXT_AREA_ROWS};
14pub use hints::{Hint, Hints, completion, matching};
15pub use rows::RowsWidget;
16pub use select::{SelectOption, SelectWidget, with_none};
17pub use splits::{FocusedSubWidget, SplitRow, SplitsWidget};
18pub use table_panel::TablePanel;
19
20#[cfg(test)]
21mod tests;
22
23/// Discriminant for widget-first key routing without carrying widget data.
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum WidgetKind {
26    Text,
27    TextArea,
28    Select,
29    Amount,
30    Date,
31    Splits,
32    Rows,
33}
34
35/// The primary widget union — enum dispatch, no `Box<dyn>`, derives `Debug`.
36#[derive(Debug)]
37pub enum Widget {
38    Text(Editor),
39    Select(SelectWidget),
40    Amount(AmountWidget),
41    Date(DateWidget),
42    Splits(SplitsWidget),
43    Rows(RowsWidget),
44}
45
46impl Widget {
47    /// The submittable string value of this widget.
48    ///
49    /// Returns `""` for `Splits` and `Rows` (rows validated separately via [`SplitsWidget::rows`]).
50    #[must_use]
51    pub fn value(&self) -> &str {
52        match self {
53            Self::Text(ed) => ed.buffer(),
54            Self::Select(sw) => sw.value(),
55            Self::Amount(aw) => aw.value(),
56            Self::Date(dw) => dw.value(),
57            Self::Splits(_) | Self::Rows(_) => "",
58        }
59    }
60
61    /// Mutable reference to the inner [`Editor`] for `Text`, `Amount`, and
62    /// `Date` variants. Returns `None` for `Select`, `Splits` and `Rows`.
63    #[must_use]
64    pub fn as_text_mut(&mut self) -> Option<&mut Editor> {
65        match self {
66            Self::Text(ed) => Some(ed),
67            Self::Amount(aw) => Some(aw.as_editor_mut()),
68            Self::Date(dw) => Some(dw.as_editor_mut()),
69            Self::Select(_) | Self::Splits(_) | Self::Rows(_) => None,
70        }
71    }
72
73    /// The editor of the focused cell of a grid, or of the field itself for the text-like
74    /// widgets; `None` on a select cell.
75    pub fn focused_editor_mut(&mut self) -> Option<&mut Editor> {
76        match self {
77            Self::Splits(sw) => sw.focused_editor_mut(),
78            Self::Rows(rows) => rows.focused_editor_mut(),
79            other => other.as_text_mut(),
80        }
81    }
82
83    /// Replaces a text-like widget's content or focuses a select's option by id;
84    /// splits take no text.
85    pub fn set_text(&mut self, text: &str) {
86        match self {
87            Self::Text(ed) => *ed = Editor::with_buffer(text).multiline(ed.is_multiline()),
88            Self::Amount(aw) => *aw = AmountWidget::with_value(text),
89            Self::Date(dw) => *dw = DateWidget::with_value(text),
90            Self::Select(sw) => sw.select_id(text),
91            Self::Splits(_) | Self::Rows(_) => {}
92        }
93    }
94
95    /// Variant discriminant used for widget-first key routing decisions; an
96    /// open tag list of the splits takes keys like a rows editor.
97    #[must_use]
98    pub fn kind(&self) -> WidgetKind {
99        match self {
100            Self::Text(ed) if ed.is_multiline() => WidgetKind::TextArea,
101            Self::Text(_) => WidgetKind::Text,
102            Self::Select(_) => WidgetKind::Select,
103            Self::Amount(_) => WidgetKind::Amount,
104            Self::Date(_) => WidgetKind::Date,
105            Self::Splits(sw) if sw.tags_open() => WidgetKind::Rows,
106            Self::Splits(_) => WidgetKind::Splits,
107            Self::Rows(_) => WidgetKind::Rows,
108        }
109    }
110}