|
| 1 | +//! Arrow-key debounce: distinguishes mouse-wheel bursts from keyboard presses. |
| 2 | +//! |
| 3 | +//! xterm alternate scroll (`\x1b[?1007h`) translates mouse wheel into Up/Down |
| 4 | +//! arrow keys. This module uses timing to tell them apart: |
| 5 | +//! - Rapid-fire (< 30 ms gap) → mouse wheel → content scroll |
| 6 | +//! - Isolated (> 30 ms) → keyboard → history navigation |
| 7 | +//! |
| 8 | +//! State: `Idle → Pending (30 ms) → Scrolling (150 ms idle → Idle)` |
| 9 | +//! Second arrow within window → burst → Scrolling. |
| 10 | +//! Other key or timer expiry → flush Pending as history. |
| 11 | +
|
| 12 | +use std::time::{Duration, Instant}; |
| 13 | + |
| 14 | +use crossterm::event::KeyCode; |
| 15 | + |
| 16 | +use super::InputAction; |
| 17 | +use super::multiline; |
| 18 | +use super::navigation::{DEFAULT_WRAP_WIDTH, handle_down, handle_up}; |
| 19 | +use crate::app::App; |
| 20 | + |
| 21 | +/// Window within which a second arrow event is considered a mouse-wheel burst. |
| 22 | +const BURST_DETECT_MS: u64 = 30; |
| 23 | + |
| 24 | +/// After this idle period the scroll burst ends and state returns to Idle. |
| 25 | +const SCROLL_IDLE_MS: u64 = 150; |
| 26 | + |
| 27 | +/// Scroll direction derived from arrow key code. |
| 28 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 29 | +pub(crate) enum ScrollDirection { |
| 30 | + Up, |
| 31 | + Down, |
| 32 | +} |
| 33 | + |
| 34 | +impl ScrollDirection { |
| 35 | + pub(crate) fn from_key(code: KeyCode) -> Option<Self> { |
| 36 | + match code { |
| 37 | + KeyCode::Up => Some(Self::Up), |
| 38 | + KeyCode::Down => Some(Self::Down), |
| 39 | + _ => None, |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/// Arrow-key debounce state. |
| 45 | +#[derive(Debug, Default)] |
| 46 | +pub(crate) enum ArrowDebounce { |
| 47 | + /// No pending arrow event. |
| 48 | + #[default] |
| 49 | + Idle, |
| 50 | + /// First arrow received; waiting to see if a burst follows. |
| 51 | + Pending { |
| 52 | + direction: ScrollDirection, |
| 53 | + time: Instant, |
| 54 | + }, |
| 55 | + /// Mouse-wheel burst confirmed; subsequent arrows scroll content. |
| 56 | + Scrolling { last_time: Instant }, |
| 57 | +} |
| 58 | + |
| 59 | +/// Called by `handle_input_mode_key` when Up or Down is pressed. |
| 60 | +/// |
| 61 | +/// Multiline cursor navigation bypasses debounce entirely (immediate). |
| 62 | +/// Otherwise returns `StartArrowDebounce` when the first arrow is deferred, |
| 63 | +/// or `None` when handled inline (scroll / burst continuation). |
| 64 | +pub(super) fn handle_arrow_with_debounce(app: &mut App, direction: ScrollDirection) -> InputAction { |
| 65 | + // Multiline cursor navigation is always immediate — never debounced. |
| 66 | + // This keeps multiline editing responsive and avoids burst misfires |
| 67 | + // from fast keyboard repeat in multi-line input fields. |
| 68 | + if try_multiline_nav(app, direction) { |
| 69 | + app.arrow_debounce = ArrowDebounce::Idle; |
| 70 | + return InputAction::None; |
| 71 | + } |
| 72 | + |
| 73 | + match app.arrow_debounce { |
| 74 | + ArrowDebounce::Idle => { |
| 75 | + app.arrow_debounce = ArrowDebounce::Pending { |
| 76 | + direction, |
| 77 | + time: Instant::now(), |
| 78 | + }; |
| 79 | + InputAction::StartArrowDebounce |
| 80 | + } |
| 81 | + ArrowDebounce::Pending { |
| 82 | + direction: old_dir, |
| 83 | + time, |
| 84 | + } => { |
| 85 | + if time.elapsed() < burst_detect_duration() { |
| 86 | + // Second event within burst window → mouse-wheel burst → scroll. |
| 87 | + app.arrow_debounce = ArrowDebounce::Scrolling { |
| 88 | + last_time: Instant::now(), |
| 89 | + }; |
| 90 | + apply_scroll(app, old_dir); |
| 91 | + apply_scroll(app, direction); |
| 92 | + InputAction::None |
| 93 | + } else { |
| 94 | + // Timer was delayed. Flush stale pending as history, then |
| 95 | + // start a new debounce for this event. |
| 96 | + process_as_history(app, old_dir); |
| 97 | + app.arrow_debounce = ArrowDebounce::Pending { |
| 98 | + direction, |
| 99 | + time: Instant::now(), |
| 100 | + }; |
| 101 | + InputAction::StartArrowDebounce |
| 102 | + } |
| 103 | + } |
| 104 | + ArrowDebounce::Scrolling { last_time } => { |
| 105 | + if last_time.elapsed() > Duration::from_millis(SCROLL_IDLE_MS) { |
| 106 | + // Tick was dropped or delayed. Treat as fresh Idle state. |
| 107 | + app.arrow_debounce = ArrowDebounce::Pending { |
| 108 | + direction, |
| 109 | + time: Instant::now(), |
| 110 | + }; |
| 111 | + InputAction::StartArrowDebounce |
| 112 | + } else { |
| 113 | + app.arrow_debounce = ArrowDebounce::Scrolling { |
| 114 | + last_time: Instant::now(), |
| 115 | + }; |
| 116 | + apply_scroll(app, direction); |
| 117 | + InputAction::None |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/// Discard pending debounce without processing as history. |
| 124 | +/// |
| 125 | +/// Used by modal/global/autocomplete handlers that supersede the pending |
| 126 | +/// arrow event. The stale 30 ms timer will see `Idle` and become a no-op. |
| 127 | +pub(crate) fn discard_pending(app: &mut App) { |
| 128 | + app.arrow_debounce = ArrowDebounce::Idle; |
| 129 | +} |
| 130 | + |
| 131 | +/// Flush any pending arrow as a history navigation action. |
| 132 | +/// |
| 133 | +/// Called when a non-arrow key arrives or when the debounce timer expires. |
| 134 | +pub(crate) fn resolve_pending_arrow(app: &mut App) { |
| 135 | + match std::mem::replace(&mut app.arrow_debounce, ArrowDebounce::Idle) { |
| 136 | + ArrowDebounce::Pending { direction, .. } => { |
| 137 | + process_as_history(app, direction); |
| 138 | + } |
| 139 | + ArrowDebounce::Scrolling { .. } | ArrowDebounce::Idle => {} |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/// Expire stale Scrolling state (called from Tick handler). |
| 144 | +pub(crate) fn tick_debounce(app: &mut App) { |
| 145 | + if let ArrowDebounce::Scrolling { last_time } = app.arrow_debounce |
| 146 | + && last_time.elapsed() > Duration::from_millis(SCROLL_IDLE_MS) |
| 147 | + { |
| 148 | + app.arrow_debounce = ArrowDebounce::Idle; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/// Burst detection window. |
| 153 | +pub(crate) fn burst_detect_duration() -> Duration { |
| 154 | + Duration::from_millis(BURST_DETECT_MS) |
| 155 | +} |
| 156 | + |
| 157 | +fn try_multiline_nav(app: &mut App, direction: ScrollDirection) -> bool { |
| 158 | + if !multiline::is_multiline(&app.input, DEFAULT_WRAP_WIDTH) { |
| 159 | + return false; |
| 160 | + } |
| 161 | + let new_cursor = match direction { |
| 162 | + ScrollDirection::Up => { |
| 163 | + multiline::cursor_up(&app.input, app.input_cursor, DEFAULT_WRAP_WIDTH) |
| 164 | + } |
| 165 | + ScrollDirection::Down => { |
| 166 | + multiline::cursor_down(&app.input, app.input_cursor, DEFAULT_WRAP_WIDTH) |
| 167 | + } |
| 168 | + }; |
| 169 | + if let Some(pos) = new_cursor { |
| 170 | + app.input_cursor = pos; |
| 171 | + true |
| 172 | + } else { |
| 173 | + false |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +fn process_as_history(app: &mut App, direction: ScrollDirection) { |
| 178 | + match direction { |
| 179 | + ScrollDirection::Up => { |
| 180 | + handle_up(app); |
| 181 | + } |
| 182 | + ScrollDirection::Down => { |
| 183 | + handle_down(app); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +fn apply_scroll(app: &mut App, direction: ScrollDirection) { |
| 189 | + match direction { |
| 190 | + ScrollDirection::Up => { |
| 191 | + app.scroll_offset = app.scroll_offset.saturating_add(3); |
| 192 | + } |
| 193 | + ScrollDirection::Down => { |
| 194 | + app.scroll_offset = app.scroll_offset.saturating_sub(3); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments