Skip to content

Commit

Permalink
Add YGNodeSetChildren(), YGNodeTraversePreOrder()
Browse files Browse the repository at this point in the history
Reviewed By: Woody17

Differential Revision: D7360203

fbshipit-source-id: 32df8e1213ead03bc0a026ec4bf453bc799bb9ce
  • Loading branch information
Jonathan Dann authored and facebook-github-bot committed Mar 25, 2018
1 parent 228f5c8 commit 2cc75f7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
61 changes: 59 additions & 2 deletions ReactCommon/yoga/yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ static YGNodeRef YGNodeDeepClone(YGNodeRef oldNode) {
}

void YGNodeFree(const YGNodeRef node) {
if (node->getParent()) {
node->getParent()->removeChild(node);
if (YGNodeRef parent = node->getParent()) {
parent->removeChild(node);
node->setParent(nullptr);
}

Expand Down Expand Up @@ -477,6 +477,48 @@ void YGNodeRemoveAllChildren(const YGNodeRef parent) {
parent->markDirtyAndPropogate();
}

static void YGNodeSetChildrenInternal(YGNodeRef const parent, const std::vector<YGNodeRef> &children)
{
if (!parent) {
return;
}
if (children.size() == 0) {
if (YGNodeGetChildCount(parent) > 0) {
for (YGNodeRef const child : parent->getChildren()) {
child->setLayout(YGLayout());
child->setParent(nullptr);
}
parent->setChildren(YGVector());
parent->markDirtyAndPropogate();
}
} else {
if (YGNodeGetChildCount(parent) > 0) {
for (YGNodeRef const oldChild : parent->getChildren()) {
// Our new children may have nodes in common with the old children. We don't reset these common nodes.
if (std::find(children.begin(), children.end(), oldChild) == children.end()) {
oldChild->setLayout(YGLayout());
oldChild->setParent(nullptr);
}
}
}
parent->setChildren(children);
for (YGNodeRef child : children) {
child->setParent(parent);
}
parent->markDirtyAndPropogate();
}
}

void YGNodeSetChildren(YGNodeRef const parent, const YGNodeRef c[], const uint32_t count) {
const YGVector children = {c, c + count};
YGNodeSetChildrenInternal(parent, children);
}

void YGNodeSetChildren(YGNodeRef const parent, const std::vector<YGNodeRef> &children)
{
YGNodeSetChildrenInternal(parent, children);
}

YGNodeRef YGNodeGetChild(const YGNodeRef node, const uint32_t index) {
if (index < node->getChildren().size()) {
return node->getChild(index);
Expand Down Expand Up @@ -3925,3 +3967,18 @@ void *YGConfigGetContext(const YGConfigRef config) {
void YGConfigSetNodeClonedFunc(const YGConfigRef config, const YGNodeClonedFunc callback) {
config->cloneNodeCallback = callback;
}

static void YGTraverseChildrenPreOrder(const YGVector& children, const std::function<void(YGNodeRef node)>& f) {
for (YGNodeRef node : children) {
f(node);
YGTraverseChildrenPreOrder(node->getChildren(), f);
}
}

void YGTraversePreOrder(YGNodeRef const node, std::function<void(YGNodeRef node)>&& f) {
if (!node) {
return;
}
f(node);
YGTraverseChildrenPreOrder(node->getChildren(), f);
}
13 changes: 13 additions & 0 deletions ReactCommon/yoga/yoga/Yoga.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ WIN_EXPORT void YGNodeRemoveAllChildren(const YGNodeRef node);
WIN_EXPORT YGNodeRef YGNodeGetChild(const YGNodeRef node, const uint32_t index);
WIN_EXPORT YGNodeRef YGNodeGetParent(const YGNodeRef node);
WIN_EXPORT uint32_t YGNodeGetChildCount(const YGNodeRef node);
WIN_EXPORT void YGNodeSetChildren(YGNodeRef const parent, const YGNodeRef children[], const uint32_t count);

WIN_EXPORT void YGNodeCalculateLayout(const YGNodeRef node,
const float availableWidth,
Expand Down Expand Up @@ -298,3 +299,15 @@ WIN_EXPORT float YGRoundValueToPixelGrid(
const bool forceFloor);

YG_EXTERN_C_END

#ifdef __cplusplus

#include <functional>
#include <vector>

// Calls f on each node in the tree including the given node argument.
extern void YGTraversePreOrder(YGNodeRef const node, std::function<void(YGNodeRef node)>&& f);

extern void YGNodeSetChildren(YGNodeRef const parent, const std::vector<YGNodeRef> &children);

#endif

0 comments on commit 2cc75f7

Please sign in to comment.