[java] implement proper spacing objects, with support for default spacing
This commit is contained in:
@@ -9,27 +9,102 @@
|
||||
package com.facebook.csslayout;
|
||||
|
||||
/**
|
||||
* Utility class for handling CSS spacing (padding, margin, and borders). This is mostly necessary
|
||||
* to properly implement interactions and updates for properties like margin, marginLeft, and
|
||||
* marginHorizontal. This is not a great API and should probably be updated to use actual objects
|
||||
* for type safety, defaults safety, and simplicity.
|
||||
* Class representing CSS spacing (padding, margin, and borders). This is mostly necessary to
|
||||
* properly implement interactions and updates for properties like margin, marginLeft, and
|
||||
* marginHorizontal.
|
||||
*/
|
||||
public class Spacing {
|
||||
|
||||
// Indices into FullSpacingArray and SpacingResultArray
|
||||
/**
|
||||
* Spacing type that represents the left direction. E.g. {@code marginLeft}.
|
||||
*/
|
||||
public static final int LEFT = 0;
|
||||
/**
|
||||
* Spacing type that represents the top direction. E.g. {@code marginTop}.
|
||||
*/
|
||||
public static final int TOP = 1;
|
||||
/**
|
||||
* Spacing type that represents the right direction. E.g. {@code marginRight}.
|
||||
*/
|
||||
public static final int RIGHT = 2;
|
||||
/**
|
||||
* Spacing type that represents the bottom direction. E.g. {@code marginBottom}.
|
||||
*/
|
||||
public static final int BOTTOM = 3;
|
||||
/**
|
||||
* Spacing type that represents vertical direction (top and bottom). E.g. {@code marginVertical}.
|
||||
*/
|
||||
public static final int VERTICAL = 4;
|
||||
/**
|
||||
* Spacing type that represents horizontal direction (left and right). E.g.
|
||||
* {@code marginHorizontal}.
|
||||
*/
|
||||
public static final int HORIZONTAL = 5;
|
||||
/**
|
||||
* Spacing type that represents all directions (left, top, right, bottom). E.g. {@code margin}.
|
||||
*/
|
||||
public static final int ALL = 6;
|
||||
|
||||
private final float[] mSpacing = newFullSpacingArray();
|
||||
private final float[] mDefaultSpacing = newSpacingResultArray();
|
||||
private final float[] mSpacingResult = newSpacingResultArray();
|
||||
private boolean mDirty;
|
||||
|
||||
/**
|
||||
* @return an instance of an array that can be used with {@link #updateSpacing}. Stores
|
||||
* the value for each spacing type or NaN if it hasn't been explicitly set.
|
||||
* Set a spacing value.
|
||||
*
|
||||
* @param spacingType one of {@link #LEFT}, {@link #TOP}, {@link #RIGHT}, {@link #BOTTOM},
|
||||
* {@link #VERTICAL}, {@link #HORIZONTAL}, {@link #ALL}
|
||||
* @param value the value for this direction
|
||||
* @return {@code true} if the spacing has changed, or {@code false} if the same value was already
|
||||
* set
|
||||
*/
|
||||
public static float[] newFullSpacingArray() {
|
||||
public boolean set(int spacingType, float value) {
|
||||
if (!FloatUtil.floatsEqual(mSpacing[spacingType], value)) {
|
||||
mSpacing[spacingType] = value;
|
||||
mDirty = true;
|
||||
}
|
||||
return mDirty;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a default spacing value. This is used as a fallback when no spacing has been set for a
|
||||
* particular direction.
|
||||
*
|
||||
* @param spacingType one of {@link #LEFT}, {@link #TOP}, {@link #RIGHT}, {@link #BOTTOM}
|
||||
* @param value the default value for this direction
|
||||
* @return
|
||||
*/
|
||||
public boolean setDefault(int spacingType, float value) {
|
||||
if (!FloatUtil.floatsEqual(mDefaultSpacing[spacingType], value)) {
|
||||
mDefaultSpacing[spacingType] = value;
|
||||
mDirty = true;
|
||||
}
|
||||
return mDirty;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the spacing for a direction. This takes into account any default values that have been set.
|
||||
*
|
||||
* @param spacingType one of {@link #LEFT}, {@link #TOP}, {@link #RIGHT}, {@link #BOTTOM}
|
||||
*/
|
||||
public float get(int spacingType) {
|
||||
ensureResult();
|
||||
return mSpacingResult[spacingType];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the raw value (that was set using {@link #set(int, float)}), without taking into account
|
||||
* any default values.
|
||||
*
|
||||
* @param spacingType one of {@link #LEFT}, {@link #TOP}, {@link #RIGHT}, {@link #BOTTOM},
|
||||
* {@link #VERTICAL}, {@link #HORIZONTAL}, {@link #ALL}
|
||||
*/
|
||||
public float getRaw(int spacingType) {
|
||||
return mSpacing[spacingType];
|
||||
}
|
||||
|
||||
private static float[] newFullSpacingArray() {
|
||||
return new float[] {
|
||||
CSSConstants.UNDEFINED,
|
||||
CSSConstants.UNDEFINED,
|
||||
@@ -42,18 +117,11 @@ public class Spacing {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {@link #newSpacingResultArray} filled with zero.
|
||||
*/
|
||||
public static float[] newSpacingResultArray() {
|
||||
private static float[] newSpacingResultArray() {
|
||||
return newSpacingResultArray(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return an instance of an array used to store the end result of the interactions between the
|
||||
* values in a full spacing array. Use {@link #TOP}, etc to access result values.
|
||||
*/
|
||||
public static float[] newSpacingResultArray(float defaultValue) {
|
||||
private static float[] newSpacingResultArray(float defaultValue) {
|
||||
return new float[] {
|
||||
defaultValue,
|
||||
defaultValue,
|
||||
@@ -63,41 +131,46 @@ public class Spacing {
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the fullSpacing from {@link #newFullSpacingArray()} and the spacingResult from
|
||||
* {@link #newSpacingResultArray()} from a View, update them both to reflect a new value for the
|
||||
* given spacingType (e.g. {@link #TOP}). defaultValue specifies the result value that should be
|
||||
* used whenever a spacing property hasn't been set.
|
||||
* Given the {@param fullSpacing} and the spacingResult from {@link #newSpacingResultArray()} from
|
||||
* a View, update the result array to reflect values that have been set in {@param fullSpacing}
|
||||
* array. {@param defaultValues} specifies the result values that should be used whenever a
|
||||
* spacing property hasn't been set.
|
||||
*/
|
||||
public static void updateSpacing(
|
||||
float[] fullSpacing,
|
||||
float[] spacingResult,
|
||||
int spacingType,
|
||||
float value,
|
||||
float defaultValue) {
|
||||
fullSpacing[spacingType] = value;
|
||||
spacingResult[Spacing.TOP] =
|
||||
!CSSConstants.isUndefined(fullSpacing[Spacing.TOP]) ? fullSpacing[Spacing.TOP]
|
||||
: !CSSConstants.isUndefined(fullSpacing[Spacing.VERTICAL]) ?
|
||||
fullSpacing[Spacing.VERTICAL]
|
||||
: !CSSConstants.isUndefined(fullSpacing[Spacing.ALL]) ? fullSpacing[Spacing.ALL]
|
||||
: defaultValue;
|
||||
spacingResult[Spacing.BOTTOM] =
|
||||
!CSSConstants.isUndefined(fullSpacing[Spacing.BOTTOM]) ? fullSpacing[Spacing.BOTTOM]
|
||||
: !CSSConstants.isUndefined(fullSpacing[Spacing.VERTICAL]) ?
|
||||
fullSpacing[Spacing.VERTICAL]
|
||||
: !CSSConstants.isUndefined(fullSpacing[Spacing.ALL]) ? fullSpacing[Spacing.ALL]
|
||||
: defaultValue;
|
||||
spacingResult[Spacing.LEFT] =
|
||||
!CSSConstants.isUndefined(fullSpacing[Spacing.LEFT]) ? fullSpacing[Spacing.LEFT]
|
||||
: !CSSConstants.isUndefined(fullSpacing[Spacing.HORIZONTAL]) ?
|
||||
fullSpacing[Spacing.HORIZONTAL]
|
||||
: !CSSConstants.isUndefined(fullSpacing[Spacing.ALL]) ? fullSpacing[Spacing.ALL]
|
||||
: defaultValue;
|
||||
spacingResult[Spacing.RIGHT] =
|
||||
!CSSConstants.isUndefined(fullSpacing[Spacing.RIGHT]) ? fullSpacing[Spacing.RIGHT]
|
||||
: !CSSConstants.isUndefined(fullSpacing[Spacing.HORIZONTAL]) ?
|
||||
fullSpacing[Spacing.HORIZONTAL]
|
||||
: !CSSConstants.isUndefined(fullSpacing[Spacing.ALL]) ? fullSpacing[Spacing.ALL]
|
||||
: defaultValue;
|
||||
private void ensureResult() {
|
||||
if (mDirty) {
|
||||
mSpacingResult[TOP] =
|
||||
!CSSConstants.isUndefined(mSpacing[TOP])
|
||||
? mSpacing[TOP]
|
||||
: !CSSConstants.isUndefined(mSpacing[VERTICAL])
|
||||
? mSpacing[VERTICAL]
|
||||
: !CSSConstants.isUndefined(mSpacing[ALL])
|
||||
? mSpacing[ALL]
|
||||
: mDefaultSpacing[TOP];
|
||||
mSpacingResult[BOTTOM] =
|
||||
!CSSConstants.isUndefined(mSpacing[BOTTOM])
|
||||
? mSpacing[BOTTOM]
|
||||
: !CSSConstants.isUndefined(mSpacing[VERTICAL])
|
||||
? mSpacing[VERTICAL]
|
||||
: !CSSConstants.isUndefined(mSpacing[ALL])
|
||||
? mSpacing[ALL]
|
||||
: mDefaultSpacing[BOTTOM];
|
||||
mSpacingResult[LEFT] =
|
||||
!CSSConstants.isUndefined(mSpacing[LEFT])
|
||||
? mSpacing[LEFT]
|
||||
: !CSSConstants.isUndefined(mSpacing[HORIZONTAL])
|
||||
? mSpacing[HORIZONTAL]
|
||||
: !CSSConstants.isUndefined(mSpacing[ALL])
|
||||
? mSpacing[ALL]
|
||||
: mDefaultSpacing[LEFT];
|
||||
mSpacingResult[RIGHT] =
|
||||
!CSSConstants.isUndefined(mSpacing[RIGHT])
|
||||
? mSpacing[RIGHT]
|
||||
: !CSSConstants.isUndefined(mSpacing[HORIZONTAL])
|
||||
? mSpacing[HORIZONTAL]
|
||||
: !CSSConstants.isUndefined(mSpacing[ALL])
|
||||
? mSpacing[ALL]
|
||||
: mDefaultSpacing[RIGHT];
|
||||
mDirty = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user