UIView+ComponentViewProtocol.mm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "UIView+ComponentViewProtocol.h"
  8. #import <React/RCTAssert.h>
  9. #import <React/RCTLog.h>
  10. #import <React/RCTUtils.h>
  11. #import "RCTConversions.h"
  12. using namespace facebook::react;
  13. @implementation UIView (ComponentViewProtocol)
  14. + (ComponentDescriptorProvider)componentDescriptorProvider
  15. {
  16. RCTAssert(NO, @"`-[RCTComponentViewProtocol componentDescriptorProvider]` must be implemented in a concrete class.");
  17. return {};
  18. }
  19. + (std::vector<facebook::react::ComponentDescriptorProvider>)supplementalComponentDescriptorProviders
  20. {
  21. return {};
  22. }
  23. - (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
  24. {
  25. [self insertSubview:childComponentView atIndex:index];
  26. }
  27. - (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
  28. {
  29. RCTAssert(childComponentView.superview == self, @"Attempt to unmount improperly mounted component view.");
  30. [childComponentView removeFromSuperview];
  31. }
  32. - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
  33. {
  34. // Default implementation does nothing.
  35. }
  36. - (void)updateEventEmitter:(EventEmitter::Shared const &)eventEmitter
  37. {
  38. // Default implementation does nothing.
  39. }
  40. - (void)updateState:(facebook::react::State::Shared const &)state
  41. oldState:(facebook::react::State::Shared const &)oldState
  42. {
  43. // Default implementation does nothing.
  44. }
  45. - (void)handleCommand:(NSString *)commandName args:(NSArray *)args
  46. {
  47. // Default implementation does nothing.
  48. }
  49. - (void)updateLayoutMetrics:(LayoutMetrics const &)layoutMetrics
  50. oldLayoutMetrics:(LayoutMetrics const &)oldLayoutMetrics
  51. {
  52. bool forceUpdate = oldLayoutMetrics == EmptyLayoutMetrics;
  53. if (forceUpdate || (layoutMetrics.frame != oldLayoutMetrics.frame)) {
  54. CGRect frame = RCTCGRectFromRect(layoutMetrics.frame);
  55. if (!std::isfinite(frame.origin.x) || !std::isfinite(frame.origin.y) || !std::isfinite(frame.size.width) ||
  56. !std::isfinite(frame.size.height)) {
  57. // CALayer will crash if we pass NaN or Inf values.
  58. // It's unclear how to detect this case on cross-platform manner holistically, so we have to do it on the mounting
  59. // layer as well. NaN/Inf is a kinda valid result of some math operations. Even if we can (and should) detect (and
  60. // report early) incorrect (NaN and Inf) values which come from JavaScript side, we sometimes cannot backtrace the
  61. // sources of a calculation that produced an incorrect/useless result.
  62. RCTLogWarn(
  63. @"-[UIView(ComponentViewProtocol) updateLayoutMetrics:oldLayoutMetrics:]: Received invalid layout metrics (%@) for a view (%@).",
  64. NSStringFromCGRect(frame),
  65. self);
  66. return;
  67. }
  68. // Note: Changing `frame` when `layer.transform` is not the `identity transform` is undefined behavior.
  69. // Therefore, we must use `center` and `bounds`.
  70. self.center = CGPoint{CGRectGetMidX(frame), CGRectGetMidY(frame)};
  71. self.bounds = CGRect{CGPointZero, frame.size};
  72. }
  73. if (forceUpdate || (layoutMetrics.layoutDirection != oldLayoutMetrics.layoutDirection)) {
  74. self.semanticContentAttribute = layoutMetrics.layoutDirection == LayoutDirection::RightToLeft
  75. ? UISemanticContentAttributeForceRightToLeft
  76. : UISemanticContentAttributeForceLeftToRight;
  77. }
  78. if (forceUpdate || (layoutMetrics.displayType != oldLayoutMetrics.displayType)) {
  79. self.hidden = layoutMetrics.displayType == DisplayType::None;
  80. }
  81. }
  82. - (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask
  83. {
  84. // Default implementation does nothing.
  85. }
  86. - (void)prepareForRecycle
  87. {
  88. // Default implementation does nothing.
  89. }
  90. - (facebook::react::SharedProps)props
  91. {
  92. RCTAssert(NO, @"props access should be implemented by RCTViewComponentView.");
  93. return nullptr;
  94. }
  95. @end