|
| 1 | +import { useState, useEffect, useCallback, createContext, useContext } from 'react' |
| 2 | +import { toast } from 'sonner' |
| 3 | +import { Button } from '@/components/ui/button' |
| 4 | +import ShortcutRenderer from '@/components/ShortcutRenderer' |
| 5 | +import { isModifierKey, getShortcutAccelerator } from '@/lib/utils/keyboard' |
| 6 | +import { useShortcutsStore } from '@/lib/store/shortcuts' |
| 7 | + |
| 8 | +const ShortcutsContext = createContext<{ |
| 9 | + recordingAction: string | null |
| 10 | + setRecordingAction: (action: string | null) => void |
| 11 | +}>({ |
| 12 | + recordingAction: null, |
| 13 | + setRecordingAction: () => {} |
| 14 | +}) |
| 15 | + |
| 16 | +export function CustomShortcuts() { |
| 17 | + const { shortcuts, updateShortcut } = useShortcutsStore() |
| 18 | + const [recordingAction, setRecordingAction] = useState<string | null>(null) |
| 19 | + |
| 20 | + const onShortcutChange = useCallback( |
| 21 | + (action: string, key: string) => { |
| 22 | + const newShortcut = { ...shortcuts[action], key } |
| 23 | + updateShortcut(action, newShortcut) |
| 24 | + window.api.updateShortcuts([newShortcut]) |
| 25 | + }, |
| 26 | + [shortcuts, updateShortcut] |
| 27 | + ) |
| 28 | + |
| 29 | + const handleKeyDown = useCallback( |
| 30 | + (e: KeyboardEvent) => { |
| 31 | + if (!recordingAction) return |
| 32 | + |
| 33 | + e.preventDefault() |
| 34 | + |
| 35 | + if (isModifierKey(e.code)) return |
| 36 | + const accelerator = getShortcutAccelerator(e) |
| 37 | + // User press escape to cancel recording. |
| 38 | + if (e.code === 'Escape' && !accelerator) { |
| 39 | + setRecordingAction(null) |
| 40 | + } |
| 41 | + if (!accelerator) return |
| 42 | + onShortcutChange(recordingAction, accelerator) |
| 43 | + setRecordingAction(null) |
| 44 | + }, |
| 45 | + [recordingAction, onShortcutChange] |
| 46 | + ) |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + window.addEventListener('keydown', handleKeyDown) |
| 50 | + return () => { |
| 51 | + window.removeEventListener('keydown', handleKeyDown) |
| 52 | + } |
| 53 | + }, [handleKeyDown]) |
| 54 | + |
| 55 | + return ( |
| 56 | + <ShortcutsContext.Provider value={{ recordingAction, setRecordingAction }}> |
| 57 | + <div className="space-y-4"> |
| 58 | + {/* Window Management */} |
| 59 | + <div className="space-y-2"> |
| 60 | + <h3 className="text-sm text-gray-500">窗口管理</h3> |
| 61 | + <Shortcut label="隐藏/显示窗口" shortcut="hideOrShowMainWindow" /> |
| 62 | + <Shortcut |
| 63 | + label="鼠标穿透" |
| 64 | + description="启用后窗口对鼠标穿透,可以点击窗口背后的内容" |
| 65 | + shortcut="ignoreOrEnableMouse" |
| 66 | + /> |
| 67 | + </div> |
| 68 | + |
| 69 | + {/* Screenshot & AI */} |
| 70 | + <div className="space-y-2"> |
| 71 | + <h3 className="text-sm text-gray-500">截图与AI</h3> |
| 72 | + <Shortcut label="截图" shortcut="takeScreenshot" /> |
| 73 | + </div> |
| 74 | + |
| 75 | + {/* Navigation */} |
| 76 | + <div className="space-y-2"> |
| 77 | + <h3 className="text-sm text-gray-500">页面导航</h3> |
| 78 | + <Shortcut label="向上翻页" shortcut="pageUp" /> |
| 79 | + <Shortcut label="向下翻页" shortcut="pageDown" /> |
| 80 | + </div> |
| 81 | + |
| 82 | + {/* Window Movement */} |
| 83 | + <div className="space-y-2"> |
| 84 | + <h3 className="text-sm text-gray-500">窗口移动</h3> |
| 85 | + <Shortcut label="向上移动窗口" shortcut="moveMainWindowUp" /> |
| 86 | + <Shortcut label="向下移动窗口" shortcut="moveMainWindowDown" /> |
| 87 | + <Shortcut label="向左移动窗口" shortcut="moveMainWindowLeft" /> |
| 88 | + <Shortcut label="向右移动窗口" shortcut="moveMainWindowRight" /> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + </ShortcutsContext.Provider> |
| 92 | + ) |
| 93 | +} |
| 94 | + |
| 95 | +function Shortcut({ |
| 96 | + label, |
| 97 | + description, |
| 98 | + shortcut: shortcutAction |
| 99 | +}: { |
| 100 | + label: string |
| 101 | + description?: string |
| 102 | + shortcut: string |
| 103 | +}) { |
| 104 | + const { shortcuts } = useShortcutsStore() |
| 105 | + const { recordingAction, setRecordingAction } = useContext(ShortcutsContext) |
| 106 | + const shortcut = shortcuts[shortcutAction] |
| 107 | + const isRecording = recordingAction === shortcutAction |
| 108 | + |
| 109 | + return shortcut ? ( |
| 110 | + <div className="flex items-center justify-between"> |
| 111 | + <div className="flex gap-2 items-center"> |
| 112 | + <label className="text-sm font-medium">{label}</label> |
| 113 | + {description && <p className="text-xs font-light">{description}</p>} |
| 114 | + </div> |
| 115 | + <span |
| 116 | + className="cursor-pointer" |
| 117 | + onClick={() => setRecordingAction(isRecording ? null : shortcutAction)} |
| 118 | + > |
| 119 | + {!isRecording ? ( |
| 120 | + <ShortcutRenderer shortcut={shortcut.key} /> |
| 121 | + ) : ( |
| 122 | + <span className="font-mono text-sm align-middle rounded-md pl-2 pr-1 py-1 transition-colors bg-gray-200 animate-pulse"> |
| 123 | + 请按下自定义快捷键... |
| 124 | + </span> |
| 125 | + )} |
| 126 | + </span> |
| 127 | + </div> |
| 128 | + ) : null |
| 129 | +} |
| 130 | + |
| 131 | +export function ResetDefaultShortcuts() { |
| 132 | + const { shortcuts, resetShortcuts } = useShortcutsStore() |
| 133 | + return ( |
| 134 | + <Button |
| 135 | + variant="outline" |
| 136 | + size="sm" |
| 137 | + className="ml-auto" |
| 138 | + onClick={async () => { |
| 139 | + await window.api.updateShortcuts( |
| 140 | + Object.values(shortcuts) |
| 141 | + .filter(({ key, defaultKey }) => key !== defaultKey) |
| 142 | + .map((shortcut) => ({ |
| 143 | + ...shortcut, |
| 144 | + key: shortcut.defaultKey |
| 145 | + })) |
| 146 | + ) |
| 147 | + resetShortcuts() |
| 148 | + toast.success('重置默认快捷键成功') |
| 149 | + }} |
| 150 | + > |
| 151 | + 重置默认快捷键 |
| 152 | + </Button> |
| 153 | + ) |
| 154 | +} |
| 155 | + |
| 156 | + |
0 commit comments