Merge pull request #1362 from NativeScript/hhristov/wrap-layout-fix

Fixed wrap-layout
This commit is contained in:
Hristo Hristov
2016-01-13 16:41:04 +02:00
2 changed files with 91 additions and 33 deletions

View File

@ -44,12 +44,9 @@ export class WrapLayoutTest extends testModule.UITest<wrapLayoutModule.WrapLayou
wrapLayout.width = layoutHelper.dp(200);
wrapLayout.height = layoutHelper.dp(200);
var label;
var i;
for (i = 0; i < 2; i++) {
label = new Label();
for (let i = 0; i < 2; i++) {
let label = new Label();
label.text = "" + i;
label.id = "" + i;
label.width = layoutHelper.dp(100);
label.height = layoutHelper.dp(100);
@ -59,6 +56,55 @@ export class WrapLayoutTest extends testModule.UITest<wrapLayoutModule.WrapLayou
return wrapLayout;
}
public testItemWidhtItemHeight() {
let wrap = this.testView;
wrap.removeChildren();
wrap.itemWidth = layoutHelper.dp(40);
wrap.itemHeight = layoutHelper.dp(40);
let lbl1 = new layoutHelper.MyButton();
lbl1.text = "1";
wrap.addChild(lbl1);
let lbl2 = new layoutHelper.MyButton();
lbl2.text = "2";
lbl2.width = layoutHelper.dp(80);
lbl2.height = layoutHelper.dp(80);
wrap.addChild(lbl2);
this.waitUntilTestElementLayoutIsValid();
TKUnit.assertEqual(lbl1.measureWidth, 40, "lbl1.measureWidth");
TKUnit.assertEqual(lbl1.measureHeight, 40, "lbl1.measureHeight");
TKUnit.assertEqual(lbl1.layoutWidth, 40, "lbl1.layoutWidth");
TKUnit.assertEqual(lbl1.layoutHeight, 40, "lbl1.layoutHeight");
TKUnit.assertEqual(lbl2.measureWidth, 40, "lbl2.measureWidth");
TKUnit.assertEqual(lbl2.measureHeight, 40, "lbl2.measureHeight");
TKUnit.assertEqual(lbl2.layoutWidth, 40, "lbl2.layoutWidth");
TKUnit.assertEqual(lbl2.layoutHeight, 40, "lbl2.layoutHeight");
}
public testPaddingReduceAvailableSize() {
let wrap = this.testView;
wrap.removeChildren();
wrap.paddingLeft = wrap.paddingTop = wrap.paddingRight = wrap.paddingBottom = layoutHelper.dp(10);
let lbl1 = new layoutHelper.MyButton();
lbl1.text = "1";
lbl1.minWidth = layoutHelper.dp(200);
lbl1.minHeight = layoutHelper.dp(200);
wrap.addChild(lbl1);
this.waitUntilTestElementLayoutIsValid();
TKUnit.assertEqual(lbl1.measureWidth, 180, "lbl1.measureWidth");
TKUnit.assertEqual(lbl1.measureHeight, 180, "lbl1.measureHeight");
TKUnit.assertEqual(lbl1.layoutWidth, 180, "lbl1.layoutWidth");
TKUnit.assertEqual(lbl1.layoutHeight, 180, "lbl1.layoutHeight");
}
public testHorizontalOrientation() {
this.testView.orientation = enums.Orientation.horizontal;

View File

@ -29,64 +29,76 @@ export class WrapLayout extends common.WrapLayout {
var measureWidth = 0;
var measureHeight = 0;
var width = utils.layout.getMeasureSpecSize(widthMeasureSpec);
var widthMode = utils.layout.getMeasureSpecMode(widthMeasureSpec);
var height = utils.layout.getMeasureSpecSize(heightMeasureSpec);
var heightMode = utils.layout.getMeasureSpecMode(heightMeasureSpec);
var density = utils.layout.getDisplayDensity();
var childWidthMeasureSpec: number = WrapLayout.getChildMeasureSpec(widthMode, width, this.itemWidth * density);
var childHeightMeasureSpec: number = WrapLayout.getChildMeasureSpec(heightMode, height, this.itemHeight * density);
var remainingWidth = widthMode === utils.layout.UNSPECIFIED ? Number.MAX_VALUE : width - ((this.paddingLeft + this.paddingRight) * density);
var remainingHeight = heightMode === utils.layout.UNSPECIFIED ? Number.MAX_VALUE : height - ((this.paddingTop + this.paddingBottom) * density);
var horizontalPadding = (this.paddingLeft + this.paddingRight) * density;
var verticalPadding = (this.paddingTop + this.paddingBottom) * density;
var availableWidth = widthMode === utils.layout.UNSPECIFIED ? Number.MAX_VALUE : utils.layout.getMeasureSpecSize(widthMeasureSpec) - horizontalPadding;
var availableHeight = heightMode === utils.layout.UNSPECIFIED ? Number.MAX_VALUE : utils.layout.getMeasureSpecSize(heightMeasureSpec) - verticalPadding;
var childWidthMeasureSpec: number = WrapLayout.getChildMeasureSpec(widthMode, availableWidth, this.itemWidth * density);
var childHeightMeasureSpec: number = WrapLayout.getChildMeasureSpec(heightMode, availableHeight, this.itemHeight * density);
var remainingWidth = availableWidth;
var remainingHeight = availableHeight;
this._lengths.length = 0;
var rowOrColumn = 0;
var maxLength = 0;
var isVertical = this.orientation === Orientation.vertical;
let useItemWidth: boolean = this.itemWidth > 0;
let useItemHeight: boolean = this.itemHeight > 0;
let itemWidth = this.itemWidth;
let itemHeight = this.itemHeight;
for (let i = 0, count = this.getChildrenCount(); i < count; i++) {
let child = this.getChildAt(i);
if (!child._isVisible) {
continue;
}
var childSize = View.measureChild(this, child, childWidthMeasureSpec, childHeightMeasureSpec);
var desiredSize = View.measureChild(this, child, childWidthMeasureSpec, childHeightMeasureSpec);
let childMeasuredWidth = useItemWidth ? itemWidth : desiredSize.measuredWidth;
let childMeasuredHeight = useItemHeight ? itemHeight : desiredSize.measuredHeight;
if (isVertical) {
if (childSize.measuredHeight > remainingHeight) {
if (childMeasuredHeight > remainingHeight) {
rowOrColumn++;
maxLength = Math.max(maxLength, measureHeight);
measureHeight = childSize.measuredHeight;
remainingWidth = height - childSize.measuredHeight;
this._lengths[rowOrColumn] = childSize.measuredWidth;
measureHeight = childMeasuredHeight;
remainingWidth = availableHeight - childMeasuredHeight;
this._lengths[rowOrColumn] = childMeasuredWidth;
}
else {
remainingHeight -= childSize.measuredHeight;
measureHeight += childSize.measuredHeight;
remainingHeight -= childMeasuredHeight;
measureHeight += childMeasuredHeight;
}
}
else {
if (childSize.measuredWidth > remainingWidth) {
if (childMeasuredWidth > remainingWidth) {
rowOrColumn++;
maxLength = Math.max(maxLength, measureWidth);
measureWidth = childSize.measuredWidth;
remainingWidth = width - childSize.measuredWidth;
this._lengths[rowOrColumn] = childSize.measuredHeight;
measureWidth = childMeasuredWidth;
remainingWidth = availableWidth - childMeasuredWidth;
this._lengths[rowOrColumn] = childMeasuredHeight;
}
else {
remainingWidth -= childSize.measuredWidth;
measureWidth += childSize.measuredWidth;
remainingWidth -= childMeasuredWidth;
measureWidth += childMeasuredWidth;
}
}
if (this._lengths.length <= rowOrColumn) {
this._lengths[rowOrColumn] = isVertical ? childSize.measuredWidth: childSize.measuredHeight;
this._lengths[rowOrColumn] = isVertical ? childMeasuredWidth : childMeasuredHeight;
}
else {
this._lengths[rowOrColumn] = Math.max(this._lengths[rowOrColumn], isVertical ? childSize.measuredWidth : childSize.measuredHeight);
this._lengths[rowOrColumn] = Math.max(this._lengths[rowOrColumn], isVertical ? childMeasuredWidth : childMeasuredHeight);
}
}
@ -103,14 +115,14 @@ export class WrapLayout extends common.WrapLayout {
});
}
measureWidth += (this.paddingLeft + this.paddingRight) * density;
measureHeight += (this.paddingTop + this.paddingBottom) * density;
measureWidth += horizontalPadding;
measureHeight += verticalPadding;
measureWidth = Math.max(measureWidth, this.minWidth * density);
measureHeight = Math.max(measureHeight, this.minHeight * density);
var widthAndState = View.resolveSizeAndState(measureWidth, width, widthMode, 0);
var heightAndState = View.resolveSizeAndState(measureHeight, height, heightMode, 0);
var widthAndState = View.resolveSizeAndState(measureWidth, utils.layout.getMeasureSpecSize(widthMeasureSpec), widthMode, 0);
var heightAndState = View.resolveSizeAndState(measureHeight, utils.layout.getMeasureSpecSize(heightMeasureSpec), heightMode, 0);
this.setMeasuredDimension(widthAndState, heightAndState);
}
@ -147,7 +159,7 @@ export class WrapLayout extends common.WrapLayout {
let childHeight = child.getMeasuredHeight() + (lp.topMargin + lp.bottomMargin) * density;
let length = this._lengths[rowOrColumn];
if (isVertical) {
if (isVertical) {
childWidth = length;
childHeight = this.itemHeight > 0 ? this.itemHeight * density : childHeight;
if (childTop + childHeight > childrenLength) {