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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! Mouse event helpers.
//!
//! ## Enabling mouse support
//!
//! In your terminal setup / teardown, enable and disable mouse capture:
//!
//! ```rust
//! use crossterm::{execute, event::{EnableMouseCapture, DisableMouseCapture}};
//!
//! // setup
//! execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
//!
//! // teardown
//! execute!(terminal.backend_mut(), LeaveAlternateScreen, DisableMouseCapture)?;
//! ```
//!
//! ## Hit-testing in the event loop
//!
//! ```rust
//! use crossterm::event::{Event, MouseButton, MouseEventKind};
//! use tui_kit::mouse_hit;
//!
//! if let Event::Mouse(m) = event::read()? {
//! if m.kind == MouseEventKind::Down(MouseButton::Left) {
//! app.handle_mouse(m.column, m.row);
//! }
//! }
//! ```
//!
//! ## Storing panel areas
//!
//! Record each focusable widget's `Rect` during the render pass, then use
//! [`mouse_hit`] to map a click coordinate back to the focused widget:
//!
//! ```rust
//! // In render:
//! app.areas[Panel::List as usize] = Some(list_area);
//!
//! // In handle_mouse:
//! for (i, area) in app.areas.iter().enumerate() {
//! if let Some(a) = area {
//! if mouse_hit(*a, col, row) {
//! app.focus = Panel::from(i);
//! return;
//! }
//! }
//! }
//! ```
use ;
/// Returns `true` if the terminal cell `(col, row)` falls inside `area`.
///
/// Use this to map a [`crossterm::event::MouseEvent`] to a widget that was
/// rendered at a known [`Rect`].
/// Given the **outer** block area (including borders) of a list widget and a
/// click `row`, returns the item index that was clicked.
///
/// Returns `None` if the click lands on a border row or is outside the area.
/// The returned index accounts for the current scroll `offset` — it is an
/// absolute index into the items slice, ready to assign to `ListState::selected`.
///
/// # Example
/// ```rust
/// if let Some(idx) = list_item_at(app.panel_areas[0], app.list_state.offset(), m.row) {
/// if idx < app.items.len() {
/// app.list_state.selected = idx;
/// }
/// }
/// ```
/// Given the **outer** block area of a scrollable paragraph and a click `row`,
/// returns the absolute content line number that was clicked (i.e. accounting
/// for the scroll offset).
///
/// Returns `None` if the click is on a border row.
/// The caller can then match this line number against their tracked link/item
/// line positions to find which element was clicked.
///
/// # Example
/// ```rust
/// if let Some(line) = paragraph_line_at(area, app.desc_scroll as usize, m.row) {
/// if let Some(i) = app.desc_link_lines.iter().position(|&l| l as usize == line) {
/// app.selected_desc_link = i;
/// }
/// }
/// ```