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
use crossterm::event::KeyModifiers;
use std::time::{Duration, Instant};
const DOUBLE_CLICK_WINDOW: Duration = Duration::from_millis(500);
/// How far a run of plain clicks at one cell has got (#1570).
#[derive(Clone, Copy, PartialEq, Eq)]
enum Run {
/// One click, which the next can make a double.
Single,
/// A double the editor kept, which the next click can make a triple.
Double,
}
#[derive(Default)]
pub struct ClickTracker {
/// The last plain click and how far its run has got.
last: Option<(Instant, u16, u16, Run)>,
}
impl ClickTracker {
/// The run this click would continue, or `None` when it starts a new
/// one: a modified click, another cell, or too late.
fn run_before(&self, now: Instant, col: u16, row: u16, mods: KeyModifiers) -> Option<Run> {
match self.last {
Some((t, x, y, run))
if mods.is_empty()
&& row == y
&& col.abs_diff(x) <= 1
&& now.duration_since(t) <= DOUBLE_CLICK_WINDOW =>
{
Some(run)
}
_ => None,
}
}
/// True when this click completes a double-click at (near enough) the
/// same cell, inside the window.
///
/// A click carrying modifiers is never half of a double (#317). The two
/// halves have to be ONE gesture, and ctrl / alt / shift + click is a
/// different gesture with its own meaning: open a link, add a caret,
/// extend a selection. The modifiers are taken here rather than tested at
/// the call sites so that every site - including ones added later - is
/// forced by the compiler to answer the question. A site that silently
/// did not is exactly what armed the tracker from a ctrl+click that
/// matched no link, so the user's next ordinary click selected a word
/// they never double-clicked.
pub fn is_double(&self, now: Instant, col: u16, row: u16, mods: KeyModifiers) -> bool {
self.run_before(now, col, row, mods) == Some(Run::Single)
}
/// True when this click is the third of a run: it follows a double that
/// [`record_double`](Self::record_double) kept, at the same cell and
/// inside the window (#1570). Only the editor keeps one, for its
/// whole-line selection; every other tracker clears after a double.
pub fn is_triple(&self, now: Instant, col: u16, row: u16, mods: KeyModifiers) -> bool {
self.run_before(now, col, row, mods) == Some(Run::Double)
}
/// Arm the tracker with this click as a possible first half of a double.
/// A modified click CLEARS instead of arming, for the same reason
/// [`is_double`](Self::is_double) refuses one: it can be neither half of
/// a pair. Clearing rather than merely skipping the arm also stops a
/// modified click from sitting harmlessly between two plain ones and
/// letting them pair across it.
pub fn record(&mut self, now: Instant, col: u16, row: u16, mods: KeyModifiers) {
self.last = mods.is_empty().then_some((now, col, row, Run::Single));
}
/// Keep this click as the second half of a double, so a third plain
/// click here inside the window is a triple (#1570). Clearing instead,
/// as a double used to, made the third click a fresh single that
/// dropped the word the double had just selected.
pub fn record_double(&mut self, now: Instant, col: u16, row: u16) {
self.last = Some((now, col, row, Run::Double));
}
pub fn clear(&mut self) {
self.last = None;
}
pub fn clear_if_moved(&mut self, col: u16, row: u16) {
if let Some((_, x, y, _)) = self.last
&& (col != x || row != y)
{
self.last = None;
}
}
}
/// Double-click tracking for MODIFIED clicks, kept apart from [`ClickTracker`].
///
/// A modified click must never be recorded into the built-in trackers: those
/// are read by the editor's plain select-word (and the tree's open-file), none
/// of which consult modifiers, so arming them with a ctrl+click would make the
/// user's next ORDINARY click select a word they never asked for.
///
/// But `double_click` is classified by asking a tracker whether it just saw a
/// click at this cell — so with nothing recording modified clicks, a
/// `ctrl+double_click` binding could never fire at all. This is that missing
/// record, in a tracker only the binding dispatch reads.
///
/// The modifier set is part of the match: ctrl+click followed by alt+click is
/// two different gestures, not a double of either.
#[derive(Default)]
pub struct ModifiedClickTracker {
last: Option<(Instant, u16, u16, crossterm::event::KeyModifiers)>,
}
impl ModifiedClickTracker {
pub fn is_double(
&self,
now: Instant,
col: u16,
row: u16,
mods: crossterm::event::KeyModifiers,
) -> bool {
matches!(
self.last,
Some((t, x, y, m))
if m == mods
&& row == y
&& col.abs_diff(x) <= 1
&& now.duration_since(t) <= DOUBLE_CLICK_WINDOW
)
}
pub fn record(
&mut self,
now: Instant,
col: u16,
row: u16,
mods: crossterm::event::KeyModifiers,
) {
self.last = Some((now, col, row, mods));
}
pub fn clear(&mut self) {
self.last = None;
}
/// Cancel a pending pair when the pointer drags away, exactly as
/// [`ClickTracker::clear_if_moved`] does for the built-ins.
///
/// Without it, click → drag away → click back counted as a double for
/// modified gestures where the identical unmodified sequence correctly
/// did not: a drag is a deliberate gesture of its own, and the click that
/// began it is spent, not the first half of a pair.
pub fn clear_if_moved(&mut self, col: u16, row: u16) {
if let Some((_, x, y, _)) = self.last
&& (col != x || row != y)
{
self.last = None;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
// #317: the two halves of a double-click must be ONE gesture. A modified
// click is a different gesture (open a link, add a caret, extend a
// selection), so it can be neither half: it must not COMPLETE a double,
// and it must not ARM one for the next ordinary click - which is how a
// ctrl+click that matched no link made the user's following plain click
// select a word they never double-clicked.
#[test]
fn a_modified_click_is_neither_half_of_a_double() {
let now = Instant::now();
let plain = KeyModifiers::NONE;
// The baseline the fix must not break: two plain clicks pair.
let mut t = ClickTracker::default();
t.record(now, 10, 3, plain);
assert!(t.is_double(now, 10, 3, plain), "two plain clicks must pair");
// A modified click cannot COMPLETE a double armed by a plain one.
for mods in [
KeyModifiers::CONTROL,
KeyModifiers::SUPER,
KeyModifiers::ALT,
KeyModifiers::SHIFT,
] {
let mut t = ClickTracker::default();
t.record(now, 10, 3, plain);
assert!(
!t.is_double(now, 10, 3, mods),
"{mods:?}+click must not classify as a double-click"
);
}
// And a modified click cannot ARM one for the next plain click. This
// is the reported defect: the ctrl+click fell through the link path
// and armed the tracker, so the NEXT ordinary click word-selected.
for mods in [
KeyModifiers::CONTROL,
KeyModifiers::SUPER,
KeyModifiers::ALT,
KeyModifiers::SHIFT,
] {
let mut t = ClickTracker::default();
t.record(now, 10, 3, mods);
assert!(
!t.is_double(now, 10, 3, plain),
"a plain click after a {mods:?}+click must not be a double"
);
}
}
// A modified click does not merely fail to arm: it clears. Otherwise it
// would sit harmlessly between two plain clicks and let them pair across
// it, which is the same word-selection-nobody-asked-for by a longer route.
#[test]
fn a_modified_click_breaks_a_pair_it_sits_between() {
let now = Instant::now();
let mut t = ClickTracker::default();
t.record(now, 10, 3, KeyModifiers::NONE);
t.record(now, 10, 3, KeyModifiers::CONTROL);
assert!(
!t.is_double(now, 10, 3, KeyModifiers::NONE),
"a modified click between two plain ones must break the pair, not be invisible"
);
}
// The window and the cell tolerance are unchanged by the modifier work.
#[test]
fn pairing_still_needs_the_same_cell_inside_the_window() {
let now = Instant::now();
let plain = KeyModifiers::NONE;
let mut t = ClickTracker::default();
t.record(now, 10, 3, plain);
assert!(
t.is_double(now, 11, 3, plain),
"one cell of drift still pairs"
);
assert!(
!t.is_double(now, 13, 3, plain),
"a distant cell does not pair"
);
assert!(!t.is_double(now, 10, 4, plain), "another row does not pair");
assert!(
!t.is_double(
now + DOUBLE_CLICK_WINDOW + Duration::from_millis(1),
10,
3,
plain
),
"past the window does not pair"
);
}
// #1570: a third plain click at the same cell inside the window after a
// double is a triple, not a fresh single and not another double; a
// fourth starts over once the triple has cleared the run.
#[test]
fn a_third_click_after_a_kept_double_is_a_triple() {
let now = Instant::now();
let plain = KeyModifiers::NONE;
let mut t = ClickTracker::default();
t.record(now, 10, 3, plain);
assert!(t.is_double(now, 10, 3, plain) && !t.is_triple(now, 10, 3, plain));
t.record_double(now, 10, 3);
assert!(t.is_triple(now, 10, 3, plain), "three clicks are a triple");
assert!(!t.is_double(now, 10, 3, plain), "and not a second double");
assert!(!t.is_triple(now, 10, 3, KeyModifiers::SHIFT));
assert!(!t.is_triple(now, 10, 4, plain), "another row starts over");
let late = now + DOUBLE_CLICK_WINDOW + Duration::from_millis(1);
assert!(!t.is_triple(late, 10, 3, plain), "too late starts over");
t.clear();
assert!(!t.is_double(now, 10, 3, plain) && !t.is_triple(now, 10, 3, plain));
}
}