Skip to content

Commit

Permalink
Add android bindings to margin:auto
Browse files Browse the repository at this point in the history
Summary: [This commit](facebook@1146013) (or diff D4501142) adds an `auto` option for margins.  This diff allows you to leverage that in android via attribute `yoga:margin_all="auto"` (and as expected for the other edges).

Reviewed By: emilsjolander

Differential Revision: D4634684

fbshipit-source-id: 158f70ec975b5bb3a666e590b76eb52daeb38f49
  • Loading branch information
Robert Spencer authored and facebook-github-bot committed Mar 1, 2017
1 parent 3ef2970 commit b940fad
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 27 deletions.
74 changes: 56 additions & 18 deletions android/src/main/java/com/facebook/yoga/android/YogaLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -107,7 +106,7 @@ YogaNode getYogaNodeForView(View view) {
* Adds a child view with the specified layout parameters.
*
* In the typical View is added, this constructs a {@code YogaNode} for this child and applies all
* the {@code yoga:*} attributes. The Toga node is added to the Yoga tree and the child is added
* the {@code yoga:*} attributes. The Yoga node is added to the Yoga tree and the child is added
* to this ViewGroup.
*
* If the child is a {@link YogaLayout} itself, we do not construct a new Yoga node for that
Expand Down Expand Up @@ -380,9 +379,9 @@ protected static void applyLayoutParams(LayoutParams layoutParameters, YogaNode
}
}

