| 
									
										
										
										
											2017-10-20 23:43:20 -04:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2018-01-28 22:18:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 14:42:03 +01:00
										 |  |  | const {app, globalShortcut} = require('electron'); | 
					
						
							| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											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-12-24 14:42:03 +01:00
										 |  |  | const windowService = require('./src/services/window'); | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 22:19:07 -05:00
										 |  |  | require('electron-dl')({ saveAs: true }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-20 23:43:20 -04:00
										 |  |  | 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
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 14:42:03 +01:00
										 |  |  |     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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await windowService.createMainWindow(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         await windowService.createSetupWindow(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-21 21:12:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 14:42:03 +01:00
										 |  |  |     await 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'); |