Lines
99.43 %
Functions
100 %
Branches
//! Left clicks on windows: a rear window is raised, the frame icons and the title row act on
//! the front one, and a modal blocks them all.
use super::overlap::{
FRONT_AT, REAR_AT, left_click, left_release, overlapped, place_window, wheel_down,
};
use super::report_chart::{call_for, echoed, show, with_table};
use crate::app::App;
use crate::hits::Target;
use crate::modal::{ConfirmAction, Modal};
use crate::tabs::list::ListTab;
use crate::test_support::{area_of, redraw};
use crate::view::ViewId;
use crate::window::WindowId;
use ratatui::layout::Rect;
use rpc::chart::ChartBackend;
use std::time::{Duration, Instant};
use tvui::Place;
fn middle(rect: Rect) -> (u16, u16) {
(rect.x + rect.width / 2, rect.y)
}
fn zoomed(app: &App, kind: ViewId) -> bool {
let id = app.window_id(kind);
id.and_then(|id| app.desktop.window(id))
.is_some_and(|window| window.zoomed)
/// Two accounts lists, the second one in front and overlapping the first.
fn two_lists() -> (App, WindowId, WindowId) {
let (mut app, ..) = overlapped();
app.close_all_windows();
let mut ids = Vec::new();
for _ in 0..2 {
app.open_view(ViewId::Accounts);
let row = |i: usize| format!(r#"(:account :id "u{i}" :name "a{i}" :parent "")"#);
let rows: Vec<String> = (0..3).map(row).collect();
app.accounts_mut()
.on_reply(&format!("(:id 1 :value ({}))", rows.join(" ")));
ids.extend(app.front_window());
let [rear, front] = ids[..] else {
panic!("two windows");
place_window(&mut app, rear, REAR_AT);
place_window(&mut app, front, FRONT_AT);
redraw(&mut app);
(app, rear, front)
fn selected(app: &App, id: WindowId) -> Option<String> {
let list = app.win::<ListTab>(id)?;
list.selected_id().map(str::to_string)
fn rear_row(app: &App, row: usize) -> (u16, u16) {
let entries = app.hits.entries();
let found = entries.iter().find(|(_, t)| *t == Target::Row(row));
found.map_or((0, 0), |(rect, _)| (rect.x, rect.y))
#[test]
fn a_click_on_a_rear_row_raises_the_window_without_selecting_it() {
let (mut app, rear, front) = two_lists();
let before = selected(&app, rear);
let at = rear_row(&app, 2);
left_click(&mut app, at, Instant::now());
assert_eq!(app.front_window(), Some(rear));
assert_eq!(selected(&app, rear), before);
assert_eq!(selected(&app, front), Some("u0".to_string()));
fn the_wheel_over_a_rear_window_scrolls_it_and_raises_nothing() {
let at = rear_row(&app, 0);
wheel_down(&mut app, at, Instant::now());
assert_eq!(selected(&app, rear), Some("u1".to_string()));
assert_eq!(app.front_window(), Some(front));
fn a_double_click_on_a_rear_title_raises_the_window_and_zooms_it() {
let (mut app, rear, _) = overlapped();
let at = (
u16::try_from(REAR_AT.x).unwrap_or_default() + 6,
u16::try_from(REAR_AT.y).unwrap_or_default(),
);
let start = Instant::now();
left_click(&mut app, at, start);
assert!(!zoomed(&app, ViewId::Console));
left_click(&mut app, at, start + Duration::from_millis(100));
assert!(zoomed(&app, ViewId::Console));
fn a_click_on_the_front_window_acts_in_it_where_it_covers_the_rear_one() {
let (mut app, _, front) = overlapped();
let row = area_of(&app, Target::Row(1));
left_click(&mut app, (row.x, row.y), Instant::now());
assert_eq!(app.selected_id(ViewId::Accounts).as_deref(), Some("u1"));
fn a_click_on_the_front_frame_over_the_rear_body_stays_in_front() {
left_click(&mut app, (20, 8), Instant::now());
fn a_click_on_the_desktop_between_windows_goes_to_no_window() {
let before = (app.front_window(), app.windows.len());
left_click(&mut app, (70, 20), Instant::now());
assert_eq!((app.front_window(), app.windows.len()), before);
fn the_close_icon_closes_the_front_window() {
let icon = middle(area_of(&app, Target::Close));
left_click(&mut app, icon, Instant::now());
assert!(app.window_id(ViewId::Accounts).is_some());
left_release(&mut app, icon, Instant::now());
assert_eq!(app.window_id(ViewId::Accounts), None);
fn a_release_off_the_close_icon_keeps_the_window() {
left_release(&mut app, (icon.0 + 10, icon.1 + 3), Instant::now());
fn the_zoom_icon_zooms_the_front_window_and_restores_it() {
for want in [true, false] {
let icon = area_of(&app, Target::Zoom);
left_click(&mut app, middle(icon), Instant::now());
assert_eq!(zoomed(&app, ViewId::Accounts), want);
#[tokio::test]
async fn the_zoom_icon_refetches_the_chart_at_the_new_size() {
let (mut app, sent) = with_table(80, 24, ChartBackend::Halfblocks);
show(&mut app);
let echoed = echoed(&mut app, &sent).await;
assert!(echoed.contains(&call_for(580, 280)), "{echoed}");
fn title_x() -> u16 {
u16::try_from(FRONT_AT.x).unwrap_or_default() + 10
fn title_y() -> u16 {
u16::try_from(FRONT_AT.y).unwrap_or_default()
fn two_quick_clicks_on_the_title_row_zoom_and_one_does_not() {
let at = (title_x(), title_y());
assert!(!zoomed(&app, ViewId::Accounts));
assert!(zoomed(&app, ViewId::Accounts));
fn two_slow_clicks_on_the_title_row_do_not_zoom() {
left_click(&mut app, at, start + Duration::from_secs(2));
fn a_modal_blocks_the_icons() {
let icons = [Target::Close, Target::Zoom].map(|target| middle(area_of(&app, target)));
app.overlays.push(Modal::Confirm {
prompt: "Sure?".to_string(),
action: ConfirmAction::DiscardForm,
focus: None,
});
for icon in icons {
fn hung_front(x: i32) -> App {
place_window(&mut app, front, Place { x, ..FRONT_AT });
app
fn a_window_off_the_right_edge_has_only_its_visible_icons() {
let mut app = hung_front(60);
let hits = &app.hits;
assert!(hits.entries().iter().all(|(_, t)| *t != Target::Zoom));
fn a_window_off_the_left_edge_has_only_its_visible_icons() {
let mut app = hung_front(-30);
assert!(app.hits.entries().iter().all(|(_, t)| *t != Target::Close));