fix(skin): match macOS recovery shortcut by physical key code and forbid toolbar hook attribute

This commit is contained in:
OddTurtleHead committed 2026-11-26 23:06:43 +08:00
1 parent 881354e283
commit cfbf488f75
7 files changed
+63 -6

No files matched your search

+6 -4
View File
@@ -133,16 +133,18 @@ export function requestBuiltInSkin(): void {
builtInSkinRequests.value += 1;
}
type RecoveryShortcutEvent = Pick<KeyboardEvent, "ctrlKey" | "metaKey" | "altKey" | "shiftKey" | "key">;
type RecoveryShortcutEvent = Pick<KeyboardEvent, "ctrlKey" | "metaKey" | "altKey" | "shiftKey" | "key" | "code">;
/**
* The documented recovery shortcut: Ctrl+Alt+Shift+R, or ⌘+⌥+⇧+R on macOS.
* The letter is matched case-insensitively, but every modifier is required so
* ordinary app shortcuts (Ctrl+R, Alt+R, Ctrl+Shift+R) keep their meaning.
* On macOS, Option/Shift turn the letter key into a derived character (US
* layout: ®), so `event.key` is not "r". Match the physical `code` as well;
* every modifier is still required so ordinary app shortcuts (Ctrl+R, Alt+R,
* Ctrl+Shift+R) keep their meaning.
*/
export function isRecoveryShortcut(event: RecoveryShortcutEvent): boolean {
return (event.ctrlKey || event.metaKey)
&& event.altKey
&& event.shiftKey
&& event.key.toLowerCase() === "r";
&& (event.key?.toLowerCase() === "r" || event.code === "KeyR");
}
@@ -9,6 +9,7 @@ type ShortcutEvent = {
altKey: boolean;
shiftKey: boolean;
key: string;
code: string;
};
const shortcut = (overrides: Partial<ShortcutEvent> = {}): ShortcutEvent => ({
@@ -17,6 +18,7 @@ const shortcut = (overrides: Partial<ShortcutEvent> = {}): ShortcutEvent => ({
altKey: false,
shiftKey: false,
key: "",
code: "",
...overrides,
});
@@ -24,16 +26,28 @@ test("the recovery shortcut requires the full Ctrl/Cmd+Alt+Shift+R chord", () =>
assert.equal(isRecoveryShortcut(shortcut({ ctrlKey: true, altKey: true, shiftKey: true, key: "r" })), true);
// macOS uses the Meta/Command key in place of Ctrl.
assert.equal(isRecoveryShortcut(shortcut({ metaKey: true, altKey: true, shiftKey: true, key: "R" })), true);
// Option/Shift turn the letter into a derived character (US layout: ®) on
// macOS; the physical key code must still match.
assert.equal(isRecoveryShortcut(shortcut({
metaKey: true, altKey: true, shiftKey: true, key: "®", code: "KeyR",
})), true);
assert.equal(isRecoveryShortcut(shortcut({
ctrlKey: true, altKey: true, shiftKey: true, key: "®", code: "KeyR",
})), true);
for (const rejected of [
shortcut({ ctrlKey: true, altKey: true, shiftKey: true, key: "x" }),
shortcut({ ctrlKey: true, altKey: true, shiftKey: true, key: "Enter" }),
// A derived character alone is not enough: the physical key must be R.
shortcut({ metaKey: true, altKey: true, shiftKey: true, key: "®", code: "KeyX" }),
// Ordinary app/browser shortcuts must stay available.
shortcut({ ctrlKey: true, key: "r" }),
shortcut({ altKey: true, key: "r" }),
shortcut({ ctrlKey: true, shiftKey: true, key: "r" }),
shortcut({ ctrlKey: true, altKey: true, key: "r" }),
shortcut({ ctrlKey: false, altKey: true, shiftKey: true, key: "r" }),
// Incomplete modifiers stay rejected even when the physical code matches.
shortcut({ ctrlKey: true, altKey: true, key: "®", code: "KeyR" }),
]) {
assert.equal(isRecoveryShortcut(rejected), false, JSON.stringify(rejected));
}
+28
View File
@@ -217,6 +217,32 @@ try {
toolbarOpen: isOpen(toolbar()),
};
// On macOS ⌥/⇧ turn the letter into a derived character (US layout: ®), so
// `key` is not "r". The physical code must match. A skin-style capture
// listener calling preventDefault() must not swallow the chord either.
const skinCapture = (event) => { if (event.type === "keydown") event.preventDefault(); };
window.addEventListener("keydown", skinCapture, true);
window.dispatchEvent(new KeyboardEvent("keydown", {
key: "®", code: "KeyR", metaKey: true, altKey: true, shiftKey: true, bubbles: true, cancelable: true,
}));
window.removeEventListener("keydown", skinCapture, true);
await nextTick();
await sleep(80);
const macShortcut = {
capsuleOpen: isOpen(capsule()),
toolbarOpen: isOpen(toolbar()),
focusedAction: layer.shadow.activeElement?.getAttribute?.("data-ws-recovery-action") ?? null,
capsuleActions: [...(capsule()?.querySelectorAll("[data-ws-recovery-action]") ?? [])]
.map((button) => button.getAttribute("data-ws-recovery-action")),
};
capsule().dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
await nextTick();
await sleep(150);
const afterMacEscape = {
capsuleOpen: isOpen(capsule()),
toolbarOpen: isOpen(toolbar()),
};
// Invalid / unreadable toolbar tokens must not make the button invisible.
const voiceAnchor = document.getElementById("voice-anchor");
voiceAnchor.style.setProperty("--skin-recovery-toolbar-background", "rgba(0,0,0,0)");
@@ -242,6 +268,8 @@ try {
redocked,
shortcut,
afterEscape,
macShortcut,
afterMacEscape,
invalidTokens,
});
} catch (error) {
+9
View File
@@ -593,6 +593,15 @@ test("toolbar-mode recovery docks plain host buttons, de-duplicates the library
assert.equal(payload.afterEscape.capsuleOpen, false);
assert.equal(payload.afterEscape.toolbarOpen, true, "closing the shortcut did not restore the docked toolbar");
// The macOS chord arrives with a derived `key` (®) + physical `code` KeyR,
// under a capture listener that calls preventDefault(); it must still work.
assert.equal(payload.macShortcut.capsuleOpen, true, "the macOS ⌘⌥⇧R chord did not summon the capsule");
assert.equal(payload.macShortcut.toolbarOpen, false, "the macOS chord must yield to the summoned capsule");
assert.equal(payload.macShortcut.focusedAction, "built-in", "the macOS chord did not focus the first host button");
assert.deepEqual(payload.macShortcut.capsuleActions, ["built-in", "library"]);
assert.equal(payload.afterMacEscape.capsuleOpen, false);
assert.equal(payload.afterMacEscape.toolbarOpen, true, "closing the macOS chord did not restore the docked toolbar");
assert.equal(payload.invalidTokens.visible, true, "invalid toolbar tokens hid the button");
assert.equal(payload.invalidTokens.inViewport, true);
assert.equal(payload.invalidTokens.background, "rgb(255, 255, 255)", "transparent background must fall back to a visible surface");