fix(app): keep a touch drag from refocusing the browser terminal

Builds on the contributor commit for the Android browser keyboard.
A touch drag now keeps the dismissed keyboard closed, and a tap
still opens it.

- Keep calling onFocus on every pointer-down, so a touch drag still
  marks the terminal as focused and claims its size as before. Only
  the input focus waits for a tap.
- Move the test tap into a helper.
- Leave the native fullscreen scrolling change, its tests, the
  inertial gesture check, and the QA video out of this branch. They
  belong to #7762.

Refs #9481

Co-authored-by: Tony <liutuo@outlook.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
paseo-bot[bot]
2026-11-19 12:30:33 +00:00
co-authored by Tony Claude Opus 5.5
parent 83e1f0b041
commit c016c161cc
6 changed files with 30 additions and 167 deletions
Binary file not shown.
+12 -24
View File
@@ -513,18 +513,12 @@ test.describe("terminal history with a finger", () => {
await expectSingleFingerScroll(page, 1);
await expect(input(page)).not.toBeFocused();
const screen = page
.getByTestId("terminal-surface")
.filter({ visible: true })
.locator(".xterm-screen");
const box = await screen.boundingBox();
if (!box) throw new Error("Terminal screen is not visible");
await page.touchscreen.tap(box.x + box.width / 2, box.y + box.height / 3);
await tapTerminalScreen(page);
await expect(input(page)).toBeFocused();
// Android's Back button hides the IME without blurring this input.
await expectSingleFingerScroll(page, 24);
await expect(input(page)).not.toBeFocused();
await screen.click();
await terminalScreen(page).click();
await expect(input(page)).toBeFocused();
});
@@ -532,19 +526,6 @@ test.describe("terminal history with a finger", () => {
test(`preserves finger navigation in ${mode} input mode`, async ({ page }) => {
await openPhoneInputCapture(page, mode);
await expectFingerNavigationInput(page, mode);
// xterm's inertial gesture has translation but no new pointer coordinates.
await page
.locator(".xterm-screen")
.filter({ visible: true })
.evaluate((screen) => {
const event = new CustomEvent("-xterm-gesturechange", { cancelable: true });
Object.assign(event, { translationX: 0, translationY: 120 });
screen.dispatchEvent(event);
});
// A following byte is a PTY round-trip barrier for the gesture's reports.
await input(page).press("z");
await expect.poll(() => getTerminalBufferText(page)).toContain("TOUCH_BYTES:7a:END");
expect(await getTerminalBufferText(page)).not.toContain("INVALID_TOUCH_INPUT");
});
}
});
@@ -576,6 +557,16 @@ async function openPhoneTerminalHistory(page: Page) {
await expect.poll(() => viewport(page)).toBeGreaterThan(0);
}
function terminalScreen(page: Page) {
return page.getByTestId("terminal-surface").filter({ visible: true }).locator(".xterm-screen");
}
async function tapTerminalScreen(page: Page) {
const box = await terminalScreen(page).boundingBox();
expect(box).not.toBeNull();
await page.touchscreen.tap(box!.x + box!.width / 2, box!.y + box!.height / 3);
}
async function openPhoneInputCapture(page: Page, mode: "alternate" | "mouse") {
const escapes = mode === "alternate" ? "\x1b[?1049h\x1b[?1h" : "\x1b[?1000h\x1b[?1006h";
await harness.createTerminal({
@@ -587,9 +578,6 @@ async function openPhoneInputCapture(page: Page, mode: "alternate" | "mouse") {
process.stdin.setRawMode(true);
process.stdin.once("data", () => {
process.stdin.on("data", chunk => {
if (chunk.includes(Buffer.from("NaN"))) {
process.stdout.write("\\r\\nINVALID_TOUCH_INPUT\\r\\n");
}
process.stdout.write("\\r\\nTOUCH_BYTES:" + chunk.toString("hex") + ":END\\r\\n");
});
process.stdout.write(${JSON.stringify(escapes)} + "TOUCH_INPUT_READY\\r\\n");
@@ -557,25 +557,6 @@ function NativeTerminalEmulator({
if (!terminal || !model || rows <= 0) {
return;
}
const metrics = metricsRef.current;
if (!selectionRangeRef.current && metrics) {
const point = gestureStartRef.current;
const applicationInput = terminal.getScrollInput({
direction,
rows,
column: Math.floor(point.x / metrics.cellWidth) + 1,
row: Math.floor(point.y / metrics.cellHeight) + 1,
x: Math.floor(point.x) + 1,
y: Math.floor(point.y) + 1,
});
if (applicationInput !== null) {
if (applicationInput.length > 0) {
claimActiveTerminalSize();
callbacksRef.current.onInput?.(applicationInput);
}
return;
}
}
const visibleRows = resolveVisibleRows(terminal);
const screen =
direction === "up"
@@ -583,7 +564,7 @@ function NativeTerminalEmulator({
: model.scrollDown({ rows, visibleRows });
commitTerminalScreen(screen);
},
[claimActiveTerminalSize, commitTerminalScreen, resolveVisibleRows],
[commitTerminalScreen, resolveVisibleRows],
);
const returnToBottom = useCallback(() => {
@@ -561,28 +561,25 @@ export default function TerminalEmulator({
}, []);
const touchFocusRef = useRef<TerminalTouchFocus | null>(null);
const focusTerminalFromPointer = useCallback(() => {
onFocus?.();
runtimeRef.current?.focus();
}, [onFocus]);
const handleRootPointerDown = useCallback(
(event: ReactPointerEvent) => {
onFocus?.();
touchFocusRef.current = null;
if (event.pointerType === "touch") {
// Focusing before a touch becomes a tap opens the keyboard during a scroll.
if (event.isPrimary) {
touchFocusRef.current = {
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY,
};
}
if (event.pointerType !== "touch") {
runtimeRef.current?.focus();
return;
}
focusTerminalFromPointer();
// Focusing before a touch becomes a tap opens the keyboard during a scroll.
if (event.isPrimary) {
touchFocusRef.current = {
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY,
};
}
},
[focusTerminalFromPointer],
[onFocus],
);
const handleRootPointerMove = useCallback((event: ReactPointerEvent) => {
@@ -595,14 +592,11 @@ export default function TerminalEmulator({
}
}, []);
const handleRootPointerUp = useCallback(
(event: ReactPointerEvent) => {
const touch = touchFocusRef.current;
touchFocusRef.current = null;
if (touch?.pointerId === event.pointerId) focusTerminalFromPointer();
},
[focusTerminalFromPointer],
);
const handleRootPointerUp = useCallback((event: ReactPointerEvent) => {
const touch = touchFocusRef.current;
touchFocusRef.current = null;
if (touch?.pointerId === event.pointerId) runtimeRef.current?.focus();
}, []);
const handleRootPointerCancel = useCallback(() => {
touchFocusRef.current = null;
@@ -19,53 +19,6 @@ function expectedBenchmarkLine(index: number): string {
}
describe("native headless terminal state", () => {
test("routes alternate-screen finger scrolling to the application's cursor keys", async () => {
const terminal = createNativeHeadlessTerminal({ rows: 5, cols: COLS });
const scroll = { direction: "up" as const, rows: 2, column: 4, row: 3, x: 24, y: 40 };
expect(terminal.getScrollInput(scroll)).toBeNull();
await terminal.write("\x1b[?1049h\x1b[?1h");
const bounds = terminal.getBufferBounds();
expect(bounds.oldestRow).toBe(bounds.bottomViewport.firstRow);
expect(terminal.getScrollInput(scroll)).toBe("\x1bOA\x1bOA");
expect(terminal.getScrollInput({ ...scroll, direction: "down", rows: 1 })).toBe("\x1bOB");
await terminal.write("\x1b[?1l");
expect(terminal.getScrollInput({ ...scroll, rows: 1 })).toBe("\x1b[A");
await terminal.write("\x1b[?1049l");
expect(terminal.getScrollInput(scroll)).toBeNull();
terminal.dispose();
});
test.each([1000, 1002, 1003])(
"routes finger scrolling through mouse tracking mode %i",
async (mode) => {
const terminal = createNativeHeadlessTerminal({ rows: 5, cols: COLS });
const scroll = { direction: "up" as const, rows: 2, column: 4, row: 3, x: 24, y: 40 };
await terminal.write(`\x1b[?1049h\x1b[?${mode}h\x1b[?1006h`);
expect(terminal.getScrollInput(scroll)).toBe("\x1b[<64;4;3M\x1b[<64;4;3M");
expect(terminal.getScrollInput({ ...scroll, direction: "down", rows: 1 })).toBe(
"\x1b[<65;4;3M",
);
await terminal.write(`\x1b[?${mode}l`);
expect(terminal.getScrollInput({ ...scroll, rows: 1 })).toBe("\x1b[A");
terminal.dispose();
},
);
test("keeps xterm's legacy and pixel mouse encodings for finger scroll input", async () => {
const terminal = createNativeHeadlessTerminal({ rows: 5, cols: COLS });
const scroll = { direction: "up" as const, rows: 1, column: 4, row: 3, x: 24, y: 40 };
await terminal.write("\x1b[?1000h");
expect(terminal.getScrollInput(scroll)).toBe("\x1b[M`$#");
await terminal.write("\x1b[?1006h\x1b[?1016h");
expect(terminal.getScrollInput(scroll)).toBe("\x1b[<64;24;40M");
terminal.reset();
expect(terminal.getScrollInput(scroll)).toBeNull();
terminal.dispose();
});
test("exposes application cursor key mode from headless xterm", async () => {
const terminal = createNativeHeadlessTerminal({ rows: 5, cols: COLS });
@@ -27,7 +27,6 @@ export interface NativeHeadlessTerminal {
write(data: NativeTerminalWriteData): Promise<void>;
getViewportState(input?: TerminalViewportStateInput): TerminalViewportState;
getInputModeState(): NativeHeadlessTerminalInputModeState;
getScrollInput(input: NativeTerminalScrollInput): string | null;
getBufferBounds(): TerminalBufferBounds;
getBufferWindow(input: TerminalBufferWindowInput): TerminalBufferWindow;
resize(input: { rows: number; cols: number }): void;
@@ -35,15 +34,6 @@ export interface NativeHeadlessTerminal {
dispose(): void;
}
export interface NativeTerminalScrollInput {
direction: "up" | "down";
rows: number;
column: number;
row: number;
x: number;
y: number;
}
export interface NativeHeadlessTerminalInputModeState {
applicationCursorKeys: boolean;
bracketedPaste: boolean;
@@ -105,9 +95,6 @@ interface XtermCoreService {
interface HeadlessTerminalWithCore {
_core?: {
coreService?: XtermCoreService;
mouseStateService?: {
encodeMouseEvent(event: XtermMouseWheelEvent): string;
};
_bufferService?: {
buffer?: {
lines?: {
@@ -118,18 +105,6 @@ interface HeadlessTerminalWithCore {
};
}
interface XtermMouseWheelEvent {
col: number;
row: number;
x: number;
y: number;
button: 4;
action: 0 | 1;
ctrl: boolean;
alt: boolean;
shift: boolean;
}
interface TerminalCoordinateStorage {
oldestRow: number;
trimSubscription: { dispose(): void } | null;
@@ -383,34 +358,6 @@ export function createNativeHeadlessTerminal(
getInputModeState(): NativeHeadlessTerminalInputModeState {
return extractInputModeState(terminal);
},
getScrollInput(input: NativeTerminalScrollInput): string | null {
const mouseMode = terminal.modes.mouseTrackingMode;
const reportsWheel = mouseMode !== "none" && mouseMode !== "x10";
if (reportsWheel) {
// Use the pinned xterm encoder so SGR, legacy and pixel reports follow the same modes
// as the web terminal. The native view supplies one-based cell and pixel coordinates.
const mouse = (terminal as unknown as HeadlessTerminalWithCore)._core?.mouseStateService;
if (!mouse) throw new Error("Native terminal mouse encoder is unavailable");
const report = mouse.encodeMouseEvent({
col: Math.max(1, Math.min(terminal.cols, input.column)),
row: Math.max(1, Math.min(terminal.rows, input.row)),
x: input.x,
y: input.y,
button: 4,
action: input.direction === "up" ? 0 : 1,
ctrl: false,
alt: false,
shift: false,
});
return report.repeat(input.rows);
}
if (terminal.buffer.active.type === "alternate") {
const prefix = terminal.modes.applicationCursorKeysMode ? "\x1bO" : "\x1b[";
const key = input.direction === "up" ? "A" : "B";
return `${prefix}${key}`.repeat(input.rows);
}
return null;
},
getBufferBounds(): TerminalBufferBounds {
return extractBufferBounds(terminal, coordinateEpoch, getActiveCoordinateStorage().oldestRow);
},