From e84f8ee541c137df6ea87868d9cadc1688f034e4 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Tue, 27 Sep 2022 01:48:08 +0200 Subject: [PATCH] start fixing KbmHandler (formerly: action handler) some more --- src/common/interfaces/SettingsInterface.ts | 16 ++- .../lib/{ActionHandler.ts => KbmHandler.ts} | 103 ++++++++++++++++-- src/ext/lib/uwui/UI.js | 1 + src/ext/lib/video-data/PageInfo.ts | 44 -------- 4 files changed, 104 insertions(+), 60 deletions(-) rename src/ext/lib/{ActionHandler.ts => KbmHandler.ts} (75%) diff --git a/src/common/interfaces/SettingsInterface.ts b/src/common/interfaces/SettingsInterface.ts index f70fab4..fdb081c 100644 --- a/src/common/interfaces/SettingsInterface.ts +++ b/src/common/interfaces/SettingsInterface.ts @@ -175,6 +175,16 @@ interface SettingsInterface { crop: { default: any; }, + stretch: { + default: any; + conditionalDifferencePercent: number // black bars less than this wide will trigger stretch + // if mode is set to '1'. 1.0=100% + }, + kbmHandler: { + enabled: boolean, // if keyboard/mouse handler service will run + keyboardEnabled: boolean, // if keyboard shortcuts are processed + mouseEnabled: boolean, // if mouse movement is processed + } zoom: { minLogZoom: number, @@ -189,11 +199,7 @@ interface SettingsInterface { mousePanReverseMouse: boolean, defaultAr?: any }, - stretch: { - default: any; - conditionalDifferencePercent: number // black bars less than this wide will trigger stretch - // if mode is set to '1'. 1.0=100% - }, + resizer: { setStyleString: { maxRetries: number, diff --git a/src/ext/lib/ActionHandler.ts b/src/ext/lib/KbmHandler.ts similarity index 75% rename from src/ext/lib/ActionHandler.ts rename to src/ext/lib/KbmHandler.ts index cc7d0c4..c9233d8 100644 --- a/src/ext/lib/ActionHandler.ts +++ b/src/ext/lib/KbmHandler.ts @@ -5,27 +5,46 @@ import Logger from './Logger'; import PageInfo from './video-data/PageInfo'; import Settings from './Settings'; import VideoData from './video-data/VideoData'; -import EventBus from './EventBus'; +import EventBus, { EventBusCommand } from './EventBus'; if(process.env.CHANNEL !== 'stable'){ console.info("Loading ActionHandler"); } -class ActionHandler { +/** + * Handles keypresses and mouse movement + */ +class KbmHandler { logger: Logger; settings: Settings; eventBus: EventBus; + allowShortcuts: boolean = true; + inputs: string[] = ['input', 'select', 'button', 'textarea']; keyboardLocalDisabled: boolean = false; + keyboardEnabled: boolean = false; + mouseEnabled: boolean = false; + mouseMoveActions: any[] = []; + keypressActions: any[] = []; - commands: any[] = []; + eventBusCommands: { [x: string]: EventBusCommand } = { + 'kbm-enable': { + function: () => this.enable() + }, + 'kbm-disable': { + function: () => this.disable() + }, + 'kbm-set-config': { + function: (data: {config: any, temporary?: boolean}) => this.setConfig(data.config, data.temporary), + }, + } - - constructor(eventBus: EventBus, settings, logger) { + //#region lifecycle + constructor(eventBus: EventBus, settings: Settings, logger: Logger) { this.logger = logger; this.settings = settings; this.eventBus = eventBus; @@ -40,18 +59,44 @@ class ActionHandler { for (const key in this.settings.active.commands) { for (const command of this.settings.active.commands[key]) { if (command.shortcut) { - this.commands.push(command); + this.keypressActions.push(command); } } } + this.load(); + } + load() { + this.settings.active.kbmHandler.enabled ? this.addListener() : this.removeListener(); + this.keyboardEnabled = this.settings.active.kbmHandler.keyboardEnabled; + this.mouseEnabled = this.settings.active.kbmHandler.mouseEnabled; + } + + destroy() { + this.removeListener(); + } + + // convenience methods + addListener() { // events should be handled in handleEvent function. We need to do things this // way, otherwise we can't remove event listener // https://stackoverflow.com/a/19507086 document.addEventListener('keyup', this ); } + removeListener() { + document.removeEventListener('keyup', this); + } + //#endregion + + enable() { + this.addListener(); + } + + disable() { + this.removeListener(); + } handleEvent(event) { switch(event.type) { @@ -64,10 +109,37 @@ class ActionHandler { } } - destroy() { - document.removeEventListener('keyup', this); + /** + * Sets configuration parameter for KbmHandler + * @param config + */ + setConfig(config, temporary = false) { + if (temporary) { + for (const confKey in config) { + switch (confKey) { + case 'enabled': + config[confKey] ? this.enable() : this.disable(); + break; + case 'keyboardEnabled': + this.keyboardEnabled = config[confKey]; + break; + case 'mouseEnabled': + this.mouseEnabled = config[confKey]; + break; + } + } + return; + } + + for (const confKey in config) { + this.settings.active.kbmHandler[confKey] = config[confKey]; + } + + this.settings.save(); + this.load(); } + registerHandleMouse(videoData) { this.logger.log('info', ['actionHandler', 'mousemove'], "[ActionHandler::registerHandleMouse] registering handle mouse for videodata:", videoData.id) @@ -190,6 +262,10 @@ class ActionHandler { handleKeyup(event) { + if (!this.keyboardEnabled) { + this.logger.log('info', 'keyboard', "%c[ActionHandler::handleKeyup] kbmHandler.keyboardEnabled is set to false. Doing nothing."); + return; + } this.logger.log('info', 'keyboard', "%c[ActionHandler::handleKeyup] we pressed a key: ", "color: #ff0", event.key , " | keyup: ", event.keyup, "event:", event); try { @@ -198,11 +274,11 @@ class ActionHandler { return; } - this.logger.log('info', 'keyboard', "%c[ActionHandler::handleKeyup] Trying to find and execute action for event. Actions/event: ", "color: #ff0", this.commands, event); + this.logger.log('info', 'keyboard', "%c[ActionHandler::handleKeyup] Trying to find and execute action for event. Actions/event: ", "color: #ff0", this.keypressActions, event); const isLatin = this.isLatin(event.key); - for (const command of this.commands) { + for (const command of this.keypressActions) { if (this.isActionMatch(command.shortcut, event, isLatin)) { this.eventBus.send(command.action, command.arguments); } @@ -214,6 +290,11 @@ class ActionHandler { handleMouseMove(event, videoData?: VideoData) { + if (!this.mouseEnabled) { + this.logger.log('info', 'keyboard', "%c[ActionHandler::handleKeyup] kbmHandler.keyboardEnabled is set to false. Doing nothing."); + return; + } + this.logger.log('info', 'keyboard', "[ActionHandler::handleMouseMove] mouse move is being handled.\nevent:", event, "\nvideo data:", videoData); console.info('mousemove must be migrated!'); // videoData?.panHandler(event); @@ -226,4 +307,4 @@ if(process.env.CHANNEL !== 'stable'){ console.info("ActionHandler loaded"); } -export default ActionHandler; +export default KbmHandler; diff --git a/src/ext/lib/uwui/UI.js b/src/ext/lib/uwui/UI.js index e54eaba..a528d32 100644 --- a/src/ext/lib/uwui/UI.js +++ b/src/ext/lib/uwui/UI.js @@ -99,6 +99,7 @@ class UI { * like current zoom levels & current aspect ratio & stuff. Some of these things are * necessary for UI display in the popup. */ + this.eventBus.subscribe( 'uw-config-broadcast', { diff --git a/src/ext/lib/video-data/PageInfo.ts b/src/ext/lib/video-data/PageInfo.ts index 8faa1bf..4913c55 100644 --- a/src/ext/lib/video-data/PageInfo.ts +++ b/src/ext/lib/video-data/PageInfo.ts @@ -132,14 +132,6 @@ class PageInfo { } } - setActionHandler(actionHandler) { - this.actionHandler = actionHandler; - for (let item of this.actionHandlerInitQueue) { - this.actionHandler.registerHandleMouse(item); - } - this.actionHandlerInitQueue = []; - } - getVideos(host) { if (this.settings.active.sites[host]?.DOM?.video?.manual && this.settings.active.sites[host]?.DOM?.video?.querySelectors){ @@ -335,42 +327,6 @@ class PageInfo { this.scheduleUrlCheck(); } - - // these need to be called on tab switch - pauseProcessing(playingOnly){ - if (playingOnly) { - for(let vd of this.videos){ - if (vd.videoData.isPlaying()) { - vd.videoData.disable(); - } - } - } else { - for(let vd of this.videos){ - vd.videoData.disable(); - } - } - } - - resumeProcessing(resumeAutoar = false, playingOnly = false){ - if (playingOnly) { - for(let vd of this.videos){ - if (vd.videoData.isPlaying()) { - vd.videoData.enable(); - if(resumeAutoar){ - vd.videoData.resumeAutoAr(); - } - } - } - } else { - for(let vd of this.videos){ - vd.videoData.enable(); - if(resumeAutoar){ - vd.videoData.resumeAutoAr(); - } - } - } - } - setArPersistence(persistenceMode) { // name of this function is mildly misleading — we don't really _set_ ar persistence. (Ar persistence // mode is set and saved via popup or keyboard shortcuts, if user defined them) We just save the current