Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tray_icon_path to config to allow overriding tray icon #396

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/@types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ declare global {
launcher: AutoLaunch;
vectorConfig: Record<string, any>;
trayConfig: {
// eslint-disable-next-line camelcase
icon_path: string;
brand: string;
iconPath: string;
};
store: Store<{
warnBeforeExit?: boolean;
Expand Down
21 changes: 13 additions & 8 deletions src/electron-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:");
Expand Down Expand Up @@ -192,17 +191,23 @@ async function setupGlobals(): Promise<void> {
}

// 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;
3nprob marked this conversation as resolved.
Show resolved Hide resolved

// launcher
global.launcher = new AutoLaunch({
name: global.vectorConfig.brand || 'Element',
name: global.trayConfig.brand,
isHidden: true,
mac: {
useLaunchAgent: true,
Expand Down Expand Up @@ -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),

Expand Down
22 changes: 14 additions & 8 deletions src/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,33 @@ 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;
3nprob marked this conversation as resolved.
Show resolved Hide resolved
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);
initApplicationMenu();
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);
Expand Down Expand Up @@ -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 {
Expand Down