Skip to content

Commit a6bafe6

Browse files
authored
Added image loading support from url (#81)
1 parent bddb442 commit a6bafe6

File tree

11 files changed

+95
-24
lines changed

11 files changed

+95
-24
lines changed

package-lock.json

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

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
},
1717
"scripts": {
1818
"build": "tsc",
19-
"dev": "tsc && qode --inspect ./dist/demo.js",
19+
"dev": "tsc && qode ./dist/demo.js",
2020
"docs": "typedoc"
2121
},
2222
"dependencies": {
23+
"@types/react-reconciler": "^0.18.0",
24+
"phin": "^3.4.1",
2325
"react-deep-force-update": "^2.1.3",
2426
"react-proxy": "^2.0.8",
25-
"react-reconciler": "^0.24.0",
26-
"@types/react-reconciler": "^0.18.0"
27+
"react-reconciler": "^0.24.0"
2728
},
2829
"peerDependencies": {
2930
"@nodegui/nodegui": ">=0.3.0",

src/components/AnimatedImage/RNAnimatedImage.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { QLabel, NodeWidget, QMovie, QSize } from "@nodegui/nodegui";
22
import { TextProps, setTextProps } from "../Text/RNText";
33
import { RNWidget } from "../config";
4-
import { throwUnsupported } from "../../utils/helpers";
4+
import { throwUnsupported, isValidUrl } from "../../utils/helpers";
5+
import phin from "phin";
56

67
export interface AnimatedImageProps extends TextProps {
78
src?: string;
9+
buffer?: Buffer;
810
}
911

1012
const setAnimatedImageProps = (
@@ -13,15 +15,22 @@ const setAnimatedImageProps = (
1315
oldProps: AnimatedImageProps
1416
) => {
1517
const setter: AnimatedImageProps = {
16-
set src(imageUrl: string) {
17-
if (!imageUrl) {
18+
set src(imageUrlOrPath: string) {
19+
if (!imageUrlOrPath) {
1820
return;
1921
}
22+
getLoadedQMovie(imageUrlOrPath)
23+
.then(movie => {
24+
widget.setMovie(movie);
25+
widget.movie()?.start();
26+
})
27+
.catch(console.warn);
28+
},
29+
set buffer(imageBuffer: Buffer) {
2030
const movie = new QMovie();
21-
movie.setFileName(imageUrl);
31+
movie.loadFromData(imageBuffer);
2232
widget.setMovie(movie);
23-
const size = widget.size();
24-
movie.setScaledSize(size);
33+
widget.movie()?.start();
2534
}
2635
};
2736
Object.assign(setter, newProps);
@@ -53,3 +62,15 @@ export class RNAnimatedImage extends QLabel implements RNWidget {
5362
movie?.setScaledSize(size);
5463
}
5564
}
65+
66+
async function getLoadedQMovie(imageUrlOrPath: string): Promise<QMovie> {
67+
const movie = new QMovie();
68+
if (isValidUrl(imageUrlOrPath)) {
69+
const res = await phin(imageUrlOrPath);
70+
const imageBuffer = Buffer.from(res.body);
71+
movie.loadFromData(imageBuffer);
72+
} else {
73+
movie.setFileName(imageUrlOrPath);
74+
}
75+
return movie;
76+
}

src/components/AnimatedImage/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ class AnimatedImageConfig extends ComponentConfig {
1616
workInProgress: Fiber
1717
): RNAnimatedImage {
1818
const widget = new RNAnimatedImage();
19+
widget.setProperty("scaledContents", true);
1920
widget.setProps(newProps, {});
20-
widget.movie()?.start();
21-
widget.addEventListener(QLabelEvents.Resize, () => {
22-
widget.scaleMovie(widget.size());
23-
});
2421
return widget;
2522
}
2623
commitMount(

src/components/Image/RNImage.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import {
77
} from "@nodegui/nodegui";
88
import { TextProps, setTextProps } from "../Text/RNText";
99
import { RNWidget } from "../config";
10-
import { throwUnsupported } from "../../utils/helpers";
10+
import { throwUnsupported, isValidUrl } from "../../utils/helpers";
11+
import phin from "phin";
1112

1213
export interface ImageProps extends TextProps {
1314
src?: string;
1415
aspectRatioMode?: AspectRatioMode;
16+
buffer?: Buffer;
1517
}
1618

1719
const setImageProps = (
@@ -20,14 +22,18 @@ const setImageProps = (
2022
oldProps: ImageProps
2123
) => {
2224
const setter: ImageProps = {
23-
set src(imageUrl: string) {
24-
if (!imageUrl) {
25+
set src(imageUrlOrPath: string) {
26+
if (!imageUrlOrPath) {
2527
return;
2628
}
27-
const pixMap = new QPixmap(imageUrl);
29+
getLoadedPixmap(imageUrlOrPath)
30+
.then(pixmap => widget.setPixmap(pixmap))
31+
.catch(console.warn);
32+
},
33+
set buffer(imageBuffer: Buffer) {
34+
const pixMap = new QPixmap();
35+
pixMap.loadFromData(imageBuffer);
2836
widget.setPixmap(pixMap);
29-
const size = widget.size();
30-
widget.scalePixmap(size);
3137
},
3238
set aspectRatioMode(mode: AspectRatioMode) {
3339
widget.setAspectRatioMode(mode);
@@ -80,3 +86,15 @@ export class RNImage extends QLabel implements RNWidget {
8086
}
8187
}
8288
}
89+
90+
async function getLoadedPixmap(imageUrlOrPath: string): Promise<QPixmap> {
91+
const pixMap = new QPixmap();
92+
if (isValidUrl(imageUrlOrPath)) {
93+
const res = await phin(imageUrlOrPath);
94+
const imageBuffer = Buffer.from(res.body);
95+
pixMap.loadFromData(imageBuffer);
96+
} else {
97+
pixMap.load(imageUrlOrPath);
98+
}
99+
return pixMap;
100+
}

src/components/Image/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ImageConfig extends ComponentConfig {
1515
workInProgress: Fiber
1616
): RNImage {
1717
const widget = new RNImage();
18+
widget.setProperty("scaledContents", true);
1819
widget.setProps(newProps, {});
1920
widget.addEventListener(QLabelEvents.Resize, () => {
2021
widget.scalePixmap(widget.size());

src/components/ScrollArea/RNScrollArea.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export class RNScrollArea extends QScrollArea implements RNWidget {
3333
child.close();
3434
}
3535
appendInitialChild(child: NodeWidget): void {
36+
if (this.contentWidget) {
37+
console.warn("ScrollView can't have more than one child node");
38+
return;
39+
}
3640
this.setWidget(child);
3741
}
3842
appendChild(child: NodeWidget): void {

src/components/Text/RNText.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { throwUnsupported } from "../../utils/helpers";
66
export interface TextProps extends ViewProps {
77
children?: string | number;
88
wordWrap?: boolean;
9+
scaledContents?: boolean;
910
}
1011

1112
/**
@@ -22,6 +23,9 @@ export const setTextProps = (
2223
},
2324
set wordWrap(shouldWrap: boolean) {
2425
widget.setWordWrap(shouldWrap);
26+
},
27+
set scaledContents(scaled: boolean) {
28+
widget.setProperty("scaledContents", scaled);
2529
}
2630
};
2731
Object.assign(setter, newProps);

src/components/Window/RNWindow.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export class RNWindow extends QMainWindow implements RNWidget {
3131
child.close();
3232
}
3333
appendInitialChild(child: NodeWidget): void {
34+
if (this.centralWidget) {
35+
console.warn("MainWindow can't have more than one child node");
36+
return;
37+
}
3438
this.setCentralWidget(child);
3539
}
3640
appendChild(child: NodeWidget): void {

src/demo.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from "react";
2-
import { Renderer, Button, Window, View } from "./index";
3-
import { AnimatedImage } from "./components/AnimatedImage";
2+
import { Renderer, Button, Window, View, AnimatedImage } from "./index";
43

54
const App = () => {
65
return (
@@ -11,7 +10,7 @@ const App = () => {
1110
<Button style={buttonStyle} text={"World"} />
1211
</View>
1312
<AnimatedImage
14-
style="width:200px; height: 150px;"
13+
style="border: 1px solid blue; flex:1;"
1514
src="/Users/atulr/Project/nodegui/nodegui/src/lib/QtGui/__tests__/assets/fine.gif"
1615
/>
1716
</View>

0 commit comments

Comments
 (0)