RCTKeyboardObserver.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "RCTKeyboardObserver.h"
  8. #import "RCTEventDispatcher.h"
  9. static NSDictionary *RCTParseKeyboardNotification(NSNotification *notification);
  10. @implementation RCTKeyboardObserver
  11. RCT_EXPORT_MODULE()
  12. - (void)startObserving
  13. {
  14. #if !TARGET_OS_TV
  15. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  16. #define ADD_KEYBOARD_HANDLER(NAME, SELECTOR) \
  17. [nc addObserver:self selector:@selector(SELECTOR:) name:NAME object:nil]
  18. ADD_KEYBOARD_HANDLER(UIKeyboardWillShowNotification, keyboardWillShow);
  19. ADD_KEYBOARD_HANDLER(UIKeyboardDidShowNotification, keyboardDidShow);
  20. ADD_KEYBOARD_HANDLER(UIKeyboardWillHideNotification, keyboardWillHide);
  21. ADD_KEYBOARD_HANDLER(UIKeyboardDidHideNotification, keyboardDidHide);
  22. ADD_KEYBOARD_HANDLER(UIKeyboardWillChangeFrameNotification, keyboardWillChangeFrame);
  23. ADD_KEYBOARD_HANDLER(UIKeyboardDidChangeFrameNotification, keyboardDidChangeFrame);
  24. #undef ADD_KEYBOARD_HANDLER
  25. #endif
  26. }
  27. - (NSArray<NSString *> *)supportedEvents
  28. {
  29. return @[@"keyboardWillShow",
  30. @"keyboardDidShow",
  31. @"keyboardWillHide",
  32. @"keyboardDidHide",
  33. @"keyboardWillChangeFrame",
  34. @"keyboardDidChangeFrame"];
  35. }
  36. - (void)stopObserving
  37. {
  38. [[NSNotificationCenter defaultCenter] removeObserver:self];
  39. }
  40. // Bridge might be already invalidated by the time the keyboard is about to be dismissed.
  41. // This might happen, for example, when reload from the packager is performed.
  42. // Thus we need to check against nil here.
  43. #define IMPLEMENT_KEYBOARD_HANDLER(EVENT) \
  44. - (void)EVENT:(NSNotification *)notification \
  45. { \
  46. if (!self.bridge) { \
  47. return; \
  48. } \
  49. [self sendEventWithName:@#EVENT \
  50. body:RCTParseKeyboardNotification(notification)]; \
  51. }
  52. IMPLEMENT_KEYBOARD_HANDLER(keyboardWillShow)
  53. IMPLEMENT_KEYBOARD_HANDLER(keyboardDidShow)
  54. IMPLEMENT_KEYBOARD_HANDLER(keyboardWillHide)
  55. IMPLEMENT_KEYBOARD_HANDLER(keyboardDidHide)
  56. IMPLEMENT_KEYBOARD_HANDLER(keyboardWillChangeFrame)
  57. IMPLEMENT_KEYBOARD_HANDLER(keyboardDidChangeFrame)
  58. @end
  59. NS_INLINE NSDictionary *RCTRectDictionaryValue(CGRect rect)
  60. {
  61. return @{
  62. @"screenX": @(rect.origin.x),
  63. @"screenY": @(rect.origin.y),
  64. @"width": @(rect.size.width),
  65. @"height": @(rect.size.height),
  66. };
  67. }
  68. static NSString *RCTAnimationNameForCurve(UIViewAnimationCurve curve)
  69. {
  70. switch (curve) {
  71. case UIViewAnimationCurveEaseIn:
  72. return @"easeIn";
  73. case UIViewAnimationCurveEaseInOut:
  74. return @"easeInEaseOut";
  75. case UIViewAnimationCurveEaseOut:
  76. return @"easeOut";
  77. case UIViewAnimationCurveLinear:
  78. return @"linear";
  79. default:
  80. return @"keyboard";
  81. }
  82. }
  83. static NSDictionary *RCTParseKeyboardNotification(NSNotification *notification)
  84. {
  85. #if TARGET_OS_TV
  86. return @{};
  87. #else
  88. NSDictionary *userInfo = notification.userInfo;
  89. CGRect beginFrame = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
  90. CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  91. NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  92. UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
  93. NSInteger isLocalUserInfoKey = [userInfo[UIKeyboardIsLocalUserInfoKey] integerValue];
  94. return @{
  95. @"startCoordinates": RCTRectDictionaryValue(beginFrame),
  96. @"endCoordinates": RCTRectDictionaryValue(endFrame),
  97. @"duration": @(duration * 1000.0), // ms
  98. @"easing": RCTAnimationNameForCurve(curve),
  99. @"isEventFromThisApp": isLocalUserInfoKey == 1 ? @YES : @NO,
  100. };
  101. #endif
  102. }