forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 147
[Key handling] pass through all keys; allow specifying modifiers for validKeys[Down|Up] #1867
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
Merged
+620
−115
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
// [macOS] | ||
|
||
#if TARGET_OS_OSX | ||
#import <React/RCTConvert.h> | ||
|
||
// This class is used for specifying key filtering e.g. for -[RCTView validKeysDown] and -[RCTView validKeysUp] | ||
// Also see RCTViewKeyboardEvent, which is a React representation of an actual NSEvent that is dispatched to JS. | ||
@interface RCTHandledKey : NSObject | ||
nakambo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
+ (BOOL)event:(NSEvent *)event matchesFilter:(NSArray<RCTHandledKey *> *)filter; | ||
+ (BOOL)key:(NSString *)key matchesFilter:(NSArray<RCTHandledKey *> *)filter; | ||
|
||
- (instancetype)initWithKey:(NSString *)key; | ||
- (BOOL)matchesEvent:(NSEvent *)event; | ||
|
||
@property (nonatomic, copy) NSString *key; | ||
|
||
// For the following modifiers, nil means we don't care about the presence of the modifier when filtering the key | ||
// They are still expected to be only boolean when not nil. | ||
@property (nonatomic, assign) NSNumber *altKey; | ||
@property (nonatomic, assign) NSNumber *ctrlKey; | ||
@property (nonatomic, assign) NSNumber *metaKey; | ||
@property (nonatomic, assign) NSNumber *shiftKey; | ||
|
||
@end | ||
|
||
@interface RCTConvert (RCTHandledKey) | ||
|
||
+ (RCTHandledKey *)RCTHandledKey:(id)json; | ||
|
||
@end | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
// [macOS] | ||
|
||
#import "objc/runtime.h" | ||
#import <React/RCTAssert.h> | ||
#import <React/RCTUtils.h> | ||
#import <RCTConvert.h> | ||
#import <RCTHandledKey.h> | ||
#import <RCTViewKeyboardEvent.h> | ||
|
||
#if TARGET_OS_OSX | ||
|
||
@implementation RCTHandledKey | ||
|
||
+ (NSArray<NSString *> *)validModifiers { | ||
// keep in sync with actual properties and RCTViewKeyboardEvent | ||
return @[@"altKey", @"ctrlKey", @"metaKey", @"shiftKey"]; | ||
} | ||
|
||
+ (BOOL)event:(NSEvent *)event matchesFilter:(NSArray<RCTHandledKey *> *)filter { | ||
for (RCTHandledKey *key in filter) { | ||
if ([key matchesEvent:event]) { | ||
return YES; | ||
} | ||
} | ||
|
||
return NO; | ||
} | ||
|
||
+ (BOOL)key:(NSString *)key matchesFilter:(NSArray<RCTHandledKey *> *)filter { | ||
for (RCTHandledKey *aKey in filter) { | ||
if ([[aKey key] isEqualToString:key]) { | ||
return YES; | ||
} | ||
} | ||
|
||
return NO; | ||
} | ||
|
||
- (instancetype)initWithKey:(NSString *)key { | ||
if ((self = [super init])) { | ||
self.key = key; | ||
} | ||
return self; | ||
} | ||
|
||
- (BOOL)matchesEvent:(NSEvent *)event | ||
{ | ||
NSEventType type = [event type]; | ||
if (type != NSEventTypeKeyDown && type != NSEventTypeKeyUp) { | ||
RCTFatal(RCTErrorWithMessage([NSString stringWithFormat:@"Wrong event type (%d) sent to -[RCTHandledKey matchesEvent:]", (int)type])); | ||
return NO; | ||
} | ||
|
||
NSDictionary *body = [RCTViewKeyboardEvent bodyFromEvent:event]; | ||
NSString *key = body[@"key"]; | ||
if (key == nil) { | ||
RCTFatal(RCTErrorWithMessage(@"Event body has missing value for 'key'")); | ||
return NO; | ||
} | ||
|
||
if (![key isEqualToString:self.key]) { | ||
return NO; | ||
} | ||
|
||
NSArray<NSString *> *modifiers = [RCTHandledKey validModifiers]; | ||
for (NSString *modifier in modifiers) { | ||
NSNumber *myValue = [self valueForKey:modifier]; | ||
|
||
if (myValue == nil) { | ||
continue; | ||
} | ||
|
||
NSNumber *eventValue = (NSNumber *)body[modifier]; | ||
if (eventValue == nil) { | ||
RCTFatal(RCTErrorWithMessage([NSString stringWithFormat:@"Event body has missing value for '%@'", modifier])); | ||
return NO; | ||
} | ||
|
||
if (![eventValue isKindOfClass:[NSNumber class]]) { | ||
RCTFatal(RCTErrorWithMessage([NSString stringWithFormat:@"Event body has unexpected value of class '%@' for '%@'", | ||
NSStringFromClass(object_getClass(eventValue)), modifier])); | ||
return NO; | ||
} | ||
|
||
if (![myValue isEqualToNumber:body[modifier]]) { | ||
return NO; | ||
} | ||
} | ||
|
||
return YES; // keys matched; all present modifiers matched | ||
} | ||
|
||
@end | ||
|
||
@implementation RCTConvert (RCTHandledKey) | ||
|
||
+ (RCTHandledKey *)RCTHandledKey:(id)json | ||
{ | ||
// legacy way of specifying validKeysDown and validKeysUp -- here we ignore the modifiers when comparing to the NSEvent | ||
if ([json isKindOfClass:[NSString class]]) { | ||
return [[RCTHandledKey alloc] initWithKey:(NSString *)json]; | ||
} | ||
|
||
// modern way of specifying validKeys and validKeysUp -- here we assume missing modifiers to mean false\NO | ||
if ([json isKindOfClass:[NSDictionary class]]) { | ||
NSDictionary *dict = (NSDictionary *)json; | ||
NSString *key = dict[@"key"]; | ||
if (key == nil) { | ||
RCTLogConvertError(dict, @"a RCTHandledKey -- must include \"key\""); | ||
return nil; | ||
} | ||
|
||
RCTHandledKey *handledKey = [[RCTHandledKey alloc] initWithKey:key]; | ||
NSArray<NSString *> *modifiers = RCTHandledKey.validModifiers; | ||
for (NSString *key in modifiers) { | ||
id value = dict[key]; | ||
if (value == nil) { | ||
value = @NO; // assume NO -- instead of nil i.e. "don't care" unlike the string case above. | ||
} | ||
|
||
if (![value isKindOfClass:[NSNumber class]]) { | ||
RCTLogConvertError(value, @"a boolean"); | ||
return nil; | ||
} | ||
|
||
[handledKey setValue:@([(NSNumber *)value boolValue]) forKey:key]; | ||
} | ||
|
||
return handledKey; | ||
} | ||
|
||
RCTLogConvertError(json, @"a RCTHandledKey -- allowed types are string and object"); | ||
return nil; | ||
} | ||
|
||
@end | ||
|
||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.