import { initializeTranslations } from "@triliumnext/server/src/services/i18n.js"; import { t } from "i18next"; import { app, globalShortcut, BrowserWindow } from "electron"; import sqlInit from "@triliumnext/server/src/services/sql_init.js"; import windowService from "@triliumnext/server/src/services/window.js"; import tray from "@triliumnext/server/src/services/tray.js"; import options from "@triliumnext/server/src/services/options.js"; import electronDebug from "electron-debug"; import electronDl from "electron-dl"; import { deferred } from "@triliumnext/server/src/services/utils.js"; import { PRODUCT_NAME } from "./app-info"; async function main() { const serverInitializedPromise = deferred(); // Prevent Trilium starting twice on first install and on uninstall for the Windows installer. if ((require("electron-squirrel-startup")).default) { process.exit(0); } // Adds debug features like hotkeys for triggering dev tools and reload electronDebug(); electronDl({ saveAs: true }); // needed for excalidraw export https://github.com/zadam/trilium/issues/4271 app.commandLine.appendSwitch("enable-experimental-web-platform-features"); app.commandLine.appendSwitch("lang", options.getOptionOrNull("formattingLocale") ?? "en"); // Disable smooth scroll if the option is set const smoothScrollEnabled = options.getOptionOrNull("smoothScrollEnabled"); if (smoothScrollEnabled === "false") { app.commandLine.appendSwitch("disable-smooth-scrolling"); } // Electron 36 crashes with "Using GTK 2/3 and GTK 4 in the same process is not supported" on some distributions. // See https://github.com/electron/electron/issues/46538 for more info. if (process.platform === "linux") { app.setName(PRODUCT_NAME); app.commandLine.appendSwitch("gtk-version", "3"); } // Quit when all windows are closed, except on macOS. There, it's common // for applications and their menu bar to stay active until the user quits // explicitly with Cmd + Q. app.on("window-all-closed", () => { if (process.platform !== "darwin") { app.quit(); } }); app.on("ready", async () => { await serverInitializedPromise; console.log("Starting Electron..."); await onReady(); }); app.on("will-quit", () => { globalShortcut.unregisterAll(); }); app.on("second-instance", (event, commandLine) => { const lastFocusedWindow = windowService.getLastFocusedWindow(); if (commandLine.includes("--new-window")) { windowService.createExtraWindow(""); } else if (lastFocusedWindow) { if (lastFocusedWindow.isMinimized()) { lastFocusedWindow.restore(); } lastFocusedWindow.show(); lastFocusedWindow.focus(); } }); await initializeTranslations(); const isPrimaryInstance = (await import("electron")).app.requestSingleInstanceLock(); if (!isPrimaryInstance) { console.info(t("desktop.instance_already_running")); process.exit(0); } // this is to disable electron warning spam in the dev console (local development only) process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true"; const startTriliumServer = (await import("@triliumnext/server/src/www.js")).default; await startTriliumServer(); console.log("Server loaded"); serverInitializedPromise.resolve(); } async function onReady() { // app.setAppUserModelId('com.github.zadam.trilium'); // if db is not initialized -> setup process // if db is initialized, then we need to wait until the migration process is finished if (sqlInit.isDbInitialized()) { await sqlInit.dbReady; await windowService.createMainWindow(app); if (process.platform === "darwin") { app.on("activate", async () => { if (BrowserWindow.getAllWindows().length === 0) { await windowService.createMainWindow(app); } }); } tray.createTray(); } else { await windowService.createSetupWindow(); } await windowService.registerGlobalShortcuts(); } main();