1
//! Drawing a grid field: its title and notes, labels, and the cells in their own areas.
2

            
3
use crate::draw::canvas::Offscreen;
4
use ratatui::layout::Rect;
5
use tvui::{InputLine, Label, Role, Theme};
6

            
7
use super::body::draw_input;
8
use crate::hits::{Hits, Target};
9
use crate::widgets::grid::{Grid, GridLine, Item, ItemKind};
10

            
11
const COMBO_MARK: char = '\u{25bc}';
12

            
13
/// The cells of `rect` the select `item` leaves to its `▼` mark.
14
2812
fn mark_rect(item: &Item<'_>, rect: Rect) -> Rect {
15
2812
    let text = item.area(rect);
16
2812
    Rect::new(text.right(), rect.y, rect.right() - text.right(), 1)
17
2812
}
18

            
19
/// The `▼` mark ending a select `item` in `rect`.
20
1389
fn draw_combo_mark(frame: &mut Offscreen, item: &Item<'_>, rect: Rect, theme: &Theme) {
21
1389
    let role = if item.focused {
22
96
        Role::InputFocused
23
    } else {
24
1293
        Role::Input
25
    };
26
1389
    let mark = mark_rect(item, rect);
27
1389
    if let Some(cell) = frame
28
1389
        .buffer_mut()
29
1389
        .cell_mut((mark.x, mark.y))
30
1389
        .filter(|_| !mark.is_empty())
31
    {
32
1389
        cell.set_char(COMBO_MARK).set_style(theme.style(role));
33
    }
34
1389
}
35

            
36
536
pub(super) fn draw_grid(
37
536
    frame: &mut Offscreen,
38
536
    grid: &Grid<'_>,
39
536
    rects: &[Vec<Rect>],
40
536
    theme: &Theme,
41
536
) {
42
536
    for (line, rects) in grid.lines.iter().zip(rects) {
43
1442
        match line {
44
            GridLine::Text { text, focused, .. } => {
45
802
                for rect in rects.iter().filter(|rect| rect.area() > 0) {
46
800
                    frame.render_widget(Label::new(theme, text, *focused), *rect);
47
                }
48
            }
49
            GridLine::Cells { items, .. } => {
50
4414
                for (item, rect) in items.iter().zip(rects).filter(|(_, rect)| rect.area() > 0) {
51
3689
                    draw_item(frame, item, *rect, theme);
52
                }
53
            }
54
        }
55
    }
56
536
}
57

            
58
3689
fn draw_item(frame: &mut Offscreen, item: &Item<'_>, rect: Rect, theme: &Theme) {
59
3689
    if item.kind == ItemKind::Label {
60
1902
        frame.render_widget(Label::new(theme, &item.text, false), rect);
61
1902
        return;
62
1787
    }
63
1787
    let cursor = if item.focused { item.cursor } else { 0 };
64
1787
    let text = item.shown(rect);
65
1787
    let field = InputLine::new(theme, &text, cursor, item.focused);
66
1787
    draw_input(frame, field, item.area(rect), item.scroll);
67
1787
    if item.kind == ItemKind::Select {
68
1389
        draw_combo_mark(frame, item, rect, theme);
69
398
    }
70
3689
}
71

            
72
/// Records each cell of the grid in field `field` in its area, labels left out.
73
537
pub(super) fn record_cells(hits: &mut Hits, field: usize, grid: &Grid<'_>, rects: &[Vec<Rect>]) {
74
537
    for (line, rects) in grid.lines.iter().zip(rects) {
75
1447
        let GridLine::Cells { items, .. } = line else {
76
805
            continue;
77
        };
78
642
        for (item, rect) in items.iter().zip(rects) {
79
4420
            let Some(cell) = item.cell else {
80
2589
                continue;
81
            };
82
1831
            hits.push(*rect, Target::Cell { field, cell });
83
1831
            let cell = Some(cell);
84
1831
            match item.kind {
85
                ItemKind::Select => {
86
1423
                    hits.push(mark_rect(item, *rect), Target::Mark { field, cell });
87
                }
88
408
                ItemKind::Text => hits.push(item.area(*rect), Target::Caret { field, cell }),
89
                ItemKind::Label => {}
90
            }
91
        }
92
    }
93
537
}