From db78dbec09571194af3ce585b7b178adbdcf638b Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Thu, 17 Jul 2025 10:05:47 +0100 Subject: [PATCH] Restore tray icon updates for Windows. --- package.json | 1 + src/tray.ts | 26 ++++++++++++++++++++++---- yarn.lock | 16 +++++++++++++++- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 5eeb5396ee..d5d4f5e432 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "electron-window-state": "^5.0.3", "keytar-forked": "7.10.0", "minimist": "^1.2.6", + "png-to-ico": "^2.1.8", "uuid": "^11.0.0" }, "devDependencies": { diff --git a/src/tray.ts b/src/tray.ts index 6df764ecd9..1489b55bfb 100644 --- a/src/tray.ts +++ b/src/tray.ts @@ -7,8 +7,11 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com Please see LICENSE files in the repository root for full details. */ -import { app, Tray, Menu, nativeImage, ipcMain, type IpcMainEvent } from "electron"; +import { app, Tray, Menu, nativeImage } from "electron"; import { v5 as uuidv5 } from "uuid"; +import { writeFile } from "node:fs/promises"; +import pngToIco from "png-to-ico"; +import path from "node:path"; import { _t } from "./language-helper.js"; @@ -68,6 +71,7 @@ export function create(config: IConfig): void { initApplicationMenu(); trayIcon.on("click", toggleWin); + // See also, badge.ts let lastFavicon: string | null = null; global.mainWindow?.webContents.on("page-favicon-updated", async function (ev, favicons) { if (!favicons || favicons.length <= 0 || !favicons[0].startsWith("data:")) { @@ -83,9 +87,23 @@ export function create(config: IConfig): void { if (favicons[0] === lastFavicon) return; lastFavicon = favicons[0]; - const newFavicon = nativeImage.createFromDataURL(favicons[0]); - trayIcon?.setImage(newFavicon); - global.mainWindow?.setIcon(newFavicon); + let newFavicon = nativeImage.createFromDataURL(favicons[0]); + + // Windows likes ico's too much. + if (process.platform === "win32") { + try { + const icoPath = path.join(app.getPath("temp"), "win32_element_icon.ico"); + await writeFile(icoPath, await pngToIco(newFavicon.toPNG())); + newFavicon = nativeImage.createFromPath(icoPath); + } catch (e) { + console.error("Failed to make win32 ico", e); + } + // Always update the tray icon for Windows. + trayIcon?.setImage(newFavicon); + } else { + trayIcon?.setImage(newFavicon); + global.mainWindow?.setIcon(newFavicon); + } }); global.mainWindow?.webContents.on("page-title-updated", function (ev, title) { diff --git a/yarn.lock b/yarn.lock index 645a42add6..61e14e8b9e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2243,7 +2243,7 @@ "@types/node" "*" form-data "^4.0.0" -"@types/node@*", "@types/node@18.19.115", "@types/node@^22.7.7": +"@types/node@*", "@types/node@18.19.115", "@types/node@^17.0.36", "@types/node@^22.7.7": version "18.19.115" resolved "https://registry.yarnpkg.com/@types/node/-/node-18.19.115.tgz#cd94caf14472021b4443c99bcd7aac6bb5c4f672" integrity sha512-kNrFiTgG4a9JAn1LMQeLOv3MvXIPokzXziohMrMsvpYgLpdEt/mMiVYc4sGKtDfyxM5gIDF4VgrPRyCw4fHOYg== @@ -6237,6 +6237,20 @@ pluralizers@^0.1.7: resolved "https://registry.yarnpkg.com/pluralizers/-/pluralizers-0.1.7.tgz#8d38dd0a1b660e739b10ab2eab10b684c9d50142" integrity sha512-mw6AejUiCaMQ6uPN9ObjJDTnR5AnBSmnHHy3uVTbxrSFSxO5scfwpTs8Dxyb6T2v7GSulhvOq+pm9y+hXUvtOA== +png-to-ico@^2.1.8: + version "2.1.8" + resolved "https://registry.yarnpkg.com/png-to-ico/-/png-to-ico-2.1.8.tgz#fdc2eda6f197df1d6c33400707e36c3b802ac6dd" + integrity sha512-Nf+IIn/cZ/DIZVdGveJp86NG5uNib1ZXMiDd/8x32HCTeKSvgpyg6D/6tUBn1QO/zybzoMK0/mc3QRgAyXdv9w== + dependencies: + "@types/node" "^17.0.36" + minimist "^1.2.6" + pngjs "^6.0.0" + +pngjs@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/pngjs/-/pngjs-6.0.0.tgz#ca9e5d2aa48db0228a52c419c3308e87720da821" + integrity sha512-TRzzuFRRmEoSW/p1KVAmiOgPco2Irlah+bGFCeNfJXxxYGwSw7YwAOAcd7X28K/m5bjBWKsC29KyoMfHbypayg== + possible-typed-array-names@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/possible-typed-array-names/-/possible-typed-array-names-1.0.0.tgz#89bb63c6fada2c3e90adc4a647beeeb39cc7bf8f"