Notes/src/services/window.ts

250 lines
7.6 KiB
TypeScript
Raw Normal View History

import path from "path";
import url from "url";
import port from "./port.js";
import optionService from "./options.js";
import env from "./env.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";
import { App, BrowserWindow, BrowserWindowConstructorOptions, WebContents, ipcMain } from 'electron';
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;
2024-02-18 12:19:09 +02:00
async function createExtraWindow(extraWindowHash: string) {
2022-01-02 21:35:02 +01:00
const spellcheckEnabled = optionService.getOptionBool('spellCheckEnabled');
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,
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
},
...getWindowExtraOpts(),
2020-04-23 23:08:15 +02:00
icon: getIcon()
});
win.setMenuBarVisibility(false);
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
}
ipcMain.on('create-extra-window', (event, arg) => {
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,
arguments: '--new-window',
iconPath: process.execPath,
iconIndex: 0,
title: 'Open New Window',
description: 'Open new window'
}
]);
}
2024-08-28 06:28:02 +00:00
const windowStateKeeper = (await import('electron-window-state')).default; // should not be statically imported
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.)
defaultWidth: 1200,
defaultHeight: 800
});
2020-06-20 12:31:38 +02:00
const spellcheckEnabled = optionService.getOptionBool('spellCheckEnabled');
const { BrowserWindow } = (await import('electron')); // should not be statically imported
2024-08-28 06:30:25 +00:00
mainWindow = new BrowserWindow({
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
title: 'TriliumNext Notes',
webPreferences: {
nodeIntegration: true,
2021-03-09 22:36:41 +01:00
contextIsolation: false,
2022-05-29 17:42:09 +02:00
spellcheck: spellcheckEnabled,
webviewTag: true
},
icon: getIcon(),
...getWindowExtraOpts()
});
mainWindowState.manage(mainWindow);
mainWindow.setMenuBarVisibility(false);
mainWindow.loadURL(`http://127.0.0.1:${port}`);
mainWindow.on('closed', () => mainWindow = null);
2022-01-02 21:35:02 +01:00
configureWebContents(mainWindow.webContents, spellcheckEnabled);
2024-08-28 06:28:02 +00:00
app.on('second-instance', (event, commandLine) => {
if (commandLine.includes('--new-window')) {
createExtraWindow("");
} 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
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
}
});
2022-01-02 21:35:02 +01:00
}
function getWindowExtraOpts() {
const extraOpts: Partial<BrowserWindowConstructorOptions> = {};
const isMac = (process.platform === "darwin");
const isWindows = (process.platform === "win32");
if (!optionService.getOptionBool('nativeTitleBarVisible')) {
if (isMac) {
extraOpts.titleBarStyle = "hiddenInset";
extraOpts.titleBarOverlay = true;
} else if (isWindows) {
extraOpts.titleBarStyle = "hidden";
extraOpts.titleBarOverlay = true;
} else {
// Linux or other platforms.
extraOpts.frame = false;
}
}
2024-12-07 00:56:19 +02:00
// Window effects (Mica)
2024-12-09 22:29:04 +02:00
if (optionService.getOptionBool('backgroundEffects') && isWindows) {
extraOpts.backgroundMaterial = "auto";
}
2024-12-07 00:56:19 +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
webContents.setWindowOpenHandler((details) => {
async function openExternal() {
(await import('electron')).shell.openExternal(details.url);
}
openExternal();
2023-07-14 21:59:43 +02:00
return { action: 'deny' }
});
// prevent drag & drop to navigate away from trilium
webContents.on('will-navigate', (ev, targetUrl) => {
const parsedUrl = url.parse(targetUrl);
// we still need to allow internal redirects from setup and migration pages
2024-02-18 12:19:09 +02:00
if (!['localhost', '127.0.0.1'].includes(parsedUrl.hostname || "") || (parsedUrl.path && parsedUrl.path !== '/' && parsedUrl.path !== '/?')) {
ev.preventDefault();
}
});
if (spellcheckEnabled) {
2020-06-20 12:31:38 +02:00
const languageCodes = (optionService.getOption('spellCheckLanguageCode'))
.split(',')
.map(code => code.trim());
webContents.session.setSpellCheckerLanguages(languageCodes);
}
}
function getIcon() {
2024-07-19 00:18:35 +03:00
return path.join(dirname(fileURLToPath(import.meta.url)), '../../images/app-icons/png/256x256' + (env.isDev() ? '-dev' : '') + '.png');
}
async function createSetupWindow() {
const { BrowserWindow } = await import("electron"); // should not be statically imported
setupWindow = new BrowserWindow({
width: 800,
height: 800,
title: 'TriliumNext Notes Setup',
icon: getIcon(),
webPreferences: {
// necessary for e.g. utils.isElectron()
nodeIntegration: true
}
});
setupWindow.setMenuBarVisibility(false);
setupWindow.loadURL(`http://127.0.0.1:${port}`);
setupWindow.on('closed', () => setupWindow = null);
}
function closeSetupWindow() {
if (setupWindow) {
setupWindow.close();
}
}
2020-06-20 21:42:41 +02:00
async function registerGlobalShortcuts() {
const { globalShortcut } = await import("electron");
2020-06-20 21:42:41 +02:00
await sqlInit.dbReady;
2020-06-20 12:31:38 +02:00
const allActions = keyboardActionsService.getKeyboardActions();
for (const action of allActions) {
if (!action.effectiveShortcuts) {
continue;
}
for (const shortcut of action.effectiveShortcuts) {
if (shortcut.startsWith('global:')) {
const translatedShortcut = shortcut.substr(7);
2020-06-20 12:31:38 +02:00
const result = globalShortcut.register(translatedShortcut, cls.wrap(() => {
2024-02-18 12:19:09 +02:00
if (!mainWindow) {
return;
}
// window may be hidden / not in focus
mainWindow.focus();
mainWindow.webContents.send('globalShortcut', action.actionName);
}));
if (result) {
log.info(`Registered global shortcut ${translatedShortcut} for action ${action.actionName}`);
}
else {
log.info(`Could not register global shortcut ${translatedShortcut}`);
}
}
}
}
}
function getMainWindow() {
return mainWindow;
}
export default {
createMainWindow,
createSetupWindow,
closeSetupWindow,
registerGlobalShortcuts,
getMainWindow
2020-06-20 12:31:38 +02:00
};