From ca9027f46638823555b4e46663accdedcc8398a1 Mon Sep 17 00:00:00 2001 From: shihuili1218 Date: Sat, 14 Nov 2026 03:40:47 +0000 Subject: [PATCH] fix(terminal): keep touch selection free of custom context menus --- frontend/lib/components/TerminalPane.svelte | 2 + .../app-shell-context-menu.dom.test.ts | 84 +++++++++++++++++++ .../terminal-block-tint.dom.test.ts | 27 ++++++ .../terminal-context-menu.dom.test.ts | 14 ++++ frontend/lib/input.test.ts | 18 ++++ frontend/lib/input.ts | 6 ++ 6 files changed, 151 insertions(+) create mode 100644 frontend/lib/components/app-shell-context-menu.dom.test.ts diff --git a/frontend/lib/components/TerminalPane.svelte b/frontend/lib/components/TerminalPane.svelte index af05dd52..7fe2ff17 100644 --- a/frontend/lib/components/TerminalPane.svelte +++ b/frontend/lib/components/TerminalPane.svelte @@ -553,6 +553,8 @@ } function openBlockMenu(r: BlockRect, e: MouseEvent) { + // The block gutter sits outside xterm's touch-selection capture guard. + if (isTouchContextMenu(e)) return; e.preventDefault(); e.stopPropagation(); if (!foldStore || !blockTracker) return; diff --git a/frontend/lib/components/app-shell-context-menu.dom.test.ts b/frontend/lib/components/app-shell-context-menu.dom.test.ts new file mode 100644 index 00000000..5c2243a6 --- /dev/null +++ b/frontend/lib/components/app-shell-context-menu.dom.test.ts @@ -0,0 +1,84 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, tick, unmount } from "svelte"; +import AppShell from "./AppShell.svelte"; +import * as app from "../stores/app.svelte.ts"; +import * as layout from "../stores/layout.svelte.ts"; +import { installInputTracking } from "../input.ts"; +import { t } from "../i18n/index.svelte.ts"; + +const invokeMock = vi.hoisted(() => vi.fn()); +vi.mock("@tauri-apps/api/core", () => ({ invoke: invokeMock })); +vi.mock("./TerminalSplitLayout.svelte", async () => import("./test-fixtures/TerminalWorkspace.svelte")); + +const tabId = "ssh:tab-context-menu"; +let component: ReturnType | undefined; +let stopViewport: (() => void) | undefined; +let stopInputTracking: (() => void) | undefined; + +async function settle() { + flushSync(); + await tick(); + flushSync(); +} + +beforeEach(() => { + invokeMock.mockReset().mockImplementation(async (command: string) => { + if (command === "get_setting" || command === "ai_settings_get") return null; + return []; + }); + app.closeSettings(); + app.closeDownloads(); + app.closeSnippetPicker(); + stopInputTracking = installInputTracking(); +}); + +afterEach(async () => { + if (component) await unmount(component); + component = undefined; + stopInputTracking?.(); + stopViewport?.(); + app.closeTab(tabId); + document.body.replaceChildren(); +}); + +describe("Tab context menus remain available independently of terminal menus", () => { + it.each([ + { width: 400, position: "top" as const }, + { width: 1200, position: "left" as const }, + ])("keeps long-press and mouse menus in the $position navigation at $width px", async ({ width, position }) => { + window.innerWidth = width; + stopViewport = layout.observeViewport(); + app.setSidebarPosition(position); + app.addTab({ id: tabId, type: "ssh", label: "Menu test session" }); + const target = document.createElement("div"); + document.body.append(target); + component = mount(AppShell, { target }); + await settle(); + const tab = target.querySelector('button.sb-item[title="Menu test session"]')!; + expect(tab).not.toBeNull(); + + for (const pointerType of ["touch", "pen", "mouse"]) { + // Test both current PointerEvent context menus and older WebViews that + // send MouseEvent after a touch/pen pointerdown. + for (const legacy of [false, true]) { + tab.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, pointerType })); + const init = { bubbles: true, cancelable: true, button: 2 }; + const event = legacy + ? new MouseEvent("contextmenu", init) + : new PointerEvent("contextmenu", { ...init, pointerType }); + tab.dispatchEvent(event); + await settle(); + + expect(event.defaultPrevented).toBe(true); + const labels = [...target.querySelectorAll(".ctx-menu .ctx-label")].map((item) => item.textContent); + expect(labels).toContain(t("tab.context.close")); + expect(labels).toContain(t("tab.context.close_others")); + expect(labels).toContain(t("tab.context.clone")); + + target.querySelector(".ctx-backdrop")!.click(); + await settle(); + expect(target.querySelector(".ctx-menu")).toBeNull(); + } + } + }); +}); diff --git a/frontend/lib/components/terminal-block-tint.dom.test.ts b/frontend/lib/components/terminal-block-tint.dom.test.ts index 2b7be865..f216f713 100644 --- a/frontend/lib/components/terminal-block-tint.dom.test.ts +++ b/frontend/lib/components/terminal-block-tint.dom.test.ts @@ -6,6 +6,7 @@ import * as app from "../stores/app.svelte.ts"; import * as theme from "../themes/store.svelte.ts"; import * as runtime from "../stores/runtime.svelte.ts"; import { setLocale } from "../i18n/index.svelte.ts"; +import { installInputTracking } from "../input.ts"; const invokeMock = vi.hoisted(() => vi.fn()); vi.mock("@tauri-apps/api/core", () => ({ invoke: invokeMock })); @@ -19,6 +20,7 @@ const tabId = "local:block-tint-test"; let component: ReturnType | undefined; let terminal: Terminal; let target: HTMLDivElement; +let stopInputTracking: () => void; async function settle() { flushSync(); @@ -51,6 +53,7 @@ async function queryColors() { } beforeEach(async () => { + stopInputTracking = installInputTracking(); invokeMock.mockReset().mockImplementation(async (command, args) => { if (command === "pty_spawn") return args.sessionId; if (command === "list_highlights") return []; @@ -94,6 +97,7 @@ beforeEach(async () => { }); afterEach(async () => { + stopInputTracking(); if (component) await unmount(component); component = undefined; app.closeTab(tabId); @@ -101,6 +105,29 @@ afterEach(async () => { vi.restoreAllMocks(); }); +it.each(["touch", "pen"])("leaves %s long-press selection available on command blocks without opening a menu", async (pointerType) => { + const block = target.querySelector(".block-hit")!; + block.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, pointerType })); + const events = [ + new PointerEvent("contextmenu", { bubbles: true, cancelable: true, pointerType }), + new MouseEvent("contextmenu", { bubbles: true, cancelable: true }), + ]; + for (const event of events) { + block.dispatchEvent(event); + await settle(); + expect(target.querySelector(".block-menu")).toBeNull(); + expect(event.defaultPrevented).toBe(false); + } + + // A connected mouse still has access to the same block actions. + block.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, pointerType: "mouse" })); + const mouseMenu = new MouseEvent("contextmenu", { bubbles: true, cancelable: true, button: 2 }); + block.dispatchEvent(mouseMenu); + await settle(); + expect(target.querySelector(".block-menu")).not.toBeNull(); + expect(mouseMenu.defaultPrevented).toBe(true); +}); + it("preserves application OSC colors when coloring and uncoloring a block", async () => { await write("\x1b]4;1;#123456\x07\x1b]10;#234567\x07\x1b]11;#345678\x07\x1b]12;#456789\x07"); const colors = await queryColors(); diff --git a/frontend/lib/components/terminal-context-menu.dom.test.ts b/frontend/lib/components/terminal-context-menu.dom.test.ts index b2b434f0..5520c7a7 100644 --- a/frontend/lib/components/terminal-context-menu.dom.test.ts +++ b/frontend/lib/components/terminal-context-menu.dom.test.ts @@ -73,6 +73,20 @@ function setup({ } describe("terminal native touch context menu", () => { + it("preserves selection for touch-generated compatibility mouse events", () => { + const ui = setup({ action: "paste" }); + const event = new PointerEvent("contextmenu", { + bubbles: true, cancelable: true, pointerType: "mouse", button: 2, + }); + Object.defineProperty(event, "sourceCapabilities", { value: { firesTouchEvents: true } }); + ui.row.dispatchEvent(event); + + expect(event.defaultPrevented).toBe(false); + expect(ui.xtermHandler).not.toHaveBeenCalled(); + expect(ui.readClipboard).not.toHaveBeenCalled(); + expect(ui.copySelection).not.toHaveBeenCalled(); + }); + it.each(["touch", "pen"])("keeps the %s native menu without running xterm or clipboard actions", (pointerType) => { for (const action of ["menu", "paste", "copyPaste"] as const) { const ui = setup({ action, selected: true }); diff --git a/frontend/lib/input.test.ts b/frontend/lib/input.test.ts index c985bf25..310ee61a 100644 --- a/frontend/lib/input.test.ts +++ b/frontend/lib/input.test.ts @@ -27,6 +27,24 @@ describe("context menu source", () => { expect(isTouchContextMenu({ pointerType: "mouse" } as PointerEvent)).toBe(false); }); + it("recognizes touch-generated compatibility mouse events", () => { + expect(isTouchContextMenu({ + pointerType: "mouse", sourceCapabilities: { firesTouchEvents: true }, + } as unknown as MouseEvent)).toBe(true); + expect(isTouchContextMenu({ + sourceCapabilities: { firesTouchEvents: true }, + } as unknown as MouseEvent)).toBe(true); + expect(isTouchContextMenu({ + pointerType: "mouse", sourceCapabilities: { firesTouchEvents: false }, + } as unknown as MouseEvent)).toBe(false); + expect(isTouchContextMenu({ + pointerType: "pen", sourceCapabilities: { firesTouchEvents: false }, + } as unknown as MouseEvent)).toBe(true); + expect(isTouchContextMenu({ + pointerType: "touch", sourceCapabilities: null, + } as unknown as MouseEvent)).toBe(true); + }); + it("tracks mixed input for older WebViews and clears touch after a keyboard event", () => { const target = new EventTarget(); const cleanup = installInputTracking(target as Document); diff --git a/frontend/lib/input.ts b/frontend/lib/input.ts index 75e128f1..d43caf69 100644 --- a/frontend/lib/input.ts +++ b/frontend/lib/input.ts @@ -21,6 +21,12 @@ export function installInputTracking(target: Document = document): () => void { } export function isTouchContextMenu(event: MouseEvent): boolean { + // Chromium may expose a touch-generated compatibility event as a mouse + // event. Prefer its explicit touch source without blocking a real mouse. + const sourceCapabilities = (event as MouseEvent & { + sourceCapabilities?: { firesTouchEvents: boolean } | null; + }).sourceCapabilities; + if (sourceCapabilities?.firesTouchEvents === true) return true; const pointerType = (event as PointerEvent).pointerType || lastPointerType; return pointerType === "touch" || pointerType === "pen"; }