From 1e322a3a0132b01200c824b609785ebe616fc35e Mon Sep 17 00:00:00 2001 From: Filip <34747899+neriousy@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:18:48 +0100 Subject: [PATCH] fix(tui): keep focus on open dialogs when a question arrives (#60365) --- packages/tui/src/context/keymap.tsx | 9 +- packages/tui/src/routes/session/form.tsx | 6 +- packages/tui/test/cli/tui/form.test.tsx | 221 +++++++++++++++++- .../tui/test/cli/tui/input-scope.test.tsx | 5 +- 4 files changed, 233 insertions(+), 8 deletions(-) diff --git a/packages/tui/src/context/keymap.tsx b/packages/tui/src/context/keymap.tsx index d6508190600..15cfddea0bd 100644 --- a/packages/tui/src/context/keymap.tsx +++ b/packages/tui/src/context/keymap.tsx @@ -174,8 +174,8 @@ export interface Keymap { readonly mode: { /** Returns the active mode. */ current(): string - /** Pushes a mode until the returned cleanup is called. */ - push(mode: string): () => void + /** Pushes a mode until the returned cleanup is called. The mode only claims input while `active` is true. */ + push(mode: string, active?: Accessor): () => void } /** Registers a low-level keymap interceptor. */ intercept: OpenTuiKeymap["intercept"] @@ -194,7 +194,10 @@ function use(): Keymap { }, mode: { current: value.mode.current, - push: (mode) => value.mode.push(mode, resolveInteractivity(enabled)), + push: (mode, active) => { + const interactive = resolveInteractivity(enabled) + return value.mode.push(mode, active ? () => interactive() && active() : interactive) + }, }, intercept: value.keymap.intercept.bind(value.keymap), isLeader, diff --git a/packages/tui/src/routes/session/form.tsx b/packages/tui/src/routes/session/form.tsx index 675067ff0c4..e65b56a6fe6 100644 --- a/packages/tui/src/routes/session/form.tsx +++ b/packages/tui/src/routes/session/form.tsx @@ -15,6 +15,7 @@ import type { FormAnswer, FormField, FormValue } from "@opencode/client" import { useData, type FormWithLocation } from "../../context/data" import { useClipboard } from "../../context/clipboard" import { SplitBorder } from "../../ui/border" +import { useDialog } from "../../ui/dialog" import { useToast } from "../../ui/toast" import { Keymap } from "../../context/keymap" import { useInteractivity } from "../../context/interactivity" @@ -61,9 +62,10 @@ export function FormPrompt(props: { form: FormWithLocation }) { const theme = useTheme() const renderer = useRenderer() const dimensions = useTerminalDimensions() + const dialog = useDialog() const keymap = Keymap.use() const enabled = useInteractivity() - const active = () => enabled() && keymap.mode.current() === FORM_MODE + const active = () => enabled() && dialog.stack.length === 0 && keymap.mode.current() === FORM_MODE const cursorStyle = useCursorStyle() const clipboard = useClipboard() const toast = useToast() @@ -533,7 +535,7 @@ export function FormPrompt(props: { form: FormWithLocation }) { ) } - onMount(() => onCleanup(keymap.mode.push(FORM_MODE))) + onMount(() => onCleanup(keymap.mode.push(FORM_MODE, () => dialog.stack.length === 0))) Keymap.createLayer(() => ({ mode: FORM_MODE, diff --git a/packages/tui/test/cli/tui/form.test.tsx b/packages/tui/test/cli/tui/form.test.tsx index b839850d89d..5017a4de408 100644 --- a/packages/tui/test/cli/tui/form.test.tsx +++ b/packages/tui/test/cli/tui/form.test.tsx @@ -1,19 +1,24 @@ /** @jsxImportSource @opentui/solid */ +import type { OpenCodeEvent } from "@opencode/client" +import { InputRenderable, TextareaRenderable } from "@opentui/core" import { testRender } from "@opentui/solid" import { expect, test } from "bun:test" import { mkdir } from "node:fs/promises" import path from "node:path" import { onMount, Show } from "solid-js" +import { CommandPaletteDialog } from "../../../src/component/command-palette" +import { DialogSessionRename } from "../../../src/component/dialog-session-rename" import { DataProvider, useData, type FormWithLocation } from "../../../src/context/data" import { ClientProvider } from "../../../src/context/client" import { ThemeProvider } from "../../../src/context/theme" import { Keymap } from "../../../src/context/keymap" import { ConfigProvider } from "../../../src/config" +import { DialogProvider, useDialog, type DialogContext } from "../../../src/ui/dialog" import { ToastProvider } from "../../../src/ui/toast" import { emptyThemeSource, tmpdir } from "../../fixture/fixture" import { TestTuiContexts } from "../../fixture/tui-environment" import { createTuiResolvedConfig } from "../../fixture/tui-runtime" -import { createApi, createEventStream, createFetch, json } from "../../fixture/tui-client" +import { createApi, createEventStream, createFetch, directory, json } from "../../fixture/tui-client" async function mountForm( root: string, @@ -108,7 +113,9 @@ async function mountForm( - {response ? : } + + {response ? : } + @@ -731,3 +738,213 @@ test("space does not select a single-choice option", async () => { prompt.app.renderer.destroy() } }) + +async function mountDialogForm(root: string) { + const state = path.join(root, "state") + await mkdir(state, { recursive: true }) + + const replies: unknown[] = [] + const cancellations: unknown[] = [] + const renames: unknown[] = [] + const events = createEventStream() + const config = createTuiResolvedConfig({ animations: false }) + let dialog!: DialogContext + + const formID = `frm_${crypto.randomUUID()}` + const transport = createFetch((url, request) => { + if (url.pathname === "/api/session/ses_test" && request.method === "PATCH") + return request.json().then((body) => { + renames.push(body) + return new Response(null, { status: 204 }) + }) + if (url.pathname === "/api/session/ses_test/form" && request.method === "GET") return json({ data: [] }) + if (url.pathname === `/api/session/ses_test/form/${formID}/reply`) + return request.json().then((answer) => { + replies.push(answer) + events.emit({ + id: "evt_form_replied", + created: 2, + type: "form.replied", + location: { directory }, + data: { id: formID, sessionID: "ses_test", answer: answer.answer }, + }) + return new Response(null, { status: 204 }) + }) + if (url.pathname === `/api/session/ses_test/form/${formID}` && request.method === "DELETE") { + cancellations.push(true) + events.emit({ + id: "evt_form_cancelled", + created: 2, + type: "form.cancelled", + location: { directory }, + data: { id: formID, sessionID: "ses_test" }, + }) + return new Response(null, { status: 204 }) + } + }, events) + const { FormPrompt } = await import("../../../src/routes/session/form") + + function Scene() { + const data = useData() + dialog = useDialog() + onMount(() => void data.session.form.sync("ses_test")) + return ( + + Composer ready}> + {(current) => } + + + ) + } + + const app = await testRender( + () => ( + + + + + + + + + + + + + + + + + + ), + { width: 80, height: 30, kittyKeyboard: true }, + ) + app.renderer.start() + await app.waitForFrame((frame) => frame.includes("Composer ready")) + + const ask = (fields: Extract["data"]["form"]["fields"]) => + events.emit({ + id: "evt_form_created", + created: 1, + type: "form.created", + location: { directory }, + data: { + form: { + id: formID, + sessionID: "ses_test", + title: "Question", + fields, + }, + }, + }) + + return { app, ask, cancellations, dialog: () => dialog, renames, replies } +} + +test("arriving choice question keeps focus with an open rename dialog until it closes", async () => { + await using tmp = await tmpdir() + const scene = await mountDialogForm(tmp.path) + try { + DialogSessionRename.show(scene.dialog(), "ses_test", "Initial") + await scene.app.waitForFrame( + (frame) => + frame.includes("Rename session") && scene.app.renderer.currentFocusedRenderable instanceof TextareaRenderable, + ) + + scene.ask([ + { + key: "approach", + type: "string", + description: "Which approach should I take?", + options: [ + { value: "a", label: "Option A" }, + { value: "b", label: "Option B" }, + ], + custom: true, + }, + ]) + await scene.app.waitForFrame((frame) => frame.includes("Option A")) + + await scene.app.mockInput.typeText(" 2") + scene.app.mockInput.pressArrow("down") + scene.app.mockInput.pressEnter() + await scene.app.waitForFrame((frame) => !frame.includes("Rename session")) + + expect(scene.renames).toEqual([{ title: "Initial 2" }]) + expect(scene.replies).toEqual([]) + expect(scene.cancellations).toEqual([]) + + scene.app.mockInput.pressKey("2") + await scene.app.waitFor(() => scene.replies.length === 1) + expect(scene.replies).toEqual([{ answer: { approach: "b" } }]) + } finally { + scene.app.renderer.destroy() + } +}) + +test("arriving text question does not steal editor focus from an open rename dialog", async () => { + await using tmp = await tmpdir() + const scene = await mountDialogForm(tmp.path) + try { + DialogSessionRename.show(scene.dialog(), "ses_test", "Draft") + await scene.app.waitForFrame( + (frame) => + frame.includes("Rename session") && scene.app.renderer.currentFocusedRenderable instanceof TextareaRenderable, + ) + + scene.ask([{ key: "notes", type: "string", description: "Explain why" }]) + await scene.app.waitForFrame((frame) => frame.includes("Explain why")) + + await scene.app.mockInput.typeText(" title") + scene.app.mockInput.pressEnter() + await scene.app.waitForFrame( + (frame) => + !frame.includes("Rename session") && scene.app.renderer.currentFocusedRenderable instanceof TextareaRenderable, + ) + + expect(scene.renames).toEqual([{ title: "Draft title" }]) + expect(scene.replies).toEqual([]) + + await scene.app.mockInput.typeText("because") + scene.app.mockInput.pressEnter() + scene.app.mockInput.pressEnter() + await scene.app.waitFor(() => scene.replies.length === 1) + expect(scene.replies).toEqual([{ answer: { notes: "because" } }]) + } finally { + scene.app.renderer.destroy() + } +}) + +test("escape closes an open command palette instead of cancelling an arriving question", async () => { + await using tmp = await tmpdir() + const scene = await mountDialogForm(tmp.path) + try { + scene.dialog().replace(() => ) + await scene.app.waitForFrame( + (frame) => frame.includes("Commands") && scene.app.renderer.currentFocusedRenderable instanceof InputRenderable, + ) + + scene.ask([ + { + key: "approach", + type: "string", + options: [ + { value: "a", label: "Option A" }, + { value: "b", label: "Option B" }, + ], + }, + ]) + await scene.app.waitForFrame((frame) => frame.includes("Option A")) + + scene.app.mockInput.pressEscape() + await scene.app.waitForFrame((frame) => !frame.includes("Commands")) + expect(scene.cancellations).toEqual([]) + expect(scene.app.captureCharFrame()).toContain("Option A") + + scene.app.mockInput.pressEnter() + await scene.app.waitFor(() => scene.replies.length === 1) + expect(scene.replies).toEqual([{ answer: { approach: "a" } }]) + } finally { + scene.app.renderer.destroy() + } +}) diff --git a/packages/tui/test/cli/tui/input-scope.test.tsx b/packages/tui/test/cli/tui/input-scope.test.tsx index 3364368458e..874d5c7e6af 100644 --- a/packages/tui/test/cli/tui/input-scope.test.tsx +++ b/packages/tui/test/cli/tui/input-scope.test.tsx @@ -13,6 +13,7 @@ import { LocationProvider } from "../../../src/context/location" import { ThemeProvider } from "../../../src/context/theme" import { FormPrompt, FORM_MODE } from "../../../src/routes/session/form" import { PermissionPrompt } from "../../../src/routes/session/permission" +import { DialogProvider } from "../../../src/ui/dialog" import { ToastProvider } from "../../../src/ui/toast" import { emptyThemeSource, tmpdir } from "../../fixture/fixture" import { createApi, createEventStream, createFetch, json } from "../../fixture/tui-client" @@ -81,7 +82,9 @@ async function mountPanes(root: string, render: () => JSX.Element, parentID?: st - + + +