diff --git a/CHANGELOG.md b/CHANGELOG.md index 69575c3..ff77e11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ ### 更新 - 拖拽将不再 fallback 到系统剪贴板, 此更改将导致在不支持 模拟复制 的软件中, 你需要手动复制再调用 Tran. 这也许会带来一些问题. 但是目前拖拽导致的 fallback 造成了一些困扰 +- 为后续自定义按键做准备 + - 修改检测逻辑, 仅在两次快速按下按键时才触发, 长按后按键不再触发, + - 在获取选中文本时, 将关闭按键监听, 避免模拟操作触发快捷键 - 调整代码, 让整体逻辑更加清晰. - 速度进一步提升. - 修复 tauri v2 依赖问题. diff --git a/package.json b/package.json index 37df39f..45a4d13 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tran", - "version": "0.2.12", + "version": "0.2.13", "description": "简洁, 快速, 划词翻译", "type": "module", "scripts": { diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 933f53a..7cd9b65 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -4674,7 +4674,7 @@ dependencies = [ [[package]] name = "tran" -version = "0.2.12" +version = "0.2.13" dependencies = [ "anyhow", "arboard", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index b0a803e..d8a856f 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tran" -version = "0.2.12" +version = "0.2.13" description = "Tran translate the selected words" authors = ["Borber"] license = "GPLv3" diff --git a/src-tauri/src/common.rs b/src-tauri/src/common.rs index d02c53d..2147447 100644 --- a/src-tauri/src/common.rs +++ b/src-tauri/src/common.rs @@ -10,6 +10,7 @@ pub static CLIENT: Lazy = Lazy::new(Client::new); pub static PIN: Lazy> = Lazy::new(|| Arc::new(AtomicBool::new(false))); pub static TMP_PIN: Lazy> = Lazy::new(|| Arc::new(AtomicBool::new(false))); pub static OLD: Lazy>> = Lazy::new(|| Arc::new(RwLock::new(String::new()))); +pub static SIMULATION: Lazy> = Lazy::new(|| Arc::new(AtomicBool::new(false))); pub async fn init() { config::load(); diff --git a/src-tauri/src/setup.rs b/src-tauri/src/setup.rs index 4d6a12a..50097e8 100644 --- a/src-tauri/src/setup.rs +++ b/src-tauri/src/setup.rs @@ -1,20 +1,19 @@ use std::{ sync::atomic::Ordering, thread::{sleep, spawn}, - time::SystemTime, }; use crossbeam_channel::bounded; use mouse_position::mouse_position::Mouse; use rdev::{ Button, - EventType::{ButtonPress, ButtonRelease, KeyRelease}, + EventType::{ButtonPress, ButtonRelease, KeyPress, KeyRelease}, Key, }; use tauri::{App, Manager}; use crate::{ - common::{self, OLD, PIN, TMP_PIN}, + common::{self, OLD, PIN, SIMULATION, TMP_PIN}, shortcut, tray, util, window, }; @@ -78,54 +77,85 @@ pub fn handler(app: &mut App) -> Result<(), Box> { // 监听快捷键 和 鼠标操作 spawn(move || { + // 检测是否快速按下并抬起按键 + // Check if the key is quickly pressed and released + let mut fast = 0; // 双击 // Double click - let mut double_cap = 0; + let mut double = 0; // 划词翻译 // Selection translation - let mut selected_cap = 0; + let mut selected = 0; // 双击鼠标左键 // Double click mouse left - let mut double_click_cap = 0; + let mut double_click = 0; let mut double_click_x = 0; let mut double_click_y = 0; + // 确定按键 + // Confirm the key + let key = Key::ShiftLeft; + rdev::listen(move |event| match event.event_type { - KeyRelease(Key::ShiftLeft) => { - let old = double_cap; + KeyPress(k) => { + // 如果按键不是设置的按键则忽略 + // If the key is not the setting key, ignore + if k != key { + return; + } + // 如果在模拟中则忽略 + // If in simulation, ignore + if SIMULATION.load(Ordering::SeqCst) { + return; + } + + if fast == 0 { + let now = util::now(); + fast = now; + } + } + KeyRelease(k) => { + // 如果按键不是设置的按键则忽略 + // If the key is not the setting key, ignore + if k != key { + // 仅处理连续双击按键的情况, 时间满足但中间若有其他按键按下则忽略 + // Only handle continuous double clicks + double = 0; + return; + } + // 如果在模拟中则忽略 + // If in simulation, ignore + if SIMULATION.load(Ordering::SeqCst) { + return; + } + + let now = util::now(); - let now = SystemTime::now(); - let timestamp = now - .duration_since(SystemTime::UNIX_EPOCH) - .expect("Time went backwards"); - let now = timestamp.as_millis() as u64; + if now > fast + 500 { + fast = 0; + return; + } + fast = 0; + let old = double; if now < old + 1000 { key_s.send(()).expect("Channel send failed"); - double_cap = 0; + double = 0; } else { - double_cap = now; + double = now; } } ButtonPress(Button::Left) => { if common::PIN.load(Ordering::SeqCst) { - let now = SystemTime::now(); - let timestamp = now - .duration_since(SystemTime::UNIX_EPOCH) - .expect("Time went backwards"); - let now = timestamp.as_millis() as u64; - selected_cap = now; + let now = util::now(); + selected = now; } } ButtonRelease(Button::Left) => { if common::PIN.load(Ordering::SeqCst) { - let now = SystemTime::now(); - let timestamp = now - .duration_since(SystemTime::UNIX_EPOCH) - .expect("Time went backwards"); - let now = timestamp.as_millis() as u64; + let now = util::now(); - let old = selected_cap; + let old = selected; if now >= old + 500 { match mouse_s.send(()) { Ok(_) => (), @@ -137,7 +167,7 @@ pub fn handler(app: &mut App) -> Result<(), Box> { } // 检测双击 - let old = double_click_cap; + let old = double_click; let x = double_click_x; let y = double_click_y; @@ -154,7 +184,7 @@ pub fn handler(app: &mut App) -> Result<(), Box> { } } } else { - double_click_cap = now; + double_click = now; double_click_x = x1; double_click_y = y1; } @@ -163,12 +193,6 @@ pub fn handler(app: &mut App) -> Result<(), Box> { }; } } - KeyRelease(_) => { - // 仅处理连续双击按键的情况, 时间满足但中间若有其他按键按下则忽略 - // Only handle continuous double clicks - double_cap = 0; - } - _ => (), }) }); diff --git a/src-tauri/src/tray.rs b/src-tauri/src/tray.rs index f3a3625..ab2b7ff 100644 --- a/src-tauri/src/tray.rs +++ b/src-tauri/src/tray.rs @@ -35,7 +35,7 @@ fn menu(handle: &AppHandle) -> Result> { .expect("Failed to create menu item github"); let telegram = MenuItem::with_id(handle, "telegram", "Telegram", true, None::<&str>) .expect("Failed to create menu item telegram"); - let version = MenuItem::with_id(handle, "version", "v0.2.12", false, None::<&str>) + let version = MenuItem::with_id(handle, "version", "v0.2.13", false, None::<&str>) .expect("Failed to create menu item version"); let about = Submenu::with_items(handle, "About", true, &[&github, &telegram, &version]) .expect("Failed to create submenu item mod."); diff --git a/src-tauri/src/util.rs b/src-tauri/src/util.rs index 80b3fed..897d5ac 100644 --- a/src-tauri/src/util.rs +++ b/src-tauri/src/util.rs @@ -1,16 +1,22 @@ -use std::path::PathBuf; +use std::{path::PathBuf, sync::atomic::Ordering, time::SystemTime}; use selection::get_text; -use crate::clip; +use crate::{clip, common::SIMULATION}; /// 模拟获取复制文本 /// /// Simulate getting copy text pub fn content(fallback: bool) -> String { + // 更改模拟标识, 按键监听 + // Change the simulation flag, trigger the key + SIMULATION.store(true, Ordering::SeqCst); // 模拟复制获取文本 // Simulate copy and get text let content = get_text(); + // 重置模拟标识 + // Reset the simulation flag + SIMULATION.store(false, Ordering::SeqCst); if content.is_empty() && fallback { // 获取系统剪贴板内容 @@ -26,6 +32,17 @@ pub fn content(fallback: bool) -> String { } } +/// 获取当前时间 +/// +/// Get current time +pub fn now() -> u64 { + let now = SystemTime::now(); + let timestamp = now + .duration_since(SystemTime::UNIX_EPOCH) + .expect("Time went backwards"); + timestamp.as_millis() as u64 +} + /// 获取可执行文件位置 /// /// Get executable file path diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 4411f59..51d56d9 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,6 +1,6 @@ { "productName": "Tran", - "version": "0.2.12", + "version": "0.2.13", "identifier": "com.borber.tran", "build": { "beforeDevCommand": "pnpm dev", diff --git a/src/App.tsx b/src/App.tsx index cc8b331..91ea382 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -53,7 +53,7 @@ const App = () => { await fetch("https://key.borber.top/TRAN_VERSION").then( async (resp) => { const version = await resp.text() - Update(version != "0.2.12") + Update(version != "0.2.13") } ) })