Skip to content

Commit b5f755c

Browse files
committed
chore: refactoring
1 parent e984b14 commit b5f755c

File tree

8 files changed

+33
-46
lines changed

8 files changed

+33
-46
lines changed

src/canvas.android.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { CSSType, Color, Device, Font, ImageSource, View } from '@nativescript/core';
2-
import { android as androidApp } from '@nativescript/core/application';
3-
import { FontStyle, FontStyleType, FontWeight, FontWeightType } from '@nativescript/core/ui/styling/font';
1+
import { Application, CSSType, Color, Device, Font, ImageSource, Utils, View } from '@nativescript/core';
42
import lazy from '@nativescript/core/utils/lazy';
5-
import { layout } from '@nativescript/core/utils/layout-helper';
63
import { Canvas as ICanvas, Paint as IPaint } from './canvas';
74
import { CanvasBase, hardwareAcceleratedProperty } from './canvas.common';
85

96
import { arrayToNativeArray } from '@nativescript-community/arraybuffers';
7+
import { FontStyleType, FontWeightType } from '@nativescript/core/ui/styling/font-interfaces';
108

119
declare global {
1210
const __runtimeVersion: string;
@@ -74,7 +72,7 @@ function createColorParam(param) {
7472
// }
7573
function drawViewOnCanvas(canvas: android.graphics.Canvas, view: View, rect?: android.graphics.Rect) {
7674
if (!view.nativeView) {
77-
const activity = androidApp.foregroundActivity;
75+
const activity = Application.android.foregroundActivity;
7876
(view as any)._setupAsRootView(activity);
7977
(view as any)._isAddedToNativeVisualTree = true;
8078
(view as any).callLoaded();
@@ -170,7 +168,7 @@ class Canvas extends ProxyClass<android.graphics.Canvas> {
170168
}
171169
} else if (methodName === 'getWidth' || methodName === 'getHeight') {
172170
if (!target._bitmap) {
173-
return layout.toDeviceIndependentPixels(native[methodName]());
171+
return Utils.layout.toDeviceIndependentPixels(native[methodName]());
174172
}
175173
} else if (methodName === 'clear') {
176174
native.drawColor(android.graphics.Color.TRANSPARENT);
@@ -519,7 +517,7 @@ class CanvasView extends CanvasBase {
519517
super.initNativeView();
520518
this.nativeViewProtected.sizeChangedListener = new com.akylas.canvas.SizeChangedListener({
521519
onSizeChanged: (w, h, oldW, oldH) => {
522-
this.onSizeChanged(layout.toDeviceIndependentPixels(w), layout.toDeviceIndependentPixels(h), oldW, oldH);
520+
this.onSizeChanged(Utils.layout.toDeviceIndependentPixels(w), Utils.layout.toDeviceIndependentPixels(h), oldW, oldH);
523521
}
524522
});
525523
this.nativeViewProtected.drawListener = new com.akylas.canvas.DrawListener({

src/canvas.common.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { ChangedData, Observable, ObservableArray, Property, Screen, View, booleanConverter, colorProperty } from '@nativescript/core';
2-
import { layout } from '@nativescript/core/utils/layout-helper';
1+
import { ChangedData, Observable, ObservableArray, Property, Screen, Utils, View, booleanConverter, colorProperty } from '@nativescript/core';
32
import { Canvas, Rect, RectF } from './canvas';
43
import Shape from './shapes/shape';
54

@@ -174,8 +173,8 @@ export abstract class CanvasBase extends View {
174173
}
175174
shapesCanvas: Canvas;
176175
drawShapes() {
177-
const width = layout.toDeviceIndependentPixels(this.getMeasuredWidth());
178-
const height = layout.toDeviceIndependentPixels(this.getMeasuredHeight());
176+
const width = Utils.layout.toDeviceIndependentPixels(this.getMeasuredWidth());
177+
const height = Utils.layout.toDeviceIndependentPixels(this.getMeasuredHeight());
179178
if (this.shapesCanvas) {
180179
// this.shapesCanvas.release();
181180
this.shapesCanvas = null;

src/canvas.ios.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* eslint-disable no-redeclare */
2-
import { Font, FontStyle, FontStyleType, FontWeight, FontWeightType } from '@nativescript/core/ui/styling/font';
3-
import { CSSType, Color, View, backgroundColorProperty } from '@nativescript/core';
4-
import { layout } from '@nativescript/core/utils/layout-helper';
5-
import { ImageSource } from '@nativescript/core/image-source';
2+
import { CSSType, Color, Font, ImageSource, Utils, View } from '@nativescript/core';
3+
import { FontStyleType, FontWeightType } from '@nativescript/core/ui/styling/font';
64
import { Canvas as ICanvas, FontMetrics as IFontMetrics, Matrix as IMatrix, Paint as IPaint, Path as IPath, PorterDuffXfermode as IPorterDuffXfermode, Rect as IRect, RectF as IRectF } from './canvas';
75
import { CanvasBase } from './canvas.common';
86

@@ -2136,7 +2134,7 @@ export class CanvasView extends CanvasBase {
21362134
}
21372135
_onSizeChanged() {
21382136
super._onSizeChanged();
2139-
this.onSizeChanged(layout.toDeviceIndependentPixels(this.getMeasuredWidth()), layout.toDeviceIndependentPixels(this.getMeasuredHeight()), -1, -1);
2137+
this.onSizeChanged(Utils.layout.toDeviceIndependentPixels(this.getMeasuredWidth()), Utils.layout.toDeviceIndependentPixels(this.getMeasuredHeight()), -1, -1);
21402138
}
21412139
redraw() {
21422140
if (this.nativeViewProtected) {

src/shapes/line.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import { CoreTypes } from '@nativescript/core';
2-
import { PercentLength } from '@nativescript/core/ui/styling/style-properties';
3-
import { layout } from '@nativescript/core/utils/layout-helper';
1+
import { CoreTypes, PercentLength, Utils } from '@nativescript/core';
42
import { Canvas, Style } from '../canvas';
53
import Rectangle from './rectangle';
64
import { percentLengthProperty } from './shape';
75

86
export default class Line extends Rectangle {
97
paintStyle = Style.STROKE;
108
drawOnCanvas(canvas: Canvas) {
11-
const availableWidth = layout.toDevicePixels(canvas.getWidth());
12-
const availableHeight = layout.toDevicePixels(canvas.getHeight());
9+
const availableWidth = Utils.layout.toDevicePixels(canvas.getWidth());
10+
const availableHeight = Utils.layout.toDevicePixels(canvas.getHeight());
1311
canvas.drawLine(
14-
layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.startX, 0, availableWidth)),
15-
layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.startY, 0, availableHeight)),
16-
layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.stopX, 0, availableWidth)),
17-
layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.stopY, 0, availableHeight)),
12+
Utils.layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.startX, 0, availableWidth)),
13+
Utils.layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.startY, 0, availableHeight)),
14+
Utils.layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.stopX, 0, availableWidth)),
15+
Utils.layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.stopY, 0, availableHeight)),
1816
this.paint
1917
);
2018
}

