update pollclipboard, hotkey blacklist

This commit is contained in:
Alexander Drozdov
2020-06-23 20:37:58 +03:00
parent 82fe517025
commit fe76cafbb9
10 changed files with 141 additions and 62 deletions
+6
View File
@@ -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'
+13
View File
@@ -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,
-34
View File
@@ -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<string> {
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()
})
}
+25 -5
View File
@@ -19,11 +19,31 @@ export function setupConfigEvents () {
})
}
export const config = new Store<Config>({
name: 'config',
cwd: 'apt-data',
defaults: defaultConfig
})
export const config = (() => {
const config = new Store<Config>({
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) {
+51
View File
@@ -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<string> | undefined
let elapsed: number
export async function pollClipboard (): Promise<string> {
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
}
+1 -1
View File
@@ -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'
+25 -11
View File
@@ -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) {
+3 -1
View File
@@ -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 }
+4 -3
View File
@@ -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)
}
}
}
}
+13 -7
View File
@@ -7,21 +7,27 @@
<div class="flex-1">Price check</div>
<div class="flex">
<span class="text-gray-500 mr-2">Auto-hide Mode</span>
<button :class="{ border: config.priceCheckKeyHold === 'Ctrl' }" @click="config.priceCheckKeyHold = 'Ctrl'" class="rounded px-1 bg-gray-900 leading-none mr-1">Ctrl</button>
<button :class="{ border: config.priceCheckKeyHold === 'Alt' }" @click="config.priceCheckKeyHold = 'Alt'" class="rounded px-1 bg-gray-900 leading-none">Alt</button>
<button :class="{ border: config.priceCheckKeyHold === 'Ctrl' }" @click="config.priceCheckKeyHold = 'Ctrl'; config.priceCheckKey = null" class="rounded px-1 bg-gray-900 leading-none mr-1">Ctrl</button>
<button :class="{ border: config.priceCheckKeyHold === 'Alt' }" @click="config.priceCheckKeyHold = 'Alt'; config.priceCheckKey = null" class="rounded px-1 bg-gray-900 leading-none">Alt</button>
<span class="mx-4">+</span>
<hotkey-input v-model="config.priceCheckKey" :forbidden="['Ctrl','Shift','Alt','C','V']" class="w-20" />
<hotkey-input v-model="config.priceCheckKey" :forbidden="['Ctrl','Shift','Alt', ...(config.priceCheckKeyHold === 'Ctrl' ? ['C','V','A','F'] : [])]" class="w-20" />
</div>
</div>
<div class="text-right mt-2">
<span class="text-gray-500 mr-2">Open without auto-hide</span>
<hotkey-input v-model="config.priceCheckLocked" :forbidden="['C','V']" class="w-48" />
<hotkey-input v-model="config.priceCheckLocked" class="w-48" />
</div>
</div>
<div class="mb-4">
<div class="flex">
<div class="flex-1">Open wiki</div>
<hotkey-input v-model="config.wikiKey" class="w-48" />
</div>
</div>
<div class="mb-8">
<div class="flex">
<div class="flex-1">Open wiki</div>
<hotkey-input v-model="config.wikiKey" :forbidden="['C','V']" class="w-48" />
<div class="flex-1">Map check</div>
<hotkey-input v-model="config.mapCheckKey" class="w-48" />
</div>
</div>
<div class="mb-8">
@@ -39,7 +45,7 @@
<input v-model="command.text" class="rounded bg-gray-900 px-1 block w-full mb-1 font-fontin-regular" />
<div class="flex justify-end">
<button @click="removeCommand(command)" class="mr-2 text-gray-500">Remove</button>
<hotkey-input v-model="command.hotkey" :forbidden="['C','V']" class="w-48" />
<hotkey-input v-model="command.hotkey" class="w-48" />
</div>
</div>
<button @click="addComand" class="bg-gray-900 rounded flex items-baseline px-2 py-1 leading-none"><i class="fas fa-plus mr-1"></i> Add command</button>