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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
use anyhow::Result;
use crossterm::event::{MouseButton, MouseEvent, MouseEventKind};
use std::time::Instant;
use crate::app::{App, FormatMode, ScrollbarType};
pub fn handle_mouse_event<B: ratatui::backend::Backend>(
app: &mut App,
mouse: MouseEvent,
terminal: &mut ratatui::Terminal<B>,
) -> Result<()> {
// Handle overlay mouse events
if app.editing_entry {
handle_overlay_mouse(app, mouse);
return Ok(());
}
match mouse.kind {
MouseEventKind::ScrollLeft => {
// Horizontal scroll left
if app.format_mode == FormatMode::View {
app.relf_hscroll_by(-8);
} else if app.format_mode == FormatMode::Edit {
app.relf_hscroll_by(-8);
}
}
MouseEventKind::ScrollRight => {
// Horizontal scroll right
if app.format_mode == FormatMode::View {
app.relf_hscroll_by(8);
} else if app.format_mode == FormatMode::Edit {
app.relf_hscroll_by(8);
}
}
MouseEventKind::ScrollUp => {
// Don't scroll vertically if horizontal scrollbar is being dragged
if app.dragging_scrollbar != Some(ScrollbarType::Horizontal) {
// If explorer has focus, scroll explorer
if app.explorer_open && app.explorer_has_focus {
app.explorer_move_up();
} else if app.format_mode == FormatMode::Edit {
// Scroll and move cursor together
for _ in 0..5 {
if app.content_cursor_line > 0 {
app.move_cursor_up();
} else {
app.scroll_up();
}
}
} else if !app.relf_entries.is_empty() {
// Card view: move selection up
if app.selected_entry_index > 0 {
app.selected_entry_index -= 1;
// Reset vertical scroll when changing cards (hscroll is misused as vscroll for cards)
app.hscroll = 0;
}
} else {
// Relf: clamp to content bounds
let dec = 5u16;
app.scroll = app.scroll.saturating_sub(dec);
}
}
}
MouseEventKind::ScrollDown => {
// Don't scroll vertically if horizontal scrollbar is being dragged
if app.dragging_scrollbar != Some(ScrollbarType::Horizontal) {
// If explorer has focus, scroll explorer
if app.explorer_open && app.explorer_has_focus {
app.explorer_move_down();
} else if app.format_mode == FormatMode::Edit {
// Scroll and move cursor together
for _ in 0..5 {
app.move_cursor_down();
}
} else if !app.relf_entries.is_empty() {
// Card view: move selection down
if app.selected_entry_index + 1 < app.relf_entries.len() {
app.selected_entry_index += 1;
// Reset vertical scroll when changing cards (hscroll is misused as vscroll for cards)
app.hscroll = 0;
}
} else {
// Relf: clamp to last content page
let inc = 5u16;
let max_off = app.relf_content_max_scroll();
let new_val = app.scroll.saturating_add(inc);
app.scroll = std::cmp::min(new_val, max_off);
}
}
}
MouseEventKind::Down(MouseButton::Left) => {
handle_left_mouse_down(app, mouse, terminal)?;
}
MouseEventKind::Up(MouseButton::Left) => {
// Disable in Edit mode
if app.format_mode == FormatMode::Edit {
return Ok(());
}
// Release scrollbar drag
app.dragging_scrollbar = None;
}
MouseEventKind::Drag(MouseButton::Left) => {
handle_left_mouse_drag(app, mouse, terminal)?;
}
_ => {}
}
Ok(())
}
fn handle_overlay_mouse(app: &mut App, mouse: MouseEvent) {
match mouse.kind {
MouseEventKind::Down(MouseButton::Left) if mouse.modifiers.is_empty() => {
// Check for double-click (clicks within 500ms)
let now = Instant::now();
let is_double_click = if let Some(last_time) = app.last_click_time {
now.duration_since(last_time).as_millis() < 500
} else {
false
};
if is_double_click {
// Check if Exit field is selected
if app.edit_field_index < app.edit_buffer.len() {
let field = &app.edit_buffer[app.edit_field_index];
if field == "Exit" {
// Close overlay without saving
app.cancel_editing_entry();
app.last_click_time = None;
return;
}
}
// Double-click: enter insert mode for currently selected field
if !app.edit_insert_mode {
app.edit_field_editing_mode = true;
app.edit_insert_mode = true;
app.edit_skip_normal_mode = true; // Mark that we skipped normal mode
let field = &app.edit_buffer[app.edit_field_index];
// Clear placeholder text when entering insert mode
if app.edit_field_index < app.edit_buffer_is_placeholder.len()
&& app.edit_buffer_is_placeholder[app.edit_field_index] {
app.edit_buffer[app.edit_field_index] = String::new();
app.edit_buffer_is_placeholder[app.edit_field_index] = false;
app.edit_cursor_pos = 0;
} else {
// Move cursor to end of text
app.edit_cursor_pos = field.chars().count();
}
}
app.last_click_time = None; // Reset after double-click
} else {
// First click: just record the time
app.last_click_time = Some(now);
}
}
// Allow scrolling in overlay (only in field selection mode)
MouseEventKind::ScrollUp => {
// Block if in field editing mode (normal/insert)
if app.edit_field_editing_mode {
return;
}
if app.edit_field_index > 0 {
app.edit_field_index -= 1;
app.edit_cursor_pos = 0;
app.edit_hscroll = 0;
app.edit_vscroll = 0;
}
}
MouseEventKind::ScrollDown => {
// Block if in field editing mode (normal/insert)
if app.edit_field_editing_mode {
return;
}
if app.edit_field_index + 1 < app.edit_buffer.len() {
app.edit_field_index += 1;
app.edit_cursor_pos = 0;
app.edit_hscroll = 0;
app.edit_vscroll = 0;
}
}
_ => {
// Other mouse events in overlay, ignore
}
}
}
fn handle_left_mouse_down<B: ratatui::backend::Backend>(
app: &mut App,
mouse: MouseEvent,
terminal: &mut ratatui::Terminal<B>,
) -> Result<()> {
// Disable scrollbar dragging in Edit mode
if app.format_mode == FormatMode::Edit {
return Ok(());
}
// Handle mouse click on scrollbars
let click_x = mouse.column;
let click_y = mouse.row;
// Check if click is on vertical scrollbar
let terminal_width = terminal.size().map(|s| s.width).unwrap_or(80);
let terminal_height = terminal.size().map(|s| s.height).unwrap_or(24);
// Check horizontal scrollbar first
let on_hscrollbar = click_y >= terminal_height.saturating_sub(2)
&& click_x > 0
&& click_x < terminal_width - 1;
let on_vscrollbar = click_x == terminal_width - 1
&& click_y > 0
&& click_y < terminal_height - 1;
if on_hscrollbar && app.format_mode == FormatMode::Edit {
// Horizontal scrollbar clicked (Edit mode only)
app.dragging_scrollbar = Some(ScrollbarType::Horizontal);
let max_hscroll = app.relf_max_hscroll();
if max_hscroll > 0 {
let scrollbar_width = (terminal_width - 2) as f32;
let click_ratio = (click_x - 1) as f32 / scrollbar_width;
let new_hscroll = (max_hscroll as f32 * click_ratio) as u16;
app.hscroll = new_hscroll.min(max_hscroll);
}
} else if on_vscrollbar {
// Vertical scrollbar clicked
app.dragging_scrollbar = Some(ScrollbarType::Vertical);
let scrollbar_height = (terminal_height - 2) as f32;
let click_ratio = (click_y - 1) as f32 / scrollbar_height;
let new_scroll = (app.max_scroll as f32 * click_ratio) as u16;
app.scroll = new_scroll.min(app.max_scroll);
} else {
// Not on any scrollbar - check for double-click
// Check for double-click (clicks within 500ms)
let now = Instant::now();
let is_double_click = if let Some(last_time) = app.last_click_time {
now.duration_since(last_time).as_millis() < 500
} else {
false
};
if is_double_click {
// If explorer has focus, open file and move to file window
if app.explorer_open && app.explorer_has_focus {
app.explorer_select_entry();
} else if app.format_mode == FormatMode::View && !app.relf_entries.is_empty() {
// Double-click: open the overlay for the currently selected entry
app.open_entry_overlay();
}
app.last_click_time = None; // Reset after double-click
} else {
// First click: just record the time
app.last_click_time = Some(now);
}
app.dragging_scrollbar = None;
}
Ok(())
}
fn handle_left_mouse_drag<B: ratatui::backend::Backend>(
app: &mut App,
mouse: MouseEvent,
terminal: &mut ratatui::Terminal<B>,
) -> Result<()> {
// Disable in Edit mode
if app.format_mode == FormatMode::Edit {
return Ok(());
}
// Only handle drag if we're already dragging a scrollbar
match app.dragging_scrollbar {
Some(ScrollbarType::Vertical) => {
// Continue vertical scrollbar drag
let click_y = mouse.row;
let terminal_height =
terminal.size().map(|s| s.height).unwrap_or(24);
if click_y > 0 && click_y < terminal_height - 1 {
let scrollbar_height = (terminal_height - 2) as f32;
let click_ratio = (click_y - 1) as f32 / scrollbar_height;
let new_scroll =
(app.max_scroll as f32 * click_ratio) as u16;
app.scroll = new_scroll.min(app.max_scroll);
}
}
Some(ScrollbarType::Horizontal) => {
// Continue horizontal scrollbar drag (Edit mode only)
if app.format_mode == FormatMode::Edit {
let click_x = mouse.column;
let terminal_width =
terminal.size().map(|s| s.width).unwrap_or(80);
if click_x > 0 && click_x < terminal_width - 1 {
let max_hscroll = app.relf_max_hscroll();
if max_hscroll > 0 {
let scrollbar_width = (terminal_width - 2) as f32;
let click_ratio =
(click_x - 1) as f32 / scrollbar_width;
let new_hscroll =
(max_hscroll as f32 * click_ratio) as u16;
app.hscroll = new_hscroll.min(max_hscroll);
}
}
}
}
None => {
// Not dragging any scrollbar, ignore
}
}
Ok(())
}