Skip to content

Commit 4549bee

Browse files
neominikmaria-rcks
andauthored
fix(web): terminal types macOS Option-layer characters (pingdotgg#21596)
Co-authored-by: maria-rcks <[email protected]>
1 parent 1d78b33 commit 4549bee

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎apps/web/src/terminal/ghostty/core.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,26 @@ describe("GhosttyTerminalCore snapshots", () => {
146146
expect(core.encodeKey(event)).toBe(expected);
147147
});
148148

149+
it.each(["", "\x1b[>1u"])("encodes macOS Option text with keyboard mode %j", async (mode) => {
150+
using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel");
151+
const core = await createCore();
152+
core.write(mode);
153+
const option = {
154+
code: "KeyL",
155+
key: "@",
156+
repeat: false,
157+
shiftKey: false,
158+
ctrlKey: false,
159+
altKey: true,
160+
metaKey: false,
161+
isComposing: false,
162+
getModifierState: () => false,
163+
} as unknown as KeyboardEvent;
164+
165+
expect(core.encodeKey(option)).toBe("@");
166+
expect(core.encodeKey({ ...option, code: "KeyA", key: "Å", shiftKey: true })).toBe("Å");
167+
});
168+
149169
it("preserves styles, wide cells, and selection after shared memory grows", async () => {
150170
const core = await createCore();
151171
const runtime = await loadGhosttyRuntime();

‎apps/web/src/terminal/ghostty/keyCodes.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ describe("ghosttyConsumedMods", () => {
4949
expect(ghosttyConsumedMods({ ...shifted, ctrlKey: true })).toBe(0);
5050
expect(ghosttyConsumedMods({ ...shifted, key: "Tab" })).toBe(0);
5151
expect(ghosttyConsumedMods({ ...shifted, key: " " })).toBe(1);
52+
expect(ghosttyConsumedMods({ ...shifted, altKey: true }, "Linux x86_64")).toBe(0);
53+
});
54+
55+
it("consumes macOS Option when it produced a character", () => {
56+
const option = { ...shifted, altKey: true, key: "[", shiftKey: false };
57+
expect(ghosttyConsumedMods(option, "MacIntel")).toBe(1 << 2);
58+
expect(ghosttyConsumedMods({ ...option, shiftKey: true }, "MacIntel")).toBe(1 | (1 << 2));
59+
expect(ghosttyConsumedMods({ ...option, key: "ArrowLeft" }, "MacIntel")).toBe(0);
60+
expect(ghosttyConsumedMods({ ...option, ctrlKey: true }, "MacIntel")).toBe(0);
61+
expect(ghosttyConsumedMods({ ...option, metaKey: true }, "MacIntel")).toBe(0);
5262
});
5363
});
5464

‎apps/web/src/terminal/ghostty/keyCodes.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { US_SHIFTED_TO_UNSHIFTED_KEYS } from "@t3tools/shared/keybindings";
1+
import { isMacPlatform, US_SHIFTED_TO_UNSHIFTED_KEYS } from "@t3tools/shared/keybindings";
22

33
// This order mirrors GhosttyKey in ghostty/vt/key/event.h. The values are
44
// intentionally derived from the official W3C-aligned enum instead of
@@ -216,12 +216,14 @@ export function loadGhosttyKeyboardLayoutMap(): Promise<GhosttyKeyboardLayoutMap
216216
return promise;
217217
}
218218

219-
// Browsers omit consumed modifiers; treat Shift as consumed for unchorded characters.
219+
// Browsers omit consumed modifiers; macOS Option composes layout characters too.
220220
export function ghosttyConsumedMods(
221221
event: Pick<KeyboardEvent, "altKey" | "ctrlKey" | "key" | "metaKey" | "shiftKey">,
222+
platform = navigator.platform,
222223
): number {
223-
if (!event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) return 0;
224-
return [...event.key].length === 1 ? 1 : 0;
224+
if (event.ctrlKey || event.metaKey || [...event.key].length !== 1) return 0;
225+
if (event.altKey && !isMacPlatform(platform)) return 0;
226+
return (event.shiftKey ? 1 : 0) | (event.altKey ? 1 << 2 : 0);
225227
}
226228

227229
export function ghosttyUnshiftedCodepoint(

0 commit comments

Comments
 (0)