Fixed percent margins

Fixed WrapLayout
Code formatting
This commit is contained in:
Hristo Hristov
2016-01-11 11:49:20 +02:00
parent 9884a78e42
commit 375a8d2592
32 changed files with 443 additions and 193 deletions

View File

@@ -23,9 +23,8 @@ public class AbsoluteLayout extends LayoutBase {
int measureWidth = 0;
int measureHeight = 0;
int childMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
int count = this.getChildCount();
for (int i = 0; i < count; i++) {
for (int i = 0, count = this.getChildCount(); i < count; i++) {
View child = this.getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
@@ -58,9 +57,8 @@ public class AbsoluteLayout extends LayoutBase {
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int leftPadding = this.getPaddingLeft();
int topPadding = this.getPaddingTop();
int count = this.getChildCount();
for (int i = 0; i < count; i++) {
for (int i = 0, count = this.getChildCount(); i < count; i++) {
View child = this.getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
@@ -80,4 +78,4 @@ public class AbsoluteLayout extends LayoutBase {
CommonLayoutParams.restoreOriginalParams(this);
}
}
}

View File

@@ -22,6 +22,7 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams {
static final String TAG = "NSLayout";
static int debuggable = -1;
private static final int NOT_SET = Integer.MIN_VALUE;
private static final StringBuilder sb = new StringBuilder();
public CommonLayoutParams() {
@@ -36,13 +37,13 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams {
public float bottomMarginPercent = 0;
public float rightMarginPercent = 0;
public int widthOriginal = -1;
public int heightOriginal = -1;
public int widthOriginal = NOT_SET;
public int heightOriginal = NOT_SET;
public int topMarginOriginal = -1;
public int leftMarginOriginal = -1;
public int bottomMarginOriginal = -1;
public int rightMarginOriginal = -1;
public int topMarginOriginal = NOT_SET;
public int leftMarginOriginal = NOT_SET;
public int bottomMarginOriginal = NOT_SET;
public int rightMarginOriginal = NOT_SET;
public int left = 0;
public int top = 0;
@@ -252,53 +253,67 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams {
CommonLayoutParams lp = (CommonLayoutParams) child.getLayoutParams();
if (widthSpec != MeasureSpec.UNSPECIFIED) {
if (lp.widthPercent > 0) {
lp.widthOriginal = lp.width;
// If we get measured twice we will override the original value with the one calculated from percentValue from the first measure.
// So we set originalValue only the first time.
if (lp.widthOriginal == NOT_SET) {
lp.widthOriginal = lp.width;
}
lp.width = (int) (availableWidth * lp.widthPercent);
}
else {
lp.widthOriginal = -1;
lp.widthOriginal = NOT_SET;
}
if (lp.leftMarginPercent > 0) {
lp.leftMarginOriginal = lp.leftMargin;
if (lp.leftMarginOriginal == NOT_SET) {
lp.leftMarginOriginal = lp.leftMargin;
}
lp.leftMargin = (int) (availableWidth * lp.leftMarginPercent);
}
else {
lp.leftMarginOriginal = -1;
lp.leftMarginOriginal = NOT_SET;
}
if (lp.rightMarginPercent > 0) {
lp.rightMarginOriginal = lp.rightMargin;
if (lp.rightMarginOriginal == NOT_SET) {
lp.rightMarginOriginal = lp.rightMargin;
}
lp.rightMargin = (int) (availableWidth * lp.rightMarginPercent);
}
else {
lp.rightMarginOriginal = -1;
lp.rightMarginOriginal = NOT_SET;
}
}
if (heightSpec != MeasureSpec.UNSPECIFIED) {
if (lp.heightPercent > 0) {
lp.heightOriginal = lp.height;
if (lp.heightOriginal == NOT_SET) {
lp.heightOriginal = lp.height;
}
lp.height = (int) (availableHeight * lp.heightPercent);
}
else {
lp.heightOriginal = -1;
lp.heightOriginal = NOT_SET;
}
if (lp.topMarginPercent > 0) {
lp.topMarginOriginal = lp.topMargin;
if (lp.topMarginOriginal == NOT_SET) {
lp.topMarginOriginal = lp.topMargin;
}
lp.topMargin = (int) (availableHeight * lp.topMarginPercent);
}
else {
lp.topMarginOriginal = -1;
lp.topMarginOriginal = NOT_SET;
}
if (lp.bottomMarginPercent > 0) {
lp.bottomMarginOriginal = lp.bottomMargin;
if (lp.bottomMarginOriginal == NOT_SET) {
lp.bottomMarginOriginal = lp.bottomMargin;
}
lp.bottomMargin = (int) (availableHeight * lp.bottomMarginPercent);
}
else {
lp.bottomMarginOriginal = -1;
lp.bottomMarginOriginal = NOT_SET;
}
}
}
@@ -333,6 +348,13 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams {
if (lp.bottomMarginPercent > 0) {
lp.bottomMargin = lp.bottomMarginOriginal;
}
lp.widthOriginal = NOT_SET;
lp.heightOriginal = NOT_SET;
lp.leftMarginOriginal = NOT_SET;
lp.topMarginOriginal = NOT_SET;
lp.rightMarginOriginal = NOT_SET;
lp.bottomMarginOriginal = NOT_SET;
}
}
}

View File

@@ -23,8 +23,7 @@ public class ContentLayout extends LayoutBase {
int measureWidth = 0;
int measureHeight = 0;
int count = this.getChildCount();
for (int i = 0; i < count; i++) {
for (int i = 0, count = this.getChildCount(); i < count; i++) {
View child = this.getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
@@ -64,9 +63,8 @@ public class ContentLayout extends LayoutBase {
int childRight = right - left - (paddingLeft + paddingRight);
int childBottom = bottom - top - (paddingRight + paddingBottom);
int count = this.getChildCount();
for (int i = 0; i < count; i++) {
for (int i = 0, count = this.getChildCount(); i < count; i++) {
View child = this.getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
@@ -77,4 +75,4 @@ public class ContentLayout extends LayoutBase {
CommonLayoutParams.restoreOriginalParams(this);
}
}
}

View File

@@ -12,4 +12,4 @@ public enum Dock {
top,
right,
bottom
}
}

View File

@@ -175,4 +175,4 @@ public class DockLayout extends LayoutBase {
CommonLayoutParams.restoreOriginalParams(this);
}
}
}

View File

@@ -266,8 +266,7 @@ public class GridLayout extends LayoutBase {
this.helper.clearMeasureSpecs();
this.helper.init();
int childrenCount = this.getChildCount();
for (int i = 0; i < childrenCount; i++) {
for (int i = 0, count = this.getChildCount(); i < count; i++) {
View child = this.getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
@@ -1163,4 +1162,4 @@ class MeasureHelper {
this.itemMeasured(measureSpec, false);
}
}
}

View File

@@ -11,4 +11,4 @@ public enum GridUnitType {
auto,
pixel,
star
}
}

View File

@@ -248,4 +248,4 @@ public class HorizontalScrollView extends android.widget.HorizontalScrollView {
}
};
}
}
}

View File

@@ -165,4 +165,4 @@ public class ImageView extends android.widget.ImageView {
super.onDraw(canvas);
}
}
}

