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 { globalShortcut } = require("electron");
const electronLocalshortcut = require("electron-localshortcut"); const electronLocalshortcut = require("electron-localshortcut");
const { setOptions } = require("../../config/plugins");
const getSongControls = require("../../providers/song-controls"); const getSongControls = require("../../providers/song-controls");
@ -19,6 +20,8 @@ function registerShortcuts(win, options) {
const songControls = getSongControls(win); const songControls = getSongControls(win);
const { playPause, next, previous, search } = songControls; const { playPause, next, previous, search } = songControls;
updateOptions(options);
if (options.overrideMediaKeys) { if (options.overrideMediaKeys) {
_registerGlobalShortcut(win.webContents, "MediaPlayPause", playPause); _registerGlobalShortcut(win.webContents, "MediaPlayPause", playPause);
_registerGlobalShortcut(win.webContents, "MediaNextTrack", next); _registerGlobalShortcut(win.webContents, "MediaNextTrack", next);
@ -29,24 +32,54 @@ function registerShortcuts(win, options) {
_registerLocalShortcut(win, "CommandOrControl+L", search); _registerLocalShortcut(win, "CommandOrControl+L", search);
const { global, local } = options; 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]); if (global) {
}); for (const action in global) {
(local || []).forEach(({ shortcut, action }) => { if (!global[action]) {
console.debug("Registering local shortcut", shortcut, ":", action); return; //accelerator is empty
if (!action || !songControls[action]) { }
console.warn("Invalid action", action); console.debug("Registering global shortcut", global[action], ":", action);
return; 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; module.exports = registerShortcuts;

View File

@ -1,5 +1,6 @@
const { setOptions } = require("../../config/plugins"); const { setOptions } = require("../../config/plugins");
const prompt = require("custom-electron-prompt"); const prompt = require("custom-electron-prompt");
const path = require("path"); const path = require("path");
const is = require("electron-is"); const is = require("electron-is");
@ -23,74 +24,19 @@ module.exports = (win, options) => [
} }
]; ];
//will not be needed if globalKeybinds will be an object const kb = (label_, value_, default_) => { return { value: value_, label: label_, default: default_ || undefined } };
function getGlobalKeybinds(options) {
let playPause, next, previous;
if (options.global) {
for (const global of options.global) {
switch (global.action) {
case "playPause":
playPause = global.shortcut;
break;
case "previous":
previous = global.shortcut;
break;
case "next":
next = global.shortcut;
}
}
}
return { playPause, next, previous };
}
//will not be needed if globalKeybinds will be an object
function setGlobalKeybinds(options, newShortcuts) {
let didSet = {};
for (const shortcut in newShortcuts) {
didSet[shortcut] = false;
}
if (!options.global) {
options.global = [];
}
for (let i in options.global) {
switch (options.global[i].action) {
case "playPause":
options.global[i].shortcut = newShortcuts.playPause;
didSet["playPause"] = true;
break;
case "previous":
options.global[i].shortcut = newShortcuts.previous;
didSet["previous"] = true;
break;
case "next":
options.global[i].shortcut = newShortcuts.next;
didSet["next"] = true;
break;
}
}
for (const action in didSet) {
if (!didSet[action]) {
options.global.push({ action: action, shortcut: newShortcuts[action] });
}
}
options.global.forEach((obj) => console.log(obj));
setOption(options);
}
const kb = (label_, value_, default_) => { return { value: value_, label: label_, default: default_ || "" } };
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) {
let globalKeybinds = getGlobalKeybinds(options);
let promptOptions = { let promptOptions = {
title: "Global Keybinds", title: "Global Keybinds",
icon: iconPath, icon: iconPath,
label: "Choose Global Keybinds for Songs Control:", label: "Choose Global Keybinds for Songs Control:",
type: "keybind", type: "keybind",
keybindOptions: [ keybindOptions: [
kb("Previous", "previous", globalKeybinds.previous), kb("Previous", "previous", options.global?.previous),
kb("Play / Pause", "playPause", globalKeybinds.playPause), kb("Play / Pause", "playPause", options.global?.playPause),
kb("Next", "next", globalKeybinds.next), kb("Next", "next", options.global?.next),
], ],
customStylesheet: "dark", customStylesheet: "dark",
height: 250 height: 250
@ -106,11 +52,10 @@ function promptKeybind(options, win) {
prompt(promptOptions, win) prompt(promptOptions, win)
.then(output => { .then(output => {
if (output) { if (output) {
let toSave = {};
for (const keybindObj of output) { for (const keybindObj of output) {
toSave[keybindObj.value] = keybindObj.accelerator; options.global[keybindObj.value] = keybindObj.accelerator;
} }
setGlobalKeybinds(options, toSave); setOption(options);
} }
//else = pressed cancel //else = pressed cancel
}) })