Extend Yoga to be able clone Yoga Node with new children
Summary: This diff includes the following changes: 1 ) I extended the Java implementation of YogaNode to be able to get a full copy of a YogaNode object without copying the List of children of the original YogaNode. In other words, the new copy of the YogaNode will have the same data of the original YogaNode, but with an empty list of children. 2 ) We created a new method in Yoga.cpp called YGNodeInsertSharedChild. This new method is going to be used by Fabric in order to temporarily share a YogaNode between two "Yoga Trees" (the "current Yoga" tree and a partial "clone of the current Yoga tree"). We exposed this new functionality in the java implementation of Yoga (method addSharedChildAt) I'm including sebmarkbage for more context. Reviewed By: emilsjolander Differential Revision: D7245421 fbshipit-source-id: 72578c8261f29e4a12fc6c72a91f2f891cd58d48
This commit is contained in:
committed by
Facebook Github Bot
parent
f0edefdbb7
commit
5e3ffb39a2
@@ -30,7 +30,7 @@ public class YogaNode implements Cloneable {
|
||||
static native int jni_YGNodeGetInstanceCount();
|
||||
|
||||
private YogaNode mOwner;
|
||||
private List<YogaNode> mChildren;
|
||||
@Nullable private List<YogaNode> mChildren;
|
||||
private YogaMeasureFunction mMeasureFunction;
|
||||
private YogaBaselineFunction mBaselineFunction;
|
||||
private long mNativePointer;
|
||||
@@ -147,6 +147,9 @@ public class YogaNode implements Cloneable {
|
||||
}
|
||||
|
||||
public YogaNode getChildAt(int i) {
|
||||
if (mChildren == null) {
|
||||
throw new IllegalStateException("YogaNode does not have children");
|
||||
}
|
||||
return mChildren.get(i);
|
||||
}
|
||||
|
||||
@@ -164,21 +167,62 @@ public class YogaNode implements Cloneable {
|
||||
jni_YGNodeInsertChild(mNativePointer, child.mNativePointer, i);
|
||||
}
|
||||
|
||||
private native void jni_YGNodeInsertSharedChild(long nativePointer, long childPointer, int index);
|
||||
|
||||
public void addSharedChildAt(YogaNode child, int i) {
|
||||
if (mChildren == null) {
|
||||
mChildren = new ArrayList<>(4);
|
||||
}
|
||||
mChildren.add(i, child);
|
||||
child.mOwner = null;
|
||||
jni_YGNodeInsertSharedChild(mNativePointer, child.mNativePointer, i);
|
||||
}
|
||||
|
||||
private native long jni_YGNodeClone(long nativePointer, Object newNode);
|
||||
|
||||
@Override
|
||||
public YogaNode clone() throws CloneNotSupportedException {
|
||||
YogaNode clonedYogaNode = (YogaNode) super.clone();
|
||||
long clonedNativePointer = jni_YGNodeClone(mNativePointer, clonedYogaNode);
|
||||
clonedYogaNode.mNativePointer = clonedNativePointer;
|
||||
clonedYogaNode.mChildren =
|
||||
mChildren != null ? (List<YogaNode>) ((ArrayList) mChildren).clone() : null;
|
||||
return clonedYogaNode;
|
||||
public YogaNode clone() {
|
||||
try {
|
||||
YogaNode clonedYogaNode = (YogaNode) super.clone();
|
||||
long clonedNativePointer = jni_YGNodeClone(mNativePointer, clonedYogaNode);
|
||||
clonedYogaNode.mNativePointer = clonedNativePointer;
|
||||
clonedYogaNode.mOwner = null;
|
||||
clonedYogaNode.mChildren =
|
||||
mChildren != null ? (List<YogaNode>) ((ArrayList) mChildren).clone() : null;
|
||||
return clonedYogaNode;
|
||||
} catch (CloneNotSupportedException ex) {
|
||||
// This class implements Cloneable, this should not happen
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public YogaNode cloneWithNewChildren() {
|
||||
try {
|
||||
YogaNode clonedYogaNode = (YogaNode) super.clone();
|
||||
long clonedNativePointer = jni_YGNodeClone(mNativePointer, clonedYogaNode);
|
||||
clonedYogaNode.mOwner = null;
|
||||
clonedYogaNode.mNativePointer = clonedNativePointer;
|
||||
clonedYogaNode.clearChildren();
|
||||
return clonedYogaNode;
|
||||
} catch (CloneNotSupportedException ex) {
|
||||
// This class implements Cloneable, this should not happen
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private native void jni_YGNodeClearChildren(long nativePointer);
|
||||
|
||||
private void clearChildren() {
|
||||
mChildren = null;
|
||||
jni_YGNodeClearChildren(mNativePointer);
|
||||
}
|
||||
|
||||
private native void jni_YGNodeRemoveChild(long nativePointer, long childPointer);
|
||||
public YogaNode removeChildAt(int i) {
|
||||
|
||||
if (mChildren == null) {
|
||||
throw new IllegalStateException(
|
||||
"Trying to remove a child of a YogaNode that does not have children");
|
||||
}
|
||||
final YogaNode child = mChildren.remove(i);
|
||||
child.mOwner = null;
|
||||
jni_YGNodeRemoveChild(mNativePointer, child.mNativePointer);
|
||||
@@ -715,8 +759,12 @@ public class YogaNode implements Cloneable {
|
||||
*/
|
||||
@DoNotStrip
|
||||
private final long replaceChild(YogaNode newNode, int childIndex) {
|
||||
if (mChildren == null) {
|
||||
throw new IllegalStateException("Cannot replace child. YogaNode does not have children");
|
||||
}
|
||||
mChildren.remove(childIndex);
|
||||
mChildren.add(childIndex, newNode);
|
||||
newNode.mOwner = this;
|
||||
return newNode.mNativePointer;
|
||||
}
|
||||
}
|
||||
|
@@ -284,6 +284,11 @@ void jni_YGNodeFree(alias_ref<jobject> thiz, jlong nativePointer) {
|
||||
YGNodeFree(node);
|
||||
}
|
||||
|
||||
void jni_YGNodeClearChildren(alias_ref<jobject> thiz, jlong nativePointer) {
|
||||
const YGNodeRef node = _jlong2YGNodeRef(nativePointer);
|
||||
node->clearChildren();
|
||||
}
|
||||
|
||||
void jni_YGNodeReset(alias_ref<jobject> thiz, jlong nativePointer) {
|
||||
const YGNodeRef node = _jlong2YGNodeRef(nativePointer);
|
||||
void* context = node->getContext();
|
||||
@@ -303,6 +308,15 @@ void jni_YGNodeInsertChild(alias_ref<jobject>, jlong nativePointer, jlong childP
|
||||
YGNodeInsertChild(_jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer), index);
|
||||
}
|
||||
|
||||
void jni_YGNodeInsertSharedChild(
|
||||
alias_ref<jobject>,
|
||||
jlong nativePointer,
|
||||
jlong childPointer,
|
||||
jint index) {
|
||||
YGNodeInsertSharedChild(
|
||||
_jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer), index);
|
||||
}
|
||||
|
||||
void jni_YGNodeRemoveChild(alias_ref<jobject>, jlong nativePointer, jlong childPointer) {
|
||||
YGNodeRemoveChild(_jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer));
|
||||
}
|
||||
@@ -577,7 +591,9 @@ jint JNI_OnLoad(JavaVM *vm, void *) {
|
||||
YGMakeNativeMethod(jni_YGNodeNewWithConfig),
|
||||
YGMakeNativeMethod(jni_YGNodeFree),
|
||||
YGMakeNativeMethod(jni_YGNodeReset),
|
||||
YGMakeNativeMethod(jni_YGNodeClearChildren),
|
||||
YGMakeNativeMethod(jni_YGNodeInsertChild),
|
||||
YGMakeNativeMethod(jni_YGNodeInsertSharedChild),
|
||||
YGMakeNativeMethod(jni_YGNodeRemoveChild),
|
||||
YGMakeNativeMethod(jni_YGNodeCalculateLayout),
|
||||
YGMakeNativeMethod(jni_YGNodeMarkDirty),
|
||||
|
@@ -10,6 +10,7 @@ package com.facebook.yoga;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
@@ -251,6 +252,48 @@ public class YogaNodeTest {
|
||||
assertEquals(1, clonedChild.getChildCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCloneWithNewChildren() throws Exception {
|
||||
YogaConfig config = new YogaConfig();
|
||||
YogaNode root = new YogaNode(config);
|
||||
YogaNode child = new YogaNode(config);
|
||||
YogaNode grandChild = new YogaNode(config);
|
||||
root.addChildAt(child, 0);
|
||||
child.addChildAt(grandChild, 0);
|
||||
child.setFlexDirection(YogaFlexDirection.ROW);
|
||||
|
||||
YogaNode clonedChild = child.cloneWithNewChildren();
|
||||
|
||||
assertNotSame(clonedChild, child);
|
||||
assertEquals(YogaFlexDirection.ROW, clonedChild.getFlexDirection());
|
||||
assertEquals(child.getFlexDirection(), clonedChild.getFlexDirection());
|
||||
assertEquals(0, clonedChild.getChildCount());
|
||||
assertEquals(1, child.getChildCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddSharedChildCloneWithNewChildren() throws Exception {
|
||||
YogaConfig config = new YogaConfig();
|
||||
YogaNode root = new YogaNode(config);
|
||||
YogaNode child = new YogaNode(config);
|
||||
YogaNode grandChild = new YogaNode(config);
|
||||
root.addChildAt(child, 0);
|
||||
child.addChildAt(grandChild, 0);
|
||||
child.setFlexDirection(YogaFlexDirection.ROW);
|
||||
|
||||
YogaNode clonedChild = child.cloneWithNewChildren();
|
||||
|
||||
assertNotSame(clonedChild, child);
|
||||
assertEquals(YogaFlexDirection.ROW, clonedChild.getFlexDirection());
|
||||
assertEquals(child.getFlexDirection(), clonedChild.getFlexDirection());
|
||||
assertEquals(0, clonedChild.getChildCount());
|
||||
assertEquals(1, child.getChildCount());
|
||||
|
||||
clonedChild.addSharedChildAt(grandChild, 0);
|
||||
assertEquals(1, clonedChild.getChildCount());
|
||||
assertNull(grandChild.getOwner());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCloneNodeListener() throws Exception {
|
||||
final AtomicBoolean onNodeClonedExecuted = new AtomicBoolean(false);
|
||||
@@ -259,13 +302,8 @@ public class YogaNodeTest {
|
||||
new YogaNodeCloneFunction() {
|
||||
@Override
|
||||
public YogaNode cloneNode(YogaNode oldNode, YogaNode owner, int childIndex) {
|
||||
try {
|
||||
onNodeClonedExecuted.set(true);
|
||||
return oldNode.clone();
|
||||
} catch (CloneNotSupportedException ex) {
|
||||
// DO nothing
|
||||
return null;
|
||||
}
|
||||
onNodeClonedExecuted.set(true);
|
||||
return oldNode.clone();
|
||||
}
|
||||
});
|
||||
YogaNode root = new YogaNode(config);
|
||||
@@ -296,12 +334,7 @@ public class YogaNodeTest {
|
||||
new YogaNodeCloneFunction() {
|
||||
@Override
|
||||
public YogaNode cloneNode(YogaNode oldNode, YogaNode owner, int childIndex) {
|
||||
try {
|
||||
return oldNode.clone();
|
||||
} catch (CloneNotSupportedException ex) {
|
||||
// DO nothing
|
||||
return null;
|
||||
}
|
||||
return oldNode.clone();
|
||||
}
|
||||
});
|
||||
config.setOnCloneNode(null);
|
||||
|
Reference in New Issue
Block a user