Skip to content

Commit 875eb59

Browse files
committed
Merge remote-tracking branch 'origin/@mbert/add-event-calculators' into @akwasniewski/v3-examples
2 parents d00f1ef + 04c91fd commit 875eb59

File tree

11 files changed

+149
-61
lines changed

11 files changed

+149
-61
lines changed

packages/react-native-gesture-handler/src/handlers/gestures/reanimatedWrapper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ComponentClass } from 'react';
22
import { tagMessage } from '../../utils';
3-
import { GestureCallbacks, UpdateEvent } from '../../v3/types';
3+
import { GestureCallbacks, GestureUpdateEvent } from '../../v3/types';
44

55
export interface SharedValue<Value = unknown> {
66
value: Value;
@@ -15,7 +15,7 @@ export interface SharedValue<Value = unknown> {
1515
}
1616

1717
export type ReanimatedContext<THandlerData> = {
18-
lastUpdateEvent: UpdateEvent<THandlerData> | undefined;
18+
lastUpdateEvent: GestureUpdateEvent<THandlerData> | undefined;
1919
};
2020

2121
interface WorkletProps {

packages/react-native-gesture-handler/src/v3/hooks/callbacks/stateChangeHandler.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,46 @@
11
import { CALLBACK_TYPE } from '../../../handlers/gestures/gesture';
22
import { State } from '../../../State';
3-
import { GestureCallbacks, StateChangeEvent } from '../../types';
4-
import { isEventForHandlerWithTag, isNativeEvent } from '../utils';
3+
import {
4+
GestureCallbacks,
5+
GestureStateChangeEvent,
6+
StateChangeEvent,
7+
} from '../../types';
8+
import { isEventForHandlerWithTag, maybeExtractNativeEvent } from '../utils';
59
import { runCallback } from '../utils/eventHandlersUtils';
610

711
export function getStateChangeHandler<THandlerData>(
812
handlerTag: number,
913
callbacks: GestureCallbacks<THandlerData>
1014
) {
11-
return (event: StateChangeEvent<THandlerData>) => {
15+
return (sourceEvent: StateChangeEvent<THandlerData>) => {
1216
'worklet';
1317

18+
const event = maybeExtractNativeEvent(
19+
sourceEvent
20+
) as GestureStateChangeEvent<THandlerData>;
21+
1422
if (!isEventForHandlerWithTag(handlerTag, event)) {
1523
return;
1624
}
1725

18-
let oldState: State | undefined;
19-
let state: State | undefined;
20-
21-
if (isNativeEvent(event)) {
22-
oldState = event.nativeEvent.oldState;
23-
state = event.nativeEvent.state;
24-
} else {
25-
oldState = event.oldState;
26-
state = event.state;
27-
}
28-
29-
if (oldState === State.UNDETERMINED && state === State.BEGAN) {
26+
if (event.oldState === State.UNDETERMINED && event.state === State.BEGAN) {
3027
runCallback(CALLBACK_TYPE.BEGAN, callbacks, event);
3128
} else if (
32-
(oldState === State.BEGAN || oldState === State.UNDETERMINED) &&
33-
state === State.ACTIVE
29+
(event.oldState === State.BEGAN ||
30+
event.oldState === State.UNDETERMINED) &&
31+
event.state === State.ACTIVE
3432
) {
3533
runCallback(CALLBACK_TYPE.START, callbacks, event);
36-
} else if (oldState !== state && state === State.END) {
37-
if (oldState === State.ACTIVE) {
34+
} else if (event.oldState !== event.state && event.state === State.END) {
35+
if (event.oldState === State.ACTIVE) {
3836
runCallback(CALLBACK_TYPE.END, callbacks, event, true);
3937
}
4038
runCallback(CALLBACK_TYPE.FINALIZE, callbacks, event, true);
4139
} else if (
42-
(state === State.FAILED || state === State.CANCELLED) &&
43-
state !== oldState
40+
(event.state === State.FAILED || event.state === State.CANCELLED) &&
41+
event.state !== event.oldState
4442
) {
45-
if (oldState === State.ACTIVE) {
43+
if (event.oldState === State.ACTIVE) {
4644
runCallback(CALLBACK_TYPE.END, callbacks, event, false);
4745
}
4846
runCallback(CALLBACK_TYPE.FINALIZE, callbacks, event, false);

packages/react-native-gesture-handler/src/v3/hooks/callbacks/touchEventHandler.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { NativeSyntheticEvent } from 'react-native';
21
import { GestureCallbacks, TouchEvent } from '../../types';
3-
import { isEventForHandlerWithTag, isNativeEvent } from '../utils';
2+
import { isEventForHandlerWithTag, maybeExtractNativeEvent } from '../utils';
43
import { TouchEventType } from '../../../TouchEventType';
54
import { GestureTouchEvent } from '../../../handlers/gestureHandlerCommon';
65
import {
@@ -12,30 +11,18 @@ export function getTouchEventHandler<THandlerData>(
1211
handlerTag: number,
1312
callbacks: GestureCallbacks<THandlerData>
1413
) {
15-
return (event: TouchEvent) => {
14+
return (sourceEvent: TouchEvent) => {
1615
'worklet';
1716

17+
const event = maybeExtractNativeEvent(sourceEvent) as GestureTouchEvent;
18+
1819
if (!isEventForHandlerWithTag(handlerTag, event)) {
1920
return;
2021
}
2122

22-
if (
23-
isNativeEvent(event) &&
24-
event.nativeEvent.eventType !== TouchEventType.UNDETERMINED
25-
) {
26-
runCallback(
27-
touchEventTypeToCallbackType(
28-
(event as NativeSyntheticEvent<GestureTouchEvent>).nativeEvent
29-
.eventType
30-
),
31-
callbacks,
32-
event
33-
);
34-
} else if (
35-
(event as GestureTouchEvent).eventType !== TouchEventType.UNDETERMINED
36-
) {
23+
if (event.eventType !== TouchEventType.UNDETERMINED) {
3724
runCallback(
38-
touchEventTypeToCallbackType((event as GestureTouchEvent).eventType),
25+
touchEventTypeToCallbackType(event.eventType),
3926
callbacks,
4027
event
4128
);

packages/react-native-gesture-handler/src/v3/hooks/callbacks/updateHandler.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { ReanimatedContext } from '../../../handlers/gestures/reanimatedWrapper'
44
import {
55
ChangeCalculatorType,
66
GestureCallbacks,
7+
GestureUpdateEvent,
78
UpdateEvent,
89
} from '../../types';
9-
import { isEventForHandlerWithTag } from '../utils';
10+
import { isEventForHandlerWithTag, maybeExtractNativeEvent } from '../utils';
1011
import { runCallback } from '../utils/eventHandlersUtils';
1112

1213
export function getUpdateHandler<THandlerData>(
@@ -15,9 +16,13 @@ export function getUpdateHandler<THandlerData>(
1516
context: ReanimatedContext<THandlerData> | undefined,
1617
changeEventCalculator?: ChangeCalculatorType<THandlerData>
1718
) {
18-
return (event: UpdateEvent<THandlerData>) => {
19+
return (sourceEvent: UpdateEvent<THandlerData>) => {
1920
'worklet';
2021

22+
const event = maybeExtractNativeEvent(
23+
sourceEvent
24+
) as GestureUpdateEvent<THandlerData>;
25+
2126
if (!isEventForHandlerWithTag(handlerTag, event)) {
2227
return;
2328
}
@@ -36,7 +41,6 @@ export function getUpdateHandler<THandlerData>(
3641
: event
3742
);
3843

39-
// TODO: Investigate why this is always undefined
4044
context.lastUpdateEvent = event;
4145
};
4246
}

packages/react-native-gesture-handler/src/v3/hooks/gestures/useHover.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { HoverEffect } from '../../../handlers/gestures/hoverGesture';
33
import {
44
BaseGestureConfig,
55
ExcludeInternalConfigProps,
6+
HandlerData,
67
SingleGestureName,
78
} from '../../types';
89
import { useGesture } from '../useGesture';
9-
import { cloneConfig } from '../utils';
10+
import { cloneConfig, getChangeEventCalculator } from '../utils';
1011

1112
type HoverGestureProperties = {
1213
/**
@@ -27,6 +28,8 @@ type HoverHandlerData = {
2728
absoluteX: number;
2829
absoluteY: number;
2930
stylusData: StylusData;
31+
changeX: number;
32+
changeY: number;
3033
};
3134

3235
type HoverGestureInternalConfig = BaseGestureConfig<
@@ -37,10 +40,23 @@ type HoverGestureInternalConfig = BaseGestureConfig<
3740
export type HoverGestureConfig =
3841
ExcludeInternalConfigProps<HoverGestureInternalConfig>;
3942

43+
function diffCalculator(
44+
current: HandlerData<HoverHandlerData>,
45+
previous: HandlerData<HoverHandlerData> | null
46+
) {
47+
'worklet';
48+
return {
49+
changeX: previous ? current.x - previous.x : 0,
50+
changeY: previous ? current.y - previous.y : 0,
51+
};
52+
}
53+
4054
export function useHover(config: HoverGestureConfig) {
4155
const hoverConfig = cloneConfig<HoverHandlerData, HoverGestureProperties>(
4256
config
4357
);
4458

59+
hoverConfig.changeEventCalculator = getChangeEventCalculator(diffCalculator);
60+
4561
return useGesture(SingleGestureName.Hover, hoverConfig);
4662
}

packages/react-native-gesture-handler/src/v3/hooks/gestures/usePan.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { StylusData } from '../../../handlers/gestureHandlerCommon';
22
import {
33
BaseGestureConfig,
44
ExcludeInternalConfigProps,
5+
HandlerData,
56
SingleGestureName,
67
} from '../../types';
78
import { useGesture } from '../useGesture';
8-
import { cloneConfig, remapProps } from '../utils';
9+
import { getChangeEventCalculator, remapProps, cloneConfig } from '../utils';
910

1011
type CommonPanGestureProperties = {
1112
/**
@@ -114,6 +115,8 @@ type PanHandlerData = {
114115
velocityX: number;
115116
velocityY: number;
116117
stylusData: StylusData;
118+
changeX: number;
119+
changeY: number;
117120
};
118121

119122
export type PanGestureConfig = ExcludeInternalConfigProps<
@@ -236,6 +239,17 @@ function transformPanProps(
236239
}
237240
}
238241

242+
function diffCalculator(
243+
current: HandlerData<PanHandlerData>,
244+
previous: HandlerData<PanHandlerData> | null
245+
) {
246+
'worklet';
247+
return {
248+
changeX: previous ? current.translationX - previous.translationX : 0,
249+
changeY: previous ? current.translationY - previous.translationY : 0,
250+
};
251+
}
252+
239253
export function usePan(config: PanGestureConfig) {
240254
if (__DEV__) {
241255
validatePanConfig(config);
@@ -252,6 +266,8 @@ export function usePan(config: PanGestureConfig) {
252266

253267
transformPanProps(panConfig);
254268

269+
panConfig.changeEventCalculator = getChangeEventCalculator(diffCalculator);
270+
255271
return useGesture<PanHandlerData, PanGestureInternalProperties>(
256272
SingleGestureName.Pan,
257273
panConfig

packages/react-native-gesture-handler/src/v3/hooks/gestures/usePinch.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {
22
BaseGestureConfig,
33
ExcludeInternalConfigProps,
4+
HandlerData,
45
SingleGestureName,
56
} from '../../types';
67
import { useGesture } from '../useGesture';
7-
import { cloneConfig } from '../utils';
8+
import { cloneConfig, getChangeEventCalculator } from '../utils';
89

910
type PinchGestureProperties = Record<string, never>;
1011

@@ -13,6 +14,7 @@ type PinchHandlerData = {
1314
focalX: number;
1415
focalY: number;
1516
velocity: number;
17+
scaleChange: number;
1618
};
1719

1820
type PinchGestureInternalConfig = BaseGestureConfig<
@@ -23,10 +25,22 @@ type PinchGestureInternalConfig = BaseGestureConfig<
2325
export type PinchGestureConfig =
2426
ExcludeInternalConfigProps<PinchGestureInternalConfig>;
2527

28+
function diffCalculator(
29+
current: HandlerData<PinchHandlerData>,
30+
previous: HandlerData<PinchHandlerData> | null
31+
) {
32+
'worklet';
33+
return {
34+
scaleChange: previous ? current.scale / previous.scale : 0,
35+
};
36+
}
37+
2638
export function usePinch(config: PinchGestureConfig) {
2739
const pinchConfig = cloneConfig<PinchHandlerData, PinchGestureProperties>(
2840
config
2941
);
3042

43+
pinchConfig.changeEventCalculator = getChangeEventCalculator(diffCalculator);
44+
3145
return useGesture(SingleGestureName.Pinch, pinchConfig);
3246
}

packages/react-native-gesture-handler/src/v3/hooks/gestures/useRotation.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {
22
BaseGestureConfig,
33
ExcludeInternalConfigProps,
4+
HandlerData,
45
SingleGestureName,
56
} from '../../types';
67
import { useGesture } from '../useGesture';
7-
import { cloneConfig } from '../utils';
8+
import { cloneConfig, getChangeEventCalculator } from '../utils';
89

910
type RotationGestureProperties = Record<string, never>;
1011

@@ -13,6 +14,7 @@ type RotationHandlerData = {
1314
anchorX: number;
1415
anchorY: number;
1516
velocity: number;
17+
rotationChange: number;
1618
};
1719

1820
type RotationGestureInternalConfig = BaseGestureConfig<
@@ -23,11 +25,24 @@ type RotationGestureInternalConfig = BaseGestureConfig<
2325
export type RotationGestureConfig =
2426
ExcludeInternalConfigProps<RotationGestureInternalConfig>;
2527

28+
function diffCalculator(
29+
current: HandlerData<RotationHandlerData>,
30+
previous: HandlerData<RotationHandlerData> | null
31+
) {
32+
'worklet';
33+
return {
34+
rotationChange: previous ? current.rotation - previous.rotation : 0,
35+
};
36+
}
37+
2638
export function useRotation(config: RotationGestureConfig) {
2739
const rotationConfig = cloneConfig<
2840
RotationHandlerData,
2941
RotationGestureProperties
3042
>(config);
3143

44+
rotationConfig.changeEventCalculator =
45+
getChangeEventCalculator(diffCalculator);
46+
3247
return useGesture(SingleGestureName.Rotation, rotationConfig);
3348
}

0 commit comments

Comments
 (0)