1+ import { RendererStyleFlags2 } from '@angular/core' ;
12import { QLabel , QPixmap , AspectRatioMode } from '@nodegui/nodegui' ;
3+ import * as phin from 'phin' ;
24import { NgComponent } from './component' ;
3- import { RendererStyleFlags2 } from '@angular/core' ;
45
56export 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+ }
0 commit comments