Skip to content

Commit ee78e77

Browse files
committed
Make shortcuts to be able to be disabled
1 parent fcb5d3c commit ee78e77

8 files changed

Lines changed: 36 additions & 2 deletions

File tree

src-tauri/src/shortcuts.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub async fn setup_shortcuts<R: Runtime>(
7676
app: AppHandle<R>,
7777
quick_query: String,
7878
new_query: String,
79+
enabled: bool,
7980
) -> Result<(), String> {
8081
use tauri_plugin_global_shortcut::{GlobalShortcutExt, Shortcut, ShortcutState};
8182

@@ -86,6 +87,10 @@ pub async fn setup_shortcuts<R: Runtime>(
8687
.unregister_all()
8788
.map_err(|e| format!("Failed to unregister shortcuts: {}", e))?;
8889

90+
if !enabled {
91+
return Ok(());
92+
}
93+
8994
// Normalize shortcuts to replace "Mod" with platform-specific key
9095
let quick_query_normalized = normalize_shortcut(&quick_query);
9196
let new_query_normalized = normalize_shortcut(&new_query);

src/app/layout.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function AppLayout() {
3131
useGlobalShortcuts({
3232
quickQuery: settings.keyboard.quickQuery,
3333
newQuery: settings.keyboard.newQuery,
34+
enabled: settings.keyboard.enabled,
3435
onQuickQuery: (text) => {
3536
// Ensure we're on the dictionary tab, then run the search.
3637
navigate("/");

src/features/settings/components/keyboard-tab.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Button } from "@/components/ui/button";
22
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
33
import { KbdInput } from "@/components/ui/kbd-input";
44
import { Label } from "@/components/ui/label";
5+
import { Switch } from "@/components/ui/switch";
56
import { useSettings } from "@/features/settings/hooks/use-settings";
67
import { toast } from "sonner";
78
import { useTranslation } from "react-i18next";
@@ -24,10 +25,26 @@ export function KeyboardTab() {
2425
<CardHeader>
2526
<CardTitle>{t("settings.keyboard.shortcuts.title")}</CardTitle>
2627
<CardDescription>
27-
Configure the key combinations for quick interactions.
28+
{t("settings.keyboard.shortcuts.description")}
2829
</CardDescription>
2930
</CardHeader>
3031
<CardContent className="grid gap-4">
32+
<div className="flex items-center justify-between gap-4 rounded-lg border bg-muted/40 px-4 py-3">
33+
<div className="space-y-1">
34+
<p className="text-sm font-medium">
35+
{t("settings.keyboard.shortcuts.enable_label")}
36+
</p>
37+
<p className="text-xs text-muted-foreground">
38+
{t("settings.keyboard.shortcuts.enable_helper")}
39+
</p>
40+
</div>
41+
<Switch
42+
checked={settings.keyboard.enabled}
43+
onCheckedChange={(checked) =>
44+
updateKeyboard({ enabled: checked })
45+
}
46+
/>
47+
</div>
3148
<div className="grid gap-2">
3249
<Label htmlFor="shortcut-quick">{t("settings.keyboard.shortcuts.quick_query")}</Label>
3350
<KbdInput

src/shared/hooks/use-global-shortcuts.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import { listen } from "@tauri-apps/api/event";
55
interface UseGlobalShortcutsOptions {
66
quickQuery: string;
77
newQuery: string;
8+
enabled: boolean;
89
onQuickQuery: (text: string) => void;
910
onNewQuery: () => void;
1011
}
1112

1213
export function useGlobalShortcuts({
1314
quickQuery,
1415
newQuery,
16+
enabled,
1517
onQuickQuery,
1618
onNewQuery,
1719
}: UseGlobalShortcutsOptions) {
@@ -22,14 +24,15 @@ export function useGlobalShortcuts({
2224
await invoke("setup_shortcuts", {
2325
quickQuery,
2426
newQuery,
27+
enabled,
2528
});
2629
} catch (error) {
2730
console.error("Failed to setup shortcuts:", error);
2831
}
2932
};
3033

3134
setupShortcuts();
32-
}, [quickQuery, newQuery]);
35+
}, [quickQuery, newQuery, enabled]);
3336

3437
useEffect(() => {
3538
// Listen for quick-query event (clipboard text)

src/shared/locales/en/translation.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@
299299
"keyboard": {
300300
"shortcuts": {
301301
"title": "Shortcuts",
302+
"description": "Configure global hotkeys for copying selections or opening a fresh query.",
303+
"enable_label": "Enable global shortcuts",
304+
"enable_helper": "When disabled, keyboard shortcuts are ignored until you turn them back on.",
302305
"quick_query": "Quick query",
303306
"new_query": "New query",
304307
"reset": "Reset to defaults"

src/shared/locales/zh/translation.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@
299299
"keyboard": {
300300
"shortcuts": {
301301
"title": "快捷键",
302+
"description": "配置全局快捷键,便于快速复制选中文本或打开新查询。",
303+
"enable_label": "启用全局快捷键",
304+
"enable_helper": "关闭后,按下快捷键不会触发操作,重新开启即可恢复。",
302305
"quick_query": "快速查询",
303306
"new_query": "新查询",
304307
"reset": "重置为默认"

src/shared/state/settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const defaultSettings: AppSettings = {
3030
keyboard: {
3131
quickQuery: "Mod+Enter",
3232
newQuery: "Mod+Shift+K",
33+
enabled: true,
3334
},
3435
about: {
3536
version: "0.1.0",

src/shared/types/settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export type DictionarySettings = {
3131
export type KeyboardShortcutSettings = {
3232
quickQuery: string;
3333
newQuery: string;
34+
enabled: boolean;
3435
};
3536

3637
export type AboutMetadata = {

0 commit comments

Comments
 (0)