Remove YogaLayoutableShadowNode::cleanLayout() and Fix ParagraphShadowNode Font Size Invalidation Logic (#51760)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/51760

This API is evil.

Yoga's public API never allows a dirty node to become clean again, until its laid out, but this API requires doing that, since we will otherwise automatically dirty by default.

Let's replace it with `YogaLayoutableShadowNode::shouldNewRevisionDirtyMeasurement()`, which lets individual ShadowNodes represent whether a new revision's state and props should cause dirtying, defaulting to true.

Changelog:
[General][Removed] - Remove `YogaLayoutableShadowNode::cleanLayout()`

Reviewed By: lenaic

Differential Revision: D75479902

fbshipit-source-id: a40aa531522a76dc49feb7d12bae5a8d877c6c06
This commit is contained in:
Nick Gerleman authored and Facebook GitHub Bot committed 2025-06-02 19:52:31 -07:00
1 parent 5c8c5388fc
commit 7979c7ce06
7 files changed
+49 -31

No files matched your search

@@ -58,28 +58,15 @@ ParagraphShadowNode::ParagraphShadowNode(
const ShadowNode& sourceShadowNode,
const ShadowNodeFragment& fragment)
: ConcreteViewShadowNode(sourceShadowNode, fragment) {
auto& sourceParagraphShadowNode =
static_cast<const ParagraphShadowNode&>(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()) {
@@ -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;
/*
@@ -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_) {
@@ -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.
*/
@@ -80,6 +80,7 @@ class ConcreteComponentDescriptor : public ComponentDescriptor {
const ShadowNode& sourceShadowNode,
const ShadowNodeFragment& fragment) const override {
auto shadowNode = std::make_shared<ShadowNodeT>(sourceShadowNode, fragment);
shadowNode->completeClone(sourceShadowNode, fragment);
sourceShadowNode.transferRuntimeShadowNodeReference(shadowNode, fragment);
adopt(*shadowNode);
@@ -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;
@@ -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<ShadowNode> 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;