Lines
84.85 %
Functions
50 %
Branches
100 %
//! What a click does inside a form: focus a grid cell.
use ratatui::layout::{Position, Rect};
use tvui::{InputLine, TextView};
use super::Form;
use crate::widgets::Widget;
use crate::widgets::grid::CellId;
impl Form {
/// Focuses `cell` of the grid in field `field`; a field that is not a grid changes nothing.
pub fn focus_cell(&mut self, field: usize, cell: CellId) {
let Some(grid) = self.fields.get_mut(field) else {
return;
};
match &mut grid.widget {
Widget::Splits(splits) => splits.focus_cell_id(cell),
Widget::Rows(rows) => rows.focus_at(cell.row, cell.col),
_ => return,
}
self.focus_field(field);
/// Puts the caret of the focused text where a click at `point` landed in `area`, the area its
/// text was drawn in through the editor's scroll.
pub fn place_caret(&mut self, area: Rect, point: Position) {
let Some(editor) = self
.fields
.get_mut(self.focus)
.and_then(|field| field.widget.focused_editor_mut())
else {
if editor.is_multiline() {
let lines = editor.text_lines();
let at = TextView::position_at(area, &lines, editor.viewport(), point);
editor.set_cursor_position(at);
let at = InputLine::position_at(area, editor.buffer(), editor.viewport(), point);
editor.set_cursor(at);
/// Whether the focus is on `cell` of the grid in field `field`.
#[must_use]
pub fn cell_focused(&self, field: usize, cell: CellId) -> bool {
if self.focus != field {
return false;
match self.fields.get(field).map(|field| &field.widget) {
Some(Widget::Splits(splits)) => splits.focused_cell() == cell,
Some(Widget::Rows(rows)) => rows.focused_cell() == cell,
_ => false,
#[cfg(test)]
mod tests;