@@ -266,12 +266,18 @@ import {
266266import { appendPreviewAnnotationPrompt } from "../lib/previewAnnotation" ;
267267import { appendReviewCommentsToPrompt , type ReviewCommentContext } from "../reviewCommentContext" ;
268268import { environmentCatalog } from "../connection/catalog" ;
269+ import { selectThreadTerminalUiState , useTerminalUiStateStore } from "../terminalUiStateStore" ;
269270import {
270- selectPinnedTerminalThreadKey ,
271- selectThreadTerminalUiState ,
272- useTerminalUiStateStore ,
273- } from "../terminalUiStateStore" ;
274- import { useTerminalDrawerRef , useThreadProjectKey } from "../hooks/useTerminalDrawerRef" ;
271+ useTerminalDrawerPin ,
272+ useTerminalDrawerRef ,
273+ useThreadProjectKey ,
274+ } from "../hooks/useTerminalDrawerRef" ;
275+ import {
276+ environmentTerminalPinKey ,
277+ nextTerminalDrawerPinState ,
278+ projectTerminalPinKey ,
279+ type TerminalDrawerPinState ,
280+ } from "../lib/terminalDrawer" ;
275281import { useKnownTerminalSessions , useThreadRunningTerminalIds } from "../state/terminalSessions" ;
276282import { projectEnvironment } from "../state/projects" ;
277283import { useEnvironmentQuery } from "../state/query" ;
@@ -808,8 +814,8 @@ interface PersistentThreadTerminalDrawerProps {
808814 /** Thread being viewed; links open in its preview even when this is another thread's pinned drawer. */
809815 previewThreadRef : ScopedThreadRef ;
810816 active : boolean ;
811- pinned : boolean ;
812- onTogglePinned : ( ) => void ;
817+ pinState : TerminalDrawerPinState ;
818+ onCyclePin : ( ) => void ;
813819 launchContext : PersistentTerminalLaunchContext | null ;
814820 focusRequestId : number ;
815821 splitShortcutLabel : string | undefined ;
@@ -825,8 +831,8 @@ const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerminalDra
825831 threadId,
826832 previewThreadRef,
827833 active,
828- pinned ,
829- onTogglePinned ,
834+ pinState ,
835+ onCyclePin ,
830836 launchContext,
831837 focusRequestId,
832838 splitShortcutLabel,
@@ -1155,8 +1161,8 @@ const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerminalDra
11551161 previewThreadRef = { previewThreadRef }
11561162 threadId = { threadId }
11571163 cwd = { cwd }
1158- pinned = { pinned }
1159- onTogglePinned = { onTogglePinned }
1164+ pinState = { pinState }
1165+ onCyclePin = { onCyclePin }
11601166 worktreePath = { effectiveWorktreePath }
11611167 runtimeEnv = { runtimeEnv }
11621168 visible = { visible }
@@ -1817,37 +1823,40 @@ export default function ChatView(props: ChatViewProps) {
18171823 const activeThreadKey = activeThreadRef ? scopedThreadKey ( activeThreadRef ) : null ;
18181824 // The drawer follows the project's pin: the pinned thread's drawer when one
18191825 // is pinned, otherwise the thread's own. Right-panel terminals ignore pins.
1820- const activeTerminalDrawerRef = useTerminalDrawerRef ( activeThreadRef ) ;
1826+ const { drawerRef : activeTerminalDrawerRef , pinState : terminalDrawerPinState } =
1827+ useTerminalDrawerPin ( activeThreadRef ) ;
18211828 const activeTerminalDrawerKey = activeTerminalDrawerRef
18221829 ? scopedThreadKey ( activeTerminalDrawerRef )
18231830 : null ;
18241831 const terminalDrawerThreadId = activeTerminalDrawerRef ?. threadId ?? null ;
18251832 const activeThreadProjectKey = useThreadProjectKey ( activeThreadRef ) ;
1826- const pinnedTerminalThreadKey = useTerminalUiStateStore ( ( state ) =>
1827- selectPinnedTerminalThreadKey (
1828- state . pinnedTerminalThreadKeyByProjectKey ,
1829- activeThreadProjectKey ,
1830- ) ,
1831- ) ;
1832- // A pin only counts once it resolved; a stale pin to a deleted thread reads as unpinned.
1833- const terminalDrawerPinned =
1834- pinnedTerminalThreadKey !== null && pinnedTerminalThreadKey === activeTerminalDrawerKey ;
18351833 const pinTerminalDrawer = useTerminalUiStateStore ( ( state ) => state . pinTerminalDrawer ) ;
18361834 const unpinTerminalDrawer = useTerminalUiStateStore ( ( state ) => state . unpinTerminalDrawer ) ;
1837- // Pinning keeps the drawer in front for every thread in the project;
1838- // unpinning hands each thread its own drawer back.
1839- const toggleTerminalDrawerPinned = useCallback ( ( ) => {
1840- if ( activeThreadProjectKey === null || ! activeThreadRef ) return ;
1841- if ( terminalDrawerPinned ) {
1842- unpinTerminalDrawer ( activeThreadProjectKey ) ;
1843- } else {
1844- pinTerminalDrawer ( activeThreadProjectKey , activeThreadRef ) ;
1835+ // The pin button cycles off → this project → every project → off. Moving
1836+ // from project to every project drops the project pin, so unpinning later
1837+ // lands on off instead of falling back to the project pin.
1838+ const cycleTerminalDrawerPin = useCallback ( ( ) => {
1839+ if ( ! activeThreadRef || ! activeTerminalDrawerRef || activeThreadProjectKey === null ) return ;
1840+ const environmentPinKey = environmentTerminalPinKey ( activeThreadRef . environmentId ) ;
1841+ const projectPinKey = projectTerminalPinKey ( activeThreadProjectKey ) ;
1842+ switch ( nextTerminalDrawerPinState ( terminalDrawerPinState ) ) {
1843+ case "project" :
1844+ pinTerminalDrawer ( projectPinKey , activeThreadRef ) ;
1845+ return ;
1846+ case "environment" :
1847+ pinTerminalDrawer ( environmentPinKey , activeTerminalDrawerRef ) ;
1848+ unpinTerminalDrawer ( projectPinKey ) ;
1849+ return ;
1850+ case "none" :
1851+ unpinTerminalDrawer ( environmentPinKey ) ;
1852+ return ;
18451853 }
18461854 } , [
1855+ activeTerminalDrawerRef ,
18471856 activeThreadProjectKey ,
18481857 activeThreadRef ,
18491858 pinTerminalDrawer ,
1850- terminalDrawerPinned ,
1859+ terminalDrawerPinState ,
18511860 unpinTerminalDrawer ,
18521861 ] ) ;
18531862 const runningTerminalIds = useThreadRunningTerminalIds ( {
@@ -8247,8 +8256,8 @@ export default function ChatView(props: ChatViewProps) {
82478256 threadId = { mountedThreadRef . threadId }
82488257 previewThreadRef = { ( isActiveDrawer ? activeThreadRef : null ) ?? mountedThreadRef }
82498258 active = { isActiveDrawer }
8250- pinned = { isActiveDrawer && terminalDrawerPinned }
8251- onTogglePinned = { toggleTerminalDrawerPinned }
8259+ pinState = { isActiveDrawer ? terminalDrawerPinState : "none" }
8260+ onCyclePin = { cycleTerminalDrawerPin }
82528261 launchContext = { isActiveDrawer ? ( activeTerminalLaunchContext ?? null ) : null }
82538262 focusRequestId = { isActiveDrawer ? terminalFocusRequestId : 0 }
82548263 splitShortcutLabel = { splitTerminalShortcutLabel ?? undefined }
0 commit comments