Skip to content

Commit

Permalink
Remove layoutContext Drilling (#1376)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#39401

Pull Request resolved: #1376

kill_with_fire_flamethrower

Reviewed By: rshest

Differential Revision: D49179244

fbshipit-source-id: 6b5ef24149dc5dfbe5c1dcb27048f4ed640e17b8
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Sep 12, 2023
1 parent 4069cfa commit 170d634
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 370 deletions.
3 changes: 1 addition & 2 deletions tests/EventsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ struct TypedEventTestData {};

template <>
struct TypedEventTestData<Event::LayoutPassEnd> {
void* layoutContext;
LayoutData layoutData;
};

Expand Down Expand Up @@ -329,7 +328,7 @@ void EventTest::listen(
case Event::LayoutPassEnd: {
auto& eventData = data.get<Event::LayoutPassEnd>();
events.push_back(createArgs<Event::LayoutPassEnd>(
node, data, {eventData.layoutContext, *eventData.layoutData}));
node, data, {*eventData.layoutData}));
break;
}

Expand Down
14 changes: 2 additions & 12 deletions tests/YGConfigTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ TEST_F(ConfigCloningTest, uses_values_provided_by_cloning_callback) {
config->setCloneNodeCallback(cloneNode);

yoga::Node node{}, owner{};
auto clone = config->cloneNode(&node, &owner, 0, nullptr);
auto clone = config->cloneNode(&node, &owner, 0);

ASSERT_EQ(clone, &clonedNode);
}
Expand All @@ -44,22 +44,12 @@ TEST_F(
config->setCloneNodeCallback(doNotClone);

yoga::Node node{}, owner{};
auto clone = config->cloneNode(&node, &owner, 0, nullptr);
auto clone = config->cloneNode(&node, &owner, 0);

ASSERT_NE(clone, nullptr);
YGNodeFree(clone);
}

TEST_F(ConfigCloningTest, can_clone_with_context) {
config->setCloneNodeCallback(
[](YGNodeConstRef, YGNodeConstRef, size_t, void* context) {
return (YGNodeRef) context;
});

yoga::Node node{}, owner{}, clone{};
ASSERT_EQ(config->cloneNode(&node, &owner, 0, &clone), &clone);
}

void ConfigCloningTest::SetUp() {
config = {static_cast<yoga::Config*>(YGConfigNew()), YGConfigFree};
}
Expand Down
71 changes: 2 additions & 69 deletions tests/YGNodeCallbackTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,7 @@ TEST(Node, measure_with_measure_fn) {
});

ASSERT_EQ(
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost, nullptr),
(YGSize{23, 12}));
}

TEST(Node, measure_with_context_measure_fn) {
auto n = Node{};
n.setMeasureFunc([](YGNodeConstRef,
float,
YGMeasureMode,
float,
YGMeasureMode,
void* ctx) { return *(YGSize*) ctx; });

auto result = YGSize{123.4f, -56.7f};
ASSERT_EQ(
n.measure(0, YGMeasureModeUndefined, 0, YGMeasureModeUndefined, &result),
result);
}

TEST(Node, switching_measure_fn_types) {
auto n = Node{};
n.setMeasureFunc(
[](YGNodeConstRef, float, YGMeasureMode, float, YGMeasureMode, void*) {
return YGSize{};
});
n.setMeasureFunc(
[](YGNodeConstRef, float w, YGMeasureMode wm, float h, YGMeasureMode hm) {
return YGSize{w * static_cast<float>(wm), h / static_cast<float>(hm)};
});

ASSERT_EQ(
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost, nullptr),
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost),
(YGSize{23, 12}));
}

Expand All @@ -84,17 +53,6 @@ TEST(Node, hasMeasureFunc_after_unset) {
ASSERT_FALSE(n.hasMeasureFunc());
}

TEST(Node, hasMeasureFunc_after_unset_context) {
auto n = Node{};
n.setMeasureFunc(
[](YGNodeConstRef, float, YGMeasureMode, float, YGMeasureMode, void*) {
return YGSize{};
});

n.setMeasureFunc(nullptr);
ASSERT_FALSE(n.hasMeasureFunc());
}

