fix registration of global shortcuts, fixes #786

This commit is contained in:
zadam
2019-12-27 20:28:27 +01:00
parent d2177cd517
commit 4f06b6de78
2 changed files with 41 additions and 37 deletions

View File

@@ -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
};