diff --git a/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp index 6a323fed40a..a78770d4555 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp @@ -58,28 +58,15 @@ ParagraphShadowNode::ParagraphShadowNode( const ShadowNode& sourceShadowNode, const ShadowNodeFragment& fragment) : ConcreteViewShadowNode(sourceShadowNode, fragment) { - auto& sourceParagraphShadowNode = - static_cast(sourceShadowNode); - auto& state = getStateData(); - const auto& sourceContent = sourceParagraphShadowNode.content_; - - if (!fragment.children && !fragment.props && - sourceParagraphShadowNode.getIsLayoutClean() && - (!ReactNativeFeatureFlags::enableFontScaleChangesUpdatingLayout() || - (sourceContent.has_value() && - sourceContent.value() - .attributedString.getBaseTextAttributes() - .fontSizeMultiplier == - state.attributedString.getBaseTextAttributes() - .fontSizeMultiplier))) { - // This ParagraphShadowNode was cloned but did not change - // in a way that affects its layout. Let's mark it clean - // to stop Yoga from traversing it. - cleanLayout(); - } initialize(); } +bool ParagraphShadowNode::shouldNewRevisionDirtyMeasurement( + const ShadowNode& /*sourceShadowNode*/, + const ShadowNodeFragment& fragment) const { + return fragment.props != nullptr; +} + const Content& ParagraphShadowNode::getContent( const LayoutContext& layoutContext) const { if (content_.has_value()) { diff --git a/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.h b/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.h index be2a10d75c3..7911e94c465 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.h @@ -90,6 +90,11 @@ class ParagraphShadowNode final : public ConcreteViewShadowNode< Attachments attachments; }; + protected: + bool shouldNewRevisionDirtyMeasurement( + const ShadowNode& sourceShadowNode, + const ShadowNodeFragment& fragment) const override; + private: void initialize() noexcept; /* diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp index 313f68d4e9a..9514703af88 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp @@ -132,13 +132,6 @@ YogaLayoutableShadowNode::YogaLayoutableShadowNode( &yogaNode_, &initializeYogaConfig(yogaConfig_, previousConfig)); updateYogaChildrenOwnersIfNeeded(); - // This is the only legit place where we can dirty cloned Yoga node. - // If we do it later, ancestor nodes will not be able to observe this and - // dirty (and clone) themselves as a result. - if (getTraits().check(ShadowNodeTraits::Trait::MeasurableYogaNode)) { - yogaNode_.setDirty(true); - } - // We do not need to reconfigure this subtree before the next layout pass if // the previous node with the same props and children has already been // configured. @@ -159,8 +152,16 @@ YogaLayoutableShadowNode::YogaLayoutableShadowNode( ensureConsistency(); } -void YogaLayoutableShadowNode::cleanLayout() { - yogaNode_.setDirty(false); +void YogaLayoutableShadowNode::completeClone( + const ShadowNode& /*sourceShadowNode*/, + const ShadowNodeFragment& fragment) { + if (getTraits().check(ShadowNodeTraits::Trait::MeasurableYogaNode) && + // New children means we must always dirty to visit. Otherwise, ask the + // Node if the new revision invalidates measurement. + (fragment.children || + shouldNewRevisionDirtyMeasurement(*this, fragment))) { + yogaNode_.setDirty(true); + } } void YogaLayoutableShadowNode::dirtyLayout() { @@ -317,6 +318,12 @@ bool YogaLayoutableShadowNode::doesOwn( return YGNodeGetOwner(&child.yogaNode_) == &yogaNode_; } +bool YogaLayoutableShadowNode::shouldNewRevisionDirtyMeasurement( + const ShadowNode& /*sourceShadowNode*/, + const ShadowNodeFragment& /*fragment*/) const { + return true; +} + void YogaLayoutableShadowNode::updateYogaChildrenOwnersIfNeeded() { for (auto& childYogaNode : yogaNode_.getChildren()) { if (YGNodeGetOwner(childYogaNode) == &yogaNode_) { diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h index 4bba73ddf04..400152d7249 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h @@ -37,6 +37,10 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode { const ShadowNode& sourceShadowNode, const ShadowNodeFragment& fragment); + void completeClone( + const ShadowNode& sourceShadowNode, + const ShadowNodeFragment& fragment) override; + #pragma mark - Mutating Methods /* @@ -69,7 +73,6 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode { #pragma mark - LayoutableShadowNode - void cleanLayout() override; void dirtyLayout() override; bool getIsLayoutClean() const override; @@ -86,6 +89,14 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode { Rect getContentBounds() const; protected: + /** + * Subclasses which provide MeasurableYogaNode may override to signal that a + * new ShadowNode revision does not need to invalidate existing measurements. + */ + virtual bool shouldNewRevisionDirtyMeasurement( + const ShadowNode& sourceShadowNode, + const ShadowNodeFragment& fragment) const; + /* * Yoga config associated (only) with this particular node. */ diff --git a/packages/react-native/ReactCommon/react/renderer/core/ConcreteComponentDescriptor.h b/packages/react-native/ReactCommon/react/renderer/core/ConcreteComponentDescriptor.h index 462055029c3..c1a3bc0e090 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ConcreteComponentDescriptor.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ConcreteComponentDescriptor.h @@ -80,6 +80,7 @@ class ConcreteComponentDescriptor : public ComponentDescriptor { const ShadowNode& sourceShadowNode, const ShadowNodeFragment& fragment) const override { auto shadowNode = std::make_shared(sourceShadowNode, fragment); + shadowNode->completeClone(sourceShadowNode, fragment); sourceShadowNode.transferRuntimeShadowNodeReference(shadowNode, fragment); adopt(*shadowNode); diff --git a/packages/react-native/ReactCommon/react/renderer/core/LayoutableShadowNode.h b/packages/react-native/ReactCommon/react/renderer/core/LayoutableShadowNode.h index cd9d4efab73..3f586164b03 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/LayoutableShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/core/LayoutableShadowNode.h @@ -146,7 +146,6 @@ class LayoutableShadowNode : public ShadowNode { * Indicates whether all nodes (and possibly their subtrees) along the path * to the root node should be re-laid out. */ - virtual void cleanLayout() = 0; virtual void dirtyLayout() = 0; virtual bool getIsLayoutClean() const = 0; diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.h b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.h index 281588eee1c..071785a4e09 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.h @@ -95,7 +95,7 @@ class ShadowNode : public Sealable, virtual ~ShadowNode() override = default; /* - * Clones the shadow node using stored `cloneFunction`. + * Clones the shadow node using the ShadowNode's ComponentDescriptor. */ std::shared_ptr clone(const ShadowNodeFragment& fragment) const; @@ -124,6 +124,14 @@ class ShadowNode : public Sealable, const ShadowNode& oldShadowNode, const ShadowNodeFragment& fragment)>& callback) const; + /** + * Called, once a fully derived ShadowNode clone has been created via + * ComponentDescriptor::cloneShadowNode. + */ + virtual void completeClone( + const ShadowNode& sourceShadowNode, + const ShadowNodeFragment& fragment) {} + #pragma mark - Getters ComponentName getComponentName() const;