1
//! A window hanging off an edge draws at its full size and shows the part inside the desktop.
2

            
3
use super::open::app_on;
4
use crate::app::App;
5
use crate::hits::{Surface, Target};
6
use crate::test_support::draw_frame;
7
use crate::view::ViewId;
8
use ratatui::buffer::Buffer;
9
use ratatui::layout::Rect;
10
use tvui::{Place, Window};
11

            
12
const WIDTH: u16 = 80;
13
const HEIGHT: u16 = 24;
14
const SIZE: (u16, u16) = (40, 10);
15
const TOP: i32 = 3;
16

            
17
3
fn console_app() -> App {
18
3
    let mut app = app_on(WIDTH, HEIGHT);
19
3
    app.open_view(ViewId::Console);
20
3
    app
21
3
}
22

            
23
6
fn place_front(app: &mut App, x: i32) -> Place {
24
6
    let id = app.front_window().expect("a front window");
25
6
    let window = app.desktop.window(id).expect("it is open");
26
6
    let place = Place {
27
6
        x,
28
6
        y: TOP,
29
6
        width: SIZE.0,
30
6
        height: SIZE.1,
31
6
    };
32
6
    app.desktop.restore(Window {
33
6
        place,
34
6
        zoomed: false,
35
6
        ..window
36
6
    });
37
6
    place
38
6
}
39

            
40
90
fn column(buf: &Buffer, x: u16) -> Vec<(String, ratatui::style::Style)> {
41
90
    let top = u16::try_from(TOP).unwrap_or_default();
42
90
    (top..top + SIZE.1)
43
900
        .map(|y| (buf[(x, y)].symbol().to_string(), buf[(x, y)].style()))
44
        .collect()
45
90
}
46

            
47
#[test]
48
1
fn a_window_off_the_right_edge_shows_its_left_part_laid_out_as_in_full() {
49
1
    let mut app = console_app();
50
1
    place_front(&mut app, 10);
51
1
    let whole = draw_frame(&app, WIDTH, HEIGHT).0;
52
1
    place_front(&mut app, 60);
53
1
    let cut = draw_frame(&app, WIDTH, HEIGHT).0;
54
1
    for shown in 0..20 {
55
20
        assert_eq!(
56
20
            column(&cut, 60 + shown),
57
20
            column(&whole, 10 + shown),
58
            "{shown}"
59
        );
60
    }
61
1
}
62

            
63
#[test]
64
1
fn a_window_off_the_left_edge_shows_its_right_part_laid_out_as_in_full() {
65
1
    let mut app = console_app();
66
1
    place_front(&mut app, 10);
67
1
    let whole = draw_frame(&app, WIDTH, HEIGHT).0;
68
1
    place_front(&mut app, -15);
69
1
    let cut = draw_frame(&app, WIDTH, HEIGHT).0;
70
1
    for shown in 0..25 {
71
25
        assert_eq!(column(&cut, shown), column(&whole, 25 + shown), "{shown}");
72
    }
73
1
}
74

            
75
#[test]
76
1
fn only_the_cells_on_screen_are_hittable() {
77
1
    let mut app = console_app();
78
1
    place_front(&mut app, 60);
79
1
    let hits = draw_frame(&app, WIDTH, HEIGHT).1;
80
1
    let body = Target::Body(Surface::Pane);
81
1
    let (rect, _) = hits
82
1
        .entries()
83
1
        .iter()
84
4
        .find(|(_, target)| *target == body)
85
        .copied()
86
1
        .expect("the console body is hittable");
87
1
    assert_eq!(rect, Rect::new(61, 4, 19, 7));
88
1
    assert_eq!(rect.right(), WIDTH);
89

            
90
1
    place_front(&mut app, -30);
91
1
    let hits = draw_frame(&app, WIDTH, HEIGHT).1;
92
1
    let (rect, _) = hits
93
1
        .entries()
94
1
        .iter()
95
4
        .find(|(_, target)| *target == body)
96
        .copied()
97
1
        .expect("the console body is hittable");
98
1
    assert_eq!(rect, Rect::new(0, 4, 9, 7));
99
1
}