use crossterm::event::KeyModifiers;
use std::time::{Duration, Instant};
const DOUBLE_CLICK_WINDOW: Duration = Duration::from_millis(500);
#[derive(Default)]
pub struct ClickTracker {
last: Option<(Instant, u16, u16)>,
}
impl ClickTracker {
pub fn is_double(&self, now: Instant, col: u16, row: u16, mods: KeyModifiers) -> bool {
mods.is_empty()
&& matches!(
self.last,
Some((t, x, y))
if 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: KeyModifiers) {
self.last = mods.is_empty().then_some((now, col, row));
}
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;
}
}
}
#[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;
}
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::*;
#[test]
fn a_modified_click_is_neither_half_of_a_double() {
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), "two plain clicks must pair");
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"
);
}
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"
);
}
}
#[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"
);
}
#[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"
);
}
}