From abeded1d0360de0a10ffcc38f6fa06536f833b40 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Tue, 7 May 2024 21:01:45 +0200 Subject: [PATCH] Further migrations to runlevel --- src/ext/lib/video-data/PlayerData.ts | 61 +++++--------------------- src/ext/lib/video-data/VideoData.ts | 57 +++++++++++------------- src/ext/lib/video-transform/Resizer.ts | 2 - 3 files changed, 35 insertions(+), 85 deletions(-) diff --git a/src/ext/lib/video-data/PlayerData.ts b/src/ext/lib/video-data/PlayerData.ts index 5c12682..f257e15 100644 --- a/src/ext/lib/video-data/PlayerData.ts +++ b/src/ext/lib/video-data/PlayerData.ts @@ -73,7 +73,7 @@ class PlayerData { //#endregion //#region flags - runLevel: RunLevel; + runLevel: RunLevel = RunLevel.Off; enabled: boolean; invalid: boolean = false; private periodicallyRefreshPlayerElement: boolean = false; @@ -221,20 +221,7 @@ class PlayerData { //#endregion /** - * Enables ultrawidify for this video by adding the relevant classes - * to the video and player element. - */ - enable() { - console.log('enabling UI') - this.enabled = true; - this.element.classList.add(this.playerCssClass); - this.startChangeDetection(); - this.videoData.enable({fromPlayer: true}); - this.ui.enable(); - } - - /** - * Sets extension runLevel + * Sets extension runLevel and sets or unsets appropriate css classes as necessary * @param runLevel * @returns */ @@ -264,22 +251,6 @@ class PlayerData { this.runLevel = runLevel; } - /** - * Disables ultrawidify for this video by removing the relevant classes - * from the video and player elements. - * - * NOTE: it is very important to keep change detection active while disabled, - * because otherwise ultrawidify will otherwise remain inactive after - * switching (back to) full screen. - */ - disable() { - console.log('disabling UI') - this.enabled = false; - this.element.classList.remove(this.playerCssClass); - this.videoData.disable({fromPlayer: true}); - this.ui.disable(); - } - /** * Detects whether player element is in theater mode or not. * If theater mode changed, emits event. @@ -354,26 +325,14 @@ class PlayerData { // Check if extension is allowed to run in current combination of theater + full screen const canEnable = this.siteSettings.isEnabledForEnvironment(this.isFullscreen, this.isTheaterMode) === ExtensionMode.Enabled; - // TODO: if canEnable is 'true' and runLevel is OFF, we should - // call restoreAr function of resizer and let it figure out the necessary run level. - // Function that restores the last user-set AR may also need to be written. - - // Enable/disable - if (canEnable) { - if (!this.enabled) { - // we should really check other constraints first before enabling - this.enable(); - this.handleDimensionChanges(currentPlayerDimensions, this.dimensions); - return; - } - } else { - // if canEnable comes out negative, there's no amount of constraints that will - // cause PlayerData to be enabled. - if (this.enabled) { - this.handleDimensionChanges(currentPlayerDimensions, this.dimensions); - this.disable(); - } - return; + if (this.runLevel === RunLevel.Off && canEnable) { + this.eventBus.send('restore-ar', null); + // must be called after + this.handleDimensionChanges(currentPlayerDimensions, this.dimensions); + } else if (!canEnable && this.runLevel !== RunLevel.Off) { + // must be called before + this.handleDimensionChanges(currentPlayerDimensions, this.dimensions); + this.setRunLevel(RunLevel.Off); } } diff --git a/src/ext/lib/video-data/VideoData.ts b/src/ext/lib/video-data/VideoData.ts index e2c1d32..8e7899f 100644 --- a/src/ext/lib/video-data/VideoData.ts +++ b/src/ext/lib/video-data/VideoData.ts @@ -38,7 +38,7 @@ class VideoData { //#region flags arSetupComplete: boolean = false; enabled: boolean; - runLevel: RunLevel; + runLevel: RunLevel = RunLevel.Off; destroyed: boolean = false; invalid: boolean = false; videoStatusOk: boolean = false; @@ -85,6 +85,18 @@ class VideoData { } } + private eventBusCommands = { + 'get-drm-status': [{ + function: () => { + this.hasDrm = hasDrm(this.video); + this.eventBus.send('uw-config-broadcast', {type: 'drm-status', hasDrm: this.hasDrm}); + } + }], + 'set-run-level': [{ + function: (runLevel: RunLevel) => this.setRunLevel(runLevel) + }] + } + /** * Creates new VideoData object * @param video @@ -120,11 +132,12 @@ class VideoData { if (pageInfo.eventBus) { this.eventBus.setUpstreamBus(pageInfo.eventBus); - this.eventBus.subscribe('get-drm-status', {function: () => { - this.hasDrm = hasDrm(this.video); - this.eventBus.send('uw-config-broadcast', {type: 'drm-status', hasDrm: this.hasDrm}); - }}); - // this.eventBus.subscribe('') + + for (const action in this.eventBusCommands) { + for (const command of this.eventBusCommands[action]) { + this.eventBus.subscribe(action, command); + } + } } this.setupEventListeners(); @@ -251,10 +264,6 @@ class VideoData { this.injectBaseCss(); this.pageInfo.initMouseActionHandler(this); - // aspect ratio autodetection cannot be properly initialized at this time, - // so we'll avoid doing that - this.enable(); - // start fallback video/player size detection this.fallbackChangeDetection(); } @@ -344,9 +353,10 @@ class VideoData { this.video.removeEventListener('ontimeupdate', this.onTimeUpdate); } - this.disable(); + this.eventBus.send('set-run-level', RunLevel.Off); this.destroyed = true; this.eventBus?.unsetUpstreamBus(); + try { this.arDetector.stop(); this.arDetector.destroy(); @@ -367,32 +377,18 @@ class VideoData { } //#endregion - /** - * Enables ultrawidify in general. - * @param options - */ - enable(options?: {fromPlayer?: boolean}) { - this.enabled = true; - - // NOTE — since base class for our