forked from wezterm/wezterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.rs
88 lines (79 loc) · 2.32 KB
/
input.rs
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
// clippy hates bitflags
#![cfg_attr(
feature = "cargo-clippy",
allow(clippy::suspicious_arithmetic_impl, clippy::redundant_field_names)
)]
use super::VisibleRowIndex;
#[cfg(feature = "use_serde")]
use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};
pub use termwiz::input::KeyCode;
pub use termwiz::input::Modifiers as KeyModifiers;
#[cfg_attr(feature = "use_serde", derive(Deserialize, Serialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum MouseButton {
Left,
Middle,
Right,
WheelUp(usize),
WheelDown(usize),
None,
}
#[cfg_attr(feature = "use_serde", derive(Deserialize, Serialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum MouseEventKind {
Press,
Release,
Move,
}
#[cfg_attr(feature = "use_serde", derive(Deserialize, Serialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct MouseEvent {
pub kind: MouseEventKind,
pub x: usize,
pub y: VisibleRowIndex,
pub button: MouseButton,
pub modifiers: KeyModifiers,
}
/// This is a little helper that keeps track of the "click streak",
/// which is the number of successive clicks of the same mouse button
/// within the `CLICK_INTERVAL`. The streak is reset to 1 each time
/// the mouse button differs from the last click, or when the elapsed
/// time exceeds `CLICK_INTERVAL`, or when the cursor position
/// changes to a different character cell.
#[derive(Debug, Clone)]
pub struct LastMouseClick {
pub button: MouseButton,
position: (usize, i64),
time: Instant,
pub streak: usize,
}
/// The multi-click interval, measured in milliseconds
const CLICK_INTERVAL: u64 = 500;
impl LastMouseClick {
pub fn new(button: MouseButton, position: (usize, i64)) -> Self {
Self {
button,
position,
time: Instant::now(),
streak: 1,
}
}
pub fn add(&self, button: MouseButton, position: (usize, i64)) -> Self {
let now = Instant::now();
let streak = if button == self.button
&& position == self.position
&& now.duration_since(self.time) <= Duration::from_millis(CLICK_INTERVAL)
{
self.streak + 1
} else {
1
};
Self {
button,
position,
time: now,
streak,
}
}
}