for (int i = 0; i < layoutParameters.attributes.size(); i++) {
final int attribute = layoutParameters.attributes.keyAt(i);
final float value = layoutParameters.attributes.valueAt(i);
for (int i = 0; i < layoutParameters.numericAttributes.size(); i++) {
final int attribute = layoutParameters.numericAttributes.keyAt(i);
final float value = layoutParameters.numericAttributes.valueAt(i);

if (attribute == R.styleable.yoga_align_content) {
node.setAlignContent(YogaAlign.fromInt(Math.round(value)));
Expand Down Expand Up @@ -568,6 +567,33 @@ protected static void applyLayoutParams(LayoutParams layoutParameters, YogaNode
node.setWrap(YogaWrap.fromInt(Math.round(value)));
}
}

for (int i = 0; i < layoutParameters.stringAttributes.size(); i++) {
final int attribute = layoutParameters.stringAttributes.keyAt(i);
final String value = layoutParameters.stringAttributes.valueAt(i);

if (value.equals("auto")) {
if (attribute == R.styleable.yoga_margin_left) {
node.setMarginAuto(YogaEdge.LEFT);
} else if (attribute == R.styleable.yoga_margin_top) {
node.setMarginAuto(YogaEdge.TOP);
} else if (attribute == R.styleable.yoga_margin_right) {
node.setMarginAuto(YogaEdge.RIGHT);
} else if (attribute == R.styleable.yoga_margin_bottom) {
node.setMarginAuto(YogaEdge.BOTTOM);
} else if (attribute == R.styleable.yoga_margin_start) {
node.setMarginAuto(YogaEdge.START);
} else if (attribute == R.styleable.yoga_margin_end) {
node.setMarginAuto(YogaEdge.END);
} else if (attribute == R.styleable.yoga_margin_horizontal) {
node.setMarginAuto(YogaEdge.HORIZONTAL);
} else if (attribute == R.styleable.yoga_margin_vertical) {
node.setMarginAuto(YogaEdge.VERTICAL);
} else if (attribute == R.styleable.yoga_margin_all) {
node.setMarginAuto(YogaEdge.ALL);
}
}
}
}

@Override
Expand Down Expand Up @@ -608,7 +634,13 @@ public static class LayoutParams extends ViewGroup.LayoutParams {
* like align_self (enums), the integer enum value is cast (rounding is used on the other side
* to prevent precision errors). Dimension attributes are stored as float pixels.
*/
SparseArray<Float> attributes;
SparseArray<Float> numericAttributes;

/**
* A mapping from attribute keys ({@code R.styleable.yoga_*}) with string values to those
* strings. This is used for values such as "auto".
*/
SparseArray<String> stringAttributes;

/**
* Constructs a set of layout params from a source set. In the case that the source set is
Expand All @@ -620,16 +652,18 @@ public static class LayoutParams extends ViewGroup.LayoutParams {
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
if (source instanceof LayoutParams) {
attributes = ((LayoutParams) source).attributes.clone();
numericAttributes = ((LayoutParams) source).numericAttributes.clone();
stringAttributes = ((LayoutParams) source).stringAttributes.clone();
} else {
attributes = new SparseArray<>();
numericAttributes = new SparseArray<>();
stringAttributes = new SparseArray<>();

// Negative values include MATCH_PARENT and WRAP_CONTENT
if (source.width >= 0) {
attributes.put(R.styleable.yoga_width, (float) width);
numericAttributes.put(R.styleable.yoga_width, (float) width);
}
if (source.height >= 0) {
attributes.put(R.styleable.yoga_height, (float) height);
numericAttributes.put(R.styleable.yoga_height, (float) height);
}
}
}
Expand All @@ -648,13 +682,14 @@ public LayoutParams(ViewGroup.LayoutParams source) {
*/
public LayoutParams(int width, int height) {
super(width, height);
attributes = new SparseArray<>();
numericAttributes = new SparseArray<>();
stringAttributes = new SparseArray<>();
// Negative values include MATCH_PARENT and WRAP_CONTENT
if (width >= 0) {
attributes.put(R.styleable.yoga_width, (float) width);
numericAttributes.put(R.styleable.yoga_width, (float) width);
}
if (height >= 0) {
attributes.put(R.styleable.yoga_height, (float) height);
numericAttributes.put(R.styleable.yoga_height, (float) height);
}
}

Expand All @@ -667,15 +702,16 @@ public LayoutParams(int width, int height) {
*/
public LayoutParams(Context context, AttributeSet attrs) {
super(context, attrs);
attributes = new SparseArray<>();
numericAttributes = new SparseArray<>();
stringAttributes = new SparseArray<>();
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.yoga);

// Negative values include MATCH_PARENT and WRAP_CONTENT
if (width >= 0) {
attributes.put(R.styleable.yoga_width, (float) width);
numericAttributes.put(R.styleable.yoga_width, (float) width);
}
if (height >= 0) {
attributes.put(R.styleable.yoga_height, (float) height);
numericAttributes.put(R.styleable.yoga_height, (float) height);
}

final int attributeCount = a.getIndexCount();
Expand All @@ -685,11 +721,13 @@ public LayoutParams(Context context, AttributeSet attrs) {
a.getValue(attribute, val);

if (val.type == TypedValue.TYPE_DIMENSION) {
attributes.put(
numericAttributes.put(
attribute,
(float) a.getDimensionPixelSize(attribute, 0));
} else if (val.type == TypedValue.TYPE_STRING) {
stringAttributes.put(attribute, a.getString(attribute));
} else {
attributes.put(attribute, a.getFloat(attribute, 0));
numericAttributes.put(attribute, a.getFloat(attribute, 0));
}
}
a.recycle();
Expand Down
18 changes: 9 additions & 9 deletions android/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@
<enum name="space_around" value="4"/>
</attr>

<attr name="margin_left" format="dimension"/>
<attr name="margin_top" format="dimension"/>
<attr name="margin_right" format="dimension"/>
<attr name="margin_bottom" format="dimension"/>
<attr name="margin_start" format="dimension"/>
<attr name="margin_end" format="dimension"/>
<attr name="margin_horizontal" format="dimension"/>
<attr name="margin_vertical" format="dimension"/>
<attr name="margin_all" format="dimension"/>
<attr name="margin_left" format="dimension|string"/>
<attr name="margin_top" format="dimension|string"/>
<attr name="margin_right" format="dimension|string"/>
<attr name="margin_bottom" format="dimension|string"/>
<attr name="margin_start" format="dimension|string"/>
<attr name="margin_end" format="dimension|string"/>
<attr name="margin_horizontal" format="dimension|string"/>
<attr name="margin_vertical" format="dimension|string"/>
<attr name="margin_all" format="dimension|string"/>

<attr name="margin_percent_left" format="dimension"/>
<attr name="margin_percent_top" format="dimension"/>
Expand Down
4 changes: 4 additions & 0 deletions docs/_docs/api/android.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ RTL locales are supported by default. That is, unless you explicitly set the `y
## Attributes

The list of all attributes can be found in [attrs.xml](https://github.com/facebook/yoga/blob/master/android/sample/res/com/facebook/samples/yoga/res/values/attrs.xml), but logically map from the Yoga properties.

## Auto margins

You can specify `margin_left="auto"` (or `margin_right` etc.) for auto values. This is in addition to the dimensions you can speicfy, such as `margin_left="20dp"`.

0 comments on commit b940fad

Please sign in to comment.