RCTRootContentView.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "RCTRootContentView.h"
  8. #import "RCTBridge.h"
  9. #import "RCTPerformanceLogger.h"
  10. #import "RCTRootView.h"
  11. #import "RCTRootViewInternal.h"
  12. #import "RCTTouchHandler.h"
  13. #import "RCTUIManager.h"
  14. #import "UIView+React.h"
  15. @implementation RCTRootContentView
  16. - (instancetype)initWithFrame:(CGRect)frame
  17. bridge:(RCTBridge *)bridge
  18. reactTag:(NSNumber *)reactTag
  19. sizeFlexiblity:(RCTRootViewSizeFlexibility)sizeFlexibility
  20. {
  21. if ((self = [super initWithFrame:frame])) {
  22. _bridge = bridge;
  23. self.reactTag = reactTag;
  24. _sizeFlexibility = sizeFlexibility;
  25. _touchHandler = [[RCTTouchHandler alloc] initWithBridge:_bridge];
  26. [_touchHandler attachToView:self];
  27. [_bridge.uiManager registerRootView:self];
  28. }
  29. return self;
  30. }
  31. RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame:(CGRect)frame)
  32. RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder:(nonnull NSCoder *)aDecoder)
  33. - (void)layoutSubviews
  34. {
  35. [super layoutSubviews];
  36. [self updateAvailableSize];
  37. }
  38. - (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex
  39. {
  40. [super insertReactSubview:subview atIndex:atIndex];
  41. [_bridge.performanceLogger markStopForTag:RCTPLTTI];
  42. dispatch_async(dispatch_get_main_queue(), ^{
  43. if (!self->_contentHasAppeared) {
  44. self->_contentHasAppeared = YES;
  45. [[NSNotificationCenter defaultCenter] postNotificationName:RCTContentDidAppearNotification
  46. object:self.superview];
  47. }
  48. });
  49. }
  50. - (void)setSizeFlexibility:(RCTRootViewSizeFlexibility)sizeFlexibility
  51. {
  52. if (_sizeFlexibility == sizeFlexibility) {
  53. return;
  54. }
  55. _sizeFlexibility = sizeFlexibility;
  56. [self setNeedsLayout];
  57. }
  58. - (CGSize)availableSize
  59. {
  60. CGSize size = self.bounds.size;
  61. return CGSizeMake(
  62. _sizeFlexibility & RCTRootViewSizeFlexibilityWidth ? INFINITY : size.width,
  63. _sizeFlexibility & RCTRootViewSizeFlexibilityHeight ? INFINITY : size.height
  64. );
  65. }
  66. - (void)updateAvailableSize
  67. {
  68. if (!self.reactTag || !_bridge.isValid) {
  69. return;
  70. }
  71. [_bridge.uiManager setAvailableSize:self.availableSize forRootView:self];
  72. }
  73. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
  74. {
  75. // The root content view itself should never receive touches
  76. UIView *hitView = [super hitTest:point withEvent:event];
  77. if (_passThroughTouches && hitView == self) {
  78. return nil;
  79. }
  80. return hitView;
  81. }
  82. - (void)invalidate
  83. {
  84. if (self.userInteractionEnabled) {
  85. self.userInteractionEnabled = NO;
  86. [(RCTRootView *)self.superview contentViewInvalidated];
  87. [_bridge enqueueJSCall:@"AppRegistry"
  88. method:@"unmountApplicationComponentAtRootTag"
  89. args:@[self.reactTag]
  90. completion:NULL];
  91. }
  92. }
  93. @end