Skip to content

Commit 7a9ab8b

Browse files
committed
feat(ui-canvaslabel): CanvasLabel now is a GridLayout to allow adding subviews.
1 parent 6833723 commit 7a9ab8b

File tree

8 files changed

+167
-58
lines changed

8 files changed

+167
-58
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
import groovy.json.JsonSlurper
12
dependencies {
3+
implementation(name:'widgets-release', ext:'aar')
24
implementation "androidx.annotation:annotation:1.1.0"
5+
}
6+
repositories {
7+
def widgetsDir = "$USER_PROJECT_ROOT/node_modules/@nativescript/core/platforms/android"
8+
def appPackageJsonFile = file("$USER_PROJECT_ROOT/package.json")
9+
if (appPackageJsonFile.exists()) {
10+
def appPackageJson = new JsonSlurper().parseText(appPackageJsonFile.text)
11+
if (appPackageJson.dependencies['@akylas/nativescript'] != null) {
12+
widgetsDir = "$USER_PROJECT_ROOT/node_modules/@akylas/nativescript/platforms/android"
13+
}
14+
}
15+
flatDir {
16+
dirs "$widgetsDir"
17+
}
318
}

packages/ui-canvas/platforms/android/java/com/akylas/canvas/CanvasView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import android.view.View;
55
import android.graphics.Canvas;
66

7-
public class CanvasView extends View {
7+
public class CanvasView extends org.nativescript.widgets.GridLayout {
88
public SizeChangedListener sizeChangedListener = null;
99
public DrawListener drawListener = null;
1010

src/ui-canvas/index.android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ class CanvasView extends CanvasBase {
532532
}
533533
canvas.drawBitmap(shapeCanvas.getImage() as android.graphics.Bitmap, 0, 0, this.shapePaint);
534534
} else if (!this.cached) {
535-
const shapes = this.mShapes;
535+
const shapes = this.shapes;
536536
const width = canvas.getWidth();
537537
const height = canvas.getHeight();
538538
if (shapes && shapes.length > 0) {

src/ui-canvas/index.common.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangedData, Observable, ObservableArray, Property, Screen, Utils, View, booleanConverter, colorProperty } from '@nativescript/core';
1+
import { ChangedData, GridLayout, Observable, ObservableArray, Property, Screen, Utils, View, booleanConverter, colorProperty } from '@nativescript/core';
22
import { Canvas, Rect, RectF } from '.';
33
import Shape from './shapes/shape';
44

@@ -8,6 +8,12 @@ import Shape from './shapes/shape';
88
// }
99
// }
1010

11+
declare module '@nativescript/core/ui/core/view' {
12+
interface ViewCommon {
13+
_addChildFromBuilder(name: string, value: any);
14+
}
15+
}
16+
1117
export function createRect(x: number, y: number, w: number, h: number) {
1218
return new Rect(x, y, x + w, y + h);
1319
}
@@ -36,7 +42,7 @@ function throttle(fn, limit) {
3642
}
3743
};
3844
}
39-
export abstract class CanvasBase extends View {
45+
export abstract class CanvasBase extends GridLayout {
4046
// Declare events as static variables, so that they can be set in NativeScript Core XML
4147
public static drawEvent = 'draw';
4248

@@ -93,6 +99,14 @@ export abstract class CanvasBase extends View {
9399
public addShape(shape: Shape) {
94100
this.getOrCreateShapes().push(shape);
95101
}
102+
public insertShape(shape: Shape, atIndex: number) {
103+
const shapes = this.getOrCreateShapes();
104+
if (atIndex >= shapes.length) {
105+
this.addShape(shape);
106+
} else {
107+
shapes.splice(atIndex, 0, shape);
108+
}
109+
}
96110

97111
public removeShape(shape: Shape) {
98112
if (this.mShapes) {
@@ -104,21 +118,34 @@ export abstract class CanvasBase extends View {
104118
}
105119

106120
public _addArrayFromBuilder(name: string, value: any[]) {
107-
value.forEach((v) => {
108-
this._addChildFromBuilder(null, value);
109-
});
121+
if (name === 'shapes') {
122+
value.forEach((v) => {
123+
this._addChildFromBuilder(null, value);
124+
});
125+
}
110126
// we ignore any other kind of view.
111127
}
112128

113129
public _addChildFromBuilder(name: string, value: any): void {
114130
if (value instanceof Shape) {
115131
this.addShape(value);
132+
} else {
133+
super._addChildFromBuilder(name, value);
116134
}
117135
// we ignore any other kind of view.
118136
}
119137
public _removeView(view: any) {
120138
if (view instanceof Shape) {
121139
this.removeShape(view);
140+
} else {
141+
super._removeView(view);
142+
}
143+
}
144+
public _addView(view: any) {
145+
if (view instanceof Shape) {
146+
this.addShape(view);
147+
} else {
148+
super._addView(view);
122149
}
123150
}
124151

src/ui-canvas/index.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/adjacent-overload-signatures */
22
/* eslint-disable @typescript-eslint/unified-signatures */
3-
import { Color, ImageSource, ObservableArray, View } from '@nativescript/core';
3+
import { Color, GridLayout, ImageSource, ObservableArray, View } from '@nativescript/core';
44
import { Font, FontStyle, FontStyleType, FontWeight, FontWeightType } from '@nativescript/core/ui/styling/font';
55
import Shape from './shapes/shape';
66

@@ -323,20 +323,24 @@ export class DashPathEffect extends android.graphics.DashPathEffect {
323323
// return android.graphics.Paint.Join;
324324
// }
325325

326-
declare class CanvasView extends View {
326+
declare class CanvasView extends GridLayout {
327327
cached: boolean;
328328
hardwareAccelerated: boolean;
329329
density: number;
330330
drawFrameRate: boolean;
331331

332332
shapes: ObservableArray<Shape>;
333+
// mShapes: ObservableArray<Shape>;
333334

334335
onDraw(canvas: Canvas);
335336
addShape(shape: Shape);
337+
insertShape(shape: Shape, atIndex: number);
336338
removeShape(shape: Shape);
337339
redraw();
338340
invalidate();
339341
onSizeChanged(w: number, h: number, oldw: number, oldh: number);
342+
343+
public _addArrayFromBuilder(name: string, value: any[]);
340344
}
341345

342346
export function createImage(options: { width: number; height: number; scale?: number; config?: any }): ImageSource;

src/ui-canvaslabel/canvaslabel.android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Paint } from '@nativescript-community/ui-canvas';
22
import { Color, getTransformedText, profile } from '@nativescript/core';
33
import { FontWeight, FontWeightType } from '@nativescript/core/ui/styling/font';
4-
import { CanvasLabel as CanvasLabelBase, Group as GroupBase, Span as SpanBase, paintFontCache } from './canvaslabel.common';
4+
import { CanvasLabel as CanvasLabelBase, Group as GroupBase, SpanBase, paintFontCache } from './canvaslabel.common';
55
import { createNativeAttributedString, createSpannable, typefaceCache } from '@nativescript-community/text';
66

77
export class Span extends SpanBase {

0 commit comments

Comments
 (0)