From 1a89306dcdce45c05657fa4f354e32dde19311e1 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Tue, 15 Sep 2026 20:49:37 -0400 Subject: [PATCH] refactor(player): route the mount-time Play focus through the shared check The onMounted branch repeated the pad/key test that shouldClaimFocusOnModality already owns, so it now calls the helper and also leaves an already-focused element alone. Trims the comments that came with the original fix down to the non-obvious part. Co-Authored-By: Claude Opus 5 (1M context) --- frontend/src/v2/utils/autofocus.test.ts | 2 +- frontend/src/v2/utils/autofocus.ts | 6 ++---- frontend/src/v2/views/Player/EmulatorJS.vue | 18 ++++++++++-------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/frontend/src/v2/utils/autofocus.test.ts b/frontend/src/v2/utils/autofocus.test.ts index a513ee18c..7e8ad9a80 100644 --- a/frontend/src/v2/utils/autofocus.test.ts +++ b/frontend/src/v2/utils/autofocus.test.ts @@ -24,7 +24,7 @@ describe("shouldAutofocusSearch", () => { }); describe("shouldClaimFocusOnModality", () => { - const body = document.createElement("body"); + const body = document.body; const button = document.createElement("button"); it("claims an unfocused page for a pad or a keyboard", () => { diff --git a/frontend/src/v2/utils/autofocus.ts b/frontend/src/v2/utils/autofocus.ts index 64d92f72e..88f2f37fa 100644 --- a/frontend/src/v2/utils/autofocus.ts +++ b/frontend/src/v2/utils/autofocus.ts @@ -15,10 +15,8 @@ export function shouldAutofocusSearch( return win.matchMedia("(hover: hover) and (pointer: fine)").matches; } -// Whether a switch to a directional input device should pull focus onto a -// view's primary action. Only pad/keyboard navigate by focus, and only an -// unfocused page is up for grabs — a user who tabbed somewhere keeps their -// place. +// Whether a modality switch should pull focus onto a view's primary action: +// only pad/key navigate by focus, and only an unfocused page is up for grabs. export function shouldClaimFocusOnModality( modality: InputModality, active: Element | null, diff --git a/frontend/src/v2/views/Player/EmulatorJS.vue b/frontend/src/v2/views/Player/EmulatorJS.vue index fa799bd16..d1424fab2 100644 --- a/frontend/src/v2/views/Player/EmulatorJS.vue +++ b/frontend/src/v2/views/Player/EmulatorJS.vue @@ -361,19 +361,21 @@ onMounted(async () => { configBiosFile: coreOptions["bios_file"], }); - // Autofocus the Play CTA so gamepad/keyboard users land on the - // primary action without an extra Tab. Mouse / touch keep the - // default no-autofocus behaviour. - if (modality.value === "pad" || modality.value === "key") { + // Land gamepad/keyboard users on the primary action without an extra Tab. + if ( + shouldClaimFocusOnModality( + modality.value, + document.activeElement, + document.body, + ) + ) { await nextTick(); focusPlayButton(); } }); -// Arriving by mouse leaves the page unfocused, and this view has no spatial -// navigation for the d-pad to walk, so a pad picked up here had no way to -// reach Play (issue #4397). Landing on it the moment the user switches away -// from the mouse gives the pad an entry point. +// This view has no spatial navigation for a d-pad to walk, so landing on Play +// the moment the user picks up a pad is the only entry point into the view. watch(modality, (next) => { if (gameRunning.value) return; if (!shouldClaimFocusOnModality(next, document.activeElement, document.body))