Skip to content

Commit

Permalink
Add pointerType to event (#2760)
Browse files Browse the repository at this point in the history
## Description

This PR adds `pointerType` field to event in callbacks so that users can distinguish which pointer is used (for example `finger` or `stylus`).

## Test plan

Tested on example app ("PointerType" example).
  • Loading branch information
m-bert authored Feb 29, 2024
1 parent d55d8de commit 04e7898
Show file tree
Hide file tree
Showing 29 changed files with 354 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
protected var orchestrator: GestureHandlerOrchestrator? = null
private var onTouchEventListener: OnTouchEventListener? = null
private var interactionController: GestureHandlerInteractionController? = null
var pointerType: Int = POINTER_TYPE_OTHER
private set

protected var mouseButton = 0

Expand Down Expand Up @@ -378,6 +380,11 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
lastAbsolutePositionY = GestureUtils.getLastPointerY(adaptedTransformedEvent, true)
lastEventOffsetX = adaptedTransformedEvent.rawX - adaptedTransformedEvent.x
lastEventOffsetY = adaptedTransformedEvent.rawY - adaptedTransformedEvent.y

if (sourceEvent.action == MotionEvent.ACTION_DOWN || sourceEvent.action == MotionEvent.ACTION_HOVER_ENTER || sourceEvent.action == MotionEvent.ACTION_HOVER_MOVE) {
setPointerType(sourceEvent)
}

if (sourceEvent.action == MotionEvent.ACTION_HOVER_ENTER ||
sourceEvent.action == MotionEvent.ACTION_HOVER_MOVE ||
sourceEvent.action == MotionEvent.ACTION_HOVER_EXIT
Expand Down Expand Up @@ -768,6 +775,17 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
isWithinBounds = false
}

private fun setPointerType(event: MotionEvent) {
val pointerIndex = event.actionIndex

pointerType = when (event.getToolType(pointerIndex)) {
MotionEvent.TOOL_TYPE_FINGER -> POINTER_TYPE_TOUCH
MotionEvent.TOOL_TYPE_STYLUS -> POINTER_TYPE_STYLUS
MotionEvent.TOOL_TYPE_MOUSE -> POINTER_TYPE_MOUSE
else -> POINTER_TYPE_OTHER
}
}

fun setOnTouchEventListener(listener: OnTouchEventListener?): GestureHandler<*> {
onTouchEventListener = listener
return this
Expand Down Expand Up @@ -810,6 +828,10 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
const val ACTION_TYPE_NATIVE_ANIMATED_EVENT = 2
const val ACTION_TYPE_JS_FUNCTION_OLD_API = 3
const val ACTION_TYPE_JS_FUNCTION_NEW_API = 4
const val POINTER_TYPE_TOUCH = 0
const val POINTER_TYPE_STYLUS = 1
const val POINTER_TYPE_MOUSE = 2
const val POINTER_TYPE_OTHER = 3
private const val MAX_POINTERS_COUNT = 12
private lateinit var pointerProps: Array<PointerProperties?>
private lateinit var pointerCoords: Array<PointerCoords?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ abstract class GestureHandlerEventDataBuilder<T : GestureHandler<T>>(handler: T)
private val numberOfPointers: Int
private val handlerTag: Int
private val state: Int
private val pointerType: Int

init {
numberOfPointers = handler.numberOfPointers
handlerTag = handler.tag
state = handler.state
pointerType = handler.pointerType
}

open fun buildEventData(eventData: WritableMap) {
eventData.putInt("numberOfPointers", numberOfPointers)
eventData.putInt("handlerTag", handlerTag)
eventData.putInt("state", state)
eventData.putInt("pointerType", pointerType)
}
}
9 changes: 5 additions & 4 deletions apple/Handlers/RNFlingHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ - (id)initWithGestureHandler:(RNGestureHandler *)gestureHandler

- (void)touchesBegan:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
{
[_gestureHandler setCurrentPointerType:event];
_lastPoint = [[[touches allObjects] objectAtIndex:0] locationInView:_gestureHandler.recognizer.view];
[_gestureHandler reset];
[super touchesBegan:touches withEvent:event];
Expand Down Expand Up @@ -141,16 +142,16 @@ - (RNGestureHandlerEventExtraData *)eventExtraData:(id)_recognizer

RNBetterSwipeGestureRecognizer *recognizer = (RNBetterSwipeGestureRecognizer *)_recognizer;

CGPoint viewAbsolutePosition =
[recognizer.view convertPoint:recognizer.view.bounds.origin
toView:RCTKeyWindow().rootViewController.view];
CGPoint viewAbsolutePosition = [recognizer.view convertPoint:recognizer.view.bounds.origin
toView:RCTKeyWindow().rootViewController.view];
CGPoint locationInView = [recognizer getLastLocation];

return [RNGestureHandlerEventExtraData
forPosition:locationInView
withAbsolutePosition:CGPointMake(
viewAbsolutePosition.x + locationInView.x, viewAbsolutePosition.y + locationInView.y)
withNumberOfTouches:recognizer.numberOfTouches];
withNumberOfTouches:recognizer.numberOfTouches
withPointerType:_pointerType];
}
@end

