use spread operator + async await

This commit is contained in:
Araxeus
2021-05-10 00:40:02 +03:00
parent 5418ef7ae2
commit f910593fb6
3 changed files with 52 additions and 65 deletions

25
menu.js
View File

@ -312,7 +312,7 @@ module.exports.setApplicationMenu = (win) => {
const iconPath = path.join(__dirname, "assets", "youtube-music-tray.png"); const iconPath = path.join(__dirname, "assets", "youtube-music-tray.png");
const example = "Example: 'socks5://127.0.0.1:9999'"; const example = "Example: 'socks5://127.0.0.1:9999'";
function setProxy(item, win) { async function setProxy(item, win) {
let options = { let options = {
title: 'Set Proxy', title: 'Set Proxy',
label: 'Enter Proxy Address: (leave empty to disable)', label: 'Enter Proxy Address: (leave empty to disable)',
@ -325,22 +325,19 @@ function setProxy(item, win) {
customStylesheet: "dark", customStylesheet: "dark",
width: 450, width: 450,
}; };
//TODO: custom bar on prompt need testing on macOS
if (!is.macOS()) { if (!is.macOS()) {
Object.assign(options, { options = {
...options,
frame: false, frame: false,
customScript: path.join(__dirname, "plugins", "in-app-menu", "prompt-custom-titlebar.js"), customScript: path.join(__dirname, "plugins", "in-app-menu", "prompt-custom-titlebar.js"),
enableRemoteModule: true, enableRemoteModule: true,
}); };
} }
prompt(options, win) const output = await prompt(options, win);
.then(output => { if (output !== null && output !== example) {
if (output !== null && output !== example) { config.set("options.proxy", output);
config.set("options.proxy", output); item.checked = output !== "";
item.checked = output !== ""; } else { //user pressed cancel
} else { //user pressed cancel item.checked = !item.checked; //reset checkbox
item.checked = !item.checked; //reset checkbox }
}
})
.catch(console.error);
} }

View File

@ -37,29 +37,25 @@ const customTitlebarPath = path.join(app.getAppPath(), "plugins", "in-app-menu",
// Helper function for globalShortcuts prompt // Helper function for globalShortcuts prompt
const kb = (label_, value_, default_) => { return { value: value_, label: label_, default: default_ || undefined }; }; const kb = (label_, value_, default_) => { return { value: value_, label: label_, default: default_ || undefined }; };
function promptVolumeSteps(win, options) { async function promptVolumeSteps(win, options) {
const promptOptions = { const promptOptions = setupPromptOptions({
title: "Volume Steps", title: "Volume Steps",
label: "Choose Volume Increase/Decrease Steps", label: "Choose Volume Increase/Decrease Steps",
value: options.steps || 1, value: options.steps || 1,
type: "counter", type: "counter",
counterOptions: { minimum: 0, maximum: 100, multiFire: true }, counterOptions: { minimum: 0, maximum: 100, multiFire: true },
width: 380 width: 380
}; });
setupPromptOptions(promptOptions); const output = await prompt(promptOptions, win)
if (output || output === 0) { // 0 is somewhat valid
prompt(promptOptions, win) options.steps = output;
.then(output => { setOptions("precise-volume", options);
if (output || output === 0) { // 0 is somehow valid }
options.steps = output;
setOptions("precise-volume", options);
}
}).catch(console.error);
} }
function promptGlobalShortcuts(win, options, item) { async function promptGlobalShortcuts(win, options, item) {
const promptOptions = { const promptOptions = setupPromptOptions({
title: "Global Volume Keybinds", title: "Global Volume Keybinds",
label: "Choose Global Volume Keybinds:", label: "Choose Global Volume Keybinds:",
type: "keybind", type: "keybind",
@ -67,43 +63,40 @@ function promptGlobalShortcuts(win, options, item) {
kb("Increase Volume", "volumeUp", options.globalShortcuts?.volumeUp), kb("Increase Volume", "volumeUp", options.globalShortcuts?.volumeUp),
kb("Decrease Volume", "volumeDown", options.globalShortcuts?.volumeDown) kb("Decrease Volume", "volumeDown", options.globalShortcuts?.volumeDown)
] ]
}; });
setupPromptOptions(promptOptions); const output = await prompt(promptOptions, win)
if (output) {
for (const keybindObject of output) {
options.globalShortcuts[keybindObject.value] = keybindObject.accelerator;
}
prompt(promptOptions, win) setOptions("precise-volume", options);
.then(output => {
if (output) {
for (const keybindObject of output) {
options.globalShortcuts[keybindObject.value] = keybindObject.accelerator;
}
setOptions("precise-volume", options); item.checked = !!options.globalShortcuts.volumeUp || !!options.globalShortcuts.volumeDown;
} else {
item.checked = !!options.globalShortcuts.volumeUp || !!options.globalShortcuts.volumeDown; // Reset checkbox if prompt was canceled
} else { item.checked = !item.checked;
// Reset checkbox if prompt was canceled }
item.checked = !item.checked;
}
})
.catch(console.error);
} }
function setupPromptOptions(options) { function setupPromptOptions(options) {
// TODO Custom titlebar needs testing on macOS // TODO Custom titlebar needs testing on macOS
if (is.macOS()) { if (is.macOS()) {
Object.assign(options, { return {
...options,
customStylesheet: "dark", customStylesheet: "dark",
icon: iconPath icon: iconPath
}); };
} else { } else {
Object.assign(options, { return {
...options,
customStylesheet: "dark", customStylesheet: "dark",
icon: iconPath, icon: iconPath,
// The following are used for custom titlebar // The following are used for custom titlebar
frame: false, frame: false,
customScript: customTitlebarPath, customScript: customTitlebarPath,
enableRemoteModule: true enableRemoteModule: true
}); };
} }
} }

View File

@ -29,8 +29,8 @@ const iconPath = path.join(process.cwd(), "assets", "youtube-music-tray.png");
// Helper function for keybind prompt // Helper function for keybind prompt
const kb = (label_, value_, default_) => { return { value: value_, label: label_, default: default_ }; }; const kb = (label_, value_, default_) => { return { value: value_, label: label_, default: default_ }; };
function promptKeybind(options, win) { async function promptKeybind(options, win) {
const 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:",
@ -45,24 +45,21 @@ function promptKeybind(options, win) {
}; };
if (!is.macOS()) { if (!is.macOS()) {
Object.assign(promptOptions, { promptOptions = {
...promptOptions,
frame: false, frame: false,
customScript: path.join(process.cwd(), "plugins", "in-app-menu", "prompt-custom-titlebar.js"), customScript: path.join(process.cwd(), "plugins", "in-app-menu", "prompt-custom-titlebar.js"),
enableRemoteModule: true, enableRemoteModule: true,
height: 270 height: 270
}); };
} }
prompt(promptOptions, win) const output = await prompt(promptOptions, win);
.then(output => { if (output) {
if (output) { for (const keybindObject of output) {
for (const keybindObject of output) { options.global[keybindObject.value] = keybindObject.accelerator;
options.global[keybindObject.value] = keybindObject.accelerator; }
} setOption(options);
}
setOption(options); // else -> pressed cancel
}
// else -> pressed cancel
})
.catch(console.error);
} }