Skip to content

Commit

Permalink
New version: 0.2.10
Browse files Browse the repository at this point in the history
  • Loading branch information
Borber committed Mar 28, 2024
1 parent 4f5b80d commit 8ef9c37
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 9 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## [0.2.10]

### 重要!

- 更换快捷键为 双击 `Shift`, 因为 `CapsLock` 在终端模拟类软件中表现不佳, 非常抱歉.

### 修复

- 拖动 Tran 窗口导致触发翻译
- 快捷键按键导致固定窗口被移动并关闭

## [0.2.9]

### 重要!
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tran",
"version": "0.2.9",
"version": "0.2.10",
"description": "简洁, 快速, 划词翻译",
"type": "module",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"]
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/src/common.rs
Original file line number Diff line number Diff line change
@@ -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<Client> = Lazy::new(Client::new);
pub static PIN: Lazy<Arc<AtomicBool>> = Lazy::new(|| Arc::new(AtomicBool::new(false)));
pub static OLD_CONTENT: Lazy<Arc<RwLock<String>>> =
Lazy::new(|| Arc::new(RwLock::new(String::new())));

pub async fn init() {
config::load();
Expand Down
6 changes: 5 additions & 1 deletion src-tauri/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ pub fn handler(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
// 监听快捷键
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")
}
});

Expand Down
18 changes: 18 additions & 0 deletions src-tauri/src/shortcut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use tauri::WebviewWindow;
use selection::get_text;

use crate::clip;
use crate::common::OLD_CONTENT;
use crate::util;

/// 鼠标坐标与选中内容
///
Expand All @@ -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() {
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn menu(handle: &AppHandle) -> Result<Menu<Wry>> {
.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.");
Expand Down
17 changes: 17 additions & 0 deletions src-tauri/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::path::PathBuf;

use windows::Win32::UI::WindowsAndMessaging::{
GetForegroundWindow, GetWindowTextLengthW, GetWindowTextW,
};

/// 获取可执行文件位置
///
/// Get executable file path
Expand All @@ -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<u16> = vec![0; length + 1];
GetWindowTextW(window, &mut buffer);
String::from_utf16_lossy(&buffer)
}
}
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"productName": "Tran",
"version": "0.2.9",
"version": "0.2.10",
"identifier": "com.borber.tran",
"build": {
"beforeDevCommand": "pnpm dev",
Expand Down
13 changes: 10 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const App = () => {
const panel = getCurrent()
const [result, Result] = createSignal<TransVO>()
const [update, Update] = createSignal(false)
let pin = false

onMount(async () => {
// 监听事件, 显示panel
Expand All @@ -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(
Expand Down Expand Up @@ -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")
}
)
})
Expand All @@ -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)
}
}}
>
<Switch>
Expand Down

0 comments on commit 8ef9c37

Please sign in to comment.