mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Implemented percent support for width, height and margins
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import utils = require("utils/utils");
|
||||
import enums = require("ui/enums");
|
||||
import view = require("ui/core/view");
|
||||
import {Orientation, VerticalAlignment, HorizontalAlignment} from "ui/enums";
|
||||
import {View} from "ui/core/view";
|
||||
import common = require("./stack-layout-common");
|
||||
import {CommonLayoutParams, nativeLayoutParamsProperty} from "ui/styling/style";
|
||||
|
||||
global.moduleMerge(common, exports);
|
||||
|
||||
@@ -10,6 +11,7 @@ export class StackLayout extends common.StackLayout {
|
||||
private _totalLength = 0;
|
||||
|
||||
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
|
||||
StackLayout.adjustChildrenLayoutParams(this, widthMeasureSpec, heightMeasureSpec);
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
var density = utils.layout.getDisplayDensity();
|
||||
|
||||
@@ -22,12 +24,10 @@ export class StackLayout extends common.StackLayout {
|
||||
var height = utils.layout.getMeasureSpecSize(heightMeasureSpec);
|
||||
var heightMode = utils.layout.getMeasureSpecMode(heightMeasureSpec);
|
||||
|
||||
var isVertical = this.orientation === enums.Orientation.vertical;
|
||||
var isVertical = this.orientation === Orientation.vertical;
|
||||
var verticalPadding = (this.paddingTop + this.paddingBottom) * density;
|
||||
var horizontalPadding = (this.paddingLeft + this.paddingRight) * density;
|
||||
|
||||
var count = this.getChildrenCount();
|
||||
|
||||
var measureSpec: number;
|
||||
|
||||
var mode = isVertical ? heightMode : widthMode;
|
||||
@@ -44,32 +44,32 @@ export class StackLayout extends common.StackLayout {
|
||||
|
||||
var childMeasureSpec: number;
|
||||
if (isVertical) {
|
||||
var childWidth = (widthMode === utils.layout.UNSPECIFIED) ? 0 : width - horizontalPadding;
|
||||
let childWidth = (widthMode === utils.layout.UNSPECIFIED) ? 0 : width - horizontalPadding;
|
||||
childWidth = Math.max(0, childWidth);
|
||||
childMeasureSpec = utils.layout.makeMeasureSpec(childWidth, widthMode)
|
||||
}
|
||||
else {
|
||||
var childHeight = (heightMode === utils.layout.UNSPECIFIED) ? 0 : height - verticalPadding;
|
||||
let childHeight = (heightMode === utils.layout.UNSPECIFIED) ? 0 : height - verticalPadding;
|
||||
childHeight = Math.max(0, childHeight);
|
||||
childMeasureSpec = utils.layout.makeMeasureSpec(childHeight, heightMode)
|
||||
}
|
||||
|
||||
var childSize: { measuredWidth: number; measuredHeight: number };
|
||||
for (var i = 0; i < count; i++) {
|
||||
var child = this.getChildAt(i);
|
||||
if (!child || !child._isVisible) {
|
||||
for (let i = 0, count = this.getChildrenCount(); i < count; i++) {
|
||||
let child = this.getChildAt(i);
|
||||
if (!child._isVisible) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isVertical) {
|
||||
childSize = view.View.measureChild(this, child, childMeasureSpec, utils.layout.makeMeasureSpec(remainingLength, measureSpec));
|
||||
childSize = View.measureChild(this, child, childMeasureSpec, utils.layout.makeMeasureSpec(remainingLength, measureSpec));
|
||||
measureWidth = Math.max(measureWidth, childSize.measuredWidth);
|
||||
var viewHeight = childSize.measuredHeight;
|
||||
measureHeight += viewHeight;
|
||||
remainingLength = Math.max(0, remainingLength - viewHeight);
|
||||
}
|
||||
else {
|
||||
childSize = view.View.measureChild(this, child, utils.layout.makeMeasureSpec(remainingLength, measureSpec), childMeasureSpec);
|
||||
childSize = View.measureChild(this, child, utils.layout.makeMeasureSpec(remainingLength, measureSpec), childMeasureSpec);
|
||||
measureHeight = Math.max(measureHeight, childSize.measuredHeight);
|
||||
var viewWidth = childSize.measuredWidth;
|
||||
measureWidth += viewWidth;
|
||||
@@ -85,20 +85,22 @@ export class StackLayout extends common.StackLayout {
|
||||
|
||||
this._totalLength = isVertical ? measureHeight : measureWidth;
|
||||
|
||||
var widthAndState = view.View.resolveSizeAndState(measureWidth, width, widthMode, 0);
|
||||
var heightAndState = view.View.resolveSizeAndState(measureHeight, height, heightMode, 0);
|
||||
var widthAndState = View.resolveSizeAndState(measureWidth, width, widthMode, 0);
|
||||
var heightAndState = View.resolveSizeAndState(measureHeight, height, heightMode, 0);
|
||||
|
||||
this.setMeasuredDimension(widthAndState, heightAndState);
|
||||
}
|
||||
|
||||
public onLayout(left: number, top: number, right: number, bottom: number): void {
|
||||
super.onLayout(left, top, right, bottom);
|
||||
if (this.orientation === enums.Orientation.vertical) {
|
||||
if (this.orientation === Orientation.vertical) {
|
||||
this.layoutVertical(left, top, right, bottom);
|
||||
}
|
||||
else {
|
||||
this.layoutHorizontal(left, top, right, bottom);
|
||||
}
|
||||
|
||||
StackLayout.restoreOriginalParams(this);
|
||||
}
|
||||
|
||||
private layoutVertical(left: number, top: number, right: number, bottom: number): void {
|
||||
@@ -114,30 +116,31 @@ export class StackLayout extends common.StackLayout {
|
||||
var childRight = right - left - paddingRight;
|
||||
|
||||
switch (this.verticalAlignment) {
|
||||
case enums.VerticalAlignment.center || enums.VerticalAlignment.middle:
|
||||
case VerticalAlignment.center || VerticalAlignment.middle:
|
||||
childTop = (bottom - top - this._totalLength) / 2 + paddingTop - paddingBottom;
|
||||
break;
|
||||
|
||||
case enums.VerticalAlignment.bottom:
|
||||
case VerticalAlignment.bottom:
|
||||
childTop = bottom - top - this._totalLength + paddingTop - paddingBottom;
|
||||
break;
|
||||
|
||||
case enums.VerticalAlignment.top:
|
||||
case enums.VerticalAlignment.stretch:
|
||||
case VerticalAlignment.top:
|
||||
case VerticalAlignment.stretch:
|
||||
default:
|
||||
childTop = paddingTop;
|
||||
break;
|
||||
}
|
||||
|
||||
var count = this.getChildrenCount();
|
||||
for (var i = 0; i < count; i++) {
|
||||
var child = this.getChildAt(i);
|
||||
if (!child || !child._isVisible) {
|
||||
for (let i = 0, count = this.getChildrenCount(); i < count; i++) {
|
||||
let child = this.getChildAt(i);
|
||||
if (!child._isVisible) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var childHeight = child.getMeasuredHeight() + (child.marginTop + child.marginBottom) * density;
|
||||
view.View.layoutChild(this, child, childLeft, childTop, childRight, childTop + childHeight);
|
||||
let lp: CommonLayoutParams = child.style._getValue(nativeLayoutParamsProperty);
|
||||
let childHeight = child.getMeasuredHeight() + (lp.topMargin + lp.bottomMargin) * density;
|
||||
|
||||
View.layoutChild(this, child, childLeft, childTop, childRight, childTop + childHeight);
|
||||
childTop += childHeight;
|
||||
}
|
||||
}
|
||||
@@ -155,31 +158,32 @@ export class StackLayout extends common.StackLayout {
|
||||
var childBottom = bottom - top - paddingBottom;
|
||||
|
||||
switch (this.horizontalAlignment) {
|
||||
case enums.HorizontalAlignment.center:
|
||||
case HorizontalAlignment.center:
|
||||
childLeft = (right - left - this._totalLength) / 2 + paddingLeft - paddingRight;
|
||||
break;
|
||||
|
||||
case enums.HorizontalAlignment.right:
|
||||
case HorizontalAlignment.right:
|
||||
childLeft = right - left - this._totalLength + paddingLeft - paddingRight;
|
||||
break;
|
||||
|
||||
case enums.HorizontalAlignment.left:
|
||||
case enums.HorizontalAlignment.stretch:
|
||||
case HorizontalAlignment.left:
|
||||
case HorizontalAlignment.stretch:
|
||||
default:
|
||||
childLeft = paddingLeft;
|
||||
break;
|
||||
}
|
||||
|
||||
var count = this.getChildrenCount();
|
||||
for (var i = 0; i < count; i++) {
|
||||
var child = this.getChildAt(i);
|
||||
if (!child || !child._isVisible) {
|
||||
for (let i = 0, count = this.getChildrenCount(); i < count; i++) {
|
||||
let child = this.getChildAt(i);
|
||||
if (!child._isVisible) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var childWidth = child.getMeasuredWidth() + (child.marginLeft + child.marginRight) * density;;
|
||||
view.View.layoutChild(this, child, childLeft, childTop, childLeft + childWidth, childBottom);
|
||||
let lp: CommonLayoutParams = child.style._getValue(nativeLayoutParamsProperty);
|
||||
let childWidth = child.getMeasuredWidth() + (lp.leftMargin + lp.rightMargin) * density;
|
||||
|
||||
View.layoutChild(this, child, childLeft, childTop, childLeft + childWidth, childBottom);
|
||||
childLeft += childWidth;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user