Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <const char* ComponentName, bool keepsMeasurementOnClone>
template <
const char* ComponentName,
bool keepsMeasurementOnClone,
bool connectsThroughTrait = true>
class FontScaledShadowNode final
: public ConcreteViewShadowNode<ComponentName> {
public:
using Base = ConcreteViewShadowNode<ComponentName>;
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;
}

Expand All @@ -827,6 +847,8 @@ using FontScaledTextShadowNode =
FontScaledShadowNode<FontScaledTextComponentName, true>;
using FontScaledInputShadowNode =
FontScaledShadowNode<FontScaledInputComponentName, false>;
using FontScaledTextWithoutTraitShadowNode =
FontScaledShadowNode<FontScaledTextWithoutTraitComponentName, true, false>;

// `ModalHostViewShadowNode` needs JNI to create its initial state on Android.
#ifndef ANDROID
Expand Down Expand Up @@ -858,6 +880,9 @@ class FontScaleLayoutTest : public ::testing::Test {
providerRegistry_.add(
concreteComponentDescriptorProvider<
ConcreteComponentDescriptor<FontScaledInputShadowNode>>());
providerRegistry_.add(
concreteComponentDescriptorProvider<ConcreteComponentDescriptor<
FontScaledTextWithoutTraitShadowNode>>());
}

// Clones the root with a new font scale, as
Expand Down Expand Up @@ -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<RootShadowNode>{};
auto leafShadowNode = std::shared_ptr<FontScaledTextWithoutTraitShadowNode>{};

// clang-format off
auto element =
Element<RootShadowNode>()
.reference(rootShadowNode)
.tag(1)
.children({
Element<ViewShadowNode>()
.tag(2)
.children({
Element<ViewShadowNode>()
.tag(3)
.children({
Element<FontScaledTextWithoutTraitShadowNode>()
.reference(leafShadowNode)
.tag(4)
})
})
});
// clang-format on

builder_.build(element);
EXPECT_TRUE(rootShadowNode->layoutIfNeeded());

auto clonedRootShadowNode =
std::static_pointer_cast<RootShadowNode>(rootShadowNode->cloneTree(
leafShadowNode->getFamily(), [](const ShadowNode& oldShadowNode) {
return oldShadowNode.clone(
ShadowNodeFragment{.props = std::make_shared<ViewProps>()});
}));
EXPECT_TRUE(clonedRootShadowNode->layoutIfNeeded());

auto expectLaidOutWithFontScale = [](const RootShadowNode& root,
Float fontSizeMultiplier) {
const auto& leaf = static_cast<const LayoutableShadowNode&>(
*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<RootShadowNode>{};
auto hiddenShadowNode = std::shared_ptr<ViewShadowNode>{};
Expand Down
Loading