View File

@@ -57,4 +57,4 @@ public class ItemSpec {
public int getActualLength() {
return this._actualLength;
}
}
}

View File

@@ -64,4 +64,4 @@ public abstract class LayoutBase extends ViewGroup {
return gravity;
}
}
}

View File

@@ -8,6 +8,6 @@ package org.nativescript.widgets;
*
*/
public enum Orientation {
horzontal,
horizontal,
vertical
}
}

View File

@@ -159,8 +159,7 @@ public class StackLayout extends LayoutBase {
break;
}
int count = this.getChildCount();
for (int i = 0; i < count; i++) {
for (int i = 0, count = this.getChildCount(); i < count; i++) {
View child = this.getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
@@ -203,8 +202,7 @@ public class StackLayout extends LayoutBase {
break;
}
int count = this.getChildCount();
for (int i = 0; i < count; i++) {
for (int i = 0, count = this.getChildCount(); i < count; i++) {
View child = this.getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
@@ -215,4 +213,4 @@ public class StackLayout extends LayoutBase {
childLeft += childWidth;
}
}
}
}

View File

@@ -6,4 +6,4 @@ public class TabItemSpec {
public String title;
public int iconId;
public Drawable iconDrawable;
}
}

View File

@@ -166,4 +166,4 @@ class TabStrip extends LinearLayout {
mIndicatorColors = colors;
}
}
}
}

View File

@@ -77,7 +77,7 @@ public class VerticalScrollView extends ScrollView {
this.setPadding(0, 0, 0, 0);
}
else {
CommonLayoutParams.measureChild(child, widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
CommonLayoutParams.measureChild(child, widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
this.contentMeasuredWidth = CommonLayoutParams.getDesiredWidth(child);
this.contentMeasuredHeight = CommonLayoutParams.getDesiredHeight(child);
@@ -188,4 +188,4 @@ public class VerticalScrollView extends ScrollView {
this.scrollBy(scrollDelta, 0);
}
}
}
}

View File

