Skip to content
TypeScript

fix(web): ignore right-clicks in the flat Device viewport (#17020)

Sebastián Malukdfc093ab9b03
diff --git a/apps/web/src/components/device/DeviceStreamView.browser.test.tsx b/apps/web/src/components/device/DeviceStreamView.browser.test.tsx
index 843849df0ff..c6229faaf44 100644
--- a/apps/web/src/components/device/DeviceStreamView.browser.test.tsx
+++ b/apps/web/src/components/device/DeviceStreamView.browser.test.tsx
@@ -110,6 +110,7 @@ afterEach(async () => {
});
async function setup(h264 = false) {
+ const touches: unknown[] = [];
vi.useFakeTimers();
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
let videoBody: ReadableStreamDefaultController<Uint8Array> | undefined;
@@ -168,7 +169,11 @@ async function setup(h264 = false) {
sockets.push(this);
}
onmessage?: (event: { data: ArrayBuffer }) => void;
- send() {}
+ send(packet: Uint8Array) {
+ if (packet[0] === 0x03) {
+ touches.push(JSON.parse(new TextDecoder().decode(packet.subarray(1))));
+ }
+ }
close() {}
},
);
@@ -186,6 +191,7 @@ async function setup(h264 = false) {
return {
images,
view,
+ touches,
configure() {
const json = new TextEncoder().encode(
JSON.stringify({ width: 400, height: 800, orientation: "portrait" }),
@@ -213,6 +219,51 @@ const reconnect = () =>
.click(),
);
+it.each([
+ { name: "middle", button: 1, buttons: 4 },
+ { name: "right", button: 2, buttons: 2 },
+])("ignores $name-button drags and forwards the next primary drag", async ({ button, buttons }) => {
+ const { touches, configure } = await setup();
+ await act(async () => configure());
+ const viewport = container.querySelector("canvas")!.parentElement!;
+ viewport.style.width = "400px";
+ viewport.style.height = "800px";
+ // Synthetic pointer events do not register an active browser pointer to capture.
+ viewport.setPointerCapture = () => {};
+ const rect = viewport.getBoundingClientRect();
+ const pointer = (type: string, button: number, buttons: number, y: number) =>
+ viewport.dispatchEvent(
+ new PointerEvent(type, {
+ bubbles: true,
+ pointerId: 1,
+ pointerType: "mouse",
+ button,
+ buttons,
+ clientX: rect.left + 100,
+ clientY: rect.top + y,
+ }),
+ );
+
+ await act(async () => {
+ pointer("pointerdown", button, buttons, 600);
+ pointer("pointermove", -1, buttons, 200);
+ pointer("pointerup", button, 0, 200);
+ });
+ expect(touches).toEqual([]);
+
+ await act(async () => {
+ pointer("pointerdown", 0, 1, 600);
+ pointer("pointermove", -1, 1, 200);
+ pointer("pointerup", 0, 0, 200);
+ pointer("pointermove", -1, 0, 400);
+ });
+ expect(touches).toEqual([
+ { type: "begin", x: 0.25, y: 0.75 },
+ { type: "move", x: 0.25, y: 0.25 },
+ { type: "end", x: 0.25, y: 0.25 },
+ ]);
+});
+
it("removes MJPEG requests while hidden and reconnects when shown", async () => {
const { images, view } = await setup();
expect(images[0]!.src).toContain("stream.mjpeg");
diff --git a/apps/web/src/components/device/DeviceStreamView.tsx b/apps/web/src/components/device/DeviceStreamView.tsx
index 5b27c1d69a7..299dddb1899 100644
--- a/apps/web/src/components/device/DeviceStreamView.tsx
+++ b/apps/web/src/components/device/DeviceStreamView.tsx
@@ -398,6 +398,7 @@ export function DeviceStreamView(props: {
className={cn("relative select-none", showPhone && "invisible pointer-events-none")}
style={{ width: frame.width, height: frame.height }}
onPointerDown={(event) => {
+ if (event.button !== 0) return;
event.currentTarget.setPointerCapture(event.pointerId);
(event.currentTarget.parentElement as HTMLElement | null)?.focus();
pointerActive.current = true;