From fcf75ee6c869516140b81f2533aafabbad5f5563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bert?= Date: Tue, 23 Jul 2024 10:57:24 +0200 Subject: [PATCH 01/10] Recognizer --- apple/Handlers/RNNativeViewHandler.mm | 58 +++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/apple/Handlers/RNNativeViewHandler.mm b/apple/Handlers/RNNativeViewHandler.mm index 262e659169..04e41e8486 100644 --- a/apple/Handlers/RNNativeViewHandler.mm +++ b/apple/Handlers/RNNativeViewHandler.mm @@ -205,12 +205,41 @@ - (void)handleTouchCancel:(UIView *)sender forEvent:(UIEvent *)event @implementation RNDummyGestureRecognizer +__weak RNGestureHandler *_gestureHandler; + +- (void)setDelegate:(id)delegate +{ + [super setDelegate:delegate]; +} + - (id)initWithGestureHandler:(RNGestureHandler *)gestureHandler { - self = [super initWithTarget:gestureHandler action:@selector(handleGesture:)]; + if (self = [super initWithTarget:gestureHandler action:@selector(handleGesture:)]) { + _gestureHandler = gestureHandler; + }; + return self; } +- (void)mouseDown:(NSEvent *)event +{ + self.state = NSGestureRecognizerStateBegan; + [_gestureHandler.pointerTracker touchesBegan:[NSSet setWithObject:event] withEvent:event]; +} + +- (void)mouseDragged:(NSEvent *)event +{ + self.state = NSGestureRecognizerStateChanged; + [_gestureHandler.pointerTracker touchesMoved:[NSSet setWithObject:event] withEvent:event]; +} + +- (void)mouseUp:(NSEvent *)event +{ + [_gestureHandler.pointerTracker touchesEnded:[NSSet setWithObject:event] withEvent:event]; + self.state = NSGestureRecognizerStateEnded; + [self reset]; +} + @end #pragma mark RNNativeViewGestureHandler @@ -219,13 +248,36 @@ @implementation RNNativeViewGestureHandler - (instancetype)initWithTag:(NSNumber *)tag { - RCTLogWarn(@"NativeViewGestureHandler is not supported on macOS"); if ((self = [super initWithTag:tag])) { - _recognizer = [NSGestureRecognizer alloc]; + _recognizer = [[RNDummyGestureRecognizer alloc] initWithGestureHandler:self]; } return self; } +- (void)bindToView:(NSView *)view +{ + [super bindToView:view]; + + NSLog(@"Bound to view"); + + // We can restore default scrollview behaviour to delay touches to scrollview's children + // because gesture handler system can handle cancellation of scroll recognizer when JS responder + // is set +#ifdef RCT_NEW_ARCH_ENABLED + if ([view isKindOfClass:[RCTScrollViewComponentView class]]) { + UIScrollView *scrollView = ((RCTScrollViewComponentView *)view).scrollView; + scrollView.delaysContentTouches = YES; + } +#else + if ([view isKindOfClass:[RCTScrollView class]]) { + // This part of the code is coupled with RN implementation of ScrollView native wrapper and + // we expect for RCTScrollView component to contain a subclass of UIScrollview as the only + // subview + NSScrollView *scrollView = [view.subviews objectAtIndex:0]; + } +#endif // RCT_NEW_ARCH_ENABLED +} + @end #endif From c07268fa223e8af31dd4d101eff65dac9705ff2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bert?= Date: Tue, 23 Jul 2024 16:24:59 +0200 Subject: [PATCH 02/10] It works --- apple/Handlers/RNNativeViewHandler.mm | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/apple/Handlers/RNNativeViewHandler.mm b/apple/Handlers/RNNativeViewHandler.mm index 04e41e8486..f6128da5d0 100644 --- a/apple/Handlers/RNNativeViewHandler.mm +++ b/apple/Handlers/RNNativeViewHandler.mm @@ -212,6 +212,11 @@ - (void)setDelegate:(id)delegate [super setDelegate:delegate]; } +- (BOOL)hasPointerInside +{ + return NSPointInRect([self locationInView:self.view], self.view.bounds); +} + - (id)initWithGestureHandler:(RNGestureHandler *)gestureHandler { if (self = [super initWithTarget:gestureHandler action:@selector(handleGesture:)]) { @@ -229,6 +234,7 @@ - (void)mouseDown:(NSEvent *)event - (void)mouseDragged:(NSEvent *)event { + [self hasPointerInside]; self.state = NSGestureRecognizerStateChanged; [_gestureHandler.pointerTracker touchesMoved:[NSSet setWithObject:event] withEvent:event]; } @@ -278,6 +284,12 @@ - (void)bindToView:(NSView *)view #endif // RCT_NEW_ARCH_ENABLED } +- (RNGestureHandlerEventExtraData *)eventExtraData:(RNDummyGestureRecognizer *)recognizer +{ + return [RNGestureHandlerEventExtraData forPointerInside:[recognizer hasPointerInside] + withPointerType:RNGestureHandlerMouse]; +} + @end #endif From 54f7033a16bb83aa69dd868f99a16a722b3baeda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bert?= Date: Tue, 23 Jul 2024 17:44:37 +0200 Subject: [PATCH 03/10] Remove unnecessary call --- apple/Handlers/RNNativeViewHandler.mm | 1 - 1 file changed, 1 deletion(-) diff --git a/apple/Handlers/RNNativeViewHandler.mm b/apple/Handlers/RNNativeViewHandler.mm index f6128da5d0..5c09a94d29 100644 --- a/apple/Handlers/RNNativeViewHandler.mm +++ b/apple/Handlers/RNNativeViewHandler.mm @@ -234,7 +234,6 @@ - (void)mouseDown:(NSEvent *)event - (void)mouseDragged:(NSEvent *)event { - [self hasPointerInside]; self.state = NSGestureRecognizerStateChanged; [_gestureHandler.pointerTracker touchesMoved:[NSSet setWithObject:event] withEvent:event]; } From 52c112125a64f15020e946b16d9c41753a233267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bert?= Date: Wed, 24 Jul 2024 15:00:55 +0200 Subject: [PATCH 04/10] Change Pressable to RectButton in example app --- MacOSExample/src/App.tsx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/MacOSExample/src/App.tsx b/MacOSExample/src/App.tsx index dc3ca419c8..d0fd3f7697 100644 --- a/MacOSExample/src/App.tsx +++ b/MacOSExample/src/App.tsx @@ -12,7 +12,10 @@ import { StackScreenProps, } from '@react-navigation/stack'; import { NavigationContainer, ParamListBase } from '@react-navigation/native'; -import { GestureHandlerRootView } from 'react-native-gesture-handler'; +import { + GestureHandlerRootView, + RectButton, +} from 'react-native-gesture-handler'; import Draggable from './basic/draggable'; import PinchableBox from './recipes/scaleAndRotate'; @@ -100,9 +103,9 @@ interface MainScreenItemProps { function MainScreenItem({ name, onPressItem }: MainScreenItemProps) { return ( - onPressItem(name)}> + onPressItem(name)}> {name} - + ); } From 88c9686c934cbf0c103a14df2f2fe473f77eb2bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bert?= Date: Wed, 24 Jul 2024 17:42:20 +0200 Subject: [PATCH 05/10] Remove useless override --- apple/Handlers/RNNativeViewHandler.mm | 5 ----- 1 file changed, 5 deletions(-) diff --git a/apple/Handlers/RNNativeViewHandler.mm b/apple/Handlers/RNNativeViewHandler.mm index 5c09a94d29..3d7ddc6986 100644 --- a/apple/Handlers/RNNativeViewHandler.mm +++ b/apple/Handlers/RNNativeViewHandler.mm @@ -207,11 +207,6 @@ @implementation RNDummyGestureRecognizer __weak RNGestureHandler *_gestureHandler; -- (void)setDelegate:(id)delegate -{ - [super setDelegate:delegate]; -} - - (BOOL)hasPointerInside { return NSPointInRect([self locationInView:self.view], self.view.bounds); From b8c6d8138346ad841cac4015a4268016cf19e791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bert?= Date: Wed, 24 Jul 2024 17:53:08 +0200 Subject: [PATCH 06/10] Remove other events --- apple/Handlers/RNNativeViewHandler.mm | 3 --- 1 file changed, 3 deletions(-) diff --git a/apple/Handlers/RNNativeViewHandler.mm b/apple/Handlers/RNNativeViewHandler.mm index 3d7ddc6986..d5da1710f0 100644 --- a/apple/Handlers/RNNativeViewHandler.mm +++ b/apple/Handlers/RNNativeViewHandler.mm @@ -224,18 +224,15 @@ - (id)initWithGestureHandler:(RNGestureHandler *)gestureHandler - (void)mouseDown:(NSEvent *)event { self.state = NSGestureRecognizerStateBegan; - [_gestureHandler.pointerTracker touchesBegan:[NSSet setWithObject:event] withEvent:event]; } - (void)mouseDragged:(NSEvent *)event { self.state = NSGestureRecognizerStateChanged; - [_gestureHandler.pointerTracker touchesMoved:[NSSet setWithObject:event] withEvent:event]; } - (void)mouseUp:(NSEvent *)event { - [_gestureHandler.pointerTracker touchesEnded:[NSSet setWithObject:event] withEvent:event]; self.state = NSGestureRecognizerStateEnded; [self reset]; } From de5bdd552bc600cdc520b3dcbeb03a79ed4afdcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bert?= Date: Wed, 24 Jul 2024 17:55:45 +0200 Subject: [PATCH 07/10] Remove bindToView --- apple/Handlers/RNNativeViewHandler.mm | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/apple/Handlers/RNNativeViewHandler.mm b/apple/Handlers/RNNativeViewHandler.mm index d5da1710f0..b13ca8fcd5 100644 --- a/apple/Handlers/RNNativeViewHandler.mm +++ b/apple/Handlers/RNNativeViewHandler.mm @@ -251,30 +251,6 @@ - (instancetype)initWithTag:(NSNumber *)tag return self; } -- (void)bindToView:(NSView *)view -{ - [super bindToView:view]; - - NSLog(@"Bound to view"); - - // We can restore default scrollview behaviour to delay touches to scrollview's children - // because gesture handler system can handle cancellation of scroll recognizer when JS responder - // is set -#ifdef RCT_NEW_ARCH_ENABLED - if ([view isKindOfClass:[RCTScrollViewComponentView class]]) { - UIScrollView *scrollView = ((RCTScrollViewComponentView *)view).scrollView; - scrollView.delaysContentTouches = YES; - } -#else - if ([view isKindOfClass:[RCTScrollView class]]) { - // This part of the code is coupled with RN implementation of ScrollView native wrapper and - // we expect for RCTScrollView component to contain a subclass of UIScrollview as the only - // subview - NSScrollView *scrollView = [view.subviews objectAtIndex:0]; - } -#endif // RCT_NEW_ARCH_ENABLED -} - - (RNGestureHandlerEventExtraData *)eventExtraData:(RNDummyGestureRecognizer *)recognizer { return [RNGestureHandlerEventExtraData forPointerInside:[recognizer hasPointerInside] From 3291e648dca07fe19c4ccf2fed795e65dad4181c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bert?= Date: Wed, 24 Jul 2024 18:11:00 +0200 Subject: [PATCH 08/10] Switch implementations to ifs --- apple/Handlers/RNNativeViewHandler.mm | 85 +++++++++------------------ 1 file changed, 29 insertions(+), 56 deletions(-) diff --git a/apple/Handlers/RNNativeViewHandler.mm b/apple/Handlers/RNNativeViewHandler.mm index b13ca8fcd5..16ac094c7a 100644 --- a/apple/Handlers/RNNativeViewHandler.mm +++ b/apple/Handlers/RNNativeViewHandler.mm @@ -21,8 +21,6 @@ #import #endif // RCT_NEW_ARCH_ENABLED -#if !TARGET_OS_OSX - #pragma mark RNDummyGestureRecognizer @implementation RNDummyGestureRecognizer { @@ -37,6 +35,7 @@ - (id)initWithGestureHandler:(RNGestureHandler *)gestureHandler return self; } +#if !TARGET_OS_OSX - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [_gestureHandler setCurrentPointerType:event]; @@ -62,6 +61,30 @@ - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)ev [self reset]; } +#else +- (BOOL)hasPointerInside +{ + return NSPointInRect([self locationInView:self.view], self.view.bounds); +} + +- (void)mouseDown:(NSEvent *)event +{ + self.state = NSGestureRecognizerStateBegan; +} + +- (void)mouseDragged:(NSEvent *)event +{ + self.state = NSGestureRecognizerStateChanged; +} + +- (void)mouseUp:(NSEvent *)event +{ + self.state = NSGestureRecognizerStateEnded; + [self reset]; +} + +#endif + - (void)reset { [_gestureHandler.pointerTracker reset]; @@ -93,6 +116,8 @@ - (void)configure:(NSDictionary *)config _disallowInterruption = [RCTConvert BOOL:config[@"disallowInterruption"]]; } +#if !TARGET_OS_OSX + - (void)bindToView:(UIView *)view { // For UIControl based views (UIButton, UISwitch) we provide special handling that would allow @@ -197,66 +222,14 @@ - (void)handleTouchCancel:(UIView *)sender forEvent:(UIEvent *)event withExtraData:[RNGestureHandlerEventExtraData forPointerInside:NO withPointerType:_pointerType]]; } -@end - #else -#pragma mark RNDummyGestureRecognizer - -@implementation RNDummyGestureRecognizer - -__weak RNGestureHandler *_gestureHandler; - -- (BOOL)hasPointerInside -{ - return NSPointInRect([self locationInView:self.view], self.view.bounds); -} - -- (id)initWithGestureHandler:(RNGestureHandler *)gestureHandler -{ - if (self = [super initWithTarget:gestureHandler action:@selector(handleGesture:)]) { - _gestureHandler = gestureHandler; - }; - - return self; -} - -- (void)mouseDown:(NSEvent *)event -{ - self.state = NSGestureRecognizerStateBegan; -} - -- (void)mouseDragged:(NSEvent *)event -{ - self.state = NSGestureRecognizerStateChanged; -} - -- (void)mouseUp:(NSEvent *)event -{ - self.state = NSGestureRecognizerStateEnded; - [self reset]; -} - -@end - -#pragma mark RNNativeViewGestureHandler - -@implementation RNNativeViewGestureHandler - -- (instancetype)initWithTag:(NSNumber *)tag -{ - if ((self = [super initWithTag:tag])) { - _recognizer = [[RNDummyGestureRecognizer alloc] initWithGestureHandler:self]; - } - return self; -} - - (RNGestureHandlerEventExtraData *)eventExtraData:(RNDummyGestureRecognizer *)recognizer { return [RNGestureHandlerEventExtraData forPointerInside:[recognizer hasPointerInside] withPointerType:RNGestureHandlerMouse]; } -@end - #endif + +@end From 0b87b12358b6503d23aafb7a303fffa3f9cc3cc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bert?= Date: Wed, 24 Jul 2024 18:41:39 +0200 Subject: [PATCH 09/10] Restore touch events --- apple/Handlers/RNNativeViewHandler.mm | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apple/Handlers/RNNativeViewHandler.mm b/apple/Handlers/RNNativeViewHandler.mm index 16ac094c7a..779825cbe3 100644 --- a/apple/Handlers/RNNativeViewHandler.mm +++ b/apple/Handlers/RNNativeViewHandler.mm @@ -70,16 +70,19 @@ - (BOOL)hasPointerInside - (void)mouseDown:(NSEvent *)event { self.state = NSGestureRecognizerStateBegan; + [_gestureHandler.pointerTracker touchesBegan:[NSSet setWithObject:event] withEvent:event]; } - (void)mouseDragged:(NSEvent *)event { self.state = NSGestureRecognizerStateChanged; + [_gestureHandler.pointerTracker touchesMoved:[NSSet setWithObject:event] withEvent:event]; } - (void)mouseUp:(NSEvent *)event { self.state = NSGestureRecognizerStateEnded; + [_gestureHandler.pointerTracker touchesEnded:[NSSet setWithObject:event] withEvent:event]; [self reset]; } From 452f4287fe1336db0742be86c7c99ce8f2385c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bert?= Date: Wed, 24 Jul 2024 18:52:35 +0200 Subject: [PATCH 10/10] Set pointer type to mouse --- apple/Handlers/RNNativeViewHandler.mm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apple/Handlers/RNNativeViewHandler.mm b/apple/Handlers/RNNativeViewHandler.mm index 779825cbe3..de49cc36a7 100644 --- a/apple/Handlers/RNNativeViewHandler.mm +++ b/apple/Handlers/RNNativeViewHandler.mm @@ -69,6 +69,8 @@ - (BOOL)hasPointerInside - (void)mouseDown:(NSEvent *)event { + [_gestureHandler setCurrentPointerTypeToMouse]; + self.state = NSGestureRecognizerStateBegan; [_gestureHandler.pointerTracker touchesBegan:[NSSet setWithObject:event] withEvent:event]; }