From aa814c84dd9d008295e380b2abff301408cfd36c Mon Sep 17 00:00:00 2001 From: 3nprob <3nprob@example.com> Date: Wed, 3 Aug 2022 11:04:04 +0900 Subject: [PATCH 1/6] Add tray_icon_path to config to allow overriding tray icon --- element.io/nightly/config.json | 3 ++- element.io/release/config.json | 3 ++- src/electron-main.ts | 15 ++++++++------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/element.io/nightly/config.json b/element.io/nightly/config.json index 5b7a6ec01e..ea6219cd6e 100644 --- a/element.io/nightly/config.json +++ b/element.io/nightly/config.json @@ -49,5 +49,6 @@ "feature_spotlight": true, "feature_video_rooms": true }, - "map_style_url": "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx" + "map_style_url": "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx", + "tray_icons": {} } diff --git a/element.io/release/config.json b/element.io/release/config.json index ec3dcaa763..e18e01771c 100644 --- a/element.io/release/config.json +++ b/element.io/release/config.json @@ -41,5 +41,6 @@ "apiHost": "https://posthog.element.io" }, "privacy_policy_url": "https://element.io/cookie-policy", - "map_style_url": "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx" + "map_style_url": "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx", + "tray_icons": {} } diff --git a/src/electron-main.ts b/src/electron-main.ts index 4ee2b37e73..e74277ecd9 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,19 @@ 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 icon_path = 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, + icon_path, brand: global.vectorConfig.brand || 'Element', }; // launcher global.launcher = new AutoLaunch({ - name: global.vectorConfig.brand || 'Element', + name: global.trayConfig.brand, isHidden: true, mac: { useLaunchAgent: true, @@ -418,7 +419,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.icon_path, show: false, autoHideMenuBar: global.store.get('autoHideMenuBar', true), From a591698d983e5fcb5e305500989ba8f6022cc152 Mon Sep 17 00:00:00 2001 From: 3nprob <3nprob@example.com> Date: Tue, 9 Aug 2022 05:13:34 +0900 Subject: [PATCH 2/6] rename icon_path to iconPath --- src/@types/global.d.ts | 3 +-- src/electron-main.ts | 6 +++--- src/tray.ts | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/@types/global.d.ts b/src/@types/global.d.ts index 339cf0e78e..68c3a62b87 100644 --- a/src/@types/global.d.ts +++ b/src/@types/global.d.ts @@ -29,8 +29,7 @@ declare global { launcher: AutoLaunch; vectorConfig: Record; trayConfig: { - // eslint-disable-next-line camelcase - icon_path: string; + iconPath: string; brand: string; }; store: Store<{ diff --git a/src/electron-main.ts b/src/electron-main.ts index e74277ecd9..4e89240358 100644 --- a/src/electron-main.ts +++ b/src/electron-main.ts @@ -192,12 +192,12 @@ async function setupGlobals(): Promise { // The tray icon // 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 icon_path = process.platform === 'win32' + 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', }; @@ -419,7 +419,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: global.trayConfig.icon_path, + icon: global.trayConfig.iconPath, show: false, autoHideMenuBar: global.store.get('autoHideMenuBar', true), diff --git a/src/tray.ts b/src/tray.ts index 6d95d3052e..684db3d57f 100644 --- a/src/tray.ts +++ b/src/tray.ts @@ -46,14 +46,14 @@ function toggleWin(): void { } interface IConfig { - icon_path: string; // eslint-disable-line camelcase + 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); From 1f51e5915dbbcca791ed9fe6cda38529b9469dbf Mon Sep 17 00:00:00 2001 From: 3nprob <3nprob@example.com> Date: Tue, 9 Aug 2022 08:24:12 +0900 Subject: [PATCH 3/6] Don't override trayicon with favicon when manually configured --- src/@types/global.d.ts | 2 +- src/electron-main.ts | 7 +++++-- src/tray.ts | 17 +++++++++++------ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/@types/global.d.ts b/src/@types/global.d.ts index 68c3a62b87..8d09d18288 100644 --- a/src/@types/global.d.ts +++ b/src/@types/global.d.ts @@ -29,8 +29,8 @@ declare global { launcher: AutoLaunch; vectorConfig: Record; trayConfig: { - iconPath: string; brand: string; + iconPath: string; }; store: Store<{ warnBeforeExit?: boolean; diff --git a/src/electron-main.ts b/src/electron-main.ts index 4e89240358..83f1f2a529 100644 --- a/src/electron-main.ts +++ b/src/electron-main.ts @@ -197,9 +197,12 @@ async function setupGlobals(): Promise { : (global.vectorConfig?.tray_icons?.png || path.join(resPath, 'img', 'element.png')); global.trayConfig = { - 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({ diff --git a/src/tray.ts b/src/tray.ts index 684db3d57f..c816d04652 100644 --- a/src/tray.ts +++ b/src/tray.ts @@ -45,7 +45,8 @@ function toggleWin(): void { } } -interface IConfig { +export interface IConfig { + allowWebIconOverride: boolean; iconPath: string; brand: string; } @@ -61,7 +62,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 +100,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 { From 47dfa7964f17f92e4b379eb150dfcc5a55d27859 Mon Sep 17 00:00:00 2001 From: 3nprob <3nprob@example.com> Date: Tue, 9 Aug 2022 10:04:12 +0900 Subject: [PATCH 4/6] remove tray_icons from default config.json --- element.io/nightly/config.json | 3 +-- element.io/release/config.json | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/element.io/nightly/config.json b/element.io/nightly/config.json index ea6219cd6e..5b7a6ec01e 100644 --- a/element.io/nightly/config.json +++ b/element.io/nightly/config.json @@ -49,6 +49,5 @@ "feature_spotlight": true, "feature_video_rooms": true }, - "map_style_url": "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx", - "tray_icons": {} + "map_style_url": "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx" } diff --git a/element.io/release/config.json b/element.io/release/config.json index e18e01771c..ec3dcaa763 100644 --- a/element.io/release/config.json +++ b/element.io/release/config.json @@ -41,6 +41,5 @@ "apiHost": "https://posthog.element.io" }, "privacy_policy_url": "https://element.io/cookie-policy", - "map_style_url": "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx", - "tray_icons": {} + "map_style_url": "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx" } From 68aeaf1341b3fe75f2f42a7867c8f8b3ba8a5888 Mon Sep 17 00:00:00 2001 From: 3nprob <3nprob@example.com> Date: Tue, 9 Aug 2022 10:06:37 +0900 Subject: [PATCH 5/6] wrap boolean operators --- src/electron-main.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/electron-main.ts b/src/electron-main.ts index 83f1f2a529..9980ec31a7 100644 --- a/src/electron-main.ts +++ b/src/electron-main.ts @@ -193,14 +193,15 @@ async function setupGlobals(): Promise { // The tray icon // 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.vectorConfig?.tray_icons?.ico || path.join(resPath, 'img', 'element.ico')) + : (global.vectorConfig?.tray_icons?.png || path.join(resPath, 'img', 'element.png')); global.trayConfig = { brand: global.vectorConfig.brand || 'Element', iconPath, allowWebIconOverride: !!( - process.platform === 'win32' && global.vectorConfig?.tray_icons?.ico || global.vectorConfig?.tray_icons?.png + (process.platform === 'win32' && global.vectorConfig?.tray_icons?.ico) + || global.vectorConfig?.tray_icons?.png ), } as tray.IConfig; From 8f7283aa982c1d2364b3af9aad03e3c925b3bf7f Mon Sep 17 00:00:00 2001 From: 3nprob <3nprob@example.com> Date: Tue, 9 Aug 2022 10:15:52 +0900 Subject: [PATCH 6/6] add comment on tray.IConfig.allowWebIconOverride --- src/tray.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tray.ts b/src/tray.ts index c816d04652..64767a28f0 100644 --- a/src/tray.ts +++ b/src/tray.ts @@ -46,6 +46,7 @@ function toggleWin(): void { } 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;