support global shortcuts with global: prefix

This commit is contained in:
zadam
2019-11-21 21:12:07 +01:00
parent 00c295e4bf
commit 0e5028acd3
11 changed files with 401 additions and 403 deletions

View File

@@ -8,6 +8,7 @@ const cls = require('./src/services/cls');
const url = require("url");
const port = require('./src/services/port');
const env = require('./src/services/env');
const keyboardActionsService = require('./src/services/keyboard_actions');
const appIconService = require('./src/services/app_icon');
const windowStateKeeper = require('electron-window-state');
@@ -95,21 +96,42 @@ app.on('activate', () => {
}
});
async function registerGlobalShortcuts() {
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');
mainWindow = await createMainWindow();
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');
}));
if (!result) {
log.error("Could not register global shortcut CTRL+ALT+P");
}
registerGlobalShortcuts();
});
app.on('will-quit', () => {