From 0e7a7082d3b6149844477b8e2711d45f756814d2 Mon Sep 17 00:00:00 2001 From: Borber Date: Fri, 1 Mar 2024 11:38:09 +0800 Subject: [PATCH] Initial support for double-clicking to select translation --- src-tauri/src/setup.rs | 49 +++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/src-tauri/src/setup.rs b/src-tauri/src/setup.rs index c18d925..2e310f4 100644 --- a/src-tauri/src/setup.rs +++ b/src-tauri/src/setup.rs @@ -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}, @@ -32,9 +33,6 @@ pub fn handler(app: &mut App) -> Result<(), Box> { .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); @@ -57,9 +55,18 @@ pub fn handler(app: &mut App) -> Result<(), Box> { // 监听快捷键 和 鼠标操作 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 @@ -69,9 +76,9 @@ pub fn handler(app: &mut App) -> Result<(), Box> { 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) => { @@ -81,7 +88,7 @@ pub fn handler(app: &mut App) -> Result<(), Box> { .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) => { @@ -92,10 +99,32 @@ pub fn handler(app: &mut App) -> Result<(), Box> { .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"), + }; } } _ => (),