Skip to content

Build your personal knowledge base with Trilium Notes

feat(desktop): apply tray setting without restarting

Elian Doranfc7784af8de2
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 5405d208b8..7be3977082 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1522,7 +1522,7 @@
"tray": {
"title": "System Tray",
"enable_tray": "Tray icon",
- "enable_tray_description": "Trilium needs to be restarted for this change to take effect."
+ "enable_tray_description": "Changes apply immediately, though on some platforms hiding the tray icon may require restarting Trilium."
},
"text_editor": {
"title": "Editor",
diff --git a/apps/client/src/widgets/type_widgets/options/other.tsx b/apps/client/src/widgets/type_widgets/options/other.tsx
index 1ab94476c4..044657d6de 100644
--- a/apps/client/src/widgets/type_widgets/options/other.tsx
+++ b/apps/client/src/widgets/type_widgets/options/other.tsx
@@ -5,7 +5,7 @@ import { t } from "../../../services/i18n";
import search from "../../../services/search";
import server from "../../../services/server";
import toast from "../../../services/toast";
-import { isElectron } from "../../../services/utils";
+import utils, { isElectron } from "../../../services/utils";
import { Badge } from "../../react/Badge";
import Button from "../../react/Button";
import FormText from "../../react/FormText";
@@ -117,7 +117,11 @@ function TrayOptionsSettings() {
label={t("tray.enable_tray")}
description={t("tray.enable_tray_description")}
currentValue={!disableTray}
- onChange={trayEnabled => setDisableTray(!trayEnabled)}
+ onChange={async trayEnabled => {
+ await setDisableTray(!trayEnabled);
+ // Apply the change immediately so the user doesn't have to restart the app.
+ utils.reloadTray();
+ }}
/>
</OptionsSection>
);
diff --git a/apps/desktop/src/services/tray.spec.ts b/apps/desktop/src/services/tray.spec.ts
index 3efd5f4372..c37c5ffbe7 100644
--- a/apps/desktop/src/services/tray.spec.ts
+++ b/apps/desktop/src/services/tray.spec.ts
@@ -22,6 +22,7 @@ const state = vi.hoisted(() => ({
setToolTip: ReturnType<typeof vi.fn>;
on: ReturnType<typeof vi.fn>;
setContextMenu: ReturnType<typeof vi.fn>;
+ destroy: ReturnType<typeof vi.fn>;
clickHandlers: Map<string, Handler>;
lastTemplate: unknown[];
},
@@ -61,6 +62,9 @@ vi.mock("electron", () => ({
on = vi.fn((event: string, fn: Handler) => {
this.clickHandlers.set(event, fn);
});
+ destroy = vi.fn(() => {
+ state.trayInstance = undefined;
+ });
constructor() {
state.trayInstance = this;
}
@@ -206,9 +210,12 @@ describe("tray", () => {
expect(state.trayInstance).toBeUndefined();
});
- it("reload-tray ipc is a no-op before the tray exists", () => {
- // The handler is only registered inside createTray, so it can't be fired
- // yet — assert no tray was created as a side effect of prior tests.
+ it("reload-tray with the tray disabled does not create a tray", () => {
+ // The reload-tray handler now reconciles the tray with the option, so
+ // firing it while `disableTray` is set must not bring a tray into being
+ // (exercises destroyTray's no-tray early return).
+ state.disableTray = true;
+ state.ipcHandlers.get("reload-tray")?.();
expect(state.trayInstance).toBeUndefined();
});
@@ -537,6 +544,42 @@ describe("tray", () => {
});
});
+ describe("runtime enable/disable (no restart)", () => {
+ beforeEach(() => {
+ // Start each test with a live tray so we can toggle it off and on.
+ state.disableTray = false;
+ fireAppCreated();
+ });
+
+ it("destroys the tray when disabled and recreates it when re-enabled", () => {
+ expect(state.trayInstance).toBeDefined();
+ const destroy = state.trayInstance?.destroy;
+
+ // Toggle the option on -> tray is torn down without a restart.
+ state.disableTray = true;
+ state.ipcHandlers.get("reload-tray")?.();
+ expect(destroy).toHaveBeenCalled();
+ expect(state.trayInstance).toBeUndefined();
+
+ // Firing again while disabled is a no-op (destroyTray early return).
+ expect(() => state.ipcHandlers.get("reload-tray")?.()).not.toThrow();
+ expect(state.trayInstance).toBeUndefined();
+
+ // Toggle the option back off -> a fresh tray is created.
+ state.disableTray = false;
+ state.ipcHandlers.get("reload-tray")?.();
+ expect(state.trayInstance).toBeDefined();
+ expect(state.trayInstance?.setToolTip).toHaveBeenCalledWith("tray.tooltip");
+ });
+
+ it("refreshes rather than recreates the tray when reloaded while enabled", () => {
+ const before = state.trayInstance;
+ expect(before).toBeDefined();
+ state.ipcHandlers.get("reload-tray")?.();
+ expect(state.trayInstance).toBe(before);
+ });
+ });
+
// `createTray` runs `getTrayIconPath` exactly once per module instance and the
// module-level `tray` singleton can't be reset in-place. Re-import the module
// fresh (via vi.resetModules) so each icon-path branch executes from a clean
diff --git a/apps/desktop/src/services/tray.ts b/apps/desktop/src/services/tray.ts
index e009068922..ca43fa6628 100644
--- a/apps/desktop/src/services/tray.ts
+++ b/apps/desktop/src/services/tray.ts
@@ -7,7 +7,8 @@ import electron from "electron";
import { default as i18next, t } from "i18next";
import path from "path";
-let tray: Tray;
+let tray: Tray | null = null;
+let listenersRegistered = false;
// `mainWindow.isVisible` doesn't work with `mainWindow.show` and `mainWindow.hide` - it returns `false` when the window
// is minimized
const windowVisibilityMap: Record<number, boolean> = {};; // Dictionary for storing window ID and its visibility status
@@ -291,21 +292,63 @@ function changeVisibility() {
}
function createTray() {
- if (tray || optionService.getOptionBool("disableTray")) {
- return;
- }
-
tray = new electron.Tray(getTrayIconPath());
tray.setToolTip(t("tray.tooltip"));
// Restore focus
tray.on("click", changeVisibility);
updateTrayMenu();
+}
+
+function destroyTray() {
+ if (!tray) {
+ return;
+ }
+
+ // Best-effort: on Windows, macOS and most Linux DEs this removes the icon
+ // immediately. On GNOME the StatusNotifier host ignores the unregister, so
+ // the icon lingers until the app restarts — upstream Electron bug #24976,
+ // with no app-level workaround.
+ tray.destroy();
+ tray = null;
+}
+
+/**
+ * Reconciles the tray with the current `disableTray` option so the setting takes
+ * effect without restarting the app. This is the single entry point for the
+ * `reload-tray` IPC the renderer sends after the option changes, as well as for
+ * window focus/close, theme and language changes — it creates, destroys or just
+ * refreshes the tray as appropriate.
+ */
+function reloadTray() {
+ if (optionService.getOptionBool("disableTray")) {
+ destroyTray();
+ return;
+ }
+
+ if (tray) {
+ updateTrayMenu();
+ } else {
+ createTray();
+ }
+}
+
+/**
+ * Registers the long-lived listeners exactly once. Deferred until a real window
+ * exists (rather than run from {@link setupSystemTray}) because `isMac()` needs
+ * the core to be initialised, which isn't guaranteed during the early Electron
+ * startup when `setupSystemTray` runs.
+ */
+function registerTrayListeners() {
+ if (listenersRegistered) {
+ return;
+ }
+ listenersRegistered = true;
+ electron.ipcMain.on("reload-tray", reloadTray);
if (!coreUtils.isMac()) {
// macOS uses template icons which work great on dark & light themes.
electron.nativeTheme.on("updated", updateTrayMenu);
}
- electron.ipcMain.on("reload-tray", updateTrayMenu);
i18next.on("languageChanged", updateTrayMenu);
}
@@ -314,13 +357,15 @@ function createTray() {
*
* Skipping while the DB is uninitialised avoids attaching a tray to the setup
* wizard, where it would block the app from quitting via "close last window".
- * `createTray` is idempotent so subsequent windows (extra windows, the macOS
- * "activate" re-creation path) are no-ops.
+ * Both {@link registerTrayListeners} and {@link reloadTray} are idempotent, so
+ * subsequent windows (extra windows, the macOS "activate" re-creation path) just
+ * refresh the existing tray.
*/
export function setupSystemTray() {
electron.app.on("browser-window-created", () => {
if (sql_init.isDbInitialized()) {
- createTray();
+ registerTrayListeners();
+ reloadTray();
}
});
}