fix: generate only one icon on error

This commit is contained in:
Oleh Polisan 2024-06-22 14:54:37 +03:00
parent 183a388495
commit 4e9636ad83
2 changed files with 28 additions and 12 deletions

View file

@ -42,6 +42,13 @@ export async function setTrayIcon(iconName: string) {
trayImage = nativeImage.createFromPath(join(ICONS_DIR, "icon.png")); trayImage = nativeImage.createFromPath(join(ICONS_DIR, "icon.png"));
} }
if (trayImage.isEmpty()) {
const iconKey = statusToSettingsKey[iconName as keyof typeof statusToSettingsKey];
Settings.store[iconKey] = false;
generateTrayIcons("icon");
return;
}
const badgeSvg = readFileSync(join(BADGE_DIR, `badge.svg`), "utf8"); const badgeSvg = readFileSync(join(BADGE_DIR, `badge.svg`), "utf8");
// and send IPC call to renderer to add badge to icon // and send IPC call to renderer to add badge to icon
mainWin.webContents.send(IpcEvents.ADD_BADGE_TO_ICON, trayImage.toDataURL(), badgeSvg); mainWin.webContents.send(IpcEvents.ADD_BADGE_TO_ICON, trayImage.toDataURL(), badgeSvg);
@ -55,12 +62,12 @@ export async function setTrayIcon(iconName: string) {
if (trayImage.isEmpty()) { if (trayImage.isEmpty()) {
const iconKey = statusToSettingsKey[iconName as keyof typeof statusToSettingsKey]; const iconKey = statusToSettingsKey[iconName as keyof typeof statusToSettingsKey];
Settings.store[iconKey] = false; Settings.store[iconKey] = false;
generateTrayIcons(); generateTrayIcons(iconName);
return; return;
} }
} else trayImage = nativeImage.createFromPath(join(ICONS_DIR, iconName + ".png")); } else trayImage = nativeImage.createFromPath(join(ICONS_DIR, iconName + ".png"));
if (trayImage.isEmpty()) { if (trayImage.isEmpty()) {
generateTrayIcons(); generateTrayIcons(iconName);
return; return;
} }
if (process.platform === "darwin") { if (process.platform === "darwin") {
@ -68,8 +75,8 @@ export async function setTrayIcon(iconName: string) {
} }
tray.setImage(trayImage); tray.setImage(trayImage);
} catch (error) { } catch (error) {
console.log("Error: ", error, "Regenerating tray icons."); console.log("Error: ", error, "Regenerating tray icon.");
generateTrayIcons(); // TODO: pass here only one icon request generateTrayIcons(iconName);
} }
return; return;
} }
@ -102,8 +109,8 @@ export function getTrayIconFileSync(iconName: string) {
} else img = nativeImage.createFromPath(join(ICONS_DIR, iconName + ".png")); } else img = nativeImage.createFromPath(join(ICONS_DIR, iconName + ".png"));
img = img.resize({ width: 128, height: 128 }); img = img.resize({ width: 128, height: 128 });
if (img.isEmpty()) { if (img.isEmpty()) {
console.log("Can't open icon file. Regenerating."); console.log("Can't open icon file for", iconName, ". Regenerating.");
generateTrayIcons(); generateTrayIcons(iconName);
img = nativeImage.createFromPath(join(ICONS_DIR, iconName + ".png")); img = nativeImage.createFromPath(join(ICONS_DIR, iconName + ".png"));
const iconKey = statusToSettingsKey[iconName as keyof typeof statusToSettingsKey]; const iconKey = statusToSettingsKey[iconName as keyof typeof statusToSettingsKey];
Settings.store[iconKey] = false; Settings.store[iconKey] = false;
@ -131,16 +138,26 @@ export async function createTrayIcon(
mainWin.webContents.send(IpcEvents.SET_CURRENT_VOICE_TRAY_ICON); mainWin.webContents.send(IpcEvents.SET_CURRENT_VOICE_TRAY_ICON);
} }
export async function generateTrayIcons() { export async function generateTrayIcons(iconName: string = "") {
// this function generates tray icons as .png's in Vesktop cache for future use // this function generates tray icons as .png's in Vesktop cache for future use
mkdirSync(ICONS_DIR, { recursive: true }); mkdirSync(ICONS_DIR, { recursive: true });
const Icons = ["speaking", "muted", "deafened", "idle"]; const Icons = ["speaking", "muted", "deafened", "idle"];
for (const icon of Icons) {
mainWin.webContents.send(IpcEvents.CREATE_TRAY_ICON_REQUEST, icon); const createMainIcon = () => {
}
const img = nativeImage.createFromPath(join(STATIC_DIR, "icon.png")).resize({ width: 128, height: 128 }); const img = nativeImage.createFromPath(join(STATIC_DIR, "icon.png")).resize({ width: 128, height: 128 });
writeFileSync(join(ICONS_DIR, "icon.png"), img.toPNG()); writeFileSync(join(ICONS_DIR, "icon.png"), img.toPNG());
mainWin.webContents.send(IpcEvents.SET_CURRENT_VOICE_TRAY_ICON); mainWin.webContents.send(IpcEvents.SET_CURRENT_VOICE_TRAY_ICON);
};
if (iconName) {
if (Icons.includes(iconName)) mainWin.webContents.send(IpcEvents.CREATE_TRAY_ICON_REQUEST, iconName);
else if (iconName === "icon") createMainIcon();
return;
}
for (const icon of Icons) {
mainWin.webContents.send(IpcEvents.CREATE_TRAY_ICON_REQUEST, icon);
}
createMainIcon();
} }
export async function pickTrayIcon(iconName: string) { export async function pickTrayIcon(iconName: string) {

View file

@ -97,7 +97,6 @@ function trayEditButton(iconName: string) {
function TrayModalComponent({ modalProps, close }: { modalProps: any; close: () => void }) { function TrayModalComponent({ modalProps, close }: { modalProps: any; close: () => void }) {
const Settings = useSettings(); const Settings = useSettings();
return ( return (
<Modals.ModalRoot {...modalProps} size={ModalSize.MEDIUM}> <Modals.ModalRoot {...modalProps} size={ModalSize.MEDIUM}>
<Modals.ModalHeader className="vcd-custom-tray-header"> <Modals.ModalHeader className="vcd-custom-tray-header">
@ -118,7 +117,7 @@ function TrayModalComponent({ modalProps, close }: { modalProps: any; close: ()
> >
Choose Icon Choose Icon
</Button> </Button>
{Settings[key] && ( {VesktopNative.settings.get()[key] && (
<Button <Button
onClick={() => { onClick={() => {
Settings[key] = false; Settings[key] = false;