All files / app-play/src/services/themes built-in.ts

74.07% Statements 20/27
62.5% Branches 5/8
100% Functions 6/6
74.07% Lines 20/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93                                        6x           6x 6x 6x         6x         3x 3x 2x 2x   1x 1x 1x               2x 2x 1x 1x   1x         1x                               2x         2x                
// The Be-Music Skin the built-in family draws with: the one the Options dialog picked (`guiState.builtInSkin`), the
// player's values for its settings, and the skins added while the page is open.
 
import type { BuiltInKit } from '@be-music/player-browser/host';
import type { BeMusicSkin } from '@be-music/player-skin-sdk';
import { readStoredSkinSettings, type StoredSkinSettings } from '../../skin-settings.ts';
import type { PlayerGUIState } from '../../types.ts';
import { BE_MUSIC_SKINS, loadSkinFonts } from '../built-in-skins.ts';
 
export interface BuiltInThemeServiceOptions {
  /** The Options dialog's state: the built-in skin picked (`builtInSkin`) and how much motion skins show (`skinEffects`). */
  readonly guiState: Pick<PlayerGUIState, 'builtInSkin' | 'skinEffects'>;
  readonly setStatus: (text: string) => void;
  /** Switches to the skin `id`: it is stored, and the select screen is made again with it. */
  readonly handleBuiltInSkinChange: (id: string) => void;
}
 
/** The Be-Music Skin the built-in screens draw with, the player's settings for each skin, and adding skins. */
export class BuiltInThemeService {
  /** Each Be-Music Skin's `settings` values by skin id, persisted in `localStorage` and handed to its scenes. */
  public readonly skinSettings: StoredSkinSettings = readStoredSkinSettings();
  private readonly guiState: BuiltInThemeServiceOptions['guiState'];
  private readonly setStatus: BuiltInThemeServiceOptions['setStatus'];
  private readonly handleBuiltInSkinChange: BuiltInThemeServiceOptions['handleBuiltInSkinChange'];
 
  public constructor(options: BuiltInThemeServiceOptions) {
    this.guiState = options.guiState;
    this.setStatus = options.setStatus;
    this.handleBuiltInSkinChange = options.handleBuiltInSkinChange;
  }
 
  /** The Be-Music Skin the default-family scenes render with. */
  public get beMusicSkin(): BeMusicSkin {
    return BE_MUSIC_SKINS.resolve(this.guiState.builtInSkin);
  }
 
  /** Registers `skin` (validated), loads its fonts, and switches to it; reports why when it is refused. */
  public addBeMusicSkin(skin: BeMusicSkin): void {
    const problems = BE_MUSIC_SKINS.add(skin);
    if (problems.length > 0) {
      this.setStatus(`Skin "${skin.id}" was not added: ${problems.join('; ')}`);
      return;
    }
    loadSkinFonts(skin);
    this.handleBuiltInSkinChange(skin.id);
    this.setStatus(`Added skin ${skin.label} ${skin.version} by ${skin.author.name}`);
  }
 
  /**
   * Imports a skin module from `url` and adds its default export. A skin is code that runs with this page's full
   * access, so the user confirms the source first.
   */
  public async addBeMusicSkinFromURL(url: string): Promise<void> {
    const trimmed = url.trim();
    if (!trimmed) {
      this.setStatus('Enter the URL of a skin module first');
      return;
    }
    Eif (
      !window.confirm(
        `Load the skin at ${trimmed}?\n\nA skin is code that runs with full access to this page. Only load skins you trust.`,
      )
    ) {
      return;
    }
    try {
      const module = (await import(/* @vite-ignore */ trimmed)) as { default?: BeMusicSkin };
      if (!module.default) {
        this.setStatus('That module has no default export to use as a skin');
        return;
      }
      this.addBeMusicSkin(module.default);
    } catch (error) {
      this.setStatus(`Could not load the skin: ${(error as Error).message}`);
    }
  }
 
  /** The stored values for the active Be-Music Skin's settings, as handed to its scenes (`beMusicSkinSettings`). */
  public get beMusicSkinSettings(): Readonly<Record<string, unknown>> | undefined {
    return this.skinSettings.get(this.beMusicSkin.id);
  }
 
  /** The kit the built-in scenes draw with: the active Be-Music Skin, its stored settings and the effect level. */
  public get builtInKit(): BuiltInKit {
    return {
      family: 'built-in',
      skin: this.beMusicSkin,
      settings: this.beMusicSkinSettings,
      effects: this.guiState.skinEffects,
    };
  }
}