Skip to content

Commit 1d77d16

Browse files
committed
Added image loading support from url
1 parent 7207ca9 commit 1d77d16

File tree

17 files changed

+95
-114
lines changed

17 files changed

+95
-114
lines changed

package-lock.json

Lines changed: 43 additions & 94 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
"@angular/platform-browser": "~8.2.11",
2929
"@angular/platform-browser-dynamic": "~8.2.11",
3030
"@angular/router": "~8.2.11",
31-
"@nodegui/nodegui": "^0.2.1",
31+
"@nodegui/nodegui": "^0.8.0",
3232
"core-js": "^3.3.2",
3333
"node-fetch": "^2.6.0",
34+
"phin": "^3.4.1",
3435
"rxjs": "~6.4.0",
3536
"tslib": "^1.10.0",
3637
"zone.js": "^0.9.1"

projects/angular-nodegui/src/lib/components/button.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class NgButton extends QPushButton implements NgComponent {
3333
}
3434
}
3535

36-
public setProperty(name: string, value: boolean | number): void {
36+
public setNgProperty(name: string, value: boolean | number): void {
3737
switch (name) {
3838
case 'enabled':
3939
this.setEnabled(value as boolean);

projects/angular-nodegui/src/lib/components/checkbox.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class NgCheckbox extends QCheckBox implements NgComponent {
3333
}
3434
}
3535

36-
public setProperty(name: string, value: boolean): void {
36+
public setNgProperty(name: string, value: boolean): void {
3737
switch (name) {
3838
case 'checked':
3939
this.setChecked(value);

projects/angular-nodegui/src/lib/components/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface NgComponent {
77

88
setNgAttribute(name: string, value: string, namespace?: string | null): void;
99

10-
setProperty(name: string, value: any): void;
10+
setNgProperty(name: string, value: any): void;
1111

1212
setStyle(style: string, value: any, flags?: RendererStyleFlags2): void;
1313

projects/angular-nodegui/src/lib/components/dial.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class NgDial extends QDial implements NgComponent {
2727
}
2828
}
2929

30-
public setProperty(name: string, value: boolean | number): void {
30+
public setNgProperty(name: string, value: boolean | number): void {
3131
switch (name) {
3232
case 'enabled':
3333
this.setEnabled(value as boolean);

projects/angular-nodegui/src/lib/components/image.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { RendererStyleFlags2 } from '@angular/core';
12
import { QLabel, QPixmap, AspectRatioMode } from '@nodegui/nodegui';
3+
import * as phin from 'phin';
24
import { NgComponent } from './component';
3-
import { RendererStyleFlags2 } from '@angular/core';
45

56
export class NgImage extends QLabel implements NgComponent {
67
public static nodeName = 'image';
@@ -30,9 +31,9 @@ export class NgImage extends QLabel implements NgComponent {
3031
}
3132
}
3233

33-
public setProperty(
34+
public setNgProperty(
3435
name: string,
35-
value: string | boolean | AspectRatioMode
36+
value: string | boolean | AspectRatioMode | Buffer
3637
): void {
3738
switch (name) {
3839
case 'enabled':
@@ -42,13 +43,21 @@ export class NgImage extends QLabel implements NgComponent {
4243
if (!value) {
4344
return;
4445
}
45-
const pixMap = new QPixmap(value as string);
46+
getLoadedPixmap(value as string)
47+
.then(pixmap => this.setPixmap(pixmap))
48+
.catch(console.warn);
4649

47-
this.setPixmap(pixMap);
4850
// TODO: not set current aspect size
4951
// const size = this.size();
5052
// this.scalePixmap(size.width, size.height);
5153
break;
54+
55+
case 'buffer':
56+
const pixMap = new QPixmap();
57+
pixMap.loadFromData(value as Buffer);
58+
this.setPixmap(pixMap);
59+
break;
60+
5261
case 'aspectRatioMode':
5362
this.setAspectRatioMode(value as AspectRatioMode);
5463
break;
@@ -100,3 +109,25 @@ export class NgImage extends QLabel implements NgComponent {
100109
throw new Error('Method not implemented.');
101110
}
102111
}
112+
113+
async function getLoadedPixmap(imageUrlOrPath: string): Promise<QPixmap> {
114+
const pixMap = new QPixmap();
115+
if (isValidUrl(imageUrlOrPath)) {
116+
const res = await phin(imageUrlOrPath);
117+
const imageBuffer = Buffer.from(res.body);
118+
pixMap.loadFromData(imageBuffer);
119+
} else {
120+
pixMap.load(imageUrlOrPath);
121+
}
122+
return pixMap;
123+
}
124+
125+
export function isValidUrl(str: string) {
126+
try {
127+
// tslint:disable-next-line:no-unused-expression
128+
new URL(str);
129+
return true;
130+
} catch (_) {
131+
return false;
132+
}
133+
}

projects/angular-nodegui/src/lib/components/line-edit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class NgLineEdit extends QLineEdit implements NgComponent {
2727
}
2828
}
2929

30-
public setProperty(name: string, value: boolean | string): void {
30+
public setNgProperty(name: string, value: boolean | string): void {
3131
switch (name) {
3232
case 'text':
3333
value ? this.setText(value as string) : this.clear();

projects/angular-nodegui/src/lib/components/pline-text-edit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class NgPlainTextEdit extends QPlainTextEdit implements NgComponent {
2727
}
2828
}
2929

30-
public setProperty(name: string, value: boolean | string): void {
30+
public setNgProperty(name: string, value: boolean | string): void {
3131
switch (name) {
3232
case 'text':
3333
value ? this.setPlainText(value as string) : this.clear();

projects/angular-nodegui/src/lib/components/progress-bar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class NgProgressBar extends QProgressBar implements NgComponent {
1818
namespace?: string | null
1919
): void {}
2020

21-
public setProperty(name: string, value: any): void {
21+
public setNgProperty(name: string, value: any): void {
2222
switch (name) {
2323
case 'value':
2424
this.setValue(value);

0 commit comments

Comments
 (0)