Skip to content

Commit bb62b06

Browse files
authored
[Web] Fix view relative coordinates of trackpad pan events (#4531)
## Description `WheelEventManager` synthesizes a moving pointer out of a stationary cursor by accumulating wheel deltas, but only the page coordinates use the accumulator: ```js this.wheelDelta.x += event.deltaX; // wheelCallback x: event.clientX + this.wheelDelta.x, // accumulated offsetX: event.offsetX - event.deltaX, // one event, opposite sign ``` Repro: a Pan with `enableTrackpadTwoFingerGesture` on web, three trackpad frames of `deltaY: 10` from `clientY: 0`, `offsetY: 0`. `absoluteY` reads 10, 20, 30 while `y` reads -10, -10, -10. `y` points the wrong way and never advances for the whole gesture. `translationY`, `velocityY` and `absoluteY` are fine, they come off the page coordinates. `x`/`y` and `absoluteX`/`absoluteY` are the same point in two spaces, so they can only differ by the view's page offset. Every other producer keeps that invariant: `PointerEventManager.ts:230` derives `offsetX` from the same `clientX` it writes to `x`, `KeyboardEventManager.ts:120-129` writes the view centre into both, `ScrollEventManager.ts:26-29` writes one value twice, and `GestureHandlerButton.web.tsx:51-53` computes `x` from `absoluteX`. `WheelEventManager.ts:35-36` is the only place where the two move apart. The fix, as suggested in review, derives the view relative position from the accumulated page position the same way `PointerEventManager` does: `(x - rect.left) / scaleX`, with `getEffectiveBoundingRect` and `calculateViewScale` on the handler's view. This also stops relying on `event.offsetX`, which is relative to `event.target` rather than to the handler's view, and is in the target's untransformed space. With the v3 detector the view is the `display: contents` wrapper, so the target is always one of its descendants. The cost is one `getBoundingClientRect` per wheel event, the same read `PointerEventManager` does per pointer move. `onWheel` is overridden only by `PanGestureHandler`, and the wheel `offsetX`/`offsetY` reaches exactly one consumer, `PointerTracker.relativeCoords` -> `GestureHandler.transformNativeEvent()`. ## Test plan `yarn workspace react-native-gesture-handler test` - 182 passed, was 178. Merged onto current `main` it is 189, was 185. The `PanGestureHandler trackpad coordinates` cases drive the real `WheelEventManager` off a fake view with a mocked `getBoundingClientRect` and computed style, and assert `x`/`y` against `absoluteX`/`absoluteY` for: - a view at the page origin, - a view at (40, 60), - a wheel over a child of that view, where `offsetX`/`offsetY` are relative to the child, - a view scaled 2x by its transform. On main all four fail, the first one reports `x: -5, y: -10` against `absoluteX: 15, absoluteY: 30`. The previous revision of this PR (`event.offsetX` plus the accumulated delta) passed the first two but reported `x: 25, y: 40` instead of `55, 70` for the child and `x: 20, y: 40` instead of `12.5, 25` for the scaled view. `ts-check`, `lint-js` and `circular-dependency-check` are clean.
1 parent 736318e commit bb62b06

2 files changed

Lines changed: 110 additions & 5 deletions

File tree

‎packages/react-native-gesture-handler/src/web/handlers/__tests__/PanGestureHandler.test.ts‎

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class TestPanGestureHandler extends PanGestureHandler {
1717
this.onWheel(event);
1818
}
1919

20+
public nativeEvent() {
21+
return this.transformNativeEvent();
22+
}
23+
2024
protected override onWheel(event: AdaptedEvent): void {
2125
this.wheelEvents.push(event);
2226
super.onWheel(event);
@@ -83,8 +87,27 @@ afterEach(() => {
8387
});
8488

8589
class FakeView {
90+
public readonly style = {};
91+
public readonly children = [];
92+
public readonly computedStyle: Record<string, string>;
8693
private readonly listeners = new Map<string, Set<(event: unknown) => void>>();
8794

95+
constructor(
96+
private readonly left = 0,
97+
private readonly top = 0,
98+
scale = 1
99+
) {
100+
this.computedStyle = {
101+
display: 'block',
102+
scale: 'none',
103+
transform: `matrix(${scale}, 0, 0, ${scale}, 0, 0)`,
104+
};
105+
}
106+
107+
public getBoundingClientRect() {
108+
return { left: this.left, top: this.top };
109+
}
110+
88111
public addEventListener(type: string, listener: (event: unknown) => void) {
89112
const listeners = this.listeners.get(type) ?? new Set();
90113
listeners.add(listener);
@@ -95,7 +118,7 @@ class FakeView {
95118
this.listeners.get(type)?.delete(listener);
96119
}
97120

98-
public wheel(deltaY: number): void {
121+
public wheel(deltaY: number, event: Partial<WheelEvent> = {}): void {
99122
this.listeners.get('wheel')?.forEach((listener) =>
100123
listener({
101124
clientX: 0,
@@ -107,11 +130,23 @@ class FakeView {
107130
timeStamp: 0,
108131
// Not a multiple of 120, so the wheel is recognized as a touchpad.
109132
wheelDeltaY: 13,
133+
...event,
110134
})
111135
);
112136
}
113137
}
114138

139+
// The Jest environment is node, the view bounds are read through
140+
// getComputedStyle.
141+
beforeAll(() => {
142+
(globalThis as Record<string, unknown>).getComputedStyle = (view: FakeView) =>
143+
view.computedStyle;
144+
});
145+
146+
afterAll(() => {
147+
delete (globalThis as Record<string, unknown>).getComputedStyle;
148+
});
149+
115150
describe('PanGestureHandler config reset', () => {
116151
test('a config without enableTrackpadTwoFingerGesture restores the disabled default', () => {
117152
const handler = createHandler();
@@ -177,3 +212,66 @@ describe('PanGestureHandler trackpad gesture end', () => {
177212
expect(handler.wheelEvents[1].y).toBe(30);
178213
});
179214
});
215+
216+
describe('PanGestureHandler trackpad coordinates', () => {
217+
beforeEach(() => {
218+
jest.useFakeTimers();
219+
});
220+
221+
afterEach(() => {
222+
jest.clearAllTimers();
223+
jest.useRealTimers();
224+
});
225+
226+
function trackpadPan(view: FakeView, event: Partial<WheelEvent> = {}) {
227+
const manager = new WheelEventManager(view as unknown as HTMLElement);
228+
const handler = createHandler([manager]);
229+
230+
handler.setGestureConfig({
231+
enabled: true,
232+
enableTrackpadTwoFingerGesture: true,
233+
});
234+
handler.attachEventManager(manager);
235+
236+
view.wheel(10, { deltaX: 5, ...event });
237+
view.wheel(10, { deltaX: 5, ...event });
238+
view.wheel(10, { deltaX: 5, ...event });
239+
240+
return handler.nativeEvent();
241+
}
242+
243+
test('a view at the page origin', () => {
244+
expect(trackpadPan(new FakeView())).toMatchObject({
245+
x: 15,
246+
y: 30,
247+
absoluteX: 15,
248+
absoluteY: 30,
249+
});
250+
});
251+
252+
test('a view away from the page origin', () => {
253+
const view = new FakeView(40, 60);
254+
255+
expect(
256+
trackpadPan(view, { clientX: 50, clientY: 80, offsetX: 10, offsetY: 20 })
257+
).toMatchObject({ x: 25, y: 50, absoluteX: 65, absoluteY: 110 });
258+
});
259+
260+
test('a wheel over a child of the view', () => {
261+
const view = new FakeView(40, 60);
262+
263+
// The child sits at (70, 90), offsetX and offsetY are relative to it.
264+
expect(
265+
trackpadPan(view, { clientX: 80, clientY: 100, offsetX: 10, offsetY: 10 })
266+
).toMatchObject({ x: 55, y: 70, absoluteX: 95, absoluteY: 130 });
267+
});
268+
269+
test('a scaled view', () => {
270+
const view = new FakeView(40, 60, 2);
271+
272+
// offsetX and offsetY are in the untransformed space of the view.
273+
expect(
274+
trackpadPan(view, { clientX: 50, clientY: 80, offsetX: 5, offsetY: 10 })
275+
).toMatchObject({ x: 12.5, y: 25, absoluteX: 65, absoluteY: 110 });
276+
});
277+
});

‎packages/react-native-gesture-handler/src/web/tools/WheelEventManager.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { PointerType } from '../../PointerType';
22
import type { AdaptedEvent } from '../interfaces';
33
import { EventTypes } from '../interfaces';
4+
import { calculateViewScale, getEffectiveBoundingRect } from '../utils';
45
import EventManager from './EventManager';
56

67
export default class WheelEventManager extends EventManager<HTMLElement> {
@@ -29,11 +30,17 @@ export default class WheelEventManager extends EventManager<HTMLElement> {
2930
}
3031

3132
protected mapEvent(event: WheelEvent): AdaptedEvent {
33+
const rect = getEffectiveBoundingRect(this.view);
34+
const { scaleX, scaleY } = calculateViewScale(this.view);
35+
36+
const x = event.clientX + this.wheelDelta.x;
37+
const y = event.clientY + this.wheelDelta.y;
38+
3239
return {
33-
x: event.clientX + this.wheelDelta.x,
34-
y: event.clientY + this.wheelDelta.y,
35-
offsetX: event.offsetX - event.deltaX,
36-
offsetY: event.offsetY - event.deltaY,
40+
x,
41+
y,
42+
offsetX: (x - rect.left) / scaleX,
43+
offsetY: (y - rect.top) / scaleY,
3744
pointerId: -1,
3845
eventType: EventTypes.MOVE,
3946
pointerType: PointerType.OTHER,

0 commit comments

Comments
 (0)