| 
									
										
										
										
											2017-10-20 23:43:20 -04:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2018-01-28 22:18:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 20:27:30 +02:00
										 |  |  | const {app, globalShortcut, BrowserWindow} = require('electron'); | 
					
						
							| 
									
										
										
										
											2017-10-21 00:19:13 -04:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2018-02-15 22:17:18 -05:00
										 |  |  | const log = require('./src/services/log'); | 
					
						
							| 
									
										
										
										
											2019-03-31 11:21:44 +02:00
										 |  |  | const sqlInit = require('./src/services/sql_init'); | 
					
						
							| 
									
										
										
										
											2018-10-15 23:45:37 +02:00
										 |  |  | const cls = require('./src/services/cls'); | 
					
						
							| 
									
										
										
										
											2018-01-12 20:05:17 -05:00
										 |  |  | const url = require("url"); | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  | const port = require('./src/services/port'); | 
					
						
							| 
									
										
										
										
											2019-07-21 09:14:56 +02:00
										 |  |  | const env = require('./src/services/env'); | 
					
						
							| 
									
										
										
										
											2019-11-21 21:12:07 +01:00
										 |  |  | const keyboardActionsService = require('./src/services/keyboard_actions'); | 
					
						
							| 
									
										
										
										
											2018-11-21 11:01:03 +01:00
										 |  |  | const appIconService = require('./src/services/app_icon'); | 
					
						
							| 
									
										
										
										
											2019-02-12 22:42:40 +01:00
										 |  |  | const windowStateKeeper = require('electron-window-state'); | 
					
						
							| 
									
										
										
										
											2017-10-20 23:43:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Adds debug features like hotkeys for triggering dev tools and reload
 | 
					
						
							|  |  |  | require('electron-debug')(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 11:01:03 +01:00
										 |  |  | appIconService.installLocalAppIcon(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-20 23:43:20 -04:00
										 |  |  | // Prevent window being garbage collected
 | 
					
						
							|  |  |  | let mainWindow; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 22:19:07 -05:00
										 |  |  | require('electron-dl')({ saveAs: true }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-20 23:43:20 -04:00
										 |  |  | function onClosed() { | 
					
						
							|  |  |  |     // Dereference the window
 | 
					
						
							|  |  |  |     // For multiple windows store them in an array
 | 
					
						
							|  |  |  |     mainWindow = null; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  | async function createMainWindow() { | 
					
						
							| 
									
										
										
										
											2019-04-02 19:42:41 +02:00
										 |  |  |     await sqlInit.dbConnection; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 11:21:44 +02:00
										 |  |  |     // 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({ | 
					
						
							| 
									
										
										
										
											2019-02-12 22:42:40 +01:00
										 |  |  |         // default window width & height so it's usable on 1600 * 900 display (including some extra panels etc.)
 | 
					
						
							|  |  |  |         defaultWidth: 1200, | 
					
						
							|  |  |  |         defaultHeight: 800 | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 20:27:30 +02:00
										 |  |  |     const win = new BrowserWindow({ | 
					
						
							| 
									
										
										
										
											2019-02-12 22:42:40 +01:00
										 |  |  |         x: mainWindowState.x, | 
					
						
							|  |  |  |         y: mainWindowState.y, | 
					
						
							|  |  |  |         width: mainWindowState.width, | 
					
						
							|  |  |  |         height: mainWindowState.height, | 
					
						
							| 
									
										
										
										
											2017-12-27 17:39:41 -05:00
										 |  |  |         title: 'Trilium Notes', | 
					
						
							| 
									
										
										
										
											2019-05-23 19:59:23 +02:00
										 |  |  |         webPreferences: { | 
					
						
							|  |  |  |             nodeIntegration: true | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-07-21 09:14:56 +02:00
										 |  |  |         icon: path.join(__dirname, 'images/app-icons/png/256x256' + (env.isDev() ? '-dev' : '') + '.png') | 
					
						
							| 
									
										
										
										
											2017-10-20 23:43:20 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 22:42:40 +01:00
										 |  |  |     mainWindowState.manage(win); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 20:45:19 +02:00
										 |  |  |     win.setMenuBarVisibility(false); | 
					
						
							| 
									
										
										
										
											2019-06-24 20:26:28 +02:00
										 |  |  |     win.loadURL('http://127.0.0.1:' + await port); | 
					
						
							| 
									
										
										
										
											2017-10-20 23:43:20 -04:00
										 |  |  |     win.on('closed', onClosed); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     win.webContents.on('new-window', (e, url) => { | 
					
						
							| 
									
										
										
										
											2018-08-14 23:07:50 +02:00
										 |  |  |         if (url !== win.webContents.getURL()) { | 
					
						
							| 
									
										
										
										
											2017-10-20 23:43:20 -04:00
										 |  |  |             e.preventDefault(); | 
					
						
							|  |  |  |             require('electron').shell.openExternal(url); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-12 18:42:00 -05:00
										 |  |  |     // prevent drag & drop to navigate away from trilium
 | 
					
						
							| 
									
										
										
										
											2018-01-12 20:05:17 -05:00
										 |  |  |     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-26 20:49:17 +02:00
										 |  |  |         if (!['localhost', '127.0.0.1'].includes(parsedUrl.hostname) || (parsedUrl.path && parsedUrl.path !== '/')) { | 
					
						
							| 
									
										
										
										
											2018-01-12 20:05:17 -05:00
										 |  |  |             ev.preventDefault(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-12 18:42:00 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-20 23:43:20 -04:00
										 |  |  |     return win; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | app.on('window-all-closed', () => { | 
					
						
							|  |  |  |     if (process.platform !== 'darwin') { | 
					
						
							|  |  |  |         app.quit(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-13 20:28:14 +01:00
										 |  |  |     else if (process.platform === 'win32') { | 
					
						
							|  |  |  |         app.exit(0); // attempt to fix the issue when app.quite() won't terminate processes on windows
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-20 23:43:20 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | app.on('activate', () => { | 
					
						
							|  |  |  |     if (!mainWindow) { | 
					
						
							|  |  |  |         mainWindow = createMainWindow(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-21 21:12:07 +01:00
										 |  |  | async function registerGlobalShortcuts() { | 
					
						
							| 
									
										
										
										
											2019-11-24 22:42:54 +01:00
										 |  |  |     await sqlInit.dbReady; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-21 21:12:07 +01:00
										 |  |  |     const allActions = await keyboardActionsService.getKeyboardActions(); | 
					
						
							| 
									
										
										
										
											2018-11-19 17:16:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-21 21:12:07 +01:00
										 |  |  |     for (const action of allActions) { | 
					
						
							|  |  |  |         if (!action.effectiveShortcuts) { | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-12 23:53:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-21 21:12:07 +01:00
										 |  |  |         for (const shortcut of action.effectiveShortcuts) { | 
					
						
							|  |  |  |             if (shortcut.startsWith('global:')) { | 
					
						
							|  |  |  |                 const translatedShortcut = shortcut.substr(7); | 
					
						
							| 
									
										
										
										
											2018-02-12 23:53:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-21 21:12:07 +01:00
										 |  |  |                 const result = globalShortcut.register(translatedShortcut, cls.wrap(async () => { | 
					
						
							|  |  |  |                     // window may be hidden / not in focus
 | 
					
						
							|  |  |  |                     mainWindow.focus(); | 
					
						
							| 
									
										
										
										
											2018-02-15 22:17:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-21 21:12:07 +01:00
										 |  |  |                     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}`); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-15 22:17:18 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-21 21:12:07 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | app.on('ready', async () => { | 
					
						
							|  |  |  |     app.setAppUserModelId('com.github.zadam.trilium'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mainWindow = await createMainWindow(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     registerGlobalShortcuts(); | 
					
						
							| 
									
										
										
										
											2018-02-12 23:53:00 -05:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | app.on('will-quit', () => { | 
					
						
							|  |  |  |     globalShortcut.unregisterAll(); | 
					
						
							| 
									
										
										
										
											2017-10-20 23:43:20 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-31 08:03:25 -05:00
										 |  |  | require('./src/www'); |