From 11d04048232e307332c233fe88d3bf65464c9314 Mon Sep 17 00:00:00 2001 From: Alexander Drozdov Date: Fri, 17 Sep 2021 14:45:21 +0300 Subject: [PATCH] move hotkey validation to main, use log for errors --- src/ipc/KeyToCode.ts | 7 ---- src/ipc/ipc-event.ts | 7 ---- src/ipc/main-process-bindings.ts | 9 ++--- src/main/config.ts | 9 ++--- src/main/shortcuts.ts | 47 +++++++++++++++++++++------ src/web/Config.ts | 6 +--- src/web/settings/HotkeyInput.vue | 21 +++++------- src/web/settings/hotkeys.vue | 56 ++------------------------------ 8 files changed, 54 insertions(+), 108 deletions(-) diff --git a/src/ipc/KeyToCode.ts b/src/ipc/KeyToCode.ts index 8b6c8c00..35d78abb 100644 --- a/src/ipc/KeyToCode.ts +++ b/src/ipc/KeyToCode.ts @@ -222,13 +222,6 @@ export const KeyToElectron = { Shift: 'Shift' } -export const forbidden = [ - 'Ctrl + C', 'Ctrl + V', 'Ctrl + A', 'Ctrl + F', 'Ctrl + Enter', - 'Home', 'Delete', 'Enter', - 'ArrowUp', 'ArrowRight', 'ArrowLeft' -] -export const forbiddenCtrl = ['C', 'V', 'A', 'F', 'Enter'] - export function hotkeyToString (keys: string[], ctrl = false, shift = false, alt = false): string { if (keys.includes('Ctrl')) ctrl = true if (keys.includes('Shift')) shift = true diff --git a/src/ipc/ipc-event.ts b/src/ipc/ipc-event.ts index c80bc3ae..8ec08266 100644 --- a/src/ipc/ipc-event.ts +++ b/src/ipc/ipc-event.ts @@ -1,12 +1,5 @@ -import type { Config as AppConfig } from '@/ipc/types' -import type { GameConfig } from '@/main/game-config' - export const GET_CONFIG = 'get-config' export const PUSH_CONFIG = 'push-config' -export interface IpcConfigs { - app: AppConfig - game: GameConfig | null -} export const PRICE_CHECK_HIDE = 'OVERLAY->MAIN::price-check-hide' diff --git a/src/ipc/main-process-bindings.ts b/src/ipc/main-process-bindings.ts index f82fd97f..eeff635e 100644 --- a/src/ipc/main-process-bindings.ts +++ b/src/ipc/main-process-bindings.ts @@ -98,16 +98,11 @@ class MainProcessBinding extends EventTarget { } } - getConfig (): ipcEvent.IpcConfigs { + getConfig (): Config { if (electron) { return electron.ipcRenderer.sendSync(ipcEvent.GET_CONFIG) } else { - return { - app: defaultConfig, - game: { - highlightKey: 'Alt' - } - } + return defaultConfig } } diff --git a/src/main/config.ts b/src/main/config.ts index 5d7ad08b..1f514eb1 100644 --- a/src/main/config.ts +++ b/src/main/config.ts @@ -2,19 +2,16 @@ import Store from 'electron-store' import { dialog, ipcMain, app } from 'electron' import isDeepEq from 'fast-deep-equal' import { Config, defaultConfig } from '@/ipc/types' -import { GET_CONFIG, PUSH_CONFIG, CLOSE_SETTINGS_WINDOW, IpcConfigs } from '@/ipc/ipc-event' +import { GET_CONFIG, PUSH_CONFIG, CLOSE_SETTINGS_WINDOW } from '@/ipc/ipc-event' import { overlayWindow } from './overlay-window' import { logger } from './logger' import { LogWatcher } from './LogWatcher' import { ItemCheckWidget } from '@/web/overlay/interfaces' -import { readConfig as readGameConfig, loadAndCache as loadAndCacheGameCfg } from './game-config' +import { loadAndCache as loadAndCacheGameCfg } from './game-config' export function setupConfigEvents () { ipcMain.on(GET_CONFIG, (e) => { - e.returnValue = { - app: config.store, - game: readGameConfig() - } as IpcConfigs + e.returnValue = config.store }) ipcMain.on(PUSH_CONFIG, (e, cfg: Config) => { batchUpdateConfig(cfg, false) diff --git a/src/main/shortcuts.ts b/src/main/shortcuts.ts index 37b111fd..32918a3e 100644 --- a/src/main/shortcuts.ts +++ b/src/main/shortcuts.ts @@ -35,8 +35,8 @@ export interface ShortcutAction { } } -function shortcutsfromConfig () { - const actions: ShortcutAction[] = [] +function shortcutsFromConfig () { + let actions: ShortcutAction[] = [] const priceCheckCfg = priceCheckConfig() if (priceCheckCfg.hotkey) { @@ -90,21 +90,48 @@ function shortcutsfromConfig () { }) } } + const copyItemShortcut = mergeTwoHotkeys('Ctrl + C', gameConfig?.highlightKey || 'Alt') + if (copyItemShortcut !== 'Ctrl + C') { + actions.push({ + shortcut: copyItemShortcut, + action: { type: 'test-only' } + }) + } + { - const copyItemShortcut = mergeTwoHotkeys('Ctrl + C', gameConfig?.highlightKey || 'Alt') - if (copyItemShortcut !== 'Ctrl + C') { - actions.push({ - shortcut: copyItemShortcut, - action: { type: 'test-only' } - }) + const allShortcuts = new Set([ + 'Ctrl + C', 'Ctrl + V', 'Ctrl + A', + 'Ctrl + F', + 'Ctrl + Enter', + 'Home', 'Delete', 'Enter', + 'ArrowUp', 'ArrowRight', 'ArrowLeft', + copyItemShortcut + ]) + + for (const action of actions) { + if (allShortcuts.has(action.shortcut) && action.action.type !== 'test-only') { + logger.error('Hotkey reserved by the game will not be registered.', { source: 'shortcuts', shortcut: action.shortcut }) + } } + actions = actions.filter(action => !allShortcuts.has(action.shortcut)) + + const duplicates = new Set() + for (const action of actions) { + if (allShortcuts.has(action.shortcut)) { + logger.error('It is not possible to use the same hotkey for multiple actions.', { source: 'shortcuts', shortcut: action.shortcut }) + duplicates.add(action.shortcut) + } else { + allShortcuts.add(action.shortcut) + } + } + actions = actions.filter(action => !duplicates.has(action.shortcut)) } return actions } function registerGlobal () { - const toRegister = shortcutsfromConfig() + const toRegister = shortcutsFromConfig() for (const entry of toRegister) { const isOk = globalShortcut.register(shortcutToElectron(entry.shortcut), () => { if (entry.keepModKeys) { @@ -149,7 +176,7 @@ function registerGlobal () { }) if (!isOk) { - logger.error('Cannot register shortcut, because it is already registered by another application.', { source: 'shortcuts', shortcut: entry.shortcut }) + logger.error('Failed to register a shortcut. It is already registered by another application.', { source: 'shortcuts', shortcut: entry.shortcut }) } if (entry.action.type === 'test-only') { diff --git a/src/web/Config.ts b/src/web/Config.ts index 9cb2e88e..a28b7e5d 100644 --- a/src/web/Config.ts +++ b/src/web/Config.ts @@ -1,18 +1,14 @@ import { reactive } from 'vue' import { MainProcess } from '@/ipc/main-process-bindings' import type { Config as ConfigType } from '@/ipc/types' -import type { GameConfig } from '@/main/game-config' import type { PriceCheckWidget } from './overlay/interfaces' import { PUSH_CONFIG } from '@/ipc/ipc-event' class ConfigService { store: ConfigType - gameConfig: GameConfig | null constructor () { - const configs = MainProcess.getConfig() - this.store = reactive(configs.app) - this.gameConfig = configs.game + this.store = reactive(MainProcess.getConfig()) MainProcess.addEventListener(PUSH_CONFIG, (e) => { const config = (e as CustomEvent).detail diff --git a/src/web/settings/HotkeyInput.vue b/src/web/settings/HotkeyInput.vue index f3f42c72..39280338 100644 --- a/src/web/settings/HotkeyInput.vue +++ b/src/web/settings/HotkeyInput.vue @@ -10,7 +10,7 @@