Skip to content

Commit 6eb5bd3

Browse files
added functionality using which child node can tell parent node to use it as a reference baseline
Summary: @public Currently only parent can tell the layout to align its children based on baseline. But if one of the children is a column or row then basealign does not work as expected. We have added an api setReferenceBaseline which when set to true would mean that it's baseline would be considered as the reference baseline for parent amongst its siblings. If there are more than one siblings with referenceBaseline set, the first one would be considered. Reviewed By: davidaurelio Differential Revision: D12883323 fbshipit-source-id: 19beccfc47d98bb38f81f5b66ba764e83680f821
1 parent a508134 commit 6eb5bd3

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

ReactAndroid/src/main/java/com/facebook/yoga/YogaNode.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public YogaNode getChildAt(int i) {
159159
}
160160

161161
private static native void jni_YGNodeInsertChild(long nativePointer, long childPointer, int index);
162+
162163
public void addChildAt(YogaNode child, int i) {
163164
if (child.mOwner != null) {
164165
throw new IllegalStateException("Child already has a parent, it must be removed first.");
@@ -183,6 +184,18 @@ public void addSharedChildAt(YogaNode child, int i) {
183184
jni_YGNodeInsertSharedChild(mNativePointer, child.mNativePointer, i);
184185
}
185186

187+
private static native void jni_YGNodeSetIsReferenceBaseline(long nativePointer, boolean isReferenceBaseline);
188+
189+
public void setIsReferenceBaseline(boolean isReferenceBaseline) {
190+
jni_YGNodeSetIsReferenceBaseline(mNativePointer, isReferenceBaseline);
191+
}
192+
193+
private static native boolean jni_YGNodeIsReferenceBaseline(long nativePointer);
194+
195+
public boolean isReferenceBaseline() {
196+
return jni_YGNodeIsReferenceBaseline(mNativePointer);
197+
}
198+
186199
private static native void jni_YGNodeSetOwner(long nativePointer, long newOwnerNativePointer);
187200

188201
private native long jni_YGNodeClone(long nativePointer, Object newNode);

ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,17 @@ void jni_YGNodeRemoveChild(jlong nativePointer, jlong childPointer) {
371371
_jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer));
372372
}
373373

374+
void jni_YGNodeSetIsReferenceBaseline(
375+
jlong nativePointer,
376+
jboolean isReferenceBaseline) {
377+
YGNodeSetIsReferenceBaseline(
378+
_jlong2YGNodeRef(nativePointer), isReferenceBaseline);
379+
}
380+
381+
jboolean jni_YGNodeIsReferenceBaseline(jlong nativePointer) {
382+
return YGNodeIsReferenceBaseline(_jlong2YGNodeRef(nativePointer));
383+
}
384+
374385
void jni_YGNodeCalculateLayout(
375386
alias_ref<jclass>,
376387
jlong nativePointer,
@@ -666,6 +677,8 @@ jint jni_YGNodeGetInstanceCount() {
666677
YGMakeCriticalNativeMethod(jni_YGNodeInsertChild), \
667678
YGMakeCriticalNativeMethod(jni_YGNodeInsertSharedChild), \
668679
YGMakeCriticalNativeMethod(jni_YGNodeRemoveChild), \
680+
YGMakeCriticalNativeMethod(jni_YGNodeSetIsReferenceBaseline), \
681+
YGMakeCriticalNativeMethod(jni_YGNodeIsReferenceBaseline), \
669682
YGMakeNativeMethod(jni_YGNodeCalculateLayout), \
670683
YGMakeCriticalNativeMethod(jni_YGNodeMarkDirty), \
671684
YGMakeCriticalNativeMethod( \

ReactCommon/yoga/yoga/YGNode.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct YGNode {
1717
void* context_ = nullptr;
1818
YGPrintFunc print_ = nullptr;
1919
bool hasNewLayout_ = true;
20+
bool isReferenceBaseline_ = false;
2021
YGNodeType nodeType_ = YGNodeTypeDefault;
2122
YGMeasureFunc measure_ = nullptr;
2223
YGBaselineFunc baseline_ = nullptr;
@@ -93,6 +94,10 @@ struct YGNode {
9394
return lineIndex_;
9495
}
9596

97+
bool isReferenceBaseline() {
98+
return isReferenceBaseline_;
99+
}
100+
96101
// returns the YGNodeRef that owns this YGNode. An owner is used to identify
97102
// the YogaTree that a YGNode belongs to.
98103
// This method will return the parent of the YGNode when a YGNode only belongs
@@ -211,6 +216,10 @@ struct YGNode {
211216
lineIndex_ = lineIndex;
212217
}
213218

219+
void setIsReferenceBaseline(bool isReferenceBaseline) {
220+
isReferenceBaseline_ = isReferenceBaseline;
221+
}
222+
214223
void setOwner(YGNodeRef owner) {
215224
owner_ = owner;
216225
}

ReactCommon/yoga/yoga/Yoga.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,14 @@ void YGConfigCopy(const YGConfigRef dest, const YGConfigRef src) {
380380
memcpy(dest, src, sizeof(YGConfig));
381381
}
382382

383+
void YGNodeSetIsReferenceBaseline(YGNodeRef node, bool isReferenceBaseline) {
384+
node->setIsReferenceBaseline(isReferenceBaseline);
385+
}
386+
387+
bool YGNodeIsReferenceBaseline(YGNodeRef node) {
388+
return node->isReferenceBaseline();
389+
}
390+
383391
void YGNodeInsertChild(
384392
const YGNodeRef node,
385393
const YGNodeRef child,
@@ -1138,7 +1146,8 @@ static float YGBaseline(const YGNodeRef node) {
11381146
if (child->getStyle().positionType == YGPositionTypeAbsolute) {
11391147
continue;
11401148
}
1141-
if (YGNodeAlignItem(node, child) == YGAlignBaseline) {
1149+
if (YGNodeAlignItem(node, child) == YGAlignBaseline ||
1150+
child->isReferenceBaseline()) {
11421151
baselineChild = child;
11431152
break;
11441153
}

ReactCommon/yoga/yoga/Yoga.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ WIN_EXPORT void YGNodeSetChildren(
114114
const YGNodeRef children[],
115115
const uint32_t count);
116116

117+
WIN_EXPORT void YGNodeSetIsReferenceBaseline(
118+
YGNodeRef node,
119+
bool isReferenceBaseline);
120+
121+
WIN_EXPORT bool YGNodeIsReferenceBaseline(YGNodeRef node);
122+
117123
WIN_EXPORT void YGNodeCalculateLayout(
118124
const YGNodeRef node,
119125
const float availableWidth,

0 commit comments

Comments
 (0)