1
//! Two overlapping windows laid out by hand, for the hit and mouse tests.
2

            
3
use crate::app::App;
4
use crate::tabs::reports::chart::TerminalEnv;
5
use crate::test_support::redraw;
6
use crate::view::ViewId;
7
use crate::window::WindowId;
8
use ratatui::crossterm::event::{KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
9
use ratatui::layout::Size;
10
use rpc::chart::ChartBackend;
11
use std::time::Instant;
12
use tvui::{Place, Window};
13

            
14
pub(crate) const REAR_AT: Place = Place {
15
    x: 2,
16
    y: 2,
17
    width: 36,
18
    height: 10,
19
};
20
pub(crate) const FRONT_AT: Place = Place {
21
    x: 20,
22
    y: 5,
23
    width: 40,
24
    height: 12,
25
};
26

            
27
93
pub(crate) fn place_window(app: &mut App, id: WindowId, place: Place) {
28
93
    let Some(window) = app.desktop.window(id) else {
29
        return;
30
    };
31
93
    app.desktop.restore(Window {
32
93
        place,
33
93
        zoomed: false,
34
93
        ..window
35
93
    });
36
93
}
37

            
38
/// An 80 by 24 app with a console behind a loaded accounts list, overlapping, and the frame drawn.
39
40
pub(crate) fn overlapped() -> (App, WindowId, WindowId) {
40
40
    let mut app = crate::test_support::app();
41
40
    app.terminal.replace(TerminalEnv {
42
40
        screen: Size::new(80, 24),
43
40
        cell_pixels: (10, 20),
44
40
        backend: ChartBackend::Halfblocks,
45
40
    });
46
40
    app.close_all_windows();
47
40
    app.open_view(ViewId::Console);
48
40
    app.open_view(ViewId::Accounts);
49
120
    let row = |i: usize| format!(r#"(:account :id "u{i}" :name "a{i}" :parent "")"#);
50
40
    let rows: Vec<String> = (0..3).map(row).collect();
51
40
    app.accounts_mut()
52
40
        .on_reply(&format!("(:id 1 :value ({}))", rows.join(" ")));
53
40
    let rear = app.window_id(ViewId::Console);
54
40
    let front = app.window_id(ViewId::Accounts);
55
40
    let (Some(rear), Some(front)) = (rear, front) else {
56
        panic!("both windows are open");
57
    };
58
40
    place_window(&mut app, rear, REAR_AT);
59
40
    place_window(&mut app, front, FRONT_AT);
60
40
    redraw(&mut app);
61
40
    (app, rear, front)
62
40
}
63

            
64
95
fn mouse(app: &mut App, kind: MouseEventKind, (x, y): (u16, u16), when: Instant) {
65
95
    let mouse = MouseEvent {
66
95
        kind,
67
95
        column: x,
68
95
        row: y,
69
95
        modifiers: KeyModifiers::NONE,
70
95
    };
71
95
    crate::mouse::handle_mouse(app, mouse, when);
72
95
    redraw(app);
73
95
}
74

            
75
/// A left press at `at` on the last drawn frame, then the redraw the runtime would do.
76
49
pub(crate) fn left_click(app: &mut App, at: (u16, u16), when: Instant) {
77
49
    mouse(app, MouseEventKind::Down(MouseButton::Left), at, when);
78
49
}
79

            
80
15
pub(crate) fn left_release(app: &mut App, at: (u16, u16), when: Instant) {
81
15
    mouse(app, MouseEventKind::Up(MouseButton::Left), at, when);
82
15
}
83

            
84
/// A move with the left button held, to `at`.
85
29
pub(crate) fn left_drag(app: &mut App, at: (u16, u16), when: Instant) {
86
29
    mouse(app, MouseEventKind::Drag(MouseButton::Left), at, when);
87
29
}
88

            
89
2
pub(crate) fn wheel_down(app: &mut App, at: (u16, u16), when: Instant) {
90
2
    mouse(app, MouseEventKind::ScrollDown, at, when);
91
2
}