Skip to content

Commit

Permalink
Support for (de)serializing node state (facebook#1570)
Browse files Browse the repository at this point in the history
Summary:

X-link: facebook/react-native#42751

tsia. Need node state

Differential Revision: D53206323
  • Loading branch information
joevilches authored and facebook-github-bot committed Feb 8, 2024
1 parent b09fe33 commit 9c5b01e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
12 changes: 12 additions & 0 deletions benchmark/Benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ void setStylesFromJson(json& j, YGNodeShared node) {
}
}

YGNodeRef buildNodeFromJson(json& j, YGConfigRef config) {
const YGNodeRef node = YGNodeNewWithConfig(config);
json nodeState = j["node"];
for (json::iterator it = nodeState.begin(); it != nodeState.end(); it++) {
if (it.key() == "always-forms-containing-block") {
YGNodeSetAlwaysFormsContainingBlock(node, it.value());
}
}

return node;
}

YogaNodeAndConfig
buildTreeFromJson(json& j, YogaNodeAndConfig* parent, size_t index) {
YGConfigShared config = buildConfigFromJson(j);
Expand Down
3 changes: 2 additions & 1 deletion capture/CaptureTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ void captureTree(YGNodeRef node, const std::filesystem::path& path) {
nodeToString(
str,
node,
PrintOptions::Style | PrintOptions::Children | PrintOptions::Config);
PrintOptions::Style | PrintOptions::Children | PrintOptions::Config |
PrintOptions::Node);
std::ofstream file(path);
file << str;
}
Expand Down
19 changes: 14 additions & 5 deletions capture/NodeToString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ static void nodeToStringImpl(json& j, YGNodeRef node, PrintOptions options) {
j["layout"]["top"] = YGNodeStyleGetPosition(node, YGEdgeTop).value;
j["layout"]["left"] = YGNodeStyleGetPosition(node, YGEdgeLeft).value;
}
const YGNodeRef defaultNode = YGNodeNew();

if ((options & PrintOptions::Style) == PrintOptions::Style) {
const YGNodeRef defaultNode = YGNodeNew();
appendEnumValueIfNotDefault(
j["style"],
"flex-direction",
Expand Down Expand Up @@ -247,10 +247,6 @@ static void nodeToStringImpl(json& j, YGNodeRef node, PrintOptions options) {
"min-height",
YGNodeStyleGetMinHeight(node),
YGNodeStyleGetMinHeight(defaultNode));

if (YGNodeHasMeasureFunc(node)) {
j["style"]["has-custom-measure"] = true;
}
}

if ((options & PrintOptions::Config) == PrintOptions::Config) {
Expand Down Expand Up @@ -286,6 +282,19 @@ static void nodeToStringImpl(json& j, YGNodeRef node, PrintOptions options) {
}
}

if ((options & PrintOptions::Node) == PrintOptions::Node) {
appendBoolIfNotDefault(
j["node"],
"always-forms-containing-block",
YGNodeGetAlwaysFormsContainingBlock(node),
YGNodeGetAlwaysFormsContainingBlock(defaultNode));
if (YGNodeHasMeasureFunc(node)) {
j["node"]["has-custom-measure"] = true;
}
}

YGNodeFree(defaultNode);

const size_t childCount = YGNodeGetChildCount(node);
if ((options & PrintOptions::Children) == PrintOptions::Children &&
childCount > 0) {
Expand Down
1 change: 1 addition & 0 deletions capture/NodeToString.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum class PrintOptions : uint8_t {
Children = 1 << 1,
Style = 1 << 2,
Config = 1 << 3,
Node = 1 << 4,
};
YG_DEFINE_ENUM_FLAG_OPERATORS(PrintOptions);

Expand Down
4 changes: 4 additions & 0 deletions yoga/YGNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ void YGNodeSetAlwaysFormsContainingBlock(
resolveRef(node)->setAlwaysFormsContainingBlock(alwaysFormsContainingBlock);
}

bool YGNodeGetAlwaysFormsContainingBlock(YGNodeConstRef node) {
return resolveRef(node)->alwaysFormsContainingBlock();
}

// TODO: This leaks internal details to the public API. Remove after removing
// ComponentKit usage of it.
bool YGNodeCanUseCachedMeasurement(
Expand Down
8 changes: 8 additions & 0 deletions yoga/YGNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ YG_EXPORT void YGNodeSetAlwaysFormsContainingBlock(
YGNodeRef node,
bool alwaysFormsContainingBlock);

/**
* Whether the node will always form a containing block for any descendant. This
* can happen in situation where the client implements something like a
* transform that can affect containing blocks but is not handled by Yoga
* directly.
*/
YG_EXPORT bool YGNodeGetAlwaysFormsContainingBlock(YGNodeConstRef node);

/**
* @deprecated
*/
Expand Down

0 comments on commit 9c5b01e

Please sign in to comment.