fix: check hotkey and optimize hotkey input, close #287
This commit is contained in:
parent
6bc83d9f27
commit
db028665fd
1
src-tauri/Cargo.lock
generated
1
src-tauri/Cargo.lock
generated
@ -530,6 +530,7 @@ dependencies = [
|
|||||||
"sysproxy",
|
"sysproxy",
|
||||||
"tauri",
|
"tauri",
|
||||||
"tauri-build",
|
"tauri-build",
|
||||||
|
"tauri-runtime-wry",
|
||||||
"tokio",
|
"tokio",
|
||||||
"warp",
|
"warp",
|
||||||
"which 4.3.0",
|
"which 4.3.0",
|
||||||
|
@ -26,6 +26,7 @@ nanoid = "0.4.0"
|
|||||||
chrono = "0.4.19"
|
chrono = "0.4.19"
|
||||||
sysinfo = "0.26.2"
|
sysinfo = "0.26.2"
|
||||||
sysproxy = "0.1"
|
sysproxy = "0.1"
|
||||||
|
rquickjs = "0.1.7"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde_yaml = "0.9"
|
serde_yaml = "0.9"
|
||||||
auto-launch = "0.4"
|
auto-launch = "0.4"
|
||||||
@ -37,9 +38,9 @@ tokio = { version = "1", features = ["full"] }
|
|||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
reqwest = { version = "0.11", features = ["json"] }
|
reqwest = { version = "0.11", features = ["json"] }
|
||||||
tauri = { version = "1.1.1", features = ["global-shortcut-all", "process-all", "shell-all", "system-tray", "updater", "window-all"] }
|
tauri = { version = "1.1.1", features = ["global-shortcut-all", "process-all", "shell-all", "system-tray", "updater", "window-all"] }
|
||||||
rquickjs = { version = "0.1.7" }
|
tauri-runtime-wry = { version = "0.12" }
|
||||||
window-shadows = { version = "0.2.0" }
|
|
||||||
window-vibrancy = { version = "0.3.0" }
|
window-vibrancy = { version = "0.3.0" }
|
||||||
|
window-shadows = { version = "0.2.0" }
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
runas = "0.2.1"
|
runas = "0.2.1"
|
||||||
|
@ -4,6 +4,7 @@ use once_cell::sync::OnceCell;
|
|||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::{collections::HashMap, sync::Arc};
|
||||||
use tauri::{AppHandle, GlobalShortcutManager};
|
use tauri::{AppHandle, GlobalShortcutManager};
|
||||||
|
use tauri_runtime_wry::wry::application::accelerator::Accelerator;
|
||||||
|
|
||||||
pub struct Hotkey {
|
pub struct Hotkey {
|
||||||
current: Arc<Mutex<Vec<String>>>, // 保存当前的热键设置
|
current: Arc<Mutex<Vec<String>>>, // 保存当前的热键设置
|
||||||
@ -32,10 +33,15 @@ impl Hotkey {
|
|||||||
let func = iter.next();
|
let func = iter.next();
|
||||||
let key = iter.next();
|
let key = iter.next();
|
||||||
|
|
||||||
if func.is_some() && key.is_some() {
|
match (key, func) {
|
||||||
log_err!(self.register(key.unwrap(), func.unwrap()));
|
(Some(key), Some(func)) => {
|
||||||
} else {
|
log_err!(Self::check_key(key).and_then(|_| self.register(key, func)));
|
||||||
log::error!(target: "app", "invalid hotkey \"{}\":\"{}\"", key.unwrap_or("None"), func.unwrap_or("None"));
|
}
|
||||||
|
_ => {
|
||||||
|
let key = key.unwrap_or("None");
|
||||||
|
let func = func.unwrap_or("None");
|
||||||
|
log::error!(target: "app", "invalid hotkey `{key}`:`{func}`");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*self.current.lock() = hotkeys.clone();
|
*self.current.lock() = hotkeys.clone();
|
||||||
@ -44,10 +50,20 @@ impl Hotkey {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 检查一个键是否合法
|
||||||
|
fn check_key(hotkey: &str) -> Result<()> {
|
||||||
|
// fix #287
|
||||||
|
// tauri的这几个方法全部有Result expect,会panic,先检测一遍避免挂了
|
||||||
|
if hotkey.parse::<Accelerator>().is_err() {
|
||||||
|
bail!("invalid hotkey `{hotkey}`");
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn get_manager(&self) -> Result<impl GlobalShortcutManager> {
|
fn get_manager(&self) -> Result<impl GlobalShortcutManager> {
|
||||||
let app_handle = self.app_handle.lock();
|
let app_handle = self.app_handle.lock();
|
||||||
if app_handle.is_none() {
|
if app_handle.is_none() {
|
||||||
bail!("failed to get hotkey manager");
|
bail!("failed to get the hotkey manager");
|
||||||
}
|
}
|
||||||
Ok(app_handle.as_ref().unwrap().global_shortcut_manager())
|
Ok(app_handle.as_ref().unwrap().global_shortcut_manager())
|
||||||
}
|
}
|
||||||
@ -92,6 +108,11 @@ impl Hotkey {
|
|||||||
|
|
||||||
let (del, add) = Self::get_diff(old_map, new_map);
|
let (del, add) = Self::get_diff(old_map, new_map);
|
||||||
|
|
||||||
|
// 先检查一遍所有新的热键是不是可以用的
|
||||||
|
for (hotkey, _) in add.iter() {
|
||||||
|
Self::check_key(hotkey)?;
|
||||||
|
}
|
||||||
|
|
||||||
del.iter().for_each(|key| {
|
del.iter().for_each(|key| {
|
||||||
let _ = self.unregister(key);
|
let _ = self.unregister(key);
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
|
import { useRef, useState } from "react";
|
||||||
import { alpha, Box, IconButton, styled } from "@mui/material";
|
import { alpha, Box, IconButton, styled } from "@mui/material";
|
||||||
import { DeleteRounded } from "@mui/icons-material";
|
import { DeleteRounded } from "@mui/icons-material";
|
||||||
import parseHotkey from "@/utils/parse-hotkey";
|
import { parseHotkey } from "@/utils/parse-hotkey";
|
||||||
|
|
||||||
const KeyWrapper = styled("div")(({ theme }) => ({
|
const KeyWrapper = styled("div")(({ theme }) => ({
|
||||||
position: "relative",
|
position: "relative",
|
||||||
@ -54,10 +55,20 @@ interface Props {
|
|||||||
export const HotkeyInput = (props: Props) => {
|
export const HotkeyInput = (props: Props) => {
|
||||||
const { value, onChange } = props;
|
const { value, onChange } = props;
|
||||||
|
|
||||||
|
const changeRef = useRef<string[]>([]);
|
||||||
|
const [keys, setKeys] = useState(value);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box sx={{ display: "flex", alignItems: "center" }}>
|
<Box sx={{ display: "flex", alignItems: "center" }}>
|
||||||
<KeyWrapper>
|
<KeyWrapper>
|
||||||
<input
|
<input
|
||||||
|
onKeyUp={() => {
|
||||||
|
const ret = changeRef.current.slice();
|
||||||
|
if (ret.length) {
|
||||||
|
onChange(ret);
|
||||||
|
changeRef.current = [];
|
||||||
|
}
|
||||||
|
}}
|
||||||
onKeyDown={(e) => {
|
onKeyDown={(e) => {
|
||||||
const evt = e.nativeEvent;
|
const evt = e.nativeEvent;
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@ -66,13 +77,13 @@ export const HotkeyInput = (props: Props) => {
|
|||||||
const key = parseHotkey(evt.key);
|
const key = parseHotkey(evt.key);
|
||||||
if (key === "UNIDENTIFIED") return;
|
if (key === "UNIDENTIFIED") return;
|
||||||
|
|
||||||
const newList = [...new Set([...value, key])];
|
changeRef.current = [...new Set([...changeRef.current, key])];
|
||||||
onChange(newList);
|
setKeys(changeRef.current);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className="list">
|
<div className="list">
|
||||||
{value.map((key) => (
|
{keys.map((key) => (
|
||||||
<div key={key} className="item">
|
<div key={key} className="item">
|
||||||
{key}
|
{key}
|
||||||
</div>
|
</div>
|
||||||
@ -84,7 +95,10 @@ export const HotkeyInput = (props: Props) => {
|
|||||||
size="small"
|
size="small"
|
||||||
title="Delete"
|
title="Delete"
|
||||||
color="inherit"
|
color="inherit"
|
||||||
onClick={() => onChange([])}
|
onClick={() => {
|
||||||
|
onChange([]);
|
||||||
|
setKeys([]);
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<DeleteRounded fontSize="inherit" />
|
<DeleteRounded fontSize="inherit" />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
|
@ -73,7 +73,7 @@ export const HotkeyViewer = forwardRef<DialogRef>((props, ref) => {
|
|||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
patchVerge({ hotkeys });
|
await patchVerge({ hotkeys });
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
Notice.error(err.message || err.toString());
|
Notice.error(err.message || err.toString());
|
||||||
|
@ -1,4 +1,26 @@
|
|||||||
const parseHotkey = (key: string) => {
|
const KEY_MAP: Record<string, string> = {
|
||||||
|
'"': "'",
|
||||||
|
":": ";",
|
||||||
|
"?": "/",
|
||||||
|
">": ".",
|
||||||
|
"<": ",",
|
||||||
|
"{": "[",
|
||||||
|
"}": "]",
|
||||||
|
"|": "\\",
|
||||||
|
"!": "1",
|
||||||
|
"@": "2",
|
||||||
|
"#": "3",
|
||||||
|
$: "4",
|
||||||
|
"%": "5",
|
||||||
|
"^": "6",
|
||||||
|
"&": "7",
|
||||||
|
"*": "8",
|
||||||
|
"(": "9",
|
||||||
|
")": "0",
|
||||||
|
"~": "`",
|
||||||
|
};
|
||||||
|
|
||||||
|
export const parseHotkey = (key: string) => {
|
||||||
let temp = key.toUpperCase();
|
let temp = key.toUpperCase();
|
||||||
|
|
||||||
if (temp.startsWith("ARROW")) {
|
if (temp.startsWith("ARROW")) {
|
||||||
@ -20,10 +42,7 @@ const parseHotkey = (key: string) => {
|
|||||||
return "CMD";
|
return "CMD";
|
||||||
case " ":
|
case " ":
|
||||||
return "SPACE";
|
return "SPACE";
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return temp;
|
return KEY_MAP[temp] || temp;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default parseHotkey;
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user