From 6886908b18016a6ac65a23b8b69c448143f3fd2c Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Mon, 8 Apr 2024 23:57:18 -0700 Subject: [PATCH] Use ReactRootViewTagGenerator for RN-iOS Summary: This builds upon https://github.com/facebook/react-native/pull/43882 and re-uses the C++ ReactRootViewTagGenerator for RN-iOS Changelog: [Internal] Differential Revision: D55876627 --- .../Base/{RCTRootView.m => RCTRootView.mm} | 38 ++++++++----------- .../React/Base/Surface/RCTSurface.mm | 6 +-- .../React/Fabric/Surface/RCTFabricSurface.mm | 4 +- .../React/Modules/RCTUIManagerUtils.h | 5 --- .../React/Modules/RCTUIManagerUtils.m | 8 ---- 5 files changed, 20 insertions(+), 41 deletions(-) rename packages/react-native/React/Base/{RCTRootView.m => RCTRootView.mm} (91%) diff --git a/packages/react-native/React/Base/RCTRootView.m b/packages/react-native/React/Base/RCTRootView.mm similarity index 91% rename from packages/react-native/React/Base/RCTRootView.m rename to packages/react-native/React/Base/RCTRootView.mm index 5acd5f03c4c951..5043bebf44282a 100644 --- a/packages/react-native/React/Base/RCTRootView.m +++ b/packages/react-native/React/Base/RCTRootView.mm @@ -10,6 +10,7 @@ #import "RCTRootViewInternal.h" #import +#import #import "RCTAssert.h" #import "RCTBridge+Private.h" @@ -30,16 +31,10 @@ NSString *const RCTContentDidAppearNotification = @"RCTContentDidAppearNotification"; -@interface RCTUIManager (RCTRootView) - -- (NSNumber *)allocateRootTag; - -@end - @implementation RCTRootView { RCTBridge *_bridge; NSString *_moduleName; - RCTRootContentView *_contentView; + UIView *_contentView; BOOL _passThroughTouches; CGSize _intrinsicContentSize; } @@ -125,13 +120,13 @@ - (UIView *)view - (BOOL)passThroughTouches { - return _contentView.passThroughTouches; + return static_cast(_contentView).passThroughTouches; } - (void)setPassThroughTouches:(BOOL)passThroughTouches { _passThroughTouches = passThroughTouches; - _contentView.passThroughTouches = passThroughTouches; + static_cast(_contentView).passThroughTouches = passThroughTouches; } #pragma mark - Layout @@ -166,9 +161,9 @@ - (void)setMinimumSize:(CGSize)minimumSize } _minimumSize = minimumSize; __block NSNumber *tag = self.reactTag; - __weak typeof(self) weakSelf = self; + __weak __typeof(self) weakSelf = self; RCTExecuteOnUIManagerQueue(^{ - __strong typeof(self) strongSelf = weakSelf; + __strong __typeof(self) strongSelf = weakSelf; if (strongSelf && strongSelf->_bridge.isValid) { RCTRootShadowView *shadowView = (RCTRootShadowView *)[strongSelf->_bridge.uiManager shadowViewForReactTag:tag]; shadowView.minimumSize = minimumSize; @@ -189,14 +184,14 @@ - (BOOL)canBecomeFirstResponder - (void)setLoadingView:(UIView *)loadingView { _loadingView = loadingView; - if (!_contentView.contentHasAppeared) { + if (!static_cast(_contentView).contentHasAppeared) { [self showLoadingView]; } } - (void)showLoadingView { - if (_loadingView && !_contentView.contentHasAppeared) { + if (_loadingView && !static_cast(_contentView).contentHasAppeared) { _loadingView.hidden = NO; [self addSubview:_loadingView]; } @@ -204,7 +199,7 @@ - (void)showLoadingView - (void)hideLoadingView { - if (_loadingView.superview == self && _contentView.contentHasAppeared) { + if (_loadingView.superview == self && static_cast(_contentView).contentHasAppeared) { if (_loadingViewFadeDuration > 0) { dispatch_after( dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_loadingViewFadeDelay * NSEC_PER_SEC)), @@ -232,13 +227,10 @@ - (NSNumber *)reactTag RCTAssertMainQueue(); if (!super.reactTag) { /** - * Every root view that is created must have a unique react tag. - * Numbering of these tags goes from 1, 11, 21, 31, etc - * * NOTE: Since the bridge persists, the RootViews might be reused, so the * react tag must be re-assigned every time a new UIManager is created. */ - self.reactTag = RCTAllocateRootViewTag(); + self.reactTag = @(facebook::react::getNextRootViewTag()); } return super.reactTag; } @@ -257,7 +249,7 @@ - (void)javaScriptDidLoad:(NSNotification *)notification // Use the (batched) bridge that's sent in the notification payload, so the // RCTRootContentView is scoped to the right bridge RCTBridge *bridge = notification.userInfo[@"bridge"]; - if (bridge != _contentView.bridge) { + if (bridge != static_cast(_contentView).bridge) { [self bundleFinishedLoading:bridge]; } } @@ -276,7 +268,7 @@ - (void)bundleFinishedLoading:(RCTBridge *)bridge sizeFlexibility:_sizeFlexibility]; [self runApplication:bridge]; - _contentView.passThroughTouches = _passThroughTouches; + static_cast(_contentView).passThroughTouches = _passThroughTouches; [self insertSubview:_contentView atIndex:0]; if (_sizeFlexibility == RCTRootViewSizeFlexibilityNone) { @@ -304,7 +296,7 @@ - (void)setSizeFlexibility:(RCTRootViewSizeFlexibility)sizeFlexibility _sizeFlexibility = sizeFlexibility; [self setNeedsLayout]; - _contentView.sizeFlexibility = _sizeFlexibility; + static_cast(_contentView).sizeFlexibility = _sizeFlexibility; } - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event @@ -382,7 +374,7 @@ - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection - (void)dealloc { - [_contentView invalidate]; + [static_cast(_contentView) invalidate]; } @end @@ -398,7 +390,7 @@ - (CGSize)intrinsicSize - (void)cancelTouches { RCTLogWarn(@"`-[RCTRootView cancelTouches]` is deprecated and will be deleted soon."); - [[_contentView touchHandler] cancel]; + [[static_cast(_contentView) touchHandler] cancel]; } @end diff --git a/packages/react-native/React/Base/Surface/RCTSurface.mm b/packages/react-native/React/Base/Surface/RCTSurface.mm index 2ff9a18c3483fb..a15c37b186ce39 100644 --- a/packages/react-native/React/Base/Surface/RCTSurface.mm +++ b/packages/react-native/React/Base/Surface/RCTSurface.mm @@ -8,9 +8,9 @@ #import "RCTSurface.h" #import "RCTSurfaceView+Internal.h" -#import - +#import #import +#import #import "RCTAssert.h" #import "RCTBridge+Private.h" @@ -73,7 +73,7 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge _batchedBridge = [_bridge batchedBridge] ?: _bridge; _moduleName = moduleName; _properties = [initialProperties copy]; - _rootViewTag = RCTAllocateRootViewTag(); + _rootViewTag = @(facebook::react::getNextRootViewTag()); _rootShadowViewDidStartRenderingSemaphore = dispatch_semaphore_create(0); _rootShadowViewDidStartLayingOutSemaphore = dispatch_semaphore_create(0); diff --git a/packages/react-native/React/Fabric/Surface/RCTFabricSurface.mm b/packages/react-native/React/Fabric/Surface/RCTFabricSurface.mm index 0567a52fd8915c..f7866246c63f1a 100644 --- a/packages/react-native/React/Fabric/Surface/RCTFabricSurface.mm +++ b/packages/react-native/React/Fabric/Surface/RCTFabricSurface.mm @@ -22,6 +22,7 @@ #import #import #import +#import #import #import "RCTSurfacePresenter.h" @@ -56,8 +57,7 @@ - (instancetype)initWithSurfacePresenter:(RCTSurfacePresenter *)surfacePresenter if (self = [super init]) { _surfacePresenter = surfacePresenter; - _surfaceHandler = - SurfaceHandler{RCTStringFromNSString(moduleName), (SurfaceId)[RCTAllocateRootViewTag() integerValue]}; + _surfaceHandler = SurfaceHandler{RCTStringFromNSString(moduleName), getNextRootViewTag()}; _surfaceHandler->setProps(convertIdToFollyDynamic(initialProperties)); [_surfacePresenter registerSurface:self]; diff --git a/packages/react-native/React/Modules/RCTUIManagerUtils.h b/packages/react-native/React/Modules/RCTUIManagerUtils.h index 80228dbd71a436..e4c621358aebc4 100644 --- a/packages/react-native/React/Modules/RCTUIManagerUtils.h +++ b/packages/react-native/React/Modules/RCTUIManagerUtils.h @@ -98,8 +98,3 @@ RCT_EXTERN void RCTUnsafeExecuteOnUIManagerQueueSync(dispatch_block_t block); #define RCTAssertUIManagerQueue() \ RCTAssert( \ RCTIsUIManagerQueue() || RCTIsPseudoUIManagerQueue(), @"This function must be called on the UIManager queue") - -/** - * Returns new unique root view tag. - */ -RCT_EXTERN NSNumber *RCTAllocateRootViewTag(void); diff --git a/packages/react-native/React/Modules/RCTUIManagerUtils.m b/packages/react-native/React/Modules/RCTUIManagerUtils.m index 66c8471ca51398..e30fb0d35f3837 100644 --- a/packages/react-native/React/Modules/RCTUIManagerUtils.m +++ b/packages/react-native/React/Modules/RCTUIManagerUtils.m @@ -94,11 +94,3 @@ void RCTUnsafeExecuteOnUIManagerQueueSync(dispatch_block_t block) } } } - -NSNumber *RCTAllocateRootViewTag(void) -{ - // Keep in sync with ReactRootViewTagGenerator.h - see that file for an explanation on why the - // increment here is 10. - static _Atomic int64_t rootViewTagCounter = 0; - return @(atomic_fetch_add_explicit(&rootViewTagCounter, 1, memory_order_relaxed) * 10 + 1); -}