diff --git a/kotlin/web-receiver/receiver/fullscreen.ts b/kotlin/web-receiver/receiver/fullscreen.ts index da888c2..2538162 100644 --- a/kotlin/web-receiver/receiver/fullscreen.ts +++ b/kotlin/web-receiver/receiver/fullscreen.ts @@ -72,6 +72,24 @@ export class FullscreenIntent { } } +/** How long fullscreen must last after a request before the browser counts as holding it. */ +export const FULLSCREEN_HOLD_MS = 1500; + +/** + * Some head-unit browsers accept a request without entering fullscreen, drop out of it at once, or never + * answer. Each counts as a refusal, so a pending restore stops consuming touches meant for CarPlay. + */ +export async function requestHeldFullscreen(options: { + request: () => Promise; held: () => boolean; delay: (ms: number) => Promise; denied: () => void; +}): Promise { + let settled: Promise; + try { settled = options.request().then(() => true, () => false); } catch { settled = Promise.resolve(false); } + const answer = await Promise.race([settled, options.delay(FULLSCREEN_HOLD_MS).then(() => null)]); + if (answer === false) { options.denied(); return; } + if (answer === true) await options.delay(FULLSCREEN_HOLD_MS); + if (!options.held()) options.denied(); +} + export class FullscreenExitHistory { private hiddenAt: number | null = null; private visibleAt: number | null = null; diff --git a/kotlin/web-receiver/receiver/main.ts b/kotlin/web-receiver/receiver/main.ts index 082e631..bc9f10c 100644 --- a/kotlin/web-receiver/receiver/main.ts +++ b/kotlin/web-receiver/receiver/main.ts @@ -24,7 +24,7 @@ import { playbackUi, type PlaybackUi, type PlaybackEvent } from './video-hold'; import { version, shouldReload } from './version'; import { frameRatePreference, RestartGate, withFrameRate } from './frame-rate'; import { playbackMode, withPlayback } from '../shared/playback'; -import { autoRestoreFullscreen, FullscreenExitHistory, FullscreenIntent } from './fullscreen'; +import { autoRestoreFullscreen, FullscreenExitHistory, FullscreenIntent, requestHeldFullscreen } from './fullscreen'; import './style.css'; const canvas = document.querySelector('#video')!; const element = (id: string) => document.getElementById(id)! as T; @@ -197,9 +197,12 @@ async function reportFullscreenExit(): Promise { post({ type: 'fullscreen-exit', hiddenMs, display, autoRestore }); } async function requestFullscreen(): Promise { - try { await document.documentElement.requestFullscreen(); } - catch { fullscreen.denied(); } + const pending = requestHeldFullscreen({ + request: () => document.documentElement.requestFullscreen(), held: () => !!document.fullscreenElement, + delay: ms => new Promise(resolve => setTimeout(resolve, ms)), denied: () => { fullscreen.denied(); fullscreenState(); }, + }); fullscreenState(); + await pending; } // Capture before canvas controls: the restore gesture must never become a CarPlay touch. document.addEventListener('pointerdown', event => { diff --git a/kotlin/web-receiver/tests/fullscreen.test.ts b/kotlin/web-receiver/tests/fullscreen.test.ts index 51a07b5..669f15c 100644 --- a/kotlin/web-receiver/tests/fullscreen.test.ts +++ b/kotlin/web-receiver/tests/fullscreen.test.ts @@ -4,7 +4,7 @@ import { expect, it, vi } from 'vitest'; import { readFileSync } from 'node:fs'; -import { autoRestoreFullscreen, FullscreenExitHistory, FullscreenIntent } from '../receiver/fullscreen'; +import { autoRestoreFullscreen, FULLSCREEN_HOLD_MS, FullscreenExitHistory, FullscreenIntent, requestHeldFullscreen } from '../receiver/fullscreen'; import { strings, type Store } from '../receiver/ui'; import type { Hello } from '../protocol/json'; const display = (JSON.parse(readFileSync(new URL('../spec/vectors/web/hello-viewport.json', import.meta.url), 'utf8')) as Hello).displays[0]; @@ -191,3 +191,38 @@ it('does not retry after a manual restoration during the timer', async () => { options.delay.mockImplementation(async () => { options.restored.mockReturnValue(true); }); expect(await autoRestoreFullscreen(options)).toBe('restored'); expect(options.request).toHaveBeenCalledOnce(); }); + +function held(request: () => Promise, fullscreen: () => boolean) { + const state = new FullscreenIntent(true, store); + state.changed(true); state.changed(false); + const options = { request: vi.fn(request), held: vi.fn(fullscreen), delay: vi.fn(async (_ms: number) => {}), denied: vi.fn(() => state.denied()) }; + return { state, options }; +} +it('lets CarPlay touches through when the browser accepts fullscreen but never shows it', async () => { + const { state, options } = held(async () => {}, () => false); + expect(state.pointerDown(pointer('touch'), vi.fn())).toBe(true); + await requestHeldFullscreen(options); + expect(options.delay).toHaveBeenCalledWith(FULLSCREEN_HOLD_MS); expect(options.denied).toHaveBeenCalledOnce(); + expect(state.pending).toBe(false); expect(state.pointerDown(pointer('touch', 2), vi.fn())).toBe(false); + expect(state.intent).toBe(true); +}); +it('treats fullscreen the browser drops at once, or a request that never answers, as a refusal', async () => { + let on = true; + const dropped = held(async () => { on = false; }, () => on); + await requestHeldFullscreen(dropped.options); + expect(dropped.state.pending).toBe(false); + const silent = held(() => new Promise(() => {}), () => false); + await requestHeldFullscreen(silent.options); + expect(silent.options.delay).toHaveBeenCalledOnce(); expect(silent.state.pending).toBe(false); +}); +it('keeps restoring after fullscreen that holds, and refuses at once on a rejection', async () => { + const kept = held(async () => {}, () => true); + await requestHeldFullscreen(kept.options); + expect(kept.options.denied).not.toHaveBeenCalled(); + const rejected = held(async () => { throw Error('gesture'); }, () => false); + await requestHeldFullscreen(rejected.options); + expect(rejected.options.denied).toHaveBeenCalledOnce(); expect(rejected.state.pending).toBe(false); + const thrown = held(() => { throw Error('sync'); }, () => false); + await requestHeldFullscreen(thrown.options); + expect(thrown.state.pending).toBe(false); +});