Lines
100 %
Functions
Branches
//! A press on the title row of a dialog and the motion that follows move it, the release keeps
//! it, and the limits are those of a window.
use super::overlap::{left_click, left_drag, left_release};
use crate::app::App;
use crate::focus::{FocusTarget, current_focus};
use crate::form::Form;
use crate::hits::Target;
use crate::modal::{ConfirmAction, Modal};
use crate::overlay::Placement;
use crate::test_support::{key, press, redraw};
use ratatui::crossterm::event::KeyCode;
use ratatui::layout::Rect;
use std::time::Instant;
use tvui::Place;
fn form_app() -> App {
let mut app = crate::test_support::app();
app.overlays.push(Modal::Form(Form::config_set()));
redraw(&mut app);
app
}
fn place_of(app: &App) -> Place {
let slot = app.hits.dialog().expect("a dialog was drawn");
app.overlays
.placement()
.place(slot.auto, slot.min, slot.host)
/// The cell `dx` columns into the title row of the dialog on top.
fn title_cell(app: &App, dx: u16) -> (u16, u16) {
let found = app
.hits
.entries()
.into_iter()
.rfind(|(_, target)| *target == Target::Title);
let (rect, _): (Rect, _) = found.expect("a title row was drawn");
(rect.x + dx, rect.y)
fn at(cell: (u16, u16), dx: i32, dy: i32) -> (u16, u16) {
let moved = |value: u16, by: i32| u16::try_from(i32::from(value) + by).unwrap_or_default();
(moved(cell.0, dx), moved(cell.1, dy))
#[test]
fn a_title_drag_moves_the_dialog_by_the_motion_and_the_release_keeps_it() {
let mut app = form_app();
let (start, grab, now) = (place_of(&app), title_cell(&app, 10), Instant::now());
left_click(&mut app, grab, now);
assert_eq!(current_focus(&app), FocusTarget::Drag);
left_drag(&mut app, at(grab, 4, 3), now);
let moved = place_of(&app);
assert_eq!((moved.x, moved.y), (start.x + 4, start.y + 3));
left_drag(&mut app, at(grab, 6, 5), now);
left_release(&mut app, at(grab, 6, 5), now);
let kept = place_of(&app);
assert_eq!((kept.x, kept.y), (start.x + 6, start.y + 5));
assert_eq!((kept.width, kept.height), (start.width, start.height));
assert!(!app.dragging());
fn a_press_and_release_without_motion_change_nothing() {
let (grab, now) = (title_cell(&app, 10), Instant::now());
left_release(&mut app, grab, now);
assert_eq!(app.overlays.placement(), Placement::default());
fn a_drag_stops_at_the_limits_of_a_window() {
let host = app.hits.dialog().expect("drawn").host;
left_drag(&mut app, (0, 0), now);
let high = place_of(&app);
assert_eq!(high.y, i32::from(host.y));
assert_eq!(high.x, -10);
left_drag(&mut app, (79, 23), now);
let low = place_of(&app);
assert_eq!(low.y, i32::from(host.bottom()) - 1);
assert_eq!(low.x, 69);
left_release(&mut app, (79, 23), now);
assert_eq!((low.width, low.height), (start.width, start.height));
fn a_lost_release_ends_on_esc_or_the_next_press() {
left_drag(&mut app, at(grab, 5, 2), now);
press(&mut app, key(KeyCode::Esc));
assert_eq!(place_of(&app), start);
left_click(&mut app, (0, 0), now);
assert_eq!(place_of(&app), moved);
fn a_confirm_is_dragged_by_its_title_too() {
app.overlays.push(Modal::Confirm {
prompt: "Discard changes?".to_string(),
action: ConfirmAction::DiscardForm,
focus: None,
});
left_drag(&mut app, at(grab, 3, 2), now);
left_release(&mut app, at(grab, 3, 2), now);
assert_eq!((moved.x, moved.y), (start.x + 3, start.y + 2));
fn a_press_on_the_close_icon_does_not_drag() {
let icon = app
.rfind(|(_, target)| *target == Target::Close)
.map(|(rect, _)| (rect.x + 1, rect.y))
.expect("a close icon was drawn");
left_click(&mut app, icon, Instant::now());