fix: add error handling and logging for shortcut registration failures, remove windows shortcut support
This commit is contained in:
parent
2a674c169e
commit
243065221d
@ -1,5 +1,6 @@
|
||||
import { Shortcut } from '@types'
|
||||
import { BrowserWindow, globalShortcut } from 'electron'
|
||||
import Logger from 'electron-log'
|
||||
|
||||
import { configManager } from './ConfigManager'
|
||||
|
||||
@ -55,40 +56,55 @@ export function registerShortcuts(window: BrowserWindow) {
|
||||
if (!shortcuts) return
|
||||
|
||||
shortcuts.forEach((shortcut) => {
|
||||
if (shortcut.shortcut.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const handler = getShortcutHandler(shortcut)
|
||||
|
||||
if (!handler) {
|
||||
return
|
||||
}
|
||||
|
||||
const accelerator = formatShortcutKey(shortcut.shortcut)
|
||||
|
||||
if (shortcut.key === 'show_app') {
|
||||
showAppAccelerator = accelerator
|
||||
}
|
||||
|
||||
if (shortcut.key.includes('zoom')) {
|
||||
switch (shortcut.key) {
|
||||
case 'zoom_in':
|
||||
globalShortcut.register('CommandOrControl+=', () => shortcut.enabled && handler(window))
|
||||
globalShortcut.register('CommandOrControl+numadd', () => shortcut.enabled && handler(window))
|
||||
return
|
||||
case 'zoom_out':
|
||||
globalShortcut.register('CommandOrControl+-', () => shortcut.enabled && handler(window))
|
||||
globalShortcut.register('CommandOrControl+numsub', () => shortcut.enabled && handler(window))
|
||||
return
|
||||
case 'zoom_reset':
|
||||
globalShortcut.register('CommandOrControl+0', () => shortcut.enabled && handler(window))
|
||||
return
|
||||
try {
|
||||
if (shortcut.shortcut.length === 0) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (shortcut.enabled) {
|
||||
globalShortcut.register(accelerator, () => handler(window))
|
||||
const handler = getShortcutHandler(shortcut)
|
||||
if (!handler) {
|
||||
return
|
||||
}
|
||||
|
||||
const accelerator = formatShortcutKey(shortcut.shortcut)
|
||||
|
||||
if (shortcut.key === 'show_app') {
|
||||
showAppAccelerator = accelerator
|
||||
}
|
||||
|
||||
if (shortcut.key.includes('zoom')) {
|
||||
switch (shortcut.key) {
|
||||
case 'zoom_in':
|
||||
try {
|
||||
globalShortcut.register('CommandOrControl+=', () => shortcut.enabled && handler(window))
|
||||
globalShortcut.register('CommandOrControl+numadd', () => shortcut.enabled && handler(window))
|
||||
} catch (error) {
|
||||
Logger.error('[ShortcutService] Failed to register zoom in shortcuts:', error)
|
||||
}
|
||||
return
|
||||
case 'zoom_out':
|
||||
try {
|
||||
globalShortcut.register('CommandOrControl+-', () => shortcut.enabled && handler(window))
|
||||
globalShortcut.register('CommandOrControl+numsub', () => shortcut.enabled && handler(window))
|
||||
} catch (error) {
|
||||
Logger.error('[ShortcutService] Failed to register zoom out shortcuts:', error)
|
||||
}
|
||||
return
|
||||
case 'zoom_reset':
|
||||
try {
|
||||
globalShortcut.register('CommandOrControl+0', () => shortcut.enabled && handler(window))
|
||||
} catch (error) {
|
||||
Logger.error('[ShortcutService] Failed to register zoom reset shortcut:', error)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (shortcut.enabled) {
|
||||
globalShortcut.register(accelerator, () => handler(window))
|
||||
}
|
||||
} catch (error) {
|
||||
Logger.error(`[ShortcutService] Failed to register shortcut ${shortcut.key}:`, error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,10 +1,9 @@
|
||||
import { isMac, isWindows } from '@renderer/config/constant'
|
||||
import { isMac } from '@renderer/config/constant'
|
||||
import { isLocalAi } from '@renderer/config/env'
|
||||
import db from '@renderer/databases'
|
||||
import i18n from '@renderer/i18n'
|
||||
import { useAppDispatch } from '@renderer/store'
|
||||
import { setAvatar, setFilesPath } from '@renderer/store/runtime'
|
||||
import { updateShortcut } from '@renderer/store/shortcuts'
|
||||
import { runAsyncFunction } from '@renderer/utils'
|
||||
import { useLiveQuery } from 'dexie-react-hooks'
|
||||
import { useEffect } from 'react'
|
||||
@ -12,7 +11,6 @@ import { useEffect } from 'react'
|
||||
import { useDefaultModel } from './useAssistant'
|
||||
import { useRuntime } from './useRuntime'
|
||||
import { useSettings } from './useSettings'
|
||||
import { useShortcuts } from './useShortcuts'
|
||||
|
||||
export function useAppInit() {
|
||||
const dispatch = useAppDispatch()
|
||||
@ -20,7 +18,6 @@ export function useAppInit() {
|
||||
const { minappShow } = useRuntime()
|
||||
const { setDefaultModel, setTopicNamingModel, setTranslateModel } = useDefaultModel()
|
||||
const avatar = useLiveQuery(() => db.settings.get('image://avatar'))
|
||||
const { shortcuts } = useShortcuts()
|
||||
|
||||
useEffect(() => {
|
||||
avatar?.value && dispatch(setAvatar(avatar.value))
|
||||
@ -72,15 +69,4 @@ export function useAppInit() {
|
||||
dispatch(setFilesPath(info.filesPath))
|
||||
})
|
||||
}, [dispatch])
|
||||
|
||||
useEffect(() => {
|
||||
if (isWindows) {
|
||||
shortcuts.forEach((shortcut) => {
|
||||
if (shortcut.shortcut[0] === 'Command') {
|
||||
shortcut.shortcut[0] = 'Ctrl'
|
||||
dispatch(updateShortcut(shortcut))
|
||||
}
|
||||
})
|
||||
}
|
||||
}, [dispatch, shortcuts])
|
||||
}
|
||||
|
||||
@ -101,7 +101,6 @@ const ShortcutSettings: FC = () => {
|
||||
}
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent, record: Shortcut) => {
|
||||
console.debug('handleKeyDown', e, record)
|
||||
e.preventDefault()
|
||||
|
||||
const keys: string[] = []
|
||||
@ -112,9 +111,7 @@ const ShortcutSettings: FC = () => {
|
||||
|
||||
const key = e.key
|
||||
|
||||
console.debug('key', key)
|
||||
|
||||
if (!['Control', 'Alt', 'Shift', 'Meta'].includes(key)) {
|
||||
if (!['Control', 'Alt', 'Shift', 'Meta', 'Process'].includes(key) && key.length === 1) {
|
||||
keys.push(key.toUpperCase())
|
||||
}
|
||||
|
||||
@ -126,12 +123,7 @@ const ShortcutSettings: FC = () => {
|
||||
return
|
||||
}
|
||||
|
||||
dispatch(
|
||||
updateShortcut({
|
||||
...record,
|
||||
shortcut: keys
|
||||
})
|
||||
)
|
||||
dispatch(updateShortcut({ ...record, shortcut: keys }))
|
||||
setEditingKey(null)
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user