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 { Shortcut } from '@types'
|
||||||
import { BrowserWindow, globalShortcut } from 'electron'
|
import { BrowserWindow, globalShortcut } from 'electron'
|
||||||
|
import Logger from 'electron-log'
|
||||||
|
|
||||||
import { configManager } from './ConfigManager'
|
import { configManager } from './ConfigManager'
|
||||||
|
|
||||||
@ -55,40 +56,55 @@ export function registerShortcuts(window: BrowserWindow) {
|
|||||||
if (!shortcuts) return
|
if (!shortcuts) return
|
||||||
|
|
||||||
shortcuts.forEach((shortcut) => {
|
shortcuts.forEach((shortcut) => {
|
||||||
if (shortcut.shortcut.length === 0) {
|
try {
|
||||||
return
|
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
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (shortcut.enabled) {
|
const handler = getShortcutHandler(shortcut)
|
||||||
globalShortcut.register(accelerator, () => handler(window))
|
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 { isLocalAi } from '@renderer/config/env'
|
||||||
import db from '@renderer/databases'
|
import db from '@renderer/databases'
|
||||||
import i18n from '@renderer/i18n'
|
import i18n from '@renderer/i18n'
|
||||||
import { useAppDispatch } from '@renderer/store'
|
import { useAppDispatch } from '@renderer/store'
|
||||||
import { setAvatar, setFilesPath } from '@renderer/store/runtime'
|
import { setAvatar, setFilesPath } from '@renderer/store/runtime'
|
||||||
import { updateShortcut } from '@renderer/store/shortcuts'
|
|
||||||
import { runAsyncFunction } from '@renderer/utils'
|
import { runAsyncFunction } from '@renderer/utils'
|
||||||
import { useLiveQuery } from 'dexie-react-hooks'
|
import { useLiveQuery } from 'dexie-react-hooks'
|
||||||
import { useEffect } from 'react'
|
import { useEffect } from 'react'
|
||||||
@ -12,7 +11,6 @@ import { useEffect } from 'react'
|
|||||||
import { useDefaultModel } from './useAssistant'
|
import { useDefaultModel } from './useAssistant'
|
||||||
import { useRuntime } from './useRuntime'
|
import { useRuntime } from './useRuntime'
|
||||||
import { useSettings } from './useSettings'
|
import { useSettings } from './useSettings'
|
||||||
import { useShortcuts } from './useShortcuts'
|
|
||||||
|
|
||||||
export function useAppInit() {
|
export function useAppInit() {
|
||||||
const dispatch = useAppDispatch()
|
const dispatch = useAppDispatch()
|
||||||
@ -20,7 +18,6 @@ export function useAppInit() {
|
|||||||
const { minappShow } = useRuntime()
|
const { minappShow } = useRuntime()
|
||||||
const { setDefaultModel, setTopicNamingModel, setTranslateModel } = useDefaultModel()
|
const { setDefaultModel, setTopicNamingModel, setTranslateModel } = useDefaultModel()
|
||||||
const avatar = useLiveQuery(() => db.settings.get('image://avatar'))
|
const avatar = useLiveQuery(() => db.settings.get('image://avatar'))
|
||||||
const { shortcuts } = useShortcuts()
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
avatar?.value && dispatch(setAvatar(avatar.value))
|
avatar?.value && dispatch(setAvatar(avatar.value))
|
||||||
@ -72,15 +69,4 @@ export function useAppInit() {
|
|||||||
dispatch(setFilesPath(info.filesPath))
|
dispatch(setFilesPath(info.filesPath))
|
||||||
})
|
})
|
||||||
}, [dispatch])
|
}, [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) => {
|
const handleKeyDown = (e: React.KeyboardEvent, record: Shortcut) => {
|
||||||
console.debug('handleKeyDown', e, record)
|
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
|
||||||
const keys: string[] = []
|
const keys: string[] = []
|
||||||
@ -112,9 +111,7 @@ const ShortcutSettings: FC = () => {
|
|||||||
|
|
||||||
const key = e.key
|
const key = e.key
|
||||||
|
|
||||||
console.debug('key', key)
|
if (!['Control', 'Alt', 'Shift', 'Meta', 'Process'].includes(key) && key.length === 1) {
|
||||||
|
|
||||||
if (!['Control', 'Alt', 'Shift', 'Meta'].includes(key)) {
|
|
||||||
keys.push(key.toUpperCase())
|
keys.push(key.toUpperCase())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,12 +123,7 @@ const ShortcutSettings: FC = () => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(
|
dispatch(updateShortcut({ ...record, shortcut: keys }))
|
||||||
updateShortcut({
|
|
||||||
...record,
|
|
||||||
shortcut: keys
|
|
||||||
})
|
|
||||||
)
|
|
||||||
setEditingKey(null)
|
setEditingKey(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user