Files
PicList/src/main/utils/shortKeyHandler.ts
2019-12-21 17:28:29 +08:00

61 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import bus from './eventBus'
import {
GlobalShortcut
} from 'electron'
let isInModifiedMode = false // 修改快捷键模式
bus.on('toggleShortKeyModifiedMode', flag => {
isInModifiedMode = flag
})
/**
*
* @param {string} name
*/
const shortKeyHandler = (name: string) => {
if (isInModifiedMode) {
return
}
if (name.includes('picgo:')) {
bus.emit(name)
} else if (name.includes('picgo-plugin-')) {
// TODO: 处理插件快捷键
}
}
/**
* 用于更新快捷键绑定
*/
const shortKeyUpdater = (globalShortcut: GlobalShortcut, item: IShortKeyConfig, oldKey: string) => {
// 如果提供了旧key则解绑
if (oldKey) {
globalShortcut.unregister(oldKey)
}
if (item.enable === false) {
globalShortcut.unregister(item.key)
} else {
globalShortcut.register(item.key, () => {
shortKeyHandler(item.name)
})
}
}
// 初始化阶段的注册
const initShortKeyRegister = (globalShortcut: GlobalShortcut, shortKeys: IShortKeyConfig[]) => {
let errorList = []
for (let i in shortKeys) {
try {
if (shortKeys[i].enable) {
globalShortcut.register(shortKeys[i].key, () => {
shortKeyHandler(shortKeys[i].name)
})
}
} catch (e) {
errorList.push(shortKeys[i])
}
}
}
export {
shortKeyUpdater,
initShortKeyRegister
}