Lines
100 %
Functions
Branches
//! A press on a dialog button draws it down; it acts when released on it.
use ratatui::crossterm::event::KeyCode;
use super::click_tests::{click_on, now, press};
use super::*;
use crate::form::Form;
use crate::hits::Target;
use crate::keymap::handle_key;
use crate::modal::ConfirmAction;
use crate::test_support::{area_of, key, redraw};
const LEFT: MouseButton = MouseButton::Left;
fn confirm_app() -> App {
let mut app = crate::test_support::app();
app.overlays.push(Modal::Confirm {
prompt: "Discard changes?".to_string(),
action: ConfirmAction::DiscardForm,
focus: None,
});
redraw(&mut app);
app
}
fn middle(app: &App, index: usize) -> Position {
let area = area_of(app, Target::Button(index));
Position::new(area.x + area.width / 2, area.y)
/// Presses and releases the left button on button `index` of the drawn frame.
pub(super) fn click_button(app: &mut App, index: usize, at: Instant) {
click_on(app, Target::Button(index), at);
let position = middle(app, index);
press(app, position, MouseEventKind::Up(LEFT), at);
#[test]
fn a_press_draws_the_button_down_and_does_not_act() {
let mut app = confirm_app();
let position = middle(&app, 0);
press(&mut app, position, MouseEventKind::Down(LEFT), now());
assert_eq!(app.pressed_button(), Some(0));
assert!(app.overlays.top().is_some());
fn the_release_on_the_button_acts() {
click_button(&mut app, 0, now());
assert!(app.overlays.top().is_none());
assert_eq!(app.pressed_button(), None);
fn the_release_off_the_button_does_not_act() {
press(
&mut app,
Position::new(0, 0),
MouseEventKind::Up(LEFT),
now(),
);
fn the_button_pops_up_while_the_pointer_is_off_it_and_down_again_on_return() {
let (on, off) = (middle(&app, 0), Position::new(0, 0));
press(&mut app, on, MouseEventKind::Down(LEFT), now());
press(&mut app, off, MouseEventKind::Drag(LEFT), now());
press(&mut app, on, MouseEventKind::Drag(LEFT), now());
press(&mut app, on, MouseEventKind::Up(LEFT), now());
fn a_release_with_no_press_acts_on_nothing() {
press(&mut app, position, MouseEventKind::Up(LEFT), now());
fn confirm_over_a_form() -> App {
app.overlays.push(Modal::Form(Form::config_set()));
fn only_the_face_presses_not_the_blank_column_the_shadow_or_the_row_below() {
let face = area_of(&app, Target::Button(0));
let off = [
(face.x - 1, face.y),
(face.right(), face.y),
(face.x + 2, face.y + 1),
];
for (x, y) in off {
let at = Position::new(x, y);
press(&mut app, at, MouseEventKind::Down(LEFT), now());
assert_eq!(app.pressed_button(), None, "{x} {y}");
press(&mut app, at, MouseEventKind::Up(LEFT), now());
fn a_key_drops_the_hold_so_the_release_does_not_press_the_dialog_under_it() {
let mut app = confirm_over_a_form();
handle_key(&mut app, key(KeyCode::Esc));
assert!(matches!(app.overlays.top(), Some(Modal::Form(_))));
assert!(app.status.is_empty());
fn a_change_of_dialog_drops_the_hold() {
app.overlays.pop();
fn a_click_that_lets_a_key_press_act_is_dropped_whole() {
handle_key(&mut app, key(KeyCode::Enter));
fn a_key_that_leaves_the_dialog_open_still_drops_the_hold() {
handle_key(&mut app, key(KeyCode::Tab));
assert!(matches!(app.overlays.top(), Some(Modal::Confirm { .. })));