Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions apps/common-app/src/components/switchAndInput/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import type { NativeGestureEvent } from 'react-native-gesture-handler';
import {
TextInput,
LegacyTextInput,
Expand All @@ -22,8 +21,8 @@ export default function SwitchTextInputExample() {
<TextInput
onBegin={() => console.log('[TextInput] onBegin')}
onActivate={() => console.log('[TextInput] onActivate')}
onFinalize={(_: NativeGestureEvent, s: boolean) =>
console.log('[TextInput] onFinalize', s)
onFinalize={(e) =>
console.log('[TextInput] onFinalize', e.canceled)
}
style={styles.input}
placeholder="Type here..."
Expand All @@ -50,9 +49,7 @@ export default function SwitchTextInputExample() {
<View style={styles.switchRow}>
<Switch
onBegin={() => console.log('[Switch] onBegin')}
onFinalize={(_: NativeGestureEvent, s: boolean) =>
console.log('[Switch] onFinalize', s)
}
onFinalize={(e) => console.log('[Switch] onFinalize', e.canceled)}
value={switchOn}
onValueChange={(v: boolean) => {
console.log('[Switch] onValueChange', v);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import type {
RectButtonProps,
} from './GestureButtonsProps';

import type { GestureEvent } from '../types';
import type { GestureEndEvent, GestureEvent } from '../types';
import type { NativeViewHandlerData } from '../hooks/gestures/native/useNativeGesture';

type CallbackEventType = GestureEvent<NativeViewHandlerData>;
type EndCallbackEventType = GestureEndEvent<NativeViewHandlerData>;

export const RawButton = createNativeWrapper(GestureHandlerButton, {
shouldCancelWhenOutside: false,
Expand Down Expand Up @@ -58,15 +59,15 @@ export const BaseButton = (props: BaseButtonProps) => {
}
};

const onDeactivate = (e: CallbackEventType, success: boolean) => {
const onDeactivate = (e: EndCallbackEventType) => {
onActiveStateChange?.(false);

if (success && !longPressDetected.current) {
if (!e.canceled && !longPressDetected.current) {
onPress?.(e.pointerInside);
}
};

const onFinalize = (_e: CallbackEventType) => {
const onFinalize = (_e: EndCallbackEventType) => {
if (longPressTimeout.current !== undefined) {
clearTimeout(longPressTimeout.current);
longPressTimeout.current = undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ReanimatedContext } from '../../../handlers/gestures/reanimatedWrapper'
import {
ChangeCalculatorType,
GestureCallbacks,
GestureEndEvent,
GestureHandlerEventWithHandlerData,
GestureStateChangeEventWithHandlerData,
GestureUpdateEventWithHandlerData,
Expand All @@ -35,23 +36,20 @@ function handleStateChangeEvent<THandlerData>(
state === State.ACTIVE
) {
runCallback(CALLBACK_TYPE.START, callbacks, event);
} else if (oldState !== state && state === State.END) {
if (oldState === State.ACTIVE) {
runCallback(CALLBACK_TYPE.END, callbacks, event, true);
}
runCallback(CALLBACK_TYPE.FINALIZE, callbacks, event, true);

if (context) {
context.lastUpdateEvent = undefined;
}
} else if (
(state === State.FAILED || state === State.CANCELLED) &&
state !== oldState
oldState !== state &&
(state === State.END || state === State.FAILED || state === State.CANCELLED)
) {
const canceled = state === State.FAILED || state === State.CANCELLED;
const endEvent: GestureEndEvent<THandlerData> = {
...event,
canceled,
};

if (oldState === State.ACTIVE) {
runCallback(CALLBACK_TYPE.END, callbacks, event, false);
runCallback(CALLBACK_TYPE.END, callbacks, endEvent);
}
runCallback(CALLBACK_TYPE.FINALIZE, callbacks, event, false);
runCallback(CALLBACK_TYPE.FINALIZE, callbacks, endEvent);

if (context) {
context.lastUpdateEvent = undefined;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { CALLBACK_TYPE } from '../../../handlers/gestures/gesture';
import {
GestureCallbacks,
GestureEventCallback,
GestureEventCallbackWithDidSucceed,
GestureTouchEventCallback,
UnpackedGestureHandlerEvent,
} from '../../types';
import { GestureEndEventCallback } from '../../types/ConfigTypes';

export function useMemoizedGestureCallbacks<THandlerData>(
callbacks: GestureCallbacks<THandlerData>
Expand Down Expand Up @@ -51,7 +51,7 @@ function getHandler<THandlerData>(
callbacks: GestureCallbacks<THandlerData>
):
| GestureEventCallback<THandlerData>
| GestureEventCallbackWithDidSucceed<THandlerData>
| GestureEndEventCallback<THandlerData>
| GestureTouchEventCallback
| undefined {
'worklet';
Expand Down Expand Up @@ -95,13 +95,11 @@ export function touchEventTypeToCallbackType(
}

type SingleParameterCallback<T> = (event: T) => void;
type DoubleParameterCallback<T> = (event: T, didSucceed: boolean) => void;

export function runCallback<THandlerData>(
type: CALLBACK_TYPE,
callbacks: GestureCallbacks<THandlerData>,
event: UnpackedGestureHandlerEvent<THandlerData>,
didSucceed?: boolean
event: UnpackedGestureHandlerEvent<THandlerData>
) {
'worklet';
const handler = getHandler(type, callbacks);
Expand All @@ -110,9 +108,5 @@ export function runCallback<THandlerData>(
return;
}

if (didSucceed === undefined) {
(handler as SingleParameterCallback<typeof event>)(event);
} else {
(handler as DoubleParameterCallback<typeof event>)(event, didSucceed);
}
(handler as SingleParameterCallback<typeof event>)(event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import {
AnimatedEvent,
ChangeCalculatorType,
GestureEndEvent,
GestureEvent,
} from './EventTypes';
import { WithSharedValue } from './ReanimatedTypes';
Expand All @@ -17,18 +18,17 @@ export type GestureEventCallback<THandlerData> = (
event: GestureEvent<THandlerData>
) => void;

export type GestureEventCallbackWithDidSucceed<THandlerData> = (
event: GestureEvent<THandlerData>,
didSucceed: boolean
export type GestureEndEventCallback<THandlerData> = (
event: GestureEndEvent<THandlerData>
) => void;

export type GestureTouchEventCallback = (event: GestureTouchEvent) => void;

export type GestureCallbacks<THandlerData> = {
onBegin?: GestureEventCallback<THandlerData>;
onActivate?: GestureEventCallback<THandlerData>;
onDeactivate?: GestureEventCallbackWithDidSucceed<THandlerData>;
onFinalize?: GestureEventCallbackWithDidSucceed<THandlerData>;
onDeactivate?: GestureEndEventCallback<THandlerData>;
onFinalize?: GestureEndEventCallback<THandlerData>;
onUpdate?: GestureEventCallback<THandlerData> | AnimatedEvent;
onTouchesDown?: GestureTouchEventCallback;
onTouchesMove?: GestureTouchEventCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export type GestureEvent<THandlerData> = {
handlerTag: number;
} & HandlerData<THandlerData>;

export type GestureEndEvent<THandlerData> = {
canceled: boolean;
} & GestureEvent<THandlerData>;

export type UnpackedGestureHandlerEvent<THandlerData> =
| GestureEvent<THandlerData>
| GestureTouchEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type {
StateChangeEventWithHandlerData,
TouchEvent,
GestureEvent,
GestureEndEvent,
AnimatedEvent,
ChangeCalculatorType,
DiffCalculatorType,
Expand All @@ -23,7 +24,6 @@ export type {
export type {
GestureCallbacks,
GestureEventCallback,
GestureEventCallbackWithDidSucceed,
GestureTouchEventCallback,
GestureRelations,
InternalConfigProps,
Expand Down
Loading