RCTRootContentView.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 object:self.superview];
  46. }
  47. });
  48. }
  49. - (void)setSizeFlexibility:(RCTRootViewSizeFlexibility)sizeFlexibility
  50. {
  51. if (_sizeFlexibility == sizeFlexibility) {
  52. return;
  53. }
  54. _sizeFlexibility = sizeFlexibility;
  55. [self setNeedsLayout];
  56. }
  57. - (CGSize)availableSize
  58. {
  59. CGSize size = self.bounds.size;
  60. return CGSizeMake(
  61. _sizeFlexibility & RCTRootViewSizeFlexibilityWidth ? INFINITY : size.width,
  62. _sizeFlexibility & RCTRootViewSizeFlexibilityHeight ? INFINITY : size.height);
  63. }
  64. - (void)updateAvailableSize
  65. {
  66. if (!self.reactTag || !_bridge.isValid) {
  67. return;
  68. }
  69. [_bridge.uiManager setAvailableSize:self.availableSize forRootView:self];
  70. }
  71. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
  72. {
  73. // The root content view itself should never receive touches
  74. UIView *hitView = [super hitTest:point withEvent:event];
  75. if (_passThroughTouches && hitView == self) {
  76. return nil;
  77. }
  78. return hitView;
  79. }
  80. - (void)invalidate
  81. {
  82. if (self.userInteractionEnabled) {
  83. self.userInteractionEnabled = NO;
  84. [(RCTRootView *)self.superview contentViewInvalidated];
  85. [_bridge enqueueJSCall:@"AppRegistry"
  86. method:@"unmountApplicationComponentAtRootTag"
  87. args:@[ self.reactTag ]
  88. completion:NULL];
  89. }
  90. }
  91. @end