tui/hits.rs
1//! Where the last frame drew everything a click can land on.
2
3use ratatui::layout::{Position, Rect};
4
5use tvui::{BarPart, Place};
6
7use crate::event::Intent;
8use crate::widgets::grid::CellId;
9use crate::window::WindowId;
10
11/// A surface the wheel and the scrollbar parts act on.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub 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)]
25pub 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
69impl Target {
70 /// The surface the wheel moves when the pointer is on this target.
71 #[must_use]
72 pub fn surface(self) -> Option<Surface> {
73 match self {
74 Target::Row(_) => Some(Surface::List),
75 Target::Option(_) => Some(Surface::Options),
76 Target::Body(surface) | Target::Scroll(surface, _) => Some(surface),
77 _ => None,
78 }
79 }
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)]
85pub 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)]
96pub 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
105impl Hits {
106 pub fn set_dialog(&mut self, slot: Slot) {
107 self.dialog = Some(slot);
108 }
109
110 #[must_use]
111 pub fn dialog(&self) -> Option<Slot> {
112 self.dialog
113 }
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 pub fn extend_shifted(&mut self, local: Hits, shift: u16, host: Rect) {
118 self.shift = shift;
119 let canvas = Place {
120 x: -i32::from(shift),
121 y: 0,
122 width: u16::MAX,
123 height: u16::MAX,
124 };
125 for &(rect, target, _) in &local.areas {
126 let shown = canvas.shown(rect, host);
127 if matches!(target, Target::Caret { .. }) && !shown.is_empty() {
128 self.carets.push((shown, rect));
129 }
130 }
131 self.extend_shown(local, None, canvas, host);
132 }
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 pub fn caret_frame(&self, at: Position) -> Option<(Rect, u16)> {
138 let (_, full) = self
139 .carets
140 .iter()
141 .rev()
142 .find(|(shown, _)| shown.contains(at))?;
143 Some((*full, self.shift))
144 }
145
146 pub fn push(&mut self, rect: Rect, target: Target) {
147 self.push_owned(rect, target, None);
148 }
149
150 fn push_owned(&mut self, rect: Rect, target: Target, window: Option<WindowId>) {
151 if !rect.is_empty() {
152 self.areas.push((rect, target, window));
153 }
154 }
155
156 /// Takes what `owner` drew in its own cells as the screen shows it inside `area`.
157 pub fn extend_shown(&mut self, local: Hits, owner: Option<WindowId>, place: Place, area: Rect) {
158 for (rect, target, _) in local.areas {
159 self.push_owned(place.shown(rect, area), target, owner);
160 }
161 }
162
163 /// The area on top at `at`, what it is and the window that drew it.
164 #[must_use]
165 pub fn hit_at(&self, at: Position) -> Option<(Rect, Target, Option<WindowId>)> {
166 self.areas
167 .iter()
168 .rev()
169 .find(|(rect, ..)| rect.contains(at))
170 .copied()
171 }
172}
173
174#[cfg(test)]
175mod tests;