Skip to content

Commit

Permalink
Make Spacing cloneable
Browse files Browse the repository at this point in the history
Reviewed By: achen1

Differential Revision: D7626745

fbshipit-source-id: ca6070a99d561ee5d5dd27456116dd2de75d2bf3
  • Loading branch information
mdvacca authored and facebook-github-bot committed Apr 19, 2018
1 parent 49655c6 commit 7e82486
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public YogaNode cloneNode(YogaNode oldYogaNode,
private int mScreenY;
private int mScreenWidth;
private int mScreenHeight;
private final Spacing mDefaultPadding = new Spacing(0);
private final Spacing mDefaultPadding;
private final float[] mPadding = new float[Spacing.ALL + 1];
private final boolean[] mPaddingIsPercent = new boolean[Spacing.ALL + 1];
private YogaNode mYogaNode;
Expand All @@ -115,6 +115,7 @@ public YogaNode cloneNode(YogaNode oldYogaNode,
private @Nullable ReactStylesDiffMap mNewProps;

public ReactShadowNodeImpl() {
mDefaultPadding = new Spacing(0);
if (!isVirtual()) {
YogaNode node = YogaNodePool.get().acquire();
mYogaNode = node == null ? new YogaNode(sYogaConfig) : node;
Expand All @@ -133,6 +134,7 @@ protected ReactShadowNodeImpl(ReactShadowNodeImpl original) {
mShouldNotifyOnLayout = original.mShouldNotifyOnLayout;
mIsLayoutOnly = original.mIsLayoutOnly;
mNativeParent = original.mNativeParent;
mDefaultPadding = new Spacing(original.mDefaultPadding);
// Cloned nodes should be always updated.
mNodeUpdated = true;
// "cached" screen coordinates are not cloned because FabricJS not always clone the last
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public class Spacing {
256, /*ALL*/
};

private final float[] mSpacing = newFullSpacingArray();
private final float[] mSpacing;
private int mValueFlags = 0;
private float mDefaultValue;
private final float mDefaultValue;
private boolean mHasAliasesSet;

public Spacing() {
Expand All @@ -79,6 +79,14 @@ public Spacing() {

public Spacing(float defaultValue) {
mDefaultValue = defaultValue;
mSpacing = newFullSpacingArray();
}

public Spacing(Spacing original) {
mDefaultValue = original.mDefaultValue;
mSpacing = Arrays.copyOf(original.mSpacing, original.mSpacing.length);
mValueFlags = original.mValueFlags;
mHasAliasesSet = original.mHasAliasesSet;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.fest.assertions.data.Offset;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -113,6 +114,18 @@ public void testCloneNode() {
assertThat(clonedNode.getChildAt(0)).isEqualTo(child);
}

@Test
public void testDefaultSpacingCloning() {
ReactShadowNode node = createViewNode();
node.setDefaultPadding(Spacing.LEFT, 10);

ReactShadowNode clonedNode = mFabricUIManager.cloneNode(node);

node.setDefaultPadding(Spacing.LEFT, 20);
assertThat(clonedNode.getStylePadding(Spacing.LEFT).value).isEqualTo(10f, Offset.offset(0.01f));
assertThat(node.getStylePadding(Spacing.LEFT).value).isEqualTo(20f, Offset.offset(0.01f));
}

@Test
public void testCloneVirtualNode() {
ReactRawTextShadowNode node = new ReactRawTextShadowNode();
Expand Down

0 comments on commit 7e82486

Please sign in to comment.