Add YGNodeSetChildren(), YGNodeTraversePreOrder()
Summary: We had functions for inserting/removing a specific child, but not for simply replacing the child set with another list. `YGNodeSetChildren()` will unhook child nodes from the parent that don't appear in the new set. We set the disconnected child node layouts to `YGLayout()` b/c that's what the rest of the code does. `YGTraversePreOrder()` walks the tree and calls a labmda for each node. We could very easily add a post-order traversal and the ability to stop traversal if we ever want, but for now this is an MVP. Reviewed By: Woody17 Differential Revision: D7360203 fbshipit-source-id: 32df8e1213ead03bc0a026ec4bf453bc799bb9ce
This commit is contained in:
committed by
Facebook Github Bot
parent
c951ad7c7b
commit
187fc54596
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user