2024-07-18 21:37:45 +03:00
|
|
|
import path from "path";
|
|
|
|
import url from "url";
|
2024-07-18 21:35:17 +03:00
|
|
|
import port from "./port.js";
|
|
|
|
import optionService from "./options.js";
|
|
|
|
import log from "./log.js";
|
|
|
|
import sqlInit from "./sql_init.js";
|
|
|
|
import cls from "./cls.js";
|
|
|
|
import keyboardActionsService from "./keyboard_actions.js";
|
2024-08-28 06:28:02 +00:00
|
|
|
import remoteMain from "@electron/remote/main/index.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { App, BrowserWindow, BrowserWindowConstructorOptions, WebContents } from "electron";
|
|
|
|
import { ipcMain } from "electron";
|
2025-01-22 19:08:38 +01:00
|
|
|
import { isDev, isMac, isWindows } from "./utils.js";
|
2019-12-24 14:42:03 +01:00
|
|
|
|
2024-07-19 00:18:35 +03:00
|
|
|
import { fileURLToPath } from "url";
|
|
|
|
import { dirname } from "path";
|
|
|
|
|
2023-06-30 11:18:34 +02:00
|
|
|
// Prevent the window being garbage collected
|
2024-02-18 12:19:09 +02:00
|
|
|
let mainWindow: BrowserWindow | null;
|
|
|
|
let setupWindow: BrowserWindow | null;
|
2019-12-24 14:42:03 +01:00
|
|
|
|
2024-02-18 12:19:09 +02:00
|
|
|
async function createExtraWindow(extraWindowHash: string) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const spellcheckEnabled = optionService.getOptionBool("spellCheckEnabled");
|
2022-01-02 21:35:02 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const { BrowserWindow } = await import("electron");
|
2022-01-02 21:35:02 +01:00
|
|
|
|
2020-04-23 23:08:15 +02:00
|
|
|
const win = new BrowserWindow({
|
2020-04-27 22:29:39 +02:00
|
|
|
width: 1000,
|
|
|
|
height: 800,
|
2025-01-09 18:07:02 +02:00
|
|
|
title: "TriliumNext Notes",
|
2020-04-23 23:08:15 +02:00
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
2021-03-10 20:02:23 +01:00
|
|
|
contextIsolation: false,
|
2022-01-02 21:35:02 +01:00
|
|
|
spellcheck: spellcheckEnabled
|
2020-04-23 23:08:15 +02:00
|
|
|
},
|
2024-12-01 17:44:42 +02:00
|
|
|
...getWindowExtraOpts(),
|
2020-04-23 23:08:15 +02:00
|
|
|
icon: getIcon()
|
|
|
|
});
|
|
|
|
|
|
|
|
win.setMenuBarVisibility(false);
|
2023-04-11 21:41:55 +02:00
|
|
|
win.loadURL(`http://127.0.0.1:${port}/?extraWindow=1${extraWindowHash}`);
|
2022-01-02 21:35:02 +01:00
|
|
|
|
|
|
|
configureWebContents(win.webContents, spellcheckEnabled);
|
2020-04-23 23:08:15 +02:00
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
ipcMain.on("create-extra-window", (event, arg) => {
|
2023-04-11 21:41:55 +02:00
|
|
|
createExtraWindow(arg.extraWindowHash);
|
2020-04-23 23:08:15 +02:00
|
|
|
});
|
|
|
|
|
2024-02-18 12:19:09 +02:00
|
|
|
async function createMainWindow(app: App) {
|
2024-08-31 23:08:19 +03:00
|
|
|
if ("setUserTasks" in app) {
|
|
|
|
app.setUserTasks([
|
|
|
|
{
|
|
|
|
program: process.execPath,
|
2025-01-09 18:07:02 +02:00
|
|
|
arguments: "--new-window",
|
2024-08-31 23:08:19 +03:00
|
|
|
iconPath: process.execPath,
|
|
|
|
iconIndex: 0,
|
2025-01-09 18:07:02 +02:00
|
|
|
title: "Open New Window",
|
|
|
|
description: "Open new window"
|
2024-08-31 23:08:19 +03:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
2024-12-22 15:42:15 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const windowStateKeeper = (await import("electron-window-state")).default; // should not be statically imported
|
2019-12-25 19:38:28 +01:00
|
|
|
|
2019-12-24 14:42:03 +01:00
|
|
|
const mainWindowState = windowStateKeeper({
|
2023-06-30 11:18:34 +02:00
|
|
|
// default window width & height, so it's usable on a 1600 * 900 display (including some extra panels etc.)
|
2019-12-24 14:42:03 +01:00
|
|
|
defaultWidth: 1200,
|
|
|
|
defaultHeight: 800
|
|
|
|
});
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const spellcheckEnabled = optionService.getOptionBool("spellCheckEnabled");
|
2024-12-22 15:42:15 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const { BrowserWindow } = await import("electron"); // should not be statically imported
|
2024-12-01 02:02:33 +02:00
|
|
|
|
2019-12-24 14:42:03 +01:00
|
|
|
mainWindow = new BrowserWindow({
|
|
|
|
x: mainWindowState.x,
|
|
|
|
y: mainWindowState.y,
|
|
|
|
width: mainWindowState.width,
|
|
|
|
height: mainWindowState.height,
|
2025-01-09 18:07:02 +02:00
|
|
|
title: "TriliumNext Notes",
|
2019-12-24 14:42:03 +01:00
|
|
|
webPreferences: {
|
2020-02-28 22:07:08 +01:00
|
|
|
nodeIntegration: true,
|
2021-03-09 22:36:41 +01:00
|
|
|
contextIsolation: false,
|
2022-05-29 17:42:09 +02:00
|
|
|
spellcheck: spellcheckEnabled,
|
|
|
|
webviewTag: true
|
2024-12-22 15:42:15 +02:00
|
|
|
},
|
2024-12-01 02:02:33 +02:00
|
|
|
icon: getIcon(),
|
2024-12-01 17:44:42 +02:00
|
|
|
...getWindowExtraOpts()
|
2019-12-24 14:42:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
mainWindowState.manage(mainWindow);
|
|
|
|
|
|
|
|
mainWindow.setMenuBarVisibility(false);
|
2022-12-21 15:19:05 +01:00
|
|
|
mainWindow.loadURL(`http://127.0.0.1:${port}`);
|
2025-01-09 18:07:02 +02:00
|
|
|
mainWindow.on("closed", () => (mainWindow = null));
|
2019-12-24 14:42:03 +01:00
|
|
|
|
2022-01-02 21:35:02 +01:00
|
|
|
configureWebContents(mainWindow.webContents, spellcheckEnabled);
|
2022-08-02 22:53:06 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
app.on("second-instance", (event, commandLine) => {
|
|
|
|
if (commandLine.includes("--new-window")) {
|
2024-12-22 15:42:15 +02:00
|
|
|
createExtraWindow("");
|
2024-08-28 06:28:02 +00:00
|
|
|
} else if (mainWindow) {
|
|
|
|
// Someone tried to run a second instance, we should focus our window.
|
|
|
|
// see www.ts "requestSingleInstanceLock" for the rest of this logic with explanation
|
2022-08-02 22:53:06 +02:00
|
|
|
if (mainWindow.isMinimized()) {
|
|
|
|
mainWindow.restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
mainWindow.focus();
|
|
|
|
}
|
|
|
|
});
|
2022-01-02 21:35:02 +01:00
|
|
|
}
|
2020-02-28 22:07:08 +01:00
|
|
|
|
2024-12-01 17:44:42 +02:00
|
|
|
function getWindowExtraOpts() {
|
|
|
|
const extraOpts: Partial<BrowserWindowConstructorOptions> = {};
|
2024-12-06 22:12:27 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (!optionService.getOptionBool("nativeTitleBarVisible")) {
|
2025-01-22 18:42:42 +01:00
|
|
|
if (isMac) {
|
2024-12-06 22:12:27 +02:00
|
|
|
extraOpts.titleBarStyle = "hiddenInset";
|
|
|
|
extraOpts.titleBarOverlay = true;
|
2025-01-22 18:42:42 +01:00
|
|
|
} else if (isWindows) {
|
2024-12-06 22:12:27 +02:00
|
|
|
extraOpts.titleBarStyle = "hidden";
|
|
|
|
extraOpts.titleBarOverlay = true;
|
|
|
|
} else {
|
|
|
|
// Linux or other platforms.
|
|
|
|
extraOpts.frame = false;
|
|
|
|
}
|
2024-12-01 17:44:42 +02:00
|
|
|
}
|
|
|
|
|
2024-12-07 00:56:19 +02:00
|
|
|
// Window effects (Mica)
|
2025-01-22 18:42:42 +01:00
|
|
|
if (optionService.getOptionBool("backgroundEffects") && isWindows) {
|
2024-12-07 09:58:41 +02:00
|
|
|
extraOpts.backgroundMaterial = "auto";
|
|
|
|
}
|
2024-12-07 00:56:19 +02:00
|
|
|
|
2024-12-01 17:44:42 +02:00
|
|
|
return extraOpts;
|
|
|
|
}
|
|
|
|
|
2024-02-18 12:19:09 +02:00
|
|
|
function configureWebContents(webContents: WebContents, spellcheckEnabled: boolean) {
|
|
|
|
remoteMain.enable(webContents);
|
2021-11-16 22:43:08 +01:00
|
|
|
|
2024-11-06 10:02:42 +08:00
|
|
|
webContents.setWindowOpenHandler((details) => {
|
2024-07-18 23:35:13 +03:00
|
|
|
async function openExternal() {
|
2025-01-09 18:07:02 +02:00
|
|
|
(await import("electron")).shell.openExternal(details.url);
|
2024-07-18 23:35:13 +03:00
|
|
|
}
|
2024-12-22 15:42:15 +02:00
|
|
|
|
2024-07-18 23:35:13 +03:00
|
|
|
openExternal();
|
2025-01-09 18:07:02 +02:00
|
|
|
return { action: "deny" };
|
2019-12-24 14:42:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// prevent drag & drop to navigate away from trilium
|
2025-01-09 18:07:02 +02:00
|
|
|
webContents.on("will-navigate", (ev, targetUrl) => {
|
2019-12-24 14:42:03 +01:00
|
|
|
const parsedUrl = url.parse(targetUrl);
|
|
|
|
|
|
|
|
// we still need to allow internal redirects from setup and migration pages
|
2025-01-09 18:07:02 +02:00
|
|
|
if (!["localhost", "127.0.0.1"].includes(parsedUrl.hostname || "") || (parsedUrl.path && parsedUrl.path !== "/" && parsedUrl.path !== "/?")) {
|
2019-12-24 14:42:03 +01:00
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2020-02-28 22:07:08 +01:00
|
|
|
|
|
|
|
if (spellcheckEnabled) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const languageCodes = optionService
|
|
|
|
.getOption("spellCheckLanguageCode")
|
|
|
|
.split(",")
|
|
|
|
.map((code) => code.trim());
|
2020-02-28 22:07:08 +01:00
|
|
|
|
|
|
|
webContents.session.setSpellCheckerLanguages(languageCodes);
|
|
|
|
}
|
2019-12-24 14:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getIcon() {
|
2025-01-22 19:08:38 +01:00
|
|
|
return path.join(dirname(fileURLToPath(import.meta.url)), "../../images/app-icons/png/256x256" + (isDev ? "-dev" : "") + ".png");
|
2019-12-24 14:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function createSetupWindow() {
|
2024-07-18 23:48:14 +03:00
|
|
|
const { BrowserWindow } = await import("electron"); // should not be statically imported
|
2019-12-24 14:42:03 +01:00
|
|
|
setupWindow = new BrowserWindow({
|
|
|
|
width: 800,
|
|
|
|
height: 800,
|
2025-01-09 18:07:02 +02:00
|
|
|
title: "TriliumNext Notes Setup",
|
2019-12-27 20:22:46 +01:00
|
|
|
icon: getIcon(),
|
|
|
|
webPreferences: {
|
|
|
|
// necessary for e.g. utils.isElectron()
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
2019-12-24 14:42:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
setupWindow.setMenuBarVisibility(false);
|
2022-12-21 15:19:05 +01:00
|
|
|
setupWindow.loadURL(`http://127.0.0.1:${port}`);
|
2025-01-09 18:07:02 +02:00
|
|
|
setupWindow.on("closed", () => (setupWindow = null));
|
2019-12-24 14:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function closeSetupWindow() {
|
|
|
|
if (setupWindow) {
|
|
|
|
setupWindow.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 21:42:41 +02:00
|
|
|
async function registerGlobalShortcuts() {
|
2024-07-18 23:48:14 +03:00
|
|
|
const { globalShortcut } = await import("electron");
|
2019-12-27 20:28:27 +01:00
|
|
|
|
2020-06-20 21:42:41 +02:00
|
|
|
await sqlInit.dbReady;
|
2019-12-27 20:28:27 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const allActions = keyboardActionsService.getKeyboardActions();
|
2019-12-27 20:28:27 +01:00
|
|
|
|
|
|
|
for (const action of allActions) {
|
|
|
|
if (!action.effectiveShortcuts) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const shortcut of action.effectiveShortcuts) {
|
2025-01-09 18:07:02 +02:00
|
|
|
if (shortcut.startsWith("global:")) {
|
2019-12-27 20:28:27 +01:00
|
|
|
const translatedShortcut = shortcut.substr(7);
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const result = globalShortcut.register(
|
|
|
|
translatedShortcut,
|
|
|
|
cls.wrap(() => {
|
|
|
|
if (!mainWindow) {
|
|
|
|
return;
|
|
|
|
}
|
2024-02-18 12:19:09 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
// window may be hidden / not in focus
|
|
|
|
mainWindow.focus();
|
2019-12-27 20:28:27 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
mainWindow.webContents.send("globalShortcut", action.actionName);
|
|
|
|
})
|
|
|
|
);
|
2019-12-27 20:28:27 +01:00
|
|
|
|
|
|
|
if (result) {
|
|
|
|
log.info(`Registered global shortcut ${translatedShortcut} for action ${action.actionName}`);
|
2025-01-09 18:07:02 +02:00
|
|
|
} else {
|
2019-12-27 20:28:27 +01:00
|
|
|
log.info(`Could not register global shortcut ${translatedShortcut}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-14 12:36:39 +00:00
|
|
|
function getMainWindow() {
|
|
|
|
return mainWindow;
|
|
|
|
}
|
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
2019-12-24 14:42:03 +01:00
|
|
|
createMainWindow,
|
|
|
|
createSetupWindow,
|
2019-12-27 20:28:27 +01:00
|
|
|
closeSetupWindow,
|
2021-11-14 12:36:39 +00:00
|
|
|
registerGlobalShortcuts,
|
|
|
|
getMainWindow
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|