All files / app-play/src fullscreen.ts

97.56% Statements 40/41
92.3% Branches 36/39
90.9% Functions 10/11
100% Lines 34/34

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                                                        11x       18x       10x 10x 9x       4x 4x 4x                       6x 6x 2x   4x                                   4x 3x                                     5x   5x         5x 5x         5x         8x 8x 8x 3x 3x 3x   5x 3x 3x 2x   1x 1x                               5x 5x 4x          
/**
 * Fullscreen helpers for the player shell. Wraps the unprefixed Fullscreen API plus Safari's
 * `webkit*` aliases so a double-click on the Pixi canvas can toggle the page into (and out
 * of) fullscreen without the rest of the player having to know about vendor prefixes.
 */
 
export type FullscreenDocumentLike = {
  fullscreenElement?: Element | null;
  webkitFullscreenElement?: Element | null;
  exitFullscreen?: () => Promise<void>;
  webkitExitFullscreen?: () => Promise<void>;
};
 
export type FullscreenElementLike = {
  requestFullscreen?: () => Promise<void>;
  webkitRequestFullscreen?: () => Promise<void>;
};
 
/**
 * The play surface (`#main`). Fullscreen covers this element: it holds the stage and the Options dialog, and the site
 * header and the site footer sit outside it. A missing surface falls back to the document element.
 */
export function playerFullscreenElement(
  doc: {
    querySelector(selector: string): FullscreenElementLike | null;
    documentElement: FullscreenElementLike;
  } = document,
): FullscreenElementLike {
  return doc.querySelector('#main') ?? doc.documentElement;
}
 
export function getFullscreenElement(doc: FullscreenDocumentLike = document): Element | null {
  return doc.fullscreenElement ?? doc.webkitFullscreenElement ?? null;
}
 
export async function requestFullscreen(element: FullscreenElementLike): Promise<void> {
  const request = element.requestFullscreen ?? element.webkitRequestFullscreen;
  if (request === undefined) return;
  await request.call(element);
}
 
export async function exitFullscreen(doc: FullscreenDocumentLike = document): Promise<void> {
  const exit = doc.exitFullscreen ?? doc.webkitExitFullscreen;
  Iif (exit === undefined) return;
  await exit.call(doc);
}
 
/**
 * Enter fullscreen on `element` when the document is windowed; leave fullscreen when it
 * already is. Failures (unsupported element, missing user gesture) are swallowed so a
 * double-click on an iOS Safari page that rejects element fullscreen stays a no-op.
 */
export async function toggleFullscreen(
  element: FullscreenElementLike,
  doc: FullscreenDocumentLike = document,
): Promise<void> {
  try {
    if (getFullscreenElement(doc) !== null) {
      await exitFullscreen(doc);
    } else {
      await requestFullscreen(element);
    }
  } catch {
    // NotAllowedError / TypeError — the click is a no-op.
  }
}
 
/**
 * True when the player should consume Escape so the active scene (quit play / dismiss
 * result / close a select folder) does not also fire while leaving fullscreen. With the
 * Escape key locked ({@link FullscreenEscapeLock}) a short Esc press belongs to the page
 * and the browser leaves fullscreen on a long press instead, so nothing is consumed.
 */
export function shouldCaptureFullscreenEscape(
  event: Pick<KeyboardEvent, 'key' | 'code'>,
  doc: FullscreenDocumentLike = document,
  escapeLocked = false,
): boolean {
  if (escapeLocked) return false;
  return (event.key === 'Escape' || event.code === 'Escape') && getFullscreenElement(doc) !== null;
}
 
/** The slice of the Keyboard Lock API (`navigator.keyboard`, Chromium only) the player uses. */
export type KeyboardLockLike = {
  lock?: (keyCodes?: string[]) => Promise<void>;
  unlock?: () => void;
};
 
export type KeyboardLockNavigatorLike = { keyboard?: KeyboardLockLike };
 
/**
 * Locks the Escape key while the player is fullscreen, where the browser supports Keyboard Lock
 * (Chrome / Edge). A short Esc press then reaches the page — quitting the play as it does
 * windowed — and the browser leaves fullscreen only on a long press, showing its own hint.
 * Safari and Firefox have no `navigator.keyboard.lock`, so this is a no-op there and Esc
 * keeps leaving fullscreen first.
 */
export class FullscreenEscapeLock {
  private locked = false;
  /** Bumped on every sync so a lock that resolves after fullscreen was left is not recorded. */
  private generation = 0;
  private readonly nav: KeyboardLockNavigatorLike | undefined;
  private readonly onLockError: (error: unknown) => void;
 
  constructor(nav: KeyboardLockNavigatorLike | undefined, onLockError: (error: unknown) => void = () => {}) {
    this.nav = nav;
    this.onLockError = onLockError;
  }
 
  /** Whether Escape is currently locked (the lock request resolved and fullscreen has not been left since). */
  isLocked(): boolean {
    return this.locked;
  }
 
  /** Locks Escape when `fullscreen` is true, unlocks it otherwise. Lock failures are reported, never thrown. */
  async sync(fullscreen: boolean): Promise<void> {
    const keyboard = this.nav?.keyboard;
    const generation = ++this.generation;
    if (!fullscreen) {
      this.locked = false;
      keyboard?.unlock?.();
      return;
    }
    if (keyboard?.lock === undefined) return;
    try {
      await keyboard.lock(['Escape']);
      if (generation === this.generation) this.locked = true;
    } catch (error) {
      Eif (generation === this.generation) this.locked = false;
      this.onLockError(error);
    }
  }
}
 
type OrientationLike = { orientation?: { lock?: (orientation: string) => Promise<void> } };
 
/**
 * Enter fullscreen on `element` (unless something already is) and ask for landscape, for a phone's first tap. Either
 * step may be unsupported (iPhone Safari has neither) or refused; both failures leave the page as it was.
 */
export async function enterTouchFullscreen(
  element: FullscreenElementLike,
  doc: FullscreenDocumentLike = document,
  screenLike: OrientationLike = globalThis.screen as OrientationLike,
): Promise<void> {
  try {
    if (getFullscreenElement(doc) === null) await requestFullscreen(element);
    await screenLike?.orientation?.lock?.('landscape');
  } catch {
    // NotSupportedError / NotAllowedError / SecurityError — stay as we are.
  }
}