TeaSpeak-Client/modules/renderer/IconHelper.ts

49 lines
1.4 KiB
TypeScript
Raw Normal View History

2019-10-25 19:51:40 -04:00
import * as electron from "electron";
import * as loader from "tc-loader";
import {Stage} from "tc-loader";
2019-10-25 19:51:40 -04:00
2020-08-09 09:03:52 -04:00
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
2019-10-25 19:51:40 -04:00
});
}
loader.register_task(Stage.JAVASCRIPT_INITIALIZING, {
priority: 100,
name: "native icon sprite loader",
function: async () => {
const image = new Image();
2020-08-09 09:03:52 -04:00
image.src = kClientSpriteUrl;
await new Promise((resolve, reject) => {
image.onload = resolve;
2020-08-09 09:03:52 -04:00
image.onerror = () => reject("failed to load client icon sprite");
});
2019-10-25 19:51:40 -04:00
const canvas = document.createElement("canvas");
2020-08-09 09:03:52 -04:00
canvas.width = kClientSpriteWidth;
canvas.height = kClientSpriteHeight;
canvas.getContext("2d").drawImage(image, 0, 0);
2019-10-25 19:51:40 -04:00
2020-08-09 09:03:52 -04:00
nativeSprite = electron.remote.nativeImage.createFromDataURL( canvas.toDataURL());
}
})
2019-10-25 19:51:40 -04:00
export function finalize() {
2020-08-09 09:03:52 -04:00
nativeSprite = undefined;
2019-10-25 19:51:40 -04:00
}