Remove layoutContext Drilling (#1376)
Summary: X-link: https://github.com/facebook/react-native/pull/39401 Pull Request resolved: https://github.com/facebook/yoga/pull/1376 kill_with_fire_flamethrower Reviewed By: rshest Differential Revision: D49179244 fbshipit-source-id: 9a827e1bd29205254fee5725449191726d6bcf5a
This commit is contained in:
committed by
Facebook GitHub Bot
parent
b1e0140aaa
commit
0a90b16ac6
@@ -32,10 +32,10 @@ Node::Node(const yoga::Config* config) : config_{config} {
|
||||
Node::Node(Node&& node) {
|
||||
context_ = node.context_;
|
||||
flags_ = node.flags_;
|
||||
measure_ = node.measure_;
|
||||
baseline_ = node.baseline_;
|
||||
print_ = node.print_;
|
||||
dirtied_ = node.dirtied_;
|
||||
measureFunc_ = node.measureFunc_;
|
||||
baselineFunc_ = node.baselineFunc_;
|
||||
printFunc_ = node.printFunc_;
|
||||
dirtiedFunc_ = node.dirtiedFunc_;
|
||||
style_ = node.style_;
|
||||
layout_ = node.layout_;
|
||||
lineIndex_ = node.lineIndex_;
|
||||
@@ -48,13 +48,9 @@ Node::Node(Node&& node) {
|
||||
}
|
||||
}
|
||||
|
||||
void Node::print(void* printContext) {
|
||||
if (print_.noContext != nullptr) {
|
||||
if (flags_.printUsesContext) {
|
||||
print_.withContext(this, printContext);
|
||||
} else {
|
||||
print_.noContext(this);
|
||||
}
|
||||
void Node::print() {
|
||||
if (printFunc_ != nullptr) {
|
||||
printFunc_(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,25 +213,18 @@ YGSize Node::measure(
|
||||
float width,
|
||||
YGMeasureMode widthMode,
|
||||
float height,
|
||||
YGMeasureMode heightMode,
|
||||
void* layoutContext) {
|
||||
return flags_.measureUsesContext
|
||||
? measure_.withContext(
|
||||
this, width, widthMode, height, heightMode, layoutContext)
|
||||
: measure_.noContext(this, width, widthMode, height, heightMode);
|
||||
YGMeasureMode heightMode) {
|
||||
return measureFunc_(this, width, widthMode, height, heightMode);
|
||||
}
|
||||
|
||||
float Node::baseline(float width, float height, void* layoutContext) const {
|
||||
return flags_.baselineUsesContext
|
||||
? baseline_.withContext(
|
||||
const_cast<Node*>(this), width, height, layoutContext)
|
||||
: baseline_.noContext(const_cast<Node*>(this), width, height);
|
||||
float Node::baseline(float width, float height) const {
|
||||
return baselineFunc_(this, width, height);
|
||||
}
|
||||
|
||||
// Setters
|
||||
|
||||
void Node::setMeasureFunc(decltype(Node::measure_) measureFunc) {
|
||||
if (measureFunc.noContext == nullptr) {
|
||||
void Node::setMeasureFunc(YGMeasureFunc measureFunc) {
|
||||
if (measureFunc == nullptr) {
|
||||
// TODO: t18095186 Move nodeType to opt-in function and mark appropriate
|
||||
// places in Litho
|
||||
setNodeType(YGNodeTypeDefault);
|
||||
@@ -250,21 +239,7 @@ void Node::setMeasureFunc(decltype(Node::measure_) measureFunc) {
|
||||
setNodeType(YGNodeTypeText);
|
||||
}
|
||||
|
||||
measure_ = measureFunc;
|
||||
}
|
||||
|
||||
void Node::setMeasureFunc(YGMeasureFunc measureFunc) {
|
||||
flags_.measureUsesContext = false;
|
||||
decltype(Node::measure_) m;
|
||||
m.noContext = measureFunc;
|
||||
setMeasureFunc(m);
|
||||
}
|
||||
|
||||
void Node::setMeasureFunc(MeasureWithContextFn measureFunc) {
|
||||
flags_.measureUsesContext = true;
|
||||
decltype(Node::measure_) m;
|
||||
m.withContext = measureFunc;
|
||||
setMeasureFunc(m);
|
||||
measureFunc_ = measureFunc;
|
||||
}
|
||||
|
||||
void Node::replaceChild(Node* child, size_t index) {
|
||||
@@ -299,8 +274,8 @@ void Node::setDirty(bool isDirty) {
|
||||
return;
|
||||
}
|
||||
flags_.isDirty = isDirty;
|
||||
if (isDirty && dirtied_) {
|
||||
dirtied_(this);
|
||||
if (isDirty && dirtiedFunc_) {
|
||||
dirtiedFunc_(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -485,11 +460,11 @@ void Node::clearChildren() {
|
||||
|
||||
// Other Methods
|
||||
|
||||
void Node::cloneChildrenIfNeeded(void* cloneContext) {
|
||||
void Node::cloneChildrenIfNeeded() {
|
||||
size_t i = 0;
|
||||
for (Node*& child : children_) {
|
||||
if (child->getOwner() != this) {
|
||||
child = resolveRef(config_->cloneNode(child, this, i, cloneContext));
|
||||
child = resolveRef(config_->cloneNode(child, this, i));
|
||||
child->setOwner(this);
|
||||
}
|
||||
i += 1;
|
||||
|
@@ -30,42 +30,17 @@ struct NodeFlags {
|
||||
bool isReferenceBaseline : 1;
|
||||
bool isDirty : 1;
|
||||
uint32_t nodeType : 1;
|
||||
bool measureUsesContext : 1;
|
||||
bool baselineUsesContext : 1;
|
||||
bool printUsesContext : 1;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
class YG_EXPORT Node : public ::YGNode {
|
||||
public:
|
||||
// Internal variants of callbacks, currently used only by JNI bindings.
|
||||
// TODO: Reconcile this with the public API
|
||||
using MeasureWithContextFn = YGSize (*)(
|
||||
YGNodeConstRef,
|
||||
float,
|
||||
YGMeasureMode,
|
||||
float,
|
||||
YGMeasureMode,
|
||||
void*);
|
||||
using BaselineWithContextFn = float (*)(YGNodeConstRef, float, float, void*);
|
||||
using PrintWithContextFn = void (*)(YGNodeConstRef, void*);
|
||||
|
||||
private:
|
||||
void* context_ = nullptr;
|
||||
NodeFlags flags_ = {};
|
||||
union {
|
||||
YGMeasureFunc noContext;
|
||||
MeasureWithContextFn withContext;
|
||||
} measure_ = {nullptr};
|
||||
union {
|
||||
YGBaselineFunc noContext;
|
||||
BaselineWithContextFn withContext;
|
||||
} baseline_ = {nullptr};
|
||||
union {
|
||||
YGPrintFunc noContext;
|
||||
PrintWithContextFn withContext;
|
||||
} print_ = {nullptr};
|
||||
YGDirtiedFunc dirtied_ = nullptr;
|
||||
YGMeasureFunc measureFunc_ = {nullptr};
|
||||
YGBaselineFunc baselineFunc_ = {nullptr};
|
||||
YGPrintFunc printFunc_ = {nullptr};
|
||||
YGDirtiedFunc dirtiedFunc_ = nullptr;
|
||||
Style style_ = {};
|
||||
LayoutResults layout_ = {};
|
||||
size_t lineIndex_ = 0;
|
||||
@@ -79,9 +54,6 @@ private:
|
||||
const YGFlexDirection axis,
|
||||
const float axisSize) const;
|
||||
|
||||
void setMeasureFunc(decltype(measure_));
|
||||
void setBaselineFunc(decltype(baseline_));
|
||||
|
||||
void useWebDefaults() {
|
||||
style_.flexDirection() = YGFlexDirectionRow;
|
||||
style_.alignContent() = YGAlignStretch;
|
||||
@@ -112,7 +84,7 @@ public:
|
||||
// Getters
|
||||
void* getContext() const { return context_; }
|
||||
|
||||
void print(void*);
|
||||
void print();
|
||||
|
||||
bool getHasNewLayout() const { return flags_.hasNewLayout; }
|
||||
|
||||
@@ -120,19 +92,17 @@ public:
|
||||
return static_cast<YGNodeType>(flags_.nodeType);
|
||||
}
|
||||
|
||||
bool hasMeasureFunc() const noexcept { return measure_.noContext != nullptr; }
|
||||
bool hasMeasureFunc() const noexcept { return measureFunc_ != nullptr; }
|
||||
|
||||
YGSize measure(float, YGMeasureMode, float, YGMeasureMode, void*);
|
||||
YGSize measure(float, YGMeasureMode, float, YGMeasureMode);
|
||||
|
||||
bool hasBaselineFunc() const noexcept {
|
||||
return baseline_.noContext != nullptr;
|
||||
}
|
||||
bool hasBaselineFunc() const noexcept { return baselineFunc_ != nullptr; }
|
||||
|
||||
float baseline(float width, float height, void* layoutContext) const;
|
||||
float baseline(float width, float height) const;
|
||||
|
||||
bool hasErrata(YGErrata errata) const { return config_->hasErrata(errata); }
|
||||
|
||||
YGDirtiedFunc getDirtied() const { return dirtied_; }
|
||||
YGDirtiedFunc getDirtiedFunc() const { return dirtiedFunc_; }
|
||||
|
||||
// For Performance reasons passing as reference.
|
||||
Style& getStyle() { return style_; }
|
||||
@@ -232,15 +202,7 @@ public:
|
||||
|
||||
void setContext(void* context) { context_ = context; }
|
||||
|
||||
void setPrintFunc(YGPrintFunc printFunc) {
|
||||
print_.noContext = printFunc;
|
||||
flags_.printUsesContext = false;
|
||||
}
|
||||
void setPrintFunc(PrintWithContextFn printFunc) {
|
||||
print_.withContext = printFunc;
|
||||
flags_.printUsesContext = true;
|
||||
}
|
||||
void setPrintFunc(std::nullptr_t) { setPrintFunc(YGPrintFunc{nullptr}); }
|
||||
void setPrintFunc(YGPrintFunc printFunc) { printFunc_ = printFunc; }
|
||||
|
||||
void setHasNewLayout(bool hasNewLayout) {
|
||||
flags_.hasNewLayout = hasNewLayout;
|
||||
@@ -251,24 +213,12 @@ public:
|
||||
}
|
||||
|
||||
void setMeasureFunc(YGMeasureFunc measureFunc);
|
||||
void setMeasureFunc(MeasureWithContextFn);
|
||||
void setMeasureFunc(std::nullptr_t) {
|
||||
return setMeasureFunc(YGMeasureFunc{nullptr});
|
||||
}
|
||||
|
||||
void setBaselineFunc(YGBaselineFunc baseLineFunc) {
|
||||
flags_.baselineUsesContext = false;
|
||||
baseline_.noContext = baseLineFunc;
|
||||
}
|
||||
void setBaselineFunc(BaselineWithContextFn baseLineFunc) {
|
||||
flags_.baselineUsesContext = true;
|
||||
baseline_.withContext = baseLineFunc;
|
||||
}
|
||||
void setBaselineFunc(std::nullptr_t) {
|
||||
return setBaselineFunc(YGBaselineFunc{nullptr});
|
||||
baselineFunc_ = baseLineFunc;
|
||||
}
|
||||
|
||||
void setDirtiedFunc(YGDirtiedFunc dirtiedFunc) { dirtied_ = dirtiedFunc; }
|
||||
void setDirtiedFunc(YGDirtiedFunc dirtiedFunc) { dirtiedFunc_ = dirtiedFunc; }
|
||||
|
||||
void setStyle(const Style& style) { style_ = style; }
|
||||
|
||||
@@ -325,7 +275,7 @@ public:
|
||||
bool removeChild(Node* child);
|
||||
void removeChild(size_t index);
|
||||
|
||||
void cloneChildrenIfNeeded(void*);
|
||||
void cloneChildrenIfNeeded();
|
||||
void markDirtyAndPropagate();
|
||||
float resolveFlexGrow() const;
|
||||
float resolveFlexShrink() const;
|
||||
|
Reference in New Issue
Block a user