Files
Netcatty/components/terminalLayer/TerminalLayerWorkspaceSection.tsx
T
陈大猫andClaude Opus 5.5 1b9c744fda perf(ui): stop whole-shell re-renders on clicks, focus, typing and modifiers (#4117)
Several everyday interactions re-rendered the entire shell (AppView, TopTabs,
TerminalLayer and every pane) even though nothing they show had changed:

- Clicking or focusing a split pane always replaced the workspaces array
  (twice per click: onClick + onFocusCapture), cascading through the whole
  app and re-saving the session restore snapshot. setWorkspaceFocusedSession
  and moveFocusInWorkspace now keep the same array when focus is unchanged
  (domain/workspace.ts setWorkspaceFocusedSessionId).
- Every window focus/visibility change resynced app lock and produced a new
  runtime state object even when nothing changed, re-rendering the provider
  tree. Equal states keep the current object and useAppLockState's result is
  memoized.
- Quick switcher keystrokes went through AppSideEffects state and the
  app-local UI store, re-rendering AppSideEffects, every host island, AppShell,
  AppView and TopTabs per character. QuickSwitcherOverlay now owns the query
  and host results; the overlay unmounts on close, which resets the query
  exactly where setQuickSearch('') used to.
- Pressing/releasing Ctrl/Cmd (every ⌘C/⌘V/Ctrl+C) re-rendered the whole tab
  bar to toggle shortcut number badges. The held flag now lives in a small
  store read only by each tab's TabIconSlot.
- AppView passed fresh inline callbacks that defeated TopTabs' memo comparator
  and every open editor tab; TextEditorTabView is now memoized. TopTabs
  memoizes per-workspace session lists and hoists the root tab icons.
- Workspace drag previews only update state when the drop hint actually moves.

React commits / render time for 10 repetitions in the real app (profiling
build, 2 split panes, 300 hosts), main -> this change:
  window focus            42 / 17.1 ms -> 2 / 0.4 ms
  press/release Cmd       50 / 41.9 ms -> 22 / 6.6 ms
  click focused pane      42 / 69.6 ms -> 3 / 0.4 ms
  quick switcher typing   41 / 61.3 ms -> 16 / 34.8 ms

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 00:22:10 -08:00

377 lines
15 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import React, { memo, useEffect, useState, useSyncExternalStore } from 'react';
import { createPortal } from 'react-dom';
import {
getSidePanelLiveSnapshot,
sidePanelLiveStore,
} from '../../application/state/sidePanelLiveStore';
import { getPaneMagnificationShortcutLabel } from '../../domain/paneMagnification';
import { terminalLayerWorkspaceCtxEqual } from './terminalLayerViewMemo';
import { isSameSplitHint } from './splitHintEquality';
type WorkspaceContext = Record<string, any>;
function TerminalLayerWorkspaceSectionInner({ ctx }: { ctx: WorkspaceContext }) {
// Active workspace / focused session come ONLY from the live store so top-tab
// switches do not force a TerminalLayerView ctx rebuild just to flip them.
// Do not fall back to ctx.* — after memo omits activeWorkspace from equality,
// ctx can still hold a previous workspace after live has been cleared
// (workspace → solo session). Falling back would keep compose bar / resizer
// handlers bound to a stale workspace.
// Panes already derive visibility from activeTabStore themselves.
const live = useSyncExternalStore(
sidePanelLiveStore.subscribe,
sidePanelLiveStore.getSnapshot,
() => getSidePanelLiveSnapshot(false),
);
const activeWorkspace = live.activeWorkspace;
const focusedSessionId = live.focusedSessionId;
const isFocusMode = activeWorkspace?.viewMode === 'focus';
const {
workspaceInnerRef,
workspaceOuterRef,
activeTabId,
workspaceOverlayRef,
draggingSessionId,
dropHint,
setDropHint,
computeSplitHint,
handleWorkspaceDrop,
TerminalPanesHost,
sessions,
sessionHostsMap,
sessionChainHostsMap,
sessionSudoAutofillPasswordsMap,
sessionSudoAutofillCandidatesMap,
resolvedSessionHostIds,
workspaceById,
workspaceRectsById,
isTerminalLayerVisible,
magnifiedPane,
handleMagnifyTerminalPane,
handleTerminalPaneInteraction,
handleRestoreMagnifiedPane,
workspaceFocusHandlersRef,
workspaceBroadcastHandlersRef,
splitHorizontalHandlersRef,
splitVerticalHandlersRef,
resolveSessionAppearance,
hostMap,
keys,
identities,
snippets,
knownHosts,
terminalFontFamilyId,
fontSize,
terminalTheme,
followAppTerminalTheme,
accentMode,
customAccent,
terminalSettings,
hotkeyScheme,
disableTerminalFontZoom,
restoreTerminalCwd,
keyBindings,
resizing,
isComposeBarOpen,
sessionLogConfig,
sshDebugLogsEnabled,
onHotkeyAction,
handleTerminalFontSizeChange,
handleOpenSftp,
handleTerminalCwdChange,
handleTerminalTitleChange,
handleTerminalBell,
handleTerminalOutput,
handleTerminalContextReaderChange,
handleOpenScripts,
handleOpenHistory,
handleOpenSystem,
handleOpenTheme,
handleCloseSession,
handleStatusChange,
handleSessionExit,
handleTerminalDataCapture,
handleOsDetected,
handleUpdateHost,
handleAddKnownHost,
handleCommandExecuted,
handleCommandSubmitted,
onSetWorkspaceFocusedSession,
onSplitSession,
isBroadcastEnabled,
isComposeSendToAll,
onToggleComposeSendTarget,
handleBroadcastInput,
handleBroadcastInterruptPriorityChange,
handleToggleWorkspaceComposeBar,
handleSnippetExecutorChange,
handleProgrammaticCommandLogRewriteChange,
handleAddSelectionToAI,
activeResizers,
composeBarThemeColors,
findSplitNode,
handleComposeSend,
handleSnippetFromPanel,
refocusTerminalSession,
setIsComposeBarOpen,
setResizing,
TerminalComposeBar,
Array,
cn,
onStartSessionRename,
onRemoveSessionFromWorkspace,
onReorderTabs,
onStartSessionDrag,
onEndSessionDrag,
isGlobalBroadcastEnabled,
isGlobalComposeSendToAll,
canUseGlobalBroadcast,
onToggleGlobalBroadcast,
onToggleGlobalComposeSendTarget,
t,
} = ctx;
const activeMagnifiedTerminal = !!activeTabId && magnifiedPane?.tabId === activeTabId
&& magnifiedPane.target.kind === 'terminal'
? magnifiedPane
: null;
const paneMagnificationShortcutLabel = getPaneMagnificationShortcutLabel(keyBindings, hotkeyScheme);
const [showMagnificationHint, setShowMagnificationHint] = useState(false);
useEffect(() => {
if (!activeMagnifiedTerminal) {
setShowMagnificationHint(false);
return undefined;
}
setShowMagnificationHint(true);
const timerId = window.setTimeout(() => setShowMagnificationHint(false), 1800);
return () => window.clearTimeout(timerId);
}, [activeMagnifiedTerminal]);
return (
<div data-section="terminal-workspace-column" className="flex-1 min-h-0 flex flex-col">
<div ref={workspaceInnerRef} className="flex-1 min-h-0 overflow-hidden relative">
{draggingSessionId && !isFocusMode && (
<div
ref={workspaceOverlayRef}
className="absolute inset-0 z-30"
onDragOver={(e) => {
if (isFocusMode) return;
if (!e.dataTransfer.types.includes('session-id')) return;
e.preventDefault();
e.stopPropagation();
const hint = computeSplitHint(e);
// Only re-render the workspace when the preview actually moves.
setDropHint((prev) => (isSameSplitHint(prev, hint) ? prev : hint));
}}
onDragLeave={(e) => {
if (!e.dataTransfer.types.includes('session-id')) return;
setDropHint(null);
}}
onDrop={(e) => {
e.preventDefault();
e.stopPropagation();
handleWorkspaceDrop(e);
}}
>
{dropHint && (
<div className="absolute inset-0 pointer-events-none">
<div
className="absolute bg-emerald-600/35 border border-emerald-400/70 backdrop-blur-sm transition-all duration-150"
style={{
width: dropHint.rect ? `${dropHint.rect.w}px` : dropHint.direction === 'vertical' ? '50%' : '100%',
height: dropHint.rect ? `${dropHint.rect.h}px` : dropHint.direction === 'vertical' ? '100%' : '50%',
left: dropHint.rect ? `${dropHint.rect.x}px` : dropHint.direction === 'vertical' ? (dropHint.position === 'left' ? 0 : '50%') : 0,
top: dropHint.rect ? `${dropHint.rect.y}px` : dropHint.direction === 'vertical' ? 0 : (dropHint.position === 'top' ? 0 : '50%'),
}}
/>
</div>
)}
</div>
)}
<TerminalPanesHost
sessions={sessions}
sessionHostsMap={sessionHostsMap}
sessionChainHostsMap={sessionChainHostsMap}
sessionSudoAutofillPasswordsMap={sessionSudoAutofillPasswordsMap}
sessionSudoAutofillCandidatesMap={sessionSudoAutofillCandidatesMap}
resolvedSessionHostIds={resolvedSessionHostIds}
workspaceById={workspaceById}
workspaceRectsById={workspaceRectsById}
isTerminalLayerVisible={isTerminalLayerVisible}
magnifiedPane={magnifiedPane}
onMagnifyTerminalPane={handleMagnifyTerminalPane}
onTerminalPaneInteraction={handleTerminalPaneInteraction}
workspaceFocusHandlersRef={workspaceFocusHandlersRef}
workspaceBroadcastHandlersRef={workspaceBroadcastHandlersRef}
splitHorizontalHandlersRef={splitHorizontalHandlersRef}
splitVerticalHandlersRef={splitVerticalHandlersRef}
resolveSessionAppearance={resolveSessionAppearance}
hostMap={hostMap}
keys={keys}
identities={identities}
snippets={snippets}
knownHosts={knownHosts}
terminalFontFamilyId={terminalFontFamilyId}
fontSize={fontSize}
terminalTheme={terminalTheme}
followAppTerminalTheme={followAppTerminalTheme}
accentMode={accentMode}
customAccent={customAccent}
terminalSettings={terminalSettings}
hotkeyScheme={hotkeyScheme}
disableTerminalFontZoom={disableTerminalFontZoom}
restoreTerminalCwd={restoreTerminalCwd}
keyBindings={keyBindings}
isResizing={!!resizing}
isComposeBarOpen={isComposeBarOpen}
sessionLog={sessionLogConfig}
sshDebugLogEnabled={sshDebugLogsEnabled}
onHotkeyAction={onHotkeyAction}
onTerminalFontSizeChange={handleTerminalFontSizeChange}
onOpenSftp={handleOpenSftp}
onTerminalCwdChange={handleTerminalCwdChange}
onTerminalTitleChange={handleTerminalTitleChange}
onTerminalBell={handleTerminalBell}
onTerminalOutput={handleTerminalOutput}
onTerminalContextReaderChange={handleTerminalContextReaderChange}
onOpenScripts={handleOpenScripts}
onOpenHistory={handleOpenHistory}
onOpenSystem={handleOpenSystem}
onOpenTheme={handleOpenTheme}
onCloseSession={handleCloseSession}
onStatusChange={handleStatusChange}
onSessionExit={handleSessionExit}
onTerminalDataCapture={handleTerminalDataCapture}
onOsDetected={handleOsDetected}
onUpdateHost={handleUpdateHost}
onAddKnownHost={handleAddKnownHost}
onCommandExecuted={handleCommandExecuted}
onCommandSubmitted={handleCommandSubmitted}
onSetWorkspaceFocusedSession={onSetWorkspaceFocusedSession}
onSplitSession={onSplitSession}
isBroadcastEnabled={isBroadcastEnabled}
isComposeSendToAll={isComposeSendToAll}
isGlobalComposeSendToAll={isGlobalComposeSendToAll}
onToggleGlobalComposeSendTarget={onToggleGlobalComposeSendTarget}
onBroadcastInput={handleBroadcastInput}
onBroadcastInterruptPriorityChange={handleBroadcastInterruptPriorityChange}
onToggleWorkspaceComposeBar={handleToggleWorkspaceComposeBar}
onSnippetExecutorChange={handleSnippetExecutorChange}
onProgrammaticCommandLogRewriteChange={handleProgrammaticCommandLogRewriteChange}
onAddSelectionToAI={handleAddSelectionToAI}
onStartSessionRename={onStartSessionRename}
onRemoveSessionFromWorkspace={onRemoveSessionFromWorkspace}
onReorderTabs={onReorderTabs}
onStartSessionDrag={onStartSessionDrag}
onEndSessionDrag={onEndSessionDrag}
isGlobalBroadcastEnabled={isGlobalBroadcastEnabled}
canUseGlobalBroadcast={canUseGlobalBroadcast}
onToggleGlobalBroadcast={onToggleGlobalBroadcast}
/>
{!isFocusMode && activeResizers.map((handle: any) => {
const isVertical = handle.direction === 'vertical';
const left = isVertical ? handle.rect.x - 3 : handle.rect.x;
const top = isVertical ? handle.rect.y : handle.rect.y - 3;
const width = isVertical ? handle.rect.w + 6 : handle.rect.w;
const height = isVertical ? handle.rect.h : handle.rect.h + 6;
return (
<div
key={handle.id}
className={cn('absolute group', isVertical ? 'cursor-ew-resize' : 'cursor-ns-resize')}
data-section="terminal-split-resizer"
data-split-direction={handle.direction}
style={{
left: `${left}px`,
top: `${top}px`,
width: `${width}px`,
height: `${height}px`,
zIndex: 25,
}}
onMouseDown={(e) => {
e.preventDefault();
e.stopPropagation();
const ws = activeWorkspace;
if (!ws) return;
const split = findSplitNode(ws.root, handle.splitId);
const childCount = split && split.type === 'split' ? split.children.length : 0;
const sizes = split && split.type === 'split' && split.sizes && split.sizes.length === childCount
? split.sizes
: Array(childCount).fill(1);
setResizing({
workspaceId: ws.id,
splitId: handle.splitId,
index: handle.index,
direction: handle.direction,
startSizes: sizes.length ? sizes : [1, 1],
startArea: handle.splitArea,
startClient: { x: e.clientX, y: e.clientY },
});
}}
>
<div
data-section="terminal-split-resizer-bar"
className={cn(
'absolute bg-border/70 group-hover:bg-primary/60 transition-colors',
isVertical ? 'w-px h-full left-1/2 -translate-x-1/2' : 'h-px w-full top-1/2 -translate-y-1/2',
)}
/>
</div>
);
})}
</div>
{activeMagnifiedTerminal && workspaceOuterRef.current && createPortal(
<>
<div
className="absolute inset-0 z-40 bg-background/55 backdrop-blur-[1px]"
aria-hidden="true"
data-section="pane-magnification-backdrop"
/>
{showMagnificationHint && (
<button
type="button"
className="absolute bottom-4 right-4 z-[60] rounded border border-border/70 bg-background/90 px-2 py-1 text-[10px] text-muted-foreground shadow-sm animate-in fade-in duration-150"
onClick={handleRestoreMagnifiedPane}
data-section="pane-magnification-hint"
aria-label={t('terminal.paneMagnification.restore')}
>
{t('terminal.paneMagnification.hint')}: {t('terminal.layer.terminal')}
{paneMagnificationShortcutLabel ? ` · ${paneMagnificationShortcutLabel} / Esc` : ' · Esc'}
</button>
)}
</>,
workspaceOuterRef.current,
)}
{activeWorkspace && focusedSessionId && isComposeBarOpen && (
<TerminalComposeBar
sessionId={focusedSessionId}
onSend={handleComposeSend}
onSnippetClick={(snippet) => void handleSnippetFromPanel(snippet)}
snippets={snippets}
onClose={() => {
setIsComposeBarOpen(false);
refocusTerminalSession(focusedSessionId);
}}
isBroadcastEnabled={isBroadcastEnabled?.(activeWorkspace.id)}
composeSendToAll={isComposeSendToAll?.(activeWorkspace.id) ?? false}
onToggleComposeSendTarget={onToggleComposeSendTarget
? () => onToggleComposeSendTarget(activeWorkspace.id)
: undefined}
themeColors={composeBarThemeColors}
/>
)}
</div>
);
}
export const TerminalLayerWorkspaceSection = memo(
TerminalLayerWorkspaceSectionInner,
(prev, next) => terminalLayerWorkspaceCtxEqual(prev.ctx, next.ctx),
);
TerminalLayerWorkspaceSection.displayName = 'TerminalLayerWorkspaceSection';