Skip to content

Commit

Permalink
fix: crash
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Jan 12, 2024
1 parent c17adc3 commit 4420c36
Show file tree
Hide file tree
Showing 49 changed files with 387 additions and 394 deletions.
2 changes: 1 addition & 1 deletion packages/canvas-babylon/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-babylon",
"version": "2.0.0-alpha.41",
"version": "2.0.0-alpha.42",
"description": "",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-chartjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-chartjs",
"version": "2.0.0-alpha.41",
"version": "2.0.0-alpha.42",
"description": "ChartJS",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-media/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-media",
"version": "2.0.0-alpha.41",
"version": "2.0.0-alpha.42",
"description": "Canvas media",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-phaser-ce/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-phaser-ce",
"version": "2.0.0-alpha.41",
"version": "2.0.0-alpha.42",
"description": "Tools for using Phaser-ce to build native 2D games in NativeScript 👾",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-phaser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-phaser",
"version": "2.0.0-alpha.41",
"version": "2.0.0-alpha.42",
"description": "Build awesome 2D games with Phaser.js and NativeScript",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-pixi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-pixi",
"version": "2.0.0-alpha.41",
"version": "2.0.0-alpha.42",
"description": "Plugin for using pixi.js in NativeScript",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-polyfill/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-polyfill",
"version": "2.0.0-alpha.41",
"version": "2.0.0-alpha.42",
"description": "Polyfill for making NativeScript compatible with web libs like pixi.js, three.js, phaser.js, babylon.js, etc....",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-three/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-three",
"version": "2.0.0-alpha.41",
"version": "2.0.0-alpha.42",
"description": "Utilities for using THREE.js on NativeScript",
"main": "index",
"typings": "index.d.ts",
Expand Down
6 changes: 0 additions & 6 deletions packages/canvas/Canvas/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,6 @@ export abstract class CanvasBase extends View implements ICanvasBase {

public removeEventListener(arg: string, callback: any, thisArg?: any) {
super.removeEventListener(arg, callback, thisArg);
// if (WEB_GESTURE_EVENTS.indexOf(arg) !== -1) {
// // if we dont have any other web gestures we can unregister gestures
// if (!WEB_GESTURE_EVENTS.some((e) => this.hasListeners(e))) {
// this.__unregisterGestures();
// }
// }

const eventtype = arg.toLowerCase();

Expand Down
59 changes: 13 additions & 46 deletions packages/canvas/Canvas/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export class Canvas extends CanvasBase {
super();
const activity = Application.android.foregroundActivity || Application.android.startActivity || Utils.android.getApplicationContext();
this._canvas = new org.nativescript.canvas.NSCCanvas(activity);
this._canvas.setBoundsBuffer(this._boundingClientRect);
(global as any).__canvasLoaded = true;
const ref = new WeakRef(this);
this._canvas.setTouchEventListener(
Expand Down Expand Up @@ -378,7 +377,15 @@ export class Canvas extends CanvasBase {
return null;
}

private _boundingClientRect = new Float32Array(8);
private _jsBuffer: Float32Array;

private get _boundingClientRect() {
if (this._jsBuffer === undefined) {
this._jsBuffer = new Float32Array(8);
this._canvas.setBoundsBuffer(this._jsBuffer);
}
return this._jsBuffer;
}

getBoundingClientRect(): {
x: number;
Expand All @@ -390,51 +397,11 @@ export class Canvas extends CanvasBase {
bottom: number;
left: number;
} {
/*
getBoundingClientRect: 0.170ms
getBoundingClientRect: 0.027ms
getBoundingClientRect: 0.018ms
*/
if (!this._canvas) {
return new DOMRect(0, 0, 0, 0);
}
org.nativescript.canvas.NSCCanvas.getBoundingClientRect(this._canvas);
//org.nativescript.canvas.NSCCanvas.getBoundingClientRect(this._canvas, this._boundingClientRect);
const ret = new DOMRect(this._boundingClientRect[6], this._boundingClientRect[7], this._boundingClientRect[4], this._boundingClientRect[5], this._boundingClientRect[0], this._boundingClientRect[1], this._boundingClientRect[2], this._boundingClientRect[3]);

// const value = JSON.parse(org.nativescript.canvas.NSCCanvas.getBoundingClientRectJSON(this._canvas));

// const ret = new DOMRect(value.x, value.y,value.width, value.height, value.top, value.right, value.bottom, value.left);

// const ret = {
// bottom: this._boundingClientRect[2],
// height: this._boundingClientRect[5],
// left: this._boundingClientRect[3],
// right: this._boundingClientRect[1],
// top: this._boundingClientRect[0],
// width: this._boundingClientRect[4],
// x: this._boundingClientRect[6],
// y: this._boundingClientRect[7],
// };

// 0.141ms
// const view = this;
// const nativeView = view.android;
// const width = this.width;
// const height = this.height;
/*const ret = {
bottom: nativeView.getBottom() / Screen.mainScreen.scale,
height: height,
left: nativeView.getLeft() / Screen.mainScreen.scale,
right: nativeView.getRight() / Screen.mainScreen.scale,
top: nativeView.getTop() / Screen.mainScreen.scale,
width: width,
x: nativeView.getX() / Screen.mainScreen.scale,
y: nativeView.getY() / Screen.mainScreen.scale,
};*/

// const ret = new DOMRect(nativeView.getX() / Screen.mainScreen.scale, nativeView.getY() / Screen.mainScreen.scale, width, height, nativeView.getTop() / Screen.mainScreen.scale, nativeView.getRight() / Screen.mainScreen.scale, nativeView.getBottom() / Screen.mainScreen.scale, nativeView.getLeft() / Screen.mainScreen.scale);

return ret;
return new DOMRect(this._boundingClientRect[6], this._boundingClientRect[7], this._boundingClientRect[4], this._boundingClientRect[5], this._boundingClientRect[0], this._boundingClientRect[1], this._boundingClientRect[2], this._boundingClientRect[3]);
}

setPointerCapture() {}
Expand Down
60 changes: 13 additions & 47 deletions packages/canvas/Canvas/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class Canvas extends CanvasBase {

const size = this._realSize;

// todo revisit
// todo revisit

const width = Utils.layout.toDevicePixels(size.width || 1);
const height = Utils.layout.toDevicePixels(size.height || 1);
Expand Down Expand Up @@ -294,7 +294,6 @@ export class Canvas extends CanvasBase {
this._layoutNative();
const opts = { ...defaultOpts, ...this._handleContextOptions(type, options), fontColor: this.parent?.style?.color?.android || -16777216 };


const ctx = this._canvas.create2DContext(opts.alpha, opts.antialias, opts.depth, opts.failIfMajorPerformanceCaveat, opts.powerPreference, opts.premultipliedAlpha, opts.preserveDrawingBuffer, opts.stencil, opts.desynchronized, opts.xrCompatible, opts.fontColor);

this._2dContext = new (CanvasRenderingContext2D as any)(ctx);
Expand Down Expand Up @@ -378,7 +377,14 @@ export class Canvas extends CanvasBase {
return null;
}

private _boundingClientRect = new Float32Array(8);
private _jsBuffer: Float32Array;

private get _boundingClientRect() {
if (this._jsBuffer === undefined) {
this._jsBuffer = new Float32Array(8);
}
return this._jsBuffer;
}

getBoundingClientRect(): {
x: number;
Expand All @@ -390,52 +396,12 @@ export class Canvas extends CanvasBase {
bottom: number;
left: number;
} {
// console.time('getBoundingClientRect');

/*
getBoundingClientRect: 0.033ms
getBoundingClientRect: 0.026ms
getBoundingClientRect: 0.008ms
*/
if (!this._canvas) {
return new DOMRect(0, 0, 0, 0);
}

NSCCanvas.getBoundingClientRect(this._canvas, this._boundingClientRect);

const ret = new DOMRect(this._boundingClientRect[6], this._boundingClientRect[7], this._boundingClientRect[4], this._boundingClientRect[5], this._boundingClientRect[0], this._boundingClientRect[1], this._boundingClientRect[2], this._boundingClientRect[3]);

// const ret = {
// bottom: this._boundingClientRect[2],
// height: this._boundingClientRect[5],
// left: this._boundingClientRect[3],
// right: this._boundingClientRect[1],
// top: this._boundingClientRect[0],
// width: this._boundingClientRect[4],
// x: this._boundingClientRect[6],
// y: this._boundingClientRect[7],
// };

/*
getBoundingClientRect: 0.064ms
getBoundingClientRect: 0.049ms
getBoundingClientRect: 0.036ms
*/

// const view = this;
// const frame = view.ios.frame as CGRect;
// const width = view.width;
// const height = view.height;
// const ret = {
// bottom: height,
// height: height,
// left: frame.origin.x,
// right: width,
// top: frame.origin.y,
// width: width,
// x: frame.origin.x,
// y: frame.origin.y,
// };

// console.timeEnd('getBoundingClientRect');

return ret;
return new DOMRect(this._boundingClientRect[6], this._boundingClientRect[7], this._boundingClientRect[4], this._boundingClientRect[5], this._boundingClientRect[0], this._boundingClientRect[1], this._boundingClientRect[2], this._boundingClientRect[3]);
}
}
4 changes: 1 addition & 3 deletions packages/canvas/WebGL2/WebGL2RenderingContext/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ export class WebGL2RenderingContext extends WebGL2RenderingContextBase {

let direction = 0;
if (global.isAndroid) {
if (androidx.core.text.TextUtilsCompat.getLayoutDirectionFromLocale(java.util.Locale.getDefault()) === androidx.core.view.ViewCompat.LAYOUT_DIRECTION_RTL) {
direction = 1;
}
direction = (<any>org).nativescript.canvas.NSCCanvas.getDirection();
} else {
//direction = 1;
}
Expand Down
Loading

0 comments on commit 4420c36

Please sign in to comment.