Skip to content

The agent that grows with you

fix(desktop): right-sidebar toggle acts on the physical right column

Brooklyn Nicholson0c208aa7ecaa
diff --git a/apps/desktop/src/app/hooks/use-keybinds-right-side.test.ts b/apps/desktop/src/app/hooks/use-keybinds-right-side.test.ts
new file mode 100644
index 00000000000..43f9ee479b7
--- /dev/null
+++ b/apps/desktop/src/app/hooks/use-keybinds-right-side.test.ts
@@ -0,0 +1,79 @@
+import { renderHook } from '@testing-library/react'
+import { createElement, type ReactNode } from 'react'
+import { MemoryRouter } from 'react-router'
+import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
+
+vi.mock('@/themes/context', () => ({
+ useTheme: () => ({ resolvedMode: 'dark' as const, setMode: () => undefined })
+}))
+
+const toggleTerminalPane = vi.hoisted(() => vi.fn())
+
+vi.mock('@/app/right-sidebar/terminal/reveal-focus', async importOriginal => ({
+ ...(await importOriginal<object>()),
+ toggleTerminalPane
+}))
+
+import { useKeybinds } from '@/app/hooks/use-keybinds'
+import { findGroupOfPane, group, type LayoutNode, split } from '@/components/pane-shell/tree/model'
+import { $layoutTree } from '@/components/pane-shell/tree/store'
+import { registry } from '@/contrib/registry'
+import { isMacPlatform } from '@/lib/platform'
+
+// ⌘J dispatch must resolve the SAME physical right target the toggle mutates:
+// a Browser column registers `placement: 'main'`, so the semantic side gate
+// saw no right side and sent ⌘J to the terminal instead of folding it.
+
+const BROWSER = 'preview-tile:url:browser'
+const wrapper = ({ children }: { children: ReactNode }) => createElement(MemoryRouter, null, children)
+
+let unmountKeybinds: (() => void) | undefined
+
+beforeAll(() => {
+ const placements = { sessions: 'left', files: 'right', [BROWSER]: 'main', workspace: 'main' }
+
+ for (const [id, placement] of Object.entries(placements)) {
+ registry.register({ id, area: 'panes', title: id, data: { placement }, render: () => null })
+ }
+})
+
+beforeEach(() => {
+ toggleTerminalPane.mockClear()
+ unmountKeybinds = renderHook(
+ () =>
+ useKeybinds({
+ archiveSelectedSession: () => undefined,
+ openNewSessionTab: () => undefined,
+ requestGateway: <T>() => Promise.resolve(undefined as T),
+ startFreshSession: () => undefined,
+ toggleCommandCenter: () => undefined,
+ toggleSelectedPin: () => undefined
+ }),
+ { wrapper }
+ ).unmount
+})
+
+afterEach(() => {
+ unmountKeybinds?.()
+ $layoutTree.set(null)
+})
+
+describe('⌘J (view.toggleRightSidebar)', () => {
+ it('folds a Browser column on the right instead of toggling the terminal', () => {
+ $layoutTree.set(split('row', [group(['sessions', 'files']), group(['workspace']), group([BROWSER])]))
+
+ window.dispatchEvent(
+ new KeyboardEvent('keydown', {
+ bubbles: true,
+ cancelable: true,
+ code: 'KeyJ',
+ ctrlKey: !isMacPlatform(),
+ key: 'j',
+ metaKey: isMacPlatform()
+ })
+ )
+
+ expect(findGroupOfPane($layoutTree.get() as LayoutNode, BROWSER)?.minimized).toBe(true)
+ expect(toggleTerminalPane).not.toHaveBeenCalled()
+ })
+})
diff --git a/apps/desktop/src/app/hooks/use-keybinds.ts b/apps/desktop/src/app/hooks/use-keybinds.ts
index a835962821a..22b0411dd19 100644
--- a/apps/desktop/src/app/hooks/use-keybinds.ts
+++ b/apps/desktop/src/app/hooks/use-keybinds.ts
@@ -11,7 +11,6 @@ import {
activateTreeTabSlot,
cycleTreeTabInFocusedZone,
isPaneVisible,
- layoutHasRootSide,
toggleTargetZoneTabStrip
} from '@/components/pane-shell/tree/store'
import { setWorkspaceScope } from '@/components/pane-shell/workspace-scope'
@@ -43,10 +42,11 @@ import { toggleSimpleMode } from '@/store/interface-mode'
import { $capture, $comboIndex, captureStep, endCapture, setBinding } from '@/store/keybinds'
import {
cycleSidebarGrouping,
+ layoutHasRightSide,
requestSessionSearchFocus,
setFileBrowserOpen,
- toggleFileBrowserOpen,
togglePanesFlipped,
+ toggleRightSide,
toggleSidebarOpen
} from '@/store/layout'
import { notifyError } from '@/store/notifications'
@@ -338,10 +338,10 @@ export function useKeybinds(deps: KeybindRuntimeDeps): void {
// Narrow-viewport reveal is handled inside the store toggles now.
'view.toggleSidebar': toggleSidebarOpen,
'view.cycleSidebarGrouping': cycleSidebarGrouping,
- // ⌘J toggles the right sidebar — but a layout with no right side (e.g.
- // terminal-on-bottom) would leave it a dead key, so it falls back to the
- // terminal there. The single "secondary panel" toggle.
- 'view.toggleRightSidebar': () => (layoutHasRootSide('right') ? toggleFileBrowserOpen() : toggleTerminalPane()),
+ // ⌘J toggles the physical right side — whatever column lives there in the
+ // live tree (the Browser preview column, the files column). Falls back to
+ // the terminal when nothing lives on the right (terminal-on-bottom).
+ 'view.toggleRightSidebar': () => (layoutHasRightSide() ? toggleRightSide() : toggleTerminalPane()),
'view.toggleReview': toggleReview,
'view.toggleStatusbar': toggleStatusbarVisible,
'view.toggleProfileRail': toggleProfileRailVisible,
diff --git a/apps/desktop/src/app/shell/titlebar-controls.tsx b/apps/desktop/src/app/shell/titlebar-controls.tsx
index 7c6beb8537c..06029237fe2 100644
--- a/apps/desktop/src/app/shell/titlebar-controls.tsx
+++ b/apps/desktop/src/app/shell/titlebar-controls.tsx
@@ -22,8 +22,8 @@ import {
$fileBrowserOpen,
$panesFlipped,
$sidebarOpen,
- toggleFileBrowserOpen,
togglePanesFlipped,
+ toggleRightSide,
toggleSidebarOpen
} from '@/store/layout'
import { $unreadSessionCount } from '@/store/session-dot-state'
@@ -160,11 +160,12 @@ export function TitlebarControls({ leftTools = [], tools = [], onOpenSettings }:
// POSITIONAL toggles: each button shows/hides everything on its physical
// side of the main zone (the layout tree collapses the whole side), so they
- // stay correct through flips and rearranges. $sidebarOpen ≙ left side,
- // $fileBrowserOpen ≙ right side. Never an active highlight — plain
- // show/hide affordances.
+ // stay correct through flips and rearranges. $sidebarOpen ≙ left side; the
+ // right toggle resolves its column from the live tree (see toggleRightSide)
+ // — the browser column, the files column, whatever is physically right.
+ // Never an active highlight — plain show/hide affordances.
const leftEdge = { open: sidebarOpen, toggle: toggleSidebarOpen }
- const rightEdge = { open: fileBrowserOpen, toggle: toggleFileBrowserOpen }
+ const rightEdge = { open: fileBrowserOpen, toggle: toggleRightSide }
const leftLabel = leftEdge.open ? t.titlebar.hideSidebar : t.titlebar.showSidebar
const rightLabel = rightEdge.open ? t.titlebar.hideRightSidebar : t.titlebar.showRightSidebar
diff --git a/apps/desktop/src/components/pane-shell/tree/store.ts b/apps/desktop/src/components/pane-shell/tree/store.ts
index 8bc17a10344..d3597386026 100644
--- a/apps/desktop/src/components/pane-shell/tree/store.ts
+++ b/apps/desktop/src/components/pane-shell/tree/store.ts
@@ -937,7 +937,7 @@ export function removeTreePane(paneId: string) {
* Usually the root itself (Default, Focus); in a column-root layout (Terminal
* deck, Quad) it's the row child that holds sessions/workspace/files. Returns
* null when the tree has no row split with side-eligible panes. */
-function rootRow(): SplitNode | null {
+export function rootRow(): SplitNode | null {
const tree = $layoutTree.get()
if (!tree || tree.type !== 'split') {
diff --git a/apps/desktop/src/store/layout.ts b/apps/desktop/src/store/layout.ts
index 86b518f9302..f89aa64236d 100644
--- a/apps/desktop/src/store/layout.ts
+++ b/apps/desktop/src/store/layout.ts
@@ -2,10 +2,18 @@ import { atom, computed, type ReadableAtom, type WritableAtom } from 'nanostores
import { SIDEBAR_COLLAPSE_MEDIA_QUERY } from '@/app/layout-constants'
import { PANE_TOGGLE_REVEAL_EVENT } from '@/components/pane-shell'
+import { allPaneIds, type GroupNode } from '@/components/pane-shell/tree/model'
import {
+ $collapsedTreeSides,
+ layoutHasRootSide,
+ paneRootSide,
restoreHiddenTreeSideTabs,
restoreMinimizedTreeSide,
+ restoreTreePane,
+ rootRow,
+ setTreeGroupMinimized,
setTreeSideCollapsed,
+ shownPanesInGroup,
type TreeSide
} from '@/components/pane-shell/tree/store'
import { matchesQuery } from '@/hooks/use-media-query'
@@ -597,6 +605,93 @@ export function setFileBrowserOpen(open: boolean) {
revealNarrowPane(FILE_BROWSER_PANE_ID, open ? 'open' : 'close')
}
+// POSITIONAL right-side toggle. The titlebar button and ⌘J promise
+// "everything on your physical right" — but preview tiles (the Browser) dock
+// beside main with placement 'main', so the SEMANTIC side derivation never
+// classifies their column as a side column, and the pane-bound toggle (files)
+// presses a pane that may have been dragged into the left stack. Resolve the
+// target from the TREE instead: the outermost root-row column right of main,
+// whatever panes live there — then fold/unfold it through the tree's own zone
+// minimize, which keeps its tab on a persistent rail so the toggle round-trips
+// exactly like the zone chevron.
+export function toggleRightSide() {
+ if (revealNarrowPane(FILE_BROWSER_PANE_ID, 'toggle')) {
+ return
+ }
+
+ const group = rightSideGroup()
+
+ if (!group) {
+ // No foldable column on the right (terminal-on-bottom, a nested right
+ // split): fall back to the semantic side collapse, never the left side.
+ toggleFileBrowserOpen()
+
+ return
+ }
+
+ if (rightSideShown(group)) {
+ setTreeGroupMinimized(group.id, true)
+
+ return
+ }
+
+ // Hidden — minimized, or its side collapsed by the side's owner (the files
+ // toggle, or the sidebar when flipped). Reopen through that owner so the
+ // store the titlebar reads agrees (revealTreePane deliberately never writes
+ // it), then restore the zone like its rail/chevron does.
+ const pane = group.active ?? group.panes[0]
+ const side = paneRootSide(pane)
+
+ if (side && $collapsedTreeSides.get().has(side)) {
+ const openSide = side === sidebarSide() ? setSidebarOpen : setFileBrowserOpen
+
+ openSide(true)
+ }
+
+ restoreTreePane(pane)
+}
+
+// ⌘J's dispatch gate, from the same resolution the toggle mutates through
+// (plus the semantic right side it falls back to). False only when nothing
+// lives on the right, so ⌘J can fall to the terminal instead.
+export function layoutHasRightSide(): boolean {
+ return rightSideGroup() !== null || layoutHasRootSide('right')
+}
+
+// The POSITIONAL right side, derived from the live tree: the outermost
+// root-row column, when it is a leaf group physically right of main. Unlike
+// the semantic `paneRootSide` (which sees only placement-tagged side panes),
+// this ALSO catches a preview-tile column (the Browser): its panes register
+// `placement: 'main'`, so the semantic walk classifies their zone as main.
+// Null when main is outermost (never reach past it into the left side) or the
+// right is a nested split (folding one inner zone would strand the rest) —
+// callers fall back to the semantic side collapse there.
+function rightSideGroup(): GroupNode | null {
+ const row = rootRow()
+ const edge = row?.children.at(-1)
+
+ if (!row || edge?.type !== 'group' || edge.panes.some(isMainSurface)) {
+ return null
+ }
+
+ return row.children.some(child => allPaneIds(child).some(isMainSurface)) ? edge : null
+}
+
+// Is the zone actually on screen? `minimized` alone isn't the whole state: a
+// side collapse or chrome-hidden panes take it off screen un-minimized.
+function rightSideShown(group: GroupNode): boolean {
+ const side = paneRootSide(allPaneIds(group)[0])
+
+ return !group.minimized && !(side && $collapsedTreeSides.get().has(side)) && shownPanesInGroup(group).length > 0
+}
+
+// Is `paneId` a main surface (the workspace or a session/route tile)? Those
+// mark THE main column; preview tiles share `placement: 'main'` with them but
+// are docked side surfaces, which is exactly why the positional walk exists.
+function isMainSurface(paneId: string): boolean {
+ return paneId === 'workspace' || paneId.startsWith('session-tile:') || paneId.startsWith('route-tile:')
+}
+
// "Reveal this file in the file-browser tree" — an absolute path the tree
// subscribes to, expanding ancestor folders and selecting/scrolling to it. Reset
// to null by the tree once consumed.
diff --git a/apps/desktop/src/store/right-side-toggle.test.ts b/apps/desktop/src/store/right-side-toggle.test.ts
new file mode 100644
index 00000000000..2bef4fecbc0
--- /dev/null
+++ b/apps/desktop/src/store/right-side-toggle.test.ts
@@ -0,0 +1,100 @@
+import { beforeAll, beforeEach, describe, expect, it } from 'vitest'
+
+import { findGroupOfPane, group, type LayoutNode, split } from '@/components/pane-shell/tree/model'
+import { $collapsedTreeSides, $hiddenTreePanes, $layoutTree } from '@/components/pane-shell/tree/store'
+import { registry } from '@/contrib/registry'
+import { $fileBrowserOpen, setFileBrowserOpen, setSidebarOpen, toggleRightSide } from '@/store/layout'
+
+// The right-side toggle must be POSITIONAL: it acts on whatever column is
+// physically rightmost in the root row — including a preview-tile column
+// (the Browser), whose panes register with `placement: 'main'` and are
+// therefore invisible to the semantic side derivation. Ground truth for the
+// "⌘J / titlebar toggle does nothing to the browser pane" bug: the old
+// pane-bound toggle pressed the files pane, which the user had dragged into
+// the left stack.
+
+const BROWSER = 'preview-tile:url:browser'
+
+// Mirror controller.tsx registrations: sessions left, files/terminal right,
+// and a preview tile (Browser) that docks beside main with placement 'main'.
+beforeAll(() => {
+ const placements = { sessions: 'left', files: 'right', terminal: 'right', [BROWSER]: 'main', workspace: 'main' }
+
+ for (const [id, placement] of Object.entries(placements)) {
+ registry.register({ id, area: 'panes', title: id, data: { placement }, render: () => null })
+ }
+})
+
+beforeEach(() => {
+ window.localStorage.clear()
+ $collapsedTreeSides.set(new Set())
+ $hiddenTreePanes.set(new Set())
+ setSidebarOpen(true)
+ setFileBrowserOpen(true)
+})
+
+const groupOf = (paneId: string) => findGroupOfPane($layoutTree.get() as LayoutNode, paneId)
+
+describe('positional right-side toggle', () => {
+ // User's arrangement: left stack holds sessions+files (dragged), browser
+ // column is its own zone on the right of the root row.
+ const browserRight = () =>
+ $layoutTree.set(split('row', [group(['sessions', 'files']), group(['workspace']), group([BROWSER])]))
+
+ it('folds the rightmost side column (the browser) to a minimized rail', () => {
+ browserRight()
+
+ toggleRightSide()
+
+ expect(groupOf(BROWSER)?.minimized).toBe(true)
+ })
+
+ it('round-trips: a second press restores the zone', () => {
+ browserRight()
+
+ toggleRightSide()
+ toggleRightSide()
+
+ expect(groupOf(BROWSER)?.minimized).toBe(false)
+ })
+
+ it('leaves the left stack (sessions+files) untouched', () => {
+ browserRight()
+
+ toggleRightSide()
+
+ expect(Boolean(groupOf('files')?.minimized)).toBe(false)
+ })
+
+ // Nothing right of main, or a nested right-hand split the positional fold
+ // can't target: the search must never cross main into the left sidebar.
+ it.each([
+ ['no right column', () => split('row', [group(['sessions']), group(['workspace'])])],
+ [
+ 'a nested right split',
+ () =>
+ split('row', [group(['sessions']), group(['workspace']), split('column', [group(['files']), group(['terminal'])])])
+ ]
+ ])('never folds the left sidebar (%s)', (_, tree) => {
+ $layoutTree.set(tree())
+
+ toggleRightSide()
+
+ expect(Boolean(groupOf('sessions')?.minimized)).toBe(false)
+ expect($collapsedTreeSides.get().has('left')).toBe(false)
+ })
+
+ // A side collapsed through its owner (the files toggle) is hidden even
+ // though its zone isn't minimized: the first press must OPEN it, through
+ // that owner, so the store the titlebar reads agrees.
+ it('first press reopens a right side collapsed by setFileBrowserOpen(false)', () => {
+ $layoutTree.set(split('row', [group(['sessions']), group(['workspace']), group(['files'])]))
+ setFileBrowserOpen(false)
+
+ toggleRightSide()
+
+ expect($collapsedTreeSides.get().has('right')).toBe(false)
+ expect(Boolean(groupOf('files')?.minimized)).toBe(false)
+ expect($fileBrowserOpen.get()).toBe(true)
+ })
+})