All files / player-browser/src/skins/native mount.ts

0% Statements 0/31
0% Branches 0/17
0% Functions 0/4
0% Lines 0/25

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                                                                                                             
import { defineBeMusicElements } from '@be-music/player-ui';
import { basecoatTheme } from '@be-music/player-ui/themes/basecoat';
import type { BeMusicDOMScreen } from '@be-music/player-skin-sdk';
import { NATIVE_EYEBROW, adoptNativeTheme } from './theme.ts';
 
/** Native only adapts scene frames and intents to the reusable presentation elements. */
export function mountElement<Frame>(
  container: HTMLElement,
  tag:
    | 'be-music-select-screen'
    | 'be-music-gameplay-hud'
    | 'be-music-playfield'
    | 'be-music-result-screen'
    | 'be-music-decide-screen',
  listeners: Readonly<Record<string, EventListener>>,
  shouldUpdate?: (frame: Frame) => boolean,
): BeMusicDOMScreen<Frame> {
  const registry = container.ownerDocument.defaultView?.customElements;
  if (!registry) throw new Error('Native skin requires a window with Custom Elements support.');
  defineBeMusicElements(registry, { theme: basecoatTheme });
  const element = container.ownerDocument.createElement(tag) as HTMLElement & {
    frame: Frame | undefined;
    focusSearch?: () => void;
  };
  element.dataset.nativeScreen =
    tag === 'be-music-gameplay-hud' ? 'gameplay' : tag.slice('be-music-'.length).replace('-screen', '');
  Object.assign(element.style, { position: 'absolute', inset: '0', width: '100%', height: '100%' });
  // Native's landscape play HUD is the cabinet arrangement.
  if (tag === 'be-music-gameplay-hud') element.toggleAttribute('cabinet', true);
  if (tag === 'be-music-select-screen') {
    const eyebrow = container.ownerDocument.createElement('span');
    eyebrow.slot = 'eyebrow';
    eyebrow.textContent = NATIVE_EYEBROW;
    element.append(eyebrow);
  }
  const releaseTheme = adoptNativeTheme(container);
  const entries = Object.entries(listeners);
  for (const [name, listener] of entries) element.addEventListener(name, listener);
  container.append(element);
  let disposed = false;
  return {
    update(frame) {
      if (!disposed && (!shouldUpdate || shouldUpdate(frame))) element.frame = frame;
    },
    ...(tag === 'be-music-select-screen' ? { focusSearch: () => element.focusSearch?.() } : {}),
    dispose() {
      if (disposed) return;
      disposed = true;
      for (const [name, listener] of entries) element.removeEventListener(name, listener);
      element.remove();
      releaseTheme();
    },
  };
}