diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpp index 31249bff4c4..3faba2b4dcb 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpp @@ -15,8 +15,11 @@ namespace reanimated { ReanimatedCommitHook::ReanimatedCommitHook( const std::shared_ptr &propsRegistry, - const std::shared_ptr &uiManager) - : propsRegistry_(propsRegistry), uiManager_(uiManager) { + const std::shared_ptr &uiManager, + const std::shared_ptr &layoutAnimationsProxy) + : propsRegistry_(propsRegistry), + uiManager_(uiManager), + layoutAnimationsProxy_(layoutAnimationsProxy) { uiManager_->registerCommitHook(*this); } @@ -28,6 +31,20 @@ RootShadowNode::Unshared ReanimatedCommitHook::shadowTreeWillCommit( ShadowTree const &, RootShadowNode::Shared const &, RootShadowNode::Unshared const &newRootShadowNode) noexcept { + auto surfaceId = newRootShadowNode->getSurfaceId(); + + { + auto lock = std::unique_lock(mutex_); + if (surfaceId > currentMaxSurfaceId_) { + uiManager_->getShadowTreeRegistry().enumerate( + [this](const ShadowTree &shadowTree, bool &stop) { + shadowTree.getMountingCoordinator()->setMountingOverrideDelegate( + layoutAnimationsProxy_); + }); + currentMaxSurfaceId_ = surfaceId; + } + } + auto reaShadowNode = std::reinterpret_pointer_cast( newRootShadowNode); diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.h b/packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.h index bba87751869..ca0708c6544 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.h +++ b/packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.h @@ -2,6 +2,7 @@ #ifdef RCT_NEW_ARCH_ENABLED #include +#include #include @@ -15,7 +16,8 @@ class ReanimatedCommitHook : public UIManagerCommitHook { public: ReanimatedCommitHook( const std::shared_ptr &propsRegistry, - const std::shared_ptr &uiManager); + const std::shared_ptr &uiManager, + const std::shared_ptr &layoutAnimationsProxy); ~ReanimatedCommitHook() noexcept override; @@ -44,6 +46,12 @@ class ReanimatedCommitHook : public UIManagerCommitHook { std::shared_ptr propsRegistry_; std::shared_ptr uiManager_; + + std::shared_ptr layoutAnimationsProxy_; + + SurfaceId currentMaxSurfaceId_ = -1; + + std::mutex mutex_; // Protects `currentMaxSurfaceId_`. }; } // namespace reanimated diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.cpp index 81c2a9ccf8f..b92552f8857 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.cpp @@ -852,15 +852,15 @@ void NativeReanimatedModule::initializeFabric( const std::shared_ptr &uiManager) { uiManager_ = uiManager; - initializeLayoutAnimations(); + initializeLayoutAnimationsProxy(); mountHook_ = std::make_shared(propsRegistry_, uiManager_); - commitHook_ = - std::make_shared(propsRegistry_, uiManager_); + commitHook_ = std::make_shared( + propsRegistry_, uiManager_, layoutAnimationsProxy_); } -void NativeReanimatedModule::initializeLayoutAnimations() { +void NativeReanimatedModule::initializeLayoutAnimationsProxy() { uiManager_->setAnimationDelegate(nullptr); auto scheduler = reinterpret_cast(uiManager_->getDelegate()); auto componentDescriptorRegistry = @@ -876,13 +876,9 @@ void NativeReanimatedModule::initializeLayoutAnimations() { scheduler->getContextContainer(), uiWorkletRuntime_->getJSIRuntime(), uiScheduler_); - uiManager_->getShadowTreeRegistry().enumerate( - [this](const ShadowTree &shadowTree, bool &stop) { - shadowTree.getMountingCoordinator()->setMountingOverrideDelegate( - layoutAnimationsProxy_); - }); } } + #endif // RCT_NEW_ARCH_ENABLED jsi::Value NativeReanimatedModule::subscribeForKeyboardEvents( diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.h b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.h index 4b1548f7163..f44d6fff725 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.h +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.h @@ -137,7 +137,7 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec { void initializeFabric(const std::shared_ptr &uiManager); - void initializeLayoutAnimations(); + void initializeLayoutAnimationsProxy(); std::string obtainPropFromShadowNode( jsi::Runtime &rt,