Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/tests/xml-declaration/mainPage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@
</TabViewItem>
</TabView.items>
</TabView>
</Page>
</Page>
9 changes: 9 additions & 0 deletions apps/ui-tests-app/css/clip-path.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Page>
<GridLayout rows="*,*,*,*,*">
<Button text="circle" style="background-color: red; border-width:2; border-color: blue; clip-path: circle(100% at 50% 50%);"/>
<Button text="ellipse" style="background-color: orange; border-width:2; border-color: green; clip-path: ellipse(50% 50% at 50% 50%);" row="1" />
<Button text="rect" style="background-color: red; border-width:2; border-color: blue; clip-path: rect(0, 0, 100%, 100%);" row="2" />
<Button text="polygon" style="background-color: magenta; border-width:2; border-color: red; clip-path: polygon(20% 0%, 0% 20%, 30% 50%, 0% 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0%, 50% 30%);" row="3" />
<Button text="image polygon" style="background-image: url('https://media-cdn.tripadvisor.com/media/photo-s/03/9b/2f/ce/maldives.jpg'); border-width:2; border-color: red; clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);" row="4" />
</GridLayout>
</Page>
1 change: 1 addition & 0 deletions ui/core/view.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ export class ViewStyler implements style.Styler {
style.registerHandler(style.borderWidthProperty, borderHandler);
style.registerHandler(style.borderColorProperty, borderHandler);
style.registerHandler(style.borderRadiusProperty, borderHandler);
style.registerHandler(style.clipPathProperty, borderHandler);

style.registerHandler(style.nativeLayoutParamsProperty, new style.StylePropertyChangedHandler(
ViewStyler.setNativeLayoutParamsProperty,
Expand Down
30 changes: 29 additions & 1 deletion ui/core/view.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ export class ViewStyler implements style.Styler {
private static getTranslateYProperty(view: View): any {
return view.translateY;
}

//z-index
private static setZIndexProperty(view: View, newValue: any) {
view.ios.layer.zPosition = newValue;
Expand All @@ -580,13 +580,41 @@ export class ViewStyler implements style.Styler {
private static getZIndexProperty(view: View): any {
return view.ios.layer.zPosition;
}

//Clip-path methods
private static setClipPathProperty(view: View, newValue: any) {
var nativeView: UIView = <UIView>view._nativeView;
if (nativeView) {
ensureBackground();
var updateSuspended = view._isPresentationLayerUpdateSuspeneded();
if (!updateSuspended) {
CATransaction.begin();
}
nativeView.backgroundColor = background.ios.createBackgroundUIColor(view);
if (!updateSuspended) {
CATransaction.commit();
}
}
}

private static resetClipPathProperty(view: View, nativeValue: any) {
var nativeView: UIView = <UIView>view._nativeView;
if (nativeView) {
// TODO: Check how to reset.
}
}

public static registerHandlers() {

style.registerHandler(style.backgroundInternalProperty, new style.StylePropertyChangedHandler(
ViewStyler.setBackgroundInternalProperty,
ViewStyler.resetBackgroundInternalProperty,
ViewStyler.getNativeBackgroundInternalValue));

style.registerHandler(style.clipPathProperty, new style.StylePropertyChangedHandler(
ViewStyler.setClipPathProperty,
ViewStyler.resetClipPathProperty));

style.registerHandler(style.visibilityProperty, new style.StylePropertyChangedHandler(
ViewStyler.setVisibilityProperty,
ViewStyler.resetVisibilityProperty));
Expand Down
16 changes: 16 additions & 0 deletions ui/styling/background-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import colorModule = require("color");
import enums = require("ui/enums");
import definition = require("ui/styling/background");
import cssValue = require("css-value");
import utils = require("utils/utils");
import * as typesModule from "utils/types";

var types: typeof typesModule;
Expand Down Expand Up @@ -242,3 +243,18 @@ export class Background implements definition.Background {
colorModule.Color.equals(value1.color, value2.color);
}
}

export function cssValueToDevicePixels(source: string, total: number): number {
var result;
source = source.trim();

if (source.indexOf("px") !== -1) {
result = parseFloat(source.replace("px", ""));
}
else if (source.indexOf("%") !== -1 && total > 0) {
result = (parseFloat(source.replace("%", "")) / 100) * utils.layout.toDeviceIndependentPixels(total);
} else {
result = parseFloat(source);
}
return utils.layout.toDevicePixels(result);
}
153 changes: 122 additions & 31 deletions ui/styling/background.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,23 @@ export module ad {
private _borderWidth: number;
private _cornerRadius: number;
private _borderColor: number;
private _clipPath: string;

constructor() {
super();
return global.__native(this);
}

get clipPath(): string {
return this._clipPath;
}
set clipPath(value: string) {
if (this._clipPath !== value) {
this._clipPath = value;
this.invalidateSelf();
}
}

get borderWidth(): number {
return this._borderWidth;
}
Expand Down Expand Up @@ -101,15 +112,19 @@ export module ad {
let backgroundBoundsF = new android.graphics.RectF(bounds.left + backoffAntialias, bounds.top + backoffAntialias, bounds.right - backoffAntialias, bounds.bottom - backoffAntialias);

let outerRadius = this._cornerRadius * this._density;

// draw background
if (this.background.color && this.background.color.android) {
let backgroundColorPaint = new android.graphics.Paint();
backgroundColorPaint.setStyle(android.graphics.Paint.Style.FILL);
backgroundColorPaint.setColor(this.background.color.android);
backgroundColorPaint.setAntiAlias(true);

canvas.drawRoundRect(backgroundBoundsF, outerRadius, outerRadius, backgroundColorPaint);
if (this.clipPath) {
drawClipPath(this.clipPath, canvas, backgroundColorPaint, backgroundBoundsF);
} else {
canvas.drawRoundRect(backgroundBoundsF, outerRadius, outerRadius, backgroundColorPaint);
}
}

// draw image
Expand Down Expand Up @@ -139,21 +154,25 @@ export module ad {
params.posX = params.repeatX ? 0 : params.posX;
params.posY = params.repeatY ? 0 : params.posY;

let supportsPathOp = android.os.Build.VERSION.SDK_INT >= 19;
if (supportsPathOp) {
// Path.Op can be used in API level 19+ to achieve the perfect geometry.
let backgroundPath = new android.graphics.Path();
backgroundPath.addRoundRect(backgroundBoundsF, outerRadius, outerRadius, android.graphics.Path.Direction.CCW);
let backgroundNoRepeatPath = new android.graphics.Path();
backgroundNoRepeatPath.addRect(params.posX, params.posY, params.posX + imageWidth, params.posY + imageHeight, android.graphics.Path.Direction.CCW);
(<any>backgroundPath).op(backgroundNoRepeatPath, (<any>android).graphics.Path.Op.INTERSECT);
canvas.drawPath(backgroundPath, backgroundImagePaint);
if (this.clipPath) {
drawClipPath(this.clipPath, canvas, backgroundImagePaint, backgroundBoundsF);
} else {
// Clipping here will not be antialiased but at least it won't shine through the rounded corners.
canvas.save();
canvas.clipRect(params.posX, params.posY, params.posX + imageWidth, params.posY + imageHeight);
canvas.drawRoundRect(backgroundBoundsF, outerRadius, outerRadius, backgroundImagePaint);
canvas.restore();
let supportsPathOp = android.os.Build.VERSION.SDK_INT >= 19;
if (supportsPathOp) {
// Path.Op can be used in API level 19+ to achieve the perfect geometry.
let backgroundPath = new android.graphics.Path();
backgroundPath.addRoundRect(backgroundBoundsF, outerRadius, outerRadius, android.graphics.Path.Direction.CCW);
let backgroundNoRepeatPath = new android.graphics.Path();
backgroundNoRepeatPath.addRect(params.posX, params.posY, params.posX + imageWidth, params.posY + imageHeight, android.graphics.Path.Direction.CCW);
(<any>backgroundPath).op(backgroundNoRepeatPath, (<any>android).graphics.Path.Op.INTERSECT);
canvas.drawPath(backgroundPath, backgroundImagePaint);
} else {
// Clipping here will not be antialiased but at least it won't shine through the rounded corners.
canvas.save();
canvas.clipRect(params.posX, params.posY, params.posX + imageWidth, params.posY + imageHeight);
canvas.drawRoundRect(backgroundBoundsF, outerRadius, outerRadius, backgroundImagePaint);
canvas.restore();
}
}
}

Expand All @@ -164,23 +183,29 @@ export module ad {
borderPaint.setColor(this._borderColor);
borderPaint.setAntiAlias(true);

if (outerRadius <= 0) {
borderPaint.setStyle(android.graphics.Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderWidth);
canvas.drawRect(middleBoundsF, borderPaint);
} else if (outerRadius >= borderWidth) {
if (this.clipPath) {
borderPaint.setStyle(android.graphics.Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderWidth);
let middleRadius = Math.max(0, outerRadius - halfBorderWidth);
canvas.drawRoundRect(middleBoundsF, middleRadius, middleRadius, borderPaint);
drawClipPath(this.clipPath, canvas, borderPaint, backgroundBoundsF);
} else {
let borderPath = new android.graphics.Path();
let borderOuterBoundsF = new android.graphics.RectF(bounds.left, bounds.top, bounds.right, bounds.bottom);
borderPath.addRoundRect(borderOuterBoundsF, outerRadius, outerRadius, android.graphics.Path.Direction.CCW);
let borderInnerBoundsF = new android.graphics.RectF(bounds.left + borderWidth, bounds.top + borderWidth, bounds.right - borderWidth, bounds.bottom - borderWidth);
borderPath.addRect(borderInnerBoundsF, android.graphics.Path.Direction.CW);
borderPaint.setStyle(android.graphics.Paint.Style.FILL);
canvas.drawPath(borderPath, borderPaint);
if (outerRadius <= 0) {
borderPaint.setStyle(android.graphics.Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderWidth);
canvas.drawRect(middleBoundsF, borderPaint);
} else if (outerRadius >= borderWidth) {
borderPaint.setStyle(android.graphics.Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderWidth);
let middleRadius = Math.max(0, outerRadius - halfBorderWidth);
canvas.drawRoundRect(middleBoundsF, middleRadius, middleRadius, borderPaint);
} else {
let borderPath = new android.graphics.Path();
let borderOuterBoundsF = new android.graphics.RectF(bounds.left, bounds.top, bounds.right, bounds.bottom);
borderPath.addRoundRect(borderOuterBoundsF, outerRadius, outerRadius, android.graphics.Path.Direction.CCW);
let borderInnerBoundsF = new android.graphics.RectF(bounds.left + borderWidth, bounds.top + borderWidth, bounds.right - borderWidth, bounds.bottom - borderWidth);
borderPath.addRect(borderInnerBoundsF, android.graphics.Path.Direction.CW);
borderPaint.setStyle(android.graphics.Paint.Style.FILL);
canvas.drawPath(borderPath, borderPaint);
}
}
}
}
Expand Down Expand Up @@ -209,6 +234,8 @@ export module ad {
ensureBorderDrawable();
ensureLazyRequires();

var clipPathValue = v.style._getValue(style.clipPathProperty);

var backgroundValue = v.style._getValue(style.backgroundInternalProperty);
var borderWidth = v.borderWidth;
var bkg = <any>nativeView.getBackground();
Expand All @@ -220,7 +247,7 @@ export module ad {
let backgroundColor = bkg.backgroundColor = v.style._getValue(style.backgroundColorProperty).android;
bkg.setColorFilter(backgroundColor, android.graphics.PorterDuff.Mode.SRC_IN);
bkg.backgroundColor = backgroundColor;
} else if (v.borderWidth !== 0 || v.borderRadius !== 0 || !backgroundValue.isEmpty()) {
} else if (v.borderWidth !== 0 || v.borderRadius !== 0 || !backgroundValue.isEmpty() || !clipPathValue.isEmpty()) {

if (!(bkg instanceof BorderDrawableClass)) {
bkg = new BorderDrawableClass();
Expand All @@ -236,6 +263,7 @@ export module ad {
bkg.cornerRadius = v.borderRadius;
bkg.borderColor = v.borderColor ? v.borderColor.android : android.graphics.Color.TRANSPARENT;
bkg.background = backgroundValue;
bkg.clipPath = clipPathValue;

if (getSDK() < 18) {
// Switch to software because of unsupported canvas methods if hardware acceleration is on:
Expand Down Expand Up @@ -274,3 +302,66 @@ export module ad {
);
}
}

function drawClipPath(clipPath: string, canvas: android.graphics.Canvas, paint: android.graphics.Paint, bounds: android.graphics.RectF) {
var functionName = clipPath.substring(0, clipPath.indexOf("("));
var value = clipPath.replace(`${functionName}(`, "").replace(")", "");

if (functionName === "rect") {
var arr = value.split(/[\s]+/);

var top = common.cssValueToDevicePixels(arr[0], bounds.top);
var left = common.cssValueToDevicePixels(arr[1], bounds.left);
var bottom = common.cssValueToDevicePixels(arr[2], bounds.bottom);
var right = common.cssValueToDevicePixels(arr[3], bounds.right);

canvas.drawRect(left, top, right, bottom, paint);

} else if (functionName === "circle") {
var arr = value.split(/[\s]+/);

var radius = common.cssValueToDevicePixels(arr[0], (bounds.width() > bounds.height() ? bounds.height() : bounds.width()) / 2);
var y = common.cssValueToDevicePixels(arr[2], bounds.height());
var x = common.cssValueToDevicePixels(arr[3], bounds.width());

canvas.drawCircle(x, y, radius, paint);

} else if (functionName === "ellipse") {
var arr = value.split(/[\s]+/);

var rX = common.cssValueToDevicePixels(arr[0], bounds.right);
var rY = common.cssValueToDevicePixels(arr[1], bounds.bottom);
var cX = common.cssValueToDevicePixels(arr[3], bounds.right);
var cY = common.cssValueToDevicePixels(arr[4], bounds.bottom);

var left = cX - rX;
var top = cY - rY;
var right = (rX * 2) + left;
var bottom = (rY * 2) + top;

canvas.drawOval(new android.graphics.RectF(left, top, right, bottom), paint);

} else if (functionName === "polygon") {
var path = new android.graphics.Path();
var firstPoint: view.Point;
var arr = value.split(/[,]+/);
for (let i = 0; i < arr.length; i++) {
let xy = arr[i].trim().split(/[\s]+/);
let point: view.Point = {
x: common.cssValueToDevicePixels(xy[0], bounds.width()),
y: common.cssValueToDevicePixels(xy[1], bounds.height())
};

if (!firstPoint) {
firstPoint = point;
path.moveTo(point.x, point.y);
}

path.lineTo(point.x, point.y);
}

path.lineTo(firstPoint.x, firstPoint.y);

canvas.drawPath(path, paint);
}
}
Loading