Lines
100 %
Functions
Branches
//! Ctrl-F5: the front window moves and sizes from the keyboard until it is kept or put back.
use super::report_chart::{call_for, chart_requests, echoed, show, with_table};
use super::window_keys::{answer_chart, drawn_kitty_chart};
use crate::app::{App, MenuId};
use crate::event::{Intent, apply};
use crate::focus::{FocusTarget, current_focus};
use crate::form::Form;
use crate::keymap::{here, in_view_all};
use crate::modal::Modal;
use crate::test_support::{alt, ctrl, draw_frame, hint_pairs, key, press};
use crate::view::ViewId;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::layout::Rect;
use rpc::chart::ChartBackend;
use tvui::{Change, FrameMode, Role, Window};
const START: Rect = Rect::new(10, 5, 30, 10);
fn chord(code: KeyCode, modifiers: KeyModifiers) -> KeyEvent {
KeyEvent::new(code, modifiers)
}
fn shift(code: KeyCode) -> KeyEvent {
chord(code, KeyModifiers::SHIFT)
fn ctrl_shift(code: KeyCode) -> KeyEvent {
chord(code, KeyModifiers::CONTROL | KeyModifiers::SHIFT)
fn start_size_move(app: &mut App) {
press(app, ctrl(KeyCode::F(5)));
fn front(app: &App) -> Window<crate::window::WindowId> {
let id = app.front_window().expect("a front window");
app.desktop.window(id).expect("it is open")
fn rect(app: &App) -> Rect {
let place = app.desktop.restored_place(id, app.desktop_rect());
let place = place.expect("it is open");
crate::test_support::flat(place)
fn placed(app: &mut App) {
let window = front(app);
app.desktop.restore(Window {
place: tvui::Place::from(START),
zoomed: false,
..window
});
fn placed_app() -> App {
let mut app = crate::test_support::app();
placed(&mut app);
app
fn mode_keys() -> [(&'static str, Intent); 22] {
let drag = Intent::Drag;
[
("Left", drag(Change::Move(-1, 0))),
("Right", drag(Change::Move(1, 0))),
("Up", drag(Change::Move(0, -1))),
("Down", drag(Change::Move(0, 1))),
("S-Left", drag(Change::Grow(-1, 0))),
("S-Right", drag(Change::Grow(1, 0))),
("S-Up", drag(Change::Grow(0, -1))),
("S-Down", drag(Change::Grow(0, 1))),
("C-Left", drag(Change::Move(-8, 0))),
("C-Right", drag(Change::Move(8, 0))),
("C-Up", drag(Change::Move(0, -4))),
("C-Down", drag(Change::Move(0, 4))),
("C-S-Left", drag(Change::Grow(-8, 0))),
("C-S-Right", drag(Change::Grow(8, 0))),
("C-S-Up", drag(Change::Grow(0, -4))),
("C-S-Down", drag(Change::Grow(0, 4))),
("Home", drag(Change::Left)),
("End", drag(Change::Right)),
("PageUp", drag(Change::Top)),
("PageDown", drag(Change::Bottom)),
("Enter", Intent::DragDone),
("Esc", Intent::DragCancel),
]
#[test]
fn ctrl_f5_puts_the_front_window_in_the_dragging_state() {
let mut app = placed_app();
assert_eq!(app.frame_state(id).mode, FrameMode::Active);
start_size_move(&mut app);
assert_eq!(current_focus(&app), FocusTarget::Drag);
assert_eq!(app.frame_state(id).mode, FrameMode::Dragging);
fn the_arrows_move_by_one_and_shift_arrows_size_by_one() {
let steps = [
(key(KeyCode::Right), Rect::new(11, 5, 30, 10)),
(key(KeyCode::Down), Rect::new(11, 6, 30, 10)),
(key(KeyCode::Left), Rect::new(10, 6, 30, 10)),
(key(KeyCode::Up), Rect::new(10, 5, 30, 10)),
(shift(KeyCode::Right), Rect::new(10, 5, 31, 10)),
(shift(KeyCode::Down), Rect::new(10, 5, 31, 11)),
(shift(KeyCode::Left), Rect::new(10, 5, 30, 11)),
(shift(KeyCode::Up), Rect::new(10, 5, 30, 10)),
];
for (event, expected) in steps {
press(&mut app, event);
assert_eq!(rect(&app), expected, "{event:?}");
fn ctrl_arrows_step_eight_columns_or_four_rows() {
(ctrl(KeyCode::Right), Rect::new(18, 5, 30, 10)),
(ctrl(KeyCode::Down), Rect::new(18, 9, 30, 10)),
(ctrl(KeyCode::Left), Rect::new(10, 9, 30, 10)),
(ctrl(KeyCode::Up), Rect::new(10, 5, 30, 10)),
(ctrl_shift(KeyCode::Right), Rect::new(10, 5, 38, 10)),
(ctrl_shift(KeyCode::Down), Rect::new(10, 5, 38, 14)),
(ctrl_shift(KeyCode::Left), Rect::new(10, 5, 30, 14)),
(ctrl_shift(KeyCode::Up), Rect::new(10, 5, 30, 10)),
fn home_end_page_up_and_page_down_go_to_the_desktop_edges() {
let area = app.desktop_rect();
press(&mut app, key(KeyCode::Home));
assert_eq!(rect(&app).x, area.x);
press(&mut app, key(KeyCode::End));
assert_eq!(rect(&app).right(), area.right());
press(&mut app, key(KeyCode::PageUp));
assert_eq!(rect(&app).y, area.y);
press(&mut app, key(KeyCode::PageDown));
assert_eq!(rect(&app).bottom(), area.bottom());
fn a_window_moved_off_the_right_edge_keeps_a_column_and_draws_it() {
for _ in 0..12 {
press(&mut app, ctrl(KeyCode::Right));
assert_eq!(rect(&app).x, area.right() - 1);
let (buffer, _) = draw_frame(&app, 80, 24);
assert_ne!(buffer[(area.right() - 1, START.y)].symbol(), "\u{2591}");
fn enter_keeps_the_new_rect_and_leaves_the_mode() {
press(&mut app, key(KeyCode::Right));
press(&mut app, shift(KeyCode::Down));
press(&mut app, key(KeyCode::Enter));
assert_eq!(rect(&app), Rect::new(11, 5, 30, 11));
assert_eq!(current_focus(&app), FocusTarget::ViewPane);
fn a_zoomed_window_moves_from_the_full_desktop() {
assert!(front(&app).zoomed);
assert!(!front(&app).zoomed);
assert_eq!(
rect(&app),
Rect::new(area.x + 1, area.y, area.width, area.height)
);
fn escape_restores_the_rect_and_the_zoom() {
let before = front(&app);
assert!(before.zoomed);
press(&mut app, key(KeyCode::Esc));
assert_eq!(front(&app), before);
fn other_keys_do_nothing_in_the_mode_and_the_menu_stays_closed() {
let order: Vec<_> = app.desktop.iter_back_to_front().copied().collect();
let ignored = [
key(KeyCode::Char('q')),
key(KeyCode::Char(':')),
key(KeyCode::Char('?')),
key(KeyCode::Tab),
key(KeyCode::F(1)),
key(KeyCode::F(5)),
key(KeyCode::F(6)),
key(KeyCode::F(10)),
ctrl(KeyCode::F(4)),
ctrl(KeyCode::F(5)),
alt(KeyCode::Char('x')),
alt(KeyCode::Char('m')),
alt(KeyCode::Char('3')),
for event in ignored {
assert_eq!(current_focus(&app), FocusTarget::Drag, "{event:?}");
assert!(app.menu.is_none() && !app.should_quit, "{event:?}");
assert!(app.overlays.is_empty() && !app.cmdline.active, "{event:?}");
let now: Vec<_> = app.desktop.iter_back_to_front().copied().collect();
assert_eq!(now, order, "{event:?}");
fn a_dialog_blocks_entering_the_mode() {
app.open_dialog_on_front(Modal::Form(Form::commodity_create()));
assert!(!app.dragging());
assert_eq!(current_focus(&app), FocusTarget::Overlay);
apply(&mut app, Intent::SizeWindow);
fn the_first_entry_of_the_window_menu_starts_the_mode() {
app.open_menu_of(MenuId::Window);
assert!(app.menu.is_none());
app.end_drag(false);
press(&mut app, key(KeyCode::Char('s')));
fn every_preset_binds_ctrl_f5_and_the_mode_keys() {
let apps = [
crate::test_support::app(),
crate::test_support::emacs_app(),
crate::test_support::vim_app(),
for mut app in apps {
let name = app.keys.indicator();
in_view_all(&app).bound("C-F5"),
Some(Intent::SizeWindow),
"{name}"
assert_eq!(current_focus(&app), FocusTarget::Drag, "{name}");
let keys = here(&app);
for (chord, intent) in mode_keys() {
assert_eq!(keys.bound(chord), Some(intent), "{name} {chord}");
fn the_status_line_shows_the_mode_hints() {
let shown = hint_pairs(&app);
let shown: Vec<(&str, &str)> = shown
.iter()
.map(|(key, label)| (key.as_str(), label.as_str()))
.collect();
let hints = [
("Left", "Move"),
("S-Left", "Size"),
("Enter", "OK"),
("Esc", "Cancel"),
assert_eq!(shown, hints);
fn the_dragging_frame_is_single_in_the_icon_colour_with_its_icons() {
let cell = &buffer[(START.x, START.y)];
assert_eq!(cell.symbol(), "\u{250c}");
assert_eq!(cell.style().fg, app.theme.style(Role::FrameIcon).fg);
let row: String = (START.x..START.right())
.map(|x| buffer[(x, START.y)].symbol().to_string())
assert!(row.starts_with("\u{250c}\u{2500}[\u{25a0}]"), "{row}");
let icon = buffer[(START.x + 3, START.y)].style();
assert_eq!(icon.fg, app.theme.style(Role::FrameIcon).fg);
#[tokio::test]
async fn accepting_fetches_the_chart_again_at_the_new_size() {
let (mut app, sent) = with_table(60, 24, ChartBackend::Halfblocks);
show(&mut app);
press(&mut app, key(KeyCode::F(5)));
let before = chart_requests(&app).len();
press(&mut app, shift(KeyCode::Left));
assert_eq!(chart_requests(&app).len(), before);
assert_eq!(chart_requests(&app).len(), before + 1);
// The window is 29x10 after the shrink: 27x8 cells inside its border.
assert!(echoed(&mut app, &sent).await.contains(&call_for(270, 160)));
async fn cancelling_fetches_the_chart_again_at_the_old_size() {
let (mut app, _) = with_table(60, 24, ChartBackend::Halfblocks);
async fn the_kitty_image_goes_when_the_mode_starts_and_returns_with_a_fresh_chart() {
let (mut app, image) = drawn_kitty_chart();
assert_eq!(app.take_outbound(), vec![tvui::kitty_delete(image)]);
answer_chart(&mut app);
assert!(
super::report_chart::text(&super::report_chart::rendered(&app, 40, 20)).contains("a=T")
fn every_view_window_can_be_sized() {
for kind in ViewId::ALL {
app.switch_tab(kind);
assert_eq!(current_focus(&app), FocusTarget::Drag, "{kind:?}");
assert_ne!(current_focus(&app), FocusTarget::Drag, "{kind:?}");