@@ -15,7 +15,7 @@ public class WrapLayout extends LayoutBase {
private int _itemWidth = -1;
private int _itemHeight = -1;
private Orientation _orientation = Orientation.horzontal;
private Orientation _orientation = Orientation.horizontal;
private ArrayList<Integer> _lengths = new ArrayList<Integer>();
public WrapLayout(Context context) {
@@ -58,6 +58,24 @@ public class WrapLayout extends LayoutBase {
}
}
private int getDesiredWidth(View child) {
if (this._itemWidth > 0) {
return this._itemWidth;
}
// Add margins because layoutChild will subtract them.
return CommonLayoutParams.getDesiredWidth(child);
}
private int getDesiredHeight(View child) {
if (this._itemHeight > 0) {
return this._itemHeight;
}
// Add margins because layoutChild will subtract them.
return CommonLayoutParams.getDesiredHeight(child);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
CommonLayoutParams.adjustChildrenLayoutParams(this, widthMeasureSpec, heightMeasureSpec);
@@ -65,44 +83,42 @@ public class WrapLayout extends LayoutBase {
int measureWidth = 0;
int measureHeight = 0;
int width = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
boolean isVertical = this._orientation == Orientation.vertical;
int verticalPadding = this.getPaddingTop() + this.getPaddingBottom();
int horizontalPadding = this.getPaddingLeft() + this.getPaddingRight();
int childWidthMeasureSpec = getViewMeasureSpec(widthMode, width, this._itemWidth);
int childHeightMeasureSpec = getViewMeasureSpec(heightMode, height, this._itemHeight);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int remainingWidth = widthMode == MeasureSpec.UNSPECIFIED ? Integer.MAX_VALUE : width - horizontalPadding;
int remainingHeight = heightMode == MeasureSpec.UNSPECIFIED ? Integer.MAX_VALUE : height - verticalPadding;
int availableWidth = widthMode == MeasureSpec.UNSPECIFIED ? Integer.MAX_VALUE : MeasureSpec.getSize(widthMeasureSpec) - horizontalPadding;
int availableHeight = heightMode == MeasureSpec.UNSPECIFIED ? Integer.MAX_VALUE : MeasureSpec.getSize(heightMeasureSpec) - verticalPadding;
int childWidthMeasureSpec = getViewMeasureSpec(widthMode, availableWidth, this._itemWidth);
int childHeightMeasureSpec = getViewMeasureSpec(heightMode, availableHeight, this._itemHeight);
int remainingWidth = availableWidth;
int remainingHeight = availableHeight;
int count = this.getChildCount();
this._lengths.clear();
int rowOrColumn = 0;
int maxLength = 0;
for (int i = 0; i < count; i++) {
for (int i = 0, count = this.getChildCount(); i < count; i++) {
View child = this.getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
}
CommonLayoutParams.measureChild(child, childWidthMeasureSpec, childHeightMeasureSpec);
final int childMeasuredWidth = CommonLayoutParams.getDesiredWidth(child);
final int childMeasuredHeight = CommonLayoutParams.getDesiredHeight(child);
final int childMeasuredWidth = this.getDesiredWidth(child);
final int childMeasuredHeight = this.getDesiredHeight(child);
if (isVertical) {
if (childMeasuredHeight > remainingHeight) {
rowOrColumn++;
maxLength = Math.max(maxLength, measureHeight);
measureHeight = childMeasuredHeight;
remainingWidth = height - childMeasuredHeight;
remainingWidth = availableHeight - childMeasuredHeight;
this._lengths.add(rowOrColumn, childMeasuredWidth);
}
else {
@@ -115,7 +131,7 @@ public class WrapLayout extends LayoutBase {
rowOrColumn++;
maxLength = Math.max(maxLength, measureWidth);
measureWidth = childMeasuredWidth;
remainingWidth = width - childMeasuredWidth;
remainingWidth = availableWidth - childMeasuredWidth;
this._lengths.add(rowOrColumn, childMeasuredHeight);
}
else {
@@ -132,16 +148,15 @@ public class WrapLayout extends LayoutBase {
}
}
count = this._lengths.size();
if (isVertical) {
measureHeight = Math.max(maxLength, measureHeight);
for (int i = 0; i < count; i++) {
for (int i = 0, count = this._lengths.size(); i < count; i++) {
measureWidth += this._lengths.get(i);
}
}
else {
measureWidth = Math.max(maxLength, measureWidth);
for (int i = 0; i < count; i++) {
for (int i = 0, count = this._lengths.size(); i < count; i++) {
measureHeight += this._lengths.get(i);
}
}
@@ -173,21 +188,18 @@ public class WrapLayout extends LayoutBase {
int childrenLength = isVertical ? bottom - top - paddingBottom : right - left - paddingRight;
int rowOrColumn = 0;
int count = this.getChildCount();
for (int i = 0; i < count; i++) {
for (int i = 0, count = this.getChildCount(); i < count; i++) {
View child = this.getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
}
// Add margins because layoutChild will subtract them.
int childWidth = CommonLayoutParams.getDesiredWidth(child);
int childHeight = CommonLayoutParams.getDesiredHeight(child);
int childWidth = this.getDesiredWidth(child);
int childHeight = this.getDesiredHeight(child);
int length = this._lengths.get(rowOrColumn);
if (isVertical) {
childWidth = length;
childHeight = this._itemHeight > 0 ? this._itemHeight : childHeight;
if (childTop + childHeight > childrenLength) {
// Move to top.
childTop = paddingTop;
@@ -203,7 +215,6 @@ public class WrapLayout extends LayoutBase {
}
}
else {
childWidth = this._itemWidth > 0 ? this._itemWidth : childWidth;
childHeight = length;
if (childLeft + childWidth > childrenLength) {
// Move to left.