fix(app): keep keys typed while a new terminal opens

This commit is contained in:
James Long
2026-11-03 18:34:46 -06:00
Unverified
parent 3fcdcb35e8
commit 3f37c05c86
3 changed files with 102 additions and 12 deletions
@@ -147,6 +147,48 @@ test("the New terminal shortcut creates the next numbered terminal", async ({ pa
await expect(terminalScreen(page, persistentPty.created[0]!.id).locator("textarea")).toBeFocused()
})
test("keys typed right after the New terminal shortcut reach the new terminal's shell in order", async ({ page }) => {
const { persistentPty, editor } = await openSession(page, { name: "Terminals", persistentPty: {} })
await openSide(page)
await editor.click()
await page.keyboard.press("Control+Alt+T")
await page.keyboard.type("echo in-session-a")
await page.keyboard.press("Enter")
await expect.poll(() => persistentPty.created.length).toBe(1)
const id = persistentPty.created[0]!.id
await expect(terminalScreen(page, id).locator("textarea")).toBeFocused()
await expect.poll(() => persistentPty.sockets.flatMap((socket) => socket.input).join("")).toBe("echo in-session-a\r")
await expect(editor).toHaveText("")
})
test("keys typed right after creating a second terminal reach the new one, not the first", async ({ page }) => {
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_first", sessionID: SESSION, title: "Terminal 1" }] },
})
await openSide(page)
await showTerminal(page, "Terminal 1", "pty_first")
await page.keyboard.press("Control+Alt+T")
await page.keyboard.type("echo in-session-a")
await page.keyboard.press("Enter")
await expect.poll(() => persistentPty.created.length).toBe(1)
const id = persistentPty.created[0]!.id
await expect(terminalScreen(page, id).locator("textarea")).toBeFocused()
const input = (ptyID: string) =>
persistentPty.sockets
.filter((socket) => socket.id === ptyID)
.flatMap((socket) => socket.input)
.join("")
await expect.poll(() => input(id)).toBe("echo in-session-a\r")
expect(input("pty_first")).toBe("")
})
test("another client's control ends at a click, which resizes the terminal to the pane", async ({ page }) => {
const { persistentPty } = await openSession(page, {
name: "Terminals",
+48 -1
View File
@@ -29,6 +29,9 @@ export type TerminalSession = ReturnType<TerminalModel["load"]>
type FocusRequest = { key: string; request: number; id?: string; pending: boolean }
/** Keys typed while the terminal of a focus request is created. */
type HeldKeys = { request?: number; keys: KeyboardEvent[] }
const importGhostty = () => import("ghostty-web").then(async (mod) => ({ mod, ghostty: await mod.Ghostty.load() }))
/** The one ghostty-web load the window's terminals share; a failed load is retried by the next terminal. */
@@ -66,6 +69,8 @@ export function createTerminalModel(input: {
const cleanups: VoidFunction[] = []
const ghostty: Ghostty = {}
const focus = { request: 0 }
// Keys typed while a terminal is created wait for it, as the TUI's pane holds input until it restores.
const held: HeldKeys = { keys: [] }
const available = (server: ServerRef) => {
watch(server)
@@ -212,6 +217,33 @@ export function createTerminalModel(input: {
// Capture phase: a tab's rename field stops its focusin from bubbling.
document.addEventListener("focusin", cancelOnOutsideFocus, true)
cleanups.push(() => document.removeEventListener("focusin", cancelOnOutsideFocus, true))
// Until the new terminal takes focus, keys would reach whatever had it, such as the composer or another terminal.
// Shortcuts, input methods and a tab's rename field keep their keys.
const holdKey = (event: KeyboardEvent) => {
if (held.request === undefined || held.request !== state.focus?.request) return
if (event.ctrlKey || event.metaKey || event.isComposing || event.keyCode === 229) return
if (event.target instanceof HTMLInputElement && event.target.closest('[role="tablist"]')) return
event.preventDefault()
event.stopImmediatePropagation()
held.keys.push(event)
}
// A click means the user moved on; the new terminal still takes focus when it opens. focusTerminalById dispatches
// its own pointerdown, which is not a click.
const releaseKeys = (event: PointerEvent) => {
if (!event.isTrusted) return
held.request = undefined
held.keys = []
}
document.addEventListener("keydown", holdKey, true)
cleanups.push(() => document.removeEventListener("keydown", holdKey, true))
document.addEventListener("pointerdown", releaseKeys, true)
cleanups.push(() => document.removeEventListener("pointerdown", releaseKeys, true))
}
const handle = (initial: SessionRef) => {
@@ -249,6 +281,8 @@ export function createTerminalModel(input: {
async new() {
const target = session()
const request = requestFocus(key, undefined, true)
held.request = request
held.keys = []
await settled(target, owner)
@@ -339,11 +373,24 @@ export function createTerminalModel(input: {
})
},
requestFocus(id?: string) {
// Showing the terminal that was just created keeps its request, and the keys held for it.
if (id && focusRequested(id) && state.focus?.id === id) return
requestFocus(key, id)
},
focusRequested,
/** Ends the request once the terminal has focus, and types the keys held for it there. */
consumeFocus(id: string) {
if (focusRequested(id)) setState("focus", undefined)
if (!focusRequested(id)) return
const keys = held.request === state.focus?.request ? held.keys : []
held.request = undefined
held.keys = []
setState("focus", undefined)
const textarea = document.getElementById(`terminal-wrapper-${id}`)?.querySelector("textarea")
// ghostty-web reads keys from keydown events on the focused terminal.
if (textarea && textarea === document.activeElement) {
keys.forEach((event) => textarea.dispatchEvent(new KeyboardEvent(event.type, event)))
}
},
cancelFocus() {
if (state.focus?.key === key) cancelFocus()
+12 -11
View File
@@ -472,6 +472,18 @@ export const Terminal = (props: TerminalProps) => {
})
setOpened(t)
// Before autofocus, which types the keys held while the terminal was created.
const onData = t.onData((data) => connection.input(data))
cleanups.push(() => disposeIfDisposable(onData))
const onKey = t.onKey((key) => {
if (key.key == "Enter") {
props.onSubmit?.()
}
})
cleanups.push(() => disposeIfDisposable(onKey))
// onAutoFocus consumes the request, which flips autoFocus, so read it once.
const autoFocus = props.autoFocus === true
@@ -496,17 +508,6 @@ export const Terminal = (props: TerminalProps) => {
cleanups.push(() => clearTimeout(timer))
}
const onData = t.onData((data) => connection.input(data))
cleanups.push(() => disposeIfDisposable(onData))
const onKey = t.onKey((key) => {
if (key.key == "Enter") {
props.onSubmit?.()
}
})
cleanups.push(() => disposeIfDisposable(onKey))
const observer = new ResizeObserver(measure)
observer.observe(container)
cleanups.push(() => observer.disconnect())