Lines
100 %
Functions
Branches
//! Ctrl-F5 on a dialog: the dialog on top moves and sizes from the keyboard with the limits of a
//! window, Esc puts it back and Enter keeps it.
use crate::app::App;
use crate::event::{Intent, apply};
use crate::focus::{FocusTarget, current_focus};
use crate::form::Form;
use crate::modal::{ConfirmAction, Modal};
use crate::overlay::Placement;
use crate::test_support::{ctrl, key, press, redraw};
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use tvui::{Change, 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 confirm_app() -> App {
app.overlays.push(Modal::Confirm {
prompt: "Discard changes?".to_string(),
action: ConfirmAction::DiscardForm,
focus: None,
});
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)
fn begin(app: &mut App) {
press(app, ctrl(KeyCode::F(5)));
assert_eq!(current_focus(app), FocusTarget::Drag);
fn change(app: &mut App, change: Change) {
apply(app, Intent::Drag(change));
redraw(app);
fn shift(code: KeyCode) -> KeyEvent {
KeyEvent::new(code, KeyModifiers::SHIFT)
#[test]
fn ctrl_f5_moves_the_form_with_the_arrows_and_enter_keeps_it() {
let mut app = form_app();
let start = place_of(&app);
begin(&mut app);
press(&mut app, key(KeyCode::Right));
press(&mut app, key(KeyCode::Down));
let moved = place_of(&app);
assert_eq!((moved.x, moved.y), (start.x + 1, start.y + 2));
press(&mut app, key(KeyCode::Enter));
assert_eq!(current_focus(&app), FocusTarget::Overlay);
assert_eq!(place_of(&app), moved);
fn esc_puts_the_dialog_back() {
press(&mut app, key(KeyCode::Left));
press(&mut app, shift(KeyCode::Right));
assert_ne!(place_of(&app), start);
press(&mut app, key(KeyCode::Esc));
assert_eq!(place_of(&app), start);
assert_eq!(app.overlays.placement(), Placement::default());
fn the_keys_of_the_mode_do_not_reach_the_form() {
let before = format!("{:?}", app.overlays.top());
press(&mut app, key(KeyCode::Char('x')));
assert_eq!(format!("{:?}", app.overlays.top()), before);
fn the_limits_are_one_column_inside_left_and_right_and_the_top_inside() {
let host = app.hits.dialog().expect("drawn").host;
let width = i32::from(place_of(&app).width);
change(&mut app, Change::Move(-500, 0));
assert_eq!(place_of(&app).x, 1 - width);
change(&mut app, Change::Move(1000, 0));
assert_eq!(place_of(&app).x, i32::from(host.right()) - 1);
change(&mut app, Change::Move(0, -500));
assert_eq!(place_of(&app).y, i32::from(host.y));
change(&mut app, Change::Move(0, 500));
assert_eq!(place_of(&app).y, i32::from(host.bottom()) - 1);
fn a_form_is_sized_between_its_least_and_the_desktop() {
let (slot, start) = (app.hits.dialog().expect("drawn"), place_of(&app));
press(&mut app, shift(KeyCode::Down));
let grown = place_of(&app);
assert_eq!(
(grown.width, grown.height),
(start.width + 1, start.height + 1)
);
change(&mut app, Change::Grow(-500, -500));
let least = place_of(&app);
assert_eq!((least.width, least.height), slot.min);
change(&mut app, Change::Grow(500, 500));
let most = place_of(&app);
(most.width, most.height),
(slot.host.width, slot.host.height)
fn a_confirm_moves_but_is_not_sized() {
let mut app = confirm_app();
change(&mut app, Change::Grow(10, 3));
change(&mut app, Change::SizeTo(70, 20));
change(&mut app, Change::Move(3, 1));
assert_eq!((moved.x, moved.y), (start.x + 3, start.y + 1));
assert_eq!((moved.width, moved.height), (start.width, start.height));
fn an_overlay_opened_during_the_mode_ends_it() {
assert!(!app.dragging());
fn ctrl_f5_needs_a_drawn_dialog() {
press(&mut app, ctrl(KeyCode::F(5)));