Files
ThreadNet-Web/apps/desktop/src/tray.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

130 lines
4.3 KiB
TypeScript
Raw Normal View History

/*
2025-07-18 08:22:58 +01:00
Copyright 2024-2025 New Vector Ltd.
Copyright 2017 Karl Glatz <karl@glatz.biz>
Copyright 2017 OpenMarket Ltd
2025-01-17 11:44:49 +00:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-06 17:56:18 +01:00
Please see LICENSE files in the repository root for full details.
*/
2025-07-17 10:05:47 +01:00
import { app, Tray, Menu, nativeImage } from "electron";
import { v5 as uuidv5 } from "uuid";
2025-07-17 10:05:47 +01:00
import { writeFile } from "node:fs/promises";
import pngToIco from "png-to-ico";
import path from "node:path";
2022-01-10 12:57:33 +00:00
2024-10-30 18:06:03 +00:00
import { _t } from "./language-helper.js";
import { getBuildConfig } from "./build-config.js";
import { getBrand } from "./config.js";
import { getIconPath } from "./icon.js";
// This hardcoded uuid is an arbitrary v4 uuid generated on https://www.uuidgenerator.net/version4
const UUID_NAMESPACE = "9fc9c6a0-9ffe-45c9-9cd7-5639ae38b232";
2022-10-13 12:42:33 +01:00
let trayIcon: Tray | null = null;
2021-06-25 14:35:58 +01:00
export function hasTray(): boolean {
2022-12-15 11:00:58 +00:00
return trayIcon !== null;
2021-06-25 14:35:58 +01:00
}
2021-06-25 14:35:58 +01:00
export function destroy(): void {
if (trayIcon) {
trayIcon.destroy();
trayIcon = null;
}
2021-06-25 14:35:58 +01:00
}
2021-06-25 14:35:58 +01:00
function toggleWin(): void {
if (global.mainWindow?.isVisible() && !global.mainWindow.isMinimized() && global.mainWindow.isFocused()) {
global.mainWindow.hide();
} else {
if (global.mainWindow?.isMinimized()) global.mainWindow.restore();
if (!global.mainWindow?.isVisible()) global.mainWindow?.show();
global.mainWindow?.focus();
}
2021-06-25 14:35:58 +01:00
}
export async function create(): Promise<void> {
// no trays on darwin
2022-12-15 11:00:58 +00:00
if (process.platform === "darwin" || trayIcon) return;
const iconPath = await getIconPath();
const defaultIcon = nativeImage.createFromPath(iconPath);
const buildConfig = getBuildConfig();
if (process.platform === "win32" && app.isPackaged && buildConfig.windowsCertSubjectName) {
// Providing a GUID lets Windows be smarter about maintaining user's tray preferences
// https://github.com/electron/electron/pull/21891
// We generate the GUID in a custom arbitrary namespace and use the subject name & userData path
// to differentiate different app builds on the same system.
const guid = uuidv5(`${buildConfig.windowsCertSubjectName}:${app.getPath("userData")}`, UUID_NAMESPACE);
trayIcon = new Tray(defaultIcon, guid);
} else {
trayIcon = new Tray(defaultIcon);
}
trayIcon.setToolTip(getBrand());
initApplicationMenu();
2022-12-15 11:00:58 +00:00
trayIcon.on("click", toggleWin);
2025-07-17 10:05:47 +01:00
// See also, badge.ts
2025-07-14 15:52:41 +01:00
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;
}
2025-07-14 15:52:41 +01:00
return;
}
2025-07-14 15:52:41 +01:00
// No need to change, shortcut
if (favicons[0] === lastFavicon) return;
lastFavicon = favicons[0];
2025-07-17 10:05:47 +01:00
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);
}
2025-07-14 15:52:41 +01:00
});
2022-12-15 11:00:58 +00:00
global.mainWindow?.webContents.on("page-title-updated", function (ev, title) {
2022-10-13 12:42:33 +01:00
trayIcon?.setToolTip(title);
});
2021-06-25 14:35:58 +01:00
}
2021-06-25 14:35:58 +01:00
export function initApplicationMenu(): void {
if (!trayIcon) {
return;
}
const contextMenu = Menu.buildFromTemplate([
{
label: _t("action|show_hide"),
click: toggleWin,
},
2022-12-15 11:00:58 +00:00
{ type: "separator" },
{
label: _t("action|quit"),
2022-12-15 11:00:58 +00:00
click: function (): void {
app.quit();
},
},
]);
trayIcon.setContextMenu(contextMenu);
}