diff --git a/src/@types/global.d.ts b/src/@types/global.d.ts index 339cf0e78e..8d09d18288 100644 --- a/src/@types/global.d.ts +++ b/src/@types/global.d.ts @@ -29,9 +29,8 @@ declare global { launcher: AutoLaunch; vectorConfig: Record; trayConfig: { - // eslint-disable-next-line camelcase - icon_path: string; brand: string; + iconPath: string; }; store: Store<{ warnBeforeExit?: boolean; diff --git a/src/electron-main.ts b/src/electron-main.ts index 4ee2b37e73..9980ec31a7 100644 --- a/src/electron-main.ts +++ b/src/electron-main.ts @@ -55,7 +55,6 @@ const argv = minimist(process.argv, { // async to are initialised in setupGlobals() let asarPath: string; let resPath: string; -let iconPath: string; if (argv["help"]) { console.log("Options:"); @@ -192,17 +191,23 @@ async function setupGlobals(): Promise { } // The tray icon - // It's important to call `path.join` so we don't end up with the packaged asar in the final path. - const iconFile = `element.${process.platform === 'win32' ? 'ico' : 'png'}`; - iconPath = path.join(resPath, "img", iconFile); + // It's important to call `path.join` for the bundled assets so we don't end up with the packaged asar in the final path. + const iconPath = process.platform === 'win32' + ? (global.vectorConfig?.tray_icons?.ico || path.join(resPath, 'img', 'element.ico')) + : (global.vectorConfig?.tray_icons?.png || path.join(resPath, 'img', 'element.png')); + global.trayConfig = { - icon_path: iconPath, brand: global.vectorConfig.brand || 'Element', - }; + iconPath, + allowWebIconOverride: !!( + (process.platform === 'win32' && global.vectorConfig?.tray_icons?.ico) + || global.vectorConfig?.tray_icons?.png + ), + } as tray.IConfig; // launcher global.launcher = new AutoLaunch({ - name: global.vectorConfig.brand || 'Element', + name: global.trayConfig.brand, isHidden: true, mac: { useLaunchAgent: true, @@ -418,7 +423,7 @@ app.on('ready', async () => { // https://www.electronjs.org/docs/faq#the-font-looks-blurry-what-is-this-and-what-can-i-do backgroundColor: '#fff', - icon: iconPath, + icon: global.trayConfig.iconPath, show: false, autoHideMenuBar: global.store.get('autoHideMenuBar', true), diff --git a/src/tray.ts b/src/tray.ts index 6d95d3052e..64767a28f0 100644 --- a/src/tray.ts +++ b/src/tray.ts @@ -45,15 +45,17 @@ function toggleWin(): void { } } -interface IConfig { - icon_path: string; // eslint-disable-line camelcase +export interface IConfig { + // if this is true, favicon updates from the main window will replace the application tray icon + allowWebIconOverride: boolean; + iconPath: string; brand: string; } export function create(config: IConfig): void { // no trays on darwin if (process.platform === 'darwin' || trayIcon) return; - const defaultIcon = nativeImage.createFromPath(config.icon_path); + const defaultIcon = nativeImage.createFromPath(config.iconPath); trayIcon = new Tray(defaultIcon); trayIcon.setToolTip(config.brand); @@ -61,7 +63,15 @@ export function create(config: IConfig): void { trayIcon.on('click', toggleWin); let lastFavicon = null; - global.mainWindow.webContents.on('page-favicon-updated', async function(ev, favicons) { + global.mainWindow.webContents.on('page-title-updated', (_ev: Event, title: string) => { + trayIcon.setToolTip(title); + }); + + if (!config.allowWebIconOverride) { + return; + } + + global.mainWindow.webContents.on('page-favicon-updated', async (_ev: Event, favicons: string[]) => { if (!favicons || favicons.length <= 0 || !favicons[0].startsWith('data:')) { if (lastFavicon !== null) { global.mainWindow.setIcon(defaultIcon); @@ -91,10 +101,6 @@ export function create(config: IConfig): void { trayIcon.setImage(newFavicon); global.mainWindow.setIcon(newFavicon); }); - - global.mainWindow.webContents.on('page-title-updated', function(ev, title) { - trayIcon.setToolTip(title); - }); } export function initApplicationMenu(): void {