update shortcuts config

This commit is contained in:
Araxeus
2021-04-30 04:13:03 +03:00
parent e456035f29
commit 49e51de274
2 changed files with 55 additions and 77 deletions

View File

@ -1,5 +1,6 @@
const { globalShortcut } = require("electron");
const electronLocalshortcut = require("electron-localshortcut");
const { setOptions } = require("../../config/plugins");
const getSongControls = require("../../providers/song-controls");
@ -19,6 +20,8 @@ function registerShortcuts(win, options) {
const songControls = getSongControls(win);
const { playPause, next, previous, search } = songControls;
updateOptions(options);
if (options.overrideMediaKeys) {
_registerGlobalShortcut(win.webContents, "MediaPlayPause", playPause);
_registerGlobalShortcut(win.webContents, "MediaNextTrack", next);
@ -29,24 +32,54 @@ function registerShortcuts(win, options) {
_registerLocalShortcut(win, "CommandOrControl+L", search);
const { global, local } = options;
(global || []).forEach(({ shortcut, action }) => {
console.debug("Registering global shortcut", shortcut, ":", action);
if (!action || !songControls[action]) {
console.warn("Invalid action", action);
return;
}
_registerGlobalShortcut(win.webContents, shortcut, songControls[action]);
});
(local || []).forEach(({ shortcut, action }) => {
console.debug("Registering local shortcut", shortcut, ":", action);
if (!action || !songControls[action]) {
console.warn("Invalid action", action);
return;
if (global) {
for (const action in global) {
if (!global[action]) {
return; //accelerator is empty
}
console.debug("Registering global shortcut", global[action], ":", action);
if (!songControls[action]) {
console.warn("Invalid action", action);
return;
}
_registerGlobalShortcut(win.webContents, global[action], songControls[action]);
}
}
_registerLocalShortcut(win, shortcut, songControls[action]);
});
if (local) {
for (const action in local) {
if (!local[action]) {
return; //accelerator is empty
}
console.debug("Registering local shortcut", local[action], ":", action);
if (!songControls[action]) {
console.warn("Invalid action", action);
return;
}
_registerLocalShortcut(win, local[action], songControls[action]);
}
}
}
/** Update options to new format */
function updateOptions(options) {
let updated = false;
for (const optionType of ["global", "local"]) {
if (Array.isArray(options[optionType])) {
const updatedOptions = {}
for (const obj of options[optionType]) {
if (obj.action && obj.shortcut) {
updatedOptions[obj.action] = obj.shortcut;
}
}
options[optionType] = updatedOptions;
updated = true;
}
}
if (updated) {
setOptions("shortcuts", options);
}
}
module.exports = registerShortcuts;