1
//! A submitted form stays where its dialog stood, whatever happens to it before the reply.
2

            
3
use crate::app::App;
4
use crate::form::Form;
5
use crate::modal::Modal;
6
use crate::overlay::Placement;
7
use crate::test_support::{app, draw_frame, draw_into};
8
use ratatui::buffer::Buffer;
9
use ratatui::layout::Rect;
10
use tvui::Place;
11

            
12
/// The rect of the double-framed dialog in `buffer`.
13
6
pub(crate) fn dialog_rect(buffer: &Buffer) -> Option<Rect> {
14
6
    let area = buffer.area;
15
6
    let mut corners = (area.y..area.bottom())
16
12976
        .flat_map(|y| (area.x..area.right()).map(move |x| (x, y)))
17
12976
        .filter(|&at| matches!(buffer[at].symbol(), "╔" | "╝"));
18
6
    let (left, top) = corners.next()?;
19
6
    let (right, bottom) = corners.next_back()?;
20
6
    Some(Rect::new(left, top, right + 1 - left, bottom + 1 - top))
21
6
}
22

            
23
6
pub(crate) fn rect_at(app: &App, width: u16, height: u16) -> Option<Rect> {
24
6
    dialog_rect(&draw_frame(app, width, height).0)
25
6
}
26

            
27
/// An app with a form moved and sized away from where it opens.
28
2
pub(crate) fn moved_form() -> (App, Rect) {
29
2
    let mut app = app();
30
2
    app.overlays.push(Modal::Form(Form::commodity_create()));
31
2
    draw_into(&mut app, 100, 30);
32
2
    let auto = app.hits.dialog().expect("the form was drawn").auto;
33
2
    let place = Place {
34
2
        x: 7,
35
2
        y: 4,
36
2
        width: auto.width.saturating_sub(6).max(40),
37
2
        height: auto.height + 3,
38
2
    };
39
2
    app.overlays.set_placement(Placement::of(place, auto));
40
2
    let rect = rect_at(&app, 100, 30).expect("the moved form is drawn");
41
2
    assert_eq!((rect.x, rect.y), (7, 4));
42
2
    (app, rect)
43
2
}
44

            
45
3
pub(crate) fn submit(app: &mut App) {
46
3
    let Some(Modal::Form(form)) = app.overlays.pop() else {
47
        panic!("the form is open");
48
    };
49
3
    app.lock_pending_form(1, form);
50
3
}
51

            
52
#[test]
53
1
fn a_moved_and_sized_form_stays_put_while_pending() {
54
1
    let (mut app, rect) = moved_form();
55
1
    submit(&mut app);
56
1
    assert_eq!(rect_at(&app, 100, 30), Some(rect));
57
1
}
58

            
59
#[test]
60
1
fn the_pending_form_is_clamped_like_the_form_when_the_terminal_shrinks() {
61
1
    let mut app = app();
62
1
    app.overlays.push(Modal::Form(Form::commodity_create()));
63
1
    draw_into(&mut app, 100, 30);
64
1
    let auto = app.hits.dialog().expect("the form was drawn").auto;
65
1
    let far = Place {
66
1
        x: 85,
67
1
        y: 26,
68
1
        ..Place::from(auto)
69
1
    };
70
1
    app.overlays.set_placement(Placement::of(far, auto));
71
2
    let corner = |buffer: &Buffer| {
72
2
        let area = buffer.area;
73
2
        (area.y..area.bottom())
74
2280
            .flat_map(|y| (area.x..area.right()).map(move |x| (x, y)))
75
2280
            .find(|&at| buffer[at].symbol() == "╔")
76
2
    };
77
1
    let open = corner(&draw_frame(&app, 60, 20).0);
78
1
    assert!(open.is_some_and(|at| at != (85, 26)), "the clamp moved it");
79
1
    submit(&mut app);
80
1
    assert_eq!(corner(&draw_frame(&app, 60, 20).0), open);
81
1
}