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#[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#[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 #[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 #[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 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 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 #[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}