diff --git a/packages/app/e2e/browser/terminal-find.spec.ts b/packages/app/e2e/browser/terminal-find.spec.ts index 26a5b5206..a362de780 100644 --- a/packages/app/e2e/browser/terminal-find.spec.ts +++ b/packages/app/e2e/browser/terminal-find.spec.ts @@ -503,6 +503,34 @@ test.describe("terminal history with a finger", () => { await expectMouseWheelScrollsHistory(page); }); + test("finger scrolling leaves terminal input unfocused until a tap", async ({ page }) => { + await openPhoneTerminalHistory(page); + await blurTerminalInput(page); + + await expectSingleFingerScroll(page, 24); + await expect(input(page)).not.toBeFocused(); + await expectSingleFingerScroll(page, 1); + await expect(input(page)).not.toBeFocused(); + + await tapTerminalScreen(page); + await expect(input(page)).toBeFocused(); + }); + + test("finger scrolling keeps a floating keyboard's input focused", async ({ page }) => { + await openPhoneTerminalHistory(page); + await tapTerminalScreen(page); + await expect(input(page)).toBeFocused(); + + // A floating keyboard leaves the visual viewport unchanged. + await expectSingleFingerScroll(page, 24); + await expect(input(page)).toBeFocused(); + await expect(input(page)).toHaveAttribute("virtualkeyboardpolicy", "manual"); + + // Android Back also leaves the input focused. A tap restores automatic keyboard opening. + await tapTerminalScreen(page); + await expect(input(page)).not.toHaveAttribute("virtualkeyboardpolicy"); + }); + for (const mode of ["alternate", "mouse"] as const) { test(`preserves finger navigation in ${mode} input mode`, async ({ page }) => { await openPhoneInputCapture(page, mode); @@ -531,6 +559,26 @@ async function openPhoneTerminalHistory(page: Page) { }); await openPhoneTerminal(page); await expect.poll(() => getTerminalBufferText(page)).toContain("HISTORY_150"); + await expect + .poll(() => page.evaluate(() => window.__paseoTerminal!.buffer.active.baseY)) + .toBeGreaterThan(0); + await page.evaluate(() => window.__paseoTerminal!.scrollToBottom()); + 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 blurTerminalInput(page: Page) { + await input(page).evaluate((element) => element.blur()); + await expect(input(page)).not.toBeFocused(); } async function openPhoneInputCapture(page: Page, mode: "alternate" | "mouse") { @@ -570,6 +618,20 @@ async function openPhoneTerminal(page: Page) { await page.getByTestId(`sidebar-workspace-row-${getServerId()}:${harness.workspaceId}`).click(); await expect(page.getByRole("textbox", { name: "Terminal input" })).toBeVisible(); await expect(page.getByTestId("terminal-surface").filter({ visible: true })).toBeVisible(); + // The terminal stays mounted while the sidebar's closing backdrop still intercepts input. + await expect + .poll(() => + page + .getByTestId("terminal-surface") + .filter({ visible: true }) + .evaluate((root) => { + const box = root.querySelector(".xterm-screen")!.getBoundingClientRect(); + return root.contains( + document.elementFromPoint(box.x + box.width / 2, box.y + box.height / 3), + ); + }), + ) + .toBe(true); } async function expectMouseWheelScrollsHistory(page: Page) { diff --git a/packages/app/src/components/terminal-emulator.tsx b/packages/app/src/components/terminal-emulator.tsx index 7504843c5..61b24ea92 100644 --- a/packages/app/src/components/terminal-emulator.tsx +++ b/packages/app/src/components/terminal-emulator.tsx @@ -13,6 +13,7 @@ import { useRef, type CSSProperties, type MouseEvent as ReactMouseEvent, + type PointerEvent as ReactPointerEvent, type Ref, } from "react"; import type { DOMProps } from "expo/dom"; @@ -63,6 +64,12 @@ const HOST_DIV_STYLE: CSSProperties = { paddingRight: 0, }; +interface TerminalTouchFocus { + pointerId: number; + x: number; + y: number; +} + function buildXtermThemeKey(theme: ITheme): string { const values: Array = [ theme.background, @@ -553,10 +560,46 @@ export default function TerminalEmulator({ }); }, []); - const handleRootPointerDown = useCallback(() => { - onFocus?.(); - runtimeRef.current?.focus(); - }, [onFocus]); + const touchFocusRef = useRef(null); + + const handleRootPointerDown = useCallback( + (event: ReactPointerEvent) => { + onFocus?.(); + touchFocusRef.current = null; + if (event.pointerType !== "touch") { + runtimeRef.current?.focus(); + return; + } + // 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, + }; + } + }, + [onFocus], + ); + + const handleRootPointerMove = useCallback((event: ReactPointerEvent) => { + const touch = touchFocusRef.current; + if (touch?.pointerId !== event.pointerId) return; + if (Math.hypot(event.clientX - touch.x, event.clientY - touch.y) > 8) { + touchFocusRef.current = null; + runtimeRef.current?.suppressAutomaticKeyboard(); + } + }, []); + + 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; + }, []); const handleRootContextMenu = useCallback( (event: ReactMouseEvent) => { @@ -588,6 +631,9 @@ export default function TerminalEmulator({ data-terminal-scrollbar-root="true" style={rootDivStyle} onPointerDown={handleRootPointerDown} + onPointerMove={handleRootPointerMove} + onPointerUp={handleRootPointerUp} + onPointerCancel={handleRootPointerCancel} onContextMenu={handleRootContextMenu} >
diff --git a/packages/app/src/terminal/runtime/terminal-emulator-runtime.ts b/packages/app/src/terminal/runtime/terminal-emulator-runtime.ts index 3aaf1e139..f538c39bc 100644 --- a/packages/app/src/terminal/runtime/terminal-emulator-runtime.ts +++ b/packages/app/src/terminal/runtime/terminal-emulator-runtime.ts @@ -1053,12 +1053,19 @@ export class TerminalEmulatorRuntime { if (!terminal) { return; } + terminal.textarea?.removeAttribute("virtualkeyboardpolicy"); if (input?.forceRefocus) { terminal.blur(); } terminal.focus(); } + suppressAutomaticKeyboard(): void { + // Android Chrome can reopen the keyboard during a drag after Back leaves the input + // focused. Manual policy prevents that without closing an already open keyboard. + this.terminal?.textarea?.setAttribute("virtualkeyboardpolicy", "manual"); + } + blur(): void { this.terminal?.blur(); }