diff --git a/src/renderer/src/components/Terminal.tsx b/src/renderer/src/components/Terminal.tsx
index b29c0ec2c..7e7b87261 100644
--- a/src/renderer/src/components/Terminal.tsx
+++ b/src/renderer/src/components/Terminal.tsx
@@ -1,5 +1,5 @@
/* eslint-disable max-lines */
-import { useEffect, useCallback, useRef, useState } from 'react'
+import { useEffect, useCallback, useRef, useState, lazy, Suspense } from 'react'
import { useAppStore } from '../store'
import {
Dialog,
@@ -10,6 +10,7 @@ import {
DialogTitle
} from '@/components/ui/dialog'
import { Button } from '@/components/ui/button'
+import TerminalPane from './terminal-pane/TerminalPane'
import {
ORCA_EDITOR_SAVE_AND_CLOSE_EVENT,
requestEditorSaveQuiesce
@@ -20,6 +21,8 @@ import { destroyPersistentWebview } from './browser-pane/BrowserPane'
import { reconcileTabOrder } from './tab-bar/reconcile-order'
import TabGroupSplitLayout from './tab-group/TabGroupSplitLayout'
+const EditorPanel = lazy(() => import('./editor/EditorPanel'))
+
export default function Terminal(): React.JSX.Element | null {
const activeWorktreeId = useAppStore((s) => s.activeWorktreeId)
const activeView = useAppStore((s) => s.activeView)
@@ -27,8 +30,10 @@ export default function Terminal(): React.JSX.Element | null {
const tabsByWorktree = useAppStore((s) => s.tabsByWorktree)
const activeTabId = useAppStore((s) => s.activeTabId)
const createTab = useAppStore((s) => s.createTab)
+ const closeTab = useAppStore((s) => s.closeTab)
const setActiveTab = useAppStore((s) => s.setActiveTab)
const setActiveWorktree = useAppStore((s) => s.setActiveWorktree)
+ const consumeSuppressedPtyExit = useAppStore((s) => s.consumeSuppressedPtyExit)
const workspaceSessionReady = useAppStore((s) => s.workspaceSessionReady)
const openFiles = useAppStore((s) => s.openFiles)
const activeBrowserTabId = useAppStore((s) => s.activeBrowserTabId)
@@ -65,6 +70,20 @@ export default function Terminal(): React.JSX.Element | null {
const worktreeBrowserTabs = activeWorktreeId
? (browserTabsByWorktree[activeWorktreeId] ?? [])
: []
+ const activeGroups = activeWorktreeId ? (groupsByWorktree[activeWorktreeId] ?? []) : []
+ const activeLayout = activeWorktreeId ? layoutByWorktree[activeWorktreeId] : undefined
+ const effectiveActiveLayout =
+ activeLayout ??
+ (activeWorktreeId
+ ? (() => {
+ const fallbackGroupId =
+ activeGroupIdByWorktree[activeWorktreeId] ?? activeGroups[0]?.id ?? null
+ if (!fallbackGroupId) {
+ return undefined
+ }
+ return { type: 'leaf', groupId: fallbackGroupId } as const
+ })()
+ : undefined)
const activeWorktreeBrowserTabIdsKey = activeWorktreeId
? (browserTabsByWorktree[activeWorktreeId] ?? []).map((tab) => tab.id).join(',')
: ''
@@ -227,6 +246,62 @@ export default function Terminal(): React.JSX.Element | null {
createBrowserTab(activeWorktreeId, 'about:blank', { title: 'New Browser Tab' })
}, [activeWorktreeId, createBrowserTab])
+ const handleCloseTab = useCallback(
+ (tabId: string) => {
+ const state = useAppStore.getState()
+ const owningWorktreeEntry = Object.entries(state.tabsByWorktree).find(([, worktreeTabs]) =>
+ worktreeTabs.some((tab) => tab.id === tabId)
+ )
+ const owningWorktreeId = owningWorktreeEntry?.[0] ?? null
+
+ if (!owningWorktreeId) {
+ return
+ }
+
+ const currentTabs = state.tabsByWorktree[owningWorktreeId] ?? []
+ if (currentTabs.length <= 1) {
+ closeTab(tabId)
+ if (state.activeWorktreeId === owningWorktreeId) {
+ // Why: only deactivate the worktree when no tabs of any kind remain.
+ // Editor files are a separate tab type; closing the last terminal tab
+ // should switch to the editor view instead of tearing down the workspace.
+ const worktreeFile = state.openFiles.find((f) => f.worktreeId === owningWorktreeId)
+ if (worktreeFile) {
+ setActiveFile(worktreeFile.id)
+ setActiveTabType('editor')
+ } else {
+ const browserTab = (state.browserTabsByWorktree[owningWorktreeId] ?? [])[0]
+ if (browserTab) {
+ setActiveBrowserTab(browserTab.id)
+ setActiveTabType('browser')
+ } else {
+ setActiveWorktree(null)
+ }
+ }
+ }
+ return
+ }
+
+ // If closing the active tab in the active worktree, switch to a neighbor.
+ if (state.activeWorktreeId === owningWorktreeId && tabId === state.activeTabId) {
+ const idx = currentTabs.findIndex((t) => t.id === tabId)
+ const nextTab = currentTabs[idx + 1] ?? currentTabs[idx - 1]
+ if (nextTab) {
+ setActiveTab(nextTab.id)
+ }
+ }
+ closeTab(tabId)
+ },
+ [
+ closeTab,
+ setActiveBrowserTab,
+ setActiveTab,
+ setActiveFile,
+ setActiveTabType,
+ setActiveWorktree
+ ]
+ )
+
const handleCloseBrowserTab = useCallback(
(tabId: string) => {
const state = useAppStore.getState()
@@ -278,6 +353,16 @@ export default function Terminal(): React.JSX.Element | null {
]
)
+ const handlePtyExit = useCallback(
+ (tabId: string, ptyId: string) => {
+ if (consumeSuppressedPtyExit(ptyId)) {
+ return
+ }
+ handleCloseTab(tabId)
+ },
+ [consumeSuppressedPtyExit, handleCloseTab]
+ )
+
// Keyboard shortcuts
useEffect(() => {
if (!activeWorktreeId) {
@@ -387,6 +472,7 @@ export default function Terminal(): React.JSX.Element | null {
activeWorktreeId,
handleNewBrowserTab,
handleNewTab,
+ handleCloseTab,
handleCloseBrowserTab,
handleCloseFile,
setActiveTab
@@ -508,47 +594,73 @@ export default function Terminal(): React.JSX.Element | null {
>