Merge pull request #1503 from NativeScript/issue-1232

Fixed Issue #1232: WrapLayout crashes when itemWidth value is too high.
This commit is contained in:
Rossen Hristov
2016-02-07 13:48:40 +02:00
2 changed files with 37 additions and 12 deletions

View File

@ -182,6 +182,14 @@ export class WrapLayoutTest extends testModule.UITest<wrapLayoutModule.WrapLayou
TKUnit.assertEqual(actualValue, 50, "ActualLeft on Index 1"); TKUnit.assertEqual(actualValue, 50, "ActualLeft on Index 1");
} }
public testItemWidthLargerThanTheAvailableWidth() {
this.testView.itemWidth = layoutHelper.dp(1000);
this.waitUntilTestElementLayoutIsValid();
TKUnit.assertEqual(this.testView.getChildAt(0)._getCurrentLayoutBounds().top, 0, "ActualTop on Index 0");
TKUnit.assertEqual(this.testView.getChildAt(1)._getCurrentLayoutBounds().top, 100, "ActualTop on Index 1");
}
public testItemHeight() { public testItemHeight() {
this.testView.itemHeight = layoutHelper.dp(50); this.testView.itemHeight = layoutHelper.dp(50);
this.testView.orientation = enums.Orientation.vertical; this.testView.orientation = enums.Orientation.vertical;
@ -201,6 +209,16 @@ export class WrapLayoutTest extends testModule.UITest<wrapLayoutModule.WrapLayou
TKUnit.assertEqual(actualValue, 50, "ActualTop on Index 1"); TKUnit.assertEqual(actualValue, 50, "ActualTop on Index 1");
} }
public testItemHeightLargerThanTheAvailableHeight() {
this.testView.orientation = enums.Orientation.vertical;
this.waitUntilTestElementLayoutIsValid();
this.testView.itemHeight = layoutHelper.dp(1000);
this.waitUntilTestElementLayoutIsValid();
TKUnit.assertEqual(this.testView.getChildAt(0)._getCurrentLayoutBounds().left, 0, "ActualLeft on Index 0");
TKUnit.assertEqual(this.testView.getChildAt(1)._getCurrentLayoutBounds().left, 100, "ActualLeft on Index 1");
}
public testPaddingLeftAndTop() { public testPaddingLeftAndTop() {
this.testView.removeChildren(); this.testView.removeChildren();
this.testView.paddingLeft = layoutHelper.dp(20); this.testView.paddingLeft = layoutHelper.dp(20);

View File

@ -60,14 +60,15 @@ export class WrapLayout extends common.WrapLayout {
var desiredSize = View.measureChild(this, child, childWidthMeasureSpec, childHeightMeasureSpec); var desiredSize = View.measureChild(this, child, childWidthMeasureSpec, childHeightMeasureSpec);
let childMeasuredWidth = useItemWidth ? itemWidth : desiredSize.measuredWidth; let childMeasuredWidth = useItemWidth ? itemWidth : desiredSize.measuredWidth;
let childMeasuredHeight = useItemHeight ? itemHeight : desiredSize.measuredHeight; let childMeasuredHeight = useItemHeight ? itemHeight : desiredSize.measuredHeight;
let isFirst = this._lengths.length <= rowOrColumn;
if (isVertical) { if (isVertical) {
if (childMeasuredHeight > remainingHeight) { if (childMeasuredHeight > remainingHeight) {
rowOrColumn++; rowOrColumn++;
maxLength = Math.max(maxLength, measureHeight); maxLength = Math.max(maxLength, measureHeight);
measureHeight = childMeasuredHeight; measureHeight = childMeasuredHeight;
remainingWidth = availableHeight - childMeasuredHeight; remainingHeight = availableHeight - childMeasuredHeight;
this._lengths[rowOrColumn] = childMeasuredWidth; this._lengths[isFirst ? rowOrColumn - 1 : rowOrColumn] = childMeasuredWidth;
} }
else { else {
remainingHeight -= childMeasuredHeight; remainingHeight -= childMeasuredHeight;
@ -80,7 +81,7 @@ export class WrapLayout extends common.WrapLayout {
maxLength = Math.max(maxLength, measureWidth); maxLength = Math.max(maxLength, measureWidth);
measureWidth = childMeasuredWidth; measureWidth = childMeasuredWidth;
remainingWidth = availableWidth - childMeasuredWidth; remainingWidth = availableWidth - childMeasuredWidth;
this._lengths[rowOrColumn] = childMeasuredHeight; this._lengths[isFirst ? rowOrColumn - 1 : rowOrColumn] = childMeasuredHeight;
} }
else { else {
remainingWidth -= childMeasuredWidth; remainingWidth -= childMeasuredWidth;
@ -88,7 +89,7 @@ export class WrapLayout extends common.WrapLayout {
} }
} }
if (this._lengths.length <= rowOrColumn) { if (isFirst) {
this._lengths[rowOrColumn] = isVertical ? childMeasuredWidth : childMeasuredHeight; this._lengths[rowOrColumn] = isVertical ? childMeasuredWidth : childMeasuredHeight;
} }
else { else {
@ -151,35 +152,41 @@ export class WrapLayout extends common.WrapLayout {
if (isVertical) { if (isVertical) {
childWidth = length; childWidth = length;
childHeight = this.itemHeight > 0 ? this.itemHeight * density : childHeight; childHeight = this.itemHeight > 0 ? this.itemHeight * density : childHeight;
let isFirst = childTop === this.paddingTop * density;
if (childTop + childHeight > childrenLength) { if (childTop + childHeight > childrenLength) {
// Move to top. // Move to top.
childTop = this.paddingTop * density; childTop = this.paddingTop * density;
if (!isFirst) {
// Move to right with current column width. // Move to right with current column width.
childLeft += length; childLeft += length;
}
// Move to next column. // Move to next column.
rowOrColumn++; rowOrColumn++;
// Take current column width. // Take respective column width.
childWidth = length = this._lengths[rowOrColumn]; childWidth = this._lengths[isFirst ? rowOrColumn - 1 : rowOrColumn];
} }
} }
else { else {
childWidth = this.itemWidth > 0 ? this.itemWidth * density : childWidth; childWidth = this.itemWidth > 0 ? this.itemWidth * density : childWidth;
childHeight = length; childHeight = length;
let isFirst = childLeft === this.paddingLeft * density;
if (childLeft + childWidth > childrenLength) { if (childLeft + childWidth > childrenLength) {
// Move to left. // Move to left.
childLeft = this.paddingLeft * density; childLeft = this.paddingLeft * density;
// Move to bottom with current row height. if (!isFirst) {
childTop += length; // Move to bottom with current row height.
childTop += length;
}
// Move to next column. // Move to next row.
rowOrColumn++; rowOrColumn++;
// Take current row height. // Take respective row height.
childHeight = length = this._lengths[rowOrColumn]; childHeight = this._lengths[isFirst ? rowOrColumn - 1 : rowOrColumn];
} }
} }