TEST(Node, hasBaselineFunc_initial) {
auto n = Node{};
ASSERT_FALSE(n.hasBaselineFunc());
Expand All @@ -110,17 +68,7 @@ TEST(Node, baseline_with_baseline_fn) {
auto n = Node{};
n.setBaselineFunc([](YGNodeConstRef, float w, float h) { return w + h; });

ASSERT_EQ(n.baseline(1.25f, 2.5f, nullptr), 3.75f);
}

TEST(Node, baseline_with_context_baseline_fn) {
auto n = Node{};
n.setBaselineFunc([](YGNodeConstRef, 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);
ASSERT_EQ(n.baseline(1.25f, 2.5f), 3.75f);
}

TEST(Node, hasBaselineFunc_after_unset) {
Expand All @@ -130,18 +78,3 @@ TEST(Node, hasBaselineFunc_after_unset) {
n.setBaselineFunc(nullptr);
ASSERT_FALSE(n.hasBaselineFunc());
}

TEST(Node, hasBaselineFunc_after_unset_context) {
auto n = Node{};
n.setBaselineFunc([](YGNodeConstRef, float, float, void*) { return 0.0f; });

n.setMeasureFunc(nullptr);
ASSERT_FALSE(n.hasMeasureFunc());
}

TEST(Node, switching_baseline_fn_types) {
auto n = Node{};
n.setBaselineFunc([](YGNodeConstRef, float, float, void*) { return 0.0f; });
n.setBaselineFunc([](YGNodeConstRef, float, float) { return 1.0f; });
ASSERT_EQ(n.baseline(1, 2, nullptr), 1.0f);
}
7 changes: 0 additions & 7 deletions yoga/Yoga-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@

YG_EXTERN_C_BEGIN

YG_EXPORT void YGNodeCalculateLayoutWithContext(
YGNodeRef node,
float availableWidth,
float availableHeight,
YGDirection ownerDirection,
void* layoutContext);

// Deallocates a Yoga Node. Unlike YGNodeFree, does not remove the node from
// its parent or children.
YG_EXPORT void YGNodeDeallocate(YGNodeRef node);
Expand Down
16 changes: 3 additions & 13 deletions yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void YGNodeSetBaselineFunc(YGNodeRef node, YGBaselineFunc baselineFunc) {
}

YGDirtiedFunc YGNodeGetDirtiedFunc(YGNodeConstRef node) {
return resolveRef(node)->getDirtied();
return resolveRef(node)->getDirtiedFunc();
}

void YGNodeSetDirtiedFunc(YGNodeRef node, YGDirtiedFunc dirtiedFunc) {
Expand Down Expand Up @@ -805,7 +805,7 @@ void YGNodePrint(const YGNodeConstRef nodeRef, const YGPrintOptions options) {
const auto node = resolveRef(nodeRef);
std::string str;
yoga::nodeToString(str, node, options, 0);
yoga::log(node, YGLogLevelDebug, nullptr, str.c_str());
yoga::log(node, YGLogLevelDebug, str.c_str());
}
#endif

Expand Down Expand Up @@ -927,16 +927,6 @@ void YGNodeCalculateLayout(
const float ownerWidth,
const float ownerHeight,
const YGDirection ownerDirection) {
YGNodeCalculateLayoutWithContext(
node, ownerWidth, ownerHeight, ownerDirection, nullptr);
}

void YGNodeCalculateLayoutWithContext(
const YGNodeRef node,
const float ownerWidth,
const float ownerHeight,
const YGDirection ownerDirection,
void* layoutContext) {
yoga::calculateLayout(
resolveRef(node), ownerWidth, ownerHeight, ownerDirection, layoutContext);
resolveRef(node), ownerWidth, ownerHeight, ownerDirection);
}
7 changes: 3 additions & 4 deletions yoga/algorithm/Baseline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@

namespace facebook::yoga {

float calculateBaseline(const yoga::Node* node, void* layoutContext) {
float calculateBaseline(const yoga::Node* node) {
if (node->hasBaselineFunc()) {

Event::publish<Event::NodeBaselineStart>(node);

const float baseline = node->baseline(
node->getLayout().measuredDimensions[YGDimensionWidth],
node->getLayout().measuredDimensions[YGDimensionHeight],
layoutContext);
node->getLayout().measuredDimensions[YGDimensionHeight]);

Event::publish<Event::NodeBaselineEnd>(node);

Expand Down Expand Up @@ -58,7 +57,7 @@ float calculateBaseline(const yoga::Node* node, void* layoutContext) {
return node->getLayout().measuredDimensions[YGDimensionHeight];
}

const float baseline = calculateBaseline(baselineChild, layoutContext);
const float baseline = calculateBaseline(baselineChild);
return baseline + baselineChild->getLayout().position[YGEdgeTop];
}

Expand Down
2 changes: 1 addition & 1 deletion yoga/algorithm/Baseline.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace facebook::yoga {

// Calculate baseline represented as an offset from the top edge of the node.
float calculateBaseline(const yoga::Node* node, void* layoutContext);
float calculateBaseline(const yoga::Node* node);

// Whether any of the children of this node participate in baseline alignment
bool isBaselineLayout(const yoga::Node* node);
Expand Down
Loading

0 comments on commit 170d634

Please sign in to comment.