1
//! The mouse holds one thing at a time: a drag or a press, and a change of overlays drops it.
2

            
3
use super::overlap::{FRONT_AT, left_click, overlapped};
4
use crate::app::{App, Face, Owner};
5
use crate::modal::{ConfirmAction, Modal};
6
use ratatui::layout::{Position, Rect};
7
use std::time::Instant;
8

            
9
const PRESSED: Rect = Rect::new(2, 2, 6, 1);
10

            
11
5
fn inside() -> Position {
12
5
    Position::new(3, 2)
13
5
}
14

            
15
4
fn confirm() -> Modal {
16
4
    Modal::Confirm {
17
4
        prompt: "Discard changes?".to_string(),
18
4
        action: ConfirmAction::DiscardForm,
19
4
        focus: None,
20
4
    }
21
4
}
22

            
23
4
fn press_button(app: &mut App) {
24
4
    app.hold_press(Owner::Layer, Face::Button(0), PRESSED);
25
4
}
26

            
27
#[test]
28
1
fn a_press_ends_the_drag_that_was_held() {
29
1
    let (mut app, _, _) = overlapped();
30
1
    let title = (
31
1
        u16::try_from(FRONT_AT.x).unwrap_or_default() + 10,
32
1
        u16::try_from(FRONT_AT.y).unwrap_or_default(),
33
1
    );
34
1
    left_click(&mut app, title, Instant::now());
35
1
    assert!(app.pointer_dragging());
36
1
    press_button(&mut app);
37
1
    assert!(!app.dragging());
38
1
    assert_eq!(app.release_press(inside()), Some(Face::Button(0)));
39
1
}
40

            
41
#[test]
42
1
fn a_drag_ends_the_press_that_was_held() {
43
1
    let (mut app, _, _) = overlapped();
44
1
    press_button(&mut app);
45
1
    let title = (
46
1
        u16::try_from(FRONT_AT.x).unwrap_or_default() + 10,
47
1
        u16::try_from(FRONT_AT.y).unwrap_or_default(),
48
1
    );
49
1
    left_click(&mut app, title, Instant::now());
50
1
    assert!(app.dragging());
51
1
    assert_eq!(app.release_press(inside()), None);
52
1
}
53

            
54
#[test]
55
1
fn a_close_press_is_a_press_like_a_button() {
56
1
    let mut app = crate::test_support::app();
57
1
    app.hold_press(Owner::Layer, Face::Close, PRESSED);
58
1
    assert_eq!(app.pressed_button(), None);
59
1
    assert_eq!(app.release_press(inside()), Some(Face::Close));
60
1
}
61

            
62
#[test]
63
1
fn pushing_an_overlay_drops_the_press() {
64
1
    let mut app = crate::test_support::app();
65
1
    app.overlays.push(confirm());
66
1
    press_button(&mut app);
67
1
    assert_eq!(app.pressed_button(), Some(0));
68
1
    app.overlays.push(confirm());
69
1
    assert_eq!(app.pressed_button(), None);
70
1
    assert_eq!(app.release_press(inside()), None);
71
1
}
72

            
73
#[test]
74
1
fn popping_an_overlay_drops_the_press() {
75
1
    let mut app = crate::test_support::app();
76
1
    app.overlays.push(confirm());
77
1
    app.overlays.push(confirm());
78
1
    press_button(&mut app);
79
1
    app.overlays.pop();
80
1
    assert_eq!(app.pressed_button(), None);
81
1
    assert_eq!(app.release_press(inside()), None);
82
1
}