mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 21:15:57 +03:00
fix(tui): show keyboard focus in export options (#57154)
This commit is contained in:
committed by
GitHub
Unverified
parent
157ad7f22a
commit
f36cda5bc8
@@ -23,8 +23,12 @@ type Active = ExportFormat | "thinking" | "tools" | "sanitize" | "copy" | "expor
|
||||
|
||||
export function DialogExportOptions(props: DialogExportOptionsProps) {
|
||||
const theme = useTheme().surface("dialog")
|
||||
const overlayTheme = useTheme()
|
||||
const newDesigns = useNewDesigns()
|
||||
// Same field styling as the question form: only the focused row is highlighted, and the
|
||||
// mark alone shows what's checked.
|
||||
const markColor = (active: boolean, checked: boolean) =>
|
||||
active ? theme.text.formfield.focused : checked ? theme.text.formfield.selected : theme.text.muted
|
||||
const labelColor = (active: boolean) => (active ? theme.text.formfield.focused : theme.text.formfield.base)
|
||||
const [store, setStore] = createStore({
|
||||
format: "markdown" as ExportFormat,
|
||||
thinking: props.defaultThinking,
|
||||
@@ -53,20 +57,18 @@ export function DialogExportOptions(props: DialogExportOptionsProps) {
|
||||
if (store.active === "copy" || store.active === "export") confirm(store.active)
|
||||
}
|
||||
|
||||
const move = (offset: number) => {
|
||||
const options: Active[] =
|
||||
store.format === "markdown" ? ["markdown", "json", "thinking", "tools"] : ["markdown", "json", "sanitize"]
|
||||
const order: Active[] = newDesigns() ? options : [...options, "copy", "export"]
|
||||
setStore("active", order[(order.indexOf(store.active) + offset + order.length) % order.length])
|
||||
}
|
||||
|
||||
Keymap.createLayer(() => ({
|
||||
mode: "modal",
|
||||
commands: [
|
||||
{
|
||||
bind: "tab",
|
||||
title: "Next export option",
|
||||
group: "Dialog",
|
||||
run: () => {
|
||||
const options: Active[] =
|
||||
store.format === "markdown" ? ["markdown", "json", "thinking", "tools"] : ["markdown", "json", "sanitize"]
|
||||
const order: Active[] = newDesigns() ? options : [...options, "copy", "export"]
|
||||
setStore("active", order[(order.indexOf(store.active) + 1) % order.length])
|
||||
},
|
||||
},
|
||||
{ bind: "tab", title: "Next export option", group: "Dialog", run: () => move(1) },
|
||||
{ bind: "shift+tab", title: "Previous export option", group: "Dialog", run: () => move(-1) },
|
||||
...(newDesigns()
|
||||
? [
|
||||
// The footer owns enter (save) and c (copy), so space toggles the focused option instead.
|
||||
@@ -106,10 +108,14 @@ export function DialogExportOptions(props: DialogExportOptionsProps) {
|
||||
<box
|
||||
paddingLeft={4}
|
||||
paddingRight={4}
|
||||
backgroundColor={overlayTheme.background.raised.high}
|
||||
backgroundColor={
|
||||
store.active === "copy" ? theme.background.action.primary.focused : theme.background.action.primary.base
|
||||
}
|
||||
onMouseUp={() => confirm("copy")}
|
||||
>
|
||||
<text fg={overlayTheme.text.base}>Copy</text>
|
||||
<text fg={store.active === "copy" ? theme.text.action.primary.focused : theme.text.action.primary.base}>
|
||||
Copy
|
||||
</text>
|
||||
</box>
|
||||
<box
|
||||
paddingLeft={4}
|
||||
@@ -133,28 +139,16 @@ export function DialogExportOptions(props: DialogExportOptionsProps) {
|
||||
<For each={["markdown", "json"] as const}>
|
||||
{(format) => (
|
||||
<box
|
||||
flexDirection="row"
|
||||
gap={1}
|
||||
paddingLeft={1}
|
||||
paddingRight={1}
|
||||
backgroundColor={
|
||||
store.active === format
|
||||
? theme.background.formfield.focused
|
||||
: store.format === format
|
||||
? theme.background.formfield.selected
|
||||
: theme.background.formfield.base
|
||||
}
|
||||
onMouseUp={() => selectFormat(format)}
|
||||
>
|
||||
<text
|
||||
fg={
|
||||
store.active === format
|
||||
? theme.text.formfield.focused
|
||||
: store.format === format
|
||||
? theme.text.formfield.selected
|
||||
: theme.text.formfield.base
|
||||
}
|
||||
>
|
||||
{store.format === format ? "◉" : "○"} {format === "markdown" ? "Markdown" : "JSON"}
|
||||
<text fg={markColor(store.active === format, store.format === format)}>
|
||||
{store.format === format ? "◉" : "○"}
|
||||
</text>
|
||||
<text fg={labelColor(store.active === format)}>{format === "markdown" ? "Markdown" : "JSON"}</text>
|
||||
</box>
|
||||
)}
|
||||
</For>
|
||||
@@ -164,118 +158,37 @@ export function DialogExportOptions(props: DialogExportOptionsProps) {
|
||||
<box
|
||||
flexDirection="row"
|
||||
gap={1}
|
||||
backgroundColor={
|
||||
store.active === "thinking"
|
||||
? theme.background.formfield.focused
|
||||
: store.thinking
|
||||
? theme.background.formfield.selected
|
||||
: theme.background.formfield.base
|
||||
}
|
||||
onMouseUp={() => {
|
||||
setStore("active", "thinking")
|
||||
setStore("thinking", !store.thinking)
|
||||
}}
|
||||
>
|
||||
<text
|
||||
fg={
|
||||
store.active === "thinking"
|
||||
? theme.text.formfield.focused
|
||||
: store.thinking
|
||||
? theme.text.formfield.selected
|
||||
: theme.text.formfield.base
|
||||
}
|
||||
>
|
||||
{store.thinking ? "[x]" : "[ ]"}
|
||||
</text>
|
||||
<text
|
||||
fg={
|
||||
store.active === "thinking"
|
||||
? theme.text.formfield.focused
|
||||
: store.thinking
|
||||
? theme.text.formfield.selected
|
||||
: theme.text.formfield.base
|
||||
}
|
||||
>
|
||||
Include thinking
|
||||
</text>
|
||||
<text fg={markColor(store.active === "thinking", store.thinking)}>{store.thinking ? "[x]" : "[ ]"}</text>
|
||||
<text fg={labelColor(store.active === "thinking")}>Include thinking</text>
|
||||
</box>
|
||||
<box
|
||||
flexDirection="row"
|
||||
gap={1}
|
||||
backgroundColor={
|
||||
store.active === "tools"
|
||||
? theme.background.formfield.focused
|
||||
: store.tools
|
||||
? theme.background.formfield.selected
|
||||
: theme.background.formfield.base
|
||||
}
|
||||
onMouseUp={() => {
|
||||
setStore("active", "tools")
|
||||
setStore("tools", !store.tools)
|
||||
}}
|
||||
>
|
||||
<text
|
||||
fg={
|
||||
store.active === "tools"
|
||||
? theme.text.formfield.focused
|
||||
: store.tools
|
||||
? theme.text.formfield.selected
|
||||
: theme.text.formfield.base
|
||||
}
|
||||
>
|
||||
{store.tools ? "[x]" : "[ ]"}
|
||||
</text>
|
||||
<text
|
||||
fg={
|
||||
store.active === "tools"
|
||||
? theme.text.formfield.focused
|
||||
: store.tools
|
||||
? theme.text.formfield.selected
|
||||
: theme.text.formfield.base
|
||||
}
|
||||
>
|
||||
Include tools
|
||||
</text>
|
||||
<text fg={markColor(store.active === "tools", store.tools)}>{store.tools ? "[x]" : "[ ]"}</text>
|
||||
<text fg={labelColor(store.active === "tools")}>Include tools</text>
|
||||
</box>
|
||||
</Show>
|
||||
<Show when={store.format === "json"}>
|
||||
<box
|
||||
flexDirection="row"
|
||||
gap={1}
|
||||
backgroundColor={
|
||||
store.active === "sanitize"
|
||||
? theme.background.formfield.focused
|
||||
: store.sanitize
|
||||
? theme.background.formfield.selected
|
||||
: theme.background.formfield.base
|
||||
}
|
||||
onMouseUp={() => {
|
||||
setStore("active", "sanitize")
|
||||
setStore("sanitize", !store.sanitize)
|
||||
}}
|
||||
>
|
||||
<text
|
||||
fg={
|
||||
store.active === "sanitize"
|
||||
? theme.text.formfield.focused
|
||||
: store.sanitize
|
||||
? theme.text.formfield.selected
|
||||
: theme.text.formfield.base
|
||||
}
|
||||
>
|
||||
{store.sanitize ? "[x]" : "[ ]"}
|
||||
</text>
|
||||
<text
|
||||
fg={
|
||||
store.active === "sanitize"
|
||||
? theme.text.formfield.focused
|
||||
: store.sanitize
|
||||
? theme.text.formfield.selected
|
||||
: theme.text.formfield.base
|
||||
}
|
||||
>
|
||||
Sanitize sensitive data
|
||||
</text>
|
||||
<text fg={markColor(store.active === "sanitize", store.sanitize)}>{store.sanitize ? "[x]" : "[ ]"}</text>
|
||||
<text fg={labelColor(store.active === "sanitize")}>Sanitize sensitive data</text>
|
||||
</box>
|
||||
</Show>
|
||||
</box>
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/** @jsxImportSource @opentui/solid */
|
||||
import { testRender } from "@opentui/solid"
|
||||
import { expect, test } from "bun:test"
|
||||
import { onMount } from "solid-js"
|
||||
import { ConfigProvider } from "../../src/config"
|
||||
import { Keymap } from "../../src/context/keymap"
|
||||
import { ThemeProvider, useTheme } from "../../src/context/theme"
|
||||
import { DialogProvider, useDialog } from "../../src/ui/dialog"
|
||||
import { DialogExportOptions } from "../../src/ui/dialog-export-options"
|
||||
import { ToastProvider } from "../../src/ui/toast"
|
||||
import { emptyThemeSource, tmpdir } from "../fixture/fixture"
|
||||
import { TestTuiContexts } from "../fixture/tui-environment"
|
||||
import { createTuiResolvedConfig } from "../fixture/tui-runtime"
|
||||
|
||||
test("tab and shift+tab move a visible focus through the export options", async () => {
|
||||
await using temporary = await tmpdir()
|
||||
let theme: ReturnType<ReturnType<typeof useTheme>["surface"]> | undefined
|
||||
const app = await testRender(
|
||||
() => (
|
||||
<TestTuiContexts directory={temporary.path} paths={{ state: temporary.path }}>
|
||||
<ConfigProvider config={createTuiResolvedConfig()}>
|
||||
<ThemeProvider mode="dark" source={emptyThemeSource}>
|
||||
<Keymap.Provider>
|
||||
<ToastProvider>
|
||||
<DialogProvider>
|
||||
{(() => {
|
||||
theme = useTheme().surface("dialog")
|
||||
const dialog = useDialog()
|
||||
onMount(() => dialog.replace(() => <DialogExportOptions defaultThinking={true} />))
|
||||
return null
|
||||
})()}
|
||||
</DialogProvider>
|
||||
</ToastProvider>
|
||||
</Keymap.Provider>
|
||||
</ThemeProvider>
|
||||
</ConfigProvider>
|
||||
</TestTuiContexts>
|
||||
),
|
||||
{ width: 80, height: 24, kittyKeyboard: true },
|
||||
)
|
||||
const span = (text: string) =>
|
||||
app
|
||||
.captureSpans()
|
||||
.lines.flatMap((line) => line.spans)
|
||||
.find((span) => span.text.includes(text))
|
||||
const focusedButton = () => theme!.background.action.primary.focused
|
||||
const focusedField = () => theme!.text.formfield.focused
|
||||
const field = () => theme!.text.formfield.base
|
||||
|
||||
try {
|
||||
app.renderer.start()
|
||||
await app.waitForFrame((frame) => frame.includes("Copy") && frame.includes("Include tools"))
|
||||
// Only the focused field is highlighted; checked fields keep the base label colour.
|
||||
expect(span("Markdown")?.fg.equals(focusedField())).toBe(true)
|
||||
expect(span("Include thinking")?.fg.equals(field())).toBe(true)
|
||||
expect(span("Copy")?.bg.equals(focusedButton())).toBe(false)
|
||||
|
||||
for (const _ of [1, 2, 3, 4]) app.mockInput.pressTab()
|
||||
await app.waitForFrame(() => span("Copy")?.bg.equals(focusedButton()) === true)
|
||||
expect(span("Include tools")?.fg.equals(field())).toBe(true)
|
||||
|
||||
app.mockInput.pressTab({ shift: true })
|
||||
await app.waitForFrame(() => span("Include tools")?.fg.equals(focusedField()) === true)
|
||||
expect(span("Copy")?.bg.equals(focusedButton())).toBe(false)
|
||||
} finally {
|
||||
app.renderer.destroy()
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user