mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
border and tests fixes
This commit is contained in:
@ -1,34 +1,31 @@
|
|||||||
import borderModule = require("ui/border");
|
import borderModule = require("ui/border");
|
||||||
import utils = require("utils/utils");
|
|
||||||
|
|
||||||
var density = utils.layout.getDisplayDensity();
|
|
||||||
|
|
||||||
export function getNativeBorderWidth(border: borderModule.Border): number {
|
export function getNativeBorderWidth(border: borderModule.Border): number {
|
||||||
var bkg = <any>(<android.view.ViewGroup>border.android).getBackground();
|
var bkg = <any>(<android.view.View>border.android).getBackground();
|
||||||
|
|
||||||
return bkg && bkg.getStroke ? bkg.getStroke() / density : -1;
|
return bkg ? bkg.borderWidth : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getNativeCornerRadius(border: borderModule.Border): number {
|
export function getNativeCornerRadius(border: borderModule.Border): number {
|
||||||
var bkg = <any>(<android.view.ViewGroup>border.android).getBackground();
|
var bkg = <any>(<android.view.View>border.android).getBackground();
|
||||||
|
|
||||||
return bkg && bkg.getCornerRadius ? bkg.getCornerRadius() / density : -1;
|
return bkg ? bkg.cornerRadius : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function checkNativeBorderColor(border: borderModule.Border): boolean {
|
export function checkNativeBorderColor(border: borderModule.Border): boolean {
|
||||||
var bkg = <any>(<android.view.ViewGroup>border.android).getBackground();
|
var bkg = <any>(<android.view.View>border.android).getBackground();
|
||||||
|
|
||||||
return border.borderColor && bkg && bkg.getBorderColor && bkg.getBorderColor() === border.borderColor.android;
|
return border.borderColor && bkg && bkg.borderColor === border.borderColor.android;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function checkNativeBackgroundColor(border: borderModule.Border): boolean {
|
export function checkNativeBackgroundColor(border: borderModule.Border): boolean {
|
||||||
var bkg = <any>(<android.view.ViewGroup>border.android).getBackground();
|
var bkg = <any>(<android.view.View>border.android).getBackground();
|
||||||
|
|
||||||
return border.backgroundColor && bkg && bkg.getBackgroundColor && bkg.getBackgroundColor() === border.backgroundColor.android;
|
return border.backgroundColor && bkg && bkg.backgroundColor === border.backgroundColor.android;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function checkNativeBackgroundImage(border: borderModule.Border): boolean {
|
export function checkNativeBackgroundImage(border: borderModule.Border): boolean {
|
||||||
var bkg = <any>(<android.view.ViewGroup>border.android).getBackground();
|
var bkg = <any>(<android.view.View>border.android).getBackground();
|
||||||
|
|
||||||
return bkg && bkg.getBitmap && bkg.getBitmap() !== undefined;
|
return bkg && bkg.bitmap !== undefined;
|
||||||
}
|
}
|
@ -80,8 +80,8 @@ export var testBackgroundImage = function () {
|
|||||||
|
|
||||||
helper.buildUIAndRunTest(border, function (views: Array<viewModule.View>) {
|
helper.buildUIAndRunTest(border, function (views: Array<viewModule.View>) {
|
||||||
var page = <pageModule.Page>views[1];
|
var page = <pageModule.Page>views[1];
|
||||||
page.css = "Border { background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC;') }";
|
page.css = "Border { background-image: url('http://www.google.com/images/errors/logo_sm_2.png') }";
|
||||||
|
|
||||||
TKUnit.assert(borderTestsNative.checkNativeBackgroundImage(border), "Style background-image not loaded correctly from data URI.");
|
TKUnit.assert(borderTestsNative.checkNativeBackgroundImage(border), "Style background-image not loaded correctly.");
|
||||||
});
|
});
|
||||||
}
|
}
|
@ -2,6 +2,7 @@
|
|||||||
import proxy = require("ui/core/proxy");
|
import proxy = require("ui/core/proxy");
|
||||||
import dependencyObservable = require("ui/core/dependency-observable");
|
import dependencyObservable = require("ui/core/dependency-observable");
|
||||||
import utils = require("utils/utils");
|
import utils = require("utils/utils");
|
||||||
|
import styleModule = require("ui/styling/style");
|
||||||
|
|
||||||
// merge the exports of the common file with the exports of this file
|
// merge the exports of the common file with the exports of this file
|
||||||
declare var exports;
|
declare var exports;
|
||||||
@ -15,76 +16,108 @@ function onBorderPropertyChanged(data: dependencyObservable.PropertyChangeData)
|
|||||||
(<proxy.PropertyMetadata>borderCommon.Border.cornerRadiusProperty.metadata).onSetNativeValue = onBorderPropertyChanged;
|
(<proxy.PropertyMetadata>borderCommon.Border.cornerRadiusProperty.metadata).onSetNativeValue = onBorderPropertyChanged;
|
||||||
(<proxy.PropertyMetadata>borderCommon.Border.borderWidthProperty.metadata).onSetNativeValue = onBorderPropertyChanged;
|
(<proxy.PropertyMetadata>borderCommon.Border.borderWidthProperty.metadata).onSetNativeValue = onBorderPropertyChanged;
|
||||||
(<proxy.PropertyMetadata>borderCommon.Border.borderColorProperty.metadata).onSetNativeValue = onBorderPropertyChanged;
|
(<proxy.PropertyMetadata>borderCommon.Border.borderColorProperty.metadata).onSetNativeValue = onBorderPropertyChanged;
|
||||||
|
//(<proxy.PropertyMetadata>styleModule.backgroundColorProperty.metadata).onSetNativeValue = onBorderPropertyChanged;
|
||||||
|
|
||||||
export class Border extends borderCommon.Border {
|
export class Border extends borderCommon.Border {
|
||||||
public _updateAndroidBorder(bmp?: android.graphics.Bitmap) {
|
public _updateAndroidBorder() {
|
||||||
if (!this._nativeView) {
|
if (!this._nativeView) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
(<android.view.ViewGroup>this._nativeView).setBackgroundDrawable(new BorderGradientDrawable(this.borderWidth,
|
var viewGroup = <android.view.ViewGroup>this._nativeView;
|
||||||
this.borderColor ? this.borderColor.android : android.graphics.Color.TRANSPARENT,
|
|
||||||
this.backgroundColor ? this.backgroundColor.android : android.graphics.Color.TRANSPARENT,
|
var bkg = <BorderGradientDrawable>viewGroup.getBackground();
|
||||||
this.cornerRadius,
|
|
||||||
bmp));
|
if (!(bkg instanceof BorderGradientDrawable)) {
|
||||||
|
bkg = new BorderGradientDrawable();
|
||||||
|
viewGroup.setBackground(bkg);
|
||||||
|
}
|
||||||
|
|
||||||
|
bkg.borderWidth = this.borderWidth;
|
||||||
|
bkg.cornerRadius = this.cornerRadius;
|
||||||
|
bkg.borderColor = this.borderColor ? this.borderColor.android : android.graphics.Color.TRANSPARENT;
|
||||||
|
bkg.backgroundColor = this.backgroundColor ? this.backgroundColor.android : android.graphics.Color.TRANSPARENT;
|
||||||
|
|
||||||
|
var value = this.style._getValue(styleModule.backgroundImageSourceProperty);
|
||||||
|
bkg.bitmap = value ? value.android : undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class BorderGradientDrawable extends android.graphics.drawable.GradientDrawable {
|
class BorderGradientDrawable extends android.graphics.drawable.GradientDrawable {
|
||||||
private paint: android.graphics.Paint;
|
private _density = utils.layout.getDisplayDensity();
|
||||||
private stroke: number;
|
|
||||||
private cornerRadius: number;
|
|
||||||
private backgroundColor: number;
|
|
||||||
private borderColor: number;
|
|
||||||
private bitmap: android.graphics.Bitmap
|
|
||||||
|
|
||||||
constructor(borderWidth: number, borderColor: number, backgroundColor: number, cornerRadius: number, bitmap?: android.graphics.Bitmap) {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.bitmap = bitmap;
|
|
||||||
|
|
||||||
if (bitmap) {
|
|
||||||
this.paint = new android.graphics.Paint();
|
|
||||||
} else {
|
|
||||||
this.backgroundColor = backgroundColor;
|
|
||||||
this.setColor(backgroundColor);
|
|
||||||
}
|
|
||||||
|
|
||||||
var density = utils.layout.getDisplayDensity();
|
|
||||||
|
|
||||||
this.stroke = borderWidth * density;
|
|
||||||
this.borderColor = borderColor;
|
|
||||||
this.setStroke(this.stroke, this.borderColor);
|
|
||||||
|
|
||||||
this.cornerRadius = cornerRadius * density;
|
|
||||||
this.setCornerRadius(this.cornerRadius);
|
|
||||||
|
|
||||||
return global.__native(this);
|
return global.__native(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getStroke(): number {
|
private _borderWidth: number;
|
||||||
return this.stroke;
|
get borderWidth(): number {
|
||||||
|
return this._borderWidth;
|
||||||
|
}
|
||||||
|
set borderWidth(value: number) {
|
||||||
|
if (this._borderWidth !== value) {
|
||||||
|
this._borderWidth = value;
|
||||||
|
|
||||||
|
this.setStroke(this._borderWidth * this._density, this._borderColor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public getCornerRadius(): number {
|
private _cornerRadius: number;
|
||||||
return this.cornerRadius;
|
get cornerRadius(): number {
|
||||||
|
return this._cornerRadius;
|
||||||
|
}
|
||||||
|
set cornerRadius(value: number) {
|
||||||
|
if (this._cornerRadius !== value) {
|
||||||
|
this._cornerRadius = value;
|
||||||
|
|
||||||
|
this.setCornerRadius(this._cornerRadius);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public getBackgroundColor(): number {
|
private _borderColor: number;
|
||||||
return this.backgroundColor;
|
get borderColor(): number {
|
||||||
|
return this._borderColor;
|
||||||
|
}
|
||||||
|
set borderColor(value: number) {
|
||||||
|
if (this._borderColor !== value) {
|
||||||
|
this._borderColor = value;
|
||||||
|
|
||||||
|
this.setStroke(this._borderWidth * this._density, this._borderColor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public getBorderColor(): number {
|
private _backgroundColor: number;
|
||||||
return this.borderColor;
|
get backgroundColor(): number {
|
||||||
|
return this._backgroundColor;
|
||||||
|
}
|
||||||
|
set backgroundColor(value: number) {
|
||||||
|
if (this._backgroundColor !== value) {
|
||||||
|
this._backgroundColor = value;
|
||||||
|
|
||||||
|
this.setColor(this._backgroundColor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public getBitmap(): android.graphics.Bitmap {
|
private _bitmap: android.graphics.Bitmap
|
||||||
return this.bitmap;
|
get bitmap(): android.graphics.Bitmap {
|
||||||
|
return this._bitmap;
|
||||||
|
}
|
||||||
|
set bitmap(value: android.graphics.Bitmap) {
|
||||||
|
if (this._bitmap !== value) {
|
||||||
|
this._bitmap = value;
|
||||||
|
|
||||||
|
this.invalidateSelf();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public draw(canvas: android.graphics.Canvas): void {
|
public draw(canvas: android.graphics.Canvas): void {
|
||||||
if (this.paint) {
|
if (this.bitmap) {
|
||||||
canvas.drawBitmap(this.bitmap, this.stroke, this.stroke, this.paint);
|
this.setColor(android.graphics.Color.TRANSPARENT);
|
||||||
|
|
||||||
|
var stroke = this._borderWidth * this._density;
|
||||||
|
canvas.drawBitmap(this.bitmap, stroke, stroke, undefined);
|
||||||
}
|
}
|
||||||
super.draw(canvas);
|
super.draw(canvas);
|
||||||
}
|
}
|
||||||
|
2
ui/border/border.d.ts
vendored
2
ui/border/border.d.ts
vendored
@ -30,7 +30,7 @@ declare module "ui/border" {
|
|||||||
borderColor: color.Color;
|
borderColor: color.Color;
|
||||||
|
|
||||||
//@private
|
//@private
|
||||||
_updateAndroidBorder(bmp?: android.graphics.Bitmap);
|
_updateAndroidBorder();
|
||||||
//@endprivate
|
//@endprivate
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -356,40 +356,35 @@ export class BorderStyler implements definition.stylers.Styler {
|
|||||||
border._updateAndroidBorder();
|
border._updateAndroidBorder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static getNativeBackgroundValue(view: view.View): any {
|
||||||
|
return view.android.getBackground();
|
||||||
|
}
|
||||||
|
|
||||||
//Background image methods
|
//Background image methods
|
||||||
private static setBackgroundImageSourceProperty(view: view.View, newValue: any) {
|
private static setBackgroundImageSourceProperty(view: view.View, newValue: any) {
|
||||||
var border = <border.Border>view;
|
var border = <border.Border>view;
|
||||||
border._updateAndroidBorder(newValue);
|
border._updateAndroidBorder();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static resetBackgroundImageSourceProperty(view: view.View, nativeValue: any) {
|
private static resetBackgroundImageSourceProperty(view: view.View, nativeValue: any) {
|
||||||
var border = <border.Border>view;
|
var border = <border.Border>view;
|
||||||
if (types.isDefined(nativeValue)) {
|
border._updateAndroidBorder();
|
||||||
(<android.view.View>view.android).setBackgroundDrawable(nativeValue);
|
|
||||||
border._updateAndroidBorder();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static getNativeBackgroundImageSourceValue(view: view.View): any {
|
private static getNativeBackgroundImageSourceValue(view: view.View): any {
|
||||||
var drawable = view.android.getBackground();
|
return view.android.getBackground();
|
||||||
|
|
||||||
if (drawable instanceof android.graphics.drawable.GradientDrawable) {
|
|
||||||
return drawable;
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static registerHandlers() {
|
public static registerHandlers() {
|
||||||
style.registerHandler(style.backgroundColorProperty, new stylersCommon.StylePropertyChangedHandler(
|
style.registerHandler(style.backgroundColorProperty, new stylersCommon.StylePropertyChangedHandler(
|
||||||
BorderStyler.setBackgroundProperty,
|
BorderStyler.setBackgroundProperty,
|
||||||
BorderStyler.resetBackgroundProperty), "Border");
|
BorderStyler.resetBackgroundProperty,
|
||||||
|
BorderStyler.getNativeBackgroundValue), "Border");
|
||||||
|
|
||||||
style.registerHandler(style.backgroundImageSourceProperty, new stylersCommon.StylePropertyChangedHandler(
|
style.registerHandler(style.backgroundImageSourceProperty, new stylersCommon.StylePropertyChangedHandler(
|
||||||
BorderStyler.setBackgroundImageSourceProperty,
|
BorderStyler.setBackgroundImageSourceProperty,
|
||||||
BorderStyler.resetBackgroundImageSourceProperty,
|
BorderStyler.resetBackgroundImageSourceProperty,
|
||||||
BorderStyler.getNativeBackgroundImageSourceValue), "Border");
|
BorderStyler.getNativeBackgroundImageSourceValue), "Border");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user