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

[macOS] Add NativeViewGestureHandler #3004

Merged
merged 13 commits into from
Jul 25, 2024
9 changes: 6 additions & 3 deletions MacOSExample/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -100,9 +103,9 @@ interface MainScreenItemProps {

function MainScreenItem({ name, onPressItem }: MainScreenItemProps) {
return (
<Pressable style={[styles.button]} onPress={() => onPressItem(name)}>
<RectButton style={[styles.button]} onPress={() => onPressItem(name)}>
<Text style={styles.text}>{name}</Text>
</Pressable>
</RectButton>
);
}

Expand Down
63 changes: 36 additions & 27 deletions apple/Handlers/RNNativeViewHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#import <React/RCTScrollView.h>
#endif // RCT_NEW_ARCH_ENABLED

#if !TARGET_OS_OSX

#pragma mark RNDummyGestureRecognizer

@implementation RNDummyGestureRecognizer {
Expand All @@ -37,6 +35,7 @@ - (id)initWithGestureHandler:(RNGestureHandler *)gestureHandler
return self;
}

#if !TARGET_OS_OSX
- (void)touchesBegan:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
{
[_gestureHandler setCurrentPointerType:event];
Expand All @@ -62,6 +61,35 @@ - (void)touchesCancelled:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)ev
[self reset];
}

#else
- (BOOL)hasPointerInside
{
return NSPointInRect([self locationInView:self.view], self.view.bounds);
}

- (void)mouseDown:(NSEvent *)event
{
[_gestureHandler setCurrentPointerTypeToMouse];

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];
}

#endif

- (void)reset
{
[_gestureHandler.pointerTracker reset];
Expand Down Expand Up @@ -93,6 +121,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
Expand Down Expand Up @@ -197,35 +227,14 @@ - (void)handleTouchCancel:(UIView *)sender forEvent:(UIEvent *)event
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:NO withPointerType:_pointerType]];
}

@end

#else

#pragma mark RNDummyGestureRecognizer

@implementation RNDummyGestureRecognizer

- (id)initWithGestureHandler:(RNGestureHandler *)gestureHandler
- (RNGestureHandlerEventExtraData *)eventExtraData:(RNDummyGestureRecognizer *)recognizer
{
self = [super initWithTarget:gestureHandler action:@selector(handleGesture:)];
return self;
return [RNGestureHandlerEventExtraData forPointerInside:[recognizer hasPointerInside]
withPointerType:RNGestureHandlerMouse];
}

@end

#pragma mark RNNativeViewGestureHandler

@implementation RNNativeViewGestureHandler

- (instancetype)initWithTag:(NSNumber *)tag
{
RCTLogWarn(@"NativeViewGestureHandler is not supported on macOS");
if ((self = [super initWithTag:tag])) {
_recognizer = [NSGestureRecognizer alloc];
}
return self;
}
#endif

@end

#endif
Loading