diff --git a/src/renderer/src/components/agent-sign-in-strip/CloudAgentSignInStrip.tsx b/src/renderer/src/components/agent-sign-in-strip/CloudAgentSignInStrip.tsx index d5d51194207..1add2e8f00a 100644 --- a/src/renderer/src/components/agent-sign-in-strip/CloudAgentSignInStrip.tsx +++ b/src/renderer/src/components/agent-sign-in-strip/CloudAgentSignInStrip.tsx @@ -213,6 +213,7 @@ export function CloudAgentSignInStrip({ } }} data-cloud-sign-in-strip + data-pane-prevent-terminal-focus onKeyDown={(event) => { if (event.key === 'Escape') { event.stopPropagation() diff --git a/src/renderer/src/components/agent-sign-in-strip/CloudStripPasteForm.tsx b/src/renderer/src/components/agent-sign-in-strip/CloudStripPasteForm.tsx index c3c7f57e3a3..4d149d5f438 100644 --- a/src/renderer/src/components/agent-sign-in-strip/CloudStripPasteForm.tsx +++ b/src/renderer/src/components/agent-sign-in-strip/CloudStripPasteForm.tsx @@ -69,6 +69,8 @@ export function CloudStripPasteForm({ value={code} onChange={(event) => setCode(event.target.value)} onPaste={(event) => { + // Stays in this field: never bubbles to the pane's terminal paste handling. + event.stopPropagation() if (!setupCode) { return } diff --git a/src/renderer/src/components/terminal-pane/context-menu/use-terminal-context-menu-trigger.ts b/src/renderer/src/components/terminal-pane/context-menu/use-terminal-context-menu-trigger.ts index bb0adf9af62..406de1a91f0 100644 --- a/src/renderer/src/components/terminal-pane/context-menu/use-terminal-context-menu-trigger.ts +++ b/src/renderer/src/components/terminal-pane/context-menu/use-terminal-context-menu-trigger.ts @@ -1,4 +1,5 @@ import { useEffect, useRef, useState } from 'react' +import { pasteBelongsToPaneControl } from '../input/paste/paste-belongs-to-pane-control' import type { ManagedPane } from '@/lib/pane-manager/pane-manager-types' import type { PaneManager } from '@/lib/pane-manager/pane-manager' import type { TerminalPasteSource } from '../input/paste/terminal-paste-model' @@ -107,6 +108,11 @@ export function useTerminalContextMenuTrigger({ const onContextMenuCapture = (event: React.MouseEvent): void => { const target = event.target + // SETUP-PASTE: a pane control's own field (the cloud strip's code field) gets the native + // Cut/Copy/Paste menu, never the terminal's, whose Paste would go to the shell. + if (pasteBelongsToPaneControl(target)) { + return + } if (!managerRef.current || !(target instanceof Node)) { event.preventDefault() contextPaneIdRef.current = null diff --git a/src/renderer/src/components/terminal-pane/input/paste/create-terminal-right-click-paste-capture.ts b/src/renderer/src/components/terminal-pane/input/paste/create-terminal-right-click-paste-capture.ts index 772470cb956..655d3ac0b2c 100644 --- a/src/renderer/src/components/terminal-pane/input/paste/create-terminal-right-click-paste-capture.ts +++ b/src/renderer/src/components/terminal-pane/input/paste/create-terminal-right-click-paste-capture.ts @@ -1,4 +1,5 @@ import type { ManagedPane } from '@/lib/pane-manager/pane-manager-types' +import { pasteBelongsToPaneControl } from './paste-belongs-to-pane-control' import { claimTerminalRightClickPasteMouseDown, isTerminalRightClickPasteGesture @@ -15,6 +16,10 @@ export function createTerminalRightClickPasteCapture({ if (!isTerminalRightClickPasteGesture(event, rightClickToPaste)) { return } + // SETUP-PASTE: right-click paste is the terminal's only when the click is on the terminal. + if (pasteBelongsToPaneControl(event.target)) { + return + } const clickedPane = findClickedPane(event.target) if (clickedPane) { claimTerminalRightClickPasteMouseDown(event, clickedPane.terminal) diff --git a/src/renderer/src/components/terminal-pane/input/paste/paste-belongs-to-pane-control.ts b/src/renderer/src/components/terminal-pane/input/paste/paste-belongs-to-pane-control.ts new file mode 100644 index 00000000000..370db510370 --- /dev/null +++ b/src/renderer/src/components/terminal-pane/input/paste/paste-belongs-to-pane-control.ts @@ -0,0 +1,18 @@ +import { isEditableTarget } from '@/lib/text-input/focus/editable-target' +import { isInsideNativeChatCover } from '../../chat/native-chat-covered-pane' + +/** + * SETUP-PASTE: a paste (keyboard, app menu, paste event, right-click) aimed at another control inside + * the pane — the cloud setup strip's code field, the search box, the chat cover, any input or textarea + * that isn't xterm's own — is that control's, never the shell's. Pane strips are portaled into the + * pane container, so the terminal's capture listeners see their events first. + */ +export function pasteBelongsToPaneControl(target: EventTarget | null): boolean { + return ( + (target instanceof Element && + (target.closest('[data-terminal-search-root]') !== null || + target.closest('[data-cloud-sign-in-strip]') !== null)) || + isInsideNativeChatCover(target) || + isEditableTarget(target) + ) +} diff --git a/src/renderer/src/components/terminal-pane/input/paste/terminal-pane-paste-listeners.ts b/src/renderer/src/components/terminal-pane/input/paste/terminal-pane-paste-listeners.ts index 8826680197e..7b684941e72 100644 --- a/src/renderer/src/components/terminal-pane/input/paste/terminal-pane-paste-listeners.ts +++ b/src/renderer/src/components/terminal-pane/input/paste/terminal-pane-paste-listeners.ts @@ -18,6 +18,7 @@ import { import { isEditableTarget } from '@/lib/text-input/focus/editable-target' import { copyTerminalSelection } from '../../clipboard/terminal-selection-copy' import { isInsideNativeChatCover } from '../../chat/native-chat-covered-pane' +import { pasteBelongsToPaneControl } from './paste-belongs-to-pane-control' import type { TerminalPaneCloseController } from '../../layout/close/use-terminal-pane-close-actions' import { formatClipboardImagePasteError, @@ -65,11 +66,7 @@ export function registerTerminalPanePasteListeners({ ) } const onKeyPaste = (event: KeyboardEvent): void => { - const target = event.target - if ( - (target instanceof Element && target.closest('[data-terminal-search-root]')) || - isInsideNativeChatCover(target) - ) { + if (pasteBelongsToPaneControl(event.target)) { return } const matchesPaste = keybindingMatchesAction( @@ -117,11 +114,7 @@ export function registerTerminalPanePasteListeners({ } const onPaste = (event: ClipboardEvent): void => { - const target = event.target - if ( - (target instanceof Element && target.closest('[data-terminal-search-root]')) || - isInsideNativeChatCover(target) - ) { + if (pasteBelongsToPaneControl(event.target)) { return } if (suppressNextNativePaste) { @@ -162,8 +155,7 @@ export function registerTerminalPanePasteListeners({ if ( !(activeElementAtDispatch instanceof Element) || !container.contains(activeElementAtDispatch) || - activeElementAtDispatch.closest('[data-terminal-search-root]') || - isInsideNativeChatCover(activeElementAtDispatch) + pasteBelongsToPaneControl(activeElementAtDispatch) ) { return } diff --git a/src/renderer/src/components/terminal-pane/input/paste/terminal-pane-paste-strip-field.test.ts b/src/renderer/src/components/terminal-pane/input/paste/terminal-pane-paste-strip-field.test.ts new file mode 100644 index 00000000000..90826706e96 --- /dev/null +++ b/src/renderer/src/components/terminal-pane/input/paste/terminal-pane-paste-strip-field.test.ts @@ -0,0 +1,126 @@ +// @vitest-environment happy-dom +// SETUP-PASTE (P0): the cloud setup strip is portaled into the pane container, above the terminal. +// A paste into its "Paste code" field — Cmd+V, the app menu's Paste, a native paste event, or a +// right-click — must stay in the field and never reach the shell. +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import type React from 'react' +import { APP_MENU_PASTE_EVENT } from '@/lib/clipboard/menu/app-menu-paste' +import { pasteBelongsToPaneControl } from './paste-belongs-to-pane-control' +import { createTerminalRightClickPasteCapture } from './create-terminal-right-click-paste-capture' +import { registerTerminalPanePasteListeners } from './terminal-pane-paste-listeners' + +vi.mock('../../../../store/index', () => ({ useAppStore: { getState: () => ({}) } })) +vi.mock('@/lib/workspace/ownership/connection-context', () => ({ getConnectionId: () => null })) + +let container: HTMLDivElement +let field: HTMLInputElement +let xtermTextarea: HTMLTextAreaElement +let dispose: () => void +const pasteFromClipboard = vi.fn() +const executePanePasteText = vi.fn() + +beforeEach(() => { + container = document.createElement('div') + const strip = document.createElement('section') + strip.setAttribute('data-cloud-sign-in-strip', '') + field = document.createElement('input') + strip.append(field) + const xterm = document.createElement('div') + xtermTextarea = document.createElement('textarea') + xtermTextarea.className = 'xterm-helper-textarea' + xterm.append(xtermTextarea) + container.append(strip, xterm) + document.body.append(container) + const pane = { id: 1, terminal: {} } + dispose = registerTerminalPanePasteListeners({ + container, + controller: { + keybindings: {}, + managerRef: { current: { getActivePane: () => pane, getPanes: () => [pane] } }, + paneTransportsRef: { current: new Map() }, + setTerminalError: vi.fn(), + worktreeId: 'wt' + } as never, + execution: { + createPaneClipboardCopiedFiles: vi.fn(), + executePanePasteText, + pasteFromClipboard, + resolvePaneProtectedMultilinePasteOptions: vi.fn() + } as never, + isMac: true, + shortcutPlatform: 'darwin' + }) +}) + +afterEach(() => { + dispose() + container.remove() + vi.clearAllMocks() +}) + +const cmdV = (): KeyboardEvent => + new KeyboardEvent('keydown', { + key: 'v', + code: 'KeyV', + metaKey: true, + bubbles: true, + cancelable: true + }) + +describe('a paste into the cloud strip field never reaches the terminal', () => { + it('Cmd+V in the field: the terminal does not paste, and the field keeps the native paste', () => { + field.focus() + const event = cmdV() + field.dispatchEvent(event) + expect(pasteFromClipboard).not.toHaveBeenCalled() + expect(event.defaultPrevented).toBe(false) + }) + + it('a native paste event in the field is left to the field', () => { + const event = new Event('paste', { bubbles: true, cancelable: true }) + field.dispatchEvent(event) + expect(pasteFromClipboard).not.toHaveBeenCalled() + expect(event.defaultPrevented).toBe(false) + }) + + it("the app menu's Paste while the field has focus is not sent to the shell", () => { + field.focus() + const event = new Event(APP_MENU_PASTE_EVENT, { cancelable: true }) + window.dispatchEvent(event) + expect(executePanePasteText).not.toHaveBeenCalled() + expect(event.defaultPrevented).toBe(false) + }) + + it('still pastes into the terminal when the terminal has focus (Cmd+V)', () => { + xtermTextarea.focus() + const event = cmdV() + xtermTextarea.dispatchEvent(event) + expect(pasteFromClipboard).toHaveBeenCalledTimes(1) + }) + + it('right-click paste on the field is not claimed by the terminal', () => { + const { onMouseDownCapture } = createTerminalRightClickPasteCapture({ + rightClickToPaste: true, + findClickedPane: () => ({ terminal: {} }) as never + }) + const preventDefault = vi.fn() + onMouseDownCapture({ + button: 2, + target: field, + preventDefault, + stopPropagation: vi.fn() + } as unknown as React.MouseEvent) + expect(preventDefault).not.toHaveBeenCalled() + }) +}) + +describe('pasteBelongsToPaneControl', () => { + it('is the field’s for the strip and any non-xterm input; the terminal’s for xterm and plain pane content', () => { + expect(pasteBelongsToPaneControl(field)).toBe(true) + expect(pasteBelongsToPaneControl(container.querySelector('[data-cloud-sign-in-strip]'))).toBe( + true + ) + expect(pasteBelongsToPaneControl(xtermTextarea)).toBe(false) + expect(pasteBelongsToPaneControl(xtermTextarea.parentElement)).toBe(false) + }) +})