Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mobile - Draggable - Disable multiple touches #40519

Merged
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
55 changes: 42 additions & 13 deletions packages/components/src/draggable/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const Draggable = ( { children, onDragEnd, onDragOver, onDragStart } ) => {
const isPanActive = useSharedValue( false );
const draggingId = useSharedValue( '' );
const panGestureRef = useRef();
const currentFirstTouchId = useSharedValue( null );

const initialPosition = {
x: useSharedValue( 0 ),
Expand Down Expand Up @@ -77,36 +78,59 @@ const Draggable = ( { children, onDragEnd, onDragOver, onDragStart } ) => {
}
);

function getFirstTouchEvent( event ) {
'worklet';

return event.allTouches.find(
( touch ) => touch.id === currentFirstTouchId.value
);
}

const panGesture = Gesture.Pan()
.manualActivation( true )
.onTouchesDown( ( event ) => {
const { x = 0, y = 0 } = event.allTouches[ 0 ];
initialPosition.x.value = x;
initialPosition.y.value = y;
if ( ! currentFirstTouchId.value ) {
const firstEvent = event.allTouches[ 0 ];
const { x = 0, y = 0 } = firstEvent;

currentFirstTouchId.value = firstEvent.id;

initialPosition.x.value = x;
initialPosition.y.value = y;
}
} )
.onTouchesMove( ( _, state ) => {
.onTouchesMove( ( event, state ) => {
if ( ! isPanActive.value && isDragging.value ) {
isPanActive.value = true;
state.activate();
}
} )
.onUpdate( ( event ) => {
lastPosition.x.value = event.x;
lastPosition.y.value = event.y;

if ( onDragOver ) {
onDragOver( event );
if ( isPanActive.value && isDragging.value ) {
const firstEvent = getFirstTouchEvent( event );
fluiddot marked this conversation as resolved.
Show resolved Hide resolved

if ( ! firstEvent ) {
state.end();
return;
Comment on lines +111 to +113
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If other touches happen and the first touch is no longer present, we cancel the dragging gesture.

}

lastPosition.x.value = firstEvent.x;
lastPosition.y.value = firstEvent.y;

if ( onDragOver ) {
onDragOver( firstEvent );
}
}
} )
.onEnd( () => {
currentFirstTouchId.value = null;
isPanActive.value = false;
isDragging.value = false;
} )
.withRef( panGestureRef )
.shouldCancelWhenOutside( false );

const providerValue = useMemo( () => {
return { panGestureRef, isDragging, draggingId };
return { panGestureRef, isDragging, isPanActive, draggingId };
}, [] );

return (
Expand Down Expand Up @@ -143,7 +167,9 @@ const DraggableTrigger = ( {
onLongPress,
onLongPressEnd,
} ) => {
const { panGestureRef, isDragging, draggingId } = useContext( Context );
const { panGestureRef, isDragging, isPanActive, draggingId } = useContext(
Context
);

const gestureHandler = useAnimatedGestureHandler( {
onActive: () => {
Expand All @@ -158,7 +184,10 @@ const DraggableTrigger = ( {
}
},
onEnd: () => {
isDragging.value = false;
if ( ! isPanActive.value ) {
geriux marked this conversation as resolved.
Show resolved Hide resolved
isDragging.value = false;
}

if ( onLongPressEnd ) {
runOnJS( onLongPressEnd )( id );
}
Expand Down