Skip to content

Commit e74ab0a

Browse files
authored
Adds AnimatedImage component (#79)
1 parent 122543c commit e74ab0a

File tree

8 files changed

+136
-15
lines changed

8 files changed

+136
-15
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"react": "^16.9.0"
3232
},
3333
"devDependencies": {
34-
"@nodegui/nodegui": "^0.6.9",
34+
"@nodegui/nodegui": "^0.7.0",
3535
"@types/node": "^12.0.10",
3636
"prettier": "^1.18.2",
3737
"react": "^16.9.0",
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { QLabel, NodeWidget, QMovie, QSize } from "@nodegui/nodegui";
2+
import { TextProps, setTextProps } from "../Text/RNText";
3+
import { RNWidget } from "../config";
4+
import { throwUnsupported } from "../../utils/helpers";
5+
6+
export interface AnimatedImageProps extends TextProps {
7+
src?: string;
8+
}
9+
10+
const setAnimatedImageProps = (
11+
widget: RNAnimatedImage,
12+
newProps: AnimatedImageProps,
13+
oldProps: AnimatedImageProps
14+
) => {
15+
const setter: AnimatedImageProps = {
16+
set src(imageUrl: string) {
17+
if (!imageUrl) {
18+
return;
19+
}
20+
const movie = new QMovie();
21+
movie.setFileName(imageUrl);
22+
widget.setMovie(movie);
23+
const size = widget.size();
24+
movie.setScaledSize(size);
25+
}
26+
};
27+
Object.assign(setter, newProps);
28+
setTextProps(widget, newProps, oldProps);
29+
};
30+
31+
/**
32+
* @ignore
33+
*/
34+
export class RNAnimatedImage extends QLabel implements RNWidget {
35+
setProps(newProps: AnimatedImageProps, oldProps: AnimatedImageProps): void {
36+
setAnimatedImageProps(this, newProps, oldProps);
37+
}
38+
appendInitialChild(child: NodeWidget): void {
39+
throwUnsupported(this);
40+
}
41+
appendChild(child: NodeWidget): void {
42+
throwUnsupported(this);
43+
}
44+
insertBefore(child: NodeWidget, beforeChild: NodeWidget): void {
45+
throwUnsupported(this);
46+
}
47+
removeChild(child: NodeWidget): void {
48+
throwUnsupported(this);
49+
}
50+
static tagName = "animatedimage";
51+
scaleMovie(size: QSize) {
52+
const movie = this.movie();
53+
movie?.setScaledSize(size);
54+
}
55+
}

src/components/AnimatedImage/index.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Fiber } from "react-reconciler";
2+
import { registerComponent, ComponentConfig } from "../config";
3+
import { RNAnimatedImage, AnimatedImageProps } from "./RNAnimatedImage";
4+
import { AppContainer } from "../../reconciler";
5+
import { QLabelEvents } from "@nodegui/nodegui";
6+
7+
class AnimatedImageConfig extends ComponentConfig {
8+
tagName = RNAnimatedImage.tagName;
9+
shouldSetTextContent(nextProps: AnimatedImageProps): boolean {
10+
return true;
11+
}
12+
createInstance(
13+
newProps: AnimatedImageProps,
14+
rootInstance: AppContainer,
15+
context: any,
16+
workInProgress: Fiber
17+
): RNAnimatedImage {
18+
const widget = new RNAnimatedImage();
19+
widget.setProps(newProps, {});
20+
widget.movie()?.start();
21+
widget.addEventListener(QLabelEvents.Resize, () => {
22+
widget.scaleMovie(widget.size());
23+
});
24+
return widget;
25+
}
26+
commitMount(
27+
instance: RNAnimatedImage,
28+
newProps: AnimatedImageProps,
29+
internalInstanceHandle: any
30+
): void {
31+
if (newProps.visible !== false) {
32+
instance.show();
33+
}
34+
return;
35+
}
36+
commitUpdate(
37+
instance: RNAnimatedImage,
38+
updatePayload: any,
39+
oldProps: AnimatedImageProps,
40+
newProps: AnimatedImageProps,
41+
finishedWork: Fiber
42+
): void {
43+
instance.setProps(newProps, oldProps);
44+
}
45+
}
46+
47+
export const AnimatedImage = registerComponent<AnimatedImageProps>(
48+
new AnimatedImageConfig()
49+
);

src/components/Image/RNImage.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { QLabel, QPixmap, AspectRatioMode, NodeWidget } from "@nodegui/nodegui";
1+
import {
2+
QLabel,
3+
QPixmap,
4+
AspectRatioMode,
5+
NodeWidget,
6+
QSize
7+
} from "@nodegui/nodegui";
28
import { TextProps, setTextProps } from "../Text/RNText";
39
import { RNWidget } from "../config";
410
import { throwUnsupported } from "../../utils/helpers";
@@ -21,7 +27,7 @@ const setImageProps = (
2127
const pixMap = new QPixmap(imageUrl);
2228
widget.setPixmap(pixMap);
2329
const size = widget.size();
24-
widget.scalePixmap(size.width(), size.height());
30+
widget.scalePixmap(size);
2531
},
2632
set aspectRatioMode(mode: AspectRatioMode) {
2733
widget.setAspectRatioMode(mode);
@@ -58,15 +64,19 @@ export class RNImage extends QLabel implements RNWidget {
5864
super.setPixmap(pixmap);
5965
this.originalPixmap = pixmap;
6066
};
61-
setAspectRatioMode = (mode: AspectRatioMode) => {
67+
setAspectRatioMode(mode: AspectRatioMode) {
6268
// react:✓ TODO://getter
6369
this.aspectRatioMode = mode;
64-
};
65-
scalePixmap = (width: number, height: number) => {
70+
}
71+
scalePixmap(size: QSize) {
6672
if (this.originalPixmap) {
6773
return super.setPixmap(
68-
this.originalPixmap.scaled(width, height, this.aspectRatioMode)
74+
this.originalPixmap.scaled(
75+
size.width(),
76+
size.height(),
77+
this.aspectRatioMode
78+
)
6979
);
7080
}
71-
};
81+
}
7282
}

src/components/Image/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ class ImageConfig extends ComponentConfig {
1717
const widget = new RNImage();
1818
widget.setProps(newProps, {});
1919
widget.addEventListener(QLabelEvents.Resize, () => {
20-
const size = widget.size();
21-
widget.scalePixmap(size.width(), size.height());
20+
widget.scalePixmap(widget.size());
2221
});
2322
return widget;
2423
}

src/demo.tsx

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

45
const App = () => {
56
return (
67
<Window>
78
<View style={containerStyle}>
8-
<Button style={buttonStyle} text={"Hello"} />
9-
<Button style={buttonStyle} text={"World"} />
9+
<View>
10+
<Button style={buttonStyle} text={"Hello"} />
11+
<Button style={buttonStyle} text={"World"} />
12+
</View>
13+
<AnimatedImage
14+
style="width:200px; height: 150px;"
15+
src="/Users/atulr/Project/nodegui/nodegui/src/lib/QtGui/__tests__/assets/fine.gif"
16+
/>
1017
</View>
1118
</Window>
1219
);

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export { View } from "./components/View";
33
export { Window } from "./components/Window";
44
export { Text } from "./components/Text";
55
export { Image } from "./components/Image";
6+
export { AnimatedImage } from "./components/AnimatedImage";
67
export { Button } from "./components/Button";
78
export { CheckBox } from "./components/CheckBox";
89
export { LineEdit } from "./components/LineEdit";

0 commit comments

Comments
 (0)