Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions kotlin/web-receiver/receiver/fullscreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>; held: () => boolean; delay: (ms: number) => Promise<void>; denied: () => void;
}): Promise<void> {
let settled: Promise<boolean>;
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;
Expand Down
9 changes: 6 additions & 3 deletions kotlin/web-receiver/receiver/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLCanvasElement>('#video')!;
const element = <T extends HTMLElement = HTMLElement>(id: string) => document.getElementById(id)! as T;
Expand Down Expand Up @@ -197,9 +197,12 @@ async function reportFullscreenExit(): Promise<void> {
post({ type: 'fullscreen-exit', hiddenMs, display, autoRestore });
}
async function requestFullscreen(): Promise<void> {
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 => {
Expand Down
37 changes: 36 additions & 1 deletion kotlin/web-receiver/tests/fullscreen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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<void>, 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<void>(() => {}), () => 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);
});
Loading