From 8ef9c37d9e1492a037af799dc2f583002139b527 Mon Sep 17 00:00:00 2001 From: Borber Date: Thu, 28 Mar 2024 13:17:19 +0800 Subject: [PATCH] New version: 0.2.10 --- CHANGELOG.md | 11 +++++++++++ package.json | 2 +- src-tauri/Cargo.lock | 3 ++- src-tauri/Cargo.toml | 6 +++++- src-tauri/src/common.rs | 3 +++ src-tauri/src/setup.rs | 6 +++++- src-tauri/src/shortcut.rs | 18 ++++++++++++++++++ src-tauri/src/tray.rs | 2 +- src-tauri/src/util.rs | 17 +++++++++++++++++ src-tauri/tauri.conf.json | 2 +- src/App.tsx | 13 ++++++++++--- 11 files changed, 74 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfc732d..ef422f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## [0.2.10] + +### 重要! + +- 更换快捷键为 双击 `Shift`, 因为 `CapsLock` 在终端模拟类软件中表现不佳, 非常抱歉. + +### 修复 + +- 拖动 Tran 窗口导致触发翻译 +- 快捷键按键导致固定窗口被移动并关闭 + ## [0.2.9] ### 重要! diff --git a/package.json b/package.json index 7ac7dd5..4e48190 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tran", - "version": "0.2.9", + "version": "0.2.10", "description": "简洁, 快速, 划词翻译", "type": "module", "scripts": { diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 6a15862..1e51de4 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -4882,7 +4882,7 @@ dependencies = [ [[package]] name = "tran" -version = "0.2.9" +version = "0.2.10" dependencies = [ "anyhow", "arboard", @@ -4904,6 +4904,7 @@ dependencies = [ "tauri-plugin-process", "tauri-plugin-single-instance", "tokio", + "windows 0.54.0", ] [[package]] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index d75ff3d..00aef71 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tran" -version = "0.2.9" +version = "0.2.10" description = "Tran translate the selected words" authors = ["Borber"] license = "GPLv3" @@ -52,6 +52,10 @@ open = "5" rdev = { git = "https://github.com/fufesou/rdev.git" } crossbeam-channel = "0.5" +[dependencies.windows] +version = "0.54" +features = ["Win32_UI_WindowsAndMessaging", "Win32_Foundation"] + [features] custom-protocol = ["tauri/custom-protocol"] diff --git a/src-tauri/src/common.rs b/src-tauri/src/common.rs index e5776db..8a62798 100644 --- a/src-tauri/src/common.rs +++ b/src-tauri/src/common.rs @@ -1,12 +1,15 @@ use std::sync::{atomic::AtomicBool, Arc}; use once_cell::sync::Lazy; +use parking_lot::RwLock; use reqwest::Client; use crate::{config, manager}; pub static CLIENT: Lazy = Lazy::new(Client::new); pub static PIN: Lazy> = Lazy::new(|| Arc::new(AtomicBool::new(false))); +pub static OLD_CONTENT: Lazy>> = + Lazy::new(|| Arc::new(RwLock::new(String::new()))); pub async fn init() { config::load(); diff --git a/src-tauri/src/setup.rs b/src-tauri/src/setup.rs index f694ab5..f6e7a9b 100644 --- a/src-tauri/src/setup.rs +++ b/src-tauri/src/setup.rs @@ -36,10 +36,14 @@ pub fn handler(app: &mut App) -> Result<(), Box> { // 监听快捷键 spawn(move || { while let Ok(()) = key_r.recv() { + // 避免固定却被快捷键调用移动 + // 在快捷键调用时, 应该暂时保证窗口不被关闭 + let pin = common::PIN.load(Ordering::SeqCst); + // pin when shortcut // 在快捷键调用时, 应该暂时保证窗口不被关闭 common::PIN.store(true, Ordering::SeqCst); - shortcut::show(&key_panel, false).expect("Shortcut key call failed") + shortcut::show(&key_panel, pin).expect("Shortcut key call failed") } }); diff --git a/src-tauri/src/shortcut.rs b/src-tauri/src/shortcut.rs index 8738fe5..b14ea45 100644 --- a/src-tauri/src/shortcut.rs +++ b/src-tauri/src/shortcut.rs @@ -8,6 +8,8 @@ use tauri::WebviewWindow; use selection::get_text; use crate::clip; +use crate::common::OLD_CONTENT; +use crate::util; /// 鼠标坐标与选中内容 /// @@ -21,6 +23,13 @@ pub struct ShowVO { } pub fn show(panel: &WebviewWindow, pin: bool) -> Result<()> { + // 判断当前活动窗口是否为 Tran 自己, 避免拖动窗口触发翻译 + // Determine whether the current active window is Tran itself to avoid dragging the window to trigger translation + let window = util::get_window_title(); + if window.contains("Tran") { + return Ok(()); + } + let s_copy = get_text(); let content = if s_copy.is_empty() { @@ -38,6 +47,15 @@ pub fn show(panel: &WebviewWindow, pin: bool) -> Result<()> { s_copy }; + // 判断 content 如果是固定状态且和上次翻译相同则直接跳过 + // Skip directly if the content is in a fixed state and is the same as the last translation + if pin && content == *OLD_CONTENT.read() { + return Ok(()); + } else { + let mut old_content = OLD_CONTENT.write(); + *old_content = content.clone(); + } + if pin { panel .emit( diff --git a/src-tauri/src/tray.rs b/src-tauri/src/tray.rs index cedaac9..7425939 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.9", false, None::<&str>) + let version = MenuItem::with_id(handle, "version", "v0.2.10", 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 e1f7dcc..483b970 100644 --- a/src-tauri/src/util.rs +++ b/src-tauri/src/util.rs @@ -1,5 +1,9 @@ use std::path::PathBuf; +use windows::Win32::UI::WindowsAndMessaging::{ + GetForegroundWindow, GetWindowTextLengthW, GetWindowTextW, +}; + /// 获取可执行文件位置 /// /// Get executable file path @@ -10,3 +14,16 @@ pub fn get_exe_dir() -> PathBuf { .expect("Failed to get current executable parent directory") .to_path_buf() } + +/// 获取活动窗口的标题 +/// +/// Get the title of the active window +pub fn get_window_title() -> String { + unsafe { + let window = GetForegroundWindow(); + let length = GetWindowTextLengthW(window) as usize; + let mut buffer: Vec = vec![0; length + 1]; + GetWindowTextW(window, &mut buffer); + String::from_utf16_lossy(&buffer) + } +} diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 6342d47..c1f73b4 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,6 +1,6 @@ { "productName": "Tran", - "version": "0.2.9", + "version": "0.2.10", "identifier": "com.borber.tran", "build": { "beforeDevCommand": "pnpm dev", diff --git a/src/App.tsx b/src/App.tsx index aabca4e..10137e0 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -33,6 +33,7 @@ const App = () => { const panel = getCurrent() const [result, Result] = createSignal() const [update, Update] = createSignal(false) + let pin = false onMount(async () => { // 监听事件, 显示panel @@ -43,6 +44,7 @@ const App = () => { content: string pin: boolean }>("show", async (pos) => { + pin = pos.payload.pin Result(undefined) if (!pos.payload.pin) { await panel.setPosition( @@ -95,7 +97,7 @@ const App = () => { await fetch("https://key.borber.top/TRAN_VERSION").then( async (resp) => { const version = await resp.text() - Update(version != "0.2.9") + Update(version != "0.2.10") } ) }) @@ -122,8 +124,13 @@ const App = () => { content, }) - await panel.hide() - Result(undefined) + // 未固定时, 双击复制关闭窗体 + // When not fixed, double-click to copy and close the windo + + if (!pin) { + await panel.hide() + Result(undefined) + } }} >