Skip to content

Commit

Permalink
New version: 0.2.14
Browse files Browse the repository at this point in the history
  • Loading branch information
Borber committed Apr 26, 2024
1 parent ede0516 commit ac3af37
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 67 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.14]

### 更新

- 支持选择快捷键, 在 `shift` `ctrl` `capslock` 中进行选择
- 更新依赖

### 预告

- 下一项添加, 暗, 亮, 以及匹配系统主题

## [0.2.13]

### 更新
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.13",
"version": "0.2.14",
"description": "简洁, 快速, 划词翻译",
"type": "module",
"scripts": {
Expand Down
65 changes: 28 additions & 37 deletions 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.13"
version = "0.2.14"
description = "Tran translate the selected words"
authors = ["Borber"]
license = "GPLv3"
Expand Down
27 changes: 24 additions & 3 deletions src-tauri/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::sync::{
atomic::{AtomicBool, Ordering},
atomic::{AtomicBool, AtomicU8, Ordering},
Arc,
};

Expand All @@ -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<Arc<AtomicBool>> = Lazy::new(|| Arc::new(AtomicBool::new(true)));

/// 快捷键配置
///
/// Key config
pub static KEY: Lazy<Arc<AtomicU8>> = Lazy::new(|| Arc::new(AtomicU8::new(0)));

/// 读取配置
///
/// read config
Expand All @@ -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>(&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);
}
}

Expand All @@ -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");
Expand All @@ -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");
}
8 changes: 4 additions & 4 deletions src-tauri/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use mouse_position::mouse_position::Mouse;
use rdev::{
Button,
EventType::{ButtonPress, ButtonRelease, KeyPress, KeyRelease},
Key,
};
use tauri::{App, Manager};

Expand Down Expand Up @@ -94,13 +93,14 @@ pub fn handler(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {

// 确定按键
// 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;
}
// 如果在模拟中则忽略
Expand All @@ -117,7 +117,7 @@ pub fn handler(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
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;
Expand Down
42 changes: 24 additions & 18 deletions src-tauri/src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,21 @@ pub fn init(app: &AppHandle) -> Result<()> {
}

fn menu(handle: &AppHandle) -> Result<Menu<Wry>> {
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"))
}

Expand All @@ -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");
}
Expand Down
15 changes: 14 additions & 1 deletion src-tauri/src/util.rs
Original file line number Diff line number Diff line change
@@ -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};

/// 模拟获取复制文本
///
Expand Down Expand Up @@ -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,
}
}
Loading

0 comments on commit ac3af37

Please sign in to comment.