1
pub mod amount;
2
pub mod date;
3
pub mod editor;
4
pub mod grid;
5
mod hints;
6
pub mod rows;
7
pub mod select;
8
pub mod splits;
9
pub mod table_panel;
10

            
11
pub use amount::AmountWidget;
12
pub use date::DateWidget;
13
pub use editor::{Editor, TEXT_AREA_ROWS};
14
pub use hints::{Hint, Hints, completion, matching};
15
pub use rows::RowsWidget;
16
pub use select::{SelectOption, SelectWidget, with_none};
17
pub use splits::{FocusedSubWidget, SplitRow, SplitsWidget};
18
pub use table_panel::TablePanel;
19

            
20
#[cfg(test)]
21
mod tests;
22

            
23
/// Discriminant for widget-first key routing without carrying widget data.
24
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25
pub 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)]
37
pub enum Widget {
38
    Text(Editor),
39
    Select(SelectWidget),
40
    Amount(AmountWidget),
41
    Date(DateWidget),
42
    Splits(SplitsWidget),
43
    Rows(RowsWidget),
44
}
45

            
46
impl 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
6278
    pub fn value(&self) -> &str {
52
6278
        match self {
53
2805
            Self::Text(ed) => ed.buffer(),
54
2435
            Self::Select(sw) => sw.value(),
55
28
            Self::Amount(aw) => aw.value(),
56
1010
            Self::Date(dw) => dw.value(),
57
            Self::Splits(_) | Self::Rows(_) => "",
58
        }
59
6278
    }
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
101
    pub fn as_text_mut(&mut self) -> Option<&mut Editor> {
65
101
        match self {
66
75
            Self::Text(ed) => Some(ed),
67
2
            Self::Amount(aw) => Some(aw.as_editor_mut()),
68
9
            Self::Date(dw) => Some(dw.as_editor_mut()),
69
15
            Self::Select(_) | Self::Splits(_) | Self::Rows(_) => None,
70
        }
71
101
    }
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
27
    pub fn focused_editor_mut(&mut self) -> Option<&mut Editor> {
76
27
        match self {
77
11
            Self::Splits(sw) => sw.focused_editor_mut(),
78
            Self::Rows(rows) => rows.focused_editor_mut(),
79
16
            other => other.as_text_mut(),
80
        }
81
27
    }
82

            
83
    /// Replaces a text-like widget's content or focuses a select's option by id;
84
    /// splits take no text.
85
1043
    pub fn set_text(&mut self, text: &str) {
86
1043
        match self {
87
407
            Self::Text(ed) => *ed = Editor::with_buffer(text).multiline(ed.is_multiline()),
88
7
            Self::Amount(aw) => *aw = AmountWidget::with_value(text),
89
343
            Self::Date(dw) => *dw = DateWidget::with_value(text),
90
286
            Self::Select(sw) => sw.select_id(text),
91
            Self::Splits(_) | Self::Rows(_) => {}
92
        }
93
1043
    }
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
4302
    pub fn kind(&self) -> WidgetKind {
99
4302
        match self {
100
1565
            Self::Text(ed) if ed.is_multiline() => WidgetKind::TextArea,
101
1038
            Self::Text(_) => WidgetKind::Text,
102
765
            Self::Select(_) => WidgetKind::Select,
103
14
            Self::Amount(_) => WidgetKind::Amount,
104
540
            Self::Date(_) => WidgetKind::Date,
105
1325
            Self::Splits(sw) if sw.tags_open() => WidgetKind::Rows,
106
1210
            Self::Splits(_) => WidgetKind::Splits,
107
93
            Self::Rows(_) => WidgetKind::Rows,
108
        }
109
4302
    }
110
}