GeezerVison is showing github.com — fix(web): thread navigation shortcuts work from the terminal and macO… · Pipyakas/t3code@8a38c63
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx +39Lines changed: 39 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,6 +26,22 @@ import { | 26 | 26 | type ComposerPromptEditorProps, | 27 | 27 | } from "./ComposerPromptEditorTiptap"; | 28 | 28 |
| | 29 | +vi.mock(import("../state/server"), async (original) => { | | 30 | + const { Atom } = await import("effect/reactivity"); | | 31 | + const { compileResolvedKeybindingsConfig, DEFAULT_KEYBINDINGS } = | | 32 | + await import("@t3tools/shared/keybindings"); | | 33 | + return { | | 34 | + ...(await original()), | | 35 | + primaryServerKeybindingsAtom: Atom.make( | | 36 | + compileResolvedKeybindingsConfig([ | | 37 | + ...DEFAULT_KEYBINDINGS, | | 38 | + { key: "mod+arrowup", command: "thread.previous" }, | | 39 | + { key: "mod+arrowdown", command: "thread.next" }, | | 40 | + ]), | | 41 | + ), | | 42 | + }; | | 43 | +}); | | 44 | + | 29 | 45 | const babysit: ServerProviderSkill = { | 30 | 46 | name: "babysit", | 31 | 47 | path: "/skills/babysit/SKILL.md", | @@ -271,6 +287,29 @@ it.each([ | 271 | 287 | }, | 272 | 288 | ); | 273 | 289 |
| | 290 | +// The mocked server keybindings bind Command-Up and Command-Down to thread navigation. | | 291 | +it("leaves Command-arrow thread navigation bindings to the app on macOS", async () => { | | 292 | + using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel"); | | 293 | + await using screen = await mountEditor("first\nsecond"); | | 294 | + const { editor } = screen; | | 295 | + await act(async () => { | | 296 | + editor.commands.focus("end"); | | 297 | + }); | | 298 | + await vi.waitFor(() => expect(document.activeElement).toBe(editor.view.dom)); | | 299 | + const reachedApp: string[] = []; | | 300 | + const onWindowKeyDown = (event: KeyboardEvent) => { | | 301 | + if (!event.defaultPrevented && event.key.startsWith("Arrow")) reachedApp.push(event.key); | | 302 | + }; | | 303 | + window.addEventListener("keydown", onWindowKeyDown); | | 304 | + using _listener = { | | 305 | + [Symbol.dispose]: () => window.removeEventListener("keydown", onWindowKeyDown), | | 306 | + }; | | 307 | + | | 308 | + await userEvent.keyboard("{Meta>}{ArrowUp}{ArrowDown}{/Meta}"); | | 309 | + | | 310 | + expect(reachedApp).toEqual(["ArrowUp", "ArrowDown"]); | | 311 | +}); | | 312 | + | 274 | 313 | it.each([ | 275 | 314 | ["e", false, false], | 276 | 315 | ["i", false, false], | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/ComposerPromptEditorTiptap.tsx +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -114,6 +114,9 @@ import { collectInlineContextIds } from "~/lib/composerContextReferences"; | 114 | 114 | import { resolveDiffThemeName } from "~/lib/diffRendering"; | 115 | 115 | import { isRichTextFormattingShortcut } from "~/keybindings"; | 116 | 116 | import { cn, isMacPlatform } from "~/lib/utils"; | | 117 | +import { isThreadNavigationShortcut } from "~/keybindings"; | | 118 | +import { appAtomRegistry } from "~/rpc/atomRegistry"; | | 119 | +import { primaryServerKeybindingsAtom } from "~/state/server"; | 117 | 120 | import { hasCommandForKeyDown } from "~/shortcuts/dispatcher"; | 118 | 121 | import { basenameOfPath } from "~/pierre-icons"; | 119 | 122 | import { WorkspaceEntryTooltip } from "./chat/WorkspaceEntryIcon"; | @@ -1143,6 +1146,13 @@ function ComposerPromptEditorTiptapInner( | 1143 | 1146 | !event.ctrlKey && | 1144 | 1147 | !event.isComposing | 1145 | 1148 | ) { | | 1149 | + if ( | | 1150 | + isThreadNavigationShortcut(event, appAtomRegistry.get(primaryServerKeybindingsAtom), { | | 1151 | + context: { editableFocus: true }, | | 1152 | + }) | | 1153 | + ) { | | 1154 | + return false; | | 1155 | + } | 1146 | 1156 | const selection = window.getSelection(); | 1147 | 1157 | if ( | 1148 | 1158 | selection?.anchorNode && | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/ThreadTerminalDrawer.browser.test.tsx +44-1Lines changed: 44 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,10 +4,14 @@ import { | 4 | 4 | ThreadId, | 5 | 5 | type ScopedThreadRef, | 6 | 6 | type FilesystemEntryMetadata, | | 7 | + type ResolvedKeybindingsConfig, | 7 | 8 | } from "@t3tools/contracts"; | | 9 | +import { compileResolvedKeybindingsConfig, DEFAULT_KEYBINDINGS } from "@t3tools/shared/keybindings"; | 8 | 10 | import { Atom, AsyncResult } from "effect/reactivity"; | 9 | 11 | import type { GhosttyTerminalSurfaceOptions } from "~/terminal/ghostty/surface"; | 10 | 12 | import { AppAtomRegistryProvider, appAtomRegistry } from "../rpc/atomRegistry"; | | 13 | +import { useRightPanelStore } from "../rightPanelStore"; | | 14 | +import { useTerminalUiStateStore } from "../terminalUiStateStore"; | 11 | 15 | import { extractTerminalLinks } from "../terminal-links"; | 12 | 16 | import { expect, it, vi } from "vite-plus/test"; | 13 | 17 | import { act, fakeTimers, render } from "../../test/render"; | @@ -110,6 +114,7 @@ function Drawer(props: { | 110 | 114 | threadRef: ScopedThreadRef; | 111 | 115 | cwd?: string; | 112 | 116 | onCloseTerminal: (terminalId: string, threadRef?: ScopedThreadRef) => void; | | 117 | + keybindings?: ResolvedKeybindingsConfig; | 113 | 118 | }) { | 114 | 119 | return ( | 115 | 120 | <ThreadTerminalDrawer | @@ -128,7 +133,7 @@ function Drawer(props: { | 128 | 133 | onCloseTerminal={props.onCloseTerminal} | 129 | 134 | onHeightChange={() => {}} | 130 | 135 | onAddTerminalContext={() => {}} | 131 | | - keybindings={[]} | | 136 | + keybindings={props.keybindings ?? []} | 132 | 137 | /> | 133 | 138 | ); | 134 | 139 | } | @@ -149,6 +154,8 @@ function setup() { | 149 | 154 | fixture.pendingSurfaces = []; | 150 | 155 | fixture.surfaceOptions = []; | 151 | 156 | fixture.metadata.clear(); | | 157 | + useRightPanelStore.setState(useRightPanelStore.getInitialState()); | | 158 | + useTerminalUiStateStore.setState(useTerminalUiStateStore.getInitialState()); | 152 | 159 | return { original, other }; | 153 | 160 | } | 154 | 161 |
| @@ -170,6 +177,42 @@ it("closes an exited terminal on its own thread after navigating to one reusing | 170 | 177 | expect(onCloseTerminal.mock.calls).toEqual([["term-1", original]]); | 171 | 178 | }); | 172 | 179 |
| | 180 | +// Thread navigation defaults have no `!terminalFocus`, so they apply in a focused terminal. | | 181 | +it("hands thread navigation shortcuts from a focused terminal back to the app", async () => { | | 182 | + const { other: threadRef } = setup(); | | 183 | + using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64"); | | 184 | + useRightPanelStore.getState().openBrowser(threadRef, null); | | 185 | + await using _screen = await render( | | 186 | + <Drawer | | 187 | + threadRef={threadRef} | | 188 | + onCloseTerminal={() => {}} | | 189 | + keybindings={compileResolvedKeybindingsConfig([ | | 190 | + ...DEFAULT_KEYBINDINGS, | | 191 | + { key: "ctrl+r", command: "thread.next", when: "!previewOpen" }, | | 192 | + { key: "ctrl+g", command: "thread.previous", when: "terminalOpen" }, | | 193 | + ])} | | 194 | + />, | | 195 | + ); | | 196 | + await act(async () => { | | 197 | + fixture.pendingSurfaces.shift()?.(); | | 198 | + }); | | 199 | + const terminalHandles = (key: string, code: string, shiftKey = true) => | | 200 | + fixture.surfaceOptions | | 201 | + .at(-1)! | | 202 | + .beforeKey(new KeyboardEvent("keydown", { key, code, ctrlKey: true, shiftKey })); | | 203 | + | | 204 | + expect([terminalHandles("{", "BracketLeft"), terminalHandles("}", "BracketRight")]).toEqual([ | | 205 | + false, | | 206 | + false, | | 207 | + ]); | | 208 | + expect(terminalHandles("C", "KeyC")).toBe(true); | | 209 | + // Sidebar sees the open preview and the closed drawer and rejects these, so the shell keeps them. | | 210 | + expect([terminalHandles("r", "KeyR", false), terminalHandles("g", "KeyG", false)]).toEqual([ | | 211 | + true, | | 212 | + true, | | 213 | + ]); | | 214 | +}); | | 215 | + | 173 | 216 | it("only activates terminal paths confirmed to exist on their environment", async () => { | 174 | 217 | const { other: threadRef } = setup(); | 175 | 218 | const key = (path: string) => JSON.stringify([threadRef.environmentId, path]); | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/ThreadTerminalDrawer.tsx +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,6 +66,7 @@ import { | 66 | 66 | isTerminalSplitShortcut, | 67 | 67 | isTerminalSplitVerticalShortcut, | 68 | 68 | isTerminalToggleShortcut, | | 69 | + isThreadNavigationShortcut, | 69 | 70 | terminalDeleteShortcutData, | 70 | 71 | terminalNavigationShortcutData, | 71 | 72 | } from "../keybindings"; | @@ -91,6 +92,7 @@ import { openTerminalLinkInPreview } from "./preview/openTerminalLinkInPreview"; | 91 | 92 | import { useAtomCommand } from "../state/use-atom-command"; | 92 | 93 | import { preventTerminalCloseShortcut } from "../lib/terminalCloseShortcut"; | 93 | 94 | import { observeResize } from "../lib/observeResize"; | | 95 | +import { threadShortcutContext } from "./Sidebar.logic"; | 94 | 96 | import { | 95 | 97 | consumePendingTerminalFocus, | 96 | 98 | focusTerminal, | @@ -575,6 +577,7 @@ function TerminalSessionViewport({ | 575 | 577 | const hasHandledExitRef = useRef(false); | 576 | 578 | const selectionActionRequestIdRef = useRef(0); | 577 | 579 | const readKeybindings = useEffectEvent(() => keybindings); | | 580 | + const readThreadShortcutContext = useEffectEvent(() => threadShortcutContext(threadRef)); | 578 | 581 | const handleSessionExited = useEffectEvent(() => { | 579 | 582 | if (hasTerminalWriteAccess()) onSessionExited(); | 580 | 583 | }); | @@ -938,7 +941,11 @@ function TerminalSessionViewport({ | 938 | 941 | isTerminalSplitShortcut(event, currentKeybindings, options) || | 939 | 942 | isTerminalSplitVerticalShortcut(event, currentKeybindings, options) || | 940 | 943 | isTerminalNewShortcut(event, currentKeybindings, options) || | 941 | | - isDiffToggleShortcut(event, currentKeybindings, options) | | 944 | + isDiffToggleShortcut(event, currentKeybindings, options) || | | 945 | + // Match Sidebar's context, or a key it will reject skips the shell. | | 946 | + isThreadNavigationShortcut(event, currentKeybindings, { | | 947 | + context: { ...readThreadShortcutContext(), terminalFocus: true }, | | 948 | + }) | 942 | 949 | ) { | 943 | 950 | return false; | 944 | 951 | } | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/keybindings.ts +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -281,12 +281,24 @@ export function threadTraversalDirectionFromCommand( | 281 | 281 | return null; | 282 | 282 | } | 283 | 283 |
| | 284 | +export function isThreadNavigationShortcut( | | 285 | + event: ShortcutEventLike, | | 286 | + keybindings: ResolvedKeybindingsConfig, | | 287 | + options?: ShortcutMatchOptions, | | 288 | +): boolean { | | 289 | + const command = resolveShortcutCommand(event, keybindings, options); | | 290 | + return ( | | 291 | + threadTraversalDirectionFromCommand(command) !== null || | | 292 | + threadJumpIndexFromCommand(command ?? "") !== null | | 293 | + ); | | 294 | +} | | 295 | + | 284 | 296 | export function shouldShowThreadJumpHintsForModifiers( | 285 | 297 | modifiers: ShortcutModifierStateLike, | 286 | 298 | keybindings: ResolvedKeybindingsConfig, | 287 | 299 | options?: ShortcutMatchOptions, | 288 | 300 | ): boolean { | 289 | | - // Ghostty can write keys to the shell before window shortcuts run, regardless of the when clause. | | 301 | + // Holding a modifier is routine in a shell (Ctrl+C, Ctrl+R), so don't flash hints over it. | 290 | 302 | if (resolveContext(options).terminalFocus) { | 291 | 303 | return false; | 292 | 304 | } | |
|