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 @@ -668,9 +668,14 @@ void YogaLayoutableShadowNode::layoutTree(
layoutMetrics.pointScaleFactor = layoutContext.pointScaleFactor;
layoutMetrics.wasLeftAndRightSwapped = swapLeftAndRight;
setLayoutMetrics(layoutMetrics);
if (layoutContext.includeOriginFromRoot) {
setOriginFromRoot(layoutMetrics.frame.origin);
}
yogaNode_.setHasNewLayout(false);
}

layoutContext.rootNode = this;

layout(layoutContext);
}

Expand Down Expand Up @@ -728,6 +733,15 @@ void YogaLayoutableShadowNode::layout(LayoutContext layoutContext) {

childNode.setLayoutMetrics(newLayoutMetrics);

if (layoutContext.includeOriginFromRoot) {
childNode.setOriginFromRoot(
LayoutableShadowNode::computeOriginFromRoot(
originFromRoot_,
newLayoutMetrics.frame,
childNode.getTransform(),
childNode.getContentOriginOffset(true)));
}

if (newLayoutMetrics.displayType != DisplayType::None) {
childNode.layout(layoutContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ struct LayoutContext {
* If React Native takes up entire screen, it will be {0, 0}.
*/
Point viewportOffset{};

/*
* Flag indicating whether to calculate and store originFromRoot
* for each LayoutableShadowNode during layout. This is typically
* enabled when laying out children of ViewTransitionViewShadowNode.
*/
bool includeOriginFromRoot{false};

/*
* Pointer to the root node of the layout tree. Set during layoutTree()
* and used by nodes that need to compute their position relative to root.
*/
const LayoutableShadowNode *rootNode{nullptr};
};

inline bool operator==(const LayoutContext &lhs, const LayoutContext &rhs)
Expand All @@ -68,13 +81,17 @@ inline bool operator==(const LayoutContext &lhs, const LayoutContext &rhs)
lhs.affectedNodes,
lhs.swapLeftAndRightInRTL,
lhs.fontSizeMultiplier,
lhs.viewportOffset) ==
lhs.viewportOffset,
lhs.includeOriginFromRoot,
lhs.rootNode) ==
std::tie(
rhs.pointScaleFactor,
rhs.affectedNodes,
rhs.swapLeftAndRightInRTL,
rhs.fontSizeMultiplier,
rhs.viewportOffset);
rhs.viewportOffset,
rhs.includeOriginFromRoot,
rhs.rootNode);
}

