|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#import "objc/runtime.h" |
| 9 | +#import <React/RCTAssert.h> |
| 10 | +#import <React/RCTUtils.h> |
| 11 | +#import <RCTConvert.h> |
| 12 | +#import <RCTHandledKey.h> |
| 13 | +#import <RCTViewKeyboardEvent.h> |
| 14 | + |
| 15 | +#if TARGET_OS_OSX // [macOS |
| 16 | + |
| 17 | +@implementation RCTHandledKey |
| 18 | + |
| 19 | ++ (NSArray<NSString *> *)validModifiers { |
| 20 | + // keep in sync with actual properties and RCTViewKeyboardEvent |
| 21 | + return @[@"capsLock", @"shift", @"ctrl", @"alt", @"meta", @"numericPad", @"help", @"function"]; |
| 22 | +} |
| 23 | + |
| 24 | ++ (BOOL)event:(NSEvent *)event matchesFilter:(NSArray<RCTHandledKey *> *)filter { |
| 25 | + for (RCTHandledKey *key in filter) { |
| 26 | + if ([key matchesEvent:event]) { |
| 27 | + return YES; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return NO; |
| 32 | +} |
| 33 | + |
| 34 | ++ (BOOL)key:(NSString *)key matchesFilter:(NSArray<RCTHandledKey *> *)filter { |
| 35 | + for (RCTHandledKey *aKey in filter) { |
| 36 | + if ([[aKey key] isEqualToString:key]) { |
| 37 | + return YES; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return NO; |
| 42 | +} |
| 43 | + |
| 44 | +- (instancetype)initWithKey:(NSString *)key { |
| 45 | + if ((self = [super init])) { |
| 46 | + self.key = key; |
| 47 | + } |
| 48 | + return self; |
| 49 | +} |
| 50 | + |
| 51 | +- (BOOL)matchesEvent:(NSEvent *)event |
| 52 | +{ |
| 53 | + NSEventType type = [event type]; |
| 54 | + if (type != NSEventTypeKeyDown && type != NSEventTypeKeyUp) { |
| 55 | + RCTFatal(RCTErrorWithMessage([NSString stringWithFormat:@"Wrong event type (%d) sent to -[RCTHandledKey matchesEvent:]", (int)type])); |
| 56 | + return NO; |
| 57 | + } |
| 58 | + |
| 59 | + NSDictionary *body = [RCTViewKeyboardEvent bodyFromEvent:event]; |
| 60 | + if (![body[@"key"] isEqualToString:self.key]) { |
| 61 | + return NO; |
| 62 | + } |
| 63 | + |
| 64 | + NSArray<NSString *> *modifiers = [RCTHandledKey validModifiers]; |
| 65 | + for (NSString *modifier in modifiers) { |
| 66 | + NSString *modifierKey = [modifier stringByAppendingString:@"Key"]; |
| 67 | + NSNumber *myValue = [self valueForKey:modifier]; |
| 68 | + |
| 69 | + if (myValue == nil) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + NSNumber *eventValue = (NSNumber *)body[modifierKey]; |
| 74 | + if (eventValue == nil) { |
| 75 | + RCTFatal(RCTErrorWithMessage([NSString stringWithFormat:@"Event body has missing value for %@", modifierKey])); |
| 76 | + return NO; |
| 77 | + } |
| 78 | + |
| 79 | + if (![eventValue isKindOfClass:[NSNumber class]]) { |
| 80 | + RCTFatal(RCTErrorWithMessage([NSString stringWithFormat:@"Event body has unexpected value of class %@ for %@", |
| 81 | + NSStringFromClass(object_getClass(eventValue)), modifierKey])); |
| 82 | + return NO; |
| 83 | + } |
| 84 | + |
| 85 | + if (![myValue isEqualToNumber:body[modifierKey]]) { |
| 86 | + return NO; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return YES; // keys matched; all present modifiers matched |
| 91 | +} |
| 92 | + |
| 93 | +@end |
| 94 | + |
| 95 | +@implementation RCTConvert (RCTHandledKey) |
| 96 | + |
| 97 | ++ (RCTHandledKey *)RCTHandledKey:(id)json |
| 98 | +{ |
| 99 | + if ([json isKindOfClass:[NSString class]]) { |
| 100 | + return [[RCTHandledKey alloc] initWithKey:(NSString *)json]; |
| 101 | + } |
| 102 | + |
| 103 | + if ([json isKindOfClass:[NSDictionary class]]) { |
| 104 | + NSDictionary *dict = (NSDictionary *)json; |
| 105 | + NSString *key = dict[@"key"]; |
| 106 | + if (key == nil) { |
| 107 | + RCTLogConvertError(dict, @"a RCTHandledKey -- must include \"key\""); |
| 108 | + return nil; |
| 109 | + } |
| 110 | + |
| 111 | + RCTHandledKey *handledKey = [[RCTHandledKey alloc] initWithKey:key]; |
| 112 | + NSArray<NSString *> *modifiers = RCTHandledKey.validModifiers; |
| 113 | + for (NSString *key in modifiers) { |
| 114 | + id value = dict[key]; |
| 115 | + if (value == nil) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + if (![value isKindOfClass:[NSNumber class]]) { |
| 120 | + RCTLogConvertError(value, @"a boolean"); |
| 121 | + return nil; |
| 122 | + } |
| 123 | + |
| 124 | + [handledKey setValue:@([(NSNumber *)value boolValue]) forKey:key]; |
| 125 | + } |
| 126 | + |
| 127 | + return handledKey; |
| 128 | + } |
| 129 | + |
| 130 | + RCTLogConvertError(json, @"a RCTHandledKey -- allowed types are string and object"); |
| 131 | + return nil; |
| 132 | +} |
| 133 | + |
| 134 | +@end |
| 135 | + |
| 136 | +#endif // macOS] |
0 commit comments