Lines
100 %
Functions
50 %
Branches
//! The splits editor as a grid of cells: one line per split, the open tag list under its split.
use super::{
COL_FROM, COL_FROM_COMM, COL_TO, COL_TO_AMOUNT, COL_TO_COMM, COL_VALUE, SplitRow, SplitsWidget,
};
use crate::widgets::grid::{Grid, Item};
use crate::widgets::rows::empty_note;
/// The tag list sits in this far under its split.
const TAGS_INDENT: u16 = 4;
const ACCOUNT_MIN: u16 = 12;
const ACCOUNT_MAX: u16 = 40;
/// A margin cell, six cells of text and the `▼` mark.
const COMMODITY_WIDTH: u16 = 9;
const AMOUNT_WIDTH: u16 = 10;
const TAG_COUNT_WIDTH: u16 = 3;
impl SplitsWidget {
/// A line of cells per split, `active` when the field has the focus. The conversion
/// (`→ to_amount`) shows when the commodities differ, the amount is filled in, or the cell
/// is focused. While the tags are open the focus is in their list, under their split, with
/// `suggestion` after it and `add_key` named in an empty one.
#[must_use]
pub fn grid(&self, active: bool, suggestion: Option<&str>, add_key: Option<&str>) -> Grid<'_> {
let mut grid = Grid::default();
for (at, row) in self.rows.iter().enumerate() {
let here = at == self.row_focus;
if here {
grid.focus_line = grid.lines.len();
}
grid.push_cells(0, self.row_items(at, row, active));
if self.tags_open && here {
grid.append(tag_grid(row, active, add_key), TAGS_INDENT, true);
if let Some(line) = suggestion {
grid.push_note(TAGS_INDENT, line);
grid
fn row_items<'a>(&self, at: usize, row: &'a SplitRow, active: bool) -> Vec<Item<'a>> {
let focus = |col| active && !self.tags_open && here && col == self.col_focus;
let account = |col, select| {
let item = Item::select(select, focus(col)).at(at, col);
item.flex(ACCOUNT_MIN, ACCOUNT_MAX).tail()
let commodity = |col, select| {
item.fixed(COMMODITY_WIDTH)
let amount = |col, amount: &'a crate::widgets::AmountWidget| {
let editor = amount.editor();
let item = Item::text(amount.value(), editor.cursor(), focus(col));
item.scrolled(editor.scroll())
.at(at, col)
.fixed(AMOUNT_WIDTH)
let to_commodity = row.to_commodity.value();
let conversion = (!to_commodity.is_empty() && row.from_commodity.value() != to_commodity)
|| !row.to_amount.value().is_empty()
|| (here && !self.tags_open && self.col_focus == COL_TO_AMOUNT);
let (arrow, to_amount) = if conversion {
("→", amount(COL_TO_AMOUNT, &row.to_amount))
} else {
("", Item::label(""))
vec![
Item::label(format!("{}.", at + 1)).frozen(),
Item::label(format!("#{}", row.tags.rows().len()))
.fixed(TAG_COUNT_WIDTH)
.frozen(),
account(COL_FROM, &row.from).frozen(),
Item::label("→"),
account(COL_TO, &row.to),
commodity(COL_FROM_COMM, &row.from_commodity),
commodity(COL_TO_COMM, &row.to_commodity),
amount(COL_VALUE, &row.value),
Item::label(arrow),
to_amount,
]
/// The tag list of `row`.
fn tag_grid<'a>(row: &'a SplitRow, active: bool, add_key: Option<&str>) -> Grid<'a> {
let note = empty_note("tags", add_key);
row.tags.grid(active, None, Some(¬e)).of_tags()
#[cfg(test)]
mod tests;