Skip to content

Commit 753d691

Browse files
committed
chore: updates
1 parent 3f178fa commit 753d691

File tree

38 files changed

+1293
-1068
lines changed

38 files changed

+1293
-1068
lines changed

crates/canvas-2d/src/context/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ pub enum SurfaceEngine {
136136
}
137137

138138

139+
#[derive(Debug, Copy, Clone)]
139140
pub struct SurfaceData {
140141
pub(crate) bounds: skia_safe::Rect,
141142
pub(crate) scale: f32,

crates/canvas-2d/src/context/surface.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Context {
3535
};
3636

3737
let info = ImageInfo::new(
38-
ISize::new((width * density).floor() as i32, (height * density).floor() as i32),
38+
ISize::new(width as i32, height as i32),
3939
color_type,
4040
alpha_type,
4141
None,
@@ -124,7 +124,7 @@ impl Context {
124124
return;
125125
}
126126
let info = ImageInfo::new(
127-
ISize::new((width as f32 * density).floor() as i32, (height as f32 * density).floor() as i32),
127+
ISize::new(width as i32, height as i32),
128128
ColorType::RGBA8888,
129129
AlphaType::Unknown,
130130
None,

crates/canvas-2d/src/context/surface_gl.rs

+2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ impl Context {
180180
0,
181181
frame_buffer,
182182
);
183+
183184
let surface_props = skia_safe::SurfaceProps::new(
184185
skia_safe::SurfacePropsFlags::default(),
185186
PixelGeometry::Unknown,
@@ -203,6 +204,7 @@ impl Context {
203204
surface
204205
};
205206

207+
206208
if let Some(surface) = surface {
207209
context.direct_context = direct_context;
208210
context.surface_data.engine = engine;

crates/canvas-android/src/jni_compat/org_nativescript_canvas_NSCCanvas.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ pub extern "system" fn nativeUpdateGLSurface(
298298
unsafe {
299299
if let Some(window) = NativeWindow::from_surface(env.get_native_interface(), surface) {
300300
let handle = to_raw_window_handler(&window);
301+
301302
context.gl_context.set_window_surface(
302303
&mut context.contextAttributes,
303304
window.width(),
@@ -329,8 +330,7 @@ pub extern "system" fn nativeUpdate2DSurface(
329330
if let Some(window) = NativeWindow::from_surface(env.get_native_interface(), surface) {
330331
let width = window.width() as f32;
331332
let height = window.height() as f32;
332-
let density = context.get_context().density();
333-
context.resize((width / density).floor(), (height / density).floor())
333+
context.resize(width, height)
334334
}
335335
drop(env);
336336
}

crates/canvas-c/src/c2d/context.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,21 @@ fn to_data_url(context: &mut CanvasRenderingContext2D, format: &str, quality: u3
7070
#[cfg(feature = "gl")]
7171
pub fn resize_gl(context: &mut CanvasRenderingContext2D, width: f32, height: f32) {
7272
let alpha = context.alpha;
73+
let gl = &context.gl_context;
74+
context.make_current();
7375
let context = &mut context.context;
7476
let density = context.get_surface_data().scale();
7577
let ppi = context.get_surface_data().ppi();
7678

7779
let mut fb = [0];
7880

7981
unsafe {
82+
gl_bindings::Viewport(0, 0, width as i32, height as i32);
8083
gl_bindings::ClearColor(0., 0., 0., 0.);
8184
gl_bindings::Clear(gl_bindings::COLOR_BUFFER_BIT);
82-
gl_bindings::Viewport(0, 0, (width * density).floor() as i32, (height * density).floor() as i32);
8385
gl_bindings::GetIntegerv(gl_bindings::FRAMEBUFFER_BINDING, fb.as_mut_ptr());
8486
}
87+
gl.swap_buffers();
8588

8689
Context::resize_gl(context, width, height, density, fb[0], 0, alpha, ppi)
8790
}
@@ -197,7 +200,6 @@ impl CanvasRenderingContext2D {
197200
}
198201

199202
pub fn resize(&mut self, width: f32, height: f32) {
200-
self.gl_context.make_current();
201203
resize(self, width, height);
202204
}
203205

crates/canvas-core/src/gl/android.rs

+1
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ impl GLContext {
685685
NonZeroU32::try_from(height as u32).unwrap(),
686686
);
687687

688+
688689
let surface = display
689690
.create_window_surface(&config, &surface_attr)
690691
.map(SurfaceHelper::Window)

packages/canvas-pixi/index.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,29 @@ class NSCPIXIApplication extends Pixii.Application {
99
let clientWidth = 300;
1010
let clientHeight = 150;
1111
if (context) {
12-
clientWidth = context.canvas.width;
13-
clientHeight = context.canvas.height;
14-
}
15-
if (view) {
16-
clientWidth = view.width;
17-
clientHeight = view.height;
12+
clientWidth = context.canvas.width * Screen.mainScreen.scale;
13+
clientHeight = context.canvas.height * Screen.mainScreen.scale;
1814
}
1915
if (!view) {
2016
view = context.canvas.toHTMLCanvas();
2117
}
18+
19+
if (view) {
20+
clientWidth = view.clientWidth;
21+
clientHeight = view.clientHeight;
22+
}
23+
24+
view.width = view.clientWidth * Screen.mainScreen.scale;
25+
view.height = view.clientHeight * Screen.mainScreen.scale;
26+
2227
const width = props.width || clientWidth;
2328
const height = props.height || clientHeight;
2429

2530
// PIXI.settings.RESOLUTION = 1;
2631

2732
super({
2833
...props,
29-
resolution: 1,
34+
resolution: Screen.mainScreen.scale,
3035
view,
3136
width,
3237
height,

packages/canvas-polyfill/DOM/Element.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,14 @@ export class Element extends Node {
302302

303303
get innerWidth() {
304304
if (this.nativeElement) {
305-
return this.nativeElement['width'] as never;
305+
return this.nativeElement['innerWidth'] ?? (this.nativeElement['width'] as never);
306306
}
307307
return this['width'];
308308
}
309309

310310
get innerHeight() {
311311
if (this.nativeElement) {
312-
return this.nativeElement['height'] as never;
312+
return this.nativeElement['innerHeight'] ?? (this.nativeElement['height'] as never);
313313
}
314314
return this['height'];
315315
}

packages/canvas-polyfill/DOM/HTMLCanvasElement.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,28 @@ export class HTMLCanvasElement extends HTMLElement {
1919
if (!this.nativeElement.__domElement) {
2020
this.nativeElement.__domElement = new DOMParser().parseFromString('<canvas></canvas>', 'text/html').documentElement as never;
2121
}
22-
23-
this.style.nativeElement = new WeakRef(this.nativeElement);
2422
}
2523

2624
get _canvas() {
2725
return this.nativeElement;
2826
}
2927

28+
get innerWidth() {
29+
return this.clientWidth;
30+
}
31+
32+
get innerHeight() {
33+
return this.clientHeight;
34+
}
35+
36+
get clientWidth() {
37+
return this.nativeElement['clientWidth'] as never;
38+
}
39+
40+
get clientHeight() {
41+
return this.nativeElement['clientHeight'] as never;
42+
}
43+
3044
set width(value) {
3145
setValue(this.nativeElement, 'width', value);
3246
}

packages/canvas-polyfill/DOM/HTMLElement.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ export class Style {
2121
}
2222

2323
get width() {
24-
return 0;
24+
return this._values.get('width');
2525
}
2626
set width(value) {
27-
console.log('style.width', value);
27+
this._values.set('width', value);
2828
}
2929

3030
get height() {
31-
return 0;
31+
return this._values.get('height');
3232
}
3333
set height(value) {
34-
console.log('style.height', value);
34+
this._values.set('height', value);
3535
}
3636
}
3737

@@ -44,6 +44,9 @@ export class HTMLElement extends Element {
4444
}
4545

4646
get style() {
47+
if (this._nativeElement) {
48+
return this._nativeElement.style;
49+
}
4750
return this._style;
4851
}
4952

packages/canvas/Canvas/common.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CSSType, PercentLength, View, Utils, Property, booleanConverter, CoreTypes, Screen } from '@nativescript/core';
1+
import { CSSType, PercentLength, View, Utils, Property, booleanConverter, CoreTypes, Screen, CssProperty, Style } from '@nativescript/core';
22
import { CanvasRenderingContext } from '../common';
33

44
export interface ICanvasBase {
@@ -387,6 +387,7 @@ class Size {
387387
}
388388
}
389389

390+
390391
@CSSType('Canvas')
391392
export abstract class CanvasBase extends View implements ICanvasBase {
392393
public static readyEvent = 'ready';

packages/canvas/Canvas/index.android.ts

+42-23
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,9 @@ export class Canvas extends CanvasBase {
6363
this._canvas = new org.nativescript.canvas.NSCCanvas(activity);
6464
}
6565

66-
const textureView = this._canvas.getChildAt(0) as android.view.TextureView;
67-
68-
const matrix = new android.graphics.Matrix();
69-
matrix.setScale(Screen.mainScreen.scale, Screen.mainScreen.scale);
70-
//textureView.setTransform(matrix);
66+
// default canvas size
67+
this._canvas.setSurfaceWidth(300);
68+
this._canvas.setSurfaceHeight(150);
7169

7270
(global as any).__canvasLoaded = true;
7371
const ref = new WeakRef(this);
@@ -95,39 +93,61 @@ export class Canvas extends CanvasBase {
9593
}
9694

9795
get clientWidth() {
98-
return this.width;
96+
return this.getMeasuredWidth() / Screen.mainScreen.scale;
9997
}
10098

10199
get clientHeight() {
102-
return this.height;
100+
return this.getMeasuredHeight() / Screen.mainScreen.scale;
103101
}
104102

105103
get drawingBufferHeight() {
104+
if (this._canvas === undefined || this._canvas === null) {
105+
return 0;
106+
}
106107
return this._canvas.getDrawingBufferHeight();
107108
}
108109

109110
get drawingBufferWidth() {
111+
if (this._canvas === undefined || this._canvas === null) {
112+
return 0;
113+
}
110114
return this._canvas.getDrawingBufferWidth();
111115
}
112116

113117
// @ts-ignore
114-
get width(): any {
115-
return this._logicalSize.width;
118+
get width(): number {
119+
if (this._canvas === undefined || this._canvas === null) {
120+
return 0;
121+
}
122+
return this._canvas.getSurfaceWidth();
116123
}
117124

118-
set width(value) {
119-
this._didLayout = false;
120-
this._layoutNative();
125+
set width(value: number) {
126+
if (this._canvas === undefined || this._canvas === null) {
127+
return;
128+
}
129+
if (typeof value !== 'number') {
130+
return;
131+
}
132+
this._canvas.setSurfaceWidth(value);
121133
}
122134

123135
// @ts-ignore
124-
get height(): any {
125-
return this._logicalSize.height;
136+
get height(): number {
137+
if (this._canvas === undefined || this._canvas === null) {
138+
return 0;
139+
}
140+
return this._canvas.getSurfaceHeight();
126141
}
127142

128-
set height(value) {
129-
this._didLayout = false;
130-
this._layoutNative();
143+
set height(value: number) {
144+
if (this._canvas === undefined || this._canvas === null) {
145+
return;
146+
}
147+
if (typeof value !== 'number') {
148+
return;
149+
}
150+
this._canvas.setSurfaceHeight(value);
131151
}
132152

133153
static createCustomView() {
@@ -243,13 +263,12 @@ export class Canvas extends CanvasBase {
243263
return;
244264
}
245265

246-
const size = this._physicalSize;
247-
console.log(size);
248-
org.nativescript.canvas.NSCCanvas.layoutView(size.width || 0, size.height || 0, this._canvas);
266+
// const size = this._physicalSize;
267+
// org.nativescript.canvas.NSCCanvas.layoutView(size.width || 0, size.height || 0, this._canvas);
249268

250-
if (this._is2D) {
251-
this._2dContext.native.__resize(size.width, size.height);
252-
}
269+
// if (this._is2D) {
270+
// this._2dContext.native.__resize(size.width, size.height);
271+
// }
253272

254273
this._didLayout = true;
255274
}

packages/canvas/WebGPU/GPUDevice.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export class GPUDevice extends EventTarget {
210210
}) {
211211
descriptor.layout = descriptor?.layout?.[native_];
212212
if (Array.isArray(descriptor.entries)) {
213-
for (const entry of descriptor.entries) {
213+
descriptor.entries = descriptor.entries.map((entry) => {
214214
if (entry.resource instanceof GPUTextureView) {
215215
entry.resource = entry.resource[native_];
216216
} else if (entry.resource instanceof GPUSampler) {
@@ -220,7 +220,10 @@ export class GPUDevice extends EventTarget {
220220
} else if (entry?.resource?.buffer && entry?.resource?.buffer instanceof GPUBuffer) {
221221
entry.resource.buffer = entry.resource.buffer[native_];
222222
}
223-
}
223+
return entry;
224+
});
225+
226+
console.log(descriptor);
224227
}
225228

226229
const group = this.native.createBindGroup(descriptor);
Binary file not shown.

0 commit comments

Comments
 (0)