Further migrations to runlevel

This commit is contained in:
Tamius Han 2024-05-07 21:01:45 +02:00
parent a466f77b6e
commit abeded1d03
3 changed files with 35 additions and 85 deletions

View File

@ -73,7 +73,7 @@ class PlayerData {
//#endregion //#endregion
//#region flags //#region flags
runLevel: RunLevel; runLevel: RunLevel = RunLevel.Off;
enabled: boolean; enabled: boolean;
invalid: boolean = false; invalid: boolean = false;
private periodicallyRefreshPlayerElement: boolean = false; private periodicallyRefreshPlayerElement: boolean = false;
@ -221,20 +221,7 @@ class PlayerData {
//#endregion //#endregion
/** /**
* Enables ultrawidify for this video by adding the relevant classes * Sets extension runLevel and sets or unsets appropriate css classes as necessary
* 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
* @param runLevel * @param runLevel
* @returns * @returns
*/ */
@ -264,22 +251,6 @@ class PlayerData {
this.runLevel = runLevel; 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. * Detects whether player element is in theater mode or not.
* If theater mode changed, emits event. * 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 // 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; const canEnable = this.siteSettings.isEnabledForEnvironment(this.isFullscreen, this.isTheaterMode) === ExtensionMode.Enabled;
// TODO: if canEnable is 'true' and runLevel is OFF, we should if (this.runLevel === RunLevel.Off && canEnable) {
// call restoreAr function of resizer and let it figure out the necessary run level. this.eventBus.send('restore-ar', null);
// Function that restores the last user-set AR may also need to be written. // must be called after
this.handleDimensionChanges(currentPlayerDimensions, this.dimensions);
// Enable/disable } else if (!canEnable && this.runLevel !== RunLevel.Off) {
if (canEnable) { // must be called before
if (!this.enabled) { this.handleDimensionChanges(currentPlayerDimensions, this.dimensions);
// we should really check other constraints first before enabling this.setRunLevel(RunLevel.Off);
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;
} }
} }

View File

@ -38,7 +38,7 @@ class VideoData {
//#region flags //#region flags
arSetupComplete: boolean = false; arSetupComplete: boolean = false;
enabled: boolean; enabled: boolean;
runLevel: RunLevel; runLevel: RunLevel = RunLevel.Off;
destroyed: boolean = false; destroyed: boolean = false;
invalid: boolean = false; invalid: boolean = false;
videoStatusOk: 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 * Creates new VideoData object
* @param video * @param video
@ -120,11 +132,12 @@ class VideoData {
if (pageInfo.eventBus) { if (pageInfo.eventBus) {
this.eventBus.setUpstreamBus(pageInfo.eventBus); this.eventBus.setUpstreamBus(pageInfo.eventBus);
this.eventBus.subscribe('get-drm-status', {function: () => {
this.hasDrm = hasDrm(this.video); for (const action in this.eventBusCommands) {
this.eventBus.send('uw-config-broadcast', {type: 'drm-status', hasDrm: this.hasDrm}); for (const command of this.eventBusCommands[action]) {
}}); this.eventBus.subscribe(action, command);
// this.eventBus.subscribe('') }
}
} }
this.setupEventListeners(); this.setupEventListeners();
@ -251,10 +264,6 @@ class VideoData {
this.injectBaseCss(); this.injectBaseCss();
this.pageInfo.initMouseActionHandler(this); 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 // start fallback video/player size detection
this.fallbackChangeDetection(); this.fallbackChangeDetection();
} }
@ -344,9 +353,10 @@ class VideoData {
this.video.removeEventListener('ontimeupdate', this.onTimeUpdate); this.video.removeEventListener('ontimeupdate', this.onTimeUpdate);
} }
this.disable(); this.eventBus.send('set-run-level', RunLevel.Off);
this.destroyed = true; this.destroyed = true;
this.eventBus?.unsetUpstreamBus(); this.eventBus?.unsetUpstreamBus();
try { try {
this.arDetector.stop(); this.arDetector.stop();
this.arDetector.destroy(); this.arDetector.destroy();
@ -367,32 +377,18 @@ class VideoData {
} }
//#endregion //#endregion
/**
* Enables ultrawidify in general.
* @param options
*/
enable(options?: {fromPlayer?: boolean}) {
this.enabled = true;
// NOTE — since base class for our <video> element depends on player aspect ratio,
// we handle it in PlayerData class.
this.video.classList.add(this.baseCssName);
this.video.classList.add(this.userCssClassName); // this also needs to be applied BEFORE we initialize resizer! — O RLY? NEEDS TO BE CHECKED
if (!options?.fromPlayer) {
this.player?.enable();
}
// this.restoreCrop();
}
setRunLevel(runLevel: RunLevel, options?: {fromPlayer?: boolean}) { setRunLevel(runLevel: RunLevel, options?: {fromPlayer?: boolean}) {
console.log('setting new runlevel for videodata. current:', this.runLevel, 'new', runLevel)
if (this.runLevel === runLevel) { if (this.runLevel === runLevel) {
return; // also no need to propagate to the player return; // also no need to propagate to the player
} }
console.log('setting run level ...')
// Run level decreases towards 'off' // Run level decreases towards 'off'
if (this.runLevel > runLevel) { if (this.runLevel > runLevel) {
console.log('decreasing css.')
if (runLevel < RunLevel.CustomCSSActive) { if (runLevel < RunLevel.CustomCSSActive) {
this.video.classList.remove(this.baseCssName); this.video.classList.remove(this.baseCssName);
this.video.classList.remove(this.userCssClassName); this.video.classList.remove(this.userCssClassName);
@ -428,9 +424,6 @@ class VideoData {
this.video.classList.remove(this.baseCssName); this.video.classList.remove(this.baseCssName);
this.video.classList.remove(this.userCssClassName); this.video.classList.remove(this.userCssClassName);
if (!options?.fromPlayer) {
this.player?.disable();
}
} }
//#region video status //#region video status

View File

@ -911,8 +911,6 @@ class Resizer {
return; return;
} }
} }
this.videoData.disable();
} }
//#endregion //#endregion
} }