src/shapes/rectangle.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { CoreTypes } from '@nativescript/core';
2-
import { PercentLength } from '@nativescript/core/ui/styling/style-properties';
3-
import { layout } from '@nativescript/core/utils/layout-helper';
1+
import { CoreTypes, PercentLength, Utils } from '@nativescript/core';
42
import { Canvas, Rect, RectF, createRectF } from '../canvas';
53
import Shape, { numberProperty, percentLengthProperty } from './shape';
64

@@ -18,13 +16,13 @@ export default class Rectangle extends Shape {
1816
@percentLengthProperty top: CoreTypes.PercentLengthType = 0;
1917
@numberProperty borderRadius: number;
2018
getRect(canvas: Canvas) {
21-
const availableWidth = layout.toDevicePixels(canvas.getWidth());
22-
const availableHeight = layout.toDevicePixels(canvas.getHeight());
19+
const availableWidth = Utils.layout.toDevicePixels(canvas.getWidth());
20+
const availableHeight = Utils.layout.toDevicePixels(canvas.getHeight());
2321
const rect = createRectF(
24-
layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.left)),
25-
layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.top)),
26-
layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.width, 0, availableWidth)),
27-
layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.height, 0, availableHeight))
22+
Utils.layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.left)),
23+
Utils.layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.top)),
24+
Utils.layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.width, 0, availableWidth)),
25+
Utils.layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.height, 0, availableHeight))
2826
);
2927
return rect;
3028
}

src/shapes/shape.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ export default abstract class Shape extends Observable {
224224
@colorProperty({
225225
converter: parseShadow,
226226
paintSetter: applyShadow
227-
})
228-
shadow: Shadow;
227+
}) shadow: Shadow;
229228
@stringProperty({ nonPaintProp: true }) visibility: CoreTypes.VisibilityType = 'visible';
230229
@stringProperty({ nonPaintProp: true }) horizontalAlignment: CoreTypes.HorizontalAlignmentType & 'middle';
231230
@stringProperty({ nonPaintProp: true }) verticalAlignment: CoreTypes.VerticalAlignmentType & 'center';

src/shapes/text.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import { PercentLength } from '@nativescript/core/ui/styling/style-properties';
2-
import { layout } from '@nativescript/core/utils/layout-helper';
1+
import { PercentLength, Utils } from '@nativescript/core';
32
import { Canvas } from '../canvas';
43
import Rectangle from './rectangle';
54
import { stringProperty } from './shape';
65

76
export default class Text extends Rectangle {
87
drawOnCanvas(canvas: Canvas) {
9-
const availableWidth = layout.toDevicePixels(canvas.getWidth());
10-
const availableHeight = layout.toDevicePixels(canvas.getHeight());
8+
const availableWidth = Utils.layout.toDevicePixels(canvas.getWidth());
9+
const availableHeight = Utils.layout.toDevicePixels(canvas.getHeight());
1110
canvas.drawText(
1211
this.text,
13-
layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.left, 0, availableWidth)),
14-
layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.top, 0, availableHeight)),
12+
Utils.layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.left, 0, availableWidth)),
13+
Utils.layout.toDeviceIndependentPixels(PercentLength.toDevicePixels(this.top, 0, availableHeight)),
1514
this.paint
1615
);
1716
}

src/utils.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
2-
import { Color } from '@nativescript/core/color';
3-
import { Length } from '@nativescript/core/ui/styling/style-properties';
1+
import { Color, Length } from '@nativescript/core';
42
import { Cap, DashPathEffect, Join, Style } from './canvas';
53

64
export function parseCap(value: string | number) {
@@ -59,7 +57,7 @@ export function parseShadow(value: string) {
5957
radius: hasRadius ? Length.parse(args[2]) : 3,
6058
dx: Length.toDevicePixels(Length.parse(args[0])),
6159
dy: Length.toDevicePixels(Length.parse(args[1])),
62-
color: new Color(args[hasRadius ? 3 : 2]),
60+
color: new Color(args[hasRadius ? 3 : 2])
6361
};
6462
}
6563

0 commit comments

Comments
 (0)