From c3241bdb912763a309816d1a2f736b0a08836457 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Wed, 24 Jun 2026 17:44:22 -0700 Subject: [PATCH] Wire terminal worktree navigation to back-and-forth stack (#6310) * Wire terminal-initiated worktree navigation to back-and-forth stack * Unify terminal-driven worktree activation and Cmd+J recency marking. * Record worktree visits in the back/forward history stack unless navigating history. * fix: focus terminal-initiated worktree tabs --- src/renderer/src/hooks/useIpcEvents.test.ts | 10 ++++++++++ src/renderer/src/hooks/useIpcEvents.ts | 12 +++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/renderer/src/hooks/useIpcEvents.test.ts b/src/renderer/src/hooks/useIpcEvents.test.ts index 0a72767c7..8cdcb1fe3 100644 --- a/src/renderer/src/hooks/useIpcEvents.test.ts +++ b/src/renderer/src/hooks/useIpcEvents.test.ts @@ -1816,6 +1816,8 @@ describe('useIpcEvents updater integration', () => { expect(setActiveTabType).toHaveBeenCalledWith('terminal') expect(setActiveTab).toHaveBeenCalledWith('tab-new') expect(revealWorktreeInSidebar).toHaveBeenCalledWith('wt-2') + expect(focusRuntimeTerminalSurface).toHaveBeenCalledWith('tab-new', undefined) + expect(focusTerminalTabSurface).toHaveBeenCalledWith('tab-new', undefined) expect(setTabCustomTitle).toHaveBeenCalledWith('tab-new', 'Runner', { recordInteraction: false }) @@ -1836,6 +1838,8 @@ describe('useIpcEvents updater integration', () => { setTabCustomTitle.mockClear() queueTabStartupCommand.mockClear() replyTerminalCreate.mockClear() + focusRuntimeTerminalSurface.mockClear() + focusTerminalTabSurface.mockClear() requestTerminalCreateListenerRef.current({ requestId: 'req-focused', worktreeId: 'wt-3', @@ -1850,6 +1854,8 @@ describe('useIpcEvents updater integration', () => { expect(setActiveTabType).toHaveBeenCalledWith('terminal') expect(setActiveTab).toHaveBeenCalledWith('tab-new') expect(revealWorktreeInSidebar).toHaveBeenCalledWith('wt-3') + expect(focusRuntimeTerminalSurface).toHaveBeenCalledWith('tab-new', undefined) + expect(focusTerminalTabSurface).toHaveBeenCalledWith('tab-new', undefined) expect(setTabCustomTitle).toHaveBeenCalledWith('tab-new', 'Shell', { recordInteraction: false }) @@ -1869,6 +1875,8 @@ describe('useIpcEvents updater integration', () => { revealWorktreeInSidebar.mockClear() setTabCustomTitle.mockClear() queueTabStartupCommand.mockClear() + focusRuntimeTerminalSurface.mockClear() + focusTerminalTabSurface.mockClear() requestTerminalCreateListenerRef.current({ requestId: 'req-renderer-backed', worktreeId: 'wt-2', @@ -1894,6 +1902,8 @@ describe('useIpcEvents updater integration', () => { expect(setActiveTabType).not.toHaveBeenCalled() expect(setActiveTab).not.toHaveBeenCalled() expect(revealWorktreeInSidebar).not.toHaveBeenCalled() + expect(focusRuntimeTerminalSurface).not.toHaveBeenCalled() + expect(focusTerminalTabSurface).not.toHaveBeenCalled() expect(dispatchEvent).toHaveBeenCalledWith( expect.objectContaining({ type: 'orca-background-mount-terminal-worktree', diff --git a/src/renderer/src/hooks/useIpcEvents.ts b/src/renderer/src/hooks/useIpcEvents.ts index 55992b00e..d137d0352 100644 --- a/src/renderer/src/hooks/useIpcEvents.ts +++ b/src/renderer/src/hooks/useIpcEvents.ts @@ -263,6 +263,12 @@ function activateTerminalInitiatedWorktree(store: AppState, worktreeId: string): } } +function focusTerminalInitiatedTab(tabId: string, leafId?: string | null): void { + if (!focusRuntimeTerminalSurface(tabId, leafId)) { + focusTerminalTabSurface(tabId, leafId) + } +} + type TerminalSplitDirection = 'horizontal' | 'vertical' function insertLeafAfterSource( @@ -1393,6 +1399,7 @@ export function useIpcEvents(): void { store.setActiveTabType('terminal') store.setActiveTab(tab.id) store.revealWorktreeInSidebar(worktreeId) + focusTerminalInitiatedTab(tab.id, leafId) } // Why: only stamp the runtime-supplied title on freshly created tabs. // Existing tabs may have a user customTitle (set via UI rename) that @@ -1571,6 +1578,7 @@ export function useIpcEvents(): void { store.setActiveTabType('terminal') store.setActiveTab(tab.id) store.revealWorktreeInSidebar(worktreeId) + focusTerminalInitiatedTab(tab.id) } if (data.title) { store.setTabCustomTitle(tab.id, data.title, { recordInteraction: false }) @@ -1646,9 +1654,7 @@ export function useIpcEvents(): void { }) return } - if (!focusRuntimeTerminalSurface(tabId, leafId)) { - focusTerminalTabSurface(tabId, leafId) - } + focusTerminalInitiatedTab(tabId, leafId) } ) )