From 9ce679a61db1e887e6322eec17de15ee385ae10f Mon Sep 17 00:00:00 2001 From: Victor Bonnet Date: Sat, 29 Aug 2026 03:41:12 +0000 Subject: [PATCH] Default keyboard labels to QWERTY --- app/src/ui/keyboard-layout.js | 25 ++++++++++++++----------- app/test/keyboard-layout.test.js | 30 +++++++++++++++++------------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/app/src/ui/keyboard-layout.js b/app/src/ui/keyboard-layout.js index 4604bfa..ee0e798 100644 --- a/app/src/ui/keyboard-layout.js +++ b/app/src/ui/keyboard-layout.js @@ -1,9 +1,7 @@ // Controls use physical KeyboardEvent.code positions; getLayoutMap() provides // the characters printed on those keys for the active layout. -const FALLBACK_LABELS = Object.freeze({ KeyW: "W/Z", KeyA: "A/Q", KeyQ: "Q/A" }); - const MOVEMENT_CODES = ["KeyW", "KeyA", "KeyS", "KeyD"]; -const MOVEMENT_FALLBACK_HINT = "arrows or WASD / ZQSD"; +const MOVEMENT_FALLBACK_HINT = "arrows or WASD"; function keycap(value) { if (typeof value !== "string") return null; @@ -14,22 +12,27 @@ function keycap(value) { } export function resolveKeycaps(codes, layoutMap) { + const required = [...new Set([...MOVEMENT_CODES, ...codes])]; const labels = {}; - const fromLayout = new Set(); - for (const code of new Set([...MOVEMENT_CODES, ...codes])) { + let complete = true; + for (const code of required) { let label = null; try { label = keycap(layoutMap?.get?.(code)); } catch { label = null; } - if (label) fromLayout.add(code); - labels[code] = label ?? FALLBACK_LABELS[code] ?? code.replace(/^Key/, ""); + if (!label) complete = false; + labels[code] = label; } - const moveHint = MOVEMENT_CODES.every((code) => fromLayout.has(code)) - ? `arrows or ${MOVEMENT_CODES.map((code) => labels[code]).join("")}` - : MOVEMENT_FALLBACK_HINT; - return { labels, moveHint }; + if (!complete) { + for (const code of required) labels[code] = code.replace(/^Key/, ""); + return { labels, moveHint: MOVEMENT_FALLBACK_HINT }; + } + return { + labels, + moveHint: `arrows or ${MOVEMENT_CODES.map((code) => labels[code]).join("")}`, + }; } export async function readLayoutMap(nav = globalThis.navigator) { diff --git a/app/test/keyboard-layout.test.js b/app/test/keyboard-layout.test.js index 0b6841f..7f05211 100644 --- a/app/test/keyboard-layout.test.js +++ b/app/test/keyboard-layout.test.js @@ -31,35 +31,39 @@ test("resolves caps and the movement hint from the active layout", () => { assert.equal(dvorak.moveHint, "arrows or ,AOE"); }); -test("falls back to labels naming both layouts without a map", () => { +test("falls back to QWERTY labels without a map", () => { assert.deepEqual(resolveKeycaps(CODES, null), { - labels: { KeyW: "W/Z", KeyA: "A/Q", KeyS: "S", KeyD: "D", KeyQ: "Q/A", KeyE: "E" }, - moveHint: "arrows or WASD / ZQSD", + labels: { KeyW: "W", KeyA: "A", KeyS: "S", KeyD: "D", KeyQ: "Q", KeyE: "E" }, + moveHint: "arrows or WASD", }); assert.equal(resolveKeycaps(["KeyX"], null).labels.KeyX, "X"); }); -test("keeps the inclusive hint when the map misses a movement key", () => { +const ALL_QWERTY = { + labels: { KeyW: "W", KeyA: "A", KeyS: "S", KeyD: "D", KeyQ: "Q", KeyE: "E" }, + moveHint: "arrows or WASD", +}; + +test("falls back entirely to QWERTY when the map misses a movement key", () => { const partial = new Map([["KeyW", "z"], ["KeyA", "q"], ["KeyS", "s"]]); - const { labels, moveHint } = resolveKeycaps(CODES, partial); - assert.equal(labels.KeyW, "Z"); - assert.equal(labels.KeyD, "D"); - assert.equal(moveHint, "arrows or WASD / ZQSD"); + assert.deepEqual(resolveKeycaps(CODES, partial), ALL_QWERTY); +}); + +test("falls back entirely to QWERTY when the map misses a requested key", () => { + const partial = new Map([...AZERTY].filter(([code]) => code !== "KeyE")); + assert.deepEqual(resolveKeycaps(CODES, partial), ALL_QWERTY); }); test("rejects layout values that cannot sit on a keycap", () => { const junk = new Map( Object.entries({ KeyQ: "", KeyE: " ", KeyW: "Dead", KeyA: "́", KeyS: 7 }), ); - const { labels } = resolveKeycaps(CODES, junk); - assert.deepEqual(labels, { - KeyW: "W/Z", KeyA: "A/Q", KeyS: "S", KeyD: "D", KeyQ: "Q/A", KeyE: "E", - }); + assert.deepEqual(resolveKeycaps(CODES, junk), ALL_QWERTY); }); test("a throwing map cannot break the menu", () => { const hostile = { get: () => { throw new Error("nope"); } }; - assert.equal(resolveKeycaps(CODES, hostile).labels.KeyQ, "Q/A"); + assert.deepEqual(resolveKeycaps(CODES, hostile), ALL_QWERTY); }); test("readLayoutMap yields null when the API is absent or refuses", async () => {