Skip to content
TypeScript

fix(web): warn when ctrl shortcuts collide with mod on Windows and Linux (#22496)

Victor Vogel40c0b0efe41a
diff --git a/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts b/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts
index 7db73278177..7f57caaf04f 100644
--- a/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts
+++ b/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts
@@ -266,4 +266,38 @@ describe("KeybindingsSettings.logic", () => {
}),
).toEqual(["Chat: New Local"]);
});
+
+ it.each([
+ { platform: "Linux x86_64", ctrl: ["Chat: New"], meta: [] },
+ { platform: "Win32", ctrl: ["Chat: New"], meta: [] },
+ { platform: "MacIntel", ctrl: [], meta: ["Chat: New"] },
+ ])("resolves mod to the $platform modifier when reporting conflicts", (expected) => {
+ const rows = buildKeybindingRows(
+ compileResolvedKeybindingsConfig([
+ { command: "chat.new", key: "mod+k" },
+ { command: "chat.newLocal", key: "ctrl+k" },
+ { command: "thread.copyReference", key: "meta+k" },
+ ]),
+ expected.platform,
+ );
+ const conflictsFor = (key: string) => rows.find((row) => row.key === key)?.conflicts;
+
+ expect(conflictsFor("ctrl+k")).toEqual(expected.ctrl);
+ expect(conflictsFor("meta+k")).toEqual(expected.meta);
+ expect(
+ keybindingConflictLabels(rows, { rowId: "new", key: "ctrl+k", when: "" }, expected.platform),
+ ).toEqual(["Chat: New Local", ...expected.ctrl].toSorted());
+ });
+
+ it("keeps mod and ctrl shortcuts apart when their when contexts differ", () => {
+ const rows = buildKeybindingRows(
+ compileResolvedKeybindingsConfig([
+ { command: "chat.new", key: "mod+k", when: "!terminalFocus" },
+ { command: "chat.newLocal", key: "ctrl+k", when: "terminalFocus" },
+ ]),
+ "Linux x86_64",
+ );
+
+ expect(rows.map((row) => row.conflicts)).toEqual([[], []]);
+ });
});
diff --git a/apps/web/src/components/settings/KeybindingsSettings.logic.ts b/apps/web/src/components/settings/KeybindingsSettings.logic.ts
index 1e75a023438..fe9b47b4dca 100644
--- a/apps/web/src/components/settings/KeybindingsSettings.logic.ts
+++ b/apps/web/src/components/settings/KeybindingsSettings.logic.ts
@@ -8,11 +8,12 @@ import {
} from "@t3tools/contracts";
import {
DEFAULT_RESOLVED_KEYBINDINGS,
+ parseKeybindingShortcut,
parseKeybindingWhenExpression,
US_SHIFTED_TO_UNSHIFTED_KEYS,
} from "@t3tools/shared/keybindings";
-import { shortcutKeyFromEvent } from "../../keybindings";
+import { shortcutConflictKey, shortcutKeyFromEvent } from "../../keybindings";
import { isMacPlatform } from "../../lib/utils";
import { METRIC_OPTIONS, WINDOW_OPTIONS } from "../usage/usageShortcuts";
@@ -180,13 +181,18 @@ function conflictsWithWhen(leftWhen: string, rightWhen: string): boolean {
export function keybindingConflictLabels(
rows: ReadonlyArray<KeybindingRow>,
input: { readonly rowId: string; readonly key: string; readonly when: string },
+ platform = navigator.platform,
): ReadonlyArray<string> {
if (input.key.trim().length === 0) return [];
+ const shortcut = parseKeybindingShortcut(input.key);
+ if (!shortcut) return [];
+ // Compare resolved chords so mod+k and ctrl+k collide where mod is Ctrl.
+ const conflictKey = shortcutConflictKey(shortcut, platform);
const conflicts: Array<string> = [];
for (const candidate of rows) {
if (
candidate.id !== input.rowId &&
- candidate.key === input.key &&
+ shortcutConflictKey(candidate.binding.shortcut, platform) === conflictKey &&
conflictsWithWhen(candidate.when, input.when)
) {
conflicts.push(commandLabel(candidate.command));
@@ -242,6 +248,7 @@ export function groupKeybindingRows(
export function buildKeybindingRows(
keybindings: ResolvedKeybindingsConfig,
+ platform = navigator.platform,
): ReadonlyArray<KeybindingRow> {
const rowsById = new Map<string, KeybindingRow>();
for (const binding of keybindings) {
@@ -264,11 +271,15 @@ export function buildKeybindingRows(
const rows = [...rowsById.values()];
const rowsWithConflicts = rows.map((row) => {
- const conflicts = keybindingConflictLabels(rows, {
- rowId: row.id,
- key: row.key,
- when: row.when,
- });
+ const conflicts = keybindingConflictLabels(
+ rows,
+ {
+ rowId: row.id,
+ key: row.key,
+ when: row.when,
+ },
+ platform,
+ );
return conflicts.length > 0
? Object.assign({}, row, { conflicts: [...new Set(conflicts)].toSorted() })
: row;