Fix terminal pane title input focus (#2400)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -24,6 +24,7 @@ import {
|
||||
} from './pane-fit-resize-observer'
|
||||
import { buildDefaultTerminalOptions } from './pane-terminal-options'
|
||||
import { ENABLE_WEBGL_RENDERER, attachWebgl, disposeWebgl } from './pane-webgl-renderer'
|
||||
import { shouldFocusTerminalFromPanePointerDown } from './pane-pointer-focus'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Pane creation, terminal open/close, addon management
|
||||
@@ -41,7 +42,7 @@ export function createPaneDOM(
|
||||
options: PaneManagerOptions,
|
||||
dragState: DragReorderState,
|
||||
dragCallbacks: DragReorderCallbacks,
|
||||
onPointerDown: (id: number) => void,
|
||||
onPointerDown: (id: number, options?: { focusTerminal?: boolean }) => void,
|
||||
onMouseEnter: (id: number, event: MouseEvent) => void
|
||||
): ManagedPaneInternal {
|
||||
// Create .pane container
|
||||
@@ -134,8 +135,10 @@ export function createPaneDOM(
|
||||
// the terminal. We must call focus: true here because after DOM reparenting
|
||||
// (e.g. splitPane moves the original pane into a flex container), xterm.js's
|
||||
// native click-to-focus on its internal textarea may not fire reliably.
|
||||
container.addEventListener('pointerdown', () => {
|
||||
onPointerDown(id)
|
||||
container.addEventListener('pointerdown', (event) => {
|
||||
onPointerDown(id, {
|
||||
focusTerminal: shouldFocusTerminalFromPanePointerDown(event.target)
|
||||
})
|
||||
})
|
||||
|
||||
// Focus-follows-mouse handler: when the setting is enabled, hovering a
|
||||
|
||||
@@ -277,9 +277,9 @@ export class PaneManager {
|
||||
this.getDragCallbacks(),
|
||||
// Why: always re-focus even if already active — after splits the
|
||||
// browser's real textarea focus can lag the manager's activePaneId.
|
||||
(paneId) => {
|
||||
(paneId, options) => {
|
||||
if (!this.destroyed) {
|
||||
this.setActivePane(paneId, { focus: true })
|
||||
this.setActivePane(paneId, { focus: options?.focusTerminal !== false })
|
||||
}
|
||||
},
|
||||
(paneId, event) => {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { shouldFocusTerminalFromPanePointerDown } from './pane-pointer-focus'
|
||||
|
||||
class FakeElement {
|
||||
constructor(private readonly closestResult: FakeElement | null = null) {}
|
||||
|
||||
closest(): FakeElement | null {
|
||||
return this.closestResult
|
||||
}
|
||||
}
|
||||
|
||||
describe('shouldFocusTerminalFromPanePointerDown', () => {
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal('Element', FakeElement)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
it('focuses the terminal for non-element targets', () => {
|
||||
expect(shouldFocusTerminalFromPanePointerDown({} as EventTarget)).toBe(true)
|
||||
})
|
||||
|
||||
it('focuses the terminal for ordinary pane surface clicks', () => {
|
||||
const target = new FakeElement()
|
||||
|
||||
expect(shouldFocusTerminalFromPanePointerDown(target as unknown as Element)).toBe(true)
|
||||
})
|
||||
|
||||
it('does not steal focus from pane-local controls', () => {
|
||||
const control = new FakeElement()
|
||||
const target = new FakeElement(control)
|
||||
|
||||
expect(shouldFocusTerminalFromPanePointerDown(target as unknown as Element)).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,21 @@
|
||||
const APP_CONTROL_SELECTOR = [
|
||||
'input:not(.xterm-helper-textarea)',
|
||||
'textarea:not(.xterm-helper-textarea)',
|
||||
'select',
|
||||
'button',
|
||||
'[role="textbox"]',
|
||||
'[contenteditable=""]',
|
||||
'[contenteditable="true"]',
|
||||
'[contenteditable="plaintext-only"]',
|
||||
'[data-pane-prevent-terminal-focus]'
|
||||
].join(',')
|
||||
|
||||
export function shouldFocusTerminalFromPanePointerDown(target: EventTarget | null): boolean {
|
||||
if (typeof Element === 'undefined' || !(target instanceof Element)) {
|
||||
return true
|
||||
}
|
||||
|
||||
// Why: pane-local app controls (for example the title editor) are portaled
|
||||
// into the pane container; focusing xterm from their pointerdown blurs them.
|
||||
return target.closest(APP_CONTROL_SELECTOR) === null
|
||||
}
|
||||
@@ -244,6 +244,29 @@ test.describe('Terminal Panes', () => {
|
||||
await expect(orcaPage.locator('.pane-title-text', { hasText: title })).toHaveCount(1)
|
||||
})
|
||||
|
||||
test('Set Title input stays open when clicked in a split terminal', async ({ orcaPage }) => {
|
||||
await splitActiveTerminalPane(orcaPage, 'vertical')
|
||||
await waitForPaneCount(orcaPage, 2)
|
||||
await splitActiveTerminalPane(orcaPage, 'horizontal')
|
||||
await waitForPaneCount(orcaPage, 3)
|
||||
|
||||
await openTerminalContextMenu(orcaPage)
|
||||
await orcaPage.getByText('Set Title…', { exact: true }).click()
|
||||
|
||||
const titleInput = orcaPage.locator('.pane-title-input').first()
|
||||
await expect(titleInput).toBeVisible()
|
||||
await expect(titleInput).toBeFocused()
|
||||
|
||||
// Why: pane-level pointerdown focuses xterm for terminal clicks. Pane-local
|
||||
// controls must be excluded or clicking the already-open title input blurs
|
||||
// it and commits an empty title, which looks like the editor flashed closed.
|
||||
await titleInput.click({ position: { x: 10, y: 10 } })
|
||||
await orcaPage.waitForTimeout(250)
|
||||
|
||||
await expect(titleInput).toBeVisible()
|
||||
await expect(titleInput).toBeFocused()
|
||||
})
|
||||
|
||||
test('Set Title survives an early blur during first focus handoff', async ({ orcaPage }) => {
|
||||
await openTerminalContextMenu(orcaPage)
|
||||
await orcaPage.evaluate(() => {
|
||||
|
||||
Reference in New Issue
Block a user