TeaSpeak-Client/modules/renderer/IconHelper.ts
2020-10-01 10:56:22 +02:00

49 lines
1.5 KiB
TypeScript

import * as electron from "electron";
import * as loader from "tc-loader";
import {Stage} from "tc-loader";
import {
spriteUrl as kClientSpriteUrl,
spriteWidth as kClientSpriteWidth,
spriteHeight as kClientSpriteHeight,
spriteEntries as kClientSpriteEntries
} from "svg-sprites/client-icons";
import {NativeImage} from "electron";
let nativeSprite: NativeImage;
export function clientIconClassToImage(klass: string) : NativeImage {
const sprite = kClientSpriteEntries.find(e => e.className === klass);
if(!sprite) return undefined;
return nativeSprite.crop({
height: sprite.height,
width: sprite.width,
x: sprite.xOffset,
y: sprite.yOffset
});
}
loader.register_task(Stage.JAVASCRIPT_INITIALIZING, {
priority: 100,
name: "native icon sprite loader",
function: async () => {
const image = new Image();
image.src = loader.config.baseUrl + kClientSpriteUrl;
await new Promise((resolve, reject) => {
image.onload = resolve;
image.onerror = () => reject("failed to load client icon sprite");
});
const canvas = document.createElement("canvas");
canvas.width = kClientSpriteWidth;
canvas.height = kClientSpriteHeight;
canvas.getContext("2d").drawImage(image, 0, 0);
nativeSprite = electron.remote.nativeImage.createFromDataURL( canvas.toDataURL());
}
})
export function finalize() {
nativeSprite = undefined;
}