Skip to content

Build your personal knowledge base with Trilium Notes

refactor(desktop): make tray service self-contained

Elian Doran1ed101c822e1
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index 2fd15bed66..2a6e0afcaa 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -9,7 +9,6 @@ import dataDirs from "@triliumnext/server/src/services/data_dir.js";
import port from "@triliumnext/server/src/services/port.js";
import NodeRequestProvider from "@triliumnext/server/src/services/request.js";
import { RESOURCE_DIR } from "@triliumnext/server/src/services/resource_dir.js";
-import { registerPostSetupRedirectHook } from "@triliumnext/server/src/routes/setup.js";
import windowService from "@triliumnext/server/src/services/window.js";
import BetterSqlite3Provider from "@triliumnext/server/src/sql_provider.js";
import NodejsZipProvider from "@triliumnext/server/src/zip_provider.js";
@@ -25,10 +24,7 @@ import { PRODUCT_NAME } from "./app-info";
import IpcMessagingProvider from "./ipc_messaging_provider";
import DesktopPlatformProvider from "./platform_provider";
import { registerTriliumAppScheme } from "./protocol";
-import tray from "./services/tray";
-
-// Create the tray when the setup flow completes and swaps to the main window.
-registerPostSetupRedirectHook(() => tray.createTray());
+import { setupSystemTray } from "./services/tray";
async function main() {
// Ignore EPIPE errors on stdout/stderr — these occur when the parent process
@@ -94,6 +90,8 @@ async function main() {
await onReady();
});
+ setupSystemTray();
+
app.on("will-quit", () => {
globalShortcut.unregisterAll();
});
@@ -223,8 +221,6 @@ async function onReady() {
}
});
}
-
- tray.createTray();
} else {
getLog().banner(t("sql_init.db_not_initialized_desktop"));
await windowService.createSetupWindow();
diff --git a/apps/desktop/src/services/tray.ts b/apps/desktop/src/services/tray.ts
index 9080887213..1177e47de0 100644
--- a/apps/desktop/src/services/tray.ts
+++ b/apps/desktop/src/services/tray.ts
@@ -1,5 +1,5 @@
import type { KeyboardActionNames } from "@triliumnext/commons";
-import { becca, becca_service, type BNote, type BRecentNote, cls, date_notes, options as optionService, utils as coreUtils } from "@triliumnext/core";
+import { becca, becca_service, type BNote, type BRecentNote, cls, date_notes, options as optionService, sql_init, utils as coreUtils } from "@triliumnext/core";
import { getResourceDir } from "@triliumnext/server/src/services/utils.js";
import windowService from "@triliumnext/server/src/services/window.js";
import type { BrowserWindow, Tray } from "electron";
@@ -285,7 +285,7 @@ function changeVisibility() {
}
function createTray() {
- if (optionService.getOptionBool("disableTray")) {
+ if (tray || optionService.getOptionBool("disableTray")) {
return;
}
@@ -303,6 +303,18 @@ function createTray() {
i18next.on("languageChanged", updateTrayMenu);
}
-export default {
- createTray
-};
+/**
+ * Arms the system tray to appear as soon as a real (post-setup) window exists.
+ *
+ * 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.
+ */
+export function setupSystemTray() {
+ electron.app.on("browser-window-created", () => {
+ if (sql_init.isDbInitialized()) {
+ createTray();
+ }
+ });
+}
diff --git a/apps/server/src/routes/setup.ts b/apps/server/src/routes/setup.ts
index 88da54f91d..b9276bc39f 100644
--- a/apps/server/src/routes/setup.ts
+++ b/apps/server/src/routes/setup.ts
@@ -34,20 +34,6 @@ function setupPage(req: Request, res: Response) {
});
}
-type PostSetupRedirectHook = () => void | Promise<void>;
-let postSetupRedirectHook: PostSetupRedirectHook | undefined;
-
-/**
- * Lets a platform-specific layer (currently the Electron desktop app) hook in
- * after `handleElectronRedirect` has created the main window and closed the
- * setup window, e.g. to create the system tray. Lives here because setup.ts
- * is the only entry point that observes setup-completion server-side; the
- * desktop app registers the hook on startup.
- */
-export function registerPostSetupRedirectHook(hook: PostSetupRedirectHook) {
- postSetupRedirectHook = hook;
-}
-
async function handleElectronRedirect() {
const windowService = (await import("../services/window.js")).default;
const { app } = await import("electron");
@@ -55,10 +41,6 @@ async function handleElectronRedirect() {
// Wait for the main window to be created before closing the setup window to prevent triggering `window-all-closed`.
await windowService.createMainWindow(app);
windowService.closeSetupWindow();
-
- if (postSetupRedirectHook) {
- await postSetupRedirectHook();
- }
}
export default {