mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
stylers removed
This commit is contained in:
@@ -6,6 +6,11 @@ import dependencyObservable = require("ui/core/dependency-observable");
|
||||
import proxy = require("ui/core/proxy");
|
||||
import gestures = require("ui/gestures");
|
||||
import * as typesModule from "utils/types";
|
||||
import style = require("ui/styling/style");
|
||||
import styling = require("ui/styling");
|
||||
import enums = require("ui/enums");
|
||||
import background = require("ui/styling/background");
|
||||
import {CommonLayoutParams, Thickness} from "ui/styling/style";
|
||||
|
||||
global.moduleMerge(viewCommon, exports);
|
||||
|
||||
@@ -419,3 +424,227 @@ export class CustomLayoutView extends View implements viewDefinition.CustomLayou
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class ViewStyler implements style.Styler {
|
||||
//Background and borders methods
|
||||
private static setBackgroundBorderProperty(view: View, newValue: any, defaultValue?: any) {
|
||||
background.ad.onBackgroundOrBorderPropertyChanged(view);
|
||||
}
|
||||
|
||||
private static resetBackgroundBorderProperty(view: View, nativeValue: any) {
|
||||
background.ad.onBackgroundOrBorderPropertyChanged(view);
|
||||
}
|
||||
|
||||
//Visibility methods
|
||||
private static setVisibilityProperty(view: View, newValue: any) {
|
||||
var androidValue = (newValue === enums.Visibility.visible) ? android.view.View.VISIBLE : android.view.View.GONE;
|
||||
(<android.view.View>view._nativeView).setVisibility(androidValue);
|
||||
}
|
||||
|
||||
private static resetVisibilityProperty(view: View, nativeValue: any) {
|
||||
(<android.view.View>view._nativeView).setVisibility(android.view.View.VISIBLE);
|
||||
}
|
||||
|
||||
//Opacity methods
|
||||
private static setOpacityProperty(view: View, newValue: any) {
|
||||
(<android.view.View>view._nativeView).setAlpha(float(newValue));
|
||||
}
|
||||
|
||||
private static resetOpacityProperty(view: View, nativeValue: any) {
|
||||
(<android.view.View>view._nativeView).setAlpha(float(1.0));
|
||||
}
|
||||
|
||||
//minWidth methods
|
||||
private static setMinWidthProperty(view: View, newValue: any) {
|
||||
(<android.view.View>view._nativeView).setMinimumWidth(Math.round(newValue * utils.layout.getDisplayDensity()));
|
||||
}
|
||||
|
||||
private static resetMinWidthProperty(view: View, nativeValue: any) {
|
||||
(<android.view.View>view._nativeView).setMinimumWidth(0);
|
||||
}
|
||||
|
||||
//minHeight methods
|
||||
private static setMinHeightProperty(view: View, newValue: any) {
|
||||
(<android.view.View>view._nativeView).setMinimumHeight(Math.round(newValue * utils.layout.getDisplayDensity()));
|
||||
}
|
||||
|
||||
private static resetMinHeightProperty(view: View, nativeValue: any) {
|
||||
(<android.view.View>view._nativeView).setMinimumHeight(0);
|
||||
}
|
||||
|
||||
private static getNativeLayoutParams(nativeView: android.view.View): org.nativescript.widgets.CommonLayoutParams {
|
||||
var lp = <org.nativescript.widgets.CommonLayoutParams>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;
|
||||
}
|
||||
|
||||
// If height is not specified set it as WRAP_CONTENT
|
||||
if (height < 0) {
|
||||
height = -2;
|
||||
}
|
||||
|
||||
var gravity = 0;
|
||||
switch (params.horizontalAlignment) {
|
||||
case enums.HorizontalAlignment.left:
|
||||
gravity |= android.view.Gravity.LEFT;
|
||||
break;
|
||||
|
||||
case enums.HorizontalAlignment.center:
|
||||
gravity |= android.view.Gravity.CENTER_HORIZONTAL;
|
||||
break;
|
||||
|
||||
case enums.HorizontalAlignment.right:
|
||||
gravity |= android.view.Gravity.RIGHT;
|
||||
break;
|
||||
|
||||
case enums.HorizontalAlignment.stretch:
|
||||
gravity |= android.view.Gravity.FILL_HORIZONTAL;
|
||||
// If width is not specified set it as MATCH_PARENT
|
||||
if (width < 0) {
|
||||
width = -1;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error("Invalid horizontalAlignment value: " + params.horizontalAlignment);
|
||||
}
|
||||
|
||||
switch (params.verticalAlignment) {
|
||||
case enums.VerticalAlignment.top:
|
||||
gravity |= android.view.Gravity.TOP;
|
||||
break;
|
||||
|
||||
case enums.VerticalAlignment.center || enums.VerticalAlignment.middle:
|
||||
gravity |= android.view.Gravity.CENTER_VERTICAL;
|
||||
break;
|
||||
|
||||
case enums.VerticalAlignment.bottom:
|
||||
gravity |= android.view.Gravity.BOTTOM;
|
||||
break;
|
||||
|
||||
case enums.VerticalAlignment.stretch:
|
||||
gravity |= android.view.Gravity.FILL_VERTICAL;
|
||||
// If height is not specified set it as MATCH_PARENT
|
||||
if (height < 0) {
|
||||
height = -1;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error("Invalid verticalAlignment value: " + params.verticalAlignment);
|
||||
}
|
||||
|
||||
lp.gravity = gravity;
|
||||
lp.width = Math.round(width);
|
||||
lp.height = Math.round(height);
|
||||
|
||||
nativeView.setLayoutParams(lp);
|
||||
}
|
||||
|
||||
private static resetNativeLayoutParamsProperty(view: View, nativeValue: any): void {
|
||||
var nativeView: android.view.View = view._nativeView;
|
||||
var lp = ViewStyler.getNativeLayoutParams(nativeView);
|
||||
|
||||
lp.width = -1;
|
||||
lp.height = -1;
|
||||
lp.leftMargin = 0;
|
||||
lp.topMargin = 0;
|
||||
lp.rightMargin = 0;
|
||||
lp.bottomMargin = 0;
|
||||
lp.gravity = android.view.Gravity.FILL_HORIZONTAL | android.view.Gravity.FILL_VERTICAL;
|
||||
nativeView.setLayoutParams(lp);
|
||||
}
|
||||
|
||||
private static setPaddingProperty(view: View, newValue: Thickness) {
|
||||
var density = utils.layout.getDisplayDensity();
|
||||
var left = Math.round((newValue.left + view.borderWidth) * density);
|
||||
var top = Math.round((newValue.top + view.borderWidth) * density);
|
||||
var right = Math.round((newValue.right + view.borderWidth) * density);
|
||||
var bottom = Math.round((newValue.bottom + view.borderWidth) * density);
|
||||
(<android.view.View>view._nativeView).setPadding(left, top, right, bottom);
|
||||
}
|
||||
|
||||
private static resetPaddingProperty(view: View, nativeValue: Thickness) {
|
||||
var density = utils.layout.getDisplayDensity();
|
||||
var left = Math.round((nativeValue.left + view.borderWidth) * density);
|
||||
var top = Math.round((nativeValue.top + view.borderWidth) * density);
|
||||
var right = Math.round((nativeValue.right + view.borderWidth) * density);
|
||||
var bottom = Math.round((nativeValue.bottom + view.borderWidth) * density);
|
||||
(<android.view.View>view._nativeView).setPadding(left, top, right, bottom);
|
||||
}
|
||||
|
||||
public static registerHandlers() {
|
||||
style.registerHandler(style.visibilityProperty, new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setVisibilityProperty,
|
||||
ViewStyler.resetVisibilityProperty));
|
||||
|
||||
style.registerHandler(style.opacityProperty, new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setOpacityProperty,
|
||||
ViewStyler.resetOpacityProperty));
|
||||
|
||||
style.registerHandler(style.minWidthProperty, new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setMinWidthProperty,
|
||||
ViewStyler.resetMinWidthProperty));
|
||||
|
||||
style.registerHandler(style.minHeightProperty, new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setMinHeightProperty,
|
||||
ViewStyler.resetMinHeightProperty))
|
||||
|
||||
// Use the same handler for all background/border properties
|
||||
// Note: There is no default value getter - the default value is handled in background.ad.onBackgroundOrBorderPropertyChanged
|
||||
var borderHandler = new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setBackgroundBorderProperty,
|
||||
ViewStyler.resetBackgroundBorderProperty);
|
||||
|
||||
style.registerHandler(style.backgroundInternalProperty, borderHandler);
|
||||
style.registerHandler(style.borderWidthProperty, borderHandler);
|
||||
style.registerHandler(style.borderColorProperty, borderHandler);
|
||||
style.registerHandler(style.borderRadiusProperty, borderHandler);
|
||||
|
||||
style.registerHandler(style.nativeLayoutParamsProperty, new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setNativeLayoutParamsProperty,
|
||||
ViewStyler.resetNativeLayoutParamsProperty));
|
||||
|
||||
style.registerHandler(style.nativePaddingsProperty, new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setPaddingProperty,
|
||||
ViewStyler.resetPaddingProperty), "TextBase");
|
||||
|
||||
style.registerHandler(style.nativePaddingsProperty, new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setPaddingProperty,
|
||||
ViewStyler.resetPaddingProperty), "Button");
|
||||
|
||||
style.registerHandler(style.nativePaddingsProperty, new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setPaddingProperty,
|
||||
ViewStyler.resetPaddingProperty), "LayoutBase");
|
||||
}
|
||||
}
|
||||
|
||||
ViewStyler.registerHandlers();
|
||||
|
||||
@@ -3,6 +3,10 @@ import trace = require("trace");
|
||||
import utils = require("utils/utils");
|
||||
import dependencyObservable = require("ui/core/dependency-observable");
|
||||
import proxy = require("ui/core/proxy");
|
||||
import style = require("ui/styling/style");
|
||||
import styling = require("ui/styling");
|
||||
import enums = require("ui/enums");
|
||||
import * as backgroundModule from "ui/styling/background";
|
||||
|
||||
global.moduleMerge(viewCommon, exports);
|
||||
|
||||
@@ -317,3 +321,152 @@ export class CustomLayoutView extends View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class ViewStyler implements style.Styler {
|
||||
//Background methods
|
||||
private static setBackgroundInternalProperty(view: View, newValue: any) {
|
||||
var nativeView: UIView = <UIView>view._nativeView;
|
||||
if (nativeView) {
|
||||
var background: typeof backgroundModule = require("ui/styling/background");
|
||||
nativeView.backgroundColor = background.ios.createBackgroundUIColor(view);
|
||||
}
|
||||
}
|
||||
|
||||
private static resetBackgroundInternalProperty(view: View, nativeValue: any) {
|
||||
var nativeView: UIView = <UIView>view._nativeView;
|
||||
if (nativeView) {
|
||||
nativeView.backgroundColor = nativeValue;
|
||||
}
|
||||
}
|
||||
|
||||
private static getNativeBackgroundInternalValue(view: View): any {
|
||||
var nativeView: UIView = <UIView>view._nativeView;
|
||||
if (nativeView) {
|
||||
return nativeView.backgroundColor;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
//Visibility methods
|
||||
private static setVisibilityProperty(view: View, newValue: any) {
|
||||
var nativeView: UIView = <UIView>view._nativeView;
|
||||
if (nativeView) {
|
||||
return nativeView.hidden = (newValue !== enums.Visibility.visible);
|
||||
}
|
||||
}
|
||||
|
||||
private static resetVisibilityProperty(view: View, nativeValue: any) {
|
||||
var nativeView: UIView = <UIView>view._nativeView;
|
||||
if (nativeView) {
|
||||
return nativeView.hidden = false;
|
||||
}
|
||||
}
|
||||
|
||||
//Opacity methods
|
||||
private static setOpacityProperty(view: View, newValue: any) {
|
||||
var nativeView: UIView = <UIView>view._nativeView;
|
||||
if (nativeView) {
|
||||
return nativeView.alpha = newValue;
|
||||
}
|
||||
}
|
||||
|
||||
private static resetOpacityProperty(view: View, nativeValue: any) {
|
||||
var nativeView: UIView = <UIView>view._nativeView;
|
||||
if (nativeView) {
|
||||
return nativeView.alpha = 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
//Border width methods
|
||||
private static setBorderWidthProperty(view: View, newValue: any) {
|
||||
if (view._nativeView instanceof UIView) {
|
||||
(<UIView>view._nativeView).layer.borderWidth = newValue;
|
||||
}
|
||||
}
|
||||
|
||||
private static resetBorderWidthProperty(view: View, nativeValue: any) {
|
||||
if (view._nativeView instanceof UIView) {
|
||||
(<UIView>view._nativeView).layer.borderWidth = nativeValue;
|
||||
}
|
||||
}
|
||||
|
||||
private static getBorderWidthProperty(view: View): any {
|
||||
if (view._nativeView instanceof UIView) {
|
||||
return (<UIView>view._nativeView).layer.borderWidth;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Border color methods
|
||||
private static setBorderColorProperty(view: View, newValue: any) {
|
||||
if (view._nativeView instanceof UIView && newValue instanceof UIColor) {
|
||||
(<UIView>view._nativeView).layer.borderColor = (<UIColor>newValue).CGColor;
|
||||
}
|
||||
}
|
||||
|
||||
private static resetBorderColorProperty(view: View, nativeValue: any) {
|
||||
if (view._nativeView instanceof UIView && nativeValue instanceof UIColor) {
|
||||
(<UIView>view._nativeView).layer.borderColor = nativeValue;
|
||||
}
|
||||
}
|
||||
|
||||
private static getBorderColorProperty(view: View): any {
|
||||
if (view._nativeView instanceof UIView) {
|
||||
return (<UIView>view._nativeView).layer.borderColor;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
//Border radius methods
|
||||
private static setBorderRadiusProperty(view: View, newValue: any) {
|
||||
if (view._nativeView instanceof UIView) {
|
||||
(<UIView>view._nativeView).layer.cornerRadius = newValue;
|
||||
(<UIView>view._nativeView).clipsToBounds = true;
|
||||
}
|
||||
}
|
||||
|
||||
private static resetBorderRadiusProperty(view: View, nativeValue: any) {
|
||||
if (view._nativeView instanceof UIView) {
|
||||
(<UIView>view._nativeView).layer.cornerRadius = nativeValue;
|
||||
}
|
||||
}
|
||||
|
||||
private static getBorderRadiusProperty(view: View): any {
|
||||
if (view._nativeView instanceof UIView) {
|
||||
return (<UIView>view._nativeView).layer.cornerRadius;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static registerHandlers() {
|
||||
style.registerHandler(style.backgroundInternalProperty, new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setBackgroundInternalProperty,
|
||||
ViewStyler.resetBackgroundInternalProperty,
|
||||
ViewStyler.getNativeBackgroundInternalValue));
|
||||
|
||||
style.registerHandler(style.visibilityProperty, new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setVisibilityProperty,
|
||||
ViewStyler.resetVisibilityProperty));
|
||||
|
||||
style.registerHandler(style.opacityProperty, new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setOpacityProperty,
|
||||
ViewStyler.resetOpacityProperty));
|
||||
|
||||
style.registerHandler(style.borderWidthProperty, new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setBorderWidthProperty,
|
||||
ViewStyler.resetBorderWidthProperty,
|
||||
ViewStyler.getBorderWidthProperty));
|
||||
|
||||
style.registerHandler(style.borderColorProperty, new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setBorderColorProperty,
|
||||
ViewStyler.resetBorderColorProperty,
|
||||
ViewStyler.getBorderColorProperty));
|
||||
|
||||
style.registerHandler(style.borderRadiusProperty, new style.StylePropertyChangedHandler(
|
||||
ViewStyler.setBorderRadiusProperty,
|
||||
ViewStyler.resetBorderRadiusProperty,
|
||||
ViewStyler.getBorderRadiusProperty));
|
||||
}
|
||||
}
|
||||
|
||||
ViewStyler.registerHandlers();
|
||||
Reference in New Issue
Block a user