1
//! Where the last frame drew everything a click can land on.
2

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

            
5
use tvui::{BarPart, Place};
6

            
7
use crate::event::Intent;
8
use crate::widgets::grid::CellId;
9
use crate::window::WindowId;
10

            
11
/// A surface the wheel and the scrollbar parts act on.
12
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13
pub enum Surface {
14
    /// The rows of the active list.
15
    List,
16
    /// A scrolled view that is not a list: the report table, the console scrollback, a viewer.
17
    Pane,
18
    /// The body of a CSV or draft overlay.
19
    Modal,
20
    /// The open drop-down of the focused select.
21
    Options,
22
}
23

            
24
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25
pub enum Target {
26
    /// A surface that takes a click and does nothing with it.
27
    Block,
28
    /// The screen behind an open menu: a click closes it.
29
    MenuBackdrop,
30
    MenuTitle(usize),
31
    MenuItem(usize),
32
    /// A dialog button, by index in the modal's button table.
33
    Button(usize),
34
    Field(usize),
35
    /// The mark of a select: `▐↓▌` of the plain field `field`, or `▼` of its grid cell `cell`.
36
    Mark {
37
        field: usize,
38
        cell: Option<CellId>,
39
    },
40
    /// The text of an input, a grid cell or a text area, where a click places the caret.
41
    Caret {
42
        field: usize,
43
        cell: Option<CellId>,
44
    },
45
    /// A cell of the grid in field `field`, by the widget's own id for it.
46
    Cell {
47
        field: usize,
48
        cell: CellId,
49
    },
50
    /// An option of the open drop-down, by its index among the options shown.
51
    Option(usize),
52
    /// A scrollable surface; a click on it does nothing.
53
    Body(Surface),
54
    Scroll(Surface, BarPart),
55
    /// A list row, by index among the rows the view holds.
56
    Row(usize),
57
    /// A status bar item: the intent it applies.
58
    Hint(Intent),
59
    /// The `[■]` icon of a window frame.
60
    Close,
61
    /// The `[↑]` or `[↕]` icon of a window frame.
62
    Zoom,
63
    /// The top row of a window frame; two quick clicks on it zoom the window.
64
    Title,
65
    /// The `─┘` corner of a window frame, which sizes the window.
66
    Grow,
67
}
68

            
69
impl Target {
70
    /// The surface the wheel moves when the pointer is on this target.
71
    #[must_use]
72
158
    pub fn surface(self) -> Option<Surface> {
73
158
        match self {
74
72
            Target::Row(_) => Some(Surface::List),
75
12
            Target::Option(_) => Some(Surface::Options),
76
54
            Target::Body(surface) | Target::Scroll(surface, _) => Some(surface),
77
20
            _ => None,
78
        }
79
158
    }
80
}
81

            
82
/// The areas of one frame, drawn bottom to top: the last one that holds a point is the one
83
/// on top. An area a window drew carries the window's id.
84
#[derive(Debug, Default, Clone, PartialEq, Eq)]
85
pub struct Hits {
86
    areas: Vec<(Rect, Target, Option<WindowId>)>,
87
    dialog: Option<Slot>,
88
    /// The unclipped rect of each caret area a dialog drew, in canvas cells, by its visible rect.
89
    carets: Vec<(Rect, Rect)>,
90
    /// The columns the dialog's canvas is shifted right of the screen.
91
    shift: u16,
92
}
93

            
94
/// Where the dialog on top opened and what bounds it, as the last frame drew it.
95
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
96
pub struct Slot {
97
    /// The centred rect the dialog opens at.
98
    pub auto: Rect,
99
    /// The least size it is sized to; its own size for a dialog that only moves.
100
    pub min: (u16, u16),
101
    /// The area the dialog sits in.
102
    pub host: Rect,
103
}
104

            
105
impl Hits {
106
669
    pub fn set_dialog(&mut self, slot: Slot) {
107
669
        self.dialog = Some(slot);
108
669
    }
109

            
110
    #[must_use]
111
118
    pub fn dialog(&self) -> Option<Slot> {
112
118
        self.dialog
113
118
    }
114

            
115
    /// Takes what a dialog drew in a canvas that `shift` columns hang left of the screen, as the
116
    /// screen shows it inside `host`.
117
706
    pub fn extend_shifted(&mut self, local: Hits, shift: u16, host: Rect) {
118
706
        self.shift = shift;
119
706
        let canvas = Place {
120
706
            x: -i32::from(shift),
121
706
            y: 0,
122
706
            width: u16::MAX,
123
706
            height: u16::MAX,
124
706
        };
125
706
        for &(rect, target, _) in &local.areas {
126
9549
            let shown = canvas.shown(rect, host);
127
9549
            if matches!(target, Target::Caret { .. }) && !shown.is_empty() {
128
1230
                self.carets.push((shown, rect));
129
8319
            }
130
        }
131
706
        self.extend_shown(local, None, canvas, host);
132
706
    }
133

            
134
    /// The caret area shown at `at`, as the dialog drew it unclipped, and the columns its canvas
135
    /// is shifted: the point to place a caret by is `at` moved right by them.
136
    #[must_use]
137
97
    pub fn caret_frame(&self, at: Position) -> Option<(Rect, u16)> {
138
97
        let (_, full) = self
139
97
            .carets
140
97
            .iter()
141
97
            .rev()
142
300
            .find(|(shown, _)| shown.contains(at))?;
143
24
        Some((*full, self.shift))
144
97
    }
145

            
146
33593
    pub fn push(&mut self, rect: Rect, target: Target) {
147
33593
        self.push_owned(rect, target, None);
148
33593
    }
149

            
150
54338
    fn push_owned(&mut self, rect: Rect, target: Target, window: Option<WindowId>) {
151
54338
        if !rect.is_empty() {
152
54088
            self.areas.push((rect, target, window));
153
250
        }
154
54338
    }
155

            
156
    /// Takes what `owner` drew in its own cells as the screen shows it inside `area`.
157
2220
    pub fn extend_shown(&mut self, local: Hits, owner: Option<WindowId>, place: Place, area: Rect) {
158
2220
        for (rect, target, _) in local.areas {
159
20745
            self.push_owned(place.shown(rect, area), target, owner);
160
        }
161
2220
    }
162

            
163
    /// The area on top at `at`, what it is and the window that drew it.
164
    #[must_use]
165
856
    pub fn hit_at(&self, at: Position) -> Option<(Rect, Target, Option<WindowId>)> {
166
856
        self.areas
167
856
            .iter()
168
856
            .rev()
169
11903
            .find(|(rect, ..)| rect.contains(at))
170
            .copied()
171
856
    }
172
}
173

            
174
#[cfg(test)]
175
mod tests;