Move code; drop favicon updating for win32

This commit is contained in:
Will Hunt
2025-07-15 13:55:04 +01:00
parent f91c07f929
commit 7c19e5f0a8
4 changed files with 38 additions and 60 deletions
-1
View File
@@ -64,7 +64,6 @@
"electron-window-state": "^5.0.3", "electron-window-state": "^5.0.3",
"keytar-forked": "7.10.0", "keytar-forked": "7.10.0",
"minimist": "^1.2.6", "minimist": "^1.2.6",
"png-to-ico": "^2.1.1",
"uuid": "^11.0.0" "uuid": "^11.0.0"
}, },
"devDependencies": { "devDependencies": {
-12
View File
@@ -20,18 +20,6 @@ ipcMain.on(
// has some Windows support too, and in some Windows environments this leads to two badges rendering atop // has some Windows support too, and in some Windows environments this leads to two badges rendering atop
// each other. See https://github.com/vector-im/element-web/issues/16942 // each other. See https://github.com/vector-im/element-web/issues/16942
app.badgeCount = count; app.badgeCount = count;
} else {
// similarly, we only use setOverlayIcon on Windows as it's only supported on that platform, but has good support
// from all the Windows variants we support.
// https://www.electronjs.org/docs/latest/api/browser-window#winsetoverlayiconoverlay-description-windows
if (imageBuffer && imageBufferDescription !== undefined) {
global.mainWindow?.setOverlayIcon(
nativeImage.createFromBuffer(Buffer.from(imageBuffer)),
imageBufferDescription,
);
} else {
global.mainWindow?.setOverlayIcon(null, "");
}
} }
if (count === 0) { if (count === 0) {
global.mainWindow?.flashFrame(false); global.mainWindow?.flashFrame(false);
+37 -32
View File
@@ -7,10 +7,7 @@ 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. Please see LICENSE files in the repository root for full details.
*/ */
import { app, Tray, Menu, nativeImage } from "electron"; import { app, Tray, Menu, nativeImage, ipcMain, IpcMainEvent } from "electron";
import pngToIco from "png-to-ico";
import path from "node:path";
import fs from "node:fs";
import { v5 as uuidv5 } from "uuid"; import { v5 as uuidv5 } from "uuid";
import { _t } from "./language-helper.js"; import { _t } from "./language-helper.js";
@@ -71,37 +68,45 @@ export function create(config: IConfig): void {
initApplicationMenu(); initApplicationMenu();
trayIcon.on("click", toggleWin); trayIcon.on("click", toggleWin);
let lastFavicon: string | null = null; if (process.platform === "win32") {
global.mainWindow?.webContents.on("page-favicon-updated", async function (ev, favicons) { // We only use setOverlayIcon on Windows as it's only supported on that platform, but has good support
if (!favicons || favicons.length <= 0 || !favicons[0].startsWith("data:")) { // from all the Windows variants we support.
if (lastFavicon !== null) { // https://www.electronjs.org/docs/latest/api/browser-window#winsetoverlayiconoverlay-description-windows
global.mainWindow?.setIcon(defaultIcon); ipcMain.on(
trayIcon?.setImage(defaultIcon); "setBadgeCount",
lastFavicon = null; function (_ev: IpcMainEvent, count: number, imageBuffer?: Buffer, imageBufferDescription?: string): void {
if (imageBuffer && imageBufferDescription !== undefined) {
global.mainWindow?.setOverlayIcon(
nativeImage.createFromBuffer(Buffer.from(imageBuffer)),
imageBufferDescription,
);
} else {
global.mainWindow?.setOverlayIcon(null, "");
}
},
);
} else {
// For other platforms, we instead update the application icon when the favicon changes.
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:")) {
if (lastFavicon !== null) {
global.mainWindow?.setIcon(defaultIcon);
trayIcon?.setImage(defaultIcon);
lastFavicon = null;
}
return;
} }
return;
}
// No need to change, shortcut // No need to change, shortcut
if (favicons[0] === lastFavicon) return; if (favicons[0] === lastFavicon) return;
lastFavicon = favicons[0]; lastFavicon = favicons[0];
let newFavicon = nativeImage.createFromDataURL(favicons[0]); let newFavicon = nativeImage.createFromDataURL(favicons[0]);
trayIcon?.setImage(newFavicon);
// Windows likes ico's too much. global.mainWindow?.setIcon(newFavicon);
if (process.platform === "win32") { });
try { }
const icoPath = path.join(app.getPath("temp"), "win32_element_icon.ico");
fs.writeFileSync(icoPath, await pngToIco(newFavicon.toPNG()));
newFavicon = nativeImage.createFromPath(icoPath);
} catch (e) {
console.error("Failed to make win32 ico", e);
}
}
trayIcon?.setImage(newFavicon);
global.mainWindow?.setIcon(newFavicon);
});
global.mainWindow?.webContents.on("page-title-updated", function (ev, title) { global.mainWindow?.webContents.on("page-title-updated", function (ev, title) {
trayIcon?.setToolTip(title); trayIcon?.setToolTip(title);
+1 -15
View File
@@ -2243,7 +2243,7 @@
"@types/node" "*" "@types/node" "*"
form-data "^4.0.0" form-data "^4.0.0"
"@types/node@*", "@types/node@18.19.115", "@types/node@^17.0.36", "@types/node@^22.7.7": "@types/node@*", "@types/node@18.19.115", "@types/node@^22.7.7":
version "18.19.115" version "18.19.115"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.19.115.tgz#cd94caf14472021b4443c99bcd7aac6bb5c4f672" resolved "https://registry.yarnpkg.com/@types/node/-/node-18.19.115.tgz#cd94caf14472021b4443c99bcd7aac6bb5c4f672"
integrity sha512-kNrFiTgG4a9JAn1LMQeLOv3MvXIPokzXziohMrMsvpYgLpdEt/mMiVYc4sGKtDfyxM5gIDF4VgrPRyCw4fHOYg== integrity sha512-kNrFiTgG4a9JAn1LMQeLOv3MvXIPokzXziohMrMsvpYgLpdEt/mMiVYc4sGKtDfyxM5gIDF4VgrPRyCw4fHOYg==
@@ -6237,20 +6237,6 @@ pluralizers@^0.1.7:
resolved "https://registry.yarnpkg.com/pluralizers/-/pluralizers-0.1.7.tgz#8d38dd0a1b660e739b10ab2eab10b684c9d50142" resolved "https://registry.yarnpkg.com/pluralizers/-/pluralizers-0.1.7.tgz#8d38dd0a1b660e739b10ab2eab10b684c9d50142"
integrity sha512-mw6AejUiCaMQ6uPN9ObjJDTnR5AnBSmnHHy3uVTbxrSFSxO5scfwpTs8Dxyb6T2v7GSulhvOq+pm9y+hXUvtOA== integrity sha512-mw6AejUiCaMQ6uPN9ObjJDTnR5AnBSmnHHy3uVTbxrSFSxO5scfwpTs8Dxyb6T2v7GSulhvOq+pm9y+hXUvtOA==
png-to-ico@^2.1.1:
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: possible-typed-array-names@^1.0.0:
version "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" resolved "https://registry.yarnpkg.com/possible-typed-array-names/-/possible-typed-array-names-1.0.0.tgz#89bb63c6fada2c3e90adc4a647beeeb39cc7bf8f"