-
Notifications
You must be signed in to change notification settings - Fork 3k
fix: adapt keyboard shortcut display for macOS #2484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, it, expect, vi, afterEach } from 'vitest'; | ||
|
|
||
| describe('formatShortcut', () => { | ||
| const originalPlatform = process.platform; | ||
|
|
||
| afterEach(() => { | ||
| Object.defineProperty(process, 'platform', { value: originalPlatform }); | ||
| vi.resetModules(); | ||
| }); | ||
|
|
||
| it('converts modifier keys to Mac symbols on darwin', async () => { | ||
| Object.defineProperty(process, 'platform', { value: 'darwin' }); | ||
| const { formatShortcut } = await import('./shortcutFormatter.js'); | ||
| expect(formatShortcut('ctrl+y')).toBe('⌃Y'); | ||
| expect(formatShortcut('cmd+v')).toBe('⌘V'); | ||
| expect(formatShortcut('alt+v')).toBe('⌥V'); | ||
| expect(formatShortcut('shift+tab')).toBe('⇧TAB'); | ||
| }); | ||
|
|
||
| it('handles multi-part shortcuts on darwin', async () => { | ||
| Object.defineProperty(process, 'platform', { value: 'darwin' }); | ||
| const { formatShortcut } = await import('./shortcutFormatter.js'); | ||
| expect(formatShortcut('esc esc')).toBe('ESC ESC'); | ||
| }); | ||
|
|
||
| it('returns input unchanged on non-darwin', async () => { | ||
| Object.defineProperty(process, 'platform', { value: 'linux' }); | ||
| const { formatShortcut } = await import('./shortcutFormatter.js'); | ||
| expect(formatShortcut('ctrl+y')).toBe('ctrl+y'); | ||
| expect(formatShortcut('ctrl+c')).toBe('ctrl+c'); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| const isMac = process.platform === 'darwin'; | ||
|
|
||
| /** | ||
| * Maps modifier names to macOS symbol equivalents. | ||
| */ | ||
| const MAC_MODIFIERS: Record<string, string> = { | ||
| ctrl: '⌃', | ||
| cmd: '⌘', | ||
| alt: '⌥', | ||
| shift: '⇧', | ||
| }; | ||
|
|
||
| /** | ||
| * Formats a keyboard shortcut string for display, using macOS symbols when | ||
| * running on Darwin. | ||
| * | ||
| * Examples (on macOS): | ||
| * "ctrl+y" → "⌃Y" | ||
| * "cmd+v" → "⌘V" | ||
| * "ctrl+c" → "⌃C" | ||
| * | ||
| * On other platforms the input is returned unchanged. | ||
| */ | ||
| export function formatShortcut(shortcut: string): string { | ||
| if (!isMac) { | ||
| return shortcut; | ||
| } | ||
|
|
||
| return shortcut | ||
| .split(/\s+/) | ||
| .map((combo) => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Nice to have] — Qwen Code /review |
||
| const parts = combo.split('+'); | ||
| let result = ''; | ||
| for (const part of parts) { | ||
| const lower = part.toLowerCase(); | ||
| if (MAC_MODIFIERS[lower]) { | ||
| result += MAC_MODIFIERS[lower]; | ||
| } else { | ||
| result += part.toUpperCase(); | ||
| } | ||
| } | ||
| return result; | ||
| }) | ||
| .join(' '); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Nice to have] Consider adding test cases for combined modifiers (
ctrl+shift+y→⌃⇧Y), single-character keys (!,/,@), and mixed-case input (Ctrl+Y) to catch regressions if future shortcuts use these patterns.— Qwen Code /review