Repository navigation
fix(app): keep keyboard focus in opened and switched terminals #57980
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
740fd71
chore(app): update ghostty-web to terminal focus branch
Brendonovich 2727824
fix(app): keep focus on a terminal opened with a focus request
Brendonovich b5b29d5
fix(app): keep focus in the terminal selected with tab shortcuts
Brendonovich 2feaa37
test(app): document terminal focus test stubs
Brendonovich 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import type { Page } from "@playwright/test" | ||
| import { expect, sourceURL, story } from "../../storybook/playwright/story" | ||
|
|
||
| const fixture = sourceURL(new URL("../../gui-extensions/src/terminal/terminal.fixture.tsx", import.meta.url)) | ||
|
|
||
| const mountTerminal = async (page: Page, autoFocus: boolean) => { | ||
| await page.evaluate( | ||
| async (input) => { | ||
| const { mountTerminalFocus } = await import(input.fixture) | ||
| mountTerminalFocus({ autoFocus: input.autoFocus }) | ||
| }, | ||
| { fixture, autoFocus }, | ||
| ) | ||
| const root = page.getByTestId("terminal-focus-fixture") | ||
| await expect(root.locator('[data-component="terminal"] textarea')).toBeAttached() | ||
| // The terminal's deferred focus and restore run on timers queued when it opened; let them run first. | ||
| await page.evaluate( | ||
| () => new Promise<void>((resolve) => setTimeout(() => setTimeout(() => requestAnimationFrame(() => resolve()), 0), 0)), | ||
| ) | ||
|
|
||
| return root | ||
| } | ||
|
|
||
| story.beforeEach(async ({ mount }) => { | ||
| // Any story loads the app styles; the fixture mounts the real terminal beside it. | ||
| await mount("ui-line-comment--editor") | ||
| }) | ||
|
|
||
| story("a terminal opened with a focus request keeps focus after the request is consumed", async ({ page }) => { | ||
| const root = await mountTerminal(page, true) | ||
|
|
||
| await expect(root.locator('[data-component="terminal"] textarea')).toBeFocused() | ||
| }) | ||
|
|
||
| story("a terminal opened without a focus request leaves focus in the composer", async ({ page }) => { | ||
| const root = await mountTerminal(page, false) | ||
|
|
||
| await expect(root.getByRole("textbox", { name: "Composer", exact: true })).toBeFocused() | ||
| }) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { afterEach, describe, expect, test } from "bun:test" | ||
| import { createRoot } from "solid-js" | ||
| import type { ServerRef, Sessions, Storage } from "../sdk" | ||
| import { createTerminalModel } from "./model" | ||
|
|
||
| // SAFETY: the workspace model reads only these members when it loads and handles focus. | ||
| const storage = { | ||
| memory: (_key: string, options: { initial: unknown }) => [options.initial], | ||
| store: () => ({ value: { all: [] }, ready: () => true }), | ||
| } as unknown as Storage | ||
|
|
||
| // SAFETY: load reads only sessions.list and server.id. | ||
| const sessions = { list: () => [] } as unknown as Sessions | ||
|
|
||
| // SAFETY: load reads only sessions.list and server.id. | ||
| const server = { id: "server" } as unknown as ServerRef | ||
|
|
||
| const cleanups: VoidFunction[] = [] | ||
|
|
||
| afterEach(() => { | ||
| cleanups.splice(0).forEach((cleanup) => cleanup()) | ||
| document.body.replaceChildren() | ||
| }) | ||
|
|
||
| const workspace = () => | ||
| createRoot((dispose) => { | ||
| cleanups.push(dispose) | ||
|
|
||
| return createTerminalModel({ storage, sessions }).load({ server, directory: "/workspace" }) | ||
| }) | ||
|
|
||
| const field = (parent: HTMLElement) => { | ||
| const input = document.createElement("input") | ||
| parent.append(input) | ||
|
|
||
| return input | ||
| } | ||
|
|
||
| describe("terminal focus requests", () => { | ||
| test("survive the side region focusing its selected tab while switching terminals", () => { | ||
| const side = document.createElement("div") | ||
| side.id = "review-panel" | ||
| document.body.append(side) | ||
| const terminal = workspace() | ||
|
|
||
| terminal.requestFocus("pty_2") | ||
| field(side).focus() | ||
|
|
||
| expect(terminal.focusRequested("pty_2")).toBe(true) | ||
| }) | ||
|
|
||
| test("are cancelled when focus leaves the side region", () => { | ||
| const terminal = workspace() | ||
|
|
||
| terminal.requestFocus("pty_2") | ||
| field(document.body).focus() | ||
|
|
||
| expect(terminal.focusRequested("pty_2")).toBe(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
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,69 @@ | ||
| import { ThemeProvider } from "@opencode/ui/theme/context" | ||
| import { createSignal } from "solid-js" | ||
| import { render } from "solid-js/web" | ||
| import { ExtensionContext, type Context, type ServerRef } from "../sdk" | ||
| import { Terminal } from "./terminal" | ||
|
|
||
| /** | ||
| * Mounts the real terminal beside a focused composer field, as the side panel does when the user opens a terminal while | ||
| * typing. The PTY connection never resolves, so only the mount and focus lifecycle runs. | ||
| */ | ||
| export function mountTerminalFocus(input: { autoFocus: boolean }) { | ||
| const host = document.createElement("div") | ||
| host.dataset.testid = "terminal-focus-fixture" | ||
| host.style.cssText = "display:flex;flex-direction:column;gap:8px;width:640px;height:360px" | ||
| document.body.append(host) | ||
|
|
||
| const composer = document.createElement("textarea") | ||
| composer.setAttribute("aria-label", "Composer") | ||
| host.append(composer) | ||
| composer.focus() | ||
|
|
||
| const surface = document.createElement("div") | ||
| surface.style.cssText = "position:relative;flex:1" | ||
| host.append(surface) | ||
|
|
||
| // As the panel's focus request does, consuming the request turns autoFocus off. | ||
| const [requested, setRequested] = createSignal(input.autoFocus) | ||
|
|
||
| // SAFETY: the terminal reads only these extension members while it mounts and focuses. | ||
| const extension = { | ||
| t: (key: string) => key, | ||
| appearance: { font: () => "monospace" }, | ||
| keybinds: { matches: () => false }, | ||
| system: { openExternal() {} }, | ||
| } as unknown as Context | ||
|
|
||
| // SAFETY: the terminal reads only these server members; the PTY token never resolves, so it never connects. | ||
| const server = { | ||
| id: "fixture", | ||
| name: "fixture", | ||
| url: "http://127.0.0.1:1", | ||
| client: { pty: { connect: { token: () => new Promise(() => {}) } } }, | ||
| } as unknown as ServerRef | ||
|
|
||
| const dispose = render( | ||
| () => ( | ||
| <ThemeProvider> | ||
| <ExtensionContext.Provider value={extension}> | ||
| <Terminal | ||
| pty={{ id: "pty_fixture", title: "Terminal", titleNumber: 1 }} | ||
| server={server} | ||
| directory="/fixture" | ||
| ghostty={() => | ||
| import("ghostty-web").then(async (mod) => ({ mod, ghostty: await mod.Ghostty.load() })) | ||
| } | ||
| autoFocus={requested()} | ||
| onAutoFocus={() => setRequested(false)} | ||
| /> | ||
| </ExtensionContext.Provider> | ||
| </ThemeProvider> | ||
| ), | ||
| surface, | ||
| ) | ||
|
|
||
| return () => { | ||
| dispose() | ||
| host.remove() | ||
| } | ||
| } |
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.
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.
This exempts focus anywhere in the side region, not just the tab trigger or root that the tab switch focuses. Any request that hasn't been used yet now outlives the user moving to other side-panel content. Two examples:
pty.createresolves. The pending request survives, and the new terminal takes focus when it mounts.Could this only ignore the elements the switch itself focuses (the
#review-panelroot and its tab triggers, e.g.event.target.id === "review-panel" || event.target.closest('#review-panel [role="tab"]'))? Also, this would no longer tie the terminal extension to all of the host's panel DOM.