Lines
97.33 %
Functions
100 %
Branches
//! The `▼` mark opens a list at once; a click in a text puts the caret under it.
use super::click_tests::{click_on, now, presets, press};
use super::*;
use crate::form::Field;
use crate::test_support::{area_of, redraw};
use crate::widgets::grid::CellId;
use crate::widgets::splits::{COL_FROM, COL_VALUE};
use crate::widgets::{Editor, SelectOption, SelectWidget, SplitsWidget, Widget};
use ratatui::Terminal;
use ratatui::backend::TestBackend;
const SPLITS: usize = 3;
pub(super) fn form_mut(app: &mut App) -> &mut crate::form::Form {
match app.overlays.top_mut() {
Some(Modal::Form(form)) => form,
_ => panic!("no form is open"),
}
/// A transaction form with `field` ahead of the splits.
pub(super) fn with_field(mut app: App, field: Field) -> App {
let mut form = crate::form::Form::transaction_create();
form.fields.insert(0, field);
app.overlays.push(Modal::Form(form));
app
fn field(label: &'static str, widget: Widget) -> Field {
Field {
key: None,
label,
widget,
pub(super) fn select_field() -> Field {
let mut select = SelectWidget::new();
let option = |i: usize| SelectOption {
id: format!("id{i}"),
label: format!("label{i}"),
};
select.set_options((0..3).map(option).collect());
field("pick", Widget::Select(select))
fn text_field(text: &str) -> Field {
field("note", Widget::Text(Editor::with_buffer(text)))
fn area_field(text: &str, cursor: (usize, usize)) -> Field {
let mut editor = Editor::with_buffer(text).multiline(true);
editor.set_cursor_position(cursor);
field("memo", Widget::Text(editor))
fn editor_of(app: &mut App, index: usize) -> &Editor {
match form_mut(app).fields.get(index).map(|field| &field.widget) {
Some(Widget::Text(editor)) => editor,
_ => panic!("field {index} is not a text"),
fn click_at(app: &mut App, target: Target, dx: u16, dy: u16) {
redraw(app);
let area = area_of(app, target);
let position = Position::new(area.x + dx, area.y + dy);
press(
app,
position,
MouseEventKind::Down(MouseButton::Left),
now(),
);
fn caret(field: usize) -> Target {
Target::Caret { field, cell: None }
fn cell_id(col: usize) -> CellId {
CellId {
tag: false,
row: 0,
col,
fn splits_of(app: &mut App) -> &mut SplitsWidget {
match form_mut(app)
.fields
.get_mut(SPLITS + 1)
.map(|f| &mut f.widget)
{
Some(Widget::Splits(splits)) => splits,
_ => panic!("no splits"),
#[test]
fn a_click_on_the_mark_of_an_unfocused_select_opens_it_at_once() {
for app in presets() {
let mut app = with_field(app, select_field());
form_mut(&mut app).focus = 1;
click_on(
&mut app,
Target::Mark {
field: 0,
cell: None,
},
let form = form_mut(&mut app);
assert_eq!(form.focus, 0);
assert!(form.focused_select().is_some_and(|select| select.open));
fn a_click_elsewhere_on_an_unfocused_select_only_focuses_it() {
click_on(&mut app, Target::Field(0), now());
assert!(form.focused_select().is_some_and(|select| !select.open));
fn a_click_on_the_mark_of_a_select_cell_opens_it_at_once() {
let target = Target::Mark {
field: SPLITS + 1,
cell: Some(cell_id(COL_FROM)),
click_on(&mut app, target, now());
assert_eq!(form.focus, SPLITS + 1);
fn a_click_in_a_long_text_puts_the_caret_under_it_counting_the_scroll() {
let text = "0123456789".repeat(12);
let text = text.as_str();
let mut app = with_field(app, text_field(text));
redraw(&mut app);
let width = usize::from(area_of(&app, caret(0)).width);
click_at(&mut app, caret(0), 3, 0);
assert_eq!(form_mut(&mut app).focus, 0);
let skipped = text.chars().count() + 1 - (width - 1);
assert_eq!(editor_of(&mut app, 0).cursor(), skipped + 2);
fn a_click_past_the_end_of_a_short_text_lands_at_its_end() {
let mut app = with_field(crate::test_support::app(), text_field("abc"));
click_at(&mut app, caret(0), 15, 0);
assert_eq!(editor_of(&mut app, 0).cursor(), 3);
fn amount_cell() -> Target {
Target::Caret {
cell: Some(cell_id(COL_VALUE)),
fn type_amount(app: &mut App, text: &str) {
let splits = splits_of(app);
splits.focus_cell_id(cell_id(COL_VALUE));
if let Some(crate::widgets::FocusedSubWidget::Amount(amount)) = splits.focused_subwidget_mut() {
amount.as_editor_mut().replace_buffer(text);
amount.as_editor_mut().set_cursor(0);
fn amount_cursor(app: &mut App) -> usize {
match splits_of(app).focused_subwidget_mut() {
Some(crate::widgets::FocusedSubWidget::Amount(amount)) => amount.editor().cursor(),
_ => panic!("the amount cell is not focused"),
fn a_click_in_an_unfocused_amount_cell_focuses_it_and_places_the_caret() {
let mut app = with_field(app, text_field(""));
type_amount(&mut app, "123456");
click_at(&mut app, amount_cell(), 3, 0);
assert!(form_mut(&mut app).cell_focused(SPLITS + 1, cell_id(COL_VALUE)));
assert_eq!(amount_cursor(&mut app), 2);
fn a_click_in_a_focused_scrolled_amount_cell_counts_from_the_scroll() {
let text = "1234567890".repeat(12);
let mut app = with_field(crate::test_support::app(), text_field(""));
type_amount(&mut app, text);
form_mut(&mut app).focus_cell(SPLITS + 1, cell_id(COL_VALUE));
if let Some(crate::widgets::FocusedSubWidget::Amount(amount)) =
splits_of(&mut app).focused_subwidget_mut()
amount.as_editor_mut().set_cursor(text.len());
let width = usize::from(area_of(&app, amount_cell()).width);
click_at(&mut app, amount_cell(), 1, 0);
assert_eq!(amount_cursor(&mut app), text.len() + 1 - (width - 1));
fn a_click_in_a_text_area_puts_the_caret_on_the_clicked_line_and_column() {
let mut app = with_field(app, area_field("first\nsecond line\nthird", (0, 0)));
click_at(&mut app, caret(0), 4, 1);
assert_eq!(editor_of(&mut app, 0).cursor_position(), (1, 4));
fn a_click_past_a_line_or_below_the_text_clamps_into_it() {
let mut app = with_field(crate::test_support::app(), area_field("ab\ncd", (0, 0)));
click_at(&mut app, caret(0), 9, 0);
assert_eq!(editor_of(&mut app, 0).cursor_position(), (0, 2));
click_at(&mut app, caret(0), 1, 4);
assert_eq!(editor_of(&mut app, 0).cursor_position(), (1, 1));
fn a_click_in_a_scrolled_text_area_counts_from_the_viewport() {
let text = "l0\nl1\nl2\nl3\nl4\nl5\nl6\nlast line";
let mut app = with_field(crate::test_support::app(), area_field(text, (7, 2)));
assert_eq!(editor_of(&mut app, 0).viewport().top, 3);
click_at(&mut app, caret(0), 1, 0);
assert_eq!(editor_of(&mut app, 0).cursor_position(), (3, 1));
fn a_click_in_a_text_area_lands_on_a_wide_character_by_either_cell() {
let mut app = with_field(crate::test_support::app(), area_field("a\u{4e2d}b", (0, 0)));
click_at(&mut app, caret(0), 2, 0);
assert_eq!(editor_of(&mut app, 0).cursor_position(), (0, 1));
/// Draws `app` and returns where the terminal cursor is left.
fn screen_cursor(app: &mut App) -> Position {
let mut terminal = Terminal::new(TestBackend::new(80, 24)).expect("test terminal");
let mut hits = crate::hits::Hits::default();
terminal
.draw(|frame| crate::draw::draw(frame, app, &mut hits))
.expect("the frame draws");
app.hits = hits;
terminal.get_cursor_position().expect("the cursor position")
fn a_click_in_a_scrolled_text_keeps_the_view_and_the_caret_under_the_pointer() {
let mut app = with_field(app, text_field(&text));
form_mut(&mut app).focus = 0;
screen_cursor(&mut app);
let before = editor_of(&mut app, 0).viewport();
let area = area_of(&app, caret(0));
let at = screen_cursor(&mut app);
assert_eq!(editor_of(&mut app, 0).viewport(), before);
assert_eq!((at.x, at.y), (area.x + 3, area.y));
assert_eq!(editor_of(&mut app, 0).cursor(), before.left + 2);
fn a_click_in_a_scrolled_amount_cell_keeps_the_caret_under_the_pointer() {
type_amount(&mut app, &text);
click_at(&mut app, amount_cell(), 2, 0);
let area = area_of(&app, amount_cell());
assert_eq!((at.x, at.y), (area.x + 2, area.y));
fn a_click_in_a_text_of_a_dialog_off_the_left_edge_lands_on_the_char_clicked() {
let mut app = with_field(crate::test_support::app(), text_field("abcdefghij"));
let text = area_of(&app, caret(0));
Position::new(text.x + 3, text.y),
let clicked = editor_of(&mut app, 0).cursor();
assert!(clicked > 0);
let slot = app.hits.dialog().expect("a dialog was drawn");
let x = i32::from(slot.auto.x) - i32::from(text.x) - 2;
let place = tvui::Place {
x,
..tvui::Place::from(slot.auto)
app.overlays
.set_placement(crate::overlay::Placement::of(place, slot.auto));
let shown = area_of(&app, caret(0));
assert_eq!(shown.x, 0);
Position::new(1, shown.y),
assert_eq!(editor_of(&mut app, 0).cursor(), clicked);