90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
|
|
"use strict";
|
|
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
import { Tray } from 'electron';
|
|
const nativeImage = require('electron').nativeImage
|
|
import {NativeImage} from 'electron'
|
|
import { backgroundMitt } from "@/composables/useEmitter";
|
|
import { getIconState, setIconState } from "@/store";
|
|
import { CONNECTION_STATUS } from '@/session';
|
|
|
|
declare const __static: string;
|
|
|
|
interface Icons {
|
|
[name: string]: NativeImage;
|
|
}
|
|
|
|
let tray: Tray | null = null;
|
|
const TRAY_ICON_DIR = 'trayIcon';
|
|
|
|
const ICON_NAMES = {
|
|
default: 'icon.png',
|
|
// TODO: need icons for different states
|
|
// TODO: need higher density icons @3x, @4x, @5x
|
|
// TODO: need to test with 20x20 & 24x24 icon sizes
|
|
recording: 'recording.png',
|
|
playback: 'playback.png',
|
|
connected: 'connected.png',
|
|
nominal: 'nominal.png',
|
|
reconnecting: 'reconnecting.png',
|
|
lost: 'lost.png',
|
|
}
|
|
|
|
const TRAY_STATUS = {
|
|
playback: 'playback',
|
|
recording: 'recording',
|
|
...CONNECTION_STATUS
|
|
};
|
|
|
|
|
|
const ICONS: Icons = {
|
|
default: iconFactory(ICON_NAMES.default),
|
|
[TRAY_STATUS.recording]: iconFactory(ICON_NAMES.recording),
|
|
[TRAY_STATUS.playback]: iconFactory(ICON_NAMES.playback),
|
|
[TRAY_STATUS.connected]: iconFactory(ICON_NAMES.connected),
|
|
[TRAY_STATUS.nominal]: iconFactory(ICON_NAMES.nominal),
|
|
[TRAY_STATUS.reconnecting]: iconFactory(ICON_NAMES.reconnecting),
|
|
[TRAY_STATUS.lost]: iconFactory(ICON_NAMES.lost),
|
|
};
|
|
|
|
backgroundMitt.on('update-icon-status', (payload: string) => {
|
|
updateTray(payload);
|
|
});
|
|
|
|
const changeIcon = (icon: NativeImage): void => tray ? tray.setImage(icon): undefined;
|
|
|
|
function iconFactory(iconName: string): NativeImage {
|
|
if (iconName !== 'icon.png') {
|
|
console.log('ERROR: Missing Icons: ', iconName);
|
|
}
|
|
// TODO: remove hard code, need icons for different states
|
|
let iconBuffer = fs.readFileSync(path.join(__static, TRAY_ICON_DIR, 'icon.png'));
|
|
const icon = nativeImage.createFromBuffer(iconBuffer);
|
|
return icon;
|
|
};
|
|
|
|
function updateTray(status: string): void {
|
|
// read icon state
|
|
const current = getIconState();
|
|
if (current !== status) {
|
|
setIconState(status);
|
|
if (status in ICONS) {
|
|
changeIcon(ICONS[status]);
|
|
} else {
|
|
changeIcon(ICONS.default);
|
|
}
|
|
}
|
|
};
|
|
|
|
export function initTray(): void {
|
|
if (!tray) {
|
|
try {
|
|
tray = new Tray(ICONS.default);
|
|
} catch(e) {
|
|
console.log('Error initilizing tray', e);
|
|
}
|
|
}
|
|
}
|
|
|