fix: preserve terminal panes across split groups (#1837)
This commit is contained in:
@@ -35,6 +35,7 @@ import type { TabGroupLayoutNode } from '../../../shared/types'
|
||||
import BrowserPane from './browser-pane/BrowserPane'
|
||||
import { destroyPersistentWebview } from './browser-pane/webview-registry'
|
||||
import BrowserPaneOverlayLayer from './browser-pane/BrowserPaneOverlayLayer'
|
||||
import TerminalPaneOverlayLayer from './terminal-pane/TerminalPaneOverlayLayer'
|
||||
import {
|
||||
collectBrowserWebviewIds,
|
||||
destroyWorkspaceWebviews
|
||||
@@ -1237,6 +1238,7 @@ function Terminal(): React.JSX.Element | null {
|
||||
<WorktreeSplitSurface
|
||||
key={`tab-groups-${worktree.id}`}
|
||||
worktreeId={worktree.id}
|
||||
worktreePath={worktree.path}
|
||||
layout={layout}
|
||||
focusedGroupId={activeGroupIdByWorktree[worktree.id]}
|
||||
isVisible={isVisible}
|
||||
@@ -1250,8 +1252,9 @@ function Terminal(): React.JSX.Element | null {
|
||||
{!effectiveActiveLayout && !anyMountedWorktreeHasLayout && (
|
||||
<>
|
||||
{/* Why: split-group layouts render their own terminal/browser/editor
|
||||
surfaces inside TabGroupPanel. Keeping the legacy workspace-level
|
||||
panes mounted underneath as hidden DOM creates duplicate
|
||||
surfaces through TabGroupPanel plus stable overlay layers.
|
||||
Keeping the legacy workspace-level panes mounted underneath
|
||||
as hidden DOM creates duplicate
|
||||
TerminalPane/BrowserPane instances for the same tab, which lets
|
||||
two React trees race over one PTY or webview. Render only one
|
||||
surface model at a time.
|
||||
@@ -1469,13 +1472,11 @@ function Terminal(): React.JSX.Element | null {
|
||||
}
|
||||
|
||||
// Why: each TabGroupPanel tags its body element with an `anchor-name`, and
|
||||
// a single worktree-level BrowserPaneOverlayLayer renders every browser tab
|
||||
// for this worktree once — keyed by browserTab.id only — and pins each pane
|
||||
// to the owning group's anchor via CSS `position-anchor`. Moving a tab
|
||||
// between groups now only changes which anchor-name the overlay references,
|
||||
// so the `<webview>` is never reparented (and never reloads). Mirrors
|
||||
// VS Code's OverlayWebview claim/release pattern, with the browser doing all
|
||||
// layout tracking for free.
|
||||
// worktree-level overlay layers render every terminal/browser tab once —
|
||||
// keyed by pane id only — then pin each pane to the owning group's anchor via
|
||||
// CSS `position-anchor`. Moving a tab between groups now only changes which
|
||||
// anchor-name the overlay references, so terminals do not remount and
|
||||
// webviews do not reparent/reload.
|
||||
//
|
||||
// Why `React.memo`: Terminal.tsx has many store subscriptions and re-renders
|
||||
// on unrelated updates (terminal keystrokes, editor edits, focus changes).
|
||||
@@ -1485,12 +1486,14 @@ function Terminal(): React.JSX.Element | null {
|
||||
// focusedGroupId / isVisible) actually change.
|
||||
const WorktreeSplitSurface = React.memo(function WorktreeSplitSurface({
|
||||
worktreeId,
|
||||
worktreePath,
|
||||
layout,
|
||||
focusedGroupId,
|
||||
isVisible,
|
||||
activityTerminalPortals
|
||||
}: {
|
||||
worktreeId: string
|
||||
worktreePath: string
|
||||
layout: TabGroupLayoutNode
|
||||
focusedGroupId?: string
|
||||
isVisible: boolean
|
||||
@@ -1507,6 +1510,11 @@ const WorktreeSplitSurface = React.memo(function WorktreeSplitSurface({
|
||||
worktreeId={worktreeId}
|
||||
focusedGroupId={focusedGroupId}
|
||||
isWorktreeActive={isVisible}
|
||||
/>
|
||||
<TerminalPaneOverlayLayer
|
||||
worktreeId={worktreeId}
|
||||
worktreePath={worktreePath}
|
||||
isWorktreeActive={isVisible}
|
||||
activityTerminalPortals={activityTerminalPortals}
|
||||
/>
|
||||
<BrowserPaneOverlayLayer worktreeId={worktreeId} isWorktreeActive={isVisible} />
|
||||
|
||||
@@ -3,7 +3,7 @@ import { useShallow } from 'zustand/react/shallow'
|
||||
import { useAppStore } from '../../store'
|
||||
import type { BrowserTab as BrowserTabState, Tab, TabGroup } from '../../../../shared/types'
|
||||
import BrowserPane from './BrowserPane'
|
||||
import { browserSlotAnchorName } from './browser-pane-slots'
|
||||
import { tabGroupBodyAnchorName } from '../tab-group/tab-group-body-anchor'
|
||||
|
||||
// Why: Electron `<webview>` destroys its guest contents whenever its DOM
|
||||
// parent changes. Rendering one BrowserPane per tab at the worktree level
|
||||
@@ -51,7 +51,7 @@ const BrowserOverlaySlot = memo(function BrowserOverlaySlot({
|
||||
isActive,
|
||||
onFocusOwningGroup
|
||||
}: BrowserOverlaySlotProps): React.JSX.Element {
|
||||
const anchorName = groupId !== undefined ? browserSlotAnchorName(groupId) : undefined
|
||||
const anchorName = groupId !== undefined ? tabGroupBodyAnchorName(groupId) : undefined
|
||||
// Why: each overlay pins itself to the owning TabGroupPanel's body via CSS
|
||||
// anchor positioning. `anchor()` resolves top/left relative to the viewport,
|
||||
// and the overlay's own `position: absolute` inside a positioned ancestor
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
// Why: Electron `<webview>` guest contents are destroyed whenever the host
|
||||
// element is reparented in the DOM. So browser panes cannot live inside
|
||||
// TabGroupPanel (which would unmount/remount them when a tab moves between
|
||||
// groups). Instead, a single worktree-level BrowserPaneOverlayLayer renders
|
||||
// one stable `<BrowserPane>` per browser tab and positions it over the
|
||||
// owning group's body via CSS anchor positioning. This module provides the
|
||||
// anchor-name bridge between the two:
|
||||
//
|
||||
// TabGroupPanel body → `anchor-name: --orca-browser-slot-<groupId>`
|
||||
// BrowserPaneOverlayLayer overlay → `position-anchor: --orca-browser-slot-<groupId>`
|
||||
// `top: anchor(--… top); left: anchor(--… left);`
|
||||
// `width: anchor-size(--… width); height: anchor-size(--… height);`
|
||||
//
|
||||
// The browser does all layout tracking for free — no ResizeObserver, no
|
||||
// rect state, no subscribe/notify machinery. Moving a tab between groups
|
||||
// only changes which anchor-name the overlay references, so the `<webview>`
|
||||
// is never reparented (and never reloads). Mirrors VS Code's
|
||||
// `OverlayWebview` claim/release pattern, with CSS doing the positioning.
|
||||
|
||||
const ANCHOR_PREFIX = '--orca-browser-slot-'
|
||||
|
||||
/**
|
||||
* Returns the CSS anchor name for a given tab-group id. Anchor names must be
|
||||
* `<dashed-ident>`; groupIds are UUIDs (hex + `-`) so they are already safe
|
||||
* as suffixes. Prefixed so they cannot collide with unrelated anchors.
|
||||
*/
|
||||
export function browserSlotAnchorName(groupId: string): string {
|
||||
return `${ANCHOR_PREFIX}${groupId}`
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { lazy, Suspense, useEffect, useMemo, useState } from 'react'
|
||||
import { createPortal } from 'react-dom'
|
||||
import { useDroppable } from '@dnd-kit/core'
|
||||
import { Columns2, Ellipsis, Rows2, X } from 'lucide-react'
|
||||
import { useAppStore } from '../../store'
|
||||
@@ -11,8 +10,6 @@ import {
|
||||
DropdownMenuTrigger
|
||||
} from '@/components/ui/dropdown-menu'
|
||||
import TabBar from '../tab-bar/TabBar'
|
||||
import TerminalPane from '../terminal-pane/TerminalPane'
|
||||
import { browserSlotAnchorName } from '../browser-pane/browser-pane-slots'
|
||||
import { useTabGroupWorkspaceModel } from './useTabGroupWorkspaceModel'
|
||||
import TabGroupDropOverlay from './TabGroupDropOverlay'
|
||||
import { resolveGroupTabFromVisibleId } from './tab-group-visible-id'
|
||||
@@ -21,17 +18,13 @@ import {
|
||||
type HoveredTabInsertion,
|
||||
type TabDropZone
|
||||
} from './useTabDragSplit'
|
||||
import {
|
||||
findActivityTerminalPortal,
|
||||
type ActivityTerminalPortalTarget
|
||||
} from '../activity/activity-terminal-portal'
|
||||
import { tabGroupBodyAnchorName } from './tab-group-body-anchor'
|
||||
|
||||
const EditorPanel = lazy(() => import('../editor/EditorPanel'))
|
||||
|
||||
export default function TabGroupPanel({
|
||||
groupId,
|
||||
worktreeId,
|
||||
isWorktreeActive,
|
||||
isFocused,
|
||||
hasSplitGroups,
|
||||
touchesRightEdge,
|
||||
@@ -40,12 +33,10 @@ export default function TabGroupPanel({
|
||||
reserveCollapsedSidebarHeaderSpace,
|
||||
isTabDragActive = false,
|
||||
activeDropZone = null,
|
||||
hoveredTabInsertion = null,
|
||||
activityTerminalPortals = []
|
||||
hoveredTabInsertion = null
|
||||
}: {
|
||||
groupId: string
|
||||
worktreeId: string
|
||||
isWorktreeActive: boolean
|
||||
isFocused: boolean
|
||||
hasSplitGroups: boolean
|
||||
touchesRightEdge: boolean
|
||||
@@ -55,7 +46,6 @@ export default function TabGroupPanel({
|
||||
isTabDragActive?: boolean
|
||||
activeDropZone?: TabDropZone | null
|
||||
hoveredTabInsertion?: HoveredTabInsertion | null
|
||||
activityTerminalPortals?: ActivityTerminalPortalTarget[]
|
||||
}): React.JSX.Element {
|
||||
const rightSidebarOpen = useAppStore((state) => state.rightSidebarOpen)
|
||||
const sidebarOpen = useAppStore((state) => state.sidebarOpen)
|
||||
@@ -66,16 +56,7 @@ export default function TabGroupPanel({
|
||||
}, [])
|
||||
|
||||
const model = useTabGroupWorkspaceModel({ groupId, worktreeId })
|
||||
const {
|
||||
activeTab,
|
||||
browserItems,
|
||||
commands,
|
||||
editorItems,
|
||||
runtimeTerminalTabById,
|
||||
tabBarOrder,
|
||||
terminalTabs,
|
||||
worktreePath
|
||||
} = model
|
||||
const { activeTab, browserItems, commands, editorItems, tabBarOrder, terminalTabs } = model
|
||||
const { setNodeRef: setBodyDropRef } = useDroppable({
|
||||
id: getTabPaneBodyDroppableId(groupId),
|
||||
data: {
|
||||
@@ -85,13 +66,14 @@ export default function TabGroupPanel({
|
||||
},
|
||||
disabled: !isTabDragActive
|
||||
})
|
||||
// Why: browser panes for this worktree are rendered once at the worktree
|
||||
// Why: browser and terminal panes for this worktree are rendered once at the worktree
|
||||
// level (BrowserPaneOverlayLayer) and positioned over the owning group's
|
||||
// body via CSS anchor positioning. Tagging this body with a per-group
|
||||
// `anchor-name` lets the overlay reference it via `position-anchor`;
|
||||
// moving a tab between groups only swaps which anchor-name the overlay
|
||||
// targets, never reparenting the `<webview>` (which would reload it).
|
||||
const bodyAnchorName = browserSlotAnchorName(groupId)
|
||||
// targets. Browsers avoid `<webview>` reloads; terminals avoid remounting
|
||||
// xterm and losing alt-screen TUI state.
|
||||
const bodyAnchorName = tabGroupBodyAnchorName(groupId)
|
||||
// Why: memoize the style object so the literal isn't recreated on every
|
||||
// render. A fresh object every render would make the body `<div>` appear
|
||||
// to have a new `style` prop on every parent re-render, which defeats any
|
||||
@@ -356,54 +338,6 @@ export default function TabGroupPanel({
|
||||
style={bodyAnchorStyle}
|
||||
>
|
||||
{activeDropZone ? <TabGroupDropOverlay zone={activeDropZone} /> : null}
|
||||
{model.groupTabs
|
||||
.filter((item) => item.contentType === 'terminal')
|
||||
.map((item) => {
|
||||
const activityTerminalPortal = findActivityTerminalPortal(
|
||||
activityTerminalPortals,
|
||||
worktreeId,
|
||||
item.entityId
|
||||
)
|
||||
const isActivityPortalTab = activityTerminalPortal !== null
|
||||
const isActiveTerminalTab =
|
||||
isFocused && activeTab?.id === item.id && activeTab.contentType === 'terminal'
|
||||
const isVisibleTerminalTab =
|
||||
isWorktreeActive && activeTab?.id === item.id && activeTab.contentType === 'terminal'
|
||||
const terminalPane = (
|
||||
<TerminalPane
|
||||
key={`${item.entityId}-${runtimeTerminalTabById.get(item.entityId)?.generation ?? 0}`}
|
||||
tabId={item.entityId}
|
||||
worktreeId={worktreeId}
|
||||
cwd={worktreePath}
|
||||
isActive={isActiveTerminalTab || activityTerminalPortal?.active === true}
|
||||
// Why: the Activity page shows the selected agent's existing
|
||||
// terminal while the workspace group stays hidden. The portaled
|
||||
// tab must still be visible so xterm fits against the activity
|
||||
// pane and continues foreground output scheduling.
|
||||
isVisible={isVisibleTerminalTab || isActivityPortalTab}
|
||||
// Why: when portaled to Activity for a specific agent pane,
|
||||
// isolate that leaf so split siblings stay hidden. Workspace
|
||||
// renders pass null → no override.
|
||||
isolatedPaneId={activityTerminalPortal?.paneId ?? null}
|
||||
onPtyExit={(ptyId) => {
|
||||
if (commands.consumeSuppressedPtyExit(ptyId)) {
|
||||
return
|
||||
}
|
||||
commands.closeItem(item.id)
|
||||
}}
|
||||
onCloseTab={() => commands.closeItem(item.id)}
|
||||
/>
|
||||
)
|
||||
if (activityTerminalPortal) {
|
||||
return createPortal(
|
||||
terminalPane,
|
||||
activityTerminalPortal.target,
|
||||
`activity-terminal-${item.entityId}`
|
||||
)
|
||||
}
|
||||
return terminalPane
|
||||
})}
|
||||
|
||||
{activeTab &&
|
||||
activeTab.contentType !== 'terminal' &&
|
||||
activeTab.contentType !== 'browser' && (
|
||||
@@ -426,12 +360,11 @@ export default function TabGroupPanel({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Why: browser panes are rendered at the worktree level by
|
||||
BrowserPaneOverlayLayer and absolutely positioned over this body
|
||||
element via the slot registered above. Rendering them per-group
|
||||
here caused moving a browser tab between groups to unmount and
|
||||
remount the pane, reparenting the Electron `<webview>` — which
|
||||
destroys its guest contents and reloads the page. */}
|
||||
{/* Why: terminal/browser panes are rendered at the worktree level by
|
||||
overlay layers and absolutely positioned over this body element
|
||||
via the slot registered above. Rendering them per-group caused
|
||||
split moves to remount xterm or reparent Electron `<webview>`,
|
||||
losing TUI state or reloading the page. */}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -49,7 +49,6 @@ describe('TabGroupSplitLayout', () => {
|
||||
return tabGroupPanelElement.props as {
|
||||
groupId: string
|
||||
worktreeId: string
|
||||
isWorktreeActive: boolean
|
||||
isFocused: boolean
|
||||
hasSplitGroups: boolean
|
||||
reserveClosedExplorerToggleSpace: boolean
|
||||
@@ -62,7 +61,6 @@ describe('TabGroupSplitLayout', () => {
|
||||
expect.objectContaining({
|
||||
groupId: 'group-1',
|
||||
worktreeId: 'wt-1',
|
||||
isWorktreeActive: false,
|
||||
isFocused: false,
|
||||
hasSplitGroups: false,
|
||||
reserveClosedExplorerToggleSpace: true,
|
||||
@@ -76,7 +74,6 @@ describe('TabGroupSplitLayout', () => {
|
||||
expect.objectContaining({
|
||||
groupId: 'group-1',
|
||||
worktreeId: 'wt-1',
|
||||
isWorktreeActive: true,
|
||||
isFocused: true,
|
||||
hasSplitGroups: false,
|
||||
reserveClosedExplorerToggleSpace: true,
|
||||
|
||||
@@ -5,7 +5,6 @@ import { useAppStore } from '../../store'
|
||||
import TabGroupPanel from './TabGroupPanel'
|
||||
import TabDragPreview from '../tab-bar/TabDragPreview'
|
||||
import { type HoveredTabInsertion, type TabDropZone, useTabDragSplit } from './useTabDragSplit'
|
||||
import type { ActivityTerminalPortalTarget } from '../activity/activity-terminal-portal'
|
||||
|
||||
const MIN_RATIO = 0.15
|
||||
const MAX_RATIO = 0.85
|
||||
@@ -96,8 +95,7 @@ function SplitNode({
|
||||
isTabDragActive,
|
||||
activeDropGroupId,
|
||||
activeDropZone,
|
||||
hoveredTabInsertion,
|
||||
activityTerminalPortals
|
||||
hoveredTabInsertion
|
||||
}: {
|
||||
node: TabGroupLayoutNode
|
||||
nodePath: string
|
||||
@@ -112,7 +110,6 @@ function SplitNode({
|
||||
activeDropGroupId: string | null
|
||||
activeDropZone: TabDropZone | null
|
||||
hoveredTabInsertion: HoveredTabInsertion | null
|
||||
activityTerminalPortals: ActivityTerminalPortalTarget[]
|
||||
}): React.JSX.Element {
|
||||
const setTabGroupSplitRatio = useAppStore((state) => state.setTabGroupSplitRatio)
|
||||
|
||||
@@ -121,7 +118,6 @@ function SplitNode({
|
||||
<TabGroupPanel
|
||||
groupId={node.groupId}
|
||||
worktreeId={worktreeId}
|
||||
isWorktreeActive={isWorktreeActive}
|
||||
// Why: hidden worktrees stay mounted so their PTYs and split layouts
|
||||
// survive worktree switches, but only the visible worktree may own the
|
||||
// global terminal shortcuts. If an offscreen group's pane stays
|
||||
@@ -137,7 +133,6 @@ function SplitNode({
|
||||
hoveredTabInsertion={
|
||||
hoveredTabInsertion?.groupId === node.groupId ? hoveredTabInsertion : null
|
||||
}
|
||||
activityTerminalPortals={activityTerminalPortals}
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -165,7 +160,6 @@ function SplitNode({
|
||||
activeDropGroupId={activeDropGroupId}
|
||||
activeDropZone={activeDropZone}
|
||||
hoveredTabInsertion={hoveredTabInsertion}
|
||||
activityTerminalPortals={activityTerminalPortals}
|
||||
/>
|
||||
</div>
|
||||
<ResizeHandle
|
||||
@@ -187,7 +181,6 @@ function SplitNode({
|
||||
activeDropGroupId={activeDropGroupId}
|
||||
activeDropZone={activeDropZone}
|
||||
hoveredTabInsertion={hoveredTabInsertion}
|
||||
activityTerminalPortals={activityTerminalPortals}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
@@ -198,14 +191,12 @@ export default function TabGroupSplitLayout({
|
||||
layout,
|
||||
worktreeId,
|
||||
focusedGroupId,
|
||||
isWorktreeActive,
|
||||
activityTerminalPortals = []
|
||||
isWorktreeActive
|
||||
}: {
|
||||
layout: TabGroupLayoutNode
|
||||
worktreeId: string
|
||||
focusedGroupId?: string
|
||||
isWorktreeActive: boolean
|
||||
activityTerminalPortals?: ActivityTerminalPortalTarget[]
|
||||
}): React.JSX.Element {
|
||||
const dragSplit = useTabDragSplit({ worktreeId, enabled: isWorktreeActive })
|
||||
const hasSplits = layout.type === 'split'
|
||||
@@ -262,7 +253,6 @@ export default function TabGroupSplitLayout({
|
||||
activeDropGroupId={dragSplit.hoveredDropTarget?.groupId ?? null}
|
||||
activeDropZone={dragSplit.hoveredDropTarget?.zone ?? null}
|
||||
hoveredTabInsertion={dragSplit.hoveredTabInsertion}
|
||||
activityTerminalPortals={activityTerminalPortals}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// Why: browser and terminal panes are mounted once at the worktree level and
|
||||
// positioned over their owning TabGroupPanel body. A stable per-group anchor
|
||||
// lets those overlays follow split-group layout changes without reparenting
|
||||
// heavyweight pane DOM.
|
||||
|
||||
const ANCHOR_PREFIX = '--orca-tab-group-body-'
|
||||
|
||||
/**
|
||||
* Returns the CSS anchor name for a given tab-group id. Anchor names must be
|
||||
* `<dashed-ident>`; groupIds are UUIDs (hex + `-`) so they are already safe
|
||||
* as suffixes. Prefixed so they cannot collide with unrelated anchors.
|
||||
*/
|
||||
export function tabGroupBodyAnchorName(groupId: string): string {
|
||||
return `${ANCHOR_PREFIX}${groupId}`
|
||||
}
|
||||
@@ -5,12 +5,7 @@ import { useCallback, useMemo } from 'react'
|
||||
import { toast } from 'sonner'
|
||||
import { useShallow } from 'zustand/react/shallow'
|
||||
import type { OpenFile } from '@/store/slices/editor'
|
||||
import type {
|
||||
BrowserTab as BrowserTabState,
|
||||
Tab,
|
||||
TabGroup,
|
||||
TerminalTab
|
||||
} from '../../../../shared/types'
|
||||
import type { BrowserTab as BrowserTabState, Tab, TabGroup } from '../../../../shared/types'
|
||||
import { useAppStore } from '../../store'
|
||||
import { useAllWorktrees } from '../../store/selectors'
|
||||
import { createUntitledMarkdownFile } from '../../lib/create-untitled-markdown'
|
||||
@@ -26,7 +21,6 @@ export type GroupBrowserItem = BrowserTabState & { tabId: string }
|
||||
const EMPTY_GROUPS: readonly TabGroup[] = []
|
||||
const EMPTY_UNIFIED_TABS: readonly Tab[] = []
|
||||
const EMPTY_BROWSER_TABS: readonly BrowserTabState[] = []
|
||||
const EMPTY_RUNTIME_TERMINAL_TABS: readonly TerminalTab[] = []
|
||||
|
||||
type TerminalTabItem = {
|
||||
id: string
|
||||
@@ -59,7 +53,6 @@ export function useTabGroupWorkspaceModel({
|
||||
unifiedTabs: state.unifiedTabsByWorktree[worktreeId] ?? EMPTY_UNIFIED_TABS,
|
||||
openFiles: state.openFiles,
|
||||
browserTabs: state.browserTabsByWorktree[worktreeId] ?? EMPTY_BROWSER_TABS,
|
||||
runtimeTerminalTabs: state.tabsByWorktree[worktreeId] ?? EMPTY_RUNTIME_TERMINAL_TABS,
|
||||
expandedPaneByTabId: state.expandedPaneByTabId
|
||||
}))
|
||||
)
|
||||
@@ -83,7 +76,6 @@ export function useTabGroupWorkspaceModel({
|
||||
const createEmptySplitGroup = useAppStore((state) => state.createEmptySplitGroup)
|
||||
const setTabCustomTitle = useAppStore((state) => state.setTabCustomTitle)
|
||||
const setTabColor = useAppStore((state) => state.setTabColor)
|
||||
const consumeSuppressedPtyExit = useAppStore((state) => state.consumeSuppressedPtyExit)
|
||||
const openFile = useAppStore((state) => state.openFile)
|
||||
|
||||
const group = useMemo(
|
||||
@@ -148,11 +140,6 @@ export function useTabGroupWorkspaceModel({
|
||||
[groupTabs, worktreeState.browserTabs]
|
||||
)
|
||||
|
||||
const runtimeTerminalTabById = useMemo(
|
||||
() => new Map(worktreeState.runtimeTerminalTabs.map((tab) => [tab.id, tab])),
|
||||
[worktreeState.runtimeTerminalTabs]
|
||||
)
|
||||
|
||||
const closeEditorIfUnreferenced = useCallback(
|
||||
(entityId: string, closingTabId: string) => {
|
||||
const otherReference = (useAppStore.getState().unifiedTabsByWorktree[worktreeId] ?? []).some(
|
||||
@@ -437,8 +424,6 @@ export function useTabGroupWorkspaceModel({
|
||||
terminalTabs,
|
||||
tabBarOrder,
|
||||
groupTabs,
|
||||
worktreePath: worktree?.path,
|
||||
runtimeTerminalTabById,
|
||||
expandedPaneByTabId: worktreeState.expandedPaneByTabId,
|
||||
commands: {
|
||||
focusGroup: () => {
|
||||
@@ -452,7 +437,6 @@ export function useTabGroupWorkspaceModel({
|
||||
closeItem,
|
||||
closeOthers,
|
||||
closeToRight,
|
||||
consumeSuppressedPtyExit,
|
||||
createSplitGroup,
|
||||
newBrowserTab: () => {
|
||||
const defaultUrl = useAppStore.getState().browserDefaultUrl ?? 'about:blank'
|
||||
|
||||
@@ -0,0 +1,209 @@
|
||||
import { memo, useCallback, useMemo } from 'react'
|
||||
import { createPortal } from 'react-dom'
|
||||
import { useShallow } from 'zustand/react/shallow'
|
||||
import type { Tab, TabGroup, TerminalTab } from '../../../../shared/types'
|
||||
import { useAppStore } from '../../store'
|
||||
import { tabGroupBodyAnchorName } from '../tab-group/tab-group-body-anchor'
|
||||
import {
|
||||
findActivityTerminalPortal,
|
||||
type ActivityTerminalPortalTarget
|
||||
} from '../activity/activity-terminal-portal'
|
||||
import TerminalPane from './TerminalPane'
|
||||
|
||||
type TerminalOverlayAssignment = {
|
||||
groupId: string
|
||||
isActiveInGroup: boolean
|
||||
}
|
||||
|
||||
const EMPTY_TERMINAL_TABS: readonly TerminalTab[] = []
|
||||
const EMPTY_UNIFIED_TABS: readonly Tab[] = []
|
||||
const EMPTY_GROUPS: readonly TabGroup[] = []
|
||||
const EMPTY_ACTIVITY_PORTALS: ActivityTerminalPortalTarget[] = []
|
||||
|
||||
type TerminalOverlaySlotProps = {
|
||||
terminalTabId: string
|
||||
terminalGeneration: number | undefined
|
||||
worktreeId: string
|
||||
worktreePath: string
|
||||
groupId: string | undefined
|
||||
isVisible: boolean
|
||||
isActive: boolean
|
||||
activityTerminalPortal: ActivityTerminalPortalTarget | null
|
||||
onFocusOwningGroup: ((groupId: string) => void) | undefined
|
||||
consumeSuppressedPtyExit: (ptyId: string) => boolean
|
||||
closeTab: (tabId: string) => void
|
||||
}
|
||||
|
||||
const TerminalOverlaySlot = memo(function TerminalOverlaySlot({
|
||||
terminalTabId,
|
||||
terminalGeneration,
|
||||
worktreeId,
|
||||
worktreePath,
|
||||
groupId,
|
||||
isVisible,
|
||||
isActive,
|
||||
activityTerminalPortal,
|
||||
onFocusOwningGroup,
|
||||
consumeSuppressedPtyExit,
|
||||
closeTab
|
||||
}: TerminalOverlaySlotProps): React.JSX.Element {
|
||||
const anchorName = groupId !== undefined ? tabGroupBodyAnchorName(groupId) : undefined
|
||||
const style: React.CSSProperties = useMemo(
|
||||
() =>
|
||||
anchorName
|
||||
? {
|
||||
position: 'absolute',
|
||||
positionAnchor: anchorName,
|
||||
top: `anchor(${anchorName} top)`,
|
||||
left: `anchor(${anchorName} left)`,
|
||||
width: `anchor-size(${anchorName} width)`,
|
||||
height: `anchor-size(${anchorName} height)`,
|
||||
display: isVisible ? 'flex' : 'none',
|
||||
pointerEvents: isVisible ? 'auto' : 'none'
|
||||
}
|
||||
: {
|
||||
position: 'absolute',
|
||||
top: 0,
|
||||
left: 0,
|
||||
width: 0,
|
||||
height: 0,
|
||||
display: 'none',
|
||||
pointerEvents: 'none'
|
||||
},
|
||||
[anchorName, isVisible]
|
||||
)
|
||||
const focusGroup = useCallback(() => {
|
||||
if (groupId !== undefined && onFocusOwningGroup) {
|
||||
onFocusOwningGroup(groupId)
|
||||
}
|
||||
}, [groupId, onFocusOwningGroup])
|
||||
|
||||
const terminalPane = (
|
||||
<TerminalPane
|
||||
key={`${terminalTabId}-${terminalGeneration ?? 0}`}
|
||||
tabId={terminalTabId}
|
||||
worktreeId={worktreeId}
|
||||
cwd={worktreePath}
|
||||
isActive={isActive || activityTerminalPortal?.active === true}
|
||||
// Why: split-group changes reparent TabGroupPanel subtrees. Keeping the
|
||||
// TerminalPane mounted here preserves alt-screen TUI state while this
|
||||
// flag still lets hidden tabs throttle rendering.
|
||||
isVisible={isVisible || activityTerminalPortal !== null}
|
||||
isolatedPaneId={activityTerminalPortal?.paneId ?? null}
|
||||
onPtyExit={(ptyId) => {
|
||||
if (consumeSuppressedPtyExit(ptyId)) {
|
||||
return
|
||||
}
|
||||
closeTab(terminalTabId)
|
||||
}}
|
||||
onCloseTab={() => closeTab(terminalTabId)}
|
||||
/>
|
||||
)
|
||||
|
||||
if (activityTerminalPortal) {
|
||||
return createPortal(
|
||||
terminalPane,
|
||||
activityTerminalPortal.target,
|
||||
`activity-terminal-${terminalTabId}`
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
style={style}
|
||||
data-terminal-overlay-tab-id={terminalTabId}
|
||||
onPointerDown={focusGroup}
|
||||
onFocusCapture={focusGroup}
|
||||
>
|
||||
{terminalPane}
|
||||
</div>
|
||||
)
|
||||
})
|
||||
|
||||
const TerminalPaneOverlayLayer = memo(function TerminalPaneOverlayLayer({
|
||||
worktreeId,
|
||||
worktreePath,
|
||||
isWorktreeActive,
|
||||
activityTerminalPortals = EMPTY_ACTIVITY_PORTALS
|
||||
}: {
|
||||
worktreeId: string
|
||||
worktreePath: string
|
||||
isWorktreeActive: boolean
|
||||
activityTerminalPortals?: ActivityTerminalPortalTarget[]
|
||||
}): React.JSX.Element | null {
|
||||
const { terminalTabs, unifiedTabs, groups, activeGroupId } = useAppStore(
|
||||
useShallow((state) => ({
|
||||
terminalTabs: state.tabsByWorktree[worktreeId] ?? EMPTY_TERMINAL_TABS,
|
||||
unifiedTabs: state.unifiedTabsByWorktree[worktreeId] ?? EMPTY_UNIFIED_TABS,
|
||||
groups: state.groupsByWorktree[worktreeId] ?? EMPTY_GROUPS,
|
||||
activeGroupId: state.activeGroupIdByWorktree[worktreeId]
|
||||
}))
|
||||
)
|
||||
const focusGroup = useAppStore((state) => state.focusGroup)
|
||||
const consumeSuppressedPtyExit = useAppStore((state) => state.consumeSuppressedPtyExit)
|
||||
const closeTab = useAppStore((state) => state.closeTab)
|
||||
|
||||
const focusOwningGroup = useCallback(
|
||||
(groupId: string) => focusGroup(worktreeId, groupId),
|
||||
[focusGroup, worktreeId]
|
||||
)
|
||||
|
||||
const groupActiveTabById = useMemo(() => {
|
||||
const lookup: Record<string, string | null | undefined> = {}
|
||||
for (const group of groups) {
|
||||
lookup[group.id] = group.activeTabId
|
||||
}
|
||||
return lookup
|
||||
}, [groups])
|
||||
|
||||
const assignments = useMemo(() => {
|
||||
const entries = new Map<string, TerminalOverlayAssignment>()
|
||||
for (const tab of unifiedTabs) {
|
||||
if (tab.contentType !== 'terminal') {
|
||||
continue
|
||||
}
|
||||
entries.set(tab.entityId, {
|
||||
groupId: tab.groupId,
|
||||
isActiveInGroup: groupActiveTabById[tab.groupId] === tab.id
|
||||
})
|
||||
}
|
||||
return entries
|
||||
}, [groupActiveTabById, unifiedTabs])
|
||||
|
||||
if (!worktreePath) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
{terminalTabs.map((terminalTab) => {
|
||||
const assignment = assignments.get(terminalTab.id)
|
||||
const isVisible = Boolean(isWorktreeActive && assignment && assignment.isActiveInGroup)
|
||||
const isActive = Boolean(isVisible && assignment && assignment.groupId === activeGroupId)
|
||||
const activityTerminalPortal = findActivityTerminalPortal(
|
||||
activityTerminalPortals,
|
||||
worktreeId,
|
||||
terminalTab.id
|
||||
)
|
||||
return (
|
||||
<TerminalOverlaySlot
|
||||
key={terminalTab.id}
|
||||
terminalTabId={terminalTab.id}
|
||||
terminalGeneration={terminalTab.generation}
|
||||
worktreeId={worktreeId}
|
||||
worktreePath={worktreePath}
|
||||
groupId={assignment?.groupId}
|
||||
isVisible={isVisible}
|
||||
isActive={isActive}
|
||||
activityTerminalPortal={activityTerminalPortal}
|
||||
onFocusOwningGroup={focusOwningGroup}
|
||||
consumeSuppressedPtyExit={consumeSuppressedPtyExit}
|
||||
closeTab={closeTab}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</>
|
||||
)
|
||||
})
|
||||
|
||||
export default TerminalPaneOverlayLayer
|
||||
Reference in New Issue
Block a user