diff --git a/src/main/browser/browser-guest-ui.ts b/src/main/browser/browser-guest-ui.ts index a10dfc4a3..313404888 100644 --- a/src/main/browser/browser-guest-ui.ts +++ b/src/main/browser/browser-guest-ui.ts @@ -238,6 +238,22 @@ export function setupGuestShortcutForwarding(args: { return } + // Why: Cmd/Ctrl+Alt+[ / ] cycles across every tab type. Handled before + // the generic modifier-chord gate below because that gate rejects Alt. + // Mirrors the Alt-exempt branch pattern used for worktreeHistoryNavigate. + const isPrimaryMod = + process.platform === 'darwin' ? input.meta && !input.control : input.control && !input.meta + if ( + isPrimaryMod && + input.alt && + (input.code === 'BracketRight' || input.code === 'BracketLeft') + ) { + event.preventDefault() + const renderer = resolveRenderer(browserTabId) + renderer?.send('ui:switchTabAcrossAllTypes', input.code === 'BracketRight' ? 1 : -1) + return + } + // Why: terminal-only tab switching is intentionally Ctrl+PageUp/PageDown on // every platform. Handle it before the primary-modifier gate so macOS Ctrl // (non-primary there) still forwards out of focused browser guests. diff --git a/src/preload/api-types.ts b/src/preload/api-types.ts index aad6e227c..3c5551eb3 100644 --- a/src/preload/api-types.ts +++ b/src/preload/api-types.ts @@ -858,6 +858,7 @@ export type PreloadApi = { onHardReloadBrowserPage: (callback: () => void) => () => void onCloseActiveTab: (callback: () => void) => () => void onSwitchTab: (callback: (direction: 1 | -1) => void) => () => void + onSwitchTabAcrossAllTypes: (callback: (direction: 1 | -1) => void) => () => void onSwitchTerminalTab: (callback: (direction: 1 | -1) => void) => () => void onToggleStatusBar: (callback: () => void) => () => void onExportPdfRequested: (callback: () => void) => () => void diff --git a/src/preload/index.ts b/src/preload/index.ts index 162f16293..c568c75a2 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -1437,6 +1437,11 @@ const api = { ipcRenderer.on('ui:switchTab', listener) return () => ipcRenderer.removeListener('ui:switchTab', listener) }, + onSwitchTabAcrossAllTypes: (callback: (direction: 1 | -1) => void): (() => void) => { + const listener = (_event: Electron.IpcRendererEvent, direction: 1 | -1) => callback(direction) + ipcRenderer.on('ui:switchTabAcrossAllTypes', listener) + return () => ipcRenderer.removeListener('ui:switchTabAcrossAllTypes', listener) + }, onSwitchTerminalTab: (callback: (direction: 1 | -1) => void): (() => void) => { const listener = (_event: Electron.IpcRendererEvent, direction: 1 | -1) => callback(direction) ipcRenderer.on('ui:switchTerminalTab', listener) diff --git a/src/renderer/src/components/Terminal.tsx b/src/renderer/src/components/Terminal.tsx index 5e5fc59f6..665ddfd57 100644 --- a/src/renderer/src/components/Terminal.tsx +++ b/src/renderer/src/components/Terminal.tsx @@ -39,7 +39,11 @@ import { collectBrowserWebviewIds, destroyWorkspaceWebviews } from '../store/slices/browser-webview-cleanup' -import { handleSwitchTab, handleSwitchTerminalTab } from '../hooks/ipc-tab-switch' +import { + handleSwitchTab, + handleSwitchTabAcrossAllTypes, + handleSwitchTerminalTab +} from '../hooks/ipc-tab-switch' import TabGroupSplitLayout from './tab-group/TabGroupSplitLayout' import { shouldAutoCreateInitialTerminal } from './terminal/initial-terminal' import { focusTerminalTabSurface } from '@/lib/focus-terminal-tab-surface' @@ -957,16 +961,21 @@ function Terminal(): React.JSX.Element | null { return } - // Cmd/Ctrl+Shift+] and Cmd/Ctrl+Shift+[ - switch tabs + // Cmd/Ctrl+Shift+] and Cmd/Ctrl+Shift+[ - switch tabs (scoped to the + // active tab type). Cmd/Ctrl+Alt+] and Cmd/Ctrl+Alt+[ cycles across + // every tab type as an escape hatch from the type-scoped default, and + // mirrors Safari/Chrome's tab-switch chord on macOS. // Why: use e.code instead of e.key because on macOS, Shift+[ reports '{' - // as the key value (the shifted character), not '['. + // as the key value (the shifted character), not '['. Option+[ also + // composes to dead-key / punctuation on many layouts, so matching on + // event.key would miss the chord entirely on non-US layouts. if ( mod && - e.shiftKey && (e.code === 'BracketRight' || e.code === 'BracketLeft') && - !e.repeat + !e.repeat && + (e.shiftKey || e.altKey) ) { - // Why: delegate to the shared handleSwitchTab used by the IPC shortcut + // Why: delegate to the shared handler used by the IPC shortcut path // so both code paths share one implementation. Always consume the // chord — even when the switch is a no-op (e.g. single tab), we own // this key combo and shouldn't let it reach xterm or the browser @@ -974,7 +983,11 @@ function Terminal(): React.JSX.Element | null { e.preventDefault() e.stopPropagation() e.stopImmediatePropagation() - handleSwitchTab(e.code === 'BracketRight' ? 1 : -1) + if (e.altKey) { + handleSwitchTabAcrossAllTypes(e.code === 'BracketRight' ? 1 : -1) + } else { + handleSwitchTab(e.code === 'BracketRight' ? 1 : -1) + } } // Ctrl+PageDown/PageUp - switch terminal tabs only diff --git a/src/renderer/src/components/settings/ShortcutsPane.tsx b/src/renderer/src/components/settings/ShortcutsPane.tsx index 04185accb..28063fb02 100644 --- a/src/renderer/src/components/settings/ShortcutsPane.tsx +++ b/src/renderer/src/components/settings/ShortcutsPane.tsx @@ -102,21 +102,21 @@ const SHORTCUT_GROUP_DEFINITIONS: ShortcutGroupDefinition[] = [ ] }, { - title: 'Terminal Tabs', + title: 'Tabs', items: [ { - action: 'New Terminal', - searchKeywords: ['shortcut', 'tab', 'terminal'], + action: 'New terminal tab', + searchKeywords: ['shortcut', 'tab', 'terminal', 'new'], keys: ({ mod }) => [mod, 'T'] }, { - action: 'New Browser Tab', - searchKeywords: ['shortcut', 'tab', 'browser'], + action: 'New browser tab', + searchKeywords: ['shortcut', 'tab', 'browser', 'new'], keys: ({ mod, shift }) => [mod, shift, 'B'] }, { - action: 'New Markdown', - searchKeywords: ['shortcut', 'tab', 'markdown', 'file'], + action: 'New markdown tab', + searchKeywords: ['shortcut', 'tab', 'markdown', 'file', 'new'], keys: ({ mod, shift }) => [mod, shift, 'M'] }, { @@ -128,25 +128,40 @@ const SHORTCUT_GROUP_DEFINITIONS: ShortcutGroupDefinition[] = [ action: 'Reopen closed tab', searchKeywords: ['shortcut', 'tab', 'reopen', 'restore', 'closed'], keys: ({ mod, shift }) => [mod, shift, 'T'] - }, + } + ] + }, + { + title: 'Tab Navigation', + items: [ { - action: 'Next tab', - searchKeywords: ['shortcut', 'tab', 'next'], + action: 'Next tab (same type)', + searchKeywords: ['shortcut', 'tab', 'next', 'switch', 'cycle'], keys: ({ mod, shift }) => [mod, shift, ']'] }, { - action: 'Previous tab', - searchKeywords: ['shortcut', 'tab', 'previous'], + action: 'Previous tab (same type)', + searchKeywords: ['shortcut', 'tab', 'previous', 'switch', 'cycle'], keys: ({ mod, shift }) => [mod, shift, '['] }, + { + action: 'Next tab (all types)', + searchKeywords: ['shortcut', 'tab', 'next', 'switch', 'cycle', 'all', 'any'], + keys: ({ mod }) => [mod, mod === '⌘' ? '⌥' : 'Alt', ']'] + }, + { + action: 'Previous tab (all types)', + searchKeywords: ['shortcut', 'tab', 'previous', 'switch', 'cycle', 'all', 'any'], + keys: ({ mod }) => [mod, mod === '⌘' ? '⌥' : 'Alt', '['] + }, { action: 'Next terminal tab', - searchKeywords: ['shortcut', 'tab', 'terminal', 'next'], + searchKeywords: ['shortcut', 'tab', 'terminal', 'next', 'switch'], keys: () => ['Ctrl', 'PageDown'] }, { action: 'Previous terminal tab', - searchKeywords: ['shortcut', 'tab', 'terminal', 'previous'], + searchKeywords: ['shortcut', 'tab', 'terminal', 'previous', 'switch'], keys: () => ['Ctrl', 'PageUp'] } ] diff --git a/src/renderer/src/components/terminal/tab-type-cycle.test.ts b/src/renderer/src/components/terminal/tab-type-cycle.test.ts new file mode 100644 index 000000000..3db556cb0 --- /dev/null +++ b/src/renderer/src/components/terminal/tab-type-cycle.test.ts @@ -0,0 +1,219 @@ +import { describe, expect, it } from 'vitest' +import { + getNextTabAcrossAllTypes, + getNextTabWithinActiveType, + type TypeCyclableTab +} from './tab-type-cycle' + +const mixedTabs: TypeCyclableTab[] = [ + { type: 'terminal', id: 'term-1' }, + { type: 'editor', id: 'file-1', tabId: 'tab-file-1' }, + { type: 'browser', id: 'browser-1' }, + { type: 'terminal', id: 'term-2' }, + { type: 'editor', id: 'file-2', tabId: 'tab-file-2' }, + { type: 'browser', id: 'browser-2' } +] + +describe('getNextTabWithinActiveType', () => { + it('cycles only terminal tabs when a terminal is active', () => { + expect( + getNextTabWithinActiveType({ + tabs: mixedTabs, + activeTabType: 'terminal', + activeTabId: 'term-1', + activeFileId: 'file-1', + activeBrowserTabId: 'browser-1', + direction: 1 + }) + ).toEqual({ type: 'terminal', id: 'term-2' }) + }) + + it('cycles only editor tabs when an editor is active', () => { + expect( + getNextTabWithinActiveType({ + tabs: mixedTabs, + activeTabType: 'editor', + activeTabId: 'term-1', + activeFileId: 'file-1', + activeBrowserTabId: 'browser-1', + direction: 1 + }) + ).toEqual({ type: 'editor', id: 'file-2', tabId: 'tab-file-2' }) + }) + + it('cycles only browser tabs when a browser is active', () => { + expect( + getNextTabWithinActiveType({ + tabs: mixedTabs, + activeTabType: 'browser', + activeTabId: 'term-1', + activeFileId: 'file-1', + activeBrowserTabId: 'browser-1', + direction: 1 + }) + ).toEqual({ type: 'browser', id: 'browser-2' }) + }) + + it('returns null when the active type has a single tab', () => { + expect( + getNextTabWithinActiveType({ + tabs: [ + { type: 'terminal', id: 'term-1' }, + { type: 'editor', id: 'file-1' }, + { type: 'browser', id: 'browser-1' } + ], + activeTabType: 'browser', + activeTabId: 'term-1', + activeFileId: 'file-1', + activeBrowserTabId: 'browser-1', + direction: 1 + }) + ).toBeNull() + }) + + it('uses direction-aware fallback when the active tab is missing from the active type', () => { + const terminalTabs: TypeCyclableTab[] = [ + { type: 'terminal', id: 'term-1' }, + { type: 'terminal', id: 'term-2' }, + { type: 'terminal', id: 'term-3' } + ] + + expect( + getNextTabWithinActiveType({ + tabs: terminalTabs, + activeTabType: 'terminal', + activeTabId: null, + activeFileId: 'file-1', + activeBrowserTabId: 'browser-1', + direction: -1 + }) + ).toEqual({ type: 'terminal', id: 'term-3' }) + + expect( + getNextTabWithinActiveType({ + tabs: terminalTabs, + activeTabType: 'terminal', + activeTabId: null, + activeFileId: 'file-1', + activeBrowserTabId: 'browser-1', + direction: 1 + }) + ).toEqual({ type: 'terminal', id: 'term-1' }) + }) + + it('uses the active group tab id for split editor duplicates', () => { + expect( + getNextTabWithinActiveType({ + tabs: [ + { type: 'editor', id: 'file-a', tabId: 'tab-a' }, + { type: 'terminal', id: 'term-1' }, + { type: 'editor', id: 'file-a', tabId: 'tab-b' }, + { type: 'editor', id: 'file-c', tabId: 'tab-c' } + ], + activeTabType: 'editor', + activeTabId: 'term-1', + activeFileId: 'file-a', + activeBrowserTabId: 'browser-1', + activeGroupTabId: 'tab-b', + direction: 1 + }) + ).toEqual({ type: 'editor', id: 'file-c', tabId: 'tab-c' }) + }) +}) + +describe('getNextTabAcrossAllTypes', () => { + it('cycles to the next tab regardless of type when a terminal is active', () => { + expect( + getNextTabAcrossAllTypes({ + tabs: mixedTabs, + activeTabType: 'terminal', + activeTabId: 'term-1', + activeFileId: 'file-1', + activeBrowserTabId: 'browser-1', + direction: 1 + }) + ).toEqual({ type: 'editor', id: 'file-1', tabId: 'tab-file-1' }) + }) + + it('wraps from the last tab back to the first', () => { + expect( + getNextTabAcrossAllTypes({ + tabs: mixedTabs, + activeTabType: 'browser', + activeTabId: 'term-1', + activeFileId: 'file-1', + activeBrowserTabId: 'browser-2', + direction: 1 + }) + ).toEqual({ type: 'terminal', id: 'term-1' }) + }) + + it('cycles backward across types', () => { + expect( + getNextTabAcrossAllTypes({ + tabs: mixedTabs, + activeTabType: 'editor', + activeTabId: 'term-1', + activeFileId: 'file-1', + activeBrowserTabId: 'browser-1', + direction: -1 + }) + ).toEqual({ type: 'terminal', id: 'term-1' }) + }) + + it('returns null when only one tab exists total', () => { + expect( + getNextTabAcrossAllTypes({ + tabs: [{ type: 'terminal', id: 'term-1' }], + activeTabType: 'terminal', + activeTabId: 'term-1', + activeFileId: null, + activeBrowserTabId: null, + direction: 1 + }) + ).toBeNull() + }) + + it('prefers the active group tab id to disambiguate split duplicates', () => { + expect( + getNextTabAcrossAllTypes({ + tabs: [ + { type: 'editor', id: 'file-a', tabId: 'tab-a' }, + { type: 'terminal', id: 'term-1' }, + { type: 'editor', id: 'file-a', tabId: 'tab-b' }, + { type: 'browser', id: 'browser-1', tabId: 'tab-browser-1' } + ], + activeTabType: 'editor', + activeTabId: 'term-1', + activeFileId: 'file-a', + activeBrowserTabId: null, + activeGroupTabId: 'tab-b', + direction: 1 + }) + ).toEqual({ type: 'browser', id: 'browser-1', tabId: 'tab-browser-1' }) + }) + + it('uses direction-aware fallback when the active tab id is missing', () => { + expect( + getNextTabAcrossAllTypes({ + tabs: mixedTabs, + activeTabType: 'terminal', + activeTabId: 'nonexistent', + activeFileId: null, + activeBrowserTabId: null, + direction: 1 + }) + ).toEqual(mixedTabs[0]) + + expect( + getNextTabAcrossAllTypes({ + tabs: mixedTabs, + activeTabType: 'terminal', + activeTabId: 'nonexistent', + activeFileId: null, + activeBrowserTabId: null, + direction: -1 + }) + ).toEqual(mixedTabs.at(-1)) + }) +}) diff --git a/src/renderer/src/components/terminal/tab-type-cycle.ts b/src/renderer/src/components/terminal/tab-type-cycle.ts new file mode 100644 index 000000000..af86198f3 --- /dev/null +++ b/src/renderer/src/components/terminal/tab-type-cycle.ts @@ -0,0 +1,117 @@ +export type TabCycleType = 'terminal' | 'editor' | 'browser' + +export type TypeCyclableTab = { + type: TabCycleType + id: string + tabId?: string +} + +type GetNextTabWithinActiveTypeParams = { + tabs: TypeCyclableTab[] + activeTabType: TabCycleType + activeTabId: string | null + activeFileId: string | null + activeBrowserTabId: string | null + activeGroupTabId?: string | null + direction: number +} + +export function getActiveEntityIdForTabType( + activeTabType: TabCycleType, + activeTabId: string | null, + activeFileId: string | null, + activeBrowserTabId: string | null +): string | null { + if (activeTabType === 'editor') { + return activeFileId + } + if (activeTabType === 'browser') { + return activeBrowserTabId + } + return activeTabId +} + +type GetNextTabAcrossAllTypesParams = { + tabs: TypeCyclableTab[] + activeTabType: TabCycleType + activeTabId: string | null + activeFileId: string | null + activeBrowserTabId: string | null + activeGroupTabId?: string | null + direction: number +} + +// Why: companion to getNextTabWithinActiveType for the Cmd/Ctrl+Alt+Shift+]/[ +// "cycle across every tab" chord. Keeps the same dual-id matching semantics +// (prefer the active group's unified tabId to disambiguate split layouts, fall +// back to the backing entity id) so behavior matches what the TabBar renders. +export function getNextTabAcrossAllTypes({ + tabs, + activeTabType, + activeTabId, + activeFileId, + activeBrowserTabId, + activeGroupTabId, + direction +}: GetNextTabAcrossAllTypesParams): TypeCyclableTab | null { + if (tabs.length <= 1) { + return null + } + + const groupTabIdInNav = + activeGroupTabId && tabs.some((entry) => entry.tabId === activeGroupTabId) + ? activeGroupTabId + : null + const currentId = getActiveEntityIdForTabType( + activeTabType, + activeTabId, + activeFileId, + activeBrowserTabId + ) + const currentIndex = groupTabIdInNav + ? tabs.findIndex((tab) => tab.tabId === groupTabIdInNav) + : tabs.findIndex((tab) => tab.type === activeTabType && tab.id === currentId) + + if (currentIndex === -1) { + return direction < 0 ? tabs.at(-1)! : tabs.at(0)! + } + + return tabs[(currentIndex + direction + tabs.length) % tabs.length] +} + +export function getNextTabWithinActiveType({ + tabs, + activeTabType, + activeTabId, + activeFileId, + activeBrowserTabId, + activeGroupTabId, + direction +}: GetNextTabWithinActiveTypeParams): TypeCyclableTab | null { + const tabsOfActiveType = tabs.filter((tab) => tab.type === activeTabType) + if (tabsOfActiveType.length <= 1) { + return null + } + + const groupTabIdInNav = + activeGroupTabId && tabsOfActiveType.some((entry) => entry.tabId === activeGroupTabId) + ? activeGroupTabId + : null + const currentId = getActiveEntityIdForTabType( + activeTabType, + activeTabId, + activeFileId, + activeBrowserTabId + ) + const currentIndex = groupTabIdInNav + ? tabsOfActiveType.findIndex((tab) => tab.tabId === groupTabIdInNav) + : tabsOfActiveType.findIndex((tab) => tab.id === currentId) + + if (currentIndex === -1) { + return direction < 0 ? tabsOfActiveType.at(-1)! : tabsOfActiveType.at(0)! + } + + return tabsOfActiveType[ + (currentIndex + direction + tabsOfActiveType.length) % tabsOfActiveType.length + ] +} diff --git a/src/renderer/src/components/terminal/useTerminalShortcuts.ts b/src/renderer/src/components/terminal/useTerminalShortcuts.ts index e380f5613..0d875a228 100644 --- a/src/renderer/src/components/terminal/useTerminalShortcuts.ts +++ b/src/renderer/src/components/terminal/useTerminalShortcuts.ts @@ -1,11 +1,15 @@ import { useEffect, useEffectEvent } from 'react' import type { UnifiedTerminalItem } from './useTerminalTabs' +import { getNextTabAcrossAllTypes, getNextTabWithinActiveType } from './tab-type-cycle' import { isUpdaterQuitAndInstallInProgress } from '@/lib/updater-beforeunload' type UseTerminalShortcutsParams = { activeWorktreeId: string | null activeTabId: string | null activeFileId: string | null + // Why: unifiedTabs only contains 'terminal' | 'editor' entries (no browsers), + // so this hook's cycling is constrained to those two types. Browser tab + // cycling is handled elsewhere where browser tabs are actually in scope. activeTabType: 'terminal' | 'editor' unifiedTabs: UnifiedTerminalItem[] hasDirtyFiles: boolean @@ -53,25 +57,44 @@ export function useTerminalShortcuts({ return } - // Why: use event.code instead of event.key because on macOS, Shift+[ - // reports '{' as the key value (the shifted character), not '['. + // Why: accept either Shift (type-scoped chord) or Alt (all-types chord). + // Use event.code rather than event.key because on macOS, Shift+[ reports + // '{' and Option+[ composes to a dead/accent character, so event.key + // wouldn't reliably match across layouts. if ( - !event.shiftKey || + (!event.shiftKey && !event.altKey) || (event.code !== 'BracketRight' && event.code !== 'BracketLeft') ) { return } - if (unifiedTabs.length <= 1) { + const direction = event.code === 'BracketRight' ? 1 : -1 + // Why: UnifiedTerminalItem has { type: 'terminal' | 'editor', id }, which is + // structurally assignable to TypeCyclableTab ({ type, id, tabId? }) with a + // narrower `type`. No cast needed — passing null for activeBrowserTabId + // since browser tabs can never appear in unifiedTabs. + const nextTab = event.altKey + ? getNextTabAcrossAllTypes({ + tabs: unifiedTabs, + activeTabType, + activeTabId, + activeFileId, + activeBrowserTabId: null, + direction + }) + : getNextTabWithinActiveType({ + tabs: unifiedTabs, + activeTabType, + activeTabId, + activeFileId, + activeBrowserTabId: null, + direction + }) + if (!nextTab) { return } event.preventDefault() - const currentId = activeTabType === 'editor' ? activeFileId : activeTabId - const currentIndex = unifiedTabs.findIndex((tab) => tab.id === currentId) - const direction = event.code === 'BracketRight' ? 1 : -1 - const nextTab = - unifiedTabs[(currentIndex + direction + unifiedTabs.length) % unifiedTabs.length] if (nextTab.type === 'terminal') { onActivateTerminalTab(nextTab.id) diff --git a/src/renderer/src/hooks/ipc-tab-switch.test.ts b/src/renderer/src/hooks/ipc-tab-switch.test.ts index 1efce70f8..e8bb9936e 100644 --- a/src/renderer/src/hooks/ipc-tab-switch.test.ts +++ b/src/renderer/src/hooks/ipc-tab-switch.test.ts @@ -15,7 +15,11 @@ vi.mock('@/components/tab-bar/group-tab-order', () => ({ getActiveTabNavOrder: getActiveTabNavOrderMock })) -import { handleSwitchTab, handleSwitchTerminalTab } from './ipc-tab-switch' +import { + handleSwitchTab, + handleSwitchTabAcrossAllTypes, + handleSwitchTerminalTab +} from './ipc-tab-switch' type ActiveTabType = 'terminal' | 'editor' | 'browser' @@ -171,41 +175,25 @@ describe('handleSwitchTab', () => { vi.clearAllMocks() }) - it('switches terminal to editor using the backing file id and unified tab id', () => { + it('cycles terminal tabs without jumping to editor or browser tabs', () => { const store = makeStore('terminal') store.activeTabId = 'term-1' getStateMock.mockReturnValue(store) getActiveTabNavOrderMock.mockReturnValue([ { type: 'terminal', id: 'term-1' }, { type: 'editor', id: 'file-1', tabId: 'tab-editor-1' }, - { type: 'browser', id: 'browser-1', tabId: 'tab-browser-1' } - ]) - - expect(handleSwitchTab(1)).toBe(true) - expect(store.setActiveFile).toHaveBeenCalledWith('file-1') - expect(store.activateTab).toHaveBeenCalledWith('tab-editor-1') - expect(store.setActiveTabType).toHaveBeenCalledWith('editor') - }) - - it('uses the active group tab id to find the current editor index', () => { - const store = makeStore('editor') - store.activeFileId = 'file-1' - store.activeGroupIdByWorktree = { 'wt-1': 'group-1' } - store.groupsByWorktree = { 'wt-1': [{ id: 'group-1', activeTabId: 'tab-editor-b' }] } - getStateMock.mockReturnValue(store) - getActiveTabNavOrderMock.mockReturnValue([ - { type: 'editor', id: 'file-1', tabId: 'tab-editor-a' }, - { type: 'terminal', id: 'term-1' }, - { type: 'editor', id: 'file-1', tabId: 'tab-editor-b' }, + { type: 'browser', id: 'browser-1', tabId: 'tab-browser-1' }, { type: 'terminal', id: 'term-2' } ]) expect(handleSwitchTab(1)).toBe(true) expect(store.setActiveTab).toHaveBeenCalledWith('term-2') + expect(store.setActiveFile).not.toHaveBeenCalled() + expect(store.setActiveBrowserTab).not.toHaveBeenCalled() expect(store.setActiveTabType).toHaveBeenCalledWith('terminal') }) - it('switches from one editor to another using the active group tab id and nav tab ids', () => { + it('cycles editor tabs using the active group tab id', () => { const store = makeStore('editor') store.activeFileId = 'file-a' store.activeGroupIdByWorktree = { 'wt-1': 'group-1' } @@ -213,7 +201,9 @@ describe('handleSwitchTab', () => { getStateMock.mockReturnValue(store) getActiveTabNavOrderMock.mockReturnValue([ { type: 'editor', id: 'file-a', tabId: 'tab-a' }, + { type: 'terminal', id: 'term-1' }, { type: 'editor', id: 'file-a', tabId: 'tab-b' }, + { type: 'browser', id: 'browser-1', tabId: 'tab-browser-1' }, { type: 'editor', id: 'file-c', tabId: 'tab-c' } ]) @@ -223,24 +213,26 @@ describe('handleSwitchTab', () => { expect(store.setActiveTabType).toHaveBeenCalledWith('editor') }) - it('ignores editor refs with tab ids when switching terminal tabs only', () => { - const store = makeStore('terminal') - store.activeTabId = 'term-2' + it('cycles browser tabs without jumping to other tab types', () => { + const store = makeStore('browser') + store.activeBrowserTabId = 'browser-1' getStateMock.mockReturnValue(store) getActiveTabNavOrderMock.mockReturnValue([ { type: 'terminal', id: 'term-1' }, { type: 'editor', id: 'editor-1', tabId: 'tab-editor-1' }, - { type: 'terminal', id: 'term-2' }, - { type: 'editor', id: 'editor-2', tabId: 'tab-editor-2' }, - { type: 'terminal', id: 'term-3' } + { type: 'browser', id: 'browser-1', tabId: 'tab-browser-1' }, + { type: 'browser', id: 'browser-2', tabId: 'tab-browser-2' }, + { type: 'terminal', id: 'term-2' } ]) - expect(handleSwitchTerminalTab(1)).toBe(true) - expect(store.setActiveTab).toHaveBeenCalledWith('term-3') - expect(store.setActiveTabType).toHaveBeenCalledWith('terminal') + expect(handleSwitchTab(1)).toBe(true) + expect(store.setActiveBrowserTab).toHaveBeenCalledWith('browser-2') + expect(store.setActiveTab).not.toHaveBeenCalled() + expect(store.setActiveFile).not.toHaveBeenCalled() + expect(store.setActiveTabType).toHaveBeenCalledWith('browser') }) - it('switches editor to terminal without touching editor activation', () => { + it('returns false when the active type has only one tab', () => { const store = makeStore('editor') store.activeFileId = 'file-1' getStateMock.mockReturnValue(store) @@ -250,24 +242,74 @@ describe('handleSwitchTab', () => { { type: 'browser', id: 'browser-1', tabId: 'tab-browser-1' } ]) - expect(handleSwitchTab(1)).toBe(true) - expect(store.setActiveTab).toHaveBeenCalledWith('term-1') + expect(handleSwitchTab(1)).toBe(false) + expect(store.setActiveTab).not.toHaveBeenCalled() expect(store.setActiveFile).not.toHaveBeenCalled() - expect(store.setActiveTabType).toHaveBeenCalledWith('terminal') + expect(store.setActiveTabType).not.toHaveBeenCalled() }) it('falls back when an editor entry has no unified tab id', () => { + const store = makeStore('editor') + store.activeFileId = 'file-1' + getStateMock.mockReturnValue(store) + getActiveTabNavOrderMock.mockReturnValue([ + { type: 'terminal', id: 'term-1' }, + { type: 'editor', id: 'file-1' }, + { type: 'editor', id: 'file-2' } + ]) + + expect(() => handleSwitchTab(1)).not.toThrow() + expect(store.setActiveFile).toHaveBeenCalledWith('file-2') + expect(store.activateTab).not.toHaveBeenCalled() + expect(store.setActiveTabType).toHaveBeenCalledWith('editor') + }) +}) + +describe('handleSwitchTabAcrossAllTypes', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + it('crosses tab types, e.g. terminal → editor', () => { const store = makeStore('terminal') store.activeTabId = 'term-1' getStateMock.mockReturnValue(store) getActiveTabNavOrderMock.mockReturnValue([ { type: 'terminal', id: 'term-1' }, - { type: 'editor', id: 'file-1' } + { type: 'editor', id: 'file-1', tabId: 'tab-file-1' }, + { type: 'browser', id: 'browser-1', tabId: 'tab-browser-1' } ]) - expect(() => handleSwitchTab(1)).not.toThrow() + expect(handleSwitchTabAcrossAllTypes(1)).toBe(true) expect(store.setActiveFile).toHaveBeenCalledWith('file-1') - expect(store.activateTab).not.toHaveBeenCalled() + expect(store.activateTab).toHaveBeenCalledWith('tab-file-1') expect(store.setActiveTabType).toHaveBeenCalledWith('editor') }) + + it('wraps around across types', () => { + const store = makeStore('browser') + store.activeBrowserTabId = 'browser-1' + store.activeGroupIdByWorktree = {} + store.groupsByWorktree = {} + getStateMock.mockReturnValue(store) + getActiveTabNavOrderMock.mockReturnValue([ + { type: 'terminal', id: 'term-1' }, + { type: 'editor', id: 'file-1', tabId: 'tab-file-1' }, + { type: 'browser', id: 'browser-1', tabId: 'tab-browser-1' } + ]) + + expect(handleSwitchTabAcrossAllTypes(1)).toBe(true) + expect(store.setActiveTab).toHaveBeenCalledWith('term-1') + expect(store.setActiveTabType).toHaveBeenCalledWith('terminal') + }) + + it('returns false when only one tab exists total', () => { + const store = makeStore('terminal') + getStateMock.mockReturnValue(store) + getActiveTabNavOrderMock.mockReturnValue([{ type: 'terminal', id: 'term-1' }]) + + expect(handleSwitchTabAcrossAllTypes(1)).toBe(false) + expect(store.setActiveTab).not.toHaveBeenCalled() + expect(store.setActiveTabType).not.toHaveBeenCalled() + }) }) diff --git a/src/renderer/src/hooks/ipc-tab-switch.ts b/src/renderer/src/hooks/ipc-tab-switch.ts index 60e7d9770..632d65e53 100644 --- a/src/renderer/src/hooks/ipc-tab-switch.ts +++ b/src/renderer/src/hooks/ipc-tab-switch.ts @@ -1,23 +1,38 @@ import { useAppStore } from '../store' import { getActiveTabNavOrder } from '@/components/tab-bar/group-tab-order' +import { + getActiveEntityIdForTabType, + getNextTabAcrossAllTypes, + getNextTabWithinActiveType, + type TypeCyclableTab +} from '@/components/terminal/tab-type-cycle' + +type AppStoreState = ReturnType + +type CycleContext = { + store: AppStoreState + worktreeId: string + allTabIds: TypeCyclableTab[] + groupTabIdInNav: string | null +} /** - * Handle Cmd/Ctrl+Tab direction switching across terminal, editor, and browser tabs. - * Extracted from useIpcEvents to keep file size under the max-lines lint threshold. - * Returns true if a tab switch occurred, false otherwise. + * Shared setup for the Cmd/Ctrl+Shift+[ / ] chords (both the type-scoped and + * across-all-types variants). Returns null when there is no active worktree + * or the visible nav has at most one tab (nothing to cycle). */ -export function handleSwitchTab(direction: number): boolean { +function resolveCycleContext(): CycleContext | null { const store = useAppStore.getState() const worktreeId = store.activeWorktreeId if (!worktreeId) { - return false + return null } // Why: walk the active group's visible order so drag-reordered tabs cycle // in the sequence the user sees. See getActiveTabNavOrder for the stale // legacy-order bug this replaces. const allTabIds = getActiveTabNavOrder(store, worktreeId) if (allTabIds.length <= 1) { - return false + return null } const activeGroupId = store.activeGroupIdByWorktree[worktreeId] const group = activeGroupId @@ -33,24 +48,24 @@ export function handleSwitchTab(direction: number): boolean { group?.activeTabId && allTabIds.some((entry) => entry.tabId === group.activeTabId) ? group.activeTabId : null - let idx: number - if (groupTabIdInNav) { - idx = allTabIds.findIndex((t) => t.tabId === groupTabIdInNav) - } else { - const fallbackId = - store.activeTabType === 'editor' - ? store.activeFileId - : store.activeTabType === 'browser' - ? store.activeBrowserTabId - : store.activeTabId - idx = allTabIds.findIndex((t) => t.id === fallbackId) - } - const next = allTabIds[(idx + direction + allTabIds.length) % allTabIds.length] + return { store, worktreeId, allTabIds, groupTabIdInNav } +} + +/** + * Apply the next-tab selection to the store. Preserves the split-layout + * disambiguation: `activateTab(tabId)` is required on the file/browser + * branches so that when the same entity is open in multiple splits, the + * correct tab instance is focused. + */ +function applyNextTab(store: AppStoreState, next: TypeCyclableTab): void { if (next.type === 'terminal') { store.setActiveTab(next.id) store.setActiveTabType('terminal') } else if (next.type === 'browser') { store.setActiveBrowserTab(next.id) + if (next.tabId) { + store.activateTab?.(next.tabId) + } store.setActiveTabType('browser') } else { // Why: `setActiveFile` targets the file entity (its implicit activateTab @@ -62,6 +77,64 @@ export function handleSwitchTab(direction: number): boolean { } store.setActiveTabType('editor') } +} + +/** + * Handle Cmd/Ctrl+Shift+[ / ] direction switching within the active tab type. + * Extracted from useIpcEvents to keep file size under the max-lines lint threshold. + * Returns true if a tab switch occurred, false otherwise. + */ +export function handleSwitchTab(direction: number): boolean { + const ctx = resolveCycleContext() + if (!ctx) { + return false + } + const { store, allTabIds, groupTabIdInNav } = ctx + const next = getNextTabWithinActiveType({ + tabs: allTabIds, + activeTabType: store.activeTabType, + activeTabId: store.activeTabId, + activeFileId: store.activeFileId, + activeBrowserTabId: store.activeBrowserTabId, + activeGroupTabId: groupTabIdInNav, + direction + }) + if (!next) { + return false + } + applyNextTab(store, next) + return true +} + +/** + * Handle Cmd/Ctrl+Alt+Shift+[ / ] cycling across every visible tab, + * regardless of tab type. + * + * Why: companion chord to the type-scoped Cmd/Ctrl+Shift+[ / ] that ships as + * the default. The type-scoped chord is the VS Code-style per-pane cycle; + * this one gives users an escape hatch back to the pre-scope "cycle through + * everything" behavior without needing a settings toggle (see PR #1281 + * discussion). Returns true if a tab switch occurred, false otherwise. + */ +export function handleSwitchTabAcrossAllTypes(direction: number): boolean { + const ctx = resolveCycleContext() + if (!ctx) { + return false + } + const { store, allTabIds, groupTabIdInNav } = ctx + const next = getNextTabAcrossAllTypes({ + tabs: allTabIds, + activeTabType: store.activeTabType, + activeTabId: store.activeTabId, + activeFileId: store.activeFileId, + activeBrowserTabId: store.activeBrowserTabId, + activeGroupTabId: groupTabIdInNav, + direction + }) + if (!next) { + return false + } + applyNextTab(store, next) return true } @@ -83,12 +156,12 @@ export function handleSwitchTerminalTab(direction: number): boolean { if (terminalTabs.length === 0) { return false } - const currentId = - store.activeTabType === 'editor' - ? store.activeFileId - : store.activeTabType === 'browser' - ? store.activeBrowserTabId - : store.activeTabId + const currentId = getActiveEntityIdForTabType( + store.activeTabType, + store.activeTabId, + store.activeFileId, + store.activeBrowserTabId + ) // Why: when an editor/browser tab is active, jump to the first terminal on // forward navigation instead of skipping to index 1. const idx = terminalTabs.findIndex((t) => t.id === currentId) diff --git a/src/renderer/src/hooks/useIpcEvents.test.ts b/src/renderer/src/hooks/useIpcEvents.test.ts index 83651491f..a66728403 100644 --- a/src/renderer/src/hooks/useIpcEvents.test.ts +++ b/src/renderer/src/hooks/useIpcEvents.test.ts @@ -171,6 +171,7 @@ describe('useIpcEvents updater integration', () => { onNewTerminalTab: () => () => {}, onCloseActiveTab: () => () => {}, onSwitchTab: () => () => {}, + onSwitchTabAcrossAllTypes: () => () => {}, onSwitchTerminalTab: () => () => {}, onToggleStatusBar: () => () => {}, onFullscreenChanged: () => () => {}, @@ -361,6 +362,7 @@ describe('useIpcEvents updater integration', () => { onNewTerminalTab: () => () => {}, onCloseActiveTab: () => () => {}, onSwitchTab: () => () => {}, + onSwitchTabAcrossAllTypes: () => () => {}, onSwitchTerminalTab: () => () => {}, onToggleStatusBar: () => () => {}, onFullscreenChanged: () => () => {}, @@ -550,6 +552,7 @@ describe('useIpcEvents updater integration', () => { onNewTerminalTab: () => () => {}, onCloseActiveTab: () => () => {}, onSwitchTab: () => () => {}, + onSwitchTabAcrossAllTypes: () => () => {}, onSwitchTerminalTab: () => () => {}, onToggleStatusBar: () => () => {}, onFullscreenChanged: () => () => {}, @@ -750,6 +753,7 @@ describe('useIpcEvents browser tab close routing', () => { onNewTerminalTab: () => () => {}, onCloseActiveTab: () => () => {}, onSwitchTab: () => () => {}, + onSwitchTabAcrossAllTypes: () => () => {}, onSwitchTerminalTab: () => () => {}, onToggleStatusBar: () => () => {}, onFullscreenChanged: () => () => {}, @@ -935,6 +939,7 @@ describe('useIpcEvents browser tab close routing', () => { onNewTerminalTab: () => () => {}, onCloseActiveTab: () => () => {}, onSwitchTab: () => () => {}, + onSwitchTabAcrossAllTypes: () => () => {}, onSwitchTerminalTab: () => () => {}, onToggleStatusBar: () => () => {}, onFullscreenChanged: () => () => {}, @@ -1115,6 +1120,7 @@ describe('useIpcEvents browser tab close routing', () => { onNewTerminalTab: () => () => {}, onCloseActiveTab: () => () => {}, onSwitchTab: () => () => {}, + onSwitchTabAcrossAllTypes: () => () => {}, onSwitchTerminalTab: () => () => {}, onToggleStatusBar: () => () => {}, onFullscreenChanged: () => () => {}, @@ -1304,6 +1310,7 @@ describe('useIpcEvents shortcut hint clearing', () => { onNewTerminalTab: () => () => {}, onCloseActiveTab: () => () => {}, onSwitchTab: () => () => {}, + onSwitchTabAcrossAllTypes: () => () => {}, onSwitchTerminalTab: () => () => {}, onToggleStatusBar: () => () => {}, onFullscreenChanged: () => () => {}, @@ -1500,6 +1507,7 @@ describe('useIpcEvents CLI-created worktree activation', () => { onNewTerminalTab: () => () => {}, onCloseActiveTab: () => () => {}, onSwitchTab: () => () => {}, + onSwitchTabAcrossAllTypes: () => () => {}, onSwitchTerminalTab: () => () => {}, onToggleStatusBar: () => () => {}, onFullscreenChanged: () => () => {}, diff --git a/src/renderer/src/hooks/useIpcEvents.ts b/src/renderer/src/hooks/useIpcEvents.ts index 4106b81ee..6bae8766f 100644 --- a/src/renderer/src/hooks/useIpcEvents.ts +++ b/src/renderer/src/hooks/useIpcEvents.ts @@ -13,7 +13,11 @@ import type { SshConnectionState } from '../../../shared/ssh-types' import { zoomLevelToPercent, ZOOM_MIN, ZOOM_MAX } from '@/components/settings/SettingsConstants' import { dispatchZoomLevelChanged } from '@/lib/zoom-events' import { resolveZoomTarget } from './resolve-zoom-target' -import { handleSwitchTab, handleSwitchTerminalTab } from './ipc-tab-switch' +import { + handleSwitchTab, + handleSwitchTabAcrossAllTypes, + handleSwitchTerminalTab +} from './ipc-tab-switch' import { dispatchClearModifierHints } from './useModifierHint' import { normalizeAgentStatusPayload } from '../../../shared/agent-status-types' import { isGitRepoKind } from '../../../shared/repo-kind' @@ -549,6 +553,7 @@ export function useIpcEvents(): void { ) unsubs.push(window.api.ui.onSwitchTab(handleSwitchTab)) + unsubs.push(window.api.ui.onSwitchTabAcrossAllTypes(handleSwitchTabAcrossAllTypes)) unsubs.push(window.api.ui.onSwitchTerminalTab(handleSwitchTerminalTab)) // Hydrate initial rate limit state then subscribe to push updates