1
//! Real dialogs draw the Turbo Vision buttons: a shadow, a pressed look, their own placement.
2

            
3
use crate::app::{App, Face, Owner};
4
use crate::form::Form;
5
use crate::hits::{Hits, Target};
6
use crate::modal::{ConfirmAction, Modal};
7
use crate::test_support::draw_frame;
8
use ratatui::buffer::Buffer;
9
use ratatui::layout::Rect;
10
use tvui::Theme;
11

            
12
const PRESETS: [&str; 2] = ["dn", "turbo"];
13

            
14
5
fn confirm(preset: &str) -> App {
15
5
    let mut app = crate::test_support::app();
16
5
    app.theme = Theme::preset(preset).expect("the preset parses");
17
5
    app.overlays.push(Modal::Confirm {
18
5
        prompt: "Discard changes?".to_string(),
19
5
        action: ConfirmAction::DiscardForm,
20
5
        focus: None,
21
5
    });
22
5
    app
23
5
}
24

            
25
4
fn form(preset: &str) -> App {
26
4
    let mut app = crate::test_support::app();
27
4
    app.theme = Theme::preset(preset).expect("the preset parses");
28
4
    app.overlays.push(Modal::Form(Form::config_set()));
29
4
    app
30
4
}
31

            
32
12
fn rects(hits: &Hits) -> Vec<Rect> {
33
12
    hits.entries()
34
12
        .iter()
35
228
        .filter(|(_, target)| matches!(target, Target::Button(_)))
36
24
        .map(|(rect, _)| *rect)
37
        .collect()
38
12
}
39

            
40
8
fn row(buffer: &Buffer, y: u16) -> String {
41
8
    (0..buffer.area.width)
42
640
        .map(|x| buffer[(x, y)].symbol())
43
        .collect()
44
8
}
45

            
46
#[test]
47
1
fn a_confirm_and_a_form_draw_two_row_buttons_with_a_shadow_in_both_presets() {
48
1
    for preset in PRESETS {
49
2
        for app in [confirm(preset), form(preset)] {
50
4
            let (buffer, hits) = draw_frame(&app, 80, 24);
51
4
            let buttons = rects(&hits);
52
4
            assert_eq!(buttons.len(), 2, "{preset}");
53
4
            for rect in buttons {
54
8
                assert_eq!(rect.height, 1, "{preset}");
55
8
                assert_eq!(buffer[(rect.right(), rect.y)].symbol(), "\u{2584}");
56
8
                let lower = row(&buffer, rect.y + 1);
57
8
                let under: String = lower
58
8
                    .chars()
59
8
                    .skip(usize::from(rect.x) + 1)
60
8
                    .take(8)
61
8
                    .collect();
62
8
                assert_eq!(under, "\u{2580}".repeat(8), "{preset}");
63
            }
64
        }
65
    }
66
1
}
67

            
68
#[test]
69
1
fn a_message_centres_its_buttons_and_a_form_puts_them_at_the_bottom_right() {
70
1
    let (_, hits) = draw_frame(&confirm("dn"), 80, 24);
71
1
    let buttons = rects(&hits);
72
1
    let dialog = dialog_inner(&draw_frame(&confirm("dn"), 80, 24).0);
73
1
    let left = buttons[0].x - 1 - dialog.x;
74
1
    let right = dialog.right() - buttons[1].right() - 1;
75
1
    assert!(left.abs_diff(right) <= 1, "{left} {right}");
76

            
77
1
    let (buffer, hits) = draw_frame(&form("dn"), 80, 24);
78
1
    let buttons = rects(&hits);
79
1
    let dialog = dialog_inner(&buffer);
80
1
    assert_eq!(buttons[1].right() + 2, dialog.right());
81
1
    assert_eq!(buttons[1].x - 1 - (buttons[0].right() + 1), 2);
82
1
}
83

            
84
/// The inside of the dialog: between the first `╔` and the last `╝`.
85
2
fn dialog_inner(buffer: &Buffer) -> Rect {
86
4
    let corners = |symbol: &str| -> Vec<_> {
87
4
        let all = buffer.area.positions();
88
7680
        all.filter(|p| buffer[*p].symbol() == symbol).collect()
89
4
    };
90
2
    let (first, last) = (corners("╔")[0], *corners("╝").last().expect("a corner"));
91
2
    Rect::new(
92
2
        first.x + 1,
93
2
        first.y + 1,
94
2
        last.x - first.x - 1,
95
2
        last.y - first.y - 1,
96
2
    )
97
2
}
98

            
99
#[test]
100
1
fn a_held_button_is_drawn_down_in_a_confirm_and_a_form() {
101
1
    for mut app in [confirm("dn"), form("dn")] {
102
2
        let (_, hits) = draw_frame(&app, 80, 24);
103
2
        let held = rects(&hits)[0];
104
2
        app.hold_press(Owner::Layer, Face::Button(0), held);
105
2
        let (buffer, hits) = draw_frame(&app, 80, 24);
106
2
        assert_eq!(rects(&hits)[0], held);
107
2
        assert_eq!(buffer[(held.right(), held.y)].symbol(), " ");
108
2
        assert_eq!(buffer[(held.x + 2, held.y + 1)].symbol(), " ");
109
2
        let other = rects(&hits)[1];
110
2
        assert_eq!(buffer[(other.right(), other.y)].symbol(), "\u{2584}");
111
    }
112
1
}