Skip to content

Commit a63618f

Browse files
committed
Add image component
1 parent c2dd09b commit a63618f

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

src/app/app.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Component } from '@angular/core';
55
template: `
66
<window title="Angular app in window!">
77
<view>
8+
<image [src]="'C:/temp/iw.jpg'"></image>
89
<text [style.font-size.px]="45">Hello, {{ name }}</text>
910
<checkbox [checked]="true"></checkbox>
1011
<checkbox [checked]="true" [enabled]="false"></checkbox>

src/lib/components/components-map.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { NgText } from './text';
66
import { NgView } from './view';
77
import { NgCheckbox } from './checkbox';
88
import { NgDial } from './dial';
9+
import { NgImage } from './image';
910

1011
export type Constructable<T> = new () => T;
1112
export type NgComponentClass = Constructable<NgComponent>;
@@ -24,5 +25,6 @@ export class ComponentsMap {
2425
this.map.set(NgView.nodeName, NgView);
2526
this.map.set(NgCheckbox.nodeName, NgCheckbox);
2627
this.map.set(NgDial.nodeName, NgDial);
28+
this.map.set(NgImage.nodeName, NgImage);
2729
}
2830
}

src/lib/components/image.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { QLabel, QPixmap, AspectRatioMode } from '@nodegui/nodegui';
2+
import { NgComponent } from './component';
3+
import { RendererStyleFlags2 } from '@angular/core';
4+
5+
export class NgImage extends QLabel implements NgComponent {
6+
public static nodeName = 'image';
7+
public parent: any;
8+
9+
public originalPixmap?: QPixmap;
10+
public aspectRatioMode?: AspectRatioMode;
11+
12+
public appendChild(newChild: any): void {
13+
console.warn('image not supported appendChild');
14+
}
15+
16+
public insertBefore(newChild: any, refChild: any) {}
17+
18+
public setNgAttribute(
19+
name: string,
20+
value: string,
21+
namespace?: string | null
22+
): void {}
23+
24+
public setProperty(
25+
name: string,
26+
value: string | boolean | AspectRatioMode
27+
): void {
28+
switch (name) {
29+
case 'enabled':
30+
this.setEnabled(value as boolean);
31+
break;
32+
case 'src':
33+
if (!value) {
34+
return;
35+
}
36+
const pixMap = new QPixmap(value as string);
37+
this.setPixmap(pixMap);
38+
const size = this.size();
39+
40+
this.scalePixmap(size.width, size.height);
41+
break;
42+
case 'aspectRatioMode':
43+
this.setAspectRatioMode(value as AspectRatioMode);
44+
break;
45+
46+
default:
47+
break;
48+
}
49+
}
50+
51+
public setStyle(
52+
style: string,
53+
value: any,
54+
flags?: RendererStyleFlags2
55+
): void {
56+
this.setInlineStyle(`${style}:${value}`);
57+
}
58+
59+
public setValue(value: string): void {
60+
this.setText(value);
61+
}
62+
63+
public setAspectRatioMode(mode: AspectRatioMode) {
64+
this.aspectRatioMode = mode;
65+
}
66+
67+
public scalePixmap(width: number, height: number) {
68+
if (this.originalPixmap) {
69+
return super.setPixmap(
70+
this.originalPixmap.scaled(width, height, this.aspectRatioMode)
71+
);
72+
}
73+
}
74+
75+
public setPixmap(pixmap: QPixmap) {
76+
super.setPixmap(pixmap);
77+
this.originalPixmap = pixmap;
78+
}
79+
80+
removeAttribute(name: string, namespace?: string): void {
81+
throw new Error('Method not implemented.');
82+
}
83+
removeChild(oldChild: any): void {
84+
throw new Error('Method not implemented.');
85+
}
86+
removeClass(name: string): void {
87+
throw new Error('Method not implemented.');
88+
}
89+
removeStyle(style: string, flags?: RendererStyleFlags2): void {
90+
throw new Error('Method not implemented.');
91+
}
92+
}

0 commit comments

Comments
 (0)