Repository navigation
Commit bb62b06
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__
- tools
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
20 | 24 | | |
21 | 25 | | |
22 | 26 | | |
| |||
83 | 87 | | |
84 | 88 | | |
85 | 89 | | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
86 | 93 | | |
87 | 94 | | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
88 | 111 | | |
89 | 112 | | |
90 | 113 | | |
| |||
95 | 118 | | |
96 | 119 | | |
97 | 120 | | |
98 | | - | |
| 121 | + | |
99 | 122 | | |
100 | 123 | | |
101 | 124 | | |
| |||
107 | 130 | | |
108 | 131 | | |
109 | 132 | | |
| 133 | + | |
110 | 134 | | |
111 | 135 | | |
112 | 136 | | |
113 | 137 | | |
114 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
115 | 150 | | |
116 | 151 | | |
117 | 152 | | |
| |||
177 | 212 | | |
178 | 213 | | |
179 | 214 | | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
32 | 39 | | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
37 | 44 | | |
38 | 45 | | |
39 | 46 | | |
| |||
0 commit comments