Skip to content

Commit 6997099

Browse files
committed
Adds ImageLabel
1 parent ddc2a2e commit 6997099

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/components/Image/ImageLabel.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { QLabel, QPixmap } from "@nodegui/nodegui";
2+
3+
export class ImageLabel extends QLabel {
4+
originalPixmap?: QPixmap;
5+
setPixmap = (pixmap: QPixmap) => {
6+
super.setPixmap(pixmap);
7+
this.originalPixmap = pixmap;
8+
};
9+
scalePixmap = (width: number, height: number) => {
10+
if (this.originalPixmap) {
11+
return super.setPixmap(this.originalPixmap.scaled(width, height));
12+
}
13+
};
14+
}

src/components/Image/index.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { registerComponent } from "../config";
2+
import { QPixmap } from "@nodegui/nodegui";
3+
import { ViewProps, setProps as setViewProps } from "../View";
4+
import { ImageLabel } from "./ImageLabel";
5+
6+
interface ImageProps extends ViewProps {
7+
src?: string;
8+
}
9+
10+
const setProps = (
11+
widget: ImageLabel,
12+
newProps: ImageProps,
13+
oldProps: ImageProps
14+
) => {
15+
const setter: ImageProps = {
16+
set src(imageUrl: string) {
17+
const pixMap = new QPixmap(imageUrl);
18+
widget.setPixmap(pixMap);
19+
widget.scalePixmap(320, 120);
20+
}
21+
};
22+
Object.assign(setter, newProps);
23+
setViewProps(widget, newProps, oldProps);
24+
};
25+
26+
export const Image = registerComponent<ImageProps>({
27+
id: "image",
28+
getContext() {
29+
return {};
30+
},
31+
shouldSetTextContent: () => {
32+
return false;
33+
},
34+
createInstance: newProps => {
35+
const widget = new ImageLabel();
36+
setProps(widget, newProps, {});
37+
return widget;
38+
},
39+
finalizeInitialChildren: () => {
40+
return false;
41+
},
42+
commitMount: (instance, newProps, internalInstanceHandle) => {
43+
return;
44+
},
45+
prepareUpdate: (
46+
instance,
47+
oldProps,
48+
newProps,
49+
rootContainerInstance,
50+
hostContext
51+
) => {
52+
return true;
53+
},
54+
commitUpdate: (instance, updatePayload, oldProps, newProps, finishedWork) => {
55+
setProps(instance as ImageLabel, newProps, oldProps);
56+
}
57+
});

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export { CheckBox } from "./components/CheckBox";
66
export { LineEdit } from "./components/LineEdit";
77
export { ProgressBar } from "./components/ProgressBar";
88
export { RadioButton } from "./components/RadioButton";
9+
export { Image } from "./components/Image";
910
export { Window } from "./components/Window";

0 commit comments

Comments
 (0)