Skip to content

Commit 40c0b0e

Browse files
only21milclaude
andauthored
fix(web): warn when ctrl shortcuts collide with mod on Windows and Linux (#22496)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 65fc784 commit 40c0b0e

2 files changed

Lines changed: 52 additions & 7 deletions

File tree

‎apps/web/src/components/settings/KeybindingsSettings.logic.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,38 @@ describe("KeybindingsSettings.logic", () => {
266266
}),
267267
).toEqual(["Chat: New Local"]);
268268
});
269+
270+
it.each([
271+
{ platform: "Linux x86_64", ctrl: ["Chat: New"], meta: [] },
272+
{ platform: "Win32", ctrl: ["Chat: New"], meta: [] },
273+
{ platform: "MacIntel", ctrl: [], meta: ["Chat: New"] },
274+
])("resolves mod to the $platform modifier when reporting conflicts", (expected) => {
275+
const rows = buildKeybindingRows(
276+
compileResolvedKeybindingsConfig([
277+
{ command: "chat.new", key: "mod+k" },
278+
{ command: "chat.newLocal", key: "ctrl+k" },
279+
{ command: "thread.copyReference", key: "meta+k" },
280+
]),
281+
expected.platform,
282+
);
283+
const conflictsFor = (key: string) => rows.find((row) => row.key === key)?.conflicts;
284+
285+
expect(conflictsFor("ctrl+k")).toEqual(expected.ctrl);
286+
expect(conflictsFor("meta+k")).toEqual(expected.meta);
287+
expect(
288+
keybindingConflictLabels(rows, { rowId: "new", key: "ctrl+k", when: "" }, expected.platform),
289+
).toEqual(["Chat: New Local", ...expected.ctrl].toSorted());
290+
});
291+
292+
it("keeps mod and ctrl shortcuts apart when their when contexts differ", () => {
293+
const rows = buildKeybindingRows(
294+
compileResolvedKeybindingsConfig([
295+
{ command: "chat.new", key: "mod+k", when: "!terminalFocus" },
296+
{ command: "chat.newLocal", key: "ctrl+k", when: "terminalFocus" },
297+
]),
298+
"Linux x86_64",
299+
);
300+
301+
expect(rows.map((row) => row.conflicts)).toEqual([[], []]);
302+
});
269303
});

‎apps/web/src/components/settings/KeybindingsSettings.logic.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import {
88
} from "@t3tools/contracts";
99
import {
1010
DEFAULT_RESOLVED_KEYBINDINGS,
11+
parseKeybindingShortcut,
1112
parseKeybindingWhenExpression,
1213
US_SHIFTED_TO_UNSHIFTED_KEYS,
1314
} from "@t3tools/shared/keybindings";
1415

15-
import { shortcutKeyFromEvent } from "../../keybindings";
16+
import { shortcutConflictKey, shortcutKeyFromEvent } from "../../keybindings";
1617
import { isMacPlatform } from "../../lib/utils";
1718
import { METRIC_OPTIONS, WINDOW_OPTIONS } from "../usage/usageShortcuts";
1819

@@ -180,13 +181,18 @@ function conflictsWithWhen(leftWhen: string, rightWhen: string): boolean {
180181
export function keybindingConflictLabels(
181182
rows: ReadonlyArray<KeybindingRow>,
182183
input: { readonly rowId: string; readonly key: string; readonly when: string },
184+
platform = navigator.platform,
183185
): ReadonlyArray<string> {
184186
if (input.key.trim().length === 0) return [];
187+
const shortcut = parseKeybindingShortcut(input.key);
188+
if (!shortcut) return [];
189+
// Compare resolved chords so mod+k and ctrl+k collide where mod is Ctrl.
190+
const conflictKey = shortcutConflictKey(shortcut, platform);
185191
const conflicts: Array<string> = [];
186192
for (const candidate of rows) {
187193
if (
188194
candidate.id !== input.rowId &&
189-
candidate.key === input.key &&
195+
shortcutConflictKey(candidate.binding.shortcut, platform) === conflictKey &&
190196
conflictsWithWhen(candidate.when, input.when)
191197
) {
192198
conflicts.push(commandLabel(candidate.command));
@@ -242,6 +248,7 @@ export function groupKeybindingRows(
242248

243249
export function buildKeybindingRows(
244250
keybindings: ResolvedKeybindingsConfig,
251+
platform = navigator.platform,
245252
): ReadonlyArray<KeybindingRow> {
246253
const rowsById = new Map<string, KeybindingRow>();
247254
for (const binding of keybindings) {
@@ -264,11 +271,15 @@ export function buildKeybindingRows(
264271
const rows = [...rowsById.values()];
265272

266273
const rowsWithConflicts = rows.map((row) => {
267-
const conflicts = keybindingConflictLabels(rows, {
268-
rowId: row.id,
269-
key: row.key,
270-
when: row.when,
271-
});
274+
const conflicts = keybindingConflictLabels(
275+
rows,
276+
{
277+
rowId: row.id,
278+
key: row.key,
279+
when: row.when,
280+
},
281+
platform,
282+
);
272283
return conflicts.length > 0
273284
? Object.assign({}, row, { conflicts: [...new Set(conflicts)].toSorted() })
274285
: row;

0 commit comments

Comments
 (0)