diff --git a/src/renderer/src/components/tab-bar/open-tab-selection-routing.ts b/src/renderer/src/components/tab-bar/open-tab-selection-routing.ts index 96517d6e2ad..be253e1f4b3 100644 --- a/src/renderer/src/components/tab-bar/open-tab-selection-routing.ts +++ b/src/renderer/src/components/tab-bar/open-tab-selection-routing.ts @@ -6,6 +6,7 @@ import { activateBrowserPagePaletteResult } from '@/lib/browser/palette/browser- import { focusTerminalTabSurface } from '@/components/terminal-pane/input/focus/focus-terminal-tab-surface' import { activateSimulatorTabPaletteResult } from '@/lib/simulator/tabs/simulator-tab-palette-activation' import { activateWorkspaceTabPaletteResult } from '@/lib/workspace/search/tabs/workspace-tab-palette-activation' +import { useAppStore } from '@/store/index' import type { OpenTabSearchResult } from './open-tab-search' export type OpenTabSelectionOutcome = @@ -95,6 +96,13 @@ export function activateOpenTabSearchResult(result: OpenTabSearchResult): OpenTa // Editors focus themselves when they mount; only the terminal surface needs a handoff. return { status: 'activated', - focus: result.contentType === 'terminal' ? () => focusTerminalTabSurface(result.entityId) : null + focus: + result.contentType === 'terminal' + ? () => + focusTerminalTabSurface( + result.entityId, + useAppStore.getState().terminalLayoutsByTabId[result.entityId]?.activeLeafId ?? null + ) + : null } } diff --git a/src/renderer/src/lib/workspace/search/tabs/workspace-tab-palette-activation.store.test.ts b/src/renderer/src/lib/workspace/search/tabs/workspace-tab-palette-activation.store.test.ts index 015cde35d3d..2769370ece2 100644 --- a/src/renderer/src/lib/workspace/search/tabs/workspace-tab-palette-activation.store.test.ts +++ b/src/renderer/src/lib/workspace/search/tabs/workspace-tab-palette-activation.store.test.ts @@ -1,6 +1,6 @@ // @vitest-environment happy-dom -import { afterEach, expect, it, vi } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { useAppStore } from '@/store/index' import type { Tab } from '../../../../../../shared/tab-types' import type { Worktree } from '../../../../../../shared/worktree/types' @@ -12,6 +12,7 @@ vi.mock('@/runtime/rpc/calls/runtime-rpc-call', () => ({ callRuntimeRpc: vi.fn(async () => undefined) })) +import { activateOpenTabSearchResult } from '@/components/tab-bar/open-tab-selection-routing' import { activateWorkspaceTabPaletteResult } from './workspace-tab-palette-activation' import { getClientHostedBrowserRowSelection, @@ -19,7 +20,10 @@ import { } from '../../../../runtime/browser/client-hosting/client-hosted-browser-row-state' const initialState = useAppStore.getInitialState() -afterEach(() => useAppStore.setState(initialState, true)) +afterEach(() => { + useAppStore.setState(initialState, true) + document.body.innerHTML = '' +}) const worktree: Worktree = { id: 'wt', @@ -138,3 +142,124 @@ it('shows the selected chat even when a paired-device page placeholder covered i expect(getClientHostedBrowserRowSelection()).toBeNull() expect(useAppStore.getState().activeTabType).toBe('agent-session') }) + +describe('split terminal focus', () => { + // Keyed by frame id so cancelAnimationFrame drops superseded focus requests like the browser does. + let frames = new Map() + let nextFrameId = 0 + + beforeEach(() => { + frames = new Map() + vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback) => { + frames.set(++nextFrameId, callback) + return nextFrameId + }) + vi.spyOn(window, 'cancelAnimationFrame').mockImplementation((id) => { + frames.delete(id) + }) + }) + + afterEach(() => { + vi.restoreAllMocks() + }) + + function seedSplitTerminal(): void { + const terminal: Tab = { + id: 'unified-terminal', + entityId: 'terminal', + groupId: 'group', + worktreeId: 'wt', + contentType: 'terminal', + label: 'Example terminal', + customLabel: null, + color: null, + sortOrder: 0, + createdAt: 0 + } + useAppStore.setState( + { + ...initialState, + worktreesByRepo: { repo: [worktree] }, + activeWorktreeId: 'wt', + groupsByWorktree: { + wt: [ + { + id: 'group', + worktreeId: 'wt', + activeTabId: 'unified-terminal', + tabOrder: ['unified-terminal'] + } + ] + }, + activeGroupIdByWorktree: { wt: 'group' }, + unifiedTabsByWorktree: { wt: [terminal] }, + terminalLayoutsByTabId: { + terminal: { + root: { + type: 'split', + direction: 'horizontal', + first: { type: 'leaf', leafId: 'left' }, + second: { type: 'leaf', leafId: 'right' } + }, + activeLeafId: 'right', + expandedLeafId: null + } + } + }, + true + ) + document.body.innerHTML = ` +
+
+
+
` + } + + function focusedLeafIdAfterFrames(): string | null | undefined { + // Map iteration visits entries added during the loop, so nested frames run too. + for (const [id, frame] of frames) { + frames.delete(id) + frame(0) + } + return document.activeElement?.closest('[data-leaf-id]')?.getAttribute('data-leaf-id') + } + + it('returns focus to the active pane when Cmd+J picks the tab', () => { + seedSplitTerminal() + + expect( + activateWorkspaceTabPaletteResult({ + worktreeId: 'wt', + groupId: 'group', + tabId: 'unified-terminal', + entityId: 'terminal', + contentType: 'terminal' + }) + ).toEqual({ status: 'activated' }) + expect(focusedLeafIdAfterFrames()).toBe('right') + }) + + it('returns focus to the active pane when the tab bar switch menu picks the tab', () => { + seedSplitTerminal() + + const outcome = activateOpenTabSearchResult({ + source: 'workspace', + executionHostId: 'local', + id: 'unified-terminal', + title: 'Example terminal', + matchedText: null, + worktreeId: 'wt', + contentType: 'terminal', + tabId: 'unified-terminal', + entityId: 'terminal', + groupId: 'group', + relativePath: null, + occupantAgent: null + }) + expect(outcome.status).toBe('activated') + if (outcome.status === 'activated') { + outcome.focus?.() + } + expect(focusedLeafIdAfterFrames()).toBe('right') + }) +}) diff --git a/src/renderer/src/lib/workspace/search/tabs/workspace-tab-palette-activation.ts b/src/renderer/src/lib/workspace/search/tabs/workspace-tab-palette-activation.ts index cfdbf60ee99..69a2170b724 100644 --- a/src/renderer/src/lib/workspace/search/tabs/workspace-tab-palette-activation.ts +++ b/src/renderer/src/lib/workspace/search/tabs/workspace-tab-palette-activation.ts @@ -46,6 +46,7 @@ type WorkspaceTabPaletteActivationState = Pick< | 'setActiveFile' | 'setActiveTab' | 'setActiveTabType' + | 'terminalLayoutsByTabId' | 'unifiedTabsByWorktree' | 'worktreesByRepo' > @@ -136,16 +137,19 @@ export function activateWorkspaceTabPaletteResult( state.activateTab(result.tabId, { worktreeId: result.worktreeId }) if (result.contentType === 'terminal') { + // Why: the palette already took focus, so a tab-wide focus or host activation picks the first split pane. + const activeLeafId = state.terminalLayoutsByTabId[result.entityId]?.activeLeafId ?? null if (isWebRuntimeSessionActive(runtimeEnvironmentId)) { void activateWebRuntimeSessionTab({ worktreeId: result.worktreeId, tabId: result.entityId, - environmentId: runtimeEnvironmentId + environmentId: runtimeEnvironmentId, + leafId: activeLeafId }) } state.setActiveTab(result.entityId) state.setActiveTabType('terminal', result.worktreeId) - focusTerminalTabSurface(result.entityId) + focusTerminalTabSurface(result.entityId, activeLeafId) return { status: 'activated' } }