Skip to content

Commit

Permalink
Fix problems caused by using tauri and rdev together
Browse files Browse the repository at this point in the history
  • Loading branch information
Borber committed Jan 26, 2024
1 parent d3c7e7a commit 03aa10b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 122 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

这是 Tran 的一次重大更新, 在经过艰难的抉择和现实的毒打之后 Tran 决定将快捷键 由 `Alt + X` 变更为 双击 `CapsLock`. 我知道这可能会一些非常出色的工具产生不可调和的快捷键冲突, 在此提前抱歉.

- 删除全局快捷键, 改为监听 双击 `CapsLock`
- 更新依赖
- 删除全局快捷键, 改为监听 双击 `CapsLock` 键, 软件体积将进一步减小。
- 去除一个镜像, 在使用率不高的情况下, 多一个镜像反而导致 Tran 的表现不佳, 将其移入 backup 以待后续启用.
- 当前情况下, 理论上比 0.1.11 会更快, 请提 issue 反馈你的体验
- 更新依赖

## [0.1.11]

Expand Down
111 changes: 6 additions & 105 deletions src-tauri/Cargo.lock

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

2 changes: 2 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ fastrand = "2"
open = "5"

rdev = "0.5"
crossbeam-channel = "0.5"


[features]
custom-protocol = ["tauri/custom-protocol"]
Expand Down
33 changes: 18 additions & 15 deletions src-tauri/src/setup.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::{
error::Error,
sync::{
atomic::{AtomicU32, AtomicU64},
Arc,
},
sync::{atomic::AtomicU64, Arc},
time::SystemTime,
};

use crossbeam_channel::bounded;
use rdev::{listen, EventType::KeyRelease, Key::CapsLock};
use tauri::{App, Manager};

Expand All @@ -18,30 +16,35 @@ pub fn handler(app: &mut App) -> Result<(), Box<dyn Error>> {
};
let panel = app.get_window("panel").expect("Failed to get panel window");

let sec = Arc::new(AtomicU64::new(0));
let milli = Arc::new(AtomicU32::new(0));
let cap = Arc::new(AtomicU64::new(0));
let (s, r) = bounded(1);

std::thread::spawn(move || {
while let Ok(()) = r.recv() {
shortcut::show(&panel).expect("Shortcut key call failed")
}
});

std::thread::spawn(|| {
listen(move |event| {
if let KeyRelease(CapsLock) = event.event_type {
let old_sec = sec.load(std::sync::atomic::Ordering::SeqCst);
let old_milli = milli.load(std::sync::atomic::Ordering::SeqCst);
let old = cap.load(std::sync::atomic::Ordering::SeqCst);

let now = SystemTime::now();
let timestamp = now
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Time went backwards");
let now_sec = timestamp.as_secs();
let now_milli = timestamp.subsec_millis();
let now = timestamp.as_millis() as u64;

if now_sec == old_sec || now_sec == old_sec + 1 && now_milli < old_milli {
shortcut::show(&panel).expect("Shortcut key call failed")
if now < old + 1000 {
s.send(()).expect("Channel send failed");
cap.store(0, std::sync::atomic::Ordering::SeqCst);
} else {
cap.store(now, std::sync::atomic::Ordering::SeqCst);
}

sec.store(now_sec, std::sync::atomic::Ordering::SeqCst);
milli.store(now_milli, std::sync::atomic::Ordering::SeqCst);
}
})
});

Ok(())
}

0 comments on commit 03aa10b

Please sign in to comment.