This commit is contained in:
Araxeus
2021-04-30 04:29:01 +03:00
parent 49e51de274
commit 54cbe3faa4
2 changed files with 38 additions and 28 deletions

View File

@ -36,13 +36,15 @@ function registerShortcuts(win, options) {
if (global) { if (global) {
for (const action in global) { for (const action in global) {
if (!global[action]) { if (!global[action]) {
return; //accelerator is empty continue; //accelerator is empty
} }
console.debug("Registering global shortcut", global[action], ":", action); console.debug("Registering global shortcut", global[action], ":", action);
if (!songControls[action]) { if (!songControls[action]) {
console.warn("Invalid action", action); console.warn("Invalid action", action);
return; continue;
} }
_registerGlobalShortcut(win.webContents, global[action], songControls[action]); _registerGlobalShortcut(win.webContents, global[action], songControls[action]);
} }
} }
@ -50,13 +52,15 @@ function registerShortcuts(win, options) {
if (local) { if (local) {
for (const action in local) { for (const action in local) {
if (!local[action]) { if (!local[action]) {
return; //accelerator is empty continue; //accelerator is empty
} }
console.debug("Registering local shortcut", local[action], ":", action); console.debug("Registering local shortcut", local[action], ":", action);
if (!songControls[action]) { if (!songControls[action]) {
console.warn("Invalid action", action); console.warn("Invalid action", action);
return; continue;
} }
_registerLocalShortcut(win, local[action], songControls[action]); _registerLocalShortcut(win, local[action], songControls[action]);
} }
} }
@ -67,16 +71,18 @@ function updateOptions(options) {
let updated = false; let updated = false;
for (const optionType of ["global", "local"]) { for (const optionType of ["global", "local"]) {
if (Array.isArray(options[optionType])) { if (Array.isArray(options[optionType])) {
const updatedOptions = {} const updatedOptions = {};
for (const obj of options[optionType]) { for (const optionObject of options[optionType]) {
if (obj.action && obj.shortcut) { if (optionObject.action && optionObject.shortcut) {
updatedOptions[obj.action] = obj.shortcut; updatedOptions[optionObject.action] = optionObject.shortcut;
} }
} }
options[optionType] = updatedOptions; options[optionType] = updatedOptions;
updated = true; updated = true;
} }
} }
if (updated) { if (updated) {
setOptions("shortcuts", options); setOptions("shortcuts", options);
} }

View File

@ -4,13 +4,6 @@ const prompt = require("custom-electron-prompt");
const path = require("path"); const path = require("path");
const is = require("electron-is"); const is = require("electron-is");
function setOption(options, key = null, newValue = null) {
if (key && newValue) {
options[key] = newValue;
}
setOptions("shortcuts", options)
}
module.exports = (win, options) => [ module.exports = (win, options) => [
{ {
label: "Set Global Song Controls", label: "Set Global Song Controls",
@ -20,11 +13,19 @@ module.exports = (win, options) => [
label: "Override MediaKeys", label: "Override MediaKeys",
type: "checkbox", type: "checkbox",
checked: options.overrideMediaKeys, checked: options.overrideMediaKeys,
click: (item) => setOption(options, "overrideMediaKeys", item.checked) click: item => setOption(options, "overrideMediaKeys", item.checked)
} }
]; ];
const kb = (label_, value_, default_) => { return { value: value_, label: label_, default: default_ || undefined } }; function setOption(options, key = null, newValue = null) {
if (key && newValue) {
options[key] = newValue;
}
setOptions("shortcuts", options);
}
const kb = (label_, value_, default_) => { return { value: value_, label: label_, default: default_ || undefined }; };
const iconPath = path.join(process.cwd(), "assets", "youtube-music-tray.png"); const iconPath = path.join(process.cwd(), "assets", "youtube-music-tray.png");
function promptKeybind(options, win) { function promptKeybind(options, win) {
@ -36,11 +37,12 @@ function promptKeybind(options, win) {
keybindOptions: [ keybindOptions: [
kb("Previous", "previous", options.global?.previous), kb("Previous", "previous", options.global?.previous),
kb("Play / Pause", "playPause", options.global?.playPause), kb("Play / Pause", "playPause", options.global?.playPause),
kb("Next", "next", options.global?.next), kb("Next", "next", options.global?.next)
], ],
customStylesheet: "dark", customStylesheet: "dark",
height: 250 height: 250
}; };
if (!is.macOS()) { if (!is.macOS()) {
Object.assign(promptOptions, { Object.assign(promptOptions, {
frame: false, frame: false,
@ -49,15 +51,17 @@ function promptKeybind(options, win) {
height: 270 height: 270
}); });
} }
prompt(promptOptions, win) prompt(promptOptions, win)
.then(output => { .then(output => {
if (output) { if (output) {
for (const keybindObj of output) { for (const keybindObject of output) {
options.global[keybindObj.value] = keybindObj.accelerator; options.global[keybindObject.value] = keybindObject.accelerator;
}
setOption(options);
} }
setOption(options); // else -> pressed cancel
} })
//else = pressed cancel .catch(console.error);
}) }
.catch(console.error)
}