refactor(app): pass composer focus on from the shortcut's control (#64504)

This commit is contained in:
Filip Hejmowski
2026-11-17 22:32:48 +01:00
committed by GitHub
Unverified
parent 7bec1e6b35
commit 7988f65ab8
3 changed files with 40 additions and 27 deletions
+32 -17
View File
@@ -1,4 +1,4 @@
import { expect, test, type Page } from "@playwright/test"
import { expect, test, type Locator, type Page } from "@playwright/test"
import type { OpenCodeEvent, SessionMessageInfo } from "@opencode/client/promise"
import {
NO_PROVIDER,
@@ -504,12 +504,37 @@ test("option arrows move to the previous or next tab, and with shift to the next
await expectPath(page, sessionHref(c.id))
})
for (const navigation of ["horizontal", "vertical", "keyboard", "numbered"] as const) {
test(`switching session tabs restores the caret and pastes into the destination draft (${navigation})`, async ({
const clickTab = (_: Page, tab: Locator) => tab.click()
const press = (key: string) => (page: Page) => page.keyboard.press(key)
for (const row of [
{ navigation: "horizontal", toB: clickTab, toA: press("Control+Alt+ArrowLeft"), backToB: clickTab },
{ navigation: "vertical", toB: clickTab, toA: press("Control+Alt+ArrowLeft"), backToB: clickTab },
{
navigation: "keyboard",
toB: press("Control+Alt+ArrowRight"),
toA: press("Control+Alt+ArrowLeft"),
backToB: clickTab,
},
{
navigation: "numbered",
// Pressed in a titlebar control, which stays mounted and keeps focus while the destination composer mounts.
toB: async (page: Page) => {
const home = page.locator('[data-slot="titlebar-v2"]').getByRole("button", { name: "Home", exact: true })
await home.focus()
await expect(home).toBeFocused()
await page.keyboard.press("ControlOrMeta+2")
},
toA: press("ControlOrMeta+1"),
backToB: press("ControlOrMeta+2"),
},
]) {
test(`switching session tabs restores the caret and pastes into the destination draft (${row.navigation})`, async ({
page,
context,
}) => {
const tabLayout = navigation === "vertical" ? "vertical" : "horizontal"
const tabLayout = row.navigation === "vertical" ? "vertical" : "horizontal"
const strip = page.locator(
tabLayout === "vertical" ? '[data-slot="vertical-tabs-sidebar"]' : '[data-slot="titlebar-tabs"]',
@@ -525,24 +550,14 @@ for (const navigation of ["horizontal", "vertical", "keyboard", "numbered"] as c
const tabB = strip.locator(`[data-titlebar-tab-link][href="${sessionHref(b.id)}"]`)
await editor.fill("Draft A")
if (navigation === "keyboard") await page.keyboard.press("Control+Alt+ArrowRight")
if (navigation === "numbered") {
// A titlebar control stays mounted across sessions, so it still holds focus when the destination composer mounts.
const home = page.locator('[data-slot="titlebar-v2"]').getByRole("button", { name: "Home", exact: true })
await home.focus()
await expect(home).toBeFocused()
await page.keyboard.press("ControlOrMeta+2")
}
if (navigation === "horizontal" || navigation === "vertical") await tabB.click()
await row.toB(page, tabB)
await expectPath(page, sessionHref(b.id))
await expect(editor).toBeFocused()
await page.evaluate(() => navigator.clipboard.writeText("Draft B"))
await page.keyboard.press("ControlOrMeta+V")
await expect(editor).toHaveText("Draft B")
await page.keyboard.press(navigation === "numbered" ? "ControlOrMeta+1" : "Control+Alt+ArrowLeft")
await row.toA(page)
await expectPath(page, sessionHref(a.id))
await expect(editor).toBeFocused()
await expect(editor).toHaveText("Draft A")
@@ -550,7 +565,7 @@ for (const navigation of ["horizontal", "vertical", "keyboard", "numbered"] as c
await page.keyboard.press("ControlOrMeta+V")
await expect(editor).toHaveText("Draft A appended")
await (navigation === "numbered" ? page.keyboard.press("ControlOrMeta+2") : tabB.click())
await row.backToB(page, tabB)
await expectPath(page, sessionHref(b.id))
await expect(editor).toBeFocused()
await expect(editor).toHaveText("Draft B")
+6 -1
View File
@@ -62,12 +62,17 @@ function createComposerFocus() {
let timer: ReturnType<typeof setTimeout> | undefined
let preparing: ComposerSurface | undefined
let real: ComposerSurface | undefined
// What held focus when the session opened, such as a control a tab shortcut was pressed in. Like the tab link, it
// passes focus on.
let opener: Element | null = null
const dispatch = (event: ComposerFocusEvent) => {
const previous = untrack(state)
const result = transitionComposerFocus(previous, event)
if (result.state === previous) return
if (event.type === "target.changed") opener = document.activeElement
setState(result.state)
result.effects.forEach(run)
}
@@ -116,7 +121,7 @@ function createComposerFocus() {
if (active && element.contains(active)) return "focused"
// A control chosen while the session was loading owns focus now.
if (active && active !== document.body && !isTitlebarTabLink(active)) return "claimed"
if (active && active !== document.body && active !== opener && !isTitlebarTabLink(active)) return "claimed"
element.focus({ preventScroll: true })
setCursorPosition(element, effect.cursor)
@@ -352,17 +352,10 @@ export function createSessionRowNavigation(input: {
const command = useCommand()
const language = useLanguage()
// The destination composer leaves focus on a control that still holds it (session/composer/focus.tsx), so a shortcut
// that opens another session releases focus first, as clicking its tab does.
const navigate = (key: string, reveal: boolean) => {
if (key !== input.current() && document.activeElement instanceof HTMLElement) document.activeElement.blur()
input.navigate(key, reveal)
}
const selectAdjacent = (offset: -1 | 1, include?: (key: string) => boolean) => {
const key = adjacentTabKey(input.keys(), input.current(), offset, include)
if (key) navigate(key, false)
if (key) input.navigate(key, false)
}
command.register("titlebar-tab-cycle", () => [
@@ -422,7 +415,7 @@ export function createSessionRowNavigation(input: {
title: "",
keybind: `mod+${index + 1}`,
hidden: true,
onSelect: () => navigate(key, true),
onSelect: () => input.navigate(key, true),
})),
)