Expand Down
4 changes: 3 additions & 1 deletion apple/Handlers/RNForceTouchHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ - (id)initWithGestureHandler:(RNGestureHandler *)gestureHandler

- (void)touchesBegan:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
{
[_gestureHandler setCurrentPointerType:event];
if (_firstTouch) {
// ignore rest of fingers
return;
Expand Down Expand Up @@ -171,7 +172,8 @@ - (RNGestureHandlerEventExtraData *)eventExtraData:(RNForceTouchGestureRecognize
return [RNGestureHandlerEventExtraData forForce:recognizer.force
forPosition:[recognizer locationInView:recognizer.view]
withAbsolutePosition:[recognizer locationInView:recognizer.view.window]
withNumberOfTouches:recognizer.numberOfTouches];
withNumberOfTouches:recognizer.numberOfTouches
withPointerType:_pointerType];
}

@end
Expand Down
3 changes: 2 additions & 1 deletion apple/Handlers/RNHoverHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ - (void)configure:(NSDictionary *)config
- (RNGestureHandlerEventExtraData *)eventExtraData:(UIGestureRecognizer *)recognizer
{
return [RNGestureHandlerEventExtraData forPosition:[recognizer locationInView:recognizer.view]
withAbsolutePosition:[recognizer locationInView:recognizer.view.window]];
withAbsolutePosition:[recognizer locationInView:recognizer.view.window]
withPointerType:UITouchTypePencil];
}

@end
Expand Down
4 changes: 3 additions & 1 deletion apple/Handlers/RNLongPressHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ - (CGPoint)translationInView

- (void)touchesBegan:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
{
[_gestureHandler setCurrentPointerType:event];
[super touchesBegan:touches withEvent:event];
[_gestureHandler.pointerTracker touchesBegan:touches withEvent:event];

Expand Down Expand Up @@ -181,7 +182,8 @@ - (RNGestureHandlerEventExtraData *)eventExtraData:(UIGestureRecognizer *)recogn
return [RNGestureHandlerEventExtraData forPosition:[recognizer locationInView:recognizer.view]
withAbsolutePosition:[recognizer locationInView:recognizer.view.window]
withNumberOfTouches:recognizer.numberOfTouches
withDuration:[(RNBetterLongPressGestureRecognizer *)recognizer getDuration]];
withDuration:[(RNBetterLongPressGestureRecognizer *)recognizer getDuration]
withPointerType:_pointerType];
}
@end

Expand Down
1 change: 1 addition & 0 deletions apple/Handlers/RNManualHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ - (id)initWithGestureHandler:(RNGestureHandler *)gestureHandler

- (void)touchesBegan:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
{
[_gestureHandler setCurrentPointerType:event];
[super touchesBegan:touches withEvent:event];
[_gestureHandler.pointerTracker touchesBegan:touches withEvent:event];

Expand Down
16 changes: 9 additions & 7 deletions apple/Handlers/RNNativeViewHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ - (id)initWithGestureHandler:(RNGestureHandler *)gestureHandler

- (void)touchesBegan:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
{
[_gestureHandler setCurrentPointerType:event];
[_gestureHandler.pointerTracker touchesBegan:touches withEvent:event];
}

Expand Down Expand Up @@ -133,6 +134,7 @@ - (void)bindToView:(UIView *)view

- (void)handleTouchDown:(UIView *)sender forEvent:(UIEvent *)event
{
[self setCurrentPointerType:event];
[self reset];

if (_disallowInterruption) {
Expand All @@ -148,21 +150,21 @@ - (void)handleTouchDown:(UIView *)sender forEvent:(UIEvent *)event

[self sendEventsInState:RNGestureHandlerStateActive
forViewWithTag:sender.reactTag
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:YES]];
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:YES withPointerType:_pointerType]];
}

- (void)handleTouchUpOutside:(UIView *)sender forEvent:(UIEvent *)event
{
[self sendEventsInState:RNGestureHandlerStateEnd
forViewWithTag:sender.reactTag
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:NO]];
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:NO withPointerType:_pointerType]];
}

