mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
Merge pull request #1362 from NativeScript/hhristov/wrap-layout-fix
Fixed wrap-layout
This commit is contained in:
@ -44,12 +44,9 @@ export class WrapLayoutTest extends testModule.UITest<wrapLayoutModule.WrapLayou
|
|||||||
wrapLayout.width = layoutHelper.dp(200);
|
wrapLayout.width = layoutHelper.dp(200);
|
||||||
wrapLayout.height = layoutHelper.dp(200);
|
wrapLayout.height = layoutHelper.dp(200);
|
||||||
|
|
||||||
var label;
|
for (let i = 0; i < 2; i++) {
|
||||||
var i;
|
let label = new Label();
|
||||||
for (i = 0; i < 2; i++) {
|
|
||||||
label = new Label();
|
|
||||||
label.text = "" + i;
|
label.text = "" + i;
|
||||||
label.id = "" + i;
|
|
||||||
|
|
||||||
label.width = layoutHelper.dp(100);
|
label.width = layoutHelper.dp(100);
|
||||||
label.height = layoutHelper.dp(100);
|
label.height = layoutHelper.dp(100);
|
||||||
@ -59,6 +56,55 @@ export class WrapLayoutTest extends testModule.UITest<wrapLayoutModule.WrapLayou
|
|||||||
return wrapLayout;
|
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() {
|
public testHorizontalOrientation() {
|
||||||
|
|
||||||
this.testView.orientation = enums.Orientation.horizontal;
|
this.testView.orientation = enums.Orientation.horizontal;
|
||||||
|
@ -29,64 +29,76 @@ export class WrapLayout extends common.WrapLayout {
|
|||||||
var measureWidth = 0;
|
var measureWidth = 0;
|
||||||
var measureHeight = 0;
|
var measureHeight = 0;
|
||||||
|
|
||||||
var width = utils.layout.getMeasureSpecSize(widthMeasureSpec);
|
|
||||||
var widthMode = utils.layout.getMeasureSpecMode(widthMeasureSpec);
|
var widthMode = utils.layout.getMeasureSpecMode(widthMeasureSpec);
|
||||||
|
|
||||||
var height = utils.layout.getMeasureSpecSize(heightMeasureSpec);
|
|
||||||
var heightMode = utils.layout.getMeasureSpecMode(heightMeasureSpec);
|
var heightMode = utils.layout.getMeasureSpecMode(heightMeasureSpec);
|
||||||
|
|
||||||
var density = utils.layout.getDisplayDensity();
|
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 horizontalPadding = (this.paddingLeft + this.paddingRight) * density;
|
||||||
var remainingHeight = heightMode === utils.layout.UNSPECIFIED ? Number.MAX_VALUE : height - ((this.paddingTop + this.paddingBottom) * 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;
|
this._lengths.length = 0;
|
||||||
|
|
||||||
var rowOrColumn = 0;
|
var rowOrColumn = 0;
|
||||||
var maxLength = 0;
|
var maxLength = 0;
|
||||||
|
|
||||||
var isVertical = this.orientation === Orientation.vertical;
|
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++) {
|
for (let i = 0, count = this.getChildrenCount(); i < count; i++) {
|
||||||
let child = this.getChildAt(i);
|
let child = this.getChildAt(i);
|
||||||
if (!child._isVisible) {
|
if (!child._isVisible) {
|
||||||
continue;
|
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 (isVertical) {
|
||||||
if (childSize.measuredHeight > remainingHeight) {
|
if (childMeasuredHeight > remainingHeight) {
|
||||||
rowOrColumn++;
|
rowOrColumn++;
|
||||||
maxLength = Math.max(maxLength, measureHeight);
|
maxLength = Math.max(maxLength, measureHeight);
|
||||||
measureHeight = childSize.measuredHeight;
|
measureHeight = childMeasuredHeight;
|
||||||
remainingWidth = height - childSize.measuredHeight;
|
remainingWidth = availableHeight - childMeasuredHeight;
|
||||||
this._lengths[rowOrColumn] = childSize.measuredWidth;
|
this._lengths[rowOrColumn] = childMeasuredWidth;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
remainingHeight -= childSize.measuredHeight;
|
remainingHeight -= childMeasuredHeight;
|
||||||
measureHeight += childSize.measuredHeight;
|
measureHeight += childMeasuredHeight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (childSize.measuredWidth > remainingWidth) {
|
if (childMeasuredWidth > remainingWidth) {
|
||||||
rowOrColumn++;
|
rowOrColumn++;
|
||||||
maxLength = Math.max(maxLength, measureWidth);
|
maxLength = Math.max(maxLength, measureWidth);
|
||||||
measureWidth = childSize.measuredWidth;
|
measureWidth = childMeasuredWidth;
|
||||||
remainingWidth = width - childSize.measuredWidth;
|
remainingWidth = availableWidth - childMeasuredWidth;
|
||||||
this._lengths[rowOrColumn] = childSize.measuredHeight;
|
this._lengths[rowOrColumn] = childMeasuredHeight;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
remainingWidth -= childSize.measuredWidth;
|
remainingWidth -= childMeasuredWidth;
|
||||||
measureWidth += childSize.measuredWidth;
|
measureWidth += childMeasuredWidth;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this._lengths.length <= rowOrColumn) {
|
if (this._lengths.length <= rowOrColumn) {
|
||||||
this._lengths[rowOrColumn] = isVertical ? childSize.measuredWidth: childSize.measuredHeight;
|
this._lengths[rowOrColumn] = isVertical ? childMeasuredWidth : childMeasuredHeight;
|
||||||
}
|
}
|
||||||
else {
|
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;
|
measureWidth += horizontalPadding;
|
||||||
measureHeight += (this.paddingTop + this.paddingBottom) * density;
|
measureHeight += verticalPadding;
|
||||||
|
|
||||||
measureWidth = Math.max(measureWidth, this.minWidth * density);
|
measureWidth = Math.max(measureWidth, this.minWidth * density);
|
||||||
measureHeight = Math.max(measureHeight, this.minHeight * density);
|
measureHeight = Math.max(measureHeight, this.minHeight * density);
|
||||||
|
|
||||||
var widthAndState = View.resolveSizeAndState(measureWidth, width, widthMode, 0);
|
var widthAndState = View.resolveSizeAndState(measureWidth, utils.layout.getMeasureSpecSize(widthMeasureSpec), widthMode, 0);
|
||||||
var heightAndState = View.resolveSizeAndState(measureHeight, height, heightMode, 0);
|
var heightAndState = View.resolveSizeAndState(measureHeight, utils.layout.getMeasureSpecSize(heightMeasureSpec), heightMode, 0);
|
||||||
|
|
||||||
this.setMeasuredDimension(widthAndState, heightAndState);
|
this.setMeasuredDimension(widthAndState, heightAndState);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user