Skip to content

Commit c72c152

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Expose Unsnapped Dimensions (facebook#51181)
Summary: X-link: facebook/yoga#1809 We want to know if an artifact created during measurement can fully be reused after final layout, but the final layout is allowed to be slightly larger due to pixel grid rounding (while still allowing reuse). It's hard to tell after the fact, whether it is larger because of this rounding (though the measure is used), or if it may be a pixel larger for valid reasons. We can expose the unsnapped dimensions of a node to give us this information, and to correlate measurement artifacts. This is most of the time the same as the layout's measured dimension, though I don't think it's safe to use this, since anything else measuring the node after could clobber this (I think `YGNodeLayoutGetOverflow` may also be prone to this as a bug). Changelog: [Internal] Reviewed By: rshest Differential Revision: D74292949
1 parent 4a8fda8 commit c72c152

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

packages/react-native/ReactCommon/yoga/yoga/YGNodeLayout.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,11 @@ float YGNodeLayoutGetPadding(YGNodeConstRef node, YGEdge edge) {
9090
return getResolvedLayoutProperty<&LayoutResults::padding>(
9191
node, scopedEnum(edge));
9292
}
93+
94+
float YGNodeLayoutGetRawHeight(YGNodeConstRef node) {
95+
return resolveRef(node)->getLayout().rawDimension(Dimension::Height);
96+
}
97+
98+
float YGNodeLayoutGetRawWidth(YGNodeConstRef node) {
99+
return resolveRef(node)->getLayout().rawDimension(Dimension::Width);
100+
}

packages/react-native/ReactCommon/yoga/yoga/YGNodeLayout.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@ YG_EXPORT float YGNodeLayoutGetMargin(YGNodeConstRef node, YGEdge edge);
3232
YG_EXPORT float YGNodeLayoutGetBorder(YGNodeConstRef node, YGEdge edge);
3333
YG_EXPORT float YGNodeLayoutGetPadding(YGNodeConstRef node, YGEdge edge);
3434

35+
/**
36+
* Return the measured height of the node, before layout rounding
37+
*/
38+
YG_EXPORT float YGNodeLayoutGetRawHeight(YGNodeConstRef node);
39+
40+
/**
41+
* Return the measured width of the node, before layout rounding
42+
*/
43+
YG_EXPORT float YGNodeLayoutGetRawWidth(YGNodeConstRef node);
44+
3545
YG_EXTERN_C_END

packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,25 @@ void roundLayoutResultsToPixelGrid(
106106
const bool hasFractionalHeight =
107107
!yoga::inexactEquals(round(scaledNodeHeight), scaledNodeHeight);
108108

109-
node->setLayoutDimension(
109+
node->getLayout().setDimension(
110+
Dimension::Width,
110111
roundValueToPixelGrid(
111112
absoluteNodeRight,
112113
pointScaleFactor,
113114
(textRounding && hasFractionalWidth),
114115
(textRounding && !hasFractionalWidth)) -
115116
roundValueToPixelGrid(
116-
absoluteNodeLeft, pointScaleFactor, false, textRounding),
117-
Dimension::Width);
117+
absoluteNodeLeft, pointScaleFactor, false, textRounding));
118118

119-
node->setLayoutDimension(
119+
node->getLayout().setDimension(
120+
Dimension::Height,
120121
roundValueToPixelGrid(
121122
absoluteNodeBottom,
122123
pointScaleFactor,
123124
(textRounding && hasFractionalHeight),
124125
(textRounding && !hasFractionalHeight)) -
125126
roundValueToPixelGrid(
126-
absoluteNodeTop, pointScaleFactor, false, textRounding),
127-
Dimension::Height);
127+
absoluteNodeTop, pointScaleFactor, false, textRounding));
128128
}
129129

130130
for (yoga::Node* child : node->getChildren()) {

packages/react-native/ReactCommon/yoga/yoga/node/LayoutResults.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,18 @@ struct LayoutResults {
6666
return measuredDimensions_[yoga::to_underlying(axis)];
6767
}
6868

69+
float rawDimension(Dimension axis) const {
70+
return rawDimensions_[yoga::to_underlying(axis)];
71+
}
72+
6973
void setMeasuredDimension(Dimension axis, float dimension) {
7074
measuredDimensions_[yoga::to_underlying(axis)] = dimension;
7175
}
7276

77+
void setRawDimension(Dimension axis, float dimension) {
78+
rawDimensions_[yoga::to_underlying(axis)] = dimension;
79+
}
80+
7381
float position(PhysicalEdge physicalEdge) const {
7482
return position_[yoga::to_underlying(physicalEdge)];
7583
}
@@ -113,6 +121,7 @@ struct LayoutResults {
113121

114122
std::array<float, 2> dimensions_ = {{YGUndefined, YGUndefined}};
115123
std::array<float, 2> measuredDimensions_ = {{YGUndefined, YGUndefined}};
124+
std::array<float, 2> rawDimensions_ = {{YGUndefined, YGUndefined}};
116125
std::array<float, 4> position_ = {};
117126
std::array<float, 4> margin_ = {};
118127
std::array<float, 4> border_ = {};

packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ void Node::setLayoutHadOverflow(bool hadOverflow) {
247247

248248
void Node::setLayoutDimension(float lengthValue, Dimension dimension) {
249249
layout_.setDimension(dimension, lengthValue);
250+
layout_.setRawDimension(dimension, lengthValue);
250251
}
251252

252253
// If both left and right are defined, then use left. Otherwise return +left or

0 commit comments

Comments
 (0)