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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use crossterm::event::{MouseButton, MouseEvent, MouseEventKind};
use ratatui::layout::Rect;
use std::cell::Cell;
use std::time::Instant;
/// Filter badge positions for accurate mouse click detection
/// Uses interior mutability to allow updates during rendering
#[derive(Debug, Default)]
pub struct FilterBadgePositions {
pub symbols: Cell<(usize, usize)>, // (start, end) column positions
pub regex: Cell<(usize, usize)>,
pub language: Cell<(usize, usize)>,
pub kind: Cell<(usize, usize)>,
pub expand: Cell<(usize, usize)>,
pub contains: Cell<(usize, usize)>,
}
/// Mouse interaction state and event handling
#[derive(Debug, Clone)]
pub struct MouseState {
/// Current mouse position (column, row)
pub position: (u16, u16),
/// Whether the mouse is currently hovering over a selectable item
pub hovering: bool,
/// Index of the item being hovered over (if any)
pub hover_index: Option<usize>,
/// Last click position, button, and time
pub last_click: Option<(u16, u16, MouseButton, Instant)>,
}
impl MouseState {
pub fn new() -> Self {
Self {
position: (0, 0),
hovering: false,
hover_index: None,
last_click: None,
}
}
/// Update mouse position from a mouse event
pub fn update_position(&mut self, event: &MouseEvent) {
self.position = (event.column, event.row);
}
/// Check if a position is within a rectangular area
pub fn is_in_area(&self, area: Rect) -> bool {
let (col, row) = self.position;
col >= area.x && col < area.x + area.width && row >= area.y && row < area.y + area.height
}
/// Get the row index relative to an area's top
pub fn row_in_area(&self, area: Rect) -> Option<usize> {
if self.is_in_area(area) {
Some((self.position.1 - area.y) as usize)
} else {
None
}
}
/// Handle a mouse event and return the action to take
/// Supports multiple UI regions: input, filters, and results
pub fn handle_event(
&mut self,
event: MouseEvent,
input_area: Rect,
filters_area: Rect,
result_area: Rect,
badge_positions: &FilterBadgePositions,
) -> MouseAction {
self.update_position(&event);
match event.kind {
MouseEventKind::Down(button) => {
if button != MouseButton::Left {
return MouseAction::None;
}
let now = Instant::now();
let current_pos = (event.column, event.row);
// Check for double-click (within 300ms at same position)
let is_double_click =
if let Some((last_col, last_row, last_button, last_time)) = self.last_click {
last_button == MouseButton::Left
&& last_col == current_pos.0
&& last_row == current_pos.1
&& now.duration_since(last_time).as_millis() < 300
} else {
false
};
// Update last click
self.last_click = Some((event.column, event.row, button, now));
// Check input area (click to focus or click on index status)
if self.is_in_area(input_area) {
// Index status is in the top-right corner of input area
// It appears after significant spacing from the left title
// Check if click is in the right portion of the header (last 30 chars to accommodate symbol status)
let col = event.column.saturating_sub(input_area.x);
let row = event.row.saturating_sub(input_area.y);
// If clicking in the title bar (row 0) and in the right portion
if row == 0 && col > input_area.width.saturating_sub(35) {
// Check if Shift is held for clear+reindex
use crossterm::event::KeyModifiers;
if event.modifiers.contains(KeyModifiers::SHIFT) {
return MouseAction::ClearAndReindex;
} else {
return MouseAction::TriggerIndex;
}
}
// Otherwise, calculate cursor position for input focus (subtract 1 for left border)
let cursor_pos = (event.column.saturating_sub(input_area.x + 1)) as usize;
return MouseAction::FocusInput(cursor_pos);
}
// Check filters area (click to toggle filters)
if self.is_in_area(filters_area) {
let col = event.column.saturating_sub(filters_area.x + 1) as usize;
// Use the actual badge positions calculated during rendering
let symbols_pos = badge_positions.symbols.get();
if col >= symbols_pos.0 && col < symbols_pos.1 {
return MouseAction::ToggleSymbols;
}
let regex_pos = badge_positions.regex.get();
if col >= regex_pos.0 && col < regex_pos.1 {
return MouseAction::ToggleRegex;
}
let lang_pos = badge_positions.language.get();
if col >= lang_pos.0 && col < lang_pos.1 {
return MouseAction::PromptLanguage;
}
let kind_pos = badge_positions.kind.get();
if col >= kind_pos.0 && col < kind_pos.1 {
return MouseAction::PromptKind;
}
let expand_pos = badge_positions.expand.get();
if col >= expand_pos.0 && col < expand_pos.1 {
return MouseAction::ToggleExpand;
}
let contains_pos = badge_positions.contains.get();
if col >= contains_pos.0 && col < contains_pos.1 {
return MouseAction::ToggleContains;
}
return MouseAction::None;
}
// Check results area (click to select)
if self.is_in_area(result_area) {
if let Some(row) = self.row_in_area(result_area) {
// Subtract 1 to account for top border of the List widget
if row > 0 {
let content_row = row - 1;
return if is_double_click {
MouseAction::DoubleClick(content_row)
} else {
MouseAction::SelectResult(content_row)
};
}
}
}
MouseAction::None
}
MouseEventKind::ScrollDown => {
if self.is_in_area(result_area) {
MouseAction::ScrollDown
} else {
MouseAction::None
}
}
MouseEventKind::ScrollUp => {
if self.is_in_area(result_area) {
MouseAction::ScrollUp
} else {
MouseAction::None
}
}
MouseEventKind::Moved => {
if self.is_in_area(result_area) {
if let Some(row) = self.row_in_area(result_area) {
self.hovering = true;
self.hover_index = Some(row);
return MouseAction::Hover(row);
}
}
self.hovering = false;
self.hover_index = None;
MouseAction::None
}
_ => MouseAction::None,
}
}
}
/// Actions triggered by mouse events
#[derive(Debug, Clone, PartialEq)]
pub enum MouseAction {
/// No action
None,
/// Select a result at the given index
SelectResult(usize),
/// Double-click on a result at the given index
DoubleClick(usize),
/// Hover over a result at the given index
Hover(usize),
/// Scroll down
ScrollDown,
/// Scroll up
ScrollUp,
/// Click on input field to focus (cursor position)
FocusInput(usize),
/// Toggle symbols filter
ToggleSymbols,
/// Toggle regex filter
ToggleRegex,
/// Prompt for language filter
PromptLanguage,
/// Prompt for kind filter
PromptKind,
/// Toggle expand filter
ToggleExpand,
/// Toggle contains filter
ToggleContains,
/// Close file preview
ClosePreview,
/// Trigger reindexing (click on index status)
TriggerIndex,
/// Clear cache and trigger full reindex (Shift+click on index status)
ClearAndReindex,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mouse_state_creation() {
let state = MouseState::new();
assert_eq!(state.position, (0, 0));
assert!(!state.hovering);
}
#[test]
fn test_is_in_area() {
let mut state = MouseState::new();
state.position = (10, 5);
let area = Rect::new(5, 3, 20, 10);
assert!(state.is_in_area(area));
state.position = (30, 5);
assert!(!state.is_in_area(area));
}
#[test]
fn test_row_in_area() {
let mut state = MouseState::new();
state.position = (10, 8);
let area = Rect::new(5, 5, 20, 10);
assert_eq!(state.row_in_area(area), Some(3));
state.position = (10, 20);
assert_eq!(state.row_in_area(area), None);
}
}