desktop: Show shortcut hints only when Mod is held alone

The sidebar numbers flashed on every ⌘Tab and showed up in ⌘⇧5
screenshots, then stuck until ⌘ was pressed again because the
screenshot overlay swallows the keyups. Now they wait for ⌘ to be
held on its own for a moment and hide on any sign it was let go.
This commit is contained in:
Rūdolfs Ošiņš
2026-11-12 10:09:46 +01:00
parent 0ef41cc23b
commit 2a504c2b38
2 changed files with 113 additions and 16 deletions
+44 -9
View File
@@ -127,6 +127,8 @@ export interface Binding {
let registered = $state.raw<Binding[]>([]);
let modifierDown = $state(false);
let hintTimer: ReturnType<typeof setTimeout> | undefined;
const hintDelayMs = 400;
// Whether Mod is held down in a state where Mod shortcuts would run.
export const modifierHeld = {
@@ -232,6 +234,16 @@ function isModifierKey(event: KeyboardEvent): boolean {
return event.key === (isMac() ? "Meta" : "Control");
}
function isModHeld(event: KeyboardEvent | PointerEvent): boolean {
return isMac() ? event.metaKey : event.ctrlKey;
}
function otherModifiersHeld(event: KeyboardEvent): boolean {
return (
event.shiftKey || event.altKey || (isMac() ? event.ctrlKey : event.metaKey)
);
}
// Fields marked with `data-mod-shortcuts`, like filters, hold nothing worth
// protecting, so shortcuts with Mod still work from them.
function blocksShortcuts(event: KeyboardEvent, modDown: boolean): boolean {
@@ -246,10 +258,19 @@ function blocksShortcuts(event: KeyboardEvent, modDown: boolean): boolean {
function onKeydown(event: KeyboardEvent) {
const modalOpen = get(modalStore) !== undefined;
if (isModifierKey(event)) {
modifierDown = !modalOpen && !blocksShortcuts(event, true);
const modDown = isModHeld(event);
if (isModifierKey(event) && !otherModifiersHeld(event)) {
if (!event.repeat && !modalOpen && !blocksShortcuts(event, true)) {
clearTimeout(hintTimer);
hintTimer = setTimeout(() => {
modifierDown = true;
}, hintDelayMs);
}
} else if (!modDown || otherModifiersHeld(event)) {
hideHints();
} else {
clearTimeout(hintTimer);
}
const modDown = isMac() ? event.metaKey : event.ctrlKey;
// Keys a component already handled, and anything typed into a field, are
// left alone.
if (
@@ -292,23 +313,37 @@ function onKeydown(event: KeyboardEvent) {
}
}
function hideHints() {
clearTimeout(hintTimer);
hintTimer = undefined;
modifierDown = false;
}
function onKeyup(event: KeyboardEvent) {
if (isModifierKey(event)) {
modifierDown = false;
if (!isModHeld(event)) {
hideHints();
}
}
function onBlur() {
modifierDown = false;
function onPointer(event: PointerEvent) {
if (!isModHeld(event)) {
hideHints();
}
}
export function listenForShortcuts(): () => void {
document.addEventListener("keydown", onKeydown);
document.addEventListener("keyup", onKeyup);
window.addEventListener("blur", onBlur);
document.addEventListener("pointermove", onPointer);
document.addEventListener("pointerdown", onPointer);
document.addEventListener("visibilitychange", hideHints);
window.addEventListener("blur", hideHints);
return () => {
document.removeEventListener("keydown", onKeydown);
document.removeEventListener("keyup", onKeyup);
window.removeEventListener("blur", onBlur);
document.removeEventListener("pointermove", onPointer);
document.removeEventListener("pointerdown", onPointer);
document.removeEventListener("visibilitychange", hideHints);
window.removeEventListener("blur", hideHints);
};
}
+69 -7
View File
@@ -128,7 +128,13 @@ describe("dispatch", () => {
return event;
}
function hold(modifier: string, target: EventTarget = document) {
press({ key: modifier }, target);
vi.advanceTimersByTime(400);
}
beforeEach(() => {
vi.useFakeTimers();
stopListening = listenForShortcuts();
});
@@ -139,6 +145,7 @@ describe("dispatch", () => {
stopListening();
modal.forceHide();
document.body.replaceChildren();
vi.useRealTimers();
});
test("runs a matching binding with the combo index and prevents default", () => {
@@ -284,7 +291,7 @@ describe("dispatch", () => {
test("stops listening once the listener is removed", () => {
const run = vi.fn();
bind({ shortcut: "help", run });
press({ key: "Control" });
hold("Control");
stopListening();
press({ key: "?" });
@@ -301,17 +308,23 @@ describe("dispatch", () => {
[true, "Meta"],
])("modifierHeld follows mac %s %s", (mac, modifier) => {
platform.mac = mac;
press({ key: mac ? "Control" : "Meta" });
hold(mac ? "Control" : "Meta");
expect(modifierHeld.value).toBe(false);
press({ key: modifier });
hold(modifier);
expect(modifierHeld.value).toBe(true);
document.dispatchEvent(new KeyboardEvent("keyup", { key: "Shift" }));
document.dispatchEvent(
new KeyboardEvent("keyup", {
key: "Shift",
ctrlKey: !mac,
metaKey: mac,
}),
);
expect(modifierHeld.value).toBe(true);
document.dispatchEvent(new KeyboardEvent("keyup", { key: modifier }));
expect(modifierHeld.value).toBe(false);
press({ key: modifier });
hold(modifier);
window.dispatchEvent(new Event("blur"));
expect(modifierHeld.value).toBe(false);
});
@@ -324,7 +337,7 @@ describe("dispatch", () => {
if (attribute) input.setAttribute(attribute, "");
document.body.append(input);
press({ key: "Control" }, input);
hold("Control", input);
expect(modifierHeld.value).toBe(held);
});
@@ -332,8 +345,57 @@ describe("dispatch", () => {
test("modifierHeld stays false while a modal is open", () => {
modal.show({ component: vi.fn(), props: {} });
press({ key: "Control" });
hold("Control");
expect(modifierHeld.value).toBe(false);
});
test("modifierHeld waits for the modifier to be held", () => {
press({ key: "Control" });
vi.advanceTimersByTime(399);
expect(modifierHeld.value).toBe(false);
press({ key: "Control", ctrlKey: true, repeat: true });
vi.advanceTimersByTime(1);
expect(modifierHeld.value).toBe(true);
});
test("modifierHeld stays false during a chord", () => {
press({ key: "Control" });
press({ key: "Shift", ctrlKey: true, shiftKey: true });
vi.advanceTimersByTime(400);
expect(modifierHeld.value).toBe(false);
});
test("modifierHeld stays false when another modifier went down first", () => {
press({ key: "Shift", shiftKey: true });
press({ key: "Control", ctrlKey: true, shiftKey: true });
vi.advanceTimersByTime(400);
expect(modifierHeld.value).toBe(false);
});
test("modifierHeld clears when another modifier joins", () => {
hold("Control");
press({ key: "Shift", ctrlKey: true, shiftKey: true });
expect(modifierHeld.value).toBe(false);
});
test("modifierHeld clears on any keyup without the modifier", () => {
hold("Control");
document.dispatchEvent(new KeyboardEvent("keyup", { key: "Escape" }));
expect(modifierHeld.value).toBe(false);
});
test("modifierHeld clears when a lost keyup shows on the pointer", () => {
hold("Control");
document.dispatchEvent(new PointerEvent("pointermove", { ctrlKey: true }));
expect(modifierHeld.value).toBe(true);
document.dispatchEvent(new PointerEvent("pointermove"));
expect(modifierHeld.value).toBe(false);
});
});