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 9cc7e768b30..895c0bc3ecf 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp @@ -160,7 +160,7 @@ YogaLayoutableShadowNode::YogaLayoutableShadowNode( void YogaLayoutableShadowNode::completeClone( const ShadowNode& /*sourceShadowNode*/, const ShadowNodeFragment& fragment) { - if (getTraits().check(ShadowNodeTraits::Trait::MeasurableYogaNode) && + if (yogaNode_.hasMeasureFunc() && // New children means we must always dirty to visit. Otherwise, ask the // Node if the new revision invalidates measurement. (fragment.children || @@ -564,7 +564,7 @@ void YogaLayoutableShadowNode::configureYogaTree( // re-measurement. We propagate up to the root so an unchanged, still-cached // ancestor isn't skipped by `calculateLayoutInternal` before reaching us. An // owner that is already dirty stops it; the child loop below covers that. - if (getTraits().check(ShadowNodeTraits::Trait::MeasurableYogaNode) && + if (yogaNode_.hasMeasureFunc() && !floatEquality( getLayoutMetrics().fontSizeMultiplier, fontSizeMultiplier)) { yogaNode_.markDirtyAndPropagate(); diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/tests/ViewTest.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/tests/ViewTest.cpp index 1e0a06b31da..a7c17daea6e 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/tests/ViewTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/tests/ViewTest.cpp @@ -784,24 +784,44 @@ TEST_F(YogaCloneTest, cloneTreeClonesPathPlusSiblings) { static const char FontScaledTextComponentName[] = "FontScaledText"; // NOLINTNEXTLINE(modernize-avoid-c-arrays) static const char FontScaledInputComponentName[] = "FontScaledInput"; +// NOLINTNEXTLINE(modernize-avoid-c-arrays) +static const char FontScaledTextWithoutTraitComponentName[] = + "FontScaledTextWithoutTrait"; /* * A measurable leaf whose height follows the font scale. With * `keepsMeasurementOnClone` it keeps its measurement when cloned without new * props, like `ParagraphShadowNode`; without it, every clone dirties it, like - * a text input. + * a text input. Without `connectsThroughTrait` it lacks the + * `MeasurableYogaNode` trait and connects its measure function with + * `enableMeasurement()`. */ -template +template < + const char* ComponentName, + bool keepsMeasurementOnClone, + bool connectsThroughTrait = true> class FontScaledShadowNode final : public ConcreteViewShadowNode { public: using Base = ConcreteViewShadowNode; using Base::Base; + FontScaledShadowNode( + const ShadowNodeFragment& fragment, + const ShadowNodeFamily::Shared& family, + ShadowNodeTraits traits) + : Base(fragment, family, traits) { + if constexpr (!connectsThroughTrait) { + this->enableMeasurement(); + } + } + static ShadowNodeTraits BaseTraits() { auto traits = Base::BaseTraits(); traits.set(ShadowNodeTraits::Trait::LeafYogaNode); - traits.set(ShadowNodeTraits::Trait::MeasurableYogaNode); + if constexpr (connectsThroughTrait) { + traits.set(ShadowNodeTraits::Trait::MeasurableYogaNode); + } return traits; } @@ -827,6 +847,8 @@ using FontScaledTextShadowNode = FontScaledShadowNode; using FontScaledInputShadowNode = FontScaledShadowNode; +using FontScaledTextWithoutTraitShadowNode = + FontScaledShadowNode; // `ModalHostViewShadowNode` needs JNI to create its initial state on Android. #ifndef ANDROID @@ -858,6 +880,9 @@ class FontScaleLayoutTest : public ::testing::Test { providerRegistry_.add( concreteComponentDescriptorProvider< ConcreteComponentDescriptor>()); + providerRegistry_.add( + concreteComponentDescriptorProvider>()); } // Clones the root with a new font scale, as @@ -968,6 +993,61 @@ TEST_F(FontScaleLayoutTest, fontScaleChangeLaysOutInputLikeNodeWithoutModal) { expectLaidOutWithFontScale(*smallerRootShadowNode, 1); } +// A leaf connected by `enableMeasurement()` without the trait is measured again +// when its props or the font scale change. +TEST_F( + FontScaleLayoutTest, + enableMeasurementNodeIsMeasuredAgainOnPropsAndFontScaleChanges) { + auto rootShadowNode = std::shared_ptr{}; + auto leafShadowNode = std::shared_ptr{}; + + // clang-format off + auto element = + Element() + .reference(rootShadowNode) + .tag(1) + .children({ + Element() + .tag(2) + .children({ + Element() + .tag(3) + .children({ + Element() + .reference(leafShadowNode) + .tag(4) + }) + }) + }); + // clang-format on + + builder_.build(element); + EXPECT_TRUE(rootShadowNode->layoutIfNeeded()); + + auto clonedRootShadowNode = + std::static_pointer_cast(rootShadowNode->cloneTree( + leafShadowNode->getFamily(), [](const ShadowNode& oldShadowNode) { + return oldShadowNode.clone( + ShadowNodeFragment{.props = std::make_shared()}); + })); + EXPECT_TRUE(clonedRootShadowNode->layoutIfNeeded()); + + auto expectLaidOutWithFontScale = [](const RootShadowNode& root, + Float fontSizeMultiplier) { + const auto& leaf = static_cast( + *root.getChildren().at(0)->getChildren().at(0)->getChildren().at(0)); + EXPECT_EQ(leaf.getLayoutMetrics().fontSizeMultiplier, fontSizeMultiplier); + EXPECT_EQ( + leaf.getLayoutMetrics().frame.size.height, 10 * fontSizeMultiplier); + }; + + auto largerRootShadowNode = layOutWithFontScale(*clonedRootShadowNode, 1.5); + expectLaidOutWithFontScale(*largerRootShadowNode, 1.5); + + auto smallerRootShadowNode = layOutWithFontScale(*largerRootShadowNode, 1); + expectLaidOutWithFontScale(*smallerRootShadowNode, 1); +} + TEST_F(FontScaleLayoutTest, fontScaleChangeSkipsHiddenSubtreeUntilShown) { auto rootShadowNode = std::shared_ptr{}; auto hiddenShadowNode = std::shared_ptr{};