Skip to content

Commit 243e071

Browse files
committed
feat: optimize scrolling
1 parent d1ed16f commit 243e071

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "web_touchpad"
3-
version = "1.1.0"
3+
version = "1.1.1"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/main.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ fn current_time_millis() -> u128 {
3939
}
4040
}
4141

42-
fn should_process_message(last_processed_time: &Arc<AtomicU64>) -> bool {
42+
fn should_process_scroll_message(last_processed_time: &Arc<AtomicU64>, time_interval: u64) -> bool {
4343
let now = current_time_millis() as u64;
4444
let last_time = last_processed_time.load(Ordering::Relaxed);
4545
let elapsed = now - last_time;
46-
if elapsed >= 100 {
46+
if elapsed >= time_interval {
4747
last_processed_time.store(now, Ordering::Relaxed);
4848
true
4949
} else {
@@ -78,14 +78,20 @@ fn process_mouse_events(
7878
scroll_lines = if dy_int > 0 { 1 } else { -1 };
7979
}
8080

81-
if should_process_message(&last_processed_time) {
81+
if scroll_lines != 0 && should_process_scroll_message(&last_processed_time, 100)
82+
{
8283
enigo.mouse_scroll_y(scroll_lines);
8384
println!("Mouse scrolled by: dy={}", scroll_lines);
8485
}
8586

8687
continue;
8788
}
8889

90+
// Do not respond to move messages for a period of time after scrolling
91+
if should_process_scroll_message(&last_processed_time, 1000) {
92+
continue;
93+
}
94+
8995
// Calculate the acceleration based on speed and distance
9096
// and adjust the mouse movement accordingly
9197
let acceleration_factor = 10.0; // Acceleration factor, adjustable according to actual requirements
@@ -99,8 +105,13 @@ fn process_mouse_events(
99105
let dx_int = dx.round() as i32;
100106
let dy_int = dy.round() as i32;
101107

108+
// Discard abnormal movement distances
109+
if dx_int >= 1000 || dy_int >= 1000 {
110+
continue;
111+
}
112+
102113
enigo.mouse_move_relative(dx_int, dy_int);
103-
println!("Mouse moved by: dx={}, dy={}", dx, dy);
114+
println!("Mouse moved by: dx={}, dy={}", dx_int, dy_int);
104115
}
105116
ClientEvent::MouseClick { button } => {
106117
match button {

0 commit comments

Comments
 (0)