From ac3af37281b14349d156168000c2e3ee7430c7f2 Mon Sep 17 00:00:00 2001 From: Borber Date: Fri, 26 Apr 2024 21:09:38 +0800 Subject: [PATCH] New version: 0.2.14 --- CHANGELOG.md | 11 +++++++ package.json | 2 +- src-tauri/Cargo.lock | 65 +++++++++++++++++---------------------- src-tauri/Cargo.toml | 2 +- src-tauri/src/config.rs | 27 ++++++++++++++-- src-tauri/src/setup.rs | 8 ++--- src-tauri/src/tray.rs | 42 ++++++++++++++----------- src-tauri/src/util.rs | 15 ++++++++- src-tauri/tauri.conf.json | 2 +- src/App.tsx | 2 +- 10 files changed, 109 insertions(+), 67 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59b1f3c..2f5b003 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## [0.2.14] + +### 更新 + +- 支持选择快捷键, 在 `shift` `ctrl` `capslock` 中进行选择 +- 更新依赖 + +### 预告 + +- 下一项添加, 暗, 亮, 以及匹配系统主题 + ## [0.2.13] ### 更新 diff --git a/package.json b/package.json index 42bcfec..509b2ab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tran", - "version": "0.2.13", + "version": "0.2.14", "description": "简洁, 快速, 划词翻译", "type": "module", "scripts": { diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 29a7df8..54b39f3 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -122,9 +122,9 @@ dependencies = [ [[package]] name = "async-compression" -version = "0.4.8" +version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07dbbf24db18d609b1462965249abdf49129ccad073ec257da372adc83259c60" +checksum = "4e9eabd7a98fe442131a17c316bd9349c43695e49e730c3c8e12cfb5f4da2693" dependencies = [ "flate2", "futures-core", @@ -209,9 +209,9 @@ dependencies = [ [[package]] name = "async-recursion" -version = "1.1.0" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30c5ef0ede93efbf733c1a727f3b6b5a1060bbedd5600183e66f6e4be4af0ec5" +checksum = "3b43422f69d8ff38f95f1b2bb76517c91589a924d1559a0e935d7c8ce0274c11" dependencies = [ "proc-macro2", "quote", @@ -1206,9 +1206,9 @@ checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" [[package]] name = "flate2" -version = "1.0.28" +version = "1.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46303f565772937ffe1d394a4fac6f411c6013172fadde9dcdb1e147a086940e" +checksum = "4556222738635b7a3417ae6130d8f52201e45a0c4d1a907f0826383adb5f85e7" dependencies = [ "crc32fast", "miniz_oxide", @@ -2275,9 +2275,9 @@ checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" [[package]] name = "lock_api" -version = "0.4.11" +version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" dependencies = [ "autocfg", "scopeguard", @@ -2816,9 +2816,9 @@ checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae" [[package]] name = "parking_lot" -version = "0.12.1" +version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +checksum = "7e4af0ca4f6caed20e900d564c242b8e5d4903fdacf31d3daf527b66fe6f42fb" dependencies = [ "lock_api", "parking_lot_core", @@ -2826,15 +2826,15 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.9.9" +version = "0.9.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" dependencies = [ "cfg-if", "libc", - "redox_syscall 0.4.1", + "redox_syscall", "smallvec", - "windows-targets 0.48.5", + "windows-targets 0.52.5", ] [[package]] @@ -3316,15 +3316,6 @@ dependencies = [ "x11", ] -[[package]] -name = "redox_syscall" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" -dependencies = [ - "bitflags 1.3.2", -] - [[package]] name = "redox_syscall" version = "0.5.1" @@ -3474,9 +3465,9 @@ dependencies = [ [[package]] name = "rustls-pki-types" -version = "1.4.1" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ecd36cc4259e3e4514335c4a138c6b43171a8d61d8f5c9348f9fc7529416f247" +checksum = "beb461507cee2c2ff151784c52762cf4d9ff6a61f3e80968600ed24fa837fa54" [[package]] name = "rustversion" @@ -3698,11 +3689,11 @@ dependencies = [ [[package]] name = "serde_with" -version = "3.7.0" +version = "3.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee80b0e361bbf88fd2f6e242ccd19cfda072cb0faa6ae694ecee08199938569a" +checksum = "2c85f8e96d1d6857f13768fcbd895fcb06225510022a2774ed8b5150581847b0" dependencies = [ - "base64 0.21.7", + "base64 0.22.0", "chrono", "hex", "indexmap 1.9.3", @@ -3716,9 +3707,9 @@ dependencies = [ [[package]] name = "serde_with_macros" -version = "3.7.0" +version = "3.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6561dc161a9224638a31d876ccdfefbc1df91d3f3a8342eddb35f055d48c7655" +checksum = "c8b3a576c4eb2924262d5951a3b737ccaf16c931e39a2810c36f9a7e25575557" dependencies = [ "darling", "proc-macro2", @@ -3850,7 +3841,7 @@ dependencies = [ "log", "objc", "raw-window-handle 0.6.1", - "redox_syscall 0.5.1", + "redox_syscall", "wasm-bindgen", "wayland-sys", "web-sys", @@ -4675,7 +4666,7 @@ dependencies = [ [[package]] name = "tran" -version = "0.2.13" +version = "0.2.14" dependencies = [ "anyhow", "arboard", @@ -5220,11 +5211,11 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.6" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" dependencies = [ - "winapi", + "windows-sys 0.52.0", ] [[package]] @@ -5592,9 +5583,9 @@ dependencies = [ [[package]] name = "wry" -version = "0.39.2" +version = "0.39.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a4e9d37c0bbf497cd42bc2e0ba12e0040ab42e454f2907dc1e9ddb2eaf52d34" +checksum = "6e180ac2740d6cb4d5cec0abf63eacbea90f1b7e5e3803043b13c1c84c4b7884" dependencies = [ "base64 0.22.0", "block", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index d8a856f..9f6501d 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tran" -version = "0.2.13" +version = "0.2.14" description = "Tran translate the selected words" authors = ["Borber"] license = "GPLv3" diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 11f1990..d2e1b52 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -1,5 +1,5 @@ use std::sync::{ - atomic::{AtomicBool, Ordering}, + atomic::{AtomicBool, AtomicU8, Ordering}, Arc, }; @@ -18,13 +18,23 @@ pub struct Config { /// /// Mode, true: mirror mode, false: direct mode pub mode: bool, + + /// 快捷键 + /// + /// Key, 0: shift, 1: ctrl, 2: caps + pub key: u8, } -/// 全局配置 +/// API模式 /// -/// Global config +/// API mode pub static MODE: Lazy> = Lazy::new(|| Arc::new(AtomicBool::new(true))); +/// 快捷键配置 +/// +/// Key config +pub static KEY: Lazy> = Lazy::new(|| Arc::new(AtomicU8::new(0))); + /// 读取配置 /// /// read config @@ -38,8 +48,10 @@ pub fn load() { let config = std::fs::read_to_string(path).expect("Failed to read config"); let config = serde_json::from_str::(&config).expect("Failed to parse config"); MODE.store(config.mode, Ordering::SeqCst); + KEY.store(config.key, Ordering::SeqCst); } else { MODE.store(true, Ordering::SeqCst); + KEY.store(0, Ordering::SeqCst); } } @@ -49,6 +61,7 @@ pub fn load() { fn save() -> Result<()> { let config = Config { mode: MODE.load(Ordering::SeqCst), + key: KEY.load(Ordering::SeqCst), }; let exe_dir = util::get_exe_dir(); let path = exe_dir.join("config.json"); @@ -64,3 +77,11 @@ pub fn mode(mode: bool) { MODE.store(mode, Ordering::SeqCst); save().expect("Failed to save config after switch mode"); } + +/// 切换快捷键 +/// +/// switch key +pub fn key(key: u8) { + KEY.store(key, Ordering::SeqCst); + save().expect("Failed to save config after switch key"); +} diff --git a/src-tauri/src/setup.rs b/src-tauri/src/setup.rs index 50097e8..d8499e7 100644 --- a/src-tauri/src/setup.rs +++ b/src-tauri/src/setup.rs @@ -8,7 +8,6 @@ use mouse_position::mouse_position::Mouse; use rdev::{ Button, EventType::{ButtonPress, ButtonRelease, KeyPress, KeyRelease}, - Key, }; use tauri::{App, Manager}; @@ -94,13 +93,14 @@ pub fn handler(app: &mut App) -> Result<(), Box> { // 确定按键 // Confirm the key - let key = Key::ShiftLeft; + // let k = + // let key = Key::ShiftLeft; rdev::listen(move |event| match event.event_type { KeyPress(k) => { // 如果按键不是设置的按键则忽略 // If the key is not the setting key, ignore - if k != key { + if k != util::key() { return; } // 如果在模拟中则忽略 @@ -117,7 +117,7 @@ pub fn handler(app: &mut App) -> Result<(), Box> { KeyRelease(k) => { // 如果按键不是设置的按键则忽略 // If the key is not the setting key, ignore - if k != key { + if k != util::key() { // 仅处理连续双击按键的情况, 时间满足但中间若有其他按键按下则忽略 // Only handle continuous double clicks double = 0; diff --git a/src-tauri/src/tray.rs b/src-tauri/src/tray.rs index ab2b7ff..b066181 100644 --- a/src-tauri/src/tray.rs +++ b/src-tauri/src/tray.rs @@ -24,24 +24,21 @@ pub fn init(app: &AppHandle) -> Result<()> { } fn menu(handle: &AppHandle) -> Result> { - let flag = MODE.load(Ordering::SeqCst); - let mirror = CheckMenuItem::with_id(handle, "mirror", "Mirror", true, flag, None::<&str>) - .expect("Failed to create menu item mirror"); - let google = CheckMenuItem::with_id(handle, "google", "Google", true, !flag, None::<&str>) - .expect("Failed to create menu item google"); - let mode = Submenu::with_items(handle, "Mode", true, &[&mirror, &google]) - .expect("Failed to create submenu item mod."); - let github = MenuItem::with_id(handle, "github", "GitHub", true, None::<&str>) - .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.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."); - let exit = MenuItem::with_id(handle, "exit", "Exit", true, None::<&str>) - .expect("Failed to create menu item exit"); - Menu::with_items(handle, &[&mode, &about, &exit]) + let f = MODE.load(Ordering::SeqCst); + let k = config::KEY.load(Ordering::SeqCst); + let mirror = CheckMenuItem::with_id(handle, "mirror", "Mirror", true, f, None::<&str>)?; + let google = CheckMenuItem::with_id(handle, "google", "Google", true, !f, None::<&str>)?; + let mode = Submenu::with_items(handle, "Mode", true, &[&mirror, &google])?; + let shift = CheckMenuItem::with_id(handle, "shift", "Shift", true, k == 0, None::<&str>)?; + let ctrl = CheckMenuItem::with_id(handle, "ctrl", "Ctrl", true, k == 1, None::<&str>)?; + let caps = CheckMenuItem::with_id(handle, "caps", "Caps", true, k == 2, None::<&str>)?; + let key = Submenu::with_items(handle, "Key", true, &[&shift, &ctrl, &caps])?; + let github = MenuItem::with_id(handle, "github", "GitHub", true, None::<&str>)?; + let telegram = MenuItem::with_id(handle, "telegram", "Telegram", true, None::<&str>)?; + let version = MenuItem::with_id(handle, "version", "v0.2.14", false, None::<&str>)?; + let about = Submenu::with_items(handle, "About", true, &[&github, &telegram, &version])?; + let exit = MenuItem::with_id(handle, "exit", "Exit", true, None::<&str>)?; + Menu::with_items(handle, &[&mode, &key, &about, &exit]) .map_err(|_| anyhow::anyhow!("Failed to create menu")) } @@ -60,6 +57,15 @@ fn handler(app: &AppHandle, event: MenuEvent) { "google" => { config::mode(false); } + "shift" => { + config::key(0); + } + "ctrl" => { + config::key(1); + } + "caps" => { + config::key(2); + } "github" => { let _ = open::that("https://github.com/Borber/Tran"); } diff --git a/src-tauri/src/util.rs b/src-tauri/src/util.rs index 897d5ac..69465fd 100644 --- a/src-tauri/src/util.rs +++ b/src-tauri/src/util.rs @@ -1,8 +1,9 @@ use std::{path::PathBuf, sync::atomic::Ordering, time::SystemTime}; +use rdev::Key; use selection::get_text; -use crate::{clip, common::SIMULATION}; +use crate::{clip, common::SIMULATION, config::KEY}; /// 模拟获取复制文本 /// @@ -53,3 +54,15 @@ pub fn get_exe_dir() -> PathBuf { .expect("Failed to get current executable parent directory") .to_path_buf() } + +/// 获取当前指定快捷键 +/// +/// Get current key +pub fn key() -> Key { + let key = KEY.load(Ordering::SeqCst); + match key { + 1 => Key::ControlLeft, + 2 => Key::CapsLock, + _ => Key::ShiftLeft, + } +} diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 51d56d9..045b383 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,6 +1,6 @@ { "productName": "Tran", - "version": "0.2.13", + "version": "0.2.14", "identifier": "com.borber.tran", "build": { "beforeDevCommand": "pnpm dev", diff --git a/src/App.tsx b/src/App.tsx index 91ea382..be864b8 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.13") + Update(version != "0.2.14") } ) })