feat(shortcuts): cycle Cmd/Ctrl+Shift+]/[ within current tab type by default (#1281)

* feat(shortcuts): cycle Cmd/Ctrl+Shift+]/[ within current tab type by default

Extract a shared getNextTabWithinActiveType selector into a new
tab-type-cycle.ts helper and route both useTerminalShortcuts.ts and
ipc-tab-switch.ts through it. Cmd/Ctrl+Shift+]/[ now cycles within the
active tab's type (terminal cycles only terminals, editor cycles only
editor tabs, browser cycles only browser tabs). Ctrl+PageDown/Up
remains the dedicated terminal-only chord (PR #1094) regardless of
focus.

useTerminalShortcuts gains optional activeBrowserTabId and
onActivateBrowserTab parameters so browser tabs can dispatch through
the same path. The default behavior matches VS Code's
TerminalContextKeys.focus-gated cycling and Superset's per-scope
chord registry referenced in the issue.

Adds tab-type-cycle.test.ts covering single-type, mixed-type, and
single-tab no-op cases. ipc-tab-switch.test.ts updated to assert
same-type cycling under the new selector.

Closes #1100

* fix(shortcuts): use direction-aware fallback when active tab is missing

When the active tab id is not present in the same-type subset (e.g.
during hydration when an editor is active and Cmd+Shift+]/[ targets
terminals), findIndex returns -1 and the previous modulo math made
backward navigation land on the second-to-last tab instead of the
last. Branch on the -1 case to return the last tab for direction=-1
and the first tab for direction=+1, so both directions are
predictable. Adds a regression test covering 3 terminal tabs with no
active match.

* feat(shortcuts): add Cmd/Ctrl+Alt+[/] to cycle across all tab types

Adds an "all types" variant of the tab cycle chord so users can page
through every tab in the active group regardless of type (terminal /
editor / browser), complementing the type-scoped Cmd/Ctrl+Shift+[/].

- Wires the chord through the main-window keydown handler, the browser
  guest shortcut forwarder, and a new ui:switchTabAcrossAllTypes IPC
  channel
- Factors getNextTabAcrossAllTypes alongside getNextTabWithinActiveType
  and shares the ipc-tab-switch dispatch logic via resolveCycleContext
  and applyNextTab helpers to avoid drift between the two chords
- Narrows useTerminalShortcuts activeTabType to 'terminal' | 'editor'
  (the hook's unifiedTabs never contains browsers) and drops the
  unsound TypeCyclableTab cast
- Updates ShortcutsPane to list the new chord and expands tests

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com>
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Trevin Chow
2026-05-04 09:53:00 -07:00
committed by GitHub
co-authored by Orca Jinjing
parent 05c0f776bb
commit 201685b2e0
12 changed files with 631 additions and 94 deletions
+16
View File
@@ -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.
+1
View File
@@ -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
+5
View File
@@ -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)
+20 -7
View File
@@ -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
@@ -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']
}
]
@@ -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))
})
})
@@ -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
]
}
@@ -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)
+80 -38
View File
@@ -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()
})
})
+98 -25
View File
@@ -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<typeof useAppStore.getState>
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)
@@ -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: () => () => {},
+6 -1
View File
@@ -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