From 7988f65ab84b6a8be554aafe9d6988297eca0c79 Mon Sep 17 00:00:00 2001 From: Filip Hejmowski <34747899+neriousy@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:32:48 +0100 Subject: [PATCH] refactor(app): pass composer focus on from the shortcut's control (#64504) --- packages/app/e2e/regression/tabs.spec.ts | 49 ++++++++++++------- packages/app/src/session/composer/focus.tsx | 7 ++- packages/app/src/shell/titlebar/tab-strip.tsx | 11 +---- 3 files changed, 40 insertions(+), 27 deletions(-) diff --git a/packages/app/e2e/regression/tabs.spec.ts b/packages/app/e2e/regression/tabs.spec.ts index 5966c6750dd..895b00707d8 100644 --- a/packages/app/e2e/regression/tabs.spec.ts +++ b/packages/app/e2e/regression/tabs.spec.ts @@ -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") diff --git a/packages/app/src/session/composer/focus.tsx b/packages/app/src/session/composer/focus.tsx index c59a609628a..5bb1df580be 100644 --- a/packages/app/src/session/composer/focus.tsx +++ b/packages/app/src/session/composer/focus.tsx @@ -62,12 +62,17 @@ function createComposerFocus() { let timer: ReturnType | 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) diff --git a/packages/app/src/shell/titlebar/tab-strip.tsx b/packages/app/src/shell/titlebar/tab-strip.tsx index 35518ea358f..819e354fad5 100644 --- a/packages/app/src/shell/titlebar/tab-strip.tsx +++ b/packages/app/src/shell/titlebar/tab-strip.tsx @@ -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), })), )