diff --git a/src/renderer/src/components/Terminal.tsx b/src/renderer/src/components/Terminal.tsx index e2782ebf8..9215a5963 100644 --- a/src/renderer/src/components/Terminal.tsx +++ b/src/renderer/src/components/Terminal.tsx @@ -168,7 +168,11 @@ export default function Terminal(): React.JSX.Element | null { mountedWorktreeIdsRef.current.delete(id) } } - const initialTabCreationGuardRef = useRef(null) + // Why: tracks worktrees that have already been initialized (either by + // auto-creating a first tab or by having tabs on first activation). Once a + // worktree is in this set, closing all its tabs will NOT auto-spawn a + // replacement — the user explicitly chose to close them. + const initializedWorktreesRef = useRef(new Set()) // Auto-create first tab when worktree activates useEffect(() => { @@ -176,27 +180,22 @@ export default function Terminal(): React.JSX.Element | null { return } if (!activeWorktreeId) { - initialTabCreationGuardRef.current = null return } - // Why: skip auto-creation if terminal tabs already exist, or if editor files - // are open for this worktree. The user may have intentionally closed all - // terminal tabs while keeping editors open — auto-spawning a terminal would - // be disruptive. if (tabs.length > 0 || worktreeFiles.length > 0 || worktreeBrowserTabs.length > 0) { - if (initialTabCreationGuardRef.current === activeWorktreeId) { - initialTabCreationGuardRef.current = null - } + initializedWorktreesRef.current.add(activeWorktreeId) return } - // In React StrictMode (dev), mount effects are intentionally invoked twice. - // Track the worktree we already initialized so we only create one first tab. - if (initialTabCreationGuardRef.current === activeWorktreeId) { + // Why: once a worktree has been initialized (had tabs or auto-created one), + // don't auto-create again. This prevents a new terminal from spawning + // immediately after the user closes the last tab. Also guards against + // React StrictMode double-invocation. + if (initializedWorktreesRef.current.has(activeWorktreeId)) { return } - initialTabCreationGuardRef.current = activeWorktreeId + initializedWorktreesRef.current.add(activeWorktreeId) createTab(activeWorktreeId) }, [ workspaceSessionReady, diff --git a/src/renderer/src/components/terminal-pane/keyboard-handlers.ts b/src/renderer/src/components/terminal-pane/keyboard-handlers.ts index 6f82ad6b6..a788e670c 100644 --- a/src/renderer/src/components/terminal-pane/keyboard-handlers.ts +++ b/src/renderer/src/components/terminal-pane/keyboard-handlers.ts @@ -140,7 +140,7 @@ export function useTerminalKeyboardShortcuts({ if (action.type === 'sendInput') { e.preventDefault() - e.stopPropagation() + e.stopImmediatePropagation() const pane = manager.getActivePane() ?? manager.getPanes()[0] if (!pane) { return @@ -165,7 +165,7 @@ export function useTerminalKeyboardShortcuts({ return } e.preventDefault() - e.stopPropagation() + e.stopImmediatePropagation() void window.api.ui.writeClipboardText(selection).catch(() => { /* ignore clipboard write failures */ }) @@ -176,7 +176,7 @@ export function useTerminalKeyboardShortcuts({ // top-level find-in-page flow to fall back to. if (action.type === 'toggleSearch') { e.preventDefault() - e.stopPropagation() + e.stopImmediatePropagation() setSearchOpen((prev) => !prev) return } @@ -184,7 +184,7 @@ export function useTerminalKeyboardShortcuts({ // Cmd+K clears active pane screen + scrollback. if (action.type === 'clearActivePane') { e.preventDefault() - e.stopPropagation() + e.stopImmediatePropagation() const pane = manager.getActivePane() ?? manager.getPanes()[0] if (pane) { pane.terminal.clear() @@ -199,7 +199,7 @@ export function useTerminalKeyboardShortcuts({ return } e.preventDefault() - e.stopPropagation() + e.stopImmediatePropagation() // Collapse expanded pane before switching if (expandedPaneIdRef.current !== null) { @@ -228,7 +228,7 @@ export function useTerminalKeyboardShortcuts({ return } e.preventDefault() - e.stopPropagation() + e.stopImmediatePropagation() const pane = manager.getActivePane() ?? panes[0] if (!pane) { return @@ -243,7 +243,7 @@ export function useTerminalKeyboardShortcuts({ // every pane instead of just the focused one. if (action.type === 'closeActivePane') { e.preventDefault() - e.stopPropagation() + e.stopImmediatePropagation() const pane = manager.getActivePane() ?? manager.getPanes()[0] if (!pane) { return @@ -257,7 +257,7 @@ export function useTerminalKeyboardShortcuts({ // (matches Ghostty behavior). if (action.type === 'splitActivePane') { e.preventDefault() - e.stopPropagation() + e.stopImmediatePropagation() if (expandedPaneIdRef.current !== null) { setExpandedPane(null) restoreExpandedLayout()