mirror of
https://github.com/zadam/trilium.git
synced 2025-10-20 15:19:01 +02:00
132 lines
4.7 KiB
TypeScript
132 lines
4.7 KiB
TypeScript
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";
|
|
import port from "@triliumnext/server/src/services/port.js";
|
|
import { join } from "path";
|
|
|
|
async function main() {
|
|
const userDataPath = getUserData();
|
|
app.setPath("userData", userDataPath);
|
|
|
|
const serverInitializedPromise = deferred<void>();
|
|
|
|
// 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();
|
|
}
|
|
|
|
/**
|
|
* Returns a unique user data directory for Electron so that single instance locks between legitimately different instances such as different port or data directory can still act independently, but we are focusing the main window otherwise.
|
|
*/
|
|
function getUserData() {
|
|
const name = `${app.getName()}-${port}`;
|
|
return join(app.getPath("appData"), name);
|
|
}
|
|
|
|
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();
|