-
Notifications
You must be signed in to change notification settings - Fork 965
fix(desktop): match VS Code terminal clipboard handling (originally by @AytuncYildizli) #3415
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
...main/components/WorkspaceView/ContentView/TabsContent/Terminal/clipboardShortcuts.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import { describe, expect, it } from "bun:test"; | ||
| import { | ||
| shouldBubbleClipboardShortcut, | ||
| shouldSelectAllShortcut, | ||
| } from "./clipboardShortcuts"; | ||
|
|
||
| function makeEvent( | ||
| overrides: Partial<{ | ||
| code: string; | ||
| metaKey: boolean; | ||
| ctrlKey: boolean; | ||
| altKey: boolean; | ||
| shiftKey: boolean; | ||
| }>, | ||
| ) { | ||
| return { | ||
| code: "KeyC", | ||
| metaKey: false, | ||
| ctrlKey: false, | ||
| altKey: false, | ||
| shiftKey: false, | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe("shouldBubbleClipboardShortcut", () => { | ||
| it("matches the VS Code terminal clipboard bindings", () => { | ||
| const cases = [ | ||
| { | ||
| name: "macOS Cmd+V", | ||
| event: makeEvent({ code: "KeyV", metaKey: true }), | ||
| options: { isMac: true, isWindows: false, hasSelection: false }, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "macOS Cmd+C with selection", | ||
| event: makeEvent({ code: "KeyC", metaKey: true }), | ||
| options: { isMac: true, isWindows: false, hasSelection: true }, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "windows Ctrl+V", | ||
| event: makeEvent({ code: "KeyV", ctrlKey: true }), | ||
| options: { isMac: false, isWindows: true, hasSelection: false }, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "windows Ctrl+Shift+V", | ||
| event: makeEvent({ code: "KeyV", ctrlKey: true, shiftKey: true }), | ||
| options: { isMac: false, isWindows: true, hasSelection: false }, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "windows Ctrl+C with selection", | ||
| event: makeEvent({ code: "KeyC", ctrlKey: true }), | ||
| options: { isMac: false, isWindows: true, hasSelection: true }, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "linux Ctrl+Shift+C with selection", | ||
| event: makeEvent({ code: "KeyC", ctrlKey: true, shiftKey: true }), | ||
| options: { isMac: false, isWindows: false, hasSelection: true }, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "linux Ctrl+Shift+V", | ||
| event: makeEvent({ code: "KeyV", ctrlKey: true, shiftKey: true }), | ||
| options: { isMac: false, isWindows: false, hasSelection: false }, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "linux Shift+Insert", | ||
| event: makeEvent({ code: "Insert", shiftKey: true }), | ||
| options: { isMac: false, isWindows: false, hasSelection: false }, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "macOS Cmd+C without selection", | ||
| event: makeEvent({ code: "KeyC", metaKey: true }), | ||
| options: { isMac: true, isWindows: false, hasSelection: false }, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "windows Ctrl+C without selection", | ||
| event: makeEvent({ code: "KeyC", ctrlKey: true }), | ||
| options: { isMac: false, isWindows: true, hasSelection: false }, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "linux Ctrl+Shift+C without selection", | ||
| event: makeEvent({ code: "KeyC", ctrlKey: true, shiftKey: true }), | ||
| options: { isMac: false, isWindows: false, hasSelection: false }, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "linux Ctrl+Insert stays with the PTY", | ||
| event: makeEvent({ code: "Insert", ctrlKey: true }), | ||
| options: { isMac: false, isWindows: false, hasSelection: false }, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "macOS does not inherit linux fallback chords", | ||
| event: makeEvent({ code: "KeyV", ctrlKey: true, shiftKey: true }), | ||
| options: { isMac: true, isWindows: false, hasSelection: false }, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "macOS Shift+Insert stays with the PTY", | ||
| event: makeEvent({ code: "Insert", shiftKey: true }), | ||
| options: { isMac: true, isWindows: false, hasSelection: false }, | ||
| expected: false, | ||
| }, | ||
| ]; | ||
|
|
||
| for (const { name, event, options, expected } of cases) { | ||
| expect(shouldBubbleClipboardShortcut(event, options), name).toBe( | ||
| expected, | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("shouldSelectAllShortcut", () => { | ||
| it("matches only the VS Code macOS terminal select-all binding", () => { | ||
| const cases = [ | ||
| { | ||
| name: "macOS Cmd+A", | ||
| event: makeEvent({ code: "KeyA", metaKey: true }), | ||
| isMac: true, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "windows Ctrl+A is not intercepted", | ||
| event: makeEvent({ code: "KeyA", ctrlKey: true }), | ||
| isMac: false, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "macOS Cmd+Shift+A is not intercepted", | ||
| event: makeEvent({ code: "KeyA", metaKey: true, shiftKey: true }), | ||
| isMac: true, | ||
| expected: false, | ||
| }, | ||
| ]; | ||
|
|
||
| for (const { name, event, isMac, expected } of cases) { | ||
| expect(shouldSelectAllShortcut(event, isMac), name).toBe(expected); | ||
| } | ||
| }); | ||
| }); |
74 changes: 74 additions & 0 deletions
74
...eens/main/components/WorkspaceView/ContentView/TabsContent/Terminal/clipboardShortcuts.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| export interface ClipboardShortcutEvent { | ||
| code: string; | ||
| metaKey: boolean; | ||
| ctrlKey: boolean; | ||
| altKey: boolean; | ||
| shiftKey: boolean; | ||
| } | ||
|
|
||
| export interface ClipboardShortcutOptions { | ||
| isMac: boolean; | ||
| isWindows: boolean; | ||
| hasSelection: boolean; | ||
| } | ||
|
|
||
| /** Match VS Code's macOS terminal `Cmd+A` binding. */ | ||
| export function shouldSelectAllShortcut( | ||
| event: ClipboardShortcutEvent, | ||
| isMac: boolean, | ||
| ): boolean { | ||
| return ( | ||
| isMac && | ||
| event.code === "KeyA" && | ||
| event.metaKey && | ||
| !event.ctrlKey && | ||
| !event.altKey && | ||
| !event.shiftKey | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Mirror VS Code terminal clipboard bindings so host copy/paste can run before | ||
| * xterm's kitty keyboard handler turns the chord into CSI-u input. | ||
| */ | ||
| export function shouldBubbleClipboardShortcut( | ||
| event: ClipboardShortcutEvent, | ||
| options: ClipboardShortcutOptions, | ||
| ): boolean { | ||
| const { isMac, isWindows, hasSelection } = options; | ||
|
|
||
| const onlyMeta = | ||
| event.metaKey && !event.ctrlKey && !event.altKey && !event.shiftKey; | ||
| const onlyCtrl = | ||
| event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey; | ||
| const ctrlShiftOnly = | ||
| event.ctrlKey && event.shiftKey && !event.metaKey && !event.altKey; | ||
| const onlyShift = | ||
| event.shiftKey && !event.ctrlKey && !event.metaKey && !event.altKey; | ||
|
|
||
| if (isMac && onlyMeta) { | ||
| return event.code === "KeyV" || (hasSelection && event.code === "KeyC"); | ||
| } | ||
|
|
||
| if (isWindows) { | ||
| if (event.code === "KeyV" && (onlyCtrl || ctrlShiftOnly)) { | ||
| return true; | ||
| } | ||
|
|
||
| if (hasSelection && event.code === "KeyC" && (onlyCtrl || ctrlShiftOnly)) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| if (!isMac) { | ||
| return ( | ||
| (event.code === "KeyV" && ctrlShiftOnly) || | ||
| (event.code === "Insert" && onlyShift) || | ||
| (hasSelection && event.code === "KeyC" && ctrlShiftOnly) | ||
| ); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.