-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Allow Command-Escape keybindings #4307
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
Closed
jakeleventhal
wants to merge
17
commits into
pingdotgg:main
from
jakeleventhal:t3code/allow-esc-keybindings
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
195cfdb
Allow Command-Escape keybindings
jakeleventhal 3b8c083
Fix native Escape shortcut routing
jakeleventhal d7583f5
fix(desktop): preserve modified Escape shortcuts
jakeleventhal d83a665
fix(desktop): harden native Escape capture
jakeleventhal bbbeb0e
Merge remote-tracking branch 'upstream/main' into t3code/upstream-con…
jakeleventhal 9b46960
fix(web): reserve only mod-Escape keybindings
jakeleventhal 9458be4
fix(web): reserve mod-Escape across dismissals
jakeleventhal 991c566
fix(web): align script keybinding capture
jakeleventhal 61cbcab
Merge remote-tracking branch 'upstream/main' into t3code/upstream-con…
jakeleventhal 689ec23
fix(web): keep mod-Escape recorder open
jakeleventhal 635e573
fix(web): preserve mod-Escape in Base UI overlays
jakeleventhal 9ed0163
fix(web): guard overlay Escape dismissal
jakeleventhal c87375d
fix(web): reserve mod-Escape in overlay roots
jakeleventhal 046e768
fix(web): propagate reserved Escape shortcuts
jakeleventhal 902e811
Merge remote-tracking branch 'upstream/main' into t3code/upstream-con…
jakeleventhal e0b0897
fix(desktop): scope native shortcut listeners
jakeleventhal 9ce96d8
Merge remote-tracking branch 'upstream/main' into t3code/upstream-con…
jakeleventhal 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
120 changes: 120 additions & 0 deletions
120
apps/desktop/src/keybindings/NativeKeybindingCapture.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,120 @@ | ||
| import { describe, expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| import { | ||
| dispatchNativeKeybindingCaptureInput, | ||
| nativeKeybindingCaptureInput, | ||
| } from "./NativeKeybindingCapture.ts"; | ||
|
|
||
| describe("nativeKeybindingCaptureInput", () => { | ||
| it("forwards Command-Escape with its modifiers", () => { | ||
| expect( | ||
| nativeKeybindingCaptureInput( | ||
| { | ||
| type: "keyDown", | ||
| key: "Escape", | ||
| meta: true, | ||
| control: false, | ||
| alt: false, | ||
| shift: true, | ||
| }, | ||
| "darwin", | ||
| ), | ||
| ).toEqual({ | ||
| key: "Escape", | ||
| metaKey: true, | ||
| ctrlKey: false, | ||
| altKey: false, | ||
| shiftKey: true, | ||
| }); | ||
| }); | ||
|
|
||
| it("forwards Control-Escape as mod+esc on non-macOS platforms", () => { | ||
| expect( | ||
| nativeKeybindingCaptureInput( | ||
| { | ||
| type: "keyDown", | ||
| key: "Escape", | ||
| meta: false, | ||
| control: true, | ||
| alt: true, | ||
| shift: false, | ||
| }, | ||
| "win32", | ||
| ), | ||
| ).toEqual({ | ||
| key: "Escape", | ||
| metaKey: false, | ||
| ctrlKey: true, | ||
| altKey: true, | ||
| shiftKey: false, | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["bare Escape", { type: "keyDown", key: "Escape", meta: false }, "darwin"], | ||
| ["Command keyup", { type: "keyUp", key: "Escape", meta: true }, "darwin"], | ||
| ["another Command shortcut", { type: "keyDown", key: "k", meta: true }, "darwin"], | ||
| ["Meta-Escape on Windows", { type: "keyDown", key: "Escape", meta: true }, "win32"], | ||
| ] satisfies ReadonlyArray< | ||
| readonly [ | ||
| string, | ||
| { readonly type: string; readonly key: string; readonly meta: boolean }, | ||
| NodeJS.Platform, | ||
| ] | ||
| >)("ignores %s", (_name, input, platform) => { | ||
| expect( | ||
| nativeKeybindingCaptureInput( | ||
| { | ||
| control: false, | ||
| alt: false, | ||
| shift: false, | ||
| ...input, | ||
| }, | ||
| platform, | ||
| ), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("dispatches native shortcuts at the app window unless a keybinding recorder is active", () => { | ||
| const appDispatch = vi.fn(() => true); | ||
| const activeDispatch = vi.fn(() => true); | ||
| const activeElement = { | ||
| dispatchEvent: activeDispatch, | ||
| hasAttribute: vi.fn(() => false), | ||
| }; | ||
| vi.stubGlobal("window", { dispatchEvent: appDispatch }); | ||
| vi.stubGlobal("document", { activeElement }); | ||
| vi.stubGlobal( | ||
| "KeyboardEvent", | ||
| class { | ||
| readonly type: string; | ||
| readonly init: KeyboardEventInit; | ||
|
|
||
| constructor(type: string, init: KeyboardEventInit) { | ||
| this.type = type; | ||
| this.init = init; | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| const input = { | ||
| key: "Escape" as const, | ||
| metaKey: true, | ||
| ctrlKey: false, | ||
| altKey: false, | ||
| shiftKey: false, | ||
| }; | ||
|
|
||
| try { | ||
| dispatchNativeKeybindingCaptureInput(input); | ||
| expect(appDispatch).toHaveBeenCalledOnce(); | ||
| expect(activeDispatch).not.toHaveBeenCalled(); | ||
|
|
||
| activeElement.hasAttribute.mockReturnValue(true); | ||
| dispatchNativeKeybindingCaptureInput(input); | ||
| expect(activeDispatch).toHaveBeenCalledOnce(); | ||
| } finally { | ||
| vi.unstubAllGlobals(); | ||
| } | ||
| }); | ||
| }); |
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,66 @@ | ||
| import type { Input } from "electron"; | ||
|
|
||
| export const NATIVE_KEYBINDING_CAPTURE_CHANNEL = "desktop:native-keybinding-capture"; | ||
|
|
||
| export interface NativeKeybindingCaptureInput { | ||
| readonly key: "Escape"; | ||
| readonly metaKey: boolean; | ||
| readonly ctrlKey: boolean; | ||
| readonly altKey: boolean; | ||
| readonly shiftKey: boolean; | ||
| } | ||
|
|
||
| export function nativeKeybindingCaptureInput( | ||
| input: Pick<Input, "type" | "key" | "meta" | "control" | "alt" | "shift">, | ||
| platform: NodeJS.Platform, | ||
| ): NativeKeybindingCaptureInput | null { | ||
| const key = input.key.toLowerCase(); | ||
| const modPressed = platform === "darwin" ? input.meta : input.control; | ||
| if (input.type !== "keyDown" || (key !== "escape" && key !== "esc") || !modPressed) { | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| key: "Escape", | ||
| metaKey: input.meta, | ||
| ctrlKey: input.control, | ||
| altKey: input.alt, | ||
| shiftKey: input.shift, | ||
| }; | ||
| } | ||
|
|
||
| export function dispatchNativeKeybindingCaptureInput(input: unknown): void { | ||
| if ( | ||
| typeof input !== "object" || | ||
| input === null || | ||
| !("key" in input) || | ||
| input.key !== "Escape" || | ||
| !("metaKey" in input) || | ||
| typeof input.metaKey !== "boolean" || | ||
| !("ctrlKey" in input) || | ||
| typeof input.ctrlKey !== "boolean" || | ||
| (!input.metaKey && !input.ctrlKey) || | ||
| !("altKey" in input) || | ||
| typeof input.altKey !== "boolean" || | ||
| !("shiftKey" in input) || | ||
| typeof input.shiftKey !== "boolean" | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const activeElement = document.activeElement; | ||
| const target = activeElement?.hasAttribute("data-keybinding-capture") ? activeElement : window; | ||
|
|
||
| target.dispatchEvent( | ||
| new KeyboardEvent("keydown", { | ||
|
jakeleventhal marked this conversation as resolved.
|
||
| key: input.key, | ||
| code: "Escape", | ||
| metaKey: input.metaKey, | ||
| ctrlKey: input.ctrlKey, | ||
| altKey: input.altKey, | ||
| shiftKey: input.shiftKey, | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }), | ||
| ); | ||
| } | ||
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
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
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
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
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.