mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	fix registration of global shortcuts, fixes #786
This commit is contained in:
		
							
								
								
									
										37
									
								
								electron.js
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								electron.js
									
									
									
									
									
								
							| @@ -1,10 +1,7 @@ | ||||
| 'use strict'; | ||||
|  | ||||
| const {app, globalShortcut} = require('electron'); | ||||
| const log = require('./src/services/log'); | ||||
| const sqlInit = require('./src/services/sql_init'); | ||||
| const cls = require('./src/services/cls'); | ||||
| const keyboardActionsService = require('./src/services/keyboard_actions'); | ||||
| const appIconService = require('./src/services/app_icon'); | ||||
| const windowService = require('./src/services/window'); | ||||
|  | ||||
| @@ -24,38 +21,6 @@ app.on('window-all-closed', () => { | ||||
|     } | ||||
| }); | ||||
|  | ||||
| async function registerGlobalShortcuts() { | ||||
|     await sqlInit.dbReady; | ||||
|  | ||||
|     const allActions = await 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); | ||||
|  | ||||
|                 const result = globalShortcut.register(translatedShortcut, cls.wrap(async () => { | ||||
|                     // 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}`); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| app.on('ready', async () => { | ||||
|     app.setAppUserModelId('com.github.zadam.trilium'); | ||||
|  | ||||
| @@ -72,7 +37,7 @@ app.on('ready', async () => { | ||||
|         await windowService.createSetupWindow(); | ||||
|     } | ||||
|  | ||||
|     await registerGlobalShortcuts(); | ||||
|     await windowService.registerGlobalShortcuts(); | ||||
| }); | ||||
|  | ||||
| app.on('will-quit', () => { | ||||
|   | ||||
| @@ -3,6 +3,10 @@ const url = require("url"); | ||||
| const port = require('./port'); | ||||
| const optionService = require('./options'); | ||||
| const env = require('./env'); | ||||
| const log = require('./log'); | ||||
| const sqlInit = require('./sql_init'); | ||||
| const cls = require('./cls'); | ||||
| const keyboardActionsService = require('./keyboard_actions'); | ||||
|  | ||||
| // Prevent window being garbage collected | ||||
| /** @type {Electron.BrowserWindow} */ | ||||
| @@ -85,8 +89,43 @@ function closeSetupWindow() { | ||||
|     } | ||||
| } | ||||
|  | ||||
| async function registerGlobalShortcuts() { | ||||
|     const {globalShortcut} = require('electron'); | ||||
|  | ||||
|     await sqlInit.dbReady; | ||||
|  | ||||
|     const allActions = await 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); | ||||
|  | ||||
|                 const result = globalShortcut.register(translatedShortcut, cls.wrap(async () => { | ||||
|                     // 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}`); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| module.exports = { | ||||
|     createMainWindow, | ||||
|     createSetupWindow, | ||||
|     closeSetupWindow | ||||
|     closeSetupWindow, | ||||
|     registerGlobalShortcuts | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user