@@ -89,6 +89,12 @@ import { openTerminalLinkInPreview } from "./preview/openTerminalLinkInPreview";
8989import { useAtomCommand } from "../state/use-atom-command" ;
9090import { preventTerminalCloseShortcut } from "../lib/terminalCloseShortcut" ;
9191import { observeResize } from "../lib/observeResize" ;
92+ import {
93+ consumePendingTerminalFocus ,
94+ focusTerminal ,
95+ focusTerminalViewport ,
96+ registerTerminalFocusTarget ,
97+ } from "../lib/terminalFocus" ;
9298import {
9399 resolveTerminalFontPreference ,
94100 resolveTerminalFontSizePreference ,
@@ -420,8 +426,8 @@ interface TerminalViewportProps {
420426 providerInstanceId ?: ProviderInstanceId ;
421427 onSessionExited : ( ) => void ;
422428 onAddTerminalContext ?: ( selection : TerminalContextSelection ) => void ;
423- focusRequestId : number ;
424- autoFocus : boolean ;
429+ /** Focuses the viewport when it mounts; otherwise focus arrives through `focusTerminal`. */
430+ autoFocus ? : boolean ;
425431 visible : boolean ;
426432 resizeEpoch : number ;
427433 drawerHeight : number ;
@@ -458,14 +464,14 @@ function TerminalSessionViewport({
458464 providerInstanceId,
459465 onSessionExited,
460466 onAddTerminalContext,
461- focusRequestId,
462- autoFocus,
467+ autoFocus = false ,
463468 visible,
464469 resizeEpoch,
465470 drawerHeight,
466471 keybindings,
467472} : TerminalViewportProps ) {
468473 const containerRef = useRef < HTMLDivElement > ( null ) ;
474+ const autoFocusedRef = useRef ( false ) ;
469475 const terminalRef = useRef < GhosttyTerminalSurface | null > ( null ) ;
470476 const visibleRef = useRef ( visible ) ;
471477 const environmentId = threadRef . environmentId ;
@@ -591,7 +597,9 @@ function TerminalSessionViewport({
591597
592598 useLayoutEffect ( ( ) => {
593599 if ( terminalRef . current ) terminalRef . current . input . readOnly = ! canOperateTerminal ;
594- } , [ canOperateTerminal ] ) ;
600+ if ( canOperateTerminal && visibleRef . current )
601+ consumePendingTerminalFocus ( threadRef , terminalId ) ;
602+ } , [ canOperateTerminal , terminalId , threadRef ] ) ;
595603
596604 // Links re-read the grant on activation; only the hovered link's styling needs a nudge.
597605 const linkActivationScopeRef = useRef ( canOpenHostEditor ) ;
@@ -611,7 +619,9 @@ function TerminalSessionViewport({
611619 useLayoutEffect ( ( ) => {
612620 visibleRef . current = visible ;
613621 terminalRef . current ?. setVisible ( visible ) ;
614- } , [ visible ] ) ;
622+ // A request made while this viewport was hidden could not focus it.
623+ if ( visible ) consumePendingTerminalFocus ( threadRef , terminalId ) ;
624+ } , [ terminalId , threadRef , visible ] ) ;
615625
616626 useEffect ( ( ) => {
617627 const current = terminalFontRef . current ;
@@ -1093,12 +1103,6 @@ function TerminalSessionViewport({
10931103 previousSessionRef . current = current ;
10941104 } , [ terminalOutput , terminalError , terminalStatus , terminalVersion ] ) ;
10951105
1096- const focusRequest = autoFocus && canOperateTerminal && visible ? focusRequestId : null ;
1097- useEffect ( ( ) => {
1098- if ( focusRequest === null ) return ;
1099- ( terminalRef . current ?? containerRef . current ) ?. focus ( ) ;
1100- } , [ focusRequest ] ) ;
1101-
11021106 // A hidden surface skips fitting, and showing it again fits through setVisible.
11031107 const layoutRevision = visible ? `${ drawerHeight } :${ resizeEpoch } ` : null ;
11041108 useEffect ( ( ) => {
@@ -1117,9 +1121,36 @@ function TerminalSessionViewport({
11171121 window . cancelAnimationFrame ( frame ) ;
11181122 } ;
11191123 } , [ layoutRevision ] ) ;
1124+
1125+ const attachContainer = ( mount : HTMLDivElement ) => {
1126+ containerRef . current = mount ;
1127+ const focus = ( ) =>
1128+ focusTerminalViewport ( {
1129+ mount,
1130+ surface : terminalRef . current ,
1131+ canOperate : readEnvironmentScope ( environmentId , AuthTerminalOperateScope ) ,
1132+ visible : visibleRef . current ,
1133+ } ) ;
1134+ // Once per viewport: a re-attached ref (a fresh callback identity) must not
1135+ // pull focus back on every render of a chatty terminal. As a request, it
1136+ // waits for the grant or for the viewport to show when it cannot focus yet.
1137+ if ( autoFocus && ! autoFocusedRef . current ) {
1138+ autoFocusedRef . current = true ;
1139+ focusTerminal ( threadRef , terminalId ) ;
1140+ }
1141+ const unregister = registerTerminalFocusTarget ( {
1142+ threadRef,
1143+ terminalId,
1144+ focus,
1145+ } ) ;
1146+ return ( ) => {
1147+ unregister ( ) ;
1148+ containerRef . current = null ;
1149+ } ;
1150+ } ;
11201151 return (
11211152 < div
1122- ref = { containerRef }
1153+ ref = { attachContainer }
11231154 tabIndex = { - 1 }
11241155 className = "relative h-full w-full overflow-hidden bg-(--terminal-background)"
11251156 />
@@ -1141,7 +1172,6 @@ interface ThreadTerminalDrawerProps {
11411172 activeTerminalId : string ;
11421173 terminalGroups : ThreadTerminalGroup [ ] ;
11431174 activeTerminalGroupId : string ;
1144- focusRequestId : number ;
11451175 onSplitTerminal : ( ) => void ;
11461176 onSplitTerminalVertical : ( ) => void ;
11471177 onNewTerminal : ( ) => void ;
@@ -1214,7 +1244,6 @@ function TerminalSplitPanes({
12141244 threadRef,
12151245 threadId,
12161246 onAddTerminalContext,
1217- focusRequestId,
12181247 visible,
12191248 resizeEpoch,
12201249 drawerHeight,
@@ -1231,7 +1260,6 @@ function TerminalSplitPanes({
12311260 threadRef : ScopedThreadRef ;
12321261 threadId : ThreadId ;
12331262 onAddTerminalContext : ( selection : TerminalContextSelection ) => void ;
1234- focusRequestId : number ;
12351263 visible : boolean ;
12361264 resizeEpoch : number ;
12371265 drawerHeight : number ;
@@ -1282,8 +1310,6 @@ function TerminalSplitPanes({
12821310 : { } ) }
12831311 onSessionExited = { ( ) => onCloseTerminal ( terminalId ) }
12841312 onAddTerminalContext = { onAddTerminalContext }
1285- focusRequestId = { focusRequestId }
1286- autoFocus = { terminalId === activeTerminalId }
12871313 visible = { visible }
12881314 resizeEpoch = { resizeEpoch }
12891315 drawerHeight = { drawerHeight }
@@ -1408,7 +1434,6 @@ export default function ThreadTerminalDrawer({
14081434 activeTerminalId,
14091435 terminalGroups,
14101436 activeTerminalGroupId,
1411- focusRequestId,
14121437 onSplitTerminal,
14131438 onSplitTerminalVertical,
14141439 onNewTerminal,
@@ -1881,7 +1906,6 @@ export default function ThreadTerminalDrawer({
18811906 threadRef = { threadRef }
18821907 threadId = { threadId }
18831908 onAddTerminalContext = { onAddTerminalContext }
1884- focusRequestId = { focusRequestId }
18851909 visible = { visible }
18861910 resizeEpoch = { resizeEpoch }
18871911 drawerHeight = { drawerHeight }
0 commit comments