inline bool operator!=(const LayoutContext &lhs, const LayoutContext &rhs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <react/renderer/graphics/RectangleEdges.h>
#include <react/utils/hash_combine.h>
#include <algorithm>
#include <limits>

namespace facebook::react {

Expand Down Expand Up @@ -98,6 +99,16 @@ struct LayoutMetrics {
static const LayoutMetrics EmptyLayoutMetrics = {
/* .frame = */ .frame = {.origin = {.x = 0, .y = 0}, .size = {.width = -1.0, .height = -1.0}}};

/*
* Represents some undefined, not-yet-computed or meaningless value of
* `originFromRoot` (Point type).
* Used to indicate that originFromRoot has not been calculated for a node.
* The value uses negative infinity to distinguish from valid coordinates.
*/
static const Point EmptyOriginFromRoot = {
.x = -std::numeric_limits<Float>::infinity(),
.y = -std::numeric_limits<Float>::infinity()};

#if RN_DEBUG_STRING_CONVERTIBLE

std::string getDebugName(const LayoutMetrics &object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ LayoutableShadowNode::LayoutableShadowNode(
: ShadowNode(sourceShadowNode, fragment),
layoutMetrics_(
static_cast<const LayoutableShadowNode&>(sourceShadowNode)
.layoutMetrics_) {}
.layoutMetrics_),
originFromRoot_(
static_cast<const LayoutableShadowNode&>(sourceShadowNode)
.originFromRoot_) {}

LayoutMetrics LayoutableShadowNode::computeLayoutMetricsFromRoot(
const ShadowNodeFamily& descendantNodeFamily,
Expand Down Expand Up @@ -197,10 +200,34 @@ LayoutMetrics LayoutableShadowNode::computeRelativeLayoutMetrics(
return layoutMetrics;
}

Point LayoutableShadowNode::computeOriginFromRoot(
Point parentOriginFromRoot,
const Rect& frame,
const Transform& transform,
Point contentOriginOffset) {
if (parentOriginFromRoot == EmptyOriginFromRoot) {
return EmptyOriginFromRoot;
}

Point resultOrigin = frame.origin;

if (transform != Transform::Identity()) {
resultOrigin = transform.applyWithCenter(frame, frame.getCenter()).origin;
}

resultOrigin += parentOriginFromRoot + contentOriginOffset;

return resultOrigin;
}

LayoutMetrics LayoutableShadowNode::getLayoutMetrics() const {
return layoutMetrics_;
}

Point LayoutableShadowNode::getOriginFromRoot() const {
return originFromRoot_;
}

void LayoutableShadowNode::setLayoutMetrics(LayoutMetrics layoutMetrics) {
ensureUnsealed();

Expand All @@ -211,6 +238,16 @@ void LayoutableShadowNode::setLayoutMetrics(LayoutMetrics layoutMetrics) {
layoutMetrics_ = layoutMetrics;
}

void LayoutableShadowNode::setOriginFromRoot(Point originFromRoot) {
ensureUnsealed();

if (originFromRoot_ == originFromRoot) {
return;
}

originFromRoot_ = originFromRoot;
}

Transform LayoutableShadowNode::getTransform() const {
return Transform::Identity();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ class LayoutableShadowNode : public ShadowNode {
*/
static LayoutMetrics computeRelativeLayoutMetrics(const AncestorList &ancestors, LayoutInspectingPolicy policy);

/*
* Computes the origin from root for a child node given the parent's origin
* from root, the child's layout metrics, transform, and content origin offset.
* This is similar to how computeRelativeLayoutMetrics computes positions, but
* avoids traversing all the ancestors.
*/
static Point computeOriginFromRoot(
Point parentOriginFromRoot,
const Rect &frame,
const Transform &transform,
Point contentOriginOffset);

/*
* Performs layout of the tree starting from this node. Usually is being
* called on the root node.
Expand Down Expand Up @@ -110,6 +122,8 @@ class LayoutableShadowNode : public ShadowNode {
*/
LayoutMetrics getLayoutMetrics() const;

Point getOriginFromRoot() const;

/*
* Returns a transform object that represents transformations that will/should
* be applied on top of regular layout metrics by mounting layer.
Expand All @@ -133,6 +147,8 @@ class LayoutableShadowNode : public ShadowNode {
*/
void setLayoutMetrics(LayoutMetrics layoutMetrics);

void setOriginFromRoot(Point originFromRoot);

/*
* Returns the ShadowNode that is rendered at the Point received as a
* parameter.
Expand Down Expand Up @@ -167,6 +183,8 @@ class LayoutableShadowNode : public ShadowNode {
#endif

LayoutMetrics layoutMetrics_;

Point originFromRoot_{EmptyOriginFromRoot};
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -1431,4 +1431,162 @@ TEST(LayoutableShadowNodeTest, inversedContentOriginOffset) {
EXPECT_EQ(relativeLayoutMetrics.frame.origin.y, 130);
}

/*
* Tests for computeOriginFromRoot utility function.
* These tests cross-validate computeOriginFromRoot against
* computeRelativeLayoutMetrics to ensure they produce consistent results.
*/
TEST(LayoutableShadowNodeTest, computeOriginFromRoot) {
auto builder = simpleComponentBuilder();
auto childShadowNode = std::shared_ptr<ViewShadowNode>{};
// clang-format off
auto element =
Element<ViewShadowNode>()
.finalize([](ViewShadowNode &shadowNode){
auto layoutMetrics = EmptyLayoutMetrics;
layoutMetrics.frame.origin = {.x=100, .y=200};
layoutMetrics.frame.size = {.width=500, .height=500};
shadowNode.setLayoutMetrics(layoutMetrics);
})
.children({
Element<ViewShadowNode>()
.finalize([](ViewShadowNode &shadowNode){
auto layoutMetrics = EmptyLayoutMetrics;
layoutMetrics.frame.origin = {.x=10, .y=20};
layoutMetrics.frame.size = {.width=100, .height=200};
shadowNode.setLayoutMetrics(layoutMetrics);
})
.reference(childShadowNode)
});
// clang-format on

auto parentShadowNode = builder.build(element);

// Get expected origin from computeRelativeLayoutMetrics
auto relativeLayoutMetrics =
LayoutableShadowNode::computeRelativeLayoutMetrics(
childShadowNode->getFamily(), *parentShadowNode, {});

// Compute origin using computeOriginFromRoot
// Parent's origin from root is {0, 0} since parent is the root
Point parentOriginFromRoot = {.x = 0, .y = 0};
auto computedOrigin = LayoutableShadowNode::computeOriginFromRoot(
parentOriginFromRoot,
childShadowNode->getLayoutMetrics().frame,
childShadowNode->getTransform(),
childShadowNode->getContentOriginOffset(true));

EXPECT_EQ(computedOrigin.x, relativeLayoutMetrics.frame.origin.x);
EXPECT_EQ(computedOrigin.y, relativeLayoutMetrics.frame.origin.y);
}

TEST(LayoutableShadowNodeTest, computeOriginFromRootWithTransform) {
auto builder = simpleComponentBuilder();
auto childShadowNode = std::shared_ptr<ViewShadowNode>{};
// clang-format off
auto element =
Element<ViewShadowNode>()
.finalize([](ViewShadowNode &shadowNode){
auto layoutMetrics = EmptyLayoutMetrics;
layoutMetrics.frame.size = {.width=1000, .height=1000};
shadowNode.setLayoutMetrics(layoutMetrics);
})
.children({
Element<ViewShadowNode>()
.finalize([](ViewShadowNode &shadowNode){
auto layoutMetrics = EmptyLayoutMetrics;
layoutMetrics.frame.origin = {.x=10, .y=20};
layoutMetrics.frame.size = {.width=100, .height=200};
shadowNode.setLayoutMetrics(layoutMetrics);
})
.props([] {
auto sharedProps = std::make_shared<ViewShadowNodeProps>();
sharedProps->transform = Transform::Scale(0.5, 0.5, 1);
return sharedProps;
})
.reference(childShadowNode)
});
// clang-format on

auto parentShadowNode = builder.build(element);

// Get expected origin from computeRelativeLayoutMetrics
auto relativeLayoutMetrics =
LayoutableShadowNode::computeRelativeLayoutMetrics(
childShadowNode->getFamily(), *parentShadowNode, {});

// Compute origin using computeOriginFromRoot
Point parentOriginFromRoot = {.x = 0, .y = 0};
auto computedOrigin = LayoutableShadowNode::computeOriginFromRoot(
parentOriginFromRoot,
childShadowNode->getLayoutMetrics().frame,
childShadowNode->getTransform(),
childShadowNode->getContentOriginOffset(true));

EXPECT_EQ(computedOrigin.x, relativeLayoutMetrics.frame.origin.x);
EXPECT_EQ(computedOrigin.y, relativeLayoutMetrics.frame.origin.y);
}

TEST(LayoutableShadowNodeTest, computeOriginFromRootNested) {
auto builder = simpleComponentBuilder();
auto parentShadowNode = std::shared_ptr<ViewShadowNode>{};
auto childShadowNode = std::shared_ptr<ViewShadowNode>{};
// clang-format off
auto element =
Element<ViewShadowNode>()
.finalize([](ViewShadowNode &shadowNode){
auto layoutMetrics = EmptyLayoutMetrics;
layoutMetrics.frame.origin = {.x=0, .y=0};
layoutMetrics.frame.size = {.width=1000, .height=1000};
shadowNode.setLayoutMetrics(layoutMetrics);
})
.children({
Element<ViewShadowNode>()
.finalize([](ViewShadowNode &shadowNode){
auto layoutMetrics = EmptyLayoutMetrics;
layoutMetrics.frame.origin = {.x=50, .y=50};
layoutMetrics.frame.size = {.width=200, .height=200};
shadowNode.setLayoutMetrics(layoutMetrics);
})
.reference(parentShadowNode)
.children({
Element<ViewShadowNode>()
.finalize([](ViewShadowNode &shadowNode){
auto layoutMetrics = EmptyLayoutMetrics;
layoutMetrics.frame.origin = {.x=10, .y=20};
layoutMetrics.frame.size = {.width=100, .height=100};
shadowNode.setLayoutMetrics(layoutMetrics);
})
.reference(childShadowNode)
})
});
// clang-format on

auto rootShadowNode = builder.build(element);

// Get expected origin from computeRelativeLayoutMetrics
auto relativeLayoutMetrics =
LayoutableShadowNode::computeRelativeLayoutMetrics(
childShadowNode->getFamily(), *rootShadowNode, {});

// Compute origin using computeOriginFromRoot
// First compute parent's origin from root
Point rootOrigin = {.x = 0, .y = 0};
auto parentOriginFromRoot = LayoutableShadowNode::computeOriginFromRoot(
rootOrigin,
parentShadowNode->getLayoutMetrics().frame,
parentShadowNode->getTransform(),
parentShadowNode->getContentOriginOffset(true));

// Then compute child's origin from root using parent's origin
auto computedOrigin = LayoutableShadowNode::computeOriginFromRoot(
parentOriginFromRoot,
childShadowNode->getLayoutMetrics().frame,
childShadowNode->getTransform(),
childShadowNode->getContentOriginOffset(true));

EXPECT_EQ(computedOrigin.x, relativeLayoutMetrics.frame.origin.x);
EXPECT_EQ(computedOrigin.y, relativeLayoutMetrics.frame.origin.y);
}

} // namespace facebook::react
Loading
Loading