1
//! What the mouse or the size/move mode holds: a drag, or a press waiting for its release.
2

            
3
use ratatui::layout::{Position, Rect};
4

            
5
use super::App;
6
use super::button_press::KeyPress;
7
use super::drag::Drag;
8
use crate::window::WindowId;
9

            
10
/// What holds: a desktop window, or the dialog on top.
11
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12
pub(crate) enum Owner {
13
    Window(WindowId),
14
    Layer,
15
}
16

            
17
/// The face of a press: a dialog button by its index, or the `[■]` icon of a frame.
18
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19
pub(crate) enum Face {
20
    Button(usize),
21
    Close,
22
}
23

            
24
/// A press that acts if the pointer is still on its `area` when released.
25
#[derive(Debug, Clone, Copy)]
26
pub(crate) struct Press {
27
    face: Face,
28
    area: Rect,
29
    inside: bool,
30
}
31

            
32
#[derive(Debug, Clone, Copy)]
33
pub(crate) enum Grip {
34
    Drag(Drag),
35
    Press(Press),
36
    Key(KeyPress),
37
}
38

            
39
#[derive(Debug, Clone, Copy)]
40
pub(crate) struct Hold {
41
    pub(super) owner: Owner,
42
    pub(super) grip: Grip,
43
    /// The overlay stack the hold began on.
44
    epoch: u64,
45
}
46

            
47
impl Hold {
48
    /// A press outlives no push or pop of the overlays, nor does a drag of a layer; a window
49
    /// dragged from the keyboard stays held under whatever opens above it.
50
15082
    fn survives(self, epoch: u64) -> bool {
51
15082
        let window_drag = matches!((self.owner, self.grip), (Owner::Window(_), Grip::Drag(_)));
52
15082
        window_drag || self.epoch == epoch
53
15082
    }
54
}
55

            
56
impl App {
57
    /// The hold, if what it began on is still there.
58
158495
    pub(super) fn live_hold(&self) -> Option<Hold> {
59
158495
        self.hold
60
15082
            .filter(|hold| hold.survives(self.overlays.epoch()))
61
158495
    }
62

            
63
225
    pub(super) fn begin_hold(&mut self, owner: Owner, grip: Grip) {
64
225
        let epoch = self.overlays.epoch();
65
225
        self.hold = Some(Hold { owner, grip, epoch });
66
225
    }
67

            
68
32
    pub(crate) fn hold_press(&mut self, owner: Owner, face: Face, area: Rect) {
69
32
        let press = Press {
70
32
            face,
71
32
            area,
72
32
            inside: true,
73
32
        };
74
32
        self.begin_hold(owner, Grip::Press(press));
75
32
    }
76

            
77
    /// Follows the pointer, now at `position`, with a press held.
78
8
    pub(crate) fn track_press(&mut self, position: Position) {
79
8
        if let Some(Hold {
80
8
            grip: Grip::Press(press),
81
8
            ..
82
8
        }) = &mut self.hold
83
        {
84
2
            press.inside = press.area.contains(position);
85
6
        }
86
8
    }
87

            
88
    /// Ends a press; the face it pressed when the release is on it.
89
47
    pub(crate) fn release_press(&mut self, position: Position) -> Option<Face> {
90
47
        let live = self.live_hold();
91
47
        let held = self
92
47
            .hold
93
36
            .take_if(|hold| matches!(hold.grip, Grip::Press(_)))?;
94
25
        match (live, held.grip) {
95
21
            (Some(_), Grip::Press(press)) if press.area.contains(position) => Some(press.face),
96
7
            _ => None,
97
        }
98
47
    }
99

            
100
    /// Drops a press held: a key was typed.
101
3369
    pub(crate) fn cancel_press(&mut self) {
102
3369
        self.hold
103
82
            .take_if(|hold| matches!(hold.grip, Grip::Press(_)));
104
3369
    }
105

            
106
    /// The button drawn down: the one a mouse press holds under the pointer, or a key presses.
107
739
    pub(crate) fn pressed_button(&self) -> Option<usize> {
108
739
        let held = self.live_hold().and_then(|hold| match hold.grip {
109
            Grip::Press(Press {
110
                face: Face::Button(index),
111
                inside: true,
112
                ..
113
5
            }) => Some(index),
114
144
            _ => None,
115
149
        });
116
739
        held.or_else(|| self.key_pressed_button())
117
739
    }
118
}