Skip to content

Commit

Permalink
added functionality using which child node can tell parent node to us…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
SidharthGuglani-zz authored and facebook-github-bot committed Nov 14, 2018
1 parent a508134 commit 6eb5bd3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
13 changes: 13 additions & 0 deletions ReactAndroid/src/main/java/com/facebook/yoga/YogaNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public YogaNode getChildAt(int i) {
}

private static native void jni_YGNodeInsertChild(long nativePointer, long childPointer, int index);

public void addChildAt(YogaNode child, int i) {
if (child.mOwner != null) {
throw new IllegalStateException("Child already has a parent, it must be removed first.");
Expand All @@ -183,6 +184,18 @@ public void addSharedChildAt(YogaNode child, int i) {
jni_YGNodeInsertSharedChild(mNativePointer, child.mNativePointer, i);
}

private static native void jni_YGNodeSetIsReferenceBaseline(long nativePointer, boolean isReferenceBaseline);

public void setIsReferenceBaseline(boolean isReferenceBaseline) {
jni_YGNodeSetIsReferenceBaseline(mNativePointer, isReferenceBaseline);
}

private static native boolean jni_YGNodeIsReferenceBaseline(long nativePointer);

public boolean isReferenceBaseline() {
return jni_YGNodeIsReferenceBaseline(mNativePointer);
}

private static native void jni_YGNodeSetOwner(long nativePointer, long newOwnerNativePointer);

private native long jni_YGNodeClone(long nativePointer, Object newNode);
Expand Down
13 changes: 13 additions & 0 deletions ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,17 @@ void jni_YGNodeRemoveChild(jlong nativePointer, jlong childPointer) {
_jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer));
}

void jni_YGNodeSetIsReferenceBaseline(
jlong nativePointer,
jboolean isReferenceBaseline) {
YGNodeSetIsReferenceBaseline(
_jlong2YGNodeRef(nativePointer), isReferenceBaseline);
}

jboolean jni_YGNodeIsReferenceBaseline(jlong nativePointer) {
return YGNodeIsReferenceBaseline(_jlong2YGNodeRef(nativePointer));
}

void jni_YGNodeCalculateLayout(
alias_ref<jclass>,
jlong nativePointer,
Expand Down Expand Up @@ -666,6 +677,8 @@ jint jni_YGNodeGetInstanceCount() {
YGMakeCriticalNativeMethod(jni_YGNodeInsertChild), \
YGMakeCriticalNativeMethod(jni_YGNodeInsertSharedChild), \
YGMakeCriticalNativeMethod(jni_YGNodeRemoveChild), \
YGMakeCriticalNativeMethod(jni_YGNodeSetIsReferenceBaseline), \
YGMakeCriticalNativeMethod(jni_YGNodeIsReferenceBaseline), \
YGMakeNativeMethod(jni_YGNodeCalculateLayout), \
YGMakeCriticalNativeMethod(jni_YGNodeMarkDirty), \
YGMakeCriticalNativeMethod( \
Expand Down
9 changes: 9 additions & 0 deletions ReactCommon/yoga/yoga/YGNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct YGNode {
void* context_ = nullptr;
YGPrintFunc print_ = nullptr;
bool hasNewLayout_ = true;
bool isReferenceBaseline_ = false;
YGNodeType nodeType_ = YGNodeTypeDefault;
YGMeasureFunc measure_ = nullptr;
YGBaselineFunc baseline_ = nullptr;
Expand Down Expand Up @@ -93,6 +94,10 @@ struct YGNode {
return lineIndex_;
}

bool isReferenceBaseline() {
return isReferenceBaseline_;
}

// returns the YGNodeRef that owns this YGNode. An owner is used to identify
// the YogaTree that a YGNode belongs to.
// This method will return the parent of the YGNode when a YGNode only belongs
Expand Down Expand Up @@ -211,6 +216,10 @@ struct YGNode {
lineIndex_ = lineIndex;
}

void setIsReferenceBaseline(bool isReferenceBaseline) {
isReferenceBaseline_ = isReferenceBaseline;
}

void setOwner(YGNodeRef owner) {
owner_ = owner;
}
Expand Down
11 changes: 10 additions & 1 deletion ReactCommon/yoga/yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,14 @@ void YGConfigCopy(const YGConfigRef dest, const YGConfigRef src) {
memcpy(dest, src, sizeof(YGConfig));
}

void YGNodeSetIsReferenceBaseline(YGNodeRef node, bool isReferenceBaseline) {
node->setIsReferenceBaseline(isReferenceBaseline);
}

bool YGNodeIsReferenceBaseline(YGNodeRef node) {
return node->isReferenceBaseline();
}

void YGNodeInsertChild(
const YGNodeRef node,
const YGNodeRef child,
Expand Down Expand Up @@ -1138,7 +1146,8 @@ static float YGBaseline(const YGNodeRef node) {
if (child->getStyle().positionType == YGPositionTypeAbsolute) {
continue;
}
if (YGNodeAlignItem(node, child) == YGAlignBaseline) {
if (YGNodeAlignItem(node, child) == YGAlignBaseline ||
child->isReferenceBaseline()) {
baselineChild = child;
break;
}
Expand Down
6 changes: 6 additions & 0 deletions ReactCommon/yoga/yoga/Yoga.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ WIN_EXPORT void YGNodeSetChildren(
const YGNodeRef children[],
const uint32_t count);

WIN_EXPORT void YGNodeSetIsReferenceBaseline(
YGNodeRef node,
bool isReferenceBaseline);

WIN_EXPORT bool YGNodeIsReferenceBaseline(YGNodeRef node);

WIN_EXPORT void YGNodeCalculateLayout(
const YGNodeRef node,
const float availableWidth,
Expand Down

0 comments on commit 6eb5bd3

Please sign in to comment.