Skip to content

Commit

Permalink
Unify touch events callbacks on web with respect to Android (#2923)
Browse files Browse the repository at this point in the history
## Description

As stated in #2907, web shows inconsistent behavior with native
platforms when it comes to touch event callbacks. This PR unifies its
behavior with `Android` by calling `sendTouchEvent` after handler goes
to `BEGAN` state. Previously it wouldn't fail, because transition from
`UNDETERMINED` into `FAIL` is not possible on web.

Fixes #2907.

### `onBegin` and `onFinalize`

We agreed with @j-piasecki that `onFinalize` should be called only when
`onBegin` was called. However, in that specific case (i.e. snippet from
issue) we are not sure if those callbacks should be triggered and it is
still a thing for a discussion.

## Test plan

<details>
<summary> Tested on the following code from issue </summary>

```jsx
import { View } from 'react-native';
import { Gesture, GestureDetector, GestureHandlerRootView } from 'react-native-gesture-handler';

export default function App() {
  const panGesture = Gesture.Pan()
    .manualActivation(true)
    .onTouchesDown((e, stateManager) => {
      stateManager.fail();
      console.log('onTouchesDown, fail')
    })
    .onTouchesUp(() => console.log('onTouchesUp'))
    .onTouchesMove(() => console.log('onTouchesMove'))
    .onBegin(() => console.log('onBegin'))
    .onChange(() => console.log('onChange'))
    .onTouchesCancelled(() => console.log('onTouchesCancelled'))
    .onFinalize(() => console.log('onFinalize'))
    .onStart(() => console.log('onStart'))
    .onUpdate(() => console.log('onUpdate'))
    .onEnd(() => console.log('onEnd'));
	
  return (
    <GestureHandlerRootView style={{ flex: 1 }}>
      <GestureDetector gesture={panGesture}>
        <View style={{ flex: 1, backgroundColor: 'yellow' }} />
      </GestureDetector>
    </GestureHandlerRootView>
  )
}
```

</details>
  • Loading branch information
m-bert authored May 28, 2024
1 parent 1fdd82c commit 6eaf417
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/web/handlers/FlingGestureHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export default class FlingGestureHandler extends GestureHandler {

super.onPointerDown(event);
this.newPointerAction();

this.tryToSendTouchEvent(event);
}

protected onPointerAdd(event: AdaptedEvent): void {
Expand Down
58 changes: 23 additions & 35 deletions src/web/handlers/GestureHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,32 +289,21 @@ export default abstract class GestureHandler implements IGestureHandler {
GestureHandlerOrchestrator.getInstance().cancelMouseAndPenGestures(this);
}

if (this.config.needsPointerData) {
this.sendTouchEvent(event);
}
// TODO: Bring back touch events along with introducing `handleDown` method that will handle handler specific stuff
}
// Adding another pointer to existing ones
protected onPointerAdd(event: AdaptedEvent): void {
if (this.config.needsPointerData) {
this.sendTouchEvent(event);
}
this.tryToSendTouchEvent(event);
}
protected onPointerUp(event: AdaptedEvent): void {
if (this.config.needsPointerData) {
this.sendTouchEvent(event);
}
this.tryToSendTouchEvent(event);
}
// Removing pointer, when there is more than one pointers
protected onPointerRemove(event: AdaptedEvent): void {
if (this.config.needsPointerData) {
this.sendTouchEvent(event);
}
this.tryToSendTouchEvent(event);
}
protected onPointerMove(event: AdaptedEvent): void {
this.tryToSendMoveEvent(false);
if (this.config.needsPointerData) {
this.sendTouchEvent(event);
}
this.tryToSendMoveEvent(false, event);
}
protected onPointerLeave(event: AdaptedEvent): void {
if (this.shouldCancelWhenOutside) {
Expand All @@ -329,43 +318,42 @@ export default abstract class GestureHandler implements IGestureHandler {
return;
}

if (this.config.needsPointerData) {
this.sendTouchEvent(event);
}
this.tryToSendTouchEvent(event);
}
protected onPointerEnter(event: AdaptedEvent): void {
if (this.config.needsPointerData) {
this.sendTouchEvent(event);
}
this.tryToSendTouchEvent(event);
}
protected onPointerCancel(event: AdaptedEvent): void {
if (this.config.needsPointerData) {
this.sendTouchEvent(event);
}
this.tryToSendTouchEvent(event);

this.cancel();
this.reset();
}
protected onPointerOutOfBounds(event: AdaptedEvent): void {
this.tryToSendMoveEvent(true);
if (this.config.needsPointerData) {
this.sendTouchEvent(event);
}
this.tryToSendMoveEvent(true, event);
}
protected onPointerMoveOver(_event: AdaptedEvent): void {
// used only by hover gesture handler atm
}
protected onPointerMoveOut(_event: AdaptedEvent): void {
// used only by hover gesture handler atm
}
private tryToSendMoveEvent(out: boolean): void {
if (
this.enabled &&
this.active &&
(!out || (out && !this.shouldCancelWhenOutside))
) {
private tryToSendMoveEvent(out: boolean, event: AdaptedEvent): void {
if ((out && this.shouldCancelWhenOutside) || !this.enabled) {
return;
}

if (this.active) {
this.sendEvent(this.currentState, this.currentState);
}

this.tryToSendTouchEvent(event);
}

protected tryToSendTouchEvent(event: AdaptedEvent): void {
if (this.config.needsPointerData) {
this.sendTouchEvent(event);
}
}

public sendTouchEvent(event: AdaptedEvent): void {
Expand Down
2 changes: 2 additions & 0 deletions src/web/handlers/LongPressGestureHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export default class LongPressGestureHandler extends GestureHandler {
this.tryBegin(event);
this.tryActivate();
this.checkDistanceFail(event);

this.tryToSendTouchEvent(event);
}

protected onPointerMove(event: AdaptedEvent): void {
Expand Down
2 changes: 2 additions & 0 deletions src/web/handlers/ManualGestureHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export default class ManualGestureHandler extends GestureHandler {
this.tracker.addToTracker(event);
super.onPointerDown(event);
this.begin();

this.tryToSendTouchEvent(event);
}

protected onPointerAdd(event: AdaptedEvent): void {
Expand Down
2 changes: 2 additions & 0 deletions src/web/handlers/NativeViewGestureHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export default class NativeViewGestureHandler extends GestureHandler {
this.tracker.addToTracker(event);
super.onPointerDown(event);
this.newPointerAction();

this.tryToSendTouchEvent(event);
}

protected onPointerAdd(event: AdaptedEvent): void {
Expand Down
4 changes: 3 additions & 1 deletion src/web/handlers/PanGestureHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ export default class PanGestureHandler extends GestureHandler {

this.tryBegin(event);
this.checkBegan();

this.tryToSendTouchEvent(event);
}

protected onPointerAdd(event: AdaptedEvent): void {
Expand Down Expand Up @@ -256,7 +258,6 @@ export default class PanGestureHandler extends GestureHandler {

protected onPointerUp(event: AdaptedEvent): void {
super.onPointerUp(event);

if (this.currentState === State.ACTIVE) {
this.lastX = this.tracker.getLastAvgX();
this.lastY = this.tracker.getLastAvgY();
Expand All @@ -271,6 +272,7 @@ export default class PanGestureHandler extends GestureHandler {
this.fail();
}
}

protected onPointerRemove(event: AdaptedEvent): void {
super.onPointerRemove(event);
this.tracker.removeFromTracker(event.pointerId);
Expand Down
2 changes: 2 additions & 0 deletions src/web/handlers/PinchGestureHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export default class PinchGestureHandler extends GestureHandler {
protected onPointerDown(event: AdaptedEvent): void {
this.tracker.addToTracker(event);
super.onPointerDown(event);

this.tryToSendTouchEvent(event);
}

protected onPointerAdd(event: AdaptedEvent): void {
Expand Down
2 changes: 2 additions & 0 deletions src/web/handlers/RotationGestureHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export default class RotationGestureHandler extends GestureHandler {
protected onPointerDown(event: AdaptedEvent): void {
this.tracker.addToTracker(event);
super.onPointerDown(event);

this.tryToSendTouchEvent(event);
}

protected onPointerAdd(event: AdaptedEvent): void {
Expand Down
2 changes: 2 additions & 0 deletions src/web/handlers/TapGestureHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export default class TapGestureHandler extends GestureHandler {
this.lastY = event.y;

this.updateState(event);

this.tryToSendTouchEvent(event);
}

protected onPointerAdd(event: AdaptedEvent): void {
Expand Down

0 comments on commit 6eaf417

Please sign in to comment.