diff --git a/src/renderer/src/components/terminal-pane/TerminalPane.tsx b/src/renderer/src/components/terminal-pane/TerminalPane.tsx index a737a5f34..13cbd6033 100644 --- a/src/renderer/src/components/terminal-pane/TerminalPane.tsx +++ b/src/renderer/src/components/terminal-pane/TerminalPane.tsx @@ -269,6 +269,11 @@ export default function TerminalPane({ // again. Similarly, pressing Escape runs handleRenameCancel but blur would // then call handleRenameSubmit, saving the title the user wanted to discard. const renameSubmittedRef = useRef(false) + const renameSessionIdRef = useRef(0) + const renameBlurCommitEnabledRef = useRef(true) + const renameFocusFrameRef = useRef(null) + const renameEnableBlurFrameRef = useRef(null) + const renameRefocusFrameRef = useRef(null) const onPtyErrorRef = useRef((_paneId: number, message: string) => { if (isTerminalSessionStateSaveFailure(message)) { setTerminalError(null) @@ -1207,11 +1212,41 @@ export default function TerminalPane({ } }, [tabId, worktreeId, setTabLayout]) - const handleStartRename = useCallback((paneId: number) => { - setRenameValue(paneTitlesRef.current[paneId] ?? '') - setRenamingPaneId(paneId) + const cancelPendingRenameFrames = useCallback(() => { + const frameRefs = [renameFocusFrameRef, renameEnableBlurFrameRef, renameRefocusFrameRef] + for (const frameRef of frameRefs) { + if (frameRef.current !== null) { + cancelAnimationFrame(frameRef.current) + frameRef.current = null + } + } }, []) + const closeRenameSession = useCallback(() => { + renameSessionIdRef.current += 1 + renameBlurCommitEnabledRef.current = true + cancelPendingRenameFrames() + }, [cancelPendingRenameFrames]) + + useEffect( + () => () => { + closeRenameSession() + }, + [closeRenameSession] + ) + + const handleStartRename = useCallback( + (paneId: number) => { + cancelPendingRenameFrames() + renameSessionIdRef.current += 1 + renameBlurCommitEnabledRef.current = false + renameSubmittedRef.current = false + setRenameValue(paneTitlesRef.current[paneId] ?? '') + setRenamingPaneId(paneId) + }, + [cancelPendingRenameFrames] + ) + const removePaneTitle = useCallback( (paneId: number) => { setPaneTitles((prev) => { @@ -1247,6 +1282,7 @@ export default function TerminalPane({ if (paneTitlesRef.current[renamingPaneId]) { removePaneTitle(renamingPaneId) } + closeRenameSession() setRenamingPaneId(null) return } @@ -1260,15 +1296,52 @@ export default function TerminalPane({ if (leafId) { removedTitleLeafIdsRef.current.delete(leafId) } + closeRenameSession() setRenamingPaneId(null) // Persist immediately so the title survives restarts. persistLayoutSnapshot() - }, [renamingPaneId, renameValue, removePaneTitle, persistLayoutSnapshot]) + }, [closeRenameSession, renamingPaneId, renameValue, removePaneTitle, persistLayoutSnapshot]) const handleRenameCancel = useCallback(() => { renameSubmittedRef.current = true + closeRenameSession() setRenamingPaneId(null) - }, []) + }, [closeRenameSession]) + + const handleRenameBlur = useCallback(() => { + if (renameBlurCommitEnabledRef.current) { + handleRenameSubmit() + return + } + if (renamingPaneId === null || renameRefocusFrameRef.current !== null) { + return + } + + const sessionId = renameSessionIdRef.current + const paneId = renamingPaneId + // Why: the context-menu selection can be followed by a delayed Radix/xterm + // focus handoff. That synthetic early blur is not a title submission. + renameRefocusFrameRef.current = requestAnimationFrame(() => { + renameRefocusFrameRef.current = null + if (renameSessionIdRef.current !== sessionId || renamingPaneId !== paneId) { + return + } + const input = renameInputRef.current + if (!input) { + renameBlurCommitEnabledRef.current = true + handleRenameSubmit() + return + } + input.focus() + input.select() + if (document.activeElement === input) { + renameBlurCommitEnabledRef.current = true + return + } + renameBlurCommitEnabledRef.current = true + handleRenameSubmit() + }) + }, [handleRenameSubmit, renamingPaneId]) const handleRemoveTitle = useCallback( (paneId: number) => removePaneTitle(paneId), @@ -1281,13 +1354,34 @@ export default function TerminalPane({ if (renamingPaneId === null) { return } + const sessionId = renameSessionIdRef.current + const paneId = renamingPaneId renameSubmittedRef.current = false - const frame = requestAnimationFrame(() => { - renameInputRef.current?.focus() - renameInputRef.current?.select() + renameFocusFrameRef.current = requestAnimationFrame(() => { + renameFocusFrameRef.current = null + if (renameSessionIdRef.current !== sessionId || renamingPaneId !== paneId) { + return + } + const input = renameInputRef.current + if (!input) { + return + } + input.focus() + input.select() + renameEnableBlurFrameRef.current = requestAnimationFrame(() => { + renameEnableBlurFrameRef.current = null + if ( + renameSessionIdRef.current === sessionId && + renamingPaneId === paneId && + renameInputRef.current === input && + document.activeElement === input + ) { + renameBlurCommitEnabledRef.current = true + } + }) }) - return () => cancelAnimationFrame(frame) - }, [renamingPaneId]) + return () => cancelPendingRenameFrames() + }, [cancelPendingRenameFrames, renamingPaneId]) const contextMenu = useTerminalPaneContextMenu({ managerRef, @@ -1553,7 +1647,7 @@ export default function TerminalPane({ handleRenameCancel() } }} - onBlur={handleRenameSubmit} + onBlur={handleRenameBlur} /> ) : ( <> diff --git a/tests/e2e/terminal-panes.spec.ts b/tests/e2e/terminal-panes.spec.ts index 64e3bbe72..dd6f58d7a 100644 --- a/tests/e2e/terminal-panes.spec.ts +++ b/tests/e2e/terminal-panes.spec.ts @@ -42,12 +42,7 @@ import { import { pressShortcut } from './helpers/shortcuts' async function setPaneTitleFromTerminalMenu(page: Page, title: string): Promise { - const modifiers: ('Alt' | 'Control' | 'Meta' | 'Shift')[] = - process.platform === 'win32' ? ['Control'] : [] - await page - .locator('.xterm:visible') - .first() - .click({ button: 'right', position: { x: 40, y: 40 }, modifiers }) + await openTerminalContextMenu(page) await page.getByText('Set Title…', { exact: true }).click() const titleInput = page.locator('.pane-title-input').first() await expect(titleInput).toBeVisible() @@ -63,6 +58,24 @@ async function setPaneTitleFromTerminalMenu(page: Page, title: string): Promise< await expect(titleInput).toHaveCount(0) } +async function openTerminalContextMenu(page: Page): Promise { + const modifiers: ('Alt' | 'Control' | 'Meta' | 'Shift')[] = (await page.evaluate(() => + navigator.userAgent.includes('Windows') + )) + ? ['Control'] + : [] + const isMac = await page.evaluate(() => navigator.userAgent.includes('Mac')) + await page + .locator('.xterm:visible') + .first() + .click({ + button: isMac ? 'left' : 'right', + position: { x: 40, y: 40 }, + modifiers: isMac ? ['Control'] : modifiers + }) + await expect(page.getByText('Set Title…', { exact: true })).toBeVisible() +} + async function getTabCustomTitle( page: Page, worktreeId: string, @@ -211,6 +224,72 @@ test.describe('Terminal Panes', () => { expect(activeLeafId).toMatch(UUID_RE) }) + test('first Set Title from terminal context menu stays open for typing', async ({ orcaPage }) => { + const title = `First menu title ${Date.now()}` + + await openTerminalContextMenu(orcaPage) + await orcaPage.getByText('Set Title…', { exact: true }).click() + + const titleInput = orcaPage.locator('.pane-title-input').first() + await expect(titleInput).toBeVisible() + await expect(titleInput).toBeFocused() + await orcaPage.waitForTimeout(250) + await expect(titleInput).toBeVisible() + await expect(titleInput).toBeFocused() + + await titleInput.fill(title) + await titleInput.press('Enter') + + await expect(titleInput).toHaveCount(0) + await expect(orcaPage.locator('.pane-title-text', { hasText: title })).toHaveCount(1) + }) + + test('Set Title survives an early blur during first focus handoff', async ({ orcaPage }) => { + await openTerminalContextMenu(orcaPage) + await orcaPage.evaluate(() => { + const blurOnFirstTitleFocus = (event: FocusEvent): void => { + const target = event.target + if ( + !(target instanceof HTMLInputElement) || + !target.classList.contains('pane-title-input') + ) { + return + } + document.removeEventListener('focusin', blurOnFirstTitleFocus, true) + queueMicrotask(() => target.blur()) + } + document.addEventListener('focusin', blurOnFirstTitleFocus, true) + }) + await orcaPage.getByText('Set Title…', { exact: true }).click() + + const titleInput = orcaPage.locator('.pane-title-input').first() + await expect(titleInput).toBeVisible() + await expect(titleInput).toBeFocused() + await orcaPage.waitForTimeout(250) + await expect(titleInput).toBeVisible() + await expect(titleInput).toBeFocused() + }) + + test('Set Title still commits by blur after focus settles', async ({ orcaPage }) => { + const title = `Blur commit title ${Date.now()}` + + await openTerminalContextMenu(orcaPage) + await orcaPage.getByText('Set Title…', { exact: true }).click() + + const titleInput = orcaPage.locator('.pane-title-input').first() + await expect(titleInput).toBeVisible() + await expect(titleInput).toBeFocused() + await orcaPage.waitForTimeout(100) + await titleInput.fill(title) + await orcaPage + .locator('.xterm:visible') + .first() + .click({ position: { x: 40, y: 60 } }) + + await expect(titleInput).toHaveCount(0) + await expect(orcaPage.locator('.pane-title-text', { hasText: title })).toHaveCount(1) + }) + test('Set Title stays pane-local during agent title churn', async ({ orcaPage }) => { const worktreeId = (await getActiveWorktreeId(orcaPage))! const tabId = (await getActiveTabId(orcaPage))!