Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions apps/web/src/components/settings/KeybindingsSettings.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,27 @@ describe("KeybindingsSettings.logic", () => {
).toEqual(["New local thread", ...expected.ctrl].toSorted());
});

it.each([
{
platform: "MacIntel",
visible: ["", "!isMac || terminalFocus", "isMac", "isMac && isDesktop"],
},
{ platform: "Linux x86_64", visible: ["", "!isMac", "!isMac || terminalFocus"] },
])("lists only the $platform platform variants", ({ platform, visible }) => {
const rows = buildKeybindingRows(
compileResolvedKeybindingsConfig([
{ command: "layout.focusDown", key: "mod+alt+arrowdown", when: "isMac" },
{ command: "layout.focusDown", key: "alt+shift+arrowdown", when: "!isMac" },
{ command: "layout.focusDown", key: "mod+alt+j", when: "isMac && isDesktop" },
{ command: "layout.focusDown", key: "ctrl+j", when: "!isMac || terminalFocus" },
{ command: "layout.focusDown", key: "f9" },
]),
platform,
);

expect(rows.map((row) => row.when).toSorted()).toEqual(visible);
});

it("keeps mod and ctrl shortcuts apart when their when contexts differ", () => {
const rows = buildKeybindingRows(
compileResolvedKeybindingsConfig([
Expand Down
26 changes: 26 additions & 0 deletions apps/web/src/components/settings/KeybindingsSettings.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,30 @@ function defaultBindingForBinding(
);
}

function platformAllowsWhen(node: KeybindingWhenNode | undefined, isMac: boolean): boolean | null {
if (!node) return null;
switch (node.type) {
case "identifier":
return node.name === "isMac" ? isMac : null;
case "not": {
const value = platformAllowsWhen(node.node, isMac);
return value === null ? null : !value;
}
case "and": {
const left = platformAllowsWhen(node.left, isMac);
const right = platformAllowsWhen(node.right, isMac);
if (left === false || right === false) return false;
return left === true && right === true ? true : null;
}
case "or": {
const left = platformAllowsWhen(node.left, isMac);
const right = platformAllowsWhen(node.right, isMac);
if (left === true || right === true) return true;
return left === false && right === false ? false : null;
}
}
}

function keybindingRowId(command: KeybindingCommand, key: string, when: string): string {
return `${command}\u0000${key}\u0000${when}`;
}
Expand Down Expand Up @@ -207,7 +231,9 @@ export function buildKeybindingRows(
platform = navigator.platform,
): ReadonlyArray<KeybindingRow> {
const rowsById = new Map<string, KeybindingRow>();
const isMac = isMacPlatform(platform);
for (const binding of keybindings) {
if (platformAllowsWhen(binding.whenAst, isMac) === false) continue;
const defaultBinding = defaultBindingForBinding(binding);
const key = shortcutToKeybindingInput(binding.shortcut);
const when = whenAstToExpression(binding.whenAst);
Expand Down
Loading