Skip to content

Commit

Permalink
New version: 0.2.13
Browse files Browse the repository at this point in the history
  • Loading branch information
Borber committed Apr 21, 2024
1 parent cc2ea44 commit 45ef10b
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 43 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### 更新

- 拖拽将不再 fallback 到系统剪贴板, 此更改将导致在不支持 模拟复制 的软件中, 你需要手动复制再调用 Tran. 这也许会带来一些问题. 但是目前拖拽导致的 fallback 造成了一些困扰
- 为后续自定义按键做准备
- 修改检测逻辑, 仅在两次快速按下按键时才触发, 长按后按键不再触发,
- 在获取选中文本时, 将关闭按键监听, 避免模拟操作触发快捷键
- 调整代码, 让整体逻辑更加清晰.
- 速度进一步提升.
- 修复 tauri v2 依赖问题.
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.12",
"version": "0.2.13",
"description": "简洁, 快速, 划词翻译",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

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

2 changes: 1 addition & 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.12"
version = "0.2.13"
description = "Tran translate the selected words"
authors = ["Borber"]
license = "GPLv3"
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub static CLIENT: Lazy<Client> = Lazy::new(Client::new);
pub static PIN: Lazy<Arc<AtomicBool>> = Lazy::new(|| Arc::new(AtomicBool::new(false)));
pub static TMP_PIN: Lazy<Arc<AtomicBool>> = Lazy::new(|| Arc::new(AtomicBool::new(false)));
pub static OLD: Lazy<Arc<RwLock<String>>> = Lazy::new(|| Arc::new(RwLock::new(String::new())));
pub static SIMULATION: Lazy<Arc<AtomicBool>> = Lazy::new(|| Arc::new(AtomicBool::new(false)));

pub async fn init() {
config::load();
Expand Down
94 changes: 59 additions & 35 deletions src-tauri/src/setup.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down Expand Up @@ -78,54 +77,85 @@ pub fn handler(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {

// 监听快捷键 和 鼠标操作
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(_) => (),
Expand All @@ -137,7 +167,7 @@ pub fn handler(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
}

// 检测双击
let old = double_click_cap;
let old = double_click;
let x = double_click_x;
let y = double_click_y;

Expand All @@ -154,7 +184,7 @@ pub fn handler(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
}
}
} else {
double_click_cap = now;
double_click = now;
double_click_x = x1;
double_click_y = y1;
}
Expand All @@ -163,12 +193,6 @@ pub fn handler(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
};
}
}
KeyRelease(_) => {
// 仅处理连续双击按键的情况, 时间满足但中间若有其他按键按下则忽略
// Only handle continuous double clicks
double_cap = 0;
}

_ => (),
})
});
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.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.");
Expand Down
21 changes: 19 additions & 2 deletions src-tauri/src/util.rs
Original file line number Diff line number Diff line change
@@ -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 {
// 获取系统剪贴板内容
Expand All @@ -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
Expand Down
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.12",
"version": "0.2.13",
"identifier": "com.borber.tran",
"build": {
"beforeDevCommand": "pnpm dev",
Expand Down
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
)
})
Expand Down

0 comments on commit 45ef10b

Please sign in to comment.