@@ -33,6 +33,7 @@ import {
3333 useCallback ,
3434 useEffect ,
3535 useEffectEvent ,
36+ useLayoutEffect ,
3637 useMemo ,
3738 useRef ,
3839 useState ,
@@ -295,6 +296,7 @@ interface TerminalViewportProps {
295296 onAddTerminalContext : ( selection : TerminalContextSelection ) => void ;
296297 focusRequestId : number ;
297298 autoFocus : boolean ;
299+ visible : boolean ;
298300 resizeEpoch : number ;
299301 drawerHeight : number ;
300302 keybindings : ResolvedKeybindingsConfig ;
@@ -319,12 +321,14 @@ export function TerminalViewport({
319321 onAddTerminalContext,
320322 focusRequestId,
321323 autoFocus,
324+ visible,
322325 resizeEpoch,
323326 drawerHeight,
324327 keybindings,
325328} : TerminalViewportProps ) {
326329 const containerRef = useRef < HTMLDivElement > ( null ) ;
327330 const terminalRef = useRef < GhosttyTerminalSurface | null > ( null ) ;
331+ const visibleRef = useRef ( visible ) ;
328332 const environmentId = threadRef . environmentId ;
329333 const serverConfig = useAtomValue ( serverEnvironment . configValueAtom ( environmentId ) ) ;
330334 const openInPreferredEditor = useOpenInPreferredEditor (
@@ -434,6 +438,11 @@ export function TerminalViewport({
434438 keybindingsRef . current = keybindings ;
435439 } , [ keybindings ] ) ;
436440
441+ useLayoutEffect ( ( ) => {
442+ visibleRef . current = visible ;
443+ terminalRef . current ?. setVisible ( visible ) ;
444+ } , [ visible ] ) ;
445+
437446 useEffect ( ( ) => {
438447 const current = terminalFontRef . current ;
439448 if ( current . family === terminalFontFamily && current . size === terminalFontSize ) return ;
@@ -457,6 +466,9 @@ export function TerminalViewport({
457466 const terminalOptions : GhosttyTerminalSurfaceOptions = {
458467 theme : terminalThemeFromApp ( mount ) ,
459468 font : terminalFontOptions ( setupFont . family , setupFont . size ) ,
469+ get visible ( ) {
470+ return visibleRef . current ;
471+ } ,
460472 onData : ( data ) => handleData ( data ) ,
461473 onResize : ( cols , rows ) => void resizeTerminal ( cols , rows ) ,
462474 onSelectionChange : ( ) => handleSelectionChange ( ) ,
@@ -474,6 +486,7 @@ export function TerminalViewport({
474486 terminal . dispose ( ) ;
475487 return null ;
476488 }
489+ terminal . setVisible ( visibleRef . current ) ;
477490 // The theme observer is not installed yet, so re-read the theme in case
478491 // the app toggled light/dark while the WASM surface was loading.
479492 terminal . setTheme ( terminalThemeFromApp ( mount ) ) ;
@@ -503,7 +516,7 @@ export function TerminalViewport({
503516 // never started, so only "exited" triggers the message — as with xterm.)
504517 synchronizedStatusRef . current = "closed" ;
505518 synchronizeTerminalStatus ( terminal , latestSession . status ) ;
506- if ( autoFocus ) window . requestAnimationFrame ( ( ) => terminal . focus ( ) ) ;
519+ if ( autoFocus && visibleRef . current ) window . requestAnimationFrame ( ( ) => terminal . focus ( ) ) ;
507520
508521 const dismissSelectionAction = ( supersede = false ) => {
509522 const ownsMenu =
@@ -891,7 +904,7 @@ export function TerminalViewport({
891904 writeSystemMessage ( terminal , current . error ) ;
892905 }
893906
894- if ( previous . version === 0 && autoFocus ) {
907+ if ( previous . version === 0 && autoFocus && visibleRef . current ) {
895908 window . requestAnimationFrame ( ( ) => {
896909 terminal . focus ( ) ;
897910 } ) ;
@@ -900,7 +913,7 @@ export function TerminalViewport({
900913 } , [ autoFocus , terminalOutput , terminalError , terminalStatus , terminalVersion ] ) ;
901914
902915 useEffect ( ( ) => {
903- if ( ! autoFocus ) return ;
916+ if ( ! autoFocus || ! visible ) return ;
904917 const terminal = terminalRef . current ;
905918 if ( ! terminal ) return ;
906919 const frame = window . requestAnimationFrame ( ( ) => {
@@ -909,15 +922,16 @@ export function TerminalViewport({
909922 return ( ) => {
910923 window . cancelAnimationFrame ( frame ) ;
911924 } ;
912- } , [ autoFocus , focusRequestId ] ) ;
925+ } , [ autoFocus , focusRequestId , visible ] ) ;
913926
914927 useEffect ( ( ) => {
915928 const terminal = terminalRef . current ;
916- if ( ! terminal ) return ;
929+ if ( ! terminal || ! visibleRef . current ) return ;
917930 const wasAtBottom = terminal . isAtBottom ( ) ;
918931 // The surface reports grid changes through onResize, which is the single
919932 // channel for PTY resize RPCs; fitting here only refreshes the layout.
920933 const frame = window . requestAnimationFrame ( ( ) => {
934+ if ( ! visibleRef . current ) return ;
921935 terminal . fit ( ) ;
922936 if ( wasAtBottom ) {
923937 terminal . scrollToBottom ( ) ;
@@ -1493,6 +1507,7 @@ export default function ThreadTerminalDrawer({
14931507 onAddTerminalContext = { onAddTerminalContext }
14941508 focusRequestId = { focusRequestId }
14951509 autoFocus = { terminalId === resolvedActiveTerminalId }
1510+ visible = { visible }
14961511 resizeEpoch = { resizeEpoch }
14971512 drawerHeight = { drawerHeight }
14981513 keybindings = { keybindings }
@@ -1522,6 +1537,7 @@ export default function ThreadTerminalDrawer({
15221537 onAddTerminalContext = { onAddTerminalContext }
15231538 focusRequestId = { focusRequestId }
15241539 autoFocus
1540+ visible = { visible }
15251541 resizeEpoch = { resizeEpoch }
15261542 drawerHeight = { drawerHeight }
15271543 keybindings = { keybindings }
0 commit comments