Add support for context-aware print functions

Summary:
@public

Context-aware print functions are an internal Yoga feature that will be used for Yoga’s JNI code.

It will be possible to specify a context when calculating layout, which will be passed on to baseline and measure functions. This will be a private feature.

Reviewed By: SidharthGuglani

Differential Revision: D14131098

fbshipit-source-id: 7a9da307274ceccba9f7debba581b70c1ebf2c98
This commit is contained in:
David Aurelio
2019-02-19 09:54:48 -08:00
committed by Facebook Github Bot
parent 59bcac3289
commit cbcf07f08a
4 changed files with 30 additions and 12 deletions

View File

@@ -12,9 +12,13 @@
using namespace facebook; using namespace facebook;
using facebook::yoga::detail::CompactValue; using facebook::yoga::detail::CompactValue;
void YGNode::print() { void YGNode::print(void* printContext) {
if (print_ != nullptr) { if (print_.noContext != nullptr) {
print_(this); if (printUsesContext_) {
print_.withContext(this, printContext);
} else {
print_.noContext(this);
}
} }
} }
@@ -304,13 +308,14 @@ YGNode& YGNode::operator=(const YGNode& node) {
} }
context_ = node.getContext(); context_ = node.getContext();
print_ = node.print_;
hasNewLayout_ = node.getHasNewLayout(); hasNewLayout_ = node.getHasNewLayout();
nodeType_ = node.getNodeType(); nodeType_ = node.getNodeType();
measureUsesContext_ = node.measureUsesContext_; measureUsesContext_ = node.measureUsesContext_;
baselineUsesContext_ = node.baselineUsesContext_; baselineUsesContext_ = node.baselineUsesContext_;
printUsesContext_ = node.printUsesContext_;
measure_ = node.measure_; measure_ = node.measure_;
baseline_ = node.baseline_; baseline_ = node.baseline_;
print_ = node.print_;
dirtied_ = node.getDirtied(); dirtied_ = node.getDirtied();
style_ = node.style_; style_ = node.style_;
layout_ = node.layout_; layout_ = node.layout_;

View File

@@ -15,16 +15,17 @@ struct YGNode {
using MeasureWithContextFn = using MeasureWithContextFn =
YGSize (*)(YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*); YGSize (*)(YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*);
using BaselineWithContextFn = float (*)(YGNode*, float, float, void*); using BaselineWithContextFn = float (*)(YGNode*, float, float, void*);
using PrintWithContextFn = void (*)(YGNode*, void*);
private: private:
void* context_ = nullptr; void* context_ = nullptr;
YGPrintFunc print_ = nullptr;
bool hasNewLayout_ : 1; bool hasNewLayout_ : 1;
bool isReferenceBaseline_ : 1; bool isReferenceBaseline_ : 1;
bool isDirty_ : 1; bool isDirty_ : 1;
YGNodeType nodeType_ : 1; YGNodeType nodeType_ : 1;
bool measureUsesContext_ : 1; bool measureUsesContext_ : 1;
bool baselineUsesContext_ : 1; bool baselineUsesContext_ : 1;
bool printUsesContext_ : 1;
union { union {
YGMeasureFunc noContext; YGMeasureFunc noContext;
MeasureWithContextFn withContext; MeasureWithContextFn withContext;
@@ -33,6 +34,10 @@ private:
YGBaselineFunc noContext; YGBaselineFunc noContext;
BaselineWithContextFn withContext; BaselineWithContextFn withContext;
} baseline_ = {nullptr}; } baseline_ = {nullptr};
union {
YGPrintFunc noContext;
PrintWithContextFn withContext;
} print_ = {nullptr};
YGDirtiedFunc dirtied_ = nullptr; YGDirtiedFunc dirtied_ = nullptr;
YGStyle style_ = {}; YGStyle style_ = {};
YGLayout layout_ = {}; YGLayout layout_ = {};
@@ -57,7 +62,8 @@ public:
isDirty_{false}, isDirty_{false},
nodeType_{YGNodeTypeDefault}, nodeType_{YGNodeTypeDefault},
measureUsesContext_{false}, measureUsesContext_{false},
baselineUsesContext_{false} {} baselineUsesContext_{false},
printUsesContext_{false} {}
~YGNode() = default; // cleanup of owner/children relationships in YGNodeFree ~YGNode() = default; // cleanup of owner/children relationships in YGNodeFree
explicit YGNode(const YGConfigRef newConfig) : config_(newConfig){}; explicit YGNode(const YGConfigRef newConfig) : config_(newConfig){};
YGNode(const YGNode& node) = default; YGNode(const YGNode& node) = default;
@@ -68,7 +74,7 @@ public:
return context_; return context_;
} }
void print(); void print(void*);
bool getHasNewLayout() const { bool getHasNewLayout() const {
return hasNewLayout_; return hasNewLayout_;
@@ -196,7 +202,15 @@ public:
} }
void setPrintFunc(YGPrintFunc printFunc) { void setPrintFunc(YGPrintFunc printFunc) {
print_ = printFunc; print_.noContext = printFunc;
printUsesContext_ = false;
}
void setPrintFunc(PrintWithContextFn printFunc) {
print_.withContext = printFunc;
printUsesContext_ = true;
}
void setPrintFunc(std::nullptr_t) {
setPrintFunc(YGPrintFunc{nullptr});
} }
void setHasNewLayout(bool hasNewLayout) { void setHasNewLayout(bool hasNewLayout) {

View File

@@ -114,7 +114,6 @@ void YGNodeToString(
uint32_t level) { uint32_t level) {
indent(str, level); indent(str, level);
appendFormatedString(str, "<div "); appendFormatedString(str, "<div ");
node->print();
if (options & YGPrintOptionsLayout) { if (options & YGPrintOptionsLayout) {
appendFormatedString(str, "layout=\""); appendFormatedString(str, "layout=\"");

View File

@@ -3843,7 +3843,7 @@ bool YGLayoutNodeInternal(
"%s%d.{[skipped] ", "%s%d.{[skipped] ",
YGSpacer(gDepth), YGSpacer(gDepth),
gDepth); gDepth);
node->print(); node->print(layoutContext);
Log::log( Log::log(
node, node,
YGLogLevelVerbose, YGLogLevelVerbose,
@@ -3867,7 +3867,7 @@ bool YGLayoutNodeInternal(
YGSpacer(gDepth), YGSpacer(gDepth),
gDepth, gDepth,
needToVisitNode ? "*" : ""); needToVisitNode ? "*" : "");
node->print(); node->print(layoutContext);
Log::log( Log::log(
node, node,
YGLogLevelVerbose, YGLogLevelVerbose,
@@ -3903,7 +3903,7 @@ bool YGLayoutNodeInternal(
YGSpacer(gDepth), YGSpacer(gDepth),
gDepth, gDepth,
needToVisitNode ? "*" : ""); needToVisitNode ? "*" : "");
node->print(); node->print(layoutContext);
Log::log( Log::log(
node, node,
YGLogLevelVerbose, YGLogLevelVerbose,