- (void)handleTouchUpInside:(UIView *)sender forEvent:(UIEvent *)event
{
[self sendEventsInState:RNGestureHandlerStateEnd
forViewWithTag:sender.reactTag
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:YES]];
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:YES withPointerType:_pointerType]];
}

- (void)handleDragExit:(UIView *)sender forEvent:(UIEvent *)event
Expand All @@ -173,26 +175,26 @@ - (void)handleDragExit:(UIView *)sender forEvent:(UIEvent *)event
[control cancelTrackingWithEvent:event];
[self sendEventsInState:RNGestureHandlerStateEnd
forViewWithTag:sender.reactTag
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:NO]];
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:NO withPointerType:_pointerType]];
} else {
[self sendEventsInState:RNGestureHandlerStateActive
forViewWithTag:sender.reactTag
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:NO]];
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:NO withPointerType:_pointerType]];
}
}

- (void)handleDragEnter:(UIView *)sender forEvent:(UIEvent *)event
{
[self sendEventsInState:RNGestureHandlerStateActive
forViewWithTag:sender.reactTag
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:YES]];
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:YES withPointerType:_pointerType]];
}

- (void)handleTouchCancel:(UIView *)sender forEvent:(UIEvent *)event
{
[self sendEventsInState:RNGestureHandlerStateCancelled
forViewWithTag:sender.reactTag
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:NO]];
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:NO withPointerType:_pointerType]];
}

@end
Expand Down
9 changes: 7 additions & 2 deletions apple/Handlers/RNPanHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "RNPanHandler.h"

#if TARGET_OS_OSX

@interface RNBetterPanGestureRecognizer : NSPanGestureRecognizer
#else
#import <UIKit/UIGestureRecognizerSubclass.h>
Expand Down Expand Up @@ -166,6 +167,7 @@ - (void)interactionsCancelled:(NSSet *)touches withEvent:(UIEvent *)event

- (void)mouseDown:(NSEvent *)event
{
[_gestureHandler setCurrentPointerTypeToMouse];
// super call was moved to interactionsBegan method to keep the
// original order of calls
[self interactionsBegan:[NSSet setWithObject:event] withEvent:event];
Expand All @@ -187,6 +189,7 @@ - (void)mouseUp:(NSEvent *)event

- (void)touchesBegan:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
{
[_gestureHandler setCurrentPointerType:event];
// super call was moved to interactionsBegan method to keep the
// original order of calls
[self interactionsBegan:touches withEvent:event];
Expand Down Expand Up @@ -400,7 +403,8 @@ - (RNGestureHandlerEventExtraData *)eventExtraData:(NSPanGestureRecognizer *)rec
withAbsolutePosition:[recognizer locationInView:recognizer.view.window.contentView]
withTranslation:[recognizer translationInView:recognizer.view.window.contentView]
withVelocity:[recognizer velocityInView:recognizer.view.window.contentView]
withNumberOfTouches:1];
withNumberOfTouches:1
withPointerType:RNGestureHandlerMouse];
}
#else
- (RNGestureHandlerEventExtraData *)eventExtraData:(UIPanGestureRecognizer *)recognizer
Expand All @@ -409,7 +413,8 @@ - (RNGestureHandlerEventExtraData *)eventExtraData:(UIPanGestureRecognizer *)rec
withAbsolutePosition:[recognizer locationInView:recognizer.view.window]
withTranslation:[recognizer translationInView:recognizer.view.window]
withVelocity:[recognizer velocityInView:recognizer.view.window]
withNumberOfTouches:recognizer.numberOfTouches];
withNumberOfTouches:recognizer.numberOfTouches
withPointerType:_pointerType];
}
#endif

Expand Down
8 changes: 6 additions & 2 deletions apple/Handlers/RNPinchHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ - (void)magnifyWithEvent:(NSEvent *)event

switch (self.state) {
case NSGestureRecognizerStateBegan:
[_gestureHandler setCurrentPointerTypeToMouse];
[self interactionsBegan:[NSSet setWithObject:event] withEvent:event];
break;
case NSGestureRecognizerStateChanged:
Expand All @@ -102,6 +103,7 @@ - (void)magnifyWithEvent:(NSEvent *)event
#else
- (void)touchesBegan:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
{
[_gestureHandler setCurrentPointerType:event];
[super touchesBegan:touches withEvent:event];
[self interactionsBegan:touches withEvent:event];
}
Expand Down Expand Up @@ -155,15 +157,17 @@ - (RNGestureHandlerEventExtraData *)eventExtraData:(NSMagnificationGestureRecogn
return [RNGestureHandlerEventExtraData forPinch:recognizer.magnification
withFocalPoint:[recognizer locationInView:recognizer.view]
withVelocity:((RNBetterPinchRecognizer *)recognizer).velocity
withNumberOfTouches:2];
withNumberOfTouches:2
withPointerType:RNGestureHandlerMouse];
}
#else
- (RNGestureHandlerEventExtraData *)eventExtraData:(UIPinchGestureRecognizer *)recognizer
{
return [RNGestureHandlerEventExtraData forPinch:recognizer.scale
withFocalPoint:[recognizer locationInView:recognizer.view]
withVelocity:recognizer.velocity
withNumberOfTouches:recognizer.numberOfTouches];
withNumberOfTouches:recognizer.numberOfTouches
withPointerType:_pointerType];
}
#endif
#endif // !TARGET_OS_TV
Expand Down
8 changes: 6 additions & 2 deletions apple/Handlers/RNRotationHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ - (void)rotateWithEvent:(NSEvent *)event

switch (self.state) {
case NSGestureRecognizerStateBegan:
[_gestureHandler setCurrentPointerTypeToMouse];
[self interactionsBegan:[NSSet setWithObject:event] withEvent:event];
break;
case NSGestureRecognizerStateChanged:
Expand All @@ -96,6 +97,7 @@ - (void)rotateWithEvent:(NSEvent *)event
#else
- (void)touchesBegan:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
{
[_gestureHandler setCurrentPointerType:event];
[super touchesBegan:touches withEvent:event];
[self interactionsBegan:touches withEvent:event];
}
Expand Down Expand Up @@ -149,15 +151,17 @@ - (RNGestureHandlerEventExtraData *)eventExtraData:(NSRotationGestureRecognizer
return [RNGestureHandlerEventExtraData forRotation:recognizer.rotation
withAnchorPoint:[recognizer locationInView:recognizer.view]
withVelocity:((RNBetterRotationRecognizer *)recognizer).velocity
withNumberOfTouches:2];
withNumberOfTouches:2
withPointerType:RNGestureHandlerMouse];
}
#else
- (RNGestureHandlerEventExtraData *)eventExtraData:(UIRotationGestureRecognizer *)recognizer
{
return [RNGestureHandlerEventExtraData forRotation:recognizer.rotation
withAnchorPoint:[recognizer locationInView:recognizer.view]
withVelocity:recognizer.velocity
withNumberOfTouches:recognizer.numberOfTouches];
withNumberOfTouches:recognizer.numberOfTouches
withPointerType:_pointerType];
}
#endif
#endif // !TARGET_OS_TV
Expand Down
2 changes: 2 additions & 0 deletions apple/Handlers/RNTapHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ - (void)interactionsCancelled:(NSSet *)touches withEvent:(UIEvent *)event
#if TARGET_OS_OSX
- (void)mouseDown:(NSEvent *)event
{
[_gestureHandler setCurrentPointerTypeToMouse];
[super mouseDown:event];
[self interactionsBegan:[NSSet setWithObject:event] withEvent:event];
}
Expand Down Expand Up @@ -184,6 +185,7 @@ - (void)rightMouseUp:(NSEvent *)event

- (void)touchesBegan:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
{
[_gestureHandler setCurrentPointerType:event];
[super touchesBegan:touches withEvent:event];
[self interactionsBegan:touches withEvent:event];
}
Expand Down
9 changes: 9 additions & 0 deletions apple/RNGestureHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#import "RNGestureHandlerDirection.h"
#import "RNGestureHandlerEvents.h"
#import "RNGestureHandlerPointerTracker.h"
#import "RNGestureHandlerPointerType.h"
#import "RNGestureHandlerState.h"

#import <Foundation/Foundation.h>
Expand Down Expand Up @@ -53,6 +54,8 @@
UIGestureRecognizer *_recognizer;
@protected
RNGestureHandlerState _lastState;
@protected
NSInteger _pointerType;
}

+ (nullable RNGestureHandler *)findGestureHandlerByRecognizer:(nonnull UIGestureRecognizer *)recognizer;
Expand Down Expand Up @@ -87,4 +90,10 @@
- (void)sendEvent:(nonnull RNGestureHandlerStateChange *)event;
- (void)sendTouchEventInState:(RNGestureHandlerState)state forViewWithTag:(nonnull NSNumber *)reactTag;

#if !TARGET_OS_OSX
- (void)setCurrentPointerType:(nonnull UIEvent *)event;
#else
- (void)setCurrentPointerTypeToMouse;
#endif

@end
Loading

0 comments on commit 04e7898

Please sign in to comment.