diff --git a/src/renderer/src/components/Terminal.tsx b/src/renderer/src/components/Terminal.tsx index 24aa5e70e..e6980c8ba 100644 --- a/src/renderer/src/components/Terminal.tsx +++ b/src/renderer/src/components/Terminal.tsx @@ -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 { {/* 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 `` 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} + /> + diff --git a/src/renderer/src/components/browser-pane/BrowserPaneOverlayLayer.tsx b/src/renderer/src/components/browser-pane/BrowserPaneOverlayLayer.tsx index 4aaf88619..cc8a1f6c3 100644 --- a/src/renderer/src/components/browser-pane/BrowserPaneOverlayLayer.tsx +++ b/src/renderer/src/components/browser-pane/BrowserPaneOverlayLayer.tsx @@ -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 `` 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 diff --git a/src/renderer/src/components/browser-pane/browser-pane-slots.ts b/src/renderer/src/components/browser-pane/browser-pane-slots.ts deleted file mode 100644 index a7864bd63..000000000 --- a/src/renderer/src/components/browser-pane/browser-pane-slots.ts +++ /dev/null @@ -1,29 +0,0 @@ -// Why: Electron `` 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 `` 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-` -// BrowserPaneOverlayLayer overlay → `position-anchor: --orca-browser-slot-` -// `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 `` -// 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 - * ``; 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}` -} diff --git a/src/renderer/src/components/tab-group/TabGroupPanel.tsx b/src/renderer/src/components/tab-group/TabGroupPanel.tsx index e7142af4b..bd86341ad 100644 --- a/src/renderer/src/components/tab-group/TabGroupPanel.tsx +++ b/src/renderer/src/components/tab-group/TabGroupPanel.tsx @@ -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 `` (which would reload it). - const bodyAnchorName = browserSlotAnchorName(groupId) + // targets. Browsers avoid `` 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 `
` 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 ? : 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 = ( - { - 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({
)} - {/* 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 `` — 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 ``, + losing TUI state or reloading the page. */} ) diff --git a/src/renderer/src/components/tab-group/TabGroupSplitLayout.test.ts b/src/renderer/src/components/tab-group/TabGroupSplitLayout.test.ts index ab684ce3d..eb0727419 100644 --- a/src/renderer/src/components/tab-group/TabGroupSplitLayout.test.ts +++ b/src/renderer/src/components/tab-group/TabGroupSplitLayout.test.ts @@ -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, diff --git a/src/renderer/src/components/tab-group/TabGroupSplitLayout.tsx b/src/renderer/src/components/tab-group/TabGroupSplitLayout.tsx index 06c3b5a48..7659b114c 100644 --- a/src/renderer/src/components/tab-group/TabGroupSplitLayout.tsx +++ b/src/renderer/src/components/tab-group/TabGroupSplitLayout.tsx @@ -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({