Add reset() method to CSSNode

This allows users of css-layout in Java to perform faster resets on
CSSNode in cases when you want to recycle instances.
This commit is contained in:
Lucas Rocha
2015-10-05 16:26:59 +01:00
parent 0faaabb78c
commit a821f6c555
3 changed files with 69 additions and 19 deletions

View File

@@ -400,4 +400,19 @@ public class CSSNode {
dirty(); dirty();
} }
} }
/**
* Resets this instance to its default state. This method is meant to be used when
* recycling {@link CSSNode} instances.
*/
public void reset() {
if (mParent != null || (mChildren != null && mChildren.size() > 0)) {
throw new IllegalStateException("You should not reset an attached CSSNode");
}
style.reset();
layout.resetResult();
lineIndex = 0;
mLayoutState = LayoutState.DIRTY;
}
} }

View File

@@ -8,40 +8,62 @@
*/ */
package com.facebook.csslayout; package com.facebook.csslayout;
import java.util.Arrays;
/** /**
* The CSS style definition for a {@link CSSNode}. * The CSS style definition for a {@link CSSNode}.
*/ */
public class CSSStyle { public class CSSStyle {
public CSSDirection direction = CSSDirection.INHERIT; public CSSDirection direction;
public CSSFlexDirection flexDirection = CSSFlexDirection.COLUMN; public CSSFlexDirection flexDirection;
public CSSJustify justifyContent = CSSJustify.FLEX_START; public CSSJustify justifyContent;
public CSSAlign alignContent = CSSAlign.FLEX_START; public CSSAlign alignContent;
public CSSAlign alignItems = CSSAlign.STRETCH; public CSSAlign alignItems;
public CSSAlign alignSelf = CSSAlign.AUTO; public CSSAlign alignSelf;
public CSSPositionType positionType = CSSPositionType.RELATIVE; public CSSPositionType positionType;
public CSSWrap flexWrap = CSSWrap.NOWRAP; public CSSWrap flexWrap;
public float flex; public float flex;
public Spacing margin = new Spacing(); public Spacing margin = new Spacing();
public Spacing padding = new Spacing(); public Spacing padding = new Spacing();
public Spacing border = new Spacing(); public Spacing border = new Spacing();
public float[] position = { public float[] position = new float[4];
CSSConstants.UNDEFINED, public float[] dimensions = new float[2];
CSSConstants.UNDEFINED,
CSSConstants.UNDEFINED,
CSSConstants.UNDEFINED,
};
public float[] dimensions = {
CSSConstants.UNDEFINED,
CSSConstants.UNDEFINED,
};
public float minWidth = CSSConstants.UNDEFINED; public float minWidth = CSSConstants.UNDEFINED;
public float minHeight = CSSConstants.UNDEFINED; public float minHeight = CSSConstants.UNDEFINED;
public float maxWidth = CSSConstants.UNDEFINED; public float maxWidth = CSSConstants.UNDEFINED;
public float maxHeight = CSSConstants.UNDEFINED; public float maxHeight = CSSConstants.UNDEFINED;
CSSStyle() {
reset();
}
void reset() {
direction = CSSDirection.INHERIT;
flexDirection = CSSFlexDirection.COLUMN;
justifyContent = CSSJustify.FLEX_START;
alignContent = CSSAlign.FLEX_START;
alignItems = CSSAlign.STRETCH;
alignSelf = CSSAlign.AUTO;
positionType = CSSPositionType.RELATIVE;
flexWrap = CSSWrap.NOWRAP;
flex = 0f;
margin.reset();;
padding.reset();
border.reset();
Arrays.fill(position, CSSConstants.UNDEFINED);
Arrays.fill(dimensions, CSSConstants.UNDEFINED);
minWidth = CSSConstants.UNDEFINED;
minHeight = CSSConstants.UNDEFINED;
maxWidth = CSSConstants.UNDEFINED;
maxHeight = CSSConstants.UNDEFINED;
}
} }

View File

@@ -10,6 +10,8 @@ package com.facebook.csslayout;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Arrays;
/** /**
* Class representing CSS spacing (padding, margin, and borders). This is mostly necessary to * Class representing CSS spacing (padding, margin, and borders). This is mostly necessary to
* properly implement interactions and updates for properties like margin, marginLeft, and * properly implement interactions and updates for properties like margin, marginLeft, and
@@ -161,6 +163,17 @@ public class Spacing {
return mSpacing[spacingType]; return mSpacing[spacingType];
} }
/**
* Resets the spacing instance to its default state. This method is meant to be used when
* recycling {@link Spacing} instances.
*/
void reset() {
Arrays.fill(mSpacing, CSSConstants.UNDEFINED);
mDefaultSpacing = null;
mHasAliasesSet = false;
mValueFlags = 0;
}
/** /**
* Try to get start value and fallback to given type if not defined. This is used privately * Try to get start value and fallback to given type if not defined. This is used privately
* by the layout engine as a more efficient way to fetch direction-aware values by * by the layout engine as a more efficient way to fetch direction-aware values by