From 6cfbaae94760ccaf80d4e42e42eb66096eeb64cd Mon Sep 17 00:00:00 2001 From: Panayot Cankov Date: Mon, 19 Dec 2016 15:53:01 +0200 Subject: [PATCH] Add 'auto' and number to the PercentLength and Length types --- tns-core-modules/lib.core.d.ts | 2 +- tns-core-modules/ui/core/view-common.ts | 166 +++-- tns-core-modules/ui/core/view.android.ts | 650 ++++-------------- tns-core-modules/ui/core/view.d.ts | 4 +- .../ui/list-view/list-view-common.ts | 18 +- .../ui/styling/background-common.ts | 7 +- 6 files changed, 222 insertions(+), 625 deletions(-) diff --git a/tns-core-modules/lib.core.d.ts b/tns-core-modules/lib.core.d.ts index 43918b42f..f3b1d662d 100644 --- a/tns-core-modules/lib.core.d.ts +++ b/tns-core-modules/lib.core.d.ts @@ -166,7 +166,7 @@ interface ObjectConstructor { * @param p The property name. * @param attributes Descriptor for the property. It can be for a data property or an accessor property. */ - defineProperty(o: any, p: string, attributes: PropertyDescriptor): any; + defineProperty(o: any, p: string | symbol, attributes: PropertyDescriptor): any; /** * Adds one or more properties to an object, and/or modifies attributes of existing properties. diff --git a/tns-core-modules/ui/core/view-common.ts b/tns-core-modules/ui/core/view-common.ts index 7e7d3cc48..ff6846ea5 100644 --- a/tns-core-modules/ui/core/view-common.ts +++ b/tns-core-modules/ui/core/view-common.ts @@ -960,31 +960,6 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition { } } -export function getLengthEffectiveValue(param: Length): number { - switch (param.unit) { - case "px": - return Math.round(param.value); - - default: - case "dip": - return Math.round(layout.getDisplayDensity() * param.value); - } -} - -function getPercentLengthEffectiveValue(prentAvailableLength: number, param: PercentLength): number { - switch (param.unit) { - case "%": - return Math.round(prentAvailableLength * param.value); - - case "px": - return Math.round(param.value); - - default: - case "dip": - return Math.round(layout.getDisplayDensity() * param.value); - } -} - function updateChildLayoutParams(child: ViewCommon, parent: ViewCommon, density: number): void { let style = child.style; @@ -992,25 +967,64 @@ function updateChildLayoutParams(child: ViewCommon, parent: ViewCommon, density: let parentWidthMeasureSize = layout.getMeasureSpecSize(parentWidthMeasureSpec); let parentWidthMeasureMode = layout.getMeasureSpecMode(parentWidthMeasureSpec); let parentAvailableWidth = parentWidthMeasureMode === layout.UNSPECIFIED ? -1 : parentWidthMeasureSize; - style.effectiveWidth = getPercentLengthEffectiveValue(parentAvailableWidth, style.width); - style.effectiveMarginLeft = getPercentLengthEffectiveValue(parentAvailableWidth, style.marginLeft); - style.effectiveMarginRight = getPercentLengthEffectiveValue(parentAvailableWidth, style.marginRight); + style.effectiveWidth = PercentLength.toDevicePixels(style.width, parentAvailableWidth) + style.effectiveMarginLeft = PercentLength.toDevicePixels(style.marginLeft, parentAvailableWidth); + style.effectiveMarginRight = PercentLength.toDevicePixels(style.marginRight, parentAvailableWidth); let parentHeightMeasureSpec = parent._currentHeightMeasureSpec; let parentHeightMeasureSize = layout.getMeasureSpecSize(parentHeightMeasureSpec); let parentHeightMeasureMode = layout.getMeasureSpecMode(parentHeightMeasureSpec); let parentAvailableHeight = parentHeightMeasureMode === layout.UNSPECIFIED ? -1 : parentHeightMeasureSize; - style.effectiveHeight = getPercentLengthEffectiveValue(parentAvailableHeight, style.height); - style.effectiveMarginTop = getPercentLengthEffectiveValue(parentAvailableHeight, style.marginTop); - style.effectiveMarginBottom = getPercentLengthEffectiveValue(parentAvailableHeight, style.marginBottom); + style.effectiveHeight = PercentLength.toDevicePixels(style.height, parentAvailableHeight); + style.effectiveMarginTop = PercentLength.toDevicePixels(style.marginTop, parentAvailableHeight); + style.effectiveMarginBottom = PercentLength.toDevicePixels(style.marginBottom, parentAvailableHeight); } -interface Length { - readonly unit: "dip" | "px"; - readonly value: number; +function equalsCommon(a: Length, b: Length): boolean; +function equalsCommon(a: PercentLength, b: PercentLength): boolean; +function equalsCommon(a: PercentLength, b: PercentLength): boolean { + if (a == "auto") { + return b == "auto"; + } + if (typeof a === "number") { + if (b == "auto") { + return false; + } + if (typeof b === "number") { + return a == b; + } + return b.unit == "dip" && a == b.value; + } + if (b == "auto") { + return false; + } + if (typeof b === "number") { + return a.unit == "dip" && a.value == b; + } + return a.value == b.value && a.unit == b.unit; } -interface PercentLength { +function toDevicePixelsCommon(length: Length, auto: number): number; +function toDevicePixelsCommon(length: PercentLength, auto: number, parentSize: number): number; +function toDevicePixelsCommon(length: PercentLength, auto: number, parentAvailableWidth?: number): number { + if (length == "auto") { + return auto; + } + if (typeof length === "number") { + return Math.round(layout.getDisplayDensity() * length); + } + switch (length.unit) { + case "px": + return Math.round(length.value); + default: + case "dip": + return Math.round(layout.getDisplayDensity() * length.value); + case "%": + return Math.round(parentAvailableWidth * length.value); + } +} + +export type PercentLength = "auto" | number | { readonly unit: "%" | "dip" | "px"; readonly value: number; } @@ -1053,14 +1067,20 @@ export namespace PercentLength { return value; } } - export function equals(a: PercentLength, b: PercentLength): boolean { - return a.value == b.value && a.unit == b.unit; - } + export const equals: { (a: PercentLength, b: PercentLength): boolean } = equalsCommon; + export const toDevicePixels: { (length: PercentLength, parentAvailableWidth: number): number } = toDevicePixelsCommon; } +export type Length = "auto" | number | { + readonly unit: "dip" | "px"; + readonly value: number; +}; + export namespace Length { export function parse(value: string | Length): Length { - if (typeof value === "string") { + if (value == "auto") { + return "auto"; + } else if (typeof value === "string") { let type: "dip" | "px"; let numberValue = 0; let stringValue = value.trim(); @@ -1085,9 +1105,17 @@ export namespace Length { return value; } } + export const equals: { (a: Length, b: Length): boolean } = equalsCommon; + export const toDevicePixels: { (length: Length, auto: number): number } = toDevicePixelsCommon; +} - export function equals(a: Length, b: Length): boolean { - return a.value == b.value && a.unit == b.unit; +declare module "ui/core/view" { + export namespace Length { + export function toDevicePixels(length: Length, auto: number): number; + } + + export namespace PercentLength { + export function toDevicePixels(length: PercentLength, auto: number, availableParentSize: number): number; } } @@ -1119,32 +1147,28 @@ isUserInteractionEnabledProperty.register(ViewCommon); export const zeroLength: Length = { value: 0, unit: "px" }; -export function lengthComparer(x: Length, y: Length): boolean { - return x.unit === y.unit && x.value === y.value; -} - export const minWidthProperty = new CssProperty({ - name: "minWidth", cssName: "min-width", defaultValue: zeroLength, affectsLayout: isIOS, equalityComparer: lengthComparer, + name: "minWidth", cssName: "min-width", defaultValue: zeroLength, affectsLayout: isIOS, equalityComparer: Length.equals, valueChanged: (target, newValue) => { - target.effectiveMinWidth = getLengthEffectiveValue(newValue); + target.effectiveMinWidth = Length.toDevicePixels(newValue, 0); }, valueConverter: Length.parse }); minWidthProperty.register(Style); export const minHeightProperty = new CssProperty({ - name: "minHeight", cssName: "min-height", defaultValue: zeroLength, affectsLayout: isIOS, equalityComparer: lengthComparer, + name: "minHeight", cssName: "min-height", defaultValue: zeroLength, affectsLayout: isIOS, equalityComparer: Length.equals, valueChanged: (target, newValue) => { - target.effectiveMinHeight = getLengthEffectiveValue(newValue); + target.effectiveMinHeight = Length.toDevicePixels(newValue, 0); }, valueConverter: Length.parse }); minHeightProperty.register(Style); const matchParent: Length = { value: -1, unit: "px" }; -export const widthProperty = new CssProperty({ name: "width", cssName: "width", defaultValue: matchParent, affectsLayout: isIOS, equalityComparer: lengthComparer, valueConverter: PercentLength.parse }); +export const widthProperty = new CssProperty({ name: "width", cssName: "width", defaultValue: matchParent, affectsLayout: isIOS, equalityComparer: Length.equals, valueConverter: PercentLength.parse }); widthProperty.register(Style); -export const heightProperty = new CssProperty({ name: "height", cssName: "height", defaultValue: matchParent, affectsLayout: isIOS, equalityComparer: lengthComparer, valueConverter: PercentLength.parse }); +export const heightProperty = new CssProperty({ name: "height", cssName: "height", defaultValue: matchParent, affectsLayout: isIOS, equalityComparer: Length.equals, valueConverter: PercentLength.parse }); heightProperty.register(Style); const marginProperty = new ShorthandProperty