Add support for context-aware measure and baseline functions
Summary: @public Context-aware measure and baseline 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: D14100509 fbshipit-source-id: acf4a030549b2e38d5ce0cd5dbe837864e5ffd81
This commit is contained in:
committed by
Facebook Github Bot
parent
f86c74ce7e
commit
e7fcf1ee65
@@ -36,7 +36,36 @@ TEST(YGNode, measure_with_measure_fn) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
ASSERT_EQ(
|
ASSERT_EQ(
|
||||||
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost),
|
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost, nullptr),
|
||||||
|
(YGSize{23, 12}));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(YGNode, measure_with_context_measure_fn) {
|
||||||
|
auto n = YGNode{};
|
||||||
|
n.setMeasureFunc(
|
||||||
|
[](YGNode*, float, YGMeasureMode, float, YGMeasureMode, void* ctx) {
|
||||||
|
return *(YGSize*) ctx;
|
||||||
|
});
|
||||||
|
|
||||||
|
auto result = YGSize{123.4, -56.7};
|
||||||
|
ASSERT_EQ(
|
||||||
|
n.measure(0, YGMeasureModeUndefined, 0, YGMeasureModeUndefined, &result),
|
||||||
|
result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(YGNode, switching_measure_fn_types) {
|
||||||
|
auto n = YGNode{};
|
||||||
|
n.setMeasureFunc(
|
||||||
|
[](YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*) {
|
||||||
|
return YGSize{};
|
||||||
|
});
|
||||||
|
n.setMeasureFunc(
|
||||||
|
[](YGNode*, float w, YGMeasureMode wm, float h, YGMeasureMode hm) {
|
||||||
|
return YGSize{w * wm, h / hm};
|
||||||
|
});
|
||||||
|
|
||||||
|
ASSERT_EQ(
|
||||||
|
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost, nullptr),
|
||||||
(YGSize{23, 12}));
|
(YGSize{23, 12}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,6 +79,17 @@ TEST(YGNode, hasMeasureFunc_after_unset) {
|
|||||||
ASSERT_FALSE(n.hasMeasureFunc());
|
ASSERT_FALSE(n.hasMeasureFunc());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(YGNode, hasMeasureFunc_after_unset_context) {
|
||||||
|
auto n = YGNode{};
|
||||||
|
n.setMeasureFunc(
|
||||||
|
[](YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*) {
|
||||||
|
return YGSize{};
|
||||||
|
});
|
||||||
|
|
||||||
|
n.setMeasureFunc(nullptr);
|
||||||
|
ASSERT_FALSE(n.hasMeasureFunc());
|
||||||
|
}
|
||||||
|
|
||||||
TEST(YGNode, hasBaselineFunc_initial) {
|
TEST(YGNode, hasBaselineFunc_initial) {
|
||||||
auto n = YGNode{};
|
auto n = YGNode{};
|
||||||
ASSERT_FALSE(n.hasBaselineFunc());
|
ASSERT_FALSE(n.hasBaselineFunc());
|
||||||
@@ -65,7 +105,17 @@ TEST(YGNode, baseline_with_baseline_fn) {
|
|||||||
auto n = YGNode{};
|
auto n = YGNode{};
|
||||||
n.setBaseLineFunc([](YGNode*, float w, float h) { return w + h; });
|
n.setBaseLineFunc([](YGNode*, float w, float h) { return w + h; });
|
||||||
|
|
||||||
ASSERT_EQ(n.baseline(1.25f, 2.5f), 3.75f);
|
ASSERT_EQ(n.baseline(1.25f, 2.5f, nullptr), 3.75f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(YGNode, baseline_with_context_baseline_fn) {
|
||||||
|
auto n = YGNode{};
|
||||||
|
n.setBaseLineFunc([](YGNode*, float w, float h, void* ctx) {
|
||||||
|
return w + h + *(float*) ctx;
|
||||||
|
});
|
||||||
|
|
||||||
|
auto ctx = -10.0f;
|
||||||
|
ASSERT_EQ(n.baseline(1.25f, 2.5f, &ctx), -6.25f);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(YGNode, hasBaselineFunc_after_unset) {
|
TEST(YGNode, hasBaselineFunc_after_unset) {
|
||||||
@@ -76,6 +126,21 @@ TEST(YGNode, hasBaselineFunc_after_unset) {
|
|||||||
ASSERT_FALSE(n.hasBaselineFunc());
|
ASSERT_FALSE(n.hasBaselineFunc());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(YGNode, hasBaselineFunc_after_unset_context) {
|
||||||
|
auto n = YGNode{};
|
||||||
|
n.setBaseLineFunc([](YGNode*, float, float, void*) { return 0.0f; });
|
||||||
|
|
||||||
|
n.setMeasureFunc(nullptr);
|
||||||
|
ASSERT_FALSE(n.hasMeasureFunc());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(YGNode, switching_baseline_fn_types) {
|
||||||
|
auto n = YGNode{};
|
||||||
|
n.setBaseLineFunc([](YGNode*, float, float, void*) { return 0.0f; });
|
||||||
|
n.setBaseLineFunc([](YGNode*, float, float) { return 1.0f; });
|
||||||
|
ASSERT_EQ(n.baseline(1, 2, nullptr), 1.0f);
|
||||||
|
}
|
||||||
|
|
||||||
void PrintTo(const YGSize& size, std::ostream* os) {
|
void PrintTo(const YGSize& size, std::ostream* os) {
|
||||||
*os << "YGSize{" << size.width << ", " << size.height << "}";
|
*os << "YGSize{" << size.width << ", " << size.height << "}";
|
||||||
}
|
}
|
||||||
|
@@ -101,11 +101,29 @@ YGFloatOptional YGNode::getMarginForAxis(
|
|||||||
return getLeadingMargin(axis, widthSize) + getTrailingMargin(axis, widthSize);
|
return getLeadingMargin(axis, widthSize) + getTrailingMargin(axis, widthSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
YGSize YGNode::measure(
|
||||||
|
float width,
|
||||||
|
YGMeasureMode widthMode,
|
||||||
|
float height,
|
||||||
|
YGMeasureMode heightMode,
|
||||||
|
void* layoutContext) {
|
||||||
|
|
||||||
|
return measureUsesContext_
|
||||||
|
? measure_.withContext(
|
||||||
|
this, width, widthMode, height, heightMode, layoutContext)
|
||||||
|
: measure_.noContext(this, width, widthMode, height, heightMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
float YGNode::baseline(float width, float height, void* layoutContext) {
|
||||||
|
return baselineUsesContext_
|
||||||
|
? baseline_.withContext(this, width, height, layoutContext)
|
||||||
|
: baseline_.noContext(this, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
// Setters
|
// Setters
|
||||||
|
|
||||||
void YGNode::setMeasureFunc(YGMeasureFunc measureFunc) {
|
void YGNode::setMeasureFunc(decltype(YGNode::measure_) measureFunc) {
|
||||||
if (measureFunc == nullptr) {
|
if (measureFunc.noContext == nullptr) {
|
||||||
measure_ = nullptr;
|
|
||||||
// TODO: t18095186 Move nodeType to opt-in function and mark appropriate
|
// TODO: t18095186 Move nodeType to opt-in function and mark appropriate
|
||||||
// places in Litho
|
// places in Litho
|
||||||
nodeType_ = YGNodeTypeDefault;
|
nodeType_ = YGNodeTypeDefault;
|
||||||
@@ -115,7 +133,6 @@ void YGNode::setMeasureFunc(YGMeasureFunc measureFunc) {
|
|||||||
children_.size() == 0,
|
children_.size() == 0,
|
||||||
"Cannot set measure function: Nodes with measure functions cannot have "
|
"Cannot set measure function: Nodes with measure functions cannot have "
|
||||||
"children.");
|
"children.");
|
||||||
measure_ = measureFunc;
|
|
||||||
// TODO: t18095186 Move nodeType to opt-in function and mark appropriate
|
// TODO: t18095186 Move nodeType to opt-in function and mark appropriate
|
||||||
// places in Litho
|
// places in Litho
|
||||||
setNodeType(YGNodeTypeText);
|
setNodeType(YGNodeTypeText);
|
||||||
@@ -124,6 +141,20 @@ void YGNode::setMeasureFunc(YGMeasureFunc measureFunc) {
|
|||||||
measure_ = measureFunc;
|
measure_ = measureFunc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void YGNode::setMeasureFunc(YGMeasureFunc measureFunc) {
|
||||||
|
measureUsesContext_ = false;
|
||||||
|
decltype(YGNode::measure_) m;
|
||||||
|
m.noContext = measureFunc;
|
||||||
|
setMeasureFunc(m);
|
||||||
|
}
|
||||||
|
|
||||||
|
void YGNode::setMeasureFunc(MeasureWithContextFn measureFunc) {
|
||||||
|
measureUsesContext_ = true;
|
||||||
|
decltype(YGNode::measure_) m;
|
||||||
|
m.withContext = measureFunc;
|
||||||
|
setMeasureFunc(m);
|
||||||
|
}
|
||||||
|
|
||||||
void YGNode::replaceChild(YGNodeRef child, uint32_t index) {
|
void YGNode::replaceChild(YGNodeRef child, uint32_t index) {
|
||||||
children_[index] = child;
|
children_[index] = child;
|
||||||
}
|
}
|
||||||
@@ -270,6 +301,8 @@ YGNode& YGNode::operator=(const YGNode& node) {
|
|||||||
print_ = node.getPrintFunc();
|
print_ = node.getPrintFunc();
|
||||||
hasNewLayout_ = node.getHasNewLayout();
|
hasNewLayout_ = node.getHasNewLayout();
|
||||||
nodeType_ = node.getNodeType();
|
nodeType_ = node.getNodeType();
|
||||||
|
measureUsesContext_ = node.measureUsesContext_;
|
||||||
|
baselineUsesContext_ = node.baselineUsesContext_;
|
||||||
measure_ = node.measure_;
|
measure_ = node.measure_;
|
||||||
baseline_ = node.baseline_;
|
baseline_ = node.baseline_;
|
||||||
dirtied_ = node.getDirtied();
|
dirtied_ = node.getDirtied();
|
||||||
|
@@ -12,6 +12,10 @@
|
|||||||
#include "Yoga-internal.h"
|
#include "Yoga-internal.h"
|
||||||
|
|
||||||
struct YGNode {
|
struct YGNode {
|
||||||
|
using MeasureWithContextFn =
|
||||||
|
YGSize (*)(YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*);
|
||||||
|
using BaselineWithContextFn = float (*)(YGNode*, float, float, void*);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void* context_ = nullptr;
|
void* context_ = nullptr;
|
||||||
YGPrintFunc print_ = nullptr;
|
YGPrintFunc print_ = nullptr;
|
||||||
@@ -19,8 +23,16 @@ private:
|
|||||||
bool isReferenceBaseline_ : 1;
|
bool isReferenceBaseline_ : 1;
|
||||||
bool isDirty_ : 1;
|
bool isDirty_ : 1;
|
||||||
YGNodeType nodeType_ : 1;
|
YGNodeType nodeType_ : 1;
|
||||||
YGMeasureFunc measure_ = nullptr;
|
bool measureUsesContext_ : 1;
|
||||||
YGBaselineFunc baseline_ = nullptr;
|
bool baselineUsesContext_ : 1;
|
||||||
|
union {
|
||||||
|
YGMeasureFunc noContext;
|
||||||
|
MeasureWithContextFn withContext;
|
||||||
|
} measure_ = {nullptr};
|
||||||
|
union {
|
||||||
|
YGBaselineFunc noContext;
|
||||||
|
BaselineWithContextFn withContext;
|
||||||
|
} baseline_ = {nullptr};
|
||||||
YGDirtiedFunc dirtied_ = nullptr;
|
YGDirtiedFunc dirtied_ = nullptr;
|
||||||
YGStyle style_ = {};
|
YGStyle style_ = {};
|
||||||
YGLayout layout_ = {};
|
YGLayout layout_ = {};
|
||||||
@@ -35,12 +47,17 @@ private:
|
|||||||
const YGFlexDirection axis,
|
const YGFlexDirection axis,
|
||||||
const float axisSize) const;
|
const float axisSize) const;
|
||||||
|
|
||||||
|
void setMeasureFunc(decltype(measure_));
|
||||||
|
void setBaseLineFunc(decltype(baseline_));
|
||||||
|
|
||||||
public:
|
public:
|
||||||
YGNode()
|
YGNode()
|
||||||
: hasNewLayout_(true),
|
: hasNewLayout_{true},
|
||||||
isReferenceBaseline_(false),
|
isReferenceBaseline_{false},
|
||||||
isDirty_(false),
|
isDirty_{false},
|
||||||
nodeType_(YGNodeTypeDefault) {}
|
nodeType_{YGNodeTypeDefault},
|
||||||
|
measureUsesContext_{false},
|
||||||
|
baselineUsesContext_{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;
|
||||||
@@ -64,24 +81,16 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool hasMeasureFunc() const noexcept {
|
bool hasMeasureFunc() const noexcept {
|
||||||
return measure_ != nullptr;
|
return measure_.noContext != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
YGSize measure(
|
YGSize measure(float, YGMeasureMode, float, YGMeasureMode, void*);
|
||||||
float width,
|
|
||||||
YGMeasureMode widthMode,
|
|
||||||
float height,
|
|
||||||
YGMeasureMode heightMode) {
|
|
||||||
return measure_(this, width, widthMode, height, heightMode);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool hasBaselineFunc() const noexcept {
|
bool hasBaselineFunc() const noexcept {
|
||||||
return baseline_ != nullptr;
|
return baseline_.noContext != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
float baseline(float width, float height) {
|
float baseline(float width, float height, void* layoutContext);
|
||||||
return baseline_(this, width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
YGDirtiedFunc getDirtied() const {
|
YGDirtiedFunc getDirtied() const {
|
||||||
return dirtied_;
|
return dirtied_;
|
||||||
@@ -209,9 +218,21 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setMeasureFunc(YGMeasureFunc measureFunc);
|
void setMeasureFunc(YGMeasureFunc measureFunc);
|
||||||
|
void setMeasureFunc(MeasureWithContextFn);
|
||||||
|
void setMeasureFunc(std::nullptr_t) {
|
||||||
|
return setMeasureFunc(YGMeasureFunc{nullptr});
|
||||||
|
}
|
||||||
|
|
||||||
void setBaseLineFunc(YGBaselineFunc baseLineFunc) {
|
void setBaseLineFunc(YGBaselineFunc baseLineFunc) {
|
||||||
baseline_ = baseLineFunc;
|
baselineUsesContext_ = false;
|
||||||
|
baseline_.noContext = baseLineFunc;
|
||||||
|
}
|
||||||
|
void setBaseLineFunc(BaselineWithContextFn baseLineFunc) {
|
||||||
|
baselineUsesContext_ = true;
|
||||||
|
baseline_.withContext = baseLineFunc;
|
||||||
|
}
|
||||||
|
void setBaseLineFunc(std::nullptr_t) {
|
||||||
|
return setBaseLineFunc(YGBaselineFunc{nullptr});
|
||||||
}
|
}
|
||||||
|
|
||||||
void setDirtiedFunc(YGDirtiedFunc dirtiedFunc) {
|
void setDirtiedFunc(YGDirtiedFunc dirtiedFunc) {
|
||||||
|
@@ -1080,7 +1080,8 @@ static float YGBaseline(const YGNodeRef node) {
|
|||||||
node,
|
node,
|
||||||
&YGNode::baseline,
|
&YGNode::baseline,
|
||||||
node->getLayout().measuredDimensions[YGDimensionWidth],
|
node->getLayout().measuredDimensions[YGDimensionWidth],
|
||||||
node->getLayout().measuredDimensions[YGDimensionHeight]);
|
node->getLayout().measuredDimensions[YGDimensionHeight],
|
||||||
|
(void*) nullptr);
|
||||||
YGAssertWithNode(
|
YGAssertWithNode(
|
||||||
node,
|
node,
|
||||||
!YGFloatIsUndefined(baseline),
|
!YGFloatIsUndefined(baseline),
|
||||||
@@ -1697,7 +1698,8 @@ static void YGNodeWithMeasureFuncSetMeasuredDimensions(
|
|||||||
innerWidth,
|
innerWidth,
|
||||||
widthMeasureMode,
|
widthMeasureMode,
|
||||||
innerHeight,
|
innerHeight,
|
||||||
heightMeasureMode);
|
heightMeasureMode,
|
||||||
|
(void*) nullptr);
|
||||||
|
|
||||||
node->setLayoutMeasuredDimension(
|
node->setLayoutMeasuredDimension(
|
||||||
YGNodeBoundAxis(
|
YGNodeBoundAxis(
|
||||||
|
Reference in New Issue
Block a user