import { View as ViewDefinition, Point, Size } from "ui/core/view"; import { Color } from "color"; import { Animation, AnimationPromise } from "ui/animation"; import { Source } from "utils/debug"; import { Background } from "ui/styling/background"; import { ViewBase, getEventOrGestureName, Observable, EventData, Style, Property, InheritedProperty, CssProperty, ShorthandProperty, InheritedCssProperty, gestureFromString, isIOS, traceEnabled, traceWrite, traceCategories, traceNotifyEvent, printUnregisteredProperties, makeParser, makeValidator } from "./view-base"; import { observe as gestureObserve, GesturesObserver, GestureTypes, GestureEventData } from "ui/gestures"; import { Font, parseFont, FontStyle, FontWeight } from "ui/styling/font"; import { fontSizeConverter } from "../styling/converters"; // TODO: Remove this and start using string as source (for android). import { fromFileOrResource, fromBase64, fromUrl } from "image-source"; import { isDataURI, isFileOrResourcePath, layout } from "utils/utils"; export { layout }; export * from "./view-base"; export { GestureTypes, GesturesObserver, GestureEventData, Animation, AnimationPromise, Background, Font, Color } // registerSpecialProperty("class", (instance: ViewDefinition, propertyValue: string) => { // instance.className = propertyValue; // }); // registerSpecialProperty("text", (instance, propertyValue) => { // instance.set("text", propertyValue); // }); Style.prototype.effectiveMinWidth = 0; Style.prototype.effectiveMinHeight = 0; Style.prototype.effectiveWidth = 0; Style.prototype.effectiveHeight = 0; Style.prototype.effectiveMarginTop = 0; Style.prototype.effectiveMarginRight = 0; Style.prototype.effectiveMarginBottom = 0; Style.prototype.effectiveMarginLeft = 0; Style.prototype.effectivePaddingTop = 0; Style.prototype.effectivePaddingRight = 0; Style.prototype.effectivePaddingBottom = 0; Style.prototype.effectivePaddingLeft = 0; Style.prototype.effectiveBorderTopWidth = 0; Style.prototype.effectiveBorderRightWidth = 0; Style.prototype.effectiveBorderBottomWidth = 0; Style.prototype.effectiveBorderLeftWidth = 0; export function PseudoClassHandler(...pseudoClasses: string[]): MethodDecorator { let stateEventNames = pseudoClasses.map(s => ":" + s); let listeners = Symbol("listeners"); return (target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor) => { function update(change: number) { let prev = this[listeners] || 0; let next = prev + change; if (prev <= 0 && next > 0) { this[propertyKey](true); } else if (prev > 0 && next <= 0) { this[propertyKey](false); } } stateEventNames.forEach(s => target[s] = update); } } let viewIdCounter = 0; export abstract class ViewCommon extends ViewBase implements ViewDefinition { // Dynamic properties. left: Length; top: Length; effectiveLeft: number; effectiveTop: number; dock: "left" | "top" | "right" | "bottom"; row: number; col: number; rowSpan: number; colSpan: number; public static loadedEvent = "loaded"; public static unloadedEvent = "unloaded"; private _measuredWidth: number; private _measuredHeight: number; _currentWidthMeasureSpec: number; _currentHeightMeasureSpec: number; private _oldLeft: number; private _oldTop: number; private _oldRight: number; private _oldBottom: number; private _isLayoutValid: boolean; private _cssType: string; private _updatingInheritedProperties: boolean; public _domId: number; public _isAddedToNativeVisualTree: boolean; public _gestureObservers = {}; // public parent: ViewCommon; constructor() { super(); this._domId = viewIdCounter++; this._goToVisualState("normal"); } observe(type: GestureTypes, callback: (args: GestureEventData) => void, thisArg?: any): void { if (!this._gestureObservers[type]) { this._gestureObservers[type] = []; } this._gestureObservers[type].push(gestureObserve(this, type, callback, thisArg)); } public getGestureObservers(type: GestureTypes): Array { return this._gestureObservers[type]; } public addEventListener(arg: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any) { if (typeof arg === "string") { arg = getEventOrGestureName(arg); let gesture = gestureFromString(arg); if (gesture && !this._isEvent(arg)) { this.observe(gesture, callback, thisArg); } else { let events = (arg).split(","); if (events.length > 0) { for (let i = 0; i < events.length; i++) { let evt = events[i].trim(); let gst = gestureFromString(evt); if (gst && !this._isEvent(arg)) { this.observe(gst, callback, thisArg); } else { super.addEventListener(evt, callback, thisArg); } } } else { super.addEventListener(arg, callback, thisArg); } } } else if (typeof arg === "number") { this.observe(arg, callback, thisArg); } } public removeEventListener(arg: string | GestureTypes, callback?: any, thisArg?: any) { if (typeof arg === "string") { let gesture = gestureFromString(arg); if (gesture && !this._isEvent(arg)) { this._disconnectGestureObservers(gesture); } else { let events = arg.split(","); if (events.length > 0) { for (let i = 0; i < events.length; i++) { let evt = events[i].trim(); let gst = gestureFromString(evt); if (gst && !this._isEvent(arg)) { this._disconnectGestureObservers(gst); } else { super.removeEventListener(evt, callback, thisArg); } } } else { super.removeEventListener(arg, callback, thisArg); } } } else if (typeof arg === "number") { this._disconnectGestureObservers(arg); } } private _isEvent(name: string): boolean { return this.constructor && `${name}Event` in this.constructor; } private _disconnectGestureObservers(type: GestureTypes): void { let observers = this.getGestureObservers(type); if (observers) { for (let i = 0; i < observers.length; i++) { observers[i].disconnect(); } } } // START Style property shortcuts get borderColor(): string | Color { return this.style.borderColor; } set borderColor(value: string | Color) { this.style.borderColor = value; } get borderTopColor(): Color { return this.style.borderTopColor; } set borderTopColor(value: Color) { this.style.borderTopColor = value; } get borderRightColor(): Color { return this.style.borderRightColor; } set borderRightColor(value: Color) { this.style.borderRightColor = value; } get borderBottomColor(): Color { return this.style.borderBottomColor; } set borderBottomColor(value: Color) { this.style.borderBottomColor = value; } get borderLeftColor(): Color { return this.style.borderLeftColor; } set borderLeftColor(value: Color) { this.style.borderLeftColor = value; } get borderWidth(): string | number { return this.style.borderWidth; } set borderWidth(value: string | number) { this.style.borderWidth = value; } get borderTopWidth(): Length { return this.style.borderTopWidth; } set borderTopWidth(value: Length) { this.style.borderTopWidth = value; } get borderRightWidth(): Length { return this.style.borderRightWidth; } set borderRightWidth(value: Length) { this.style.borderRightWidth = value; } get borderBottomWidth(): Length { return this.style.borderBottomWidth; } set borderBottomWidth(value: Length) { this.style.borderBottomWidth = value; } get borderLeftWidth(): Length { return this.style.borderLeftWidth; } set borderLeftWidth(value: Length) { this.style.borderLeftWidth = value; } get borderRadius(): string | number { return this.style.borderRadius; } set borderRadius(value: string | number) { this.style.borderRadius = value; } get borderTopLeftRadius(): number { return this.style.borderTopLeftRadius; } set borderTopLeftRadius(value: number) { this.style.borderTopLeftRadius = value; } get borderTopRightRadius(): number { return this.style.borderTopRightRadius; } set borderTopRightRadius(value: number) { this.style.borderTopRightRadius = value; } get borderBottomRightRadius(): number { return this.style.borderBottomRightRadius; } set borderBottomRightRadius(value: number) { this.style.borderBottomRightRadius = value; } get borderBottomLeftRadius(): number { return this.style.borderBottomLeftRadius; } set borderBottomLeftRadius(value: number) { this.style.borderBottomLeftRadius = value; } get color(): Color { return this.style.color; } set color(value: Color) { this.style.color = value; } get backgroundColor(): Color { return this.style.backgroundColor; } set backgroundColor(value: Color) { this.style.backgroundColor = value; } get backgroundImage(): string { return this.style.backgroundImage; } set backgroundImage(value: string) { this.style.backgroundImage = value; } get minWidth(): Length { return this.style.minWidth; } set minWidth(value: Length) { this.style.minWidth = value; } get minHeight(): Length { return this.style.minHeight; } set minHeight(value: Length) { this.style.minHeight = value; } get width(): PercentLength { return this.style.width; } set width(value: PercentLength) { this.style.width = value; } get height(): PercentLength { return this.style.height; } set height(value: PercentLength) { this.style.height = value; } get margin(): string { return this.style.margin; } set margin(value: string) { this.style.margin = value; } get marginLeft(): PercentLength { return this.style.marginLeft; } set marginLeft(value: PercentLength) { this.style.marginLeft = value; } get marginTop(): PercentLength { return this.style.marginTop; } set marginTop(value: PercentLength) { this.style.marginTop = value; } get marginRight(): PercentLength { return this.style.marginRight; } set marginRight(value: PercentLength) { this.style.marginRight = value; } get marginBottom(): PercentLength { return this.style.marginBottom; } set marginBottom(value: PercentLength) { this.style.marginBottom = value; } get horizontalAlignment(): HorizontalAlignment { return this.style.horizontalAlignment; } set horizontalAlignment(value: HorizontalAlignment) { this.style.horizontalAlignment = value; } get verticalAlignment(): VerticalAlignment { return this.style.verticalAlignment; } set verticalAlignment(value: VerticalAlignment) { this.style.verticalAlignment = value; } get visibility(): Visibility { return this.style.visibility; } set visibility(value: Visibility) { this.style.visibility = value; } get opacity(): number { return this.style.opacity; } set opacity(value: number) { this.style.opacity = value; } get rotate(): number { return this.style.rotate; } set rotate(value: number) { this.style.rotate = value; } get translateX(): number { return this.style.translateX; } set translateX(value: number) { this.style.translateX = value; } get translateY(): number { return this.style.translateY; } set translateY(value: number) { this.style.translateY = value; } get scaleX(): number { return this.style.scaleX; } set scaleX(value: number) { this.style.scaleX = value; } get scaleY(): number { return this.style.scaleY; } set scaleY(value: number) { this.style.scaleY = value; } //END Style property shortcuts public automationText: string; public originX: number; public originY: number; public isEnabled: boolean; public isUserInteractionEnabled: boolean; get isLayoutValid(): boolean { return this._isLayoutValid; } get cssType(): string { if (!this._cssType) { this._cssType = this.typeName.toLowerCase(); } return this._cssType; } get isLayoutRequired(): boolean { return true; } // public _onPropertyChanged(property: Property, oldValue: any, newValue: any) { // super._onPropertyChanged(property, oldValue, newValue); // if (this._childrenCount > 0) { // let shouldUpdateInheritableProps = (property.inheritable && !(property instanceof styling.Property)); // if (shouldUpdateInheritableProps) { // this._updatingInheritedProperties = true; // this._eachChildView((child) => { // child._setValue(property, this._getValue(property), ValueSource.Inherited); // return true; // }); // this._updatingInheritedProperties = false; // } // } // this._checkMetadataOnPropertyChanged(property.metadata); // } // public _isInheritedChange() { // if (this._updatingInheritedProperties) { // return true; // } // let parentView: ViewDefinition; // parentView = (this.parent); // while (parentView) { // if (parentView._updatingInheritedProperties) { // return true; // } // parentView = (parentView.parent); // } // return false; // } // public _checkMetadataOnPropertyChanged(metadata: doPropertyMetadata) { // if (metadata.affectsLayout) { // this.requestLayout(); // } // if (metadata.affectsStyle) { // this.style._resetCssValues(); // this._applyStyleFromScope(); // this._eachChildView((v) => { // v._checkMetadataOnPropertyChanged(metadata); // return true; // }); // } // } public measure(widthMeasureSpec: number, heightMeasureSpec: number): void { this._setCurrentMeasureSpecs(widthMeasureSpec, heightMeasureSpec); } public layout(left: number, top: number, right: number, bottom: number): void { this._setCurrentLayoutBounds(left, top, right, bottom); } public getMeasuredWidth(): number { return this._measuredWidth & layout.MEASURED_SIZE_MASK || 0; } public getMeasuredHeight(): number { return this._measuredHeight & layout.MEASURED_SIZE_MASK || 0; } public getMeasuredState(): number { return (this._measuredWidth & layout.MEASURED_STATE_MASK) | ((this._measuredHeight >> layout.MEASURED_HEIGHT_STATE_SHIFT) & (layout.MEASURED_STATE_MASK >> layout.MEASURED_HEIGHT_STATE_SHIFT)); } public setMeasuredDimension(measuredWidth: number, measuredHeight: number): void { this._measuredWidth = measuredWidth; this._measuredHeight = measuredHeight; if (traceEnabled) { traceWrite(this + " :setMeasuredDimension: " + measuredWidth + ", " + measuredHeight, traceCategories.Layout); } } public requestLayout(): void { this._isLayoutValid = false; } public abstract onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void; public abstract onLayout(left: number, top: number, right: number, bottom: number): void; public abstract layoutNativeView(left: number, top: number, right: number, bottom: number): void; public static resolveSizeAndState(size: number, specSize: number, specMode: number, childMeasuredState: number): number { let result = size; switch (specMode) { case layout.UNSPECIFIED: result = size; break; case layout.AT_MOST: if (specSize < size) { result = Math.round(specSize + 0.499) | layout.MEASURED_STATE_TOO_SMALL; } break; case layout.EXACTLY: result = specSize; break; } return Math.round(result + 0.499) | (childMeasuredState & layout.MEASURED_STATE_MASK); } public static combineMeasuredStates(curState: number, newState): number { return curState | newState; } public static layoutChild(parent: ViewDefinition, child: ViewDefinition, left: number, top: number, right: number, bottom: number): void { if (!child || child.isCollapsed) { return; } let childStyle = child.style; let childTop: number; let childLeft: number; let childWidth = child.getMeasuredWidth(); let childHeight = child.getMeasuredHeight(); let effectiveMarginTop = childStyle.effectiveMarginTop; let effectiveMarginBottom = childStyle.effectiveMarginBottom; let vAlignment: VerticalAlignment; if (childStyle.effectiveHeight >= 0 && childStyle.verticalAlignment === VerticalAlignment.STRETCH) { vAlignment = VerticalAlignment.MIDDLE; } else { vAlignment = childStyle.verticalAlignment; } let marginTop = childStyle.marginTop; let marginBottom = childStyle.marginBottom; let marginLeft = childStyle.marginLeft; let marginRight = childStyle.marginRight; switch (vAlignment) { case VerticalAlignment.TOP: childTop = top + effectiveMarginTop; break; case VerticalAlignment.MIDDLE: childTop = top + (bottom - top - childHeight + (effectiveMarginTop - effectiveMarginBottom)) / 2; break; case VerticalAlignment.BOTTOM: childTop = bottom - childHeight - effectiveMarginBottom; break; case VerticalAlignment.STRETCH: default: childTop = top + effectiveMarginTop; childHeight = bottom - top - (effectiveMarginTop + effectiveMarginBottom); break; } let effectiveMarginLeft = childStyle.effectiveMarginLeft; let effectiveMarginRight = childStyle.effectiveMarginRight; let hAlignment: HorizontalAlignment; if (childStyle.effectiveWidth >= 0 && childStyle.horizontalAlignment === HorizontalAlignment.STRETCH) { hAlignment = HorizontalAlignment.CENTER; } else { hAlignment = childStyle.horizontalAlignment; } switch (hAlignment) { case HorizontalAlignment.LEFT: childLeft = left + effectiveMarginLeft; break; case HorizontalAlignment.CENTER: childLeft = left + (right - left - childWidth + (effectiveMarginLeft - effectiveMarginRight)) / 2; break; case HorizontalAlignment.RIGHT: childLeft = right - childWidth - effectiveMarginRight; break; case HorizontalAlignment.STRETCH: default: childLeft = left + effectiveMarginLeft; childWidth = right - left - (effectiveMarginLeft + effectiveMarginRight); break; } let childRight = Math.round(childLeft + childWidth); let childBottom = Math.round(childTop + childHeight); childLeft = Math.round(childLeft); childTop = Math.round(childTop); if (traceEnabled) { traceWrite(child.parent + " :layoutChild: " + child + " " + childLeft + ", " + childTop + ", " + childRight + ", " + childBottom, traceCategories.Layout); } child.layout(childLeft, childTop, childRight, childBottom); } public static measureChild(parent: ViewCommon, child: ViewCommon, widthMeasureSpec: number, heightMeasureSpec: number): { measuredWidth: number; measuredHeight: number } { let measureWidth = 0; let measureHeight = 0; if (child && !child.isCollapsed) { let density = layout.getDisplayDensity(); let width = layout.getMeasureSpecSize(widthMeasureSpec); let widthMode = layout.getMeasureSpecMode(widthMeasureSpec); let height = layout.getMeasureSpecSize(heightMeasureSpec); let heightMode = layout.getMeasureSpecMode(heightMeasureSpec); let parentWidthMeasureSpec = parent._currentWidthMeasureSpec; updateChildLayoutParams(child, parent, density); let style = child.style; let horizontalMargins = style.effectiveMarginLeft + style.effectiveMarginRight; let verticalMargins = style.effectiveMarginTop + style.effectiveMarginRight; let childWidthMeasureSpec = ViewCommon.getMeasureSpec(width, widthMode, horizontalMargins, style.effectiveWidth, style.horizontalAlignment === HorizontalAlignment.STRETCH); let childHeightMeasureSpec = ViewCommon.getMeasureSpec(height, heightMode, verticalMargins, style.effectiveHeight, style.verticalAlignment === VerticalAlignment.STRETCH); if (traceEnabled) { traceWrite(child.parent + " :measureChild: " + child + " " + layout.measureSpecToString(childWidthMeasureSpec) + ", " + layout.measureSpecToString(childHeightMeasureSpec), traceCategories.Layout); } child.measure(childWidthMeasureSpec, childHeightMeasureSpec); measureWidth = Math.round(child.getMeasuredWidth() + horizontalMargins); measureHeight = Math.round(child.getMeasuredHeight() + verticalMargins); } return { measuredWidth: measureWidth, measuredHeight: measureHeight }; } private static getMeasureSpec(parentLength: number, parentSpecMode: number, margins: number, childLength: number, stretched: boolean): number { let resultSize: number; let resultMode: number; // We want a specific size... let be it. if (childLength >= 0) { // If mode !== UNSPECIFIED we take the smaller of parentLength and childLength // Otherwise we will need to clip the view but this is not possible in all Android API levels. resultSize = parentSpecMode === layout.UNSPECIFIED ? childLength : Math.min(parentLength, childLength); resultMode = layout.EXACTLY; } else { switch (parentSpecMode) { // Parent has imposed an exact size on us case layout.EXACTLY: resultSize = Math.max(0, parentLength - margins); // if stretched - nativeView wants to be our size. So be it. // else - nativeView wants to determine its own size. It can't be bigger than us. resultMode = stretched ? layout.EXACTLY : layout.AT_MOST; break; // Parent has imposed a maximum size on us case layout.AT_MOST: resultSize = Math.max(0, parentLength - margins); resultMode = layout.AT_MOST; break; // Equivalent to measure with Infinity. case layout.UNSPECIFIED: resultSize = 0; resultMode = layout.UNSPECIFIED; break; } } return layout.makeMeasureSpec(resultSize, resultMode); } _setCurrentMeasureSpecs(widthMeasureSpec: number, heightMeasureSpec: number): boolean { let changed: boolean = this._currentWidthMeasureSpec !== widthMeasureSpec || this._currentHeightMeasureSpec !== heightMeasureSpec; this._currentWidthMeasureSpec = widthMeasureSpec; this._currentHeightMeasureSpec = heightMeasureSpec; return changed; } _getCurrentLayoutBounds(): { left: number; top: number; right: number; bottom: number } { return { left: this._oldLeft, top: this._oldTop, right: this._oldRight, bottom: this._oldBottom } } /** * Returns two booleans - the first if "boundsChanged" the second is "sizeChanged". */ _setCurrentLayoutBounds(left: number, top: number, right: number, bottom: number): { boundsChanged: boolean, sizeChanged: boolean } { this._isLayoutValid = true; let boundsChanged: boolean = this._oldLeft !== left || this._oldTop !== top || this._oldRight !== right || this._oldBottom !== bottom; let sizeChanged: boolean = (this._oldRight - this._oldLeft !== right - left) || (this._oldBottom - this._oldTop !== bottom - top); this._oldLeft = left; this._oldTop = top; this._oldRight = right; this._oldBottom = bottom; return { boundsChanged, sizeChanged }; } // TODO: We need to implement some kind of build step that includes these members only when building for Android //@android public _context: android.content.Context; public _onAttached(context: android.content.Context) { // } public _onDetached(force?: boolean) { // } public _createUI() { // } public _onContextChanged() { // } //@endandroid // TODO: We need to implement some kind of build step that includes these members only when building for iOS //@endios public eachChild(callback: (child: ViewBase) => boolean): void { this._eachChildView(callback); } public _eachChildView(callback: (view: ViewDefinition) => boolean) { // } _childIndexToNativeChildIndex(index?: number): number { return index; } _getNativeViewsCount(): number { return this._isAddedToNativeVisualTree ? 1 : 0; } _eachLayoutView(callback: (View) => void): void { return callback(this); } _addToSuperview(superview: any, index?: number): boolean { // IOS specific return false; } _removeFromSuperview(): void { // IOS specific } /** * Method is intended to be overridden by inheritors and used as "protected" */ public _addViewCore(view: ViewBase, atIndex?: number) { if (view instanceof ViewCommon) { if (!view._isAddedToNativeVisualTree) { let nativeIndex = this._childIndexToNativeChildIndex(atIndex); view._isAddedToNativeVisualTree = this._addViewToNativeVisualTree(view, nativeIndex); } } super._addViewCore(view, atIndex); } /** * Method is intended to be overridden by inheritors and used as "protected" */ public _removeViewCore(view: ViewBase) { if (view instanceof ViewCommon) { // TODO: Change type from ViewCommon to ViewBase. Probably this // method will need to go to ViewBase class. // Remove the view from the native visual scene first this._removeViewFromNativeVisualTree(view); } super._removeViewCore(view); } // public unsetInheritedProperties(): void { // // this._setValue(ProxyObject.bindingContextProperty, undefined, ValueSource.Inherited); // // this._eachSetProperty((property) => { // // if (!(property instanceof styling.Property) && property.inheritable) { // // this._resetValue(property, ValueSource.Inherited); // // } // // return true; // // }); // } /** * Method is intended to be overridden by inheritors and used as "protected". */ public _addViewToNativeVisualTree(view: ViewDefinition, atIndex?: number): boolean { if (view._isAddedToNativeVisualTree) { throw new Error("Child already added to the native visual tree."); } return true; } /** * Method is intended to be overridden by inheritors and used as "protected" */ public _removeViewFromNativeVisualTree(view: ViewDefinition) { view._isAddedToNativeVisualTree = false; } public _updateLayout() { // needed for iOS. } get _nativeView(): any { return undefined; } public focus(): boolean { return undefined; } public getLocationInWindow(): Point { return undefined; } public getLocationOnScreen(): Point { return undefined; } public getLocationRelativeTo(otherView: ViewDefinition): Point { return undefined; } public getActualSize(): Size { let currentBounds = this._getCurrentLayoutBounds(); if (!currentBounds) { return undefined; } return { width: layout.toDeviceIndependentPixels(currentBounds.right - currentBounds.left), height: layout.toDeviceIndependentPixels(currentBounds.bottom - currentBounds.top), } } public animate(animation: any): AnimationPromise { return this.createAnimation(animation).play(); } public createAnimation(animation: any): any { animation.target = this; return new Animation([animation]); } public toString(): string { let str = this.typeName; if (this.id) { str += `<${this.id}>`; } else { str += `(${this._domId})`; } let source = Source.get(this); if (source) { str += `@${source};`; } return str; } public _setNativeViewFrame(nativeView: any, frame: any) { // } // public _onStylePropertyChanged(property: Property): void { // // // } // protected _canApplyNativeProperty(): boolean { // // Check for a valid _nativeView instance // return !!this._nativeView; // } public _getValue(): never { throw new Error("The View._setValue is obsolete. There is a new property system.") } public _setValue(): never { throw new Error("The View._setValue is obsolete. There is a new property system.") } } function updateChildLayoutParams(child: ViewCommon, parent: ViewCommon, density: number): void { let style = child.style; let parentWidthMeasureSpec = parent._currentWidthMeasureSpec; let parentWidthMeasureSize = layout.getMeasureSpecSize(parentWidthMeasureSpec); let parentWidthMeasureMode = layout.getMeasureSpecMode(parentWidthMeasureSpec); let parentAvailableWidth = parentWidthMeasureMode === layout.UNSPECIFIED ? -1 : parentWidthMeasureSize; 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 = PercentLength.toDevicePixels(style.height, parentAvailableHeight); style.effectiveMarginTop = PercentLength.toDevicePixels(style.marginTop, parentAvailableHeight); style.effectiveMarginBottom = PercentLength.toDevicePixels(style.marginBottom, parentAvailableHeight); } 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; } 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; } export namespace PercentLength { export function parse(value: string | Length): PercentLength { if (typeof value === "string") { let type: "%" | "dip" | "px"; let numberValue = 0; let stringValue = value.trim(); let percentIndex = stringValue.indexOf("%"); if (percentIndex !== -1) { type = "%"; // if only % or % is not last we treat it as invalid value. if (percentIndex !== (stringValue.length - 1) || percentIndex === 0) { numberValue = Number.NaN; } else { numberValue = parseFloat(stringValue.substring(0, stringValue.length - 1).trim()) / 100; } } else { if (stringValue.indexOf("px") !== -1) { type = "px"; stringValue = stringValue.replace("px", "").trim(); } else { type = "dip"; } numberValue = parseFloat(stringValue); } if (isNaN(numberValue) || !isFinite(numberValue)) { throw new Error(`Invalid value: ${value}`); } return { value: numberValue, unit: type } } else { return value; } } 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 (value == "auto") { return "auto"; } else if (typeof value === "string") { let type: "dip" | "px"; let numberValue = 0; let stringValue = value.trim(); if (stringValue.indexOf("px") !== -1) { type = "px"; stringValue = stringValue.replace("px", "").trim(); } else { type = "dip"; } numberValue = parseFloat(stringValue); if (isNaN(numberValue) || !isFinite(numberValue)) { throw new Error(`Invalid value: ${value}`); } return { value: numberValue, unit: type } } else { return value; } } export const equals: { (a: Length, b: Length): boolean } = equalsCommon; export const toDevicePixels: { (length: Length, auto: number): number } = toDevicePixelsCommon; } 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; } } export function booleanConverter(v: string): boolean { let lowercase = (v + '').toLowerCase(); if (lowercase === "true") { return true; } else if (lowercase === "false") { return false; } throw new Error(`Invalid boolean: ${v}`); } export const automationTextProperty = new Property({ name: "automationText" }); automationTextProperty.register(ViewCommon); export const originXProperty = new Property({ name: "originX", defaultValue: 0.5, valueConverter: (v) => parseFloat(v) }); originXProperty.register(ViewCommon); export const originYProperty = new Property({ name: "originY", defaultValue: 0.5, valueConverter: (v) => parseFloat(v) }); originYProperty.register(ViewCommon); export const isEnabledProperty = new Property({ name: "isEnabled", defaultValue: true, valueConverter: booleanConverter }); isEnabledProperty.register(ViewCommon); export const isUserInteractionEnabledProperty = new Property({ name: "isUserInteractionEnabled", defaultValue: true, valueConverter: booleanConverter }); isUserInteractionEnabledProperty.register(ViewCommon); export const zeroLength: Length = { value: 0, unit: "px" }; export const minWidthProperty = new CssProperty({ name: "minWidth", cssName: "min-width", defaultValue: zeroLength, affectsLayout: isIOS, equalityComparer: Length.equals, valueChanged: (target, oldValue, 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: Length.equals, valueChanged: (target, oldValue, 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: Length.equals, valueConverter: PercentLength.parse }); widthProperty.register(Style); 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