Skip to content

Commit

Permalink
Initial support for double-clicking to select translation
Browse files Browse the repository at this point in the history
  • Loading branch information
Borber committed Mar 1, 2024
1 parent a2eba65 commit 0e7a708
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions src-tauri/src/setup.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::{
sync::{
atomic::{AtomicU64, Ordering},
atomic::{AtomicI32, AtomicU64, Ordering},
Arc,
},
time::SystemTime,
};

use crossbeam_channel::bounded;
use mouse_position::mouse_position::Mouse;
use rdev::{
Button,
EventType::{ButtonPress, ButtonRelease, KeyRelease},
Expand All @@ -32,9 +33,6 @@ pub fn handler(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
.expect("Failed to get panel window");
let mouse_panel = key_panel.clone();

let key_cap = Arc::new(AtomicU64::new(0));
let mouse_cap = Arc::new(AtomicU64::new(0));

let (key_s, key_r) = bounded(1);
let (mouse_s, mouse_r) = bounded(1);

Expand All @@ -57,9 +55,18 @@ pub fn handler(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {

// 监听快捷键 和 鼠标操作
tokio::spawn(async {
// 双击capslock
let double_capslock_cap = Arc::new(AtomicU64::new(0));
// 划词翻译
let selected_cap = Arc::new(AtomicU64::new(0));
// 双击鼠标左键
let double_click_cap = Arc::new(AtomicU64::new(0));
let double_click_x = Arc::new(AtomicI32::new(0));
let double_click_y = Arc::new(AtomicI32::new(0));

rdev::listen(move |event| match event.event_type {
KeyRelease(CapsLock) => {
let old = key_cap.load(std::sync::atomic::Ordering::SeqCst);
let old = double_capslock_cap.load(std::sync::atomic::Ordering::SeqCst);

let now = SystemTime::now();
let timestamp = now
Expand All @@ -69,9 +76,9 @@ pub fn handler(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {

if now < old + 1000 {
key_s.send(()).expect("Channel send failed");
key_cap.store(0, std::sync::atomic::Ordering::SeqCst);
double_capslock_cap.store(0, std::sync::atomic::Ordering::SeqCst);
} else {
key_cap.store(now, std::sync::atomic::Ordering::SeqCst);
double_capslock_cap.store(now, std::sync::atomic::Ordering::SeqCst);
}
}
ButtonPress(Button::Left) => {
Expand All @@ -81,7 +88,7 @@ pub fn handler(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Time went backwards");
let now = timestamp.as_millis() as u64;
mouse_cap.store(now, Ordering::SeqCst);
selected_cap.store(now, Ordering::SeqCst);
}
}
ButtonRelease(Button::Left) => {
Expand All @@ -92,10 +99,32 @@ pub fn handler(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
.expect("Time went backwards");
let now = timestamp.as_millis() as u64;

let old = mouse_cap.load(Ordering::SeqCst);
if now > old + 500 {
let old = selected_cap.load(Ordering::SeqCst);
if now >= old + 500 {
mouse_s.send(()).expect("Channel send failed");
return;
}

// 检测双击
let old = double_click_cap.load(Ordering::SeqCst);
let x = double_click_x.load(Ordering::SeqCst);
let y = double_click_y.load(Ordering::SeqCst);

let position = Mouse::get_mouse_position();
match position {
Mouse::Position { x: x1, y: y1 } => {
// 判断双击时间间隔
// 判断双击是否在同一位置
if now < old + 500 && x == x1 && y == y1 {
mouse_s.send(()).expect("Channel send failed");
} else {
double_click_cap.store(now, Ordering::SeqCst);
double_click_x.store(x1, Ordering::SeqCst);
double_click_y.store(y1, Ordering::SeqCst);
}
}
Mouse::Error => println!("Error getting mouse position"),
};
}
}
_ => (),
Expand Down

0 comments on commit 0e7a708

Please sign in to comment.