From c1ca13fb2a84fba4b4187f23ac165e0c54608f71 Mon Sep 17 00:00:00 2001 From: Hristo Hristov Date: Tue, 1 Mar 2016 15:34:18 +0200 Subject: [PATCH 1/2] LayoutParams are no longer overriden with CommonLayoutParams --- tns-core-modules/ui/core/view.android.ts | 96 +++++++++++++------ .../absolute-layout.android.ts | 10 +- .../dock-layout/dock-layout.android.ts | 43 ++++----- .../grid-layout/grid-layout.android.ts | 10 +- .../ui/tab-view/tab-view.android.ts | 2 +- 5 files changed, 95 insertions(+), 66 deletions(-) diff --git a/tns-core-modules/ui/core/view.android.ts b/tns-core-modules/ui/core/view.android.ts index d87698ead9..788fb5ec1d 100644 --- a/tns-core-modules/ui/core/view.android.ts +++ b/tns-core-modules/ui/core/view.android.ts @@ -53,7 +53,7 @@ function onIsUserInteractionEnabledPropertyChanged(data: dependencyObservable.Pr export class View extends viewCommon.View { private _disableUserInteractionListener: android.view.View.OnTouchListener = new android.view.View.OnTouchListener({ - onTouch: function(view: android.view.View, event: android.view.MotionEvent) { + onTouch: function (view: android.view.View, event: android.view.MotionEvent) { return true; } }); @@ -116,7 +116,7 @@ export class View extends viewCommon.View { this._nativeView.setClickable(true); } this._nativeView.setOnTouchListener(new android.view.View.OnTouchListener({ - onTouch: function(view: android.view.View, motionEvent: android.view.MotionEvent) { + onTouch: function (view: android.view.View, motionEvent: android.view.MotionEvent) { var owner = that.get(); if (!owner) { return false; @@ -178,7 +178,7 @@ export class View extends viewCommon.View { if (this._childrenCount > 0) { // Notify each child for the _onAttached event var that = this; - var eachChild = function(child: View): boolean { + var eachChild = function (child: View): boolean { child._onAttached(context); if (!child._isAddedToNativeVisualTree) { // since we have lazy loading of the android widgets, we need to add the native instances at this point. @@ -197,7 +197,7 @@ export class View extends viewCommon.View { if (this._childrenCount > 0) { // Detach children first var that = this; - var eachChild = function(child: View): boolean { + var eachChild = function (child: View): boolean { if (child._isAddedToNativeVisualTree) { that._removeViewFromNativeVisualTree(child); } @@ -238,7 +238,7 @@ export class View extends viewCommon.View { } this._createUI(); // Ensure layout params - if (this._nativeView && !(this._nativeView.getLayoutParams() instanceof org.nativescript.widgets.CommonLayoutParams)) { + if (this._nativeView && !this._nativeView.getLayoutParams()) { this._nativeView.setLayoutParams(new org.nativescript.widgets.CommonLayoutParams()); } @@ -500,35 +500,12 @@ export class ViewStyler implements style.Styler { (view._nativeView).setMinimumHeight(0); } - private static getNativeLayoutParams(nativeView: android.view.View): org.nativescript.widgets.CommonLayoutParams { - var lp = nativeView.getLayoutParams(); - if (!(lp instanceof org.nativescript.widgets.CommonLayoutParams)) { - lp = new org.nativescript.widgets.CommonLayoutParams(); - } - - return lp; - } - private static setNativeLayoutParamsProperty(view: View, params: CommonLayoutParams): void { var nativeView: android.view.View = view._nativeView; - var lp = ViewStyler.getNativeLayoutParams(nativeView); - - lp.widthPercent = params.widthPercent; - lp.heightPercent = params.heightPercent; - - lp.leftMarginPercent = params.leftMarginPercent; - lp.topMarginPercent = params.topMarginPercent; - lp.rightMarginPercent = params.rightMarginPercent; - lp.bottomMarginPercent = params.bottomMarginPercent; - - lp.leftMargin = Math.round(params.leftMargin * utils.layout.getDisplayDensity()); - lp.topMargin = Math.round(params.topMargin * utils.layout.getDisplayDensity()); - lp.rightMargin = Math.round(params.rightMargin * utils.layout.getDisplayDensity()); - lp.bottomMargin = Math.round(params.bottomMargin * utils.layout.getDisplayDensity()); var width = params.width * utils.layout.getDisplayDensity(); var height = params.height * utils.layout.getDisplayDensity(); - + // If width is not specified set it as WRAP_CONTENT if (width < 0) { width = -2; @@ -591,10 +568,69 @@ export class ViewStyler implements style.Styler { throw new Error("Invalid verticalAlignment value: " + params.verticalAlignment); } - lp.gravity = gravity; + var lp: any = nativeView.getLayoutParams(); lp.width = Math.round(width); lp.height = Math.round(height); + if (lp instanceof org.nativescript.widgets.CommonLayoutParams) { + lp.widthPercent = params.widthPercent; + lp.heightPercent = params.heightPercent; + lp.leftMarginPercent = params.leftMarginPercent; + lp.topMarginPercent = params.topMarginPercent; + lp.rightMarginPercent = params.rightMarginPercent; + lp.bottomMarginPercent = params.bottomMarginPercent; + lp.leftMargin = Math.round(params.leftMargin * utils.layout.getDisplayDensity()); + lp.topMargin = Math.round(params.topMargin * utils.layout.getDisplayDensity()); + lp.rightMargin = Math.round(params.rightMargin * utils.layout.getDisplayDensity()); + lp.bottomMargin = Math.round(params.bottomMargin * utils.layout.getDisplayDensity()); + lp.gravity = gravity; + } + else { + if (types.isDefined(lp.widthPercent)) { + lp.widthPercent = params.widthPercent; + } + + if (types.isDefined(lp.heightPercent)) { + lp.heightPercent = params.heightPercent; + } + + if (types.isDefined(lp.leftMarginPercent)) { + lp.leftMarginPercent = params.leftMarginPercent; + } + + if (types.isDefined(lp.topMarginPercent)) { + lp.topMarginPercent = params.topMarginPercent; + } + + if (types.isDefined(lp.rightMarginPercent)) { + lp.rightMarginPercent = params.rightMarginPercent; + } + + if (types.isDefined(lp.bottomMarginPercent)) { + lp.bottomMarginPercent = params.bottomMarginPercent; + } + + if (types.isDefined(lp.leftMargin)) { + lp.leftMargin = Math.round(params.leftMargin * utils.layout.getDisplayDensity()); + } + + if (types.isDefined(lp.topMargin)) { + lp.topMargin = Math.round(params.topMargin * utils.layout.getDisplayDensity()); + } + + if (types.isDefined(lp.rightMargin)) { + lp.rightMargin = Math.round(params.rightMargin * utils.layout.getDisplayDensity()); + } + + if (types.isDefined(lp.bottomMargin)) { + lp.bottomMargin = Math.round(params.bottomMargin * utils.layout.getDisplayDensity()); + } + + if (types.isDefined(lp.gravity)) { + lp.gravity = gravity; + } + } + nativeView.setLayoutParams(lp); } diff --git a/tns-core-modules/ui/layouts/absolute-layout/absolute-layout.android.ts b/tns-core-modules/ui/layouts/absolute-layout/absolute-layout.android.ts index 2050767a86..56dc762e81 100644 --- a/tns-core-modules/ui/layouts/absolute-layout/absolute-layout.android.ts +++ b/tns-core-modules/ui/layouts/absolute-layout/absolute-layout.android.ts @@ -10,13 +10,11 @@ function setNativeProperty(data: PropertyChangeData, setter: (lp: org.nativescri var view = data.object; if (view instanceof View) { var nativeView: android.view.View = view._nativeView; - - var lp = nativeView.getLayoutParams(); - if (!(lp instanceof org.nativescript.widgets.CommonLayoutParams)) { - lp = new org.nativescript.widgets.CommonLayoutParams(); + var lp = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams(); + if (lp instanceof org.nativescript.widgets.CommonLayoutParams) { + setter(lp); + nativeView.setLayoutParams(lp); } - setter(lp); - nativeView.setLayoutParams(lp); } } diff --git a/tns-core-modules/ui/layouts/dock-layout/dock-layout.android.ts b/tns-core-modules/ui/layouts/dock-layout/dock-layout.android.ts index 01460900af..60fafec513 100644 --- a/tns-core-modules/ui/layouts/dock-layout/dock-layout.android.ts +++ b/tns-core-modules/ui/layouts/dock-layout/dock-layout.android.ts @@ -10,30 +10,27 @@ function setNativeDockProperty(data: PropertyChangeData) { var view = data.object; if (view instanceof View) { var nativeView: android.view.View = view._nativeView; - - var lp = nativeView.getLayoutParams(); - if (!(lp instanceof org.nativescript.widgets.CommonLayoutParams)) { - lp = new org.nativescript.widgets.CommonLayoutParams(); - } - - switch (data.newValue) { - case Dock.left: - lp.dock = org.nativescript.widgets.Dock.left; - break; - case Dock.top: - lp.dock = org.nativescript.widgets.Dock.top; - break; - case Dock.right: - lp.dock = org.nativescript.widgets.Dock.right; - break; - case Dock.bottom: - lp.dock = org.nativescript.widgets.Dock.bottom; - break; - default: - throw new Error("Invalid dock value: " + data.newValue + " on element: " + view); + var lp = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams(); + if (lp instanceof org.nativescript.widgets.CommonLayoutParams) { + switch (data.newValue) { + case Dock.left: + lp.dock = org.nativescript.widgets.Dock.left; + break; + case Dock.top: + lp.dock = org.nativescript.widgets.Dock.top; + break; + case Dock.right: + lp.dock = org.nativescript.widgets.Dock.right; + break; + case Dock.bottom: + lp.dock = org.nativescript.widgets.Dock.bottom; + break; + default: + throw new Error("Invalid dock value: " + data.newValue + " on element: " + view); + } + + nativeView.setLayoutParams(lp); } - - nativeView.setLayoutParams(lp); } } diff --git a/tns-core-modules/ui/layouts/grid-layout/grid-layout.android.ts b/tns-core-modules/ui/layouts/grid-layout/grid-layout.android.ts index 6b7f96ec9d..95de55828e 100644 --- a/tns-core-modules/ui/layouts/grid-layout/grid-layout.android.ts +++ b/tns-core-modules/ui/layouts/grid-layout/grid-layout.android.ts @@ -10,13 +10,11 @@ function setNativeProperty(data: PropertyChangeData, setter: (lp: org.nativescri let view = data.object; if (view instanceof View) { let nativeView: android.view.View = view._nativeView; - - let lp = nativeView.getLayoutParams(); - if (!(lp instanceof org.nativescript.widgets.CommonLayoutParams)) { - lp = new org.nativescript.widgets.CommonLayoutParams(); + var lp = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams(); + if (lp instanceof org.nativescript.widgets.CommonLayoutParams) { + setter(lp); + nativeView.setLayoutParams(lp); } - setter(lp); - nativeView.setLayoutParams(lp); } } diff --git a/tns-core-modules/ui/tab-view/tab-view.android.ts b/tns-core-modules/ui/tab-view/tab-view.android.ts index f83bf1fea0..582a940c09 100644 --- a/tns-core-modules/ui/tab-view/tab-view.android.ts +++ b/tns-core-modules/ui/tab-view/tab-view.android.ts @@ -228,7 +228,7 @@ export class TabView extends common.TabView { } this._viewPager = new android.support.v4.view.ViewPager(this._context); - var lp = new org.nativescript.widgets.CommonLayoutParams() + var lp = new org.nativescript.widgets.CommonLayoutParams(); lp.row = 1; this._viewPager.setLayoutParams(lp); this._grid.addView(this._viewPager); From 61dbfbd68fd65a08ab91ca48d4520a83e7287b03 Mon Sep 17 00:00:00 2001 From: Hristo Hristov Date: Wed, 29 Jun 2016 15:12:35 +0300 Subject: [PATCH 2/2] Small code refactoring to get intellisense --- tns-core-modules/ui/core/view.android.ts | 63 ++++++++++++------------ 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/tns-core-modules/ui/core/view.android.ts b/tns-core-modules/ui/core/view.android.ts index 788fb5ec1d..b118020ab3 100644 --- a/tns-core-modules/ui/core/view.android.ts +++ b/tns-core-modules/ui/core/view.android.ts @@ -348,7 +348,7 @@ export class View extends viewCommon.View { return { x: utils.layout.toDeviceIndependentPixels(nativeArray[0]), y: utils.layout.toDeviceIndependentPixels(nativeArray[1]), - } + } } public getLocationOnScreen(): viewDefinition.Point { @@ -361,7 +361,7 @@ export class View extends viewCommon.View { return { x: utils.layout.toDeviceIndependentPixels(nativeArray[0]), y: utils.layout.toDeviceIndependentPixels(nativeArray[1]), - } + } } public getLocationRelativeTo(otherView: viewDefinition.View): viewDefinition.Point { @@ -378,7 +378,7 @@ export class View extends viewCommon.View { return { x: utils.layout.toDeviceIndependentPixels(myArray[0] - otherArray[0]), y: utils.layout.toDeviceIndependentPixels(myArray[1] - otherArray[1]), - } + } } public static resolveSizeAndState(size: number, specSize: number, specMode: number, childMeasuredState: number): number { @@ -501,10 +501,10 @@ export class ViewStyler implements style.Styler { } private static setNativeLayoutParamsProperty(view: View, params: CommonLayoutParams): void { - var nativeView: android.view.View = view._nativeView; + let nativeView: android.view.View = view._nativeView; - var width = params.width * utils.layout.getDisplayDensity(); - var height = params.height * utils.layout.getDisplayDensity(); + let width = params.width * utils.layout.getDisplayDensity(); + let height = params.height * utils.layout.getDisplayDensity(); // If width is not specified set it as WRAP_CONTENT if (width < 0) { @@ -516,7 +516,7 @@ export class ViewStyler implements style.Styler { height = -2; } - var gravity = 0; + let gravity = 0; switch (params.horizontalAlignment) { case enums.HorizontalAlignment.left: gravity |= android.view.Gravity.LEFT; @@ -568,7 +568,7 @@ export class ViewStyler implements style.Styler { throw new Error("Invalid verticalAlignment value: " + params.verticalAlignment); } - var lp: any = nativeView.getLayoutParams(); + let lp = nativeView.getLayoutParams(); lp.width = Math.round(width); lp.height = Math.round(height); @@ -586,48 +586,49 @@ export class ViewStyler implements style.Styler { lp.gravity = gravity; } else { - if (types.isDefined(lp.widthPercent)) { - lp.widthPercent = params.widthPercent; + let layoutParams: any = lp; + if (types.isDefined(layoutParams.widthPercent)) { + layoutParams.widthPercent = params.widthPercent; } - if (types.isDefined(lp.heightPercent)) { - lp.heightPercent = params.heightPercent; + if (types.isDefined(layoutParams.heightPercent)) { + layoutParams.heightPercent = params.heightPercent; } - if (types.isDefined(lp.leftMarginPercent)) { - lp.leftMarginPercent = params.leftMarginPercent; + if (types.isDefined(layoutParams.leftMarginPercent)) { + layoutParams.leftMarginPercent = params.leftMarginPercent; } - if (types.isDefined(lp.topMarginPercent)) { - lp.topMarginPercent = params.topMarginPercent; + if (types.isDefined(layoutParams.topMarginPercent)) { + layoutParams.topMarginPercent = params.topMarginPercent; } - if (types.isDefined(lp.rightMarginPercent)) { - lp.rightMarginPercent = params.rightMarginPercent; + if (types.isDefined(layoutParams.rightMarginPercent)) { + layoutParams.rightMarginPercent = params.rightMarginPercent; } - if (types.isDefined(lp.bottomMarginPercent)) { - lp.bottomMarginPercent = params.bottomMarginPercent; + if (types.isDefined(layoutParams.bottomMarginPercent)) { + layoutParams.bottomMarginPercent = params.bottomMarginPercent; } - if (types.isDefined(lp.leftMargin)) { - lp.leftMargin = Math.round(params.leftMargin * utils.layout.getDisplayDensity()); + if (types.isDefined(layoutParams.leftMargin)) { + layoutParams.leftMargin = Math.round(params.leftMargin * utils.layout.getDisplayDensity()); } - if (types.isDefined(lp.topMargin)) { - lp.topMargin = Math.round(params.topMargin * utils.layout.getDisplayDensity()); + if (types.isDefined(layoutParams.topMargin)) { + layoutParams.topMargin = Math.round(params.topMargin * utils.layout.getDisplayDensity()); } - if (types.isDefined(lp.rightMargin)) { - lp.rightMargin = Math.round(params.rightMargin * utils.layout.getDisplayDensity()); + if (types.isDefined(layoutParams.rightMargin)) { + layoutParams.rightMargin = Math.round(params.rightMargin * utils.layout.getDisplayDensity()); } - if (types.isDefined(lp.bottomMargin)) { - lp.bottomMargin = Math.round(params.bottomMargin * utils.layout.getDisplayDensity()); + if (types.isDefined(layoutParams.bottomMargin)) { + layoutParams.bottomMargin = Math.round(params.bottomMargin * utils.layout.getDisplayDensity()); } - if (types.isDefined(lp.gravity)) { - lp.gravity = gravity; + if (types.isDefined(layoutParams.gravity)) { + layoutParams.gravity = gravity; } } @@ -710,7 +711,7 @@ export class ViewStyler implements style.Styler { if (view.android.setZ) { view.android.setZ(newValue); - if(view.android instanceof android.widget.Button){ + if (view.android instanceof android.widget.Button) { view.android.setStateListAnimator(null); } }