Lines
96.43 %
Functions
50 %
Branches
100 %
//! Drawing a grid field: its title and notes, labels, and the cells in their own areas.
use crate::draw::canvas::Offscreen;
use ratatui::layout::Rect;
use tvui::{InputLine, Label, Role, Theme};
use super::body::draw_input;
use crate::hits::{Hits, Target};
use crate::widgets::grid::{Grid, GridLine, Item, ItemKind};
const COMBO_MARK: char = '\u{25bc}';
/// The cells of `rect` the select `item` leaves to its `▼` mark.
fn mark_rect(item: &Item<'_>, rect: Rect) -> Rect {
let text = item.area(rect);
Rect::new(text.right(), rect.y, rect.right() - text.right(), 1)
}
/// The `▼` mark ending a select `item` in `rect`.
fn draw_combo_mark(frame: &mut Offscreen, item: &Item<'_>, rect: Rect, theme: &Theme) {
let role = if item.focused {
Role::InputFocused
} else {
Role::Input
};
let mark = mark_rect(item, rect);
if let Some(cell) = frame
.buffer_mut()
.cell_mut((mark.x, mark.y))
.filter(|_| !mark.is_empty())
{
cell.set_char(COMBO_MARK).set_style(theme.style(role));
pub(super) fn draw_grid(
frame: &mut Offscreen,
grid: &Grid<'_>,
rects: &[Vec<Rect>],
theme: &Theme,
) {
for (line, rects) in grid.lines.iter().zip(rects) {
match line {
GridLine::Text { text, focused, .. } => {
for rect in rects.iter().filter(|rect| rect.area() > 0) {
frame.render_widget(Label::new(theme, text, *focused), *rect);
GridLine::Cells { items, .. } => {
for (item, rect) in items.iter().zip(rects).filter(|(_, rect)| rect.area() > 0) {
draw_item(frame, item, *rect, theme);
fn draw_item(frame: &mut Offscreen, item: &Item<'_>, rect: Rect, theme: &Theme) {
if item.kind == ItemKind::Label {
frame.render_widget(Label::new(theme, &item.text, false), rect);
return;
let cursor = if item.focused { item.cursor } else { 0 };
let text = item.shown(rect);
let field = InputLine::new(theme, &text, cursor, item.focused);
draw_input(frame, field, item.area(rect), item.scroll);
if item.kind == ItemKind::Select {
draw_combo_mark(frame, item, rect, theme);
/// Records each cell of the grid in field `field` in its area, labels left out.
pub(super) fn record_cells(hits: &mut Hits, field: usize, grid: &Grid<'_>, rects: &[Vec<Rect>]) {
let GridLine::Cells { items, .. } = line else {
continue;
for (item, rect) in items.iter().zip(rects) {
let Some(cell) = item.cell else {
hits.push(*rect, Target::Cell { field, cell });
let cell = Some(cell);
match item.kind {
ItemKind::Select => {
hits.push(mark_rect(item, *rect), Target::Mark { field, cell });
ItemKind::Text => hits.push(item.area(*rect), Target::Caret { field, cell }),
ItemKind::Label => {}