@dark-engine/platform-desktop
Version:
Dark renderer to desktop platforms like Windows, Linux, macOS via Nodegui and Qt
59 lines (58 loc) • 1.69 kB
JavaScript
import { QLabel, QPixmap, AspectRatioMode, TransformationMode } from '@nodegui/nodegui';
import { component } from '@dark-engine/core';
import fetch from 'node-fetch';
import { qImage } from '../factory';
import { detectisValidURL } from '../utils';
const Image = component(props => qImage(props), { displayName: 'Image' });
class QDarkImage extends QLabel {
aspectRatioMode = AspectRatioMode.KeepAspectRatio;
transformationMode = TransformationMode.FastTransformation;
constructor() {
super();
this.setProperty('scaledContents', true);
}
async setSrc(value) {
if (!value) return;
try {
this.setPixmap(this.scale(await createPixmapFromPath(value)));
} catch (error) {
console.warn(error);
}
}
setBuffer(buffer) {
this.setPixmap(createPixmapFromBuffer(buffer));
}
setAspectRatioMode(mode) {
this.aspectRatioMode = mode;
this.fit();
}
setTransformationMode(mode) {
this.transformationMode = mode;
this.fit();
}
fit() {
if (!this.pixmap()) return;
this.setPixmap(this.scale(this.pixmap()));
}
scale(pixmap) {
return pixmap.scaled(this.width(), this.height(), this.aspectRatioMode, this.transformationMode);
}
}
async function createPixmapFromPath(src) {
const pixmap = new QPixmap();
if (detectisValidURL(src)) {
const response = await fetch(src);
const buffer = Buffer.from(await response.arrayBuffer());
pixmap.loadFromData(buffer);
} else {
pixmap.load(src);
}
return pixmap;
}
function createPixmapFromBuffer(buffer) {
const pixmap = new QPixmap();
pixmap.loadFromData(buffer);
return pixmap;
}
export { Image, QDarkImage };
//# sourceMappingURL=image.js.map