All files / player-browser/src/view/built-in dom-view.ts

0% Statements 0/88
0% Branches 0/27
0% Functions 0/43
0% Lines 0/79

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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
import {
  resolveBeMusicSkinSettings,
  type BeMusicDOMPresentation,
  type BeMusicDOMFactory,
  type BeMusicDOMSelectFrame,
  type BeMusicDOMSelectActions,
  type BeMusicDOMResultFrame,
  type BeMusicDOMResultActions,
  type BeMusicDOMDecideFrame,
  type BeMusicDOMDecideActions,
} from '@be-music/player-skin-sdk';
import { PLAY_OPTION_CHOICES } from '@be-music/player-scene-model';
import { Container } from 'pixi.js';
import type { BuiltInKit } from '../../host/family-kit.ts';
import type { PixiSceneHost } from '../../host/scene-host.ts';
import { SkinDOMMount } from '../../skin/be-music/dom-mount.ts';
import { ChartImageCache } from '../../skin/be-music/chart-images.ts';
import type { BrowserAssetSource } from '../../collection/types.ts';
import { hasReadtextForChart } from '../../collection/readtext.ts';
import { SelectReadmeDialog } from '../shared/readme-dialog.ts';
import type {
  FamilyView,
  IntentSink,
  SelectFrame,
  SelectIntent,
  SelectView,
  ResultFrame,
  ResultIntent,
  ResultView,
  DecideFrame,
  DecideIntent,
  DecideView,
} from '../types.ts';
import { BUILT_IN_RESULT_TIMING } from './result-view.ts';
import { resolveBuiltInDecideTiming } from './decide-view.ts';
 
/** Compatibility adapter for the existing Pixi host. DOM mounts themselves have no Pixi dependency. */
abstract class DOMFamilyView<Frame, Intent, DOMFrame, Actions> implements FamilyView<Frame, Intent> {
  public readonly family = 'built-in' as const;
  public readonly root = new Container();
  public readonly drawsOnCanvas = false;
  protected presentation: BeMusicDOMPresentation;
  private readonly skin: BuiltInKit['skin'];
  protected emit: IntentSink<Intent> = () => {};
  private host: PixiSceneHost | undefined;
  protected readonly mount: SkinDOMMount<DOMFrame, Actions>;
 
  public constructor(kit: BuiltInKit, factory: BeMusicDOMFactory<DOMFrame, Actions>, actions: Actions, kind: string) {
    this.skin = kit.skin;
    this.presentation = {
      settings: resolveBeMusicSkinSettings(kit.skin, kit.settings),
      effects: kit.effects ?? 'full',
    };
    this.mount = new SkinDOMMount(factory, actions, kind);
  }
 
  public attach(host: PixiSceneHost, emit: IntentSink<Intent>): void {
    this.emit = emit;
    this.host = host;
    this.mount.attach(host.domRoot);
  }
 
  public detach(): void {
    this.emit = () => {};
    this.host = undefined;
    this.mount.detach();
  }
 
  public dispose(): void {
    this.detach();
    this.mount.dispose();
    if (!this.root.destroyed) this.root.destroy();
  }
 
  public updateKit(kit: BuiltInKit): void {
    if (kit.skin === this.skin)
      this.presentation = {
        settings: resolveBeMusicSkinSettings(kit.skin, kit.settings),
        effects: kit.effects ?? 'full',
      };
  }
  /** Native screens follow CSS viewport orientation, including while their data snapshot is unchanged. */
  protected syncOrientation(): void {
    const host = this.host;
    if (!host) return;
    // Measure the element the screens mount in: while Native shows, the canvas is out of the page and the Pixi
    // screen keeps whatever size it last resized to.
    const root = host.domRoot;
    const { width, height } =
      root.clientWidth > 0 && root.clientHeight > 0
        ? { width: root.clientWidth, height: root.clientHeight }
        : host.app.screen;
    host.setStageOrientation(height > width ? 'portrait' : 'landscape');
  }
 
  public abstract draw(frame: Frame): void;
}
 
export class DOMSelectView
  extends DOMFamilyView<SelectFrame, SelectIntent, BeMusicDOMSelectFrame, BeMusicDOMSelectActions>
  implements SelectView
{
  public readonly launchOutroMs = 0;
  public get ownsSearchInput(): boolean {
    return this.mount.hasSearchInput;
  }
  public focusSearch(): boolean {
    return this.mount.focusSearch();
  }
  private revision = -1;
  private launching = false;
  private readonly readme = new SelectReadmeDialog();
 
  public constructor(kit: BuiltInKit) {
    // Event handlers resolve emit at invocation time, after attach; the skin never receives a session object.
    let send: IntentSink<SelectIntent> = () => {};
    super(
      kit,
      kit.skin.dom!.select!,
      {
        focus: (index) => send({ type: 'browse', command: { type: 'jump', index } }),
        activate: () => send({ type: 'activate' }),
        back: () => send({ type: 'browse', command: { type: 'close' } }),
        search: (text) => send({ type: 'browse', command: { type: 'search', text } }),
        play: (autoPlay) => send({ type: 'start', autoPlay }),
        setPlayOption: (key, value) => send({ type: 'set-play-option', key, value }),
        readText: () => send({ type: 'readtext' }),
        gesture: () => send({ type: 'gesture' }),
      },
      'select',
    );
    send = (intent) => this.emit(intent);
  }
 
  public override attach(host: PixiSceneHost, emit: IntentSink<SelectIntent>): void {
    this.revision = -1;
    super.attach(host, emit);
    this.readme.attach(
      () => host.domRoot,
      () => this.emit({ type: 'readtext' }),
      (index) => this.emit({ type: 'readtext-tab', index }),
    );
  }
 
  public override detach(): void {
    this.readme.detach();
    super.detach();
  }
 
  public override updateKit(kit: BuiltInKit): void {
    super.updateKit(kit);
    this.revision = -1;
  }
 
  public searchFieldRect(): undefined {
    return undefined;
  }
 
  public draw(frame: SelectFrame): void {
    this.syncOrientation();
    for (const event of frame.events) if (event.kind === 'readtext-scroll') this.readme.scrollBy(event.delta);
    this.readme.show(frame.readText);
    const launching = frame.startedAtMs !== undefined;
    if (frame.revision === this.revision && this.launching === launching) return;
    this.revision = frame.revision;
    this.launching = launching;
    const focused = frame.entries[frame.cursor];
    this.mount.update({
      ...this.presentation,
      revision: frame.revision,
      entries: frame.entries,
      selectedIndex: frame.cursor,
      folderLabel: frame.browse.path.at(-1)?.label,
      searchQuery: frame.browse.searchText,
      ...(frame.searchError !== undefined ? { searchError: frame.searchError } : {}),
      playOptions: frame.playOptions,
      playOptionChoices: PLAY_OPTION_CHOICES,
      hasReadme: focused?.kind === 'chart' && hasReadtextForChart(frame.library, focused.chart),
      launching,
    });
  }
}
 
export class DOMResultView
  extends DOMFamilyView<ResultFrame, ResultIntent, BeMusicDOMResultFrame, BeMusicDOMResultActions>
  implements ResultView
{
  public readonly timing = BUILT_IN_RESULT_TIMING;
 
  public constructor(kit: BuiltInKit) {
    let send: IntentSink<ResultIntent> = () => {};
    super(
      kit,
      kit.skin.dom!.result!,
      {
        continue: () => send({ type: 'continue' }),
        retry: () => send({ type: 'retry' }),
      },
      'result',
    );
    send = (intent) => this.emit(intent);
  }
 
  public draw(frame: ResultFrame): void {
    this.syncOrientation();
    this.mount.update({
      ...this.presentation,
      result: frame.result,
      inputReady: frame.clock.nowMs >= frame.inputOpenAtMs,
    });
  }
}
 
export class DOMDecideView
  extends DOMFamilyView<DecideFrame, DecideIntent, BeMusicDOMDecideFrame, BeMusicDOMDecideActions>
  implements DecideView
{
  public readonly timing;
  private readonly chartImages: ChartImageCache;
 
  public constructor(kit: BuiltInKit, options: { source?: BrowserAssetSource } = {}) {
    let send: IntentSink<DecideIntent> = () => {};
    super(
      kit,
      kit.skin.dom!.decide!,
      {
        continue: () => send({ type: 'continue' }),
        cancel: () => send({ type: 'cancel' }),
      },
      'decide',
    );
    send = (intent) => this.emit(intent);
    this.timing = resolveBuiltInDecideTiming(kit);
    this.chartImages = new ChartImageCache({ resolveSource: () => options.source });
  }
 
  public draw(frame: DecideFrame): void {
    this.syncOrientation();
    this.mount.update({
      ...this.presentation,
      chart: frame.chart,
      titleImage: this.chartImages.lookupURL(frame.chart, 'titleImage'),
      progress: frame.progress,
      inputReady: frame.clock.nowMs >= frame.inputOpenAtMs,
    });
  }
 
  public override dispose(): void {
    this.chartImages.dispose();
    super.dispose();
  }
}