From fe76cafbb97435f2448609d3195b1fa67feba521 Mon Sep 17 00:00:00 2001 From: Alexander Drozdov Date: Tue, 23 Jun 2020 20:37:58 +0300 Subject: [PATCH] update pollclipboard, hotkey blacklist --- src/ipc/ipc-event.ts | 6 ++++ src/ipc/types.ts | 13 ++++++++ src/main/PollClipboard.ts | 34 --------------------- src/main/config.ts | 30 +++++++++++++++--- src/main/poll-clipboard.ts | 51 +++++++++++++++++++++++++++++++ src/main/price-check.ts | 2 +- src/main/shortcuts.ts | 36 +++++++++++++++------- src/web/overlay/OverlayWindow.vue | 4 ++- src/web/settings/HotkeyInput.vue | 7 +++-- src/web/settings/hotkeys.vue | 20 +++++++----- 10 files changed, 141 insertions(+), 62 deletions(-) delete mode 100644 src/main/PollClipboard.ts create mode 100644 src/main/poll-clipboard.ts diff --git a/src/ipc/ipc-event.ts b/src/ipc/ipc-event.ts index 2b162bf5..d18190f0 100644 --- a/src/ipc/ipc-event.ts +++ b/src/ipc/ipc-event.ts @@ -26,6 +26,12 @@ export interface IpcPriceCheck { position: { x: number, y: number } } +export const MAP_CHECK = 'MAIN->OVERLAY::map-check' +export interface IpcMapCheck { + clipboard: string + position: { x: number, y: number } +} + export const OVERLAY_READY = 'OVERLAY->MAIN::ready' export const CLOSE_OVERLAY = 'OVERLAY->MAIN::close-overlay' diff --git a/src/ipc/types.ts b/src/ipc/types.ts index 843b18d5..6d864653 100644 --- a/src/ipc/types.ts +++ b/src/ipc/types.ts @@ -10,6 +10,7 @@ export interface Config { priceCheckLocked: string | null wikiKey: string | null overlayKey: string + mapCheckKey: string | null commands: Array<{ text: string hotkey: string | null @@ -51,6 +52,7 @@ export const defaultConfig: Config = { priceCheckLocked: 'Ctrl + Alt + D', wikiKey: 'Alt + W', overlayKey: 'Shift + Space', + mapCheckKey: null, commands: [{ text: '/hideout', hotkey: 'F5' @@ -102,6 +104,17 @@ export const defaultConfig: Config = { wmZorder: 'exclusive', wmFlags: ['hide-on-blur', 'skip-menu'] }, + { + wmId: 3, + wmType: 'map-check', + wmTitle: '', + wmWants: 'hide', + wmZorder: 3, + wmFlags: ['hide-on-blur', 'skip-menu'], + selectedStats: [ + // { text: '' } + ] + }, // --- DEFAULT --- { wmId: 4, diff --git a/src/main/PollClipboard.ts b/src/main/PollClipboard.ts deleted file mode 100644 index 268cfadb..00000000 --- a/src/main/PollClipboard.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { clipboard } from 'electron' -import { TAG_RARITY } from '@/parser/constants' -import { logger } from './logger' - -export async function pollClipboard (delay: number, limit: number): Promise { - let textBefore = clipboard.readText() - let elapsed = 0 - - if (textBefore.startsWith(TAG_RARITY)) { - textBefore = '' - clipboard.writeText('') - } - - return new Promise((resolve, reject) => { - function poll () { - const textAfter = clipboard.readText() - - if (textAfter.startsWith(TAG_RARITY)) { - clipboard.writeText(textBefore) - resolve(textAfter) - } else { - elapsed += delay - if (elapsed < limit) { - setTimeout(poll, delay) - } else { - logger.warn('No changes found', { source: 'clipboard', timeout: limit }) - reject(new Error('Clipboard was not changed')) - } - } - } - - poll() - }) -} diff --git a/src/main/config.ts b/src/main/config.ts index 0eda222b..dadb7203 100644 --- a/src/main/config.ts +++ b/src/main/config.ts @@ -19,11 +19,31 @@ export function setupConfigEvents () { }) } -export const config = new Store({ - name: 'config', - cwd: 'apt-data', - defaults: defaultConfig -}) +export const config = (() => { + const config = new Store({ + name: 'config', + cwd: 'apt-data', + defaults: defaultConfig + }) + + const forbidden = ['Ctrl + C', 'Ctrl + V', 'Ctrl + A', 'Ctrl + F'] + if (forbidden.includes(config.get('priceCheckLocked') as string)) { config.set('priceCheckLocked', null) } + if (forbidden.includes(config.get('wikiKey') as string)) { config.set('wikiKey', null) } + if (forbidden.includes(config.get('mapCheckKey') as string)) { config.set('mapCheckKey', null) } + const comands = config.get('commands') + for (const c of comands) { + if (forbidden.includes(c.hotkey as string)) { c.hotkey = null } + } + config.set('commands', comands) + + if (config.get('priceCheckKeyHold') === 'Ctrl') { + if (['C', 'V', 'A', 'F'].includes(config.get('priceCheckKey') as string)) { + config.set('priceCheckKey', null) + } + } + + return config +})() export function batchUpdateConfig (upd: Config, push = true) { // for (const key in upd) { diff --git a/src/main/poll-clipboard.ts b/src/main/poll-clipboard.ts new file mode 100644 index 00000000..d5817068 --- /dev/null +++ b/src/main/poll-clipboard.ts @@ -0,0 +1,51 @@ +import { clipboard } from 'electron' +import { TAG_RARITY } from '@/parser/constants' +import { logger } from './logger' + +const DELAY = 48 +const LIMIT = 500 + +export let isPollingClipboard = false +let clipboardPromise: Promise | undefined +let elapsed: number + +export async function pollClipboard (): Promise { + isPollingClipboard = true + elapsed = 0 + if (clipboardPromise) { + return clipboardPromise + } + + let textBefore = clipboard.readText() + if (textBefore.startsWith(TAG_RARITY)) { + textBefore = '' + clipboard.writeText('') + } + + clipboardPromise = new Promise((resolve, reject) => { + function poll () { + const textAfter = clipboard.readText() + + if (textAfter.startsWith(TAG_RARITY)) { + clipboard.writeText(textBefore) + isPollingClipboard = false + clipboardPromise = undefined + resolve(textAfter) + } else { + elapsed += DELAY + if (elapsed < LIMIT) { + setTimeout(poll, DELAY) + } else { + logger.warn('No changes found', { source: 'clipboard', timeout: LIMIT }) + isPollingClipboard = false + clipboardPromise = undefined + reject(new Error('Clipboard was not changed')) + } + } + } + + poll() + }) + + return clipboardPromise +} diff --git a/src/main/price-check.ts b/src/main/price-check.ts index e0243855..d300f67e 100644 --- a/src/main/price-check.ts +++ b/src/main/price-check.ts @@ -1,6 +1,6 @@ import { ipcMain, Rectangle, Point } from 'electron' import { uIOhook } from 'uiohook-napi' -import { isPollingClipboard } from './shortcuts' +import { isPollingClipboard } from './poll-clipboard' import { PoeWindow } from './PoeWindow' import * as ipc from '@/ipc/ipc-event' import { config } from './config' diff --git a/src/main/shortcuts.ts b/src/main/shortcuts.ts index 610bca15..131f351e 100644 --- a/src/main/shortcuts.ts +++ b/src/main/shortcuts.ts @@ -1,32 +1,28 @@ import { screen, Point, clipboard, globalShortcut, Notification } from 'electron' import robotjs from 'robotjs' import { uIOhook, UiohookKey } from 'uiohook-napi' -import { pollClipboard } from './PollClipboard' +import { pollClipboard } from './poll-clipboard' import { showWidget as showPriceCheck } from './price-check' import { KeyToElectron } from '@/ipc/KeyToCode' import { config } from './config' import { PoeWindow } from './PoeWindow' import { openWiki } from './wiki' import { logger } from './logger' -import { toggleOverlayState } from './overlay-window' +import { toggleOverlayState, overlayWindow } from './overlay-window' +import * as ipc from '@/ipc/ipc-event' -export let isPollingClipboard = false export let hotkeyPressPosition: Point | undefined export const UiohookToName = Object.fromEntries(Object.entries(UiohookKey).map(([k, v]) => ([v, k]))) function priceCheck (lockedMode: boolean) { - logger.info('Price check', { source: 'price-check', lockedMode }) + logger.info('Price check', { source: 'shortcuts', lockedMode }) - if (!isPollingClipboard) { - isPollingClipboard = true - pollClipboard(32, 500) + pollClipboard() .then(clipboard => showPriceCheck({ clipboard, hotkeyPressPosition: hotkeyPressPosition!, lockedMode }) ) .catch(() => { /* nothing bad */ }) - .finally(() => { isPollingClipboard = false }) - } hotkeyPressPosition = screen.getCursorScreenPoint() // if (process.platform === 'win32') { // hotkeyPressPosition = screen.dipToScreenPoint(hotkeyPressPosition) @@ -43,6 +39,18 @@ function priceCheck (lockedMode: boolean) { } } +function mapCheck () { + logger.info('Map check', { source: 'shortcuts' }) + + pollClipboard() + .then(clipboard => + overlayWindow!.webContents.send(ipc.MAP_CHECK, { clipboard, position: hotkeyPressPosition! } as ipc.IpcMapCheck) + ) + .catch(() => {}) + hotkeyPressPosition = screen.getCursorScreenPoint() + robotjs.keyTap('C', ['Ctrl']) +} + function registerGlobal () { const register = [ shortcutCallback( @@ -62,10 +70,14 @@ function registerGlobal () { shortcutCallback( config.get('wikiKey'), () => { - pollClipboard(32, 500).then(openWiki).catch(() => {}) + pollClipboard().then(openWiki).catch(() => {}) robotjs.keyTap('C', ['Ctrl']) } ), + shortcutCallback( + config.get('mapCheckKey'), + mapCheck + ), ...config.get('commands') .map(command => shortcutCallback(command.hotkey, () => typeChatCommand(command.text)) @@ -131,9 +143,11 @@ export function setupShortcuts () { shortcutCallback(pressed, toggleOverlayState, { doNotResetModKey: true }).cb() } else if (pressed === config.get('wikiKey')) { shortcutCallback(pressed, () => { - pollClipboard(32, 500).then(openWiki).catch(() => {}) + pollClipboard().then(openWiki).catch(() => {}) robotjs.keyTap('C', ['Ctrl']) }).cb() + } else if (pressed === config.get('mapCheckKey')) { + shortcutCallback(pressed, mapCheck).cb() } else { const command = config.get('commands').find(c => c.hotkey === pressed) if (command) { diff --git a/src/web/overlay/OverlayWindow.vue b/src/web/overlay/OverlayWindow.vue index 94ccbf06..7b5492d8 100644 --- a/src/web/overlay/OverlayWindow.vue +++ b/src/web/overlay/OverlayWindow.vue @@ -23,6 +23,7 @@ import WidgetInventorySearch from './WidgetInventorySearch' import WidgetMenu from './WidgetMenu' import PriceCheckWindow from '@/web/price-check/PriceCheckWindow' import WidgetDebug from './WidgetDebug' +import WidgetMapCheck from '@/web/map-check/WidgetMapCheck' import { FOCUS_CHANGE, VISIBILITY } from '@/ipc/ipc-event' import { Config } from '@/web/Config' @@ -32,7 +33,8 @@ export default { WidgetInventorySearch, WidgetMenu, WidgetPriceCheck: PriceCheckWindow, - WidgetDebug + WidgetDebug, + WidgetMapCheck }, provide () { return { wm: this } diff --git a/src/web/settings/HotkeyInput.vue b/src/web/settings/HotkeyInput.vue index b3aa0ed7..0b31d3d4 100644 --- a/src/web/settings/HotkeyInput.vue +++ b/src/web/settings/HotkeyInput.vue @@ -17,7 +17,7 @@ export default { }, forbidden: { type: Array, - default: () => [] + default: () => ['Ctrl + C', 'Ctrl + V', 'Ctrl + A', 'Ctrl + F'] }, required: { type: Boolean, @@ -48,7 +48,6 @@ export default { if ( KeyToCode[code] && - !this.forbidden.includes(code) && (ctrlKey ? !this.forbidden.includes('Ctrl') : true) && (shiftKey ? !this.forbidden.includes('Shift') : true) && (altKey ? !this.forbidden.includes('Alt') : true) @@ -60,7 +59,9 @@ export default { else if (ctrlKey) code = `Ctrl + ${code}` else if (shiftKey) code = `Shift + ${code}` - this.$emit('input', code) + if (!this.forbidden.includes(code)) { + this.$emit('input', code) + } } } } diff --git a/src/web/settings/hotkeys.vue b/src/web/settings/hotkeys.vue index 304be6ed..0bd2529c 100644 --- a/src/web/settings/hotkeys.vue +++ b/src/web/settings/hotkeys.vue @@ -7,21 +7,27 @@
Price check
Auto-hide Mode - - + + + - +
Open without auto-hide - + +
+ +
+
+
Open wiki
+
-
Open wiki
- +
Map check
+
@@ -39,7 +45,7 @@
- +