forked from nodegui/react-nodegui
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRNImage.ts
More file actions
113 lines (109 loc) · 2.91 KB
/
RNImage.ts
File metadata and controls
113 lines (109 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import {
QLabel,
QPixmap,
AspectRatioMode,
NodeWidget,
QSize,
TransformationMode
} from "@nodegui/nodegui";
import { TextProps, setTextProps } from "../Text/RNText";
import { RNWidget } from "../config";
import { throwUnsupported, isValidUrl } from "../../utils/helpers";
import phin from "phin";
export interface ImageProps extends TextProps {
src?: string;
aspectRatioMode?: AspectRatioMode;
transformationMode?: TransformationMode;
buffer?: Buffer;
}
const setImageProps = (
widget: RNImage,
newProps: ImageProps,
oldProps: ImageProps
) => {
const setter: ImageProps = {
set src(imageUrlOrPath: string) {
if (!imageUrlOrPath) {
return;
}
getLoadedPixmap(imageUrlOrPath)
.then(pixmap => widget.setPixmap(pixmap))
.catch(console.warn);
},
set buffer(imageBuffer: Buffer) {
const pixMap = new QPixmap();
pixMap.loadFromData(imageBuffer);
widget.setPixmap(pixMap);
},
set aspectRatioMode(mode: AspectRatioMode) {
widget.setAspectRatioMode(mode);
},
set transformationMode(mode: TransformationMode) {
widget.setTransformationMode(mode);
}
};
Object.assign(setter, newProps);
setTextProps(widget, newProps, oldProps);
};
/**
* @ignore
*/
export class RNImage extends QLabel implements RNWidget {
setProps(newProps: ImageProps, oldProps: ImageProps): void {
setImageProps(this, newProps, oldProps);
}
appendInitialChild(child: NodeWidget<any>): void {
throwUnsupported(this);
}
appendChild(child: NodeWidget<any>): void {
throwUnsupported(this);
}
insertBefore(child: NodeWidget<any>, beforeChild: NodeWidget<any>): void {
throwUnsupported(this);
}
removeChild(child: NodeWidget<any>): void {
throwUnsupported(this);
}
static tagName = "image";
originalPixmap?: QPixmap;
aspectRatioMode?: AspectRatioMode;
transformationMode?: TransformationMode;
setPixmap = (pixmap: QPixmap) => {
// react:✓
super.setPixmap(pixmap);
this.originalPixmap = pixmap;
};
setAspectRatioMode(mode: AspectRatioMode) {
// react:✓ TODO://getter
this.aspectRatioMode = mode;
this.scalePixmap(this.size());
}
setTransformationMode(mode: TransformationMode) {
// react:✓ TODO://getter
this.transformationMode = mode;
this.scalePixmap(this.size());
}
scalePixmap(size: QSize) {
if (this.originalPixmap) {
return super.setPixmap(
this.originalPixmap.scaled(
size.width(),
size.height(),
this.aspectRatioMode,
this.transformationMode
)
);
}
}
}
async function getLoadedPixmap(imageUrlOrPath: string): Promise<QPixmap> {
const pixMap = new QPixmap();
if (isValidUrl(imageUrlOrPath)) {
const res = await phin(imageUrlOrPath);
const imageBuffer = Buffer.from(res.body);
pixMap.loadFromData(imageBuffer);
} else {
pixMap.load(imageUrlOrPath);
}
return pixMap;
}