Skip to content
Closed
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: 3 additions & 18 deletions apps/web/src/components/settings/KeybindingsSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {

import { isElectron } from "../../env";
import { useOpenInPreferredEditor } from "../../editorPreferences";
import { formatShortcutLabel } from "../../keybindings";
import { formatShortcutLabel, formatShortcutTokenLabel } from "../../keybindings";
import { cn } from "../../lib/utils";
import {
primaryServerAvailableEditorsAtom,
Expand Down Expand Up @@ -74,26 +74,11 @@ import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import { useAtomCommand } from "../../state/use-atom-command";

function KeybindingPill({ value }: { value: string }) {
const parts = value.split("+");
return (
<KbdGroup className="bg-transparent p-0 shadow-none">
{parts.map((part) => (
{value.split("+").map((part) => (
<Kbd key={part} className="min-w-6 justify-center px-1.5">
{part === "mod"
? navigator.platform.toLowerCase().includes("mac")
? "⌘"
: "Ctrl"
: part === "shift"
? "⇧"
: part === "alt"
? navigator.platform.toLowerCase().includes("mac")
? "⌥"
: "Alt"
: part === "ctrl"
? "⌃"
: part.length === 1
? part.toUpperCase()
: part}
{formatShortcutTokenLabel(part)}
</Kbd>
))}
</KbdGroup>
Expand Down
59 changes: 48 additions & 11 deletions apps/web/src/keybindings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "@t3tools/contracts";
import {
formatShortcutLabel,
formatShortcutTokenLabel,
isChatNewShortcut,
isChatNewLocalShortcut,
isDiffToggleShortcut,
Expand Down Expand Up @@ -308,7 +309,7 @@ describe("shortcutLabelForCommand", () => {
platform: "Linux",
context: { terminalFocus: false },
}),
"Ctrl+Shift+\\",
"⌃⇧\\",
);
});

Expand All @@ -318,7 +319,7 @@ describe("shortcutLabelForCommand", () => {
"⌘B",
);
assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "chat.new", "MacIntel"), "⇧⌘O");
assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "diff.toggle", "Linux"), "Ctrl+D");
assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "diff.toggle", "Linux"), "⌃D");
assert.strictEqual(
shortcutLabelForCommand(DEFAULT_BINDINGS, "rightPanel.toggle", "MacIntel"),
"⌥⌘B",
Expand All @@ -329,19 +330,19 @@ describe("shortcutLabelForCommand", () => {
);
assert.strictEqual(
shortcutLabelForCommand(DEFAULT_BINDINGS, "modelPicker.toggle", "Linux"),
"Ctrl+Shift+M",
"⌃⇧M",
);
assert.strictEqual(
shortcutLabelForCommand(DEFAULT_BINDINGS, "editor.openFavorite", "Linux"),
"Ctrl+O",
"⌃O",
);
assert.strictEqual(
shortcutLabelForCommand(DEFAULT_BINDINGS, "thread.jump.3", "MacIntel"),
"⌘3",
);
assert.strictEqual(
shortcutLabelForCommand(DEFAULT_BINDINGS, "thread.previous", "Linux"),
"Ctrl+Shift+[",
"⌃⇧[",
);
assert.strictEqual(
shortcutLabelForCommand(DEFAULT_BINDINGS, "modelPicker.jump.3", {
Expand Down Expand Up @@ -377,7 +378,7 @@ describe("shortcutLabelForCommand", () => {
platform: "Linux",
context: { terminalFocus: false },
}),
"Ctrl+D",
"⌃D",
);
assert.isNull(
shortcutLabelForCommand(bindings, "diff.toggle", {
Expand All @@ -390,7 +391,7 @@ describe("shortcutLabelForCommand", () => {
platform: "Linux",
context: { terminalFocus: true },
}),
"Ctrl+D",
"⌃D",
);
});
});
Expand Down Expand Up @@ -647,16 +648,52 @@ describe("formatShortcutLabel", () => {
);
});

it("formats labels for non-macOS", () => {
it("formats labels for non-macOS with the same glyphs", () => {
assert.strictEqual(formatShortcutLabel(modShortcut("d", { shiftKey: true }), "Linux"), "⌃⇧D");
});

it("separates a word-form non-mac meta key without splitting glyph runs", () => {
assert.strictEqual(
formatShortcutLabel(modShortcut("d", { shiftKey: true }), "Linux"),
"Ctrl+Shift+D",
formatShortcutLabel({ ...modShortcut("b"), modKey: false, metaKey: true }, "Linux"),
"Super+B",
);
assert.strictEqual(
formatShortcutLabel(modShortcut("k", { shiftKey: true, metaKey: true }), "Linux"),
"⌃⇧+Super+K",
);
});

it("uses Apple's key glyphs, not words", () => {
assert.strictEqual(formatShortcutLabel(modShortcut("escape"), "Linux"), "⌃⎋");
assert.strictEqual(formatShortcutLabel(modShortcut("backspace"), "Linux"), "⌃⌫");
assert.strictEqual(formatShortcutLabel(modShortcut("tab"), "MacIntel"), "⌘⇥");
assert.strictEqual(formatShortcutLabel(modShortcut("arrowup"), "Linux"), "⌃↑");
// No established glyph, so the word survives.
assert.strictEqual(formatShortcutLabel(modShortcut("f1"), "Linux"), "⌃F1");
});

it("formats labels for plus key", () => {
assert.strictEqual(formatShortcutLabel(modShortcut("+"), "MacIntel"), "⌘+");
assert.strictEqual(formatShortcutLabel(modShortcut("+"), "Linux"), "Ctrl++");
assert.strictEqual(formatShortcutLabel(modShortcut("+"), "Linux"), "⌃+");
});
});

describe("formatShortcutTokenLabel", () => {
it("maps binding tokens to Apple glyphs", () => {
assert.strictEqual(formatShortcutTokenLabel("mod", "Linux"), "⌃");
assert.strictEqual(formatShortcutTokenLabel("mod", "MacIntel"), "⌘");
assert.strictEqual(formatShortcutTokenLabel("ctrl", "Linux"), "⌃");
assert.strictEqual(formatShortcutTokenLabel("alt", "Linux"), "⌥");
assert.strictEqual(formatShortcutTokenLabel("shift", "Linux"), "⇧");
assert.strictEqual(formatShortcutTokenLabel("b", "Linux"), "B");
assert.strictEqual(formatShortcutTokenLabel("esc", "Linux"), "⎋");
assert.strictEqual(formatShortcutTokenLabel("escape", "Linux"), "⎋");
assert.strictEqual(formatShortcutTokenLabel("space", "Linux"), "␣");
});

it("keeps a word for a non-mac meta key, which has no Apple glyph", () => {
assert.strictEqual(formatShortcutTokenLabel("meta", "Linux"), "Super");
assert.strictEqual(formatShortcutTokenLabel("meta", "MacIntel"), "⌘");
});
});

Expand Down
85 changes: 65 additions & 20 deletions apps/web/src/keybindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,39 +216,84 @@ export function resolveShortcutCommand(
return null;
}

/**
* Apple's glyphs for the non-letter keys, from Apple's Human Interface
* Guidelines. Keys with no established glyph (function keys, punctuation) keep
* their word form.
*/
const SHORTCUT_KEY_SYMBOLS: Readonly<Record<string, string>> = {
" ": "␣",
space: "␣",
arrowdown: "↓",
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Resolved
arrowleft: "←",
arrowright: "→",
arrowup: "↑",
backspace: "⌫",
delete: "⌦",
end: "↘",
enter: "⏎",
esc: "⎋",
escape: "⎋",
home: "↖",
pagedown: "⇟",
pageup: "⇞",
return: "⏎",
tab: "⇥",
};

function formatShortcutKeyLabel(key: string): string {
if (key === " ") return "Space";
const symbol = SHORTCUT_KEY_SYMBOLS[key];
if (symbol) return symbol;
if (key.length === 1) return key.toUpperCase();
if (key === "escape") return "Esc";
if (key === "arrowup") return "Up";
if (key === "arrowdown") return "Down";
if (key === "arrowleft") return "Left";
if (key === "arrowright") return "Right";
return key.slice(0, 1).toUpperCase() + key.slice(1);
}

/**
* Apple-style glyph for one token of a raw binding string ("mod+shift+b"), for
* surfaces that render a chord as separate chips rather than one label.
*/
export function formatShortcutTokenLabel(token: string, platform = navigator.platform): string {
const useMetaForMod = isMacPlatform(platform);
switch (token) {
case "mod":
return useMetaForMod ? "⌘" : "⌃";
case "meta":
return useMetaForMod ? "⌘" : "Super";
case "ctrl":
return "⌃";
case "alt":
return "⌥";
case "shift":
return "⇧";
default:
return formatShortcutKeyLabel(token);
}
Comment thread
colonelpanic8 marked this conversation as resolved.
Resolved
}

/**
* Shortcuts render Apple-style everywhere: modifier and key glyphs in Apple's
* canonical order with no separators, on every platform. `mod+shift+e` is
* `⌃⇧E` on Linux/Windows and `⇧⌘E` on macOS. A non-mac meta/super key has no
* Apple glyph, so it stays a `+`-delimited word rather than borrowing `⌘`.
*/
export function formatShortcutLabel(
shortcut: KeybindingShortcut,
platform = navigator.platform,
): string {
const keyLabel = formatShortcutKeyLabel(shortcut.key);
const useMetaForMod = isMacPlatform(platform);
const showMeta = shortcut.metaKey || (shortcut.modKey && useMetaForMod);
const showCtrl = shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod);
const showAlt = shortcut.altKey;
const showShift = shortcut.shiftKey;
const glyphModifiers = [
shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod) ? "⌃" : "",
shortcut.altKey ? "⌥" : "",
shortcut.shiftKey ? "⇧" : "",
useMetaForMod && (shortcut.metaKey || shortcut.modKey) ? "⌘" : "",
].join("");
Comment thread
colonelpanic8 marked this conversation as resolved.
Resolved
const keyLabel = formatShortcutKeyLabel(shortcut.key);

if (useMetaForMod) {
return `${showCtrl ? "\u2303" : ""}${showAlt ? "\u2325" : ""}${showShift ? "\u21e7" : ""}${showMeta ? "\u2318" : ""}${keyLabel}`;
if (!useMetaForMod && shortcut.metaKey) {
return [glyphModifiers, "Super", keyLabel].filter(Boolean).join("+");
}

const parts: string[] = [];
if (showCtrl) parts.push("Ctrl");
if (showAlt) parts.push("Alt");
if (showShift) parts.push("Shift");
if (showMeta) parts.push("Meta");
parts.push(keyLabel);
return parts.join("+");
return `${glyphModifiers}${keyLabel}`;
}

export function shortcutLabelForCommand(
Expand Down
Loading