1
//! The splits editor as a grid of cells: one line per split, the open tag list under its split.
2

            
3
use super::{
4
    COL_FROM, COL_FROM_COMM, COL_TO, COL_TO_AMOUNT, COL_TO_COMM, COL_VALUE, SplitRow, SplitsWidget,
5
};
6
use crate::widgets::grid::{Grid, Item};
7
use crate::widgets::rows::empty_note;
8

            
9
/// The tag list sits in this far under its split.
10
const TAGS_INDENT: u16 = 4;
11
const ACCOUNT_MIN: u16 = 12;
12
const ACCOUNT_MAX: u16 = 40;
13
/// A margin cell, six cells of text and the `▼` mark.
14
const COMMODITY_WIDTH: u16 = 9;
15
const AMOUNT_WIDTH: u16 = 10;
16
const TAG_COUNT_WIDTH: u16 = 3;
17

            
18
impl SplitsWidget {
19
    /// A line of cells per split, `active` when the field has the focus. The conversion
20
    /// (`→ to_amount`) shows when the commodities differ, the amount is filled in, or the cell
21
    /// is focused. While the tags are open the focus is in their list, under their split, with
22
    /// `suggestion` after it and `add_key` named in an empty one.
23
    #[must_use]
24
303
    pub fn grid(&self, active: bool, suggestion: Option<&str>, add_key: Option<&str>) -> Grid<'_> {
25
303
        let mut grid = Grid::default();
26
303
        for (at, row) in self.rows.iter().enumerate() {
27
399
            let here = at == self.row_focus;
28
399
            if here {
29
303
                grid.focus_line = grid.lines.len();
30
96
            }
31
399
            grid.push_cells(0, self.row_items(at, row, active));
32
399
            if self.tags_open && here {
33
13
                grid.append(tag_grid(row, active, add_key), TAGS_INDENT, true);
34
13
                if let Some(line) = suggestion {
35
1
                    grid.push_note(TAGS_INDENT, line);
36
12
                }
37
386
            }
38
        }
39
303
        grid
40
303
    }
41

            
42
399
    fn row_items<'a>(&self, at: usize, row: &'a SplitRow, active: bool) -> Vec<Item<'a>> {
43
399
        let here = at == self.row_focus;
44
2025
        let focus = |col| active && !self.tags_open && here && col == self.col_focus;
45
798
        let account = |col, select| {
46
798
            let item = Item::select(select, focus(col)).at(at, col);
47
798
            item.flex(ACCOUNT_MIN, ACCOUNT_MAX).tail()
48
798
        };
49
798
        let commodity = |col, select| {
50
798
            let item = Item::select(select, focus(col)).at(at, col);
51
798
            item.fixed(COMMODITY_WIDTH)
52
798
        };
53
429
        let amount = |col, amount: &'a crate::widgets::AmountWidget| {
54
429
            let editor = amount.editor();
55
429
            let item = Item::text(amount.value(), editor.cursor(), focus(col));
56
429
            item.scrolled(editor.scroll())
57
429
                .at(at, col)
58
429
                .fixed(AMOUNT_WIDTH)
59
429
        };
60
399
        let to_commodity = row.to_commodity.value();
61
399
        let conversion = (!to_commodity.is_empty() && row.from_commodity.value() != to_commodity)
62
391
            || !row.to_amount.value().is_empty()
63
382
            || (here && !self.tags_open && self.col_focus == COL_TO_AMOUNT);
64
399
        let (arrow, to_amount) = if conversion {
65
30
            ("→", amount(COL_TO_AMOUNT, &row.to_amount))
66
        } else {
67
369
            ("", Item::label(""))
68
        };
69
399
        vec![
70
399
            Item::label(format!("{}.", at + 1)).frozen(),
71
399
            Item::label(format!("#{}", row.tags.rows().len()))
72
399
                .fixed(TAG_COUNT_WIDTH)
73
399
                .frozen(),
74
399
            account(COL_FROM, &row.from).frozen(),
75
399
            Item::label("→"),
76
399
            account(COL_TO, &row.to),
77
399
            commodity(COL_FROM_COMM, &row.from_commodity),
78
399
            commodity(COL_TO_COMM, &row.to_commodity),
79
399
            amount(COL_VALUE, &row.value),
80
399
            Item::label(arrow),
81
399
            to_amount,
82
        ]
83
399
    }
84
}
85

            
86
/// The tag list of `row`.
87
13
fn tag_grid<'a>(row: &'a SplitRow, active: bool, add_key: Option<&str>) -> Grid<'a> {
88
13
    let note = empty_note("tags", add_key);
89
13
    row.tags.grid(active, None, Some(&note)).of_tags()
90
13
}
91

            
92
#[cfg(test)]
93
mod tests;