trilium/electron.js

137 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-10-21 11:43:20 +08:00
'use strict';
2018-01-29 11:18:14 +08:00
const {app, globalShortcut, BrowserWindow} = require('electron');
2017-10-21 12:19:13 +08:00
const path = require('path');
const log = require('./src/services/log');
const sqlInit = require('./src/services/sql_init');
2018-10-16 05:45:37 +08:00
const cls = require('./src/services/cls');
const url = require("url");
const port = require('./src/services/port');
2019-07-21 15:14:56 +08:00
const env = require('./src/services/env');
const appIconService = require('./src/services/app_icon');
const windowStateKeeper = require('electron-window-state');
2017-10-21 11:43:20 +08:00
// Adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')();
appIconService.installLocalAppIcon();
2017-10-21 11:43:20 +08:00
// Prevent window being garbage collected
let mainWindow;
require('electron-dl')({ saveAs: true });
// contextMenu({
// menu: (actions, params, browserWindow) => [
// actions.cut(),
// actions.copy(),
// actions.copyLink(),
// actions.paste(),
// {
// label: 'Search DuckDuckGo for “{selection}”',
// // Only show it when right-clicking text
// visible: params.selectionText.trim().length > 0,
// click: () => {
// const {shell} = require('electron');
//
// shell.openExternal(`https://duckduckgo.com?q=${encodeURIComponent(params.selectionText)}`);
// }
// },
// actions.inspect()
// ]
// });
2019-07-01 00:56:46 +08:00
2017-10-21 11:43:20 +08:00
function onClosed() {
// Dereference the window
// For multiple windows store them in an array
mainWindow = null;
}
async function createMainWindow() {
await sqlInit.dbConnection;
// if schema doesn't exist -> setup process
// if schema exists, then we need to wait until the migration process is finished
if (await sqlInit.schemaExists()) {
await sqlInit.dbReady;
}
const mainWindowState = windowStateKeeper({
// default window width & height so it's usable on 1600 * 900 display (including some extra panels etc.)
defaultWidth: 1200,
defaultHeight: 800
});
const win = new BrowserWindow({
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
2017-12-28 06:39:41 +08:00
title: 'Trilium Notes',
2019-05-24 01:59:23 +08:00
webPreferences: {
nodeIntegration: true
},
2019-07-21 15:14:56 +08:00
icon: path.join(__dirname, 'images/app-icons/png/256x256' + (env.isDev() ? '-dev' : '') + '.png')
2017-10-21 11:43:20 +08:00
});
mainWindowState.manage(win);
2019-06-20 02:45:19 +08:00
win.setMenuBarVisibility(false);
win.loadURL('http://127.0.0.1:' + await port);
2017-10-21 11:43:20 +08:00
win.on('closed', onClosed);
win.webContents.on('new-window', (e, url) => {
if (url !== win.webContents.getURL()) {
2017-10-21 11:43:20 +08:00
e.preventDefault();
require('electron').shell.openExternal(url);
}
});
// prevent drag & drop to navigate away from trilium
win.webContents.on('will-navigate', (ev, targetUrl) => {
const parsedUrl = url.parse(targetUrl);
// we still need to allow internal redirects from setup and migration pages
2019-06-27 02:49:17 +08:00
if (!['localhost', '127.0.0.1'].includes(parsedUrl.hostname) || (parsedUrl.path && parsedUrl.path !== '/')) {
ev.preventDefault();
}
});
2017-10-21 11:43:20 +08:00
return win;
}
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (!mainWindow) {
mainWindow = createMainWindow();
}
});
2018-10-16 05:45:37 +08:00
app.on('ready', async () => {
2018-11-20 00:16:22 +08:00
app.setAppUserModelId('com.github.zadam.trilium');
2018-10-16 05:45:37 +08:00
mainWindow = await createMainWindow();
2018-10-16 05:45:37 +08:00
const result = globalShortcut.register('CommandOrControl+Alt+P', cls.wrap(async () => {
// window may be hidden / not in focus
mainWindow.focus();
mainWindow.webContents.send('create-day-sub-note');
2018-10-16 05:45:37 +08:00
}));
if (!result) {
log.error("Could not register global shortcut CTRL+ALT+P");
}
});
app.on('will-quit', () => {
globalShortcut.unregisterAll();
2017-10-21 11:43:20 +08:00
});
2018-01-31 21:03:25 +08:00
require('./src/www');