RCTKeyCommands.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /**
  2. * Copyright (c) Facebook, Inc. and its 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. #import "RCTKeyCommands.h"
  8. #import <UIKit/UIKit.h>
  9. #import "RCTDefines.h"
  10. #import "RCTUtils.h"
  11. #if RCT_DEV
  12. static BOOL RCTIsIOS8OrEarlier()
  13. {
  14. return [UIDevice currentDevice].systemVersion.floatValue < 9;
  15. }
  16. @interface RCTKeyCommand : NSObject <NSCopying>
  17. @property (nonatomic, strong) UIKeyCommand *keyCommand;
  18. @property (nonatomic, copy) void (^block)(UIKeyCommand *);
  19. @end
  20. @implementation RCTKeyCommand
  21. - (instancetype)initWithKeyCommand:(UIKeyCommand *)keyCommand
  22. block:(void (^)(UIKeyCommand *))block
  23. {
  24. if ((self = [super init])) {
  25. _keyCommand = keyCommand;
  26. _block = block;
  27. }
  28. return self;
  29. }
  30. RCT_NOT_IMPLEMENTED(- (instancetype)init)
  31. - (id)copyWithZone:(__unused NSZone *)zone
  32. {
  33. return self;
  34. }
  35. - (NSUInteger)hash
  36. {
  37. return _keyCommand.input.hash ^ _keyCommand.modifierFlags;
  38. }
  39. - (BOOL)isEqual:(RCTKeyCommand *)object
  40. {
  41. if (![object isKindOfClass:[RCTKeyCommand class]]) {
  42. return NO;
  43. }
  44. return [self matchesInput:object.keyCommand.input
  45. flags:object.keyCommand.modifierFlags];
  46. }
  47. - (BOOL)matchesInput:(NSString *)input flags:(UIKeyModifierFlags)flags
  48. {
  49. return [_keyCommand.input isEqual:input] && _keyCommand.modifierFlags == flags;
  50. }
  51. - (NSString *)description
  52. {
  53. return [NSString stringWithFormat:@"<%@:%p input=\"%@\" flags=%lld hasBlock=%@>",
  54. [self class], self, _keyCommand.input, (long long)_keyCommand.modifierFlags,
  55. _block ? @"YES" : @"NO"];
  56. }
  57. @end
  58. @interface RCTKeyCommands ()
  59. @property (nonatomic, strong) NSMutableSet<RCTKeyCommand *> *commands;
  60. @end
  61. @implementation UIResponder (RCTKeyCommands)
  62. + (UIResponder *)RCT_getFirstResponder:(UIResponder *)view
  63. {
  64. UIResponder *firstResponder = nil;
  65. if (view.isFirstResponder) {
  66. return view;
  67. } else if ([view isKindOfClass:[UIViewController class]]) {
  68. if ([(UIViewController *)view parentViewController]) {
  69. firstResponder = [UIResponder RCT_getFirstResponder: [(UIViewController *)view parentViewController]];
  70. }
  71. return firstResponder ? firstResponder : [UIResponder RCT_getFirstResponder: [(UIViewController *)view view]];
  72. } else if ([view isKindOfClass:[UIView class]]) {
  73. for (UIView *subview in [(UIView *)view subviews]) {
  74. firstResponder = [UIResponder RCT_getFirstResponder: subview];
  75. if (firstResponder) {
  76. return firstResponder;
  77. }
  78. }
  79. }
  80. return firstResponder;
  81. }
  82. - (NSArray<UIKeyCommand *> *)RCT_keyCommands
  83. {
  84. NSSet<RCTKeyCommand *> *commands = [RCTKeyCommands sharedInstance].commands;
  85. return [[commands valueForKeyPath:@"keyCommand"] allObjects];
  86. }
  87. /**
  88. * Single Press Key Command Response
  89. * Command + KeyEvent (Command + R/D, etc.)
  90. */
  91. - (void)RCT_handleKeyCommand:(UIKeyCommand *)key
  92. {
  93. // NOTE: throttle the key handler because on iOS 9 the handleKeyCommand:
  94. // method gets called repeatedly if the command key is held down.
  95. static NSTimeInterval lastCommand = 0;
  96. if (RCTIsIOS8OrEarlier() || CACurrentMediaTime() - lastCommand > 0.5) {
  97. for (RCTKeyCommand *command in [RCTKeyCommands sharedInstance].commands) {
  98. if ([command.keyCommand.input isEqualToString:key.input] &&
  99. command.keyCommand.modifierFlags == key.modifierFlags) {
  100. if (command.block) {
  101. command.block(key);
  102. lastCommand = CACurrentMediaTime();
  103. }
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * Double Press Key Command Response
  110. * Double KeyEvent (Double R, etc.)
  111. */
  112. - (void)RCT_handleDoublePressKeyCommand:(UIKeyCommand *)key
  113. {
  114. static BOOL firstPress = YES;
  115. static NSTimeInterval lastCommand = 0;
  116. static NSTimeInterval lastDoubleCommand = 0;
  117. static NSString *lastInput = nil;
  118. static UIKeyModifierFlags lastModifierFlags = 0;
  119. if (firstPress) {
  120. for (RCTKeyCommand *command in [RCTKeyCommands sharedInstance].commands) {
  121. if ([command.keyCommand.input isEqualToString:key.input] &&
  122. command.keyCommand.modifierFlags == key.modifierFlags &&
  123. command.block) {
  124. firstPress = NO;
  125. lastCommand = CACurrentMediaTime();
  126. lastInput = key.input;
  127. lastModifierFlags = key.modifierFlags;
  128. return;
  129. }
  130. }
  131. } else {
  132. // Second keyevent within 0.2 second,
  133. // with the same key as the first one.
  134. if (CACurrentMediaTime() - lastCommand < 0.2 &&
  135. lastInput == key.input &&
  136. lastModifierFlags == key.modifierFlags) {
  137. for (RCTKeyCommand *command in [RCTKeyCommands sharedInstance].commands) {
  138. if ([command.keyCommand.input isEqualToString:key.input] &&
  139. command.keyCommand.modifierFlags == key.modifierFlags &&
  140. command.block) {
  141. // NOTE: throttle the key handler because on iOS 9 the handleKeyCommand:
  142. // method gets called repeatedly if the command key is held down.
  143. if (RCTIsIOS8OrEarlier() || CACurrentMediaTime() - lastDoubleCommand > 0.5) {
  144. command.block(key);
  145. lastDoubleCommand = CACurrentMediaTime();
  146. }
  147. firstPress = YES;
  148. return;
  149. }
  150. }
  151. }
  152. lastCommand = CACurrentMediaTime();
  153. lastInput = key.input;
  154. lastModifierFlags = key.modifierFlags;
  155. }
  156. }
  157. @end
  158. @implementation UIApplication (RCTKeyCommands)
  159. // Required for iOS 8.x
  160. - (BOOL)RCT_sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event
  161. {
  162. if (action == @selector(RCT_handleKeyCommand:)) {
  163. [self RCT_handleKeyCommand:sender];
  164. return YES;
  165. } else if (action == @selector(RCT_handleDoublePressKeyCommand:)) {
  166. [self RCT_handleDoublePressKeyCommand:sender];
  167. return YES;
  168. }
  169. return [self RCT_sendAction:action to:target from:sender forEvent:event];
  170. }
  171. @end
  172. @implementation RCTKeyCommands
  173. + (void)initialize
  174. {
  175. if (RCTIsIOS8OrEarlier()) {
  176. // swizzle UIApplication
  177. RCTSwapInstanceMethods([UIApplication class],
  178. @selector(keyCommands),
  179. @selector(RCT_keyCommands));
  180. RCTSwapInstanceMethods([UIApplication class],
  181. @selector(sendAction:to:from:forEvent:),
  182. @selector(RCT_sendAction:to:from:forEvent:));
  183. } else {
  184. // swizzle UIResponder
  185. RCTSwapInstanceMethods([UIResponder class],
  186. @selector(keyCommands),
  187. @selector(RCT_keyCommands));
  188. }
  189. }
  190. + (instancetype)sharedInstance
  191. {
  192. static RCTKeyCommands *sharedInstance;
  193. static dispatch_once_t onceToken;
  194. dispatch_once(&onceToken, ^{
  195. sharedInstance = [self new];
  196. });
  197. return sharedInstance;
  198. }
  199. - (instancetype)init
  200. {
  201. if ((self = [super init])) {
  202. _commands = [NSMutableSet new];
  203. }
  204. return self;
  205. }
  206. - (void)registerKeyCommandWithInput:(NSString *)input
  207. modifierFlags:(UIKeyModifierFlags)flags
  208. action:(void (^)(UIKeyCommand *))block
  209. {
  210. RCTAssertMainQueue();
  211. if (input.length && flags && RCTIsIOS8OrEarlier()) {
  212. // Workaround around the first cmd not working: http://openradar.appspot.com/19613391
  213. // You can register just the cmd key and do nothing. This ensures that
  214. // command-key modified commands will work first time. Fixed in iOS 9.
  215. [self registerKeyCommandWithInput:@""
  216. modifierFlags:flags
  217. action:nil];
  218. }
  219. UIKeyCommand *command = [UIKeyCommand keyCommandWithInput:input
  220. modifierFlags:flags
  221. action:@selector(RCT_handleKeyCommand:)];
  222. RCTKeyCommand *keyCommand = [[RCTKeyCommand alloc] initWithKeyCommand:command block:block];
  223. [_commands removeObject:keyCommand];
  224. [_commands addObject:keyCommand];
  225. }
  226. - (void)unregisterKeyCommandWithInput:(NSString *)input
  227. modifierFlags:(UIKeyModifierFlags)flags
  228. {
  229. RCTAssertMainQueue();
  230. for (RCTKeyCommand *command in _commands.allObjects) {
  231. if ([command matchesInput:input flags:flags]) {
  232. [_commands removeObject:command];
  233. break;
  234. }
  235. }
  236. }
  237. - (BOOL)isKeyCommandRegisteredForInput:(NSString *)input
  238. modifierFlags:(UIKeyModifierFlags)flags
  239. {
  240. RCTAssertMainQueue();
  241. for (RCTKeyCommand *command in _commands) {
  242. if ([command matchesInput:input flags:flags]) {
  243. return YES;
  244. }
  245. }
  246. return NO;
  247. }
  248. - (void)registerDoublePressKeyCommandWithInput:(NSString *)input
  249. modifierFlags:(UIKeyModifierFlags)flags
  250. action:(void (^)(UIKeyCommand *))block
  251. {
  252. RCTAssertMainQueue();
  253. if (input.length && flags && RCTIsIOS8OrEarlier()) {
  254. // Workaround around the first cmd not working: http://openradar.appspot.com/19613391
  255. // You can register just the cmd key and do nothing. This ensures that
  256. // command-key modified commands will work first time. Fixed in iOS 9.
  257. [self registerDoublePressKeyCommandWithInput:@""
  258. modifierFlags:flags
  259. action:nil];
  260. }
  261. UIKeyCommand *command = [UIKeyCommand keyCommandWithInput:input
  262. modifierFlags:flags
  263. action:@selector(RCT_handleDoublePressKeyCommand:)];
  264. RCTKeyCommand *keyCommand = [[RCTKeyCommand alloc] initWithKeyCommand:command block:block];
  265. [_commands removeObject:keyCommand];
  266. [_commands addObject:keyCommand];
  267. }
  268. - (void)unregisterDoublePressKeyCommandWithInput:(NSString *)input
  269. modifierFlags:(UIKeyModifierFlags)flags
  270. {
  271. RCTAssertMainQueue();
  272. for (RCTKeyCommand *command in _commands.allObjects) {
  273. if ([command matchesInput:input flags:flags]) {
  274. [_commands removeObject:command];
  275. break;
  276. }
  277. }
  278. }
  279. - (BOOL)isDoublePressKeyCommandRegisteredForInput:(NSString *)input
  280. modifierFlags:(UIKeyModifierFlags)flags
  281. {
  282. RCTAssertMainQueue();
  283. for (RCTKeyCommand *command in _commands) {
  284. if ([command matchesInput:input flags:flags]) {
  285. return YES;
  286. }
  287. }
  288. return NO;
  289. }
  290. @end
  291. #else
  292. @implementation RCTKeyCommands
  293. + (instancetype)sharedInstance
  294. {
  295. return nil;
  296. }
  297. - (void)registerKeyCommandWithInput:(NSString *)input
  298. modifierFlags:(UIKeyModifierFlags)flags
  299. action:(void (^)(UIKeyCommand *))block {}
  300. - (void)unregisterKeyCommandWithInput:(NSString *)input
  301. modifierFlags:(UIKeyModifierFlags)flags {}
  302. - (BOOL)isKeyCommandRegisteredForInput:(NSString *)input
  303. modifierFlags:(UIKeyModifierFlags)flags
  304. {
  305. return NO;
  306. }
  307. - (void)registerDoublePressKeyCommandWithInput:(NSString *)input
  308. modifierFlags:(UIKeyModifierFlags)flags
  309. action:(void (^)(UIKeyCommand *))block {}
  310. - (void)unregisterDoublePressKeyCommandWithInput:(NSString *)input
  311. modifierFlags:(UIKeyModifierFlags)flags {}
  312. - (BOOL)isDoublePressKeyCommandRegisteredForInput:(NSString *)input
  313. modifierFlags:(UIKeyModifierFlags)flags
  314. {
  315. return NO;
  316. }
  317. @end
  318. #endif