1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use ratatui::layout::{Position, Rect};
/// Generic utility for tracking clickable screen regions.
///
/// Stores `(Rect, T)` pairs during `render()`, then hit-tests them in `handle_event()`.
/// Replaces the 4-line bounds-check pattern that was copy-pasted across screens.
#[derive(Debug, Clone)]
pub struct MouseRegions<T> {
regions: Vec<(Rect, T)>,
}
impl<T> MouseRegions<T> {
#[must_use]
pub fn new() -> Self {
Self {
regions: Vec::new(),
}
}
/// Clear all stored regions (call at the start of each render).
pub fn clear(&mut self) {
self.regions.clear();
}
/// Register a clickable region with its associated value.
pub fn add(&mut self, area: Rect, item: T) {
self.regions.push((area, item));
}
/// Hit-test: find the first region containing the given column and row.
#[must_use]
pub fn hit_test(&self, col: u16, row: u16) -> Option<&T> {
let pos = Position::new(col, row);
self.regions
.iter()
.find(|(rect, _)| rect.contains(pos))
.map(|(_, item)| item)
}
}
impl<T> Default for MouseRegions<T> {
fn default() -> Self {
Self::new()
}
}