Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Prompt for changing options #243

Merged
merged 32 commits into from
Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e4eed2e
add custom-electron-prompt
Araxeus Apr 27, 2021
a229ba9
disable reload of plugins on window created
Araxeus Apr 27, 2021
964974c
add keybind changer v1
Araxeus Apr 30, 2021
e456035
fix typo
Araxeus Apr 30, 2021
49e51de
update shortcuts config
Araxeus Apr 30, 2021
54cbe3f
lint
Araxeus Apr 30, 2021
d0d4ada
restore original menu lint
Araxeus Apr 30, 2021
ec981ac
refactor registerAllShortcuts
Araxeus Apr 30, 2021
ebaa018
Merge remote-tracking branch 'upstream/master' into custom-electron-p…
Araxeus May 4, 2021
79acf6c
Volume Steps Prompt
Araxeus May 4, 2021
22c5ea5
lint
Araxeus May 4, 2021
34a4e6b
proxy url check
Araxeus May 4, 2021
b97a86f
Update yarn.lock
Araxeus May 4, 2021
db8d946
fix electron dependency
Araxeus May 4, 2021
98c00f7
format proxy example
Araxeus May 4, 2021
5cee331
update prompt version and lint
Araxeus May 4, 2021
834f867
massive prompt speed boost with v1.1.0
Araxeus May 5, 2021
6b147b0
fix prompt width
Araxeus May 5, 2021
5418ef7
Merge branch 'master' into custom-electron-prompt
Araxeus May 7, 2021
f910593
use spread operator + async await
Araxeus May 9, 2021
36317c9
globalize promptOptions
Araxeus May 9, 2021
0eca303
lint
Araxeus May 9, 2021
580caef
destructure keybind output
Araxeus May 10, 2021
e43c01d
lint
Araxeus May 10, 2021
002081b
use store migration
Araxeus May 11, 2021
355f611
use placeholder proxy example
Araxeus May 13, 2021
b2c2098
fix empty string input validation in setProxy
Araxeus May 14, 2021
664be51
Merge branch 'master' into custom-electron-prompt
Araxeus Jun 25, 2021
52a4608
create options.global if needed
Araxeus Jul 20, 2021
a49817f
Merge branch 'master' into custom-electron-prompt
Araxeus Jul 20, 2021
65eaaec
Merge branch 'master' into custom-electron-prompt
Araxeus Aug 19, 2021
1cd4f53
Merge branch 'master' into custom-electron-prompt
Araxeus Oct 19, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const defaultConfig = {
// Disabled plugins
shortcuts: {
enabled: false,
overrideMediaKeys: false,
Araxeus marked this conversation as resolved.
Show resolved Hide resolved
},
downloader: {
enabled: false,
Expand Down Expand Up @@ -59,9 +60,8 @@ const defaultConfig = {
steps: 1, //percentage of volume to change
arrowsShortcut: true, //enable ArrowUp + ArrowDown local shortcuts
globalShortcuts: {
enabled: false, // enable global shortcuts
volumeUp: "Shift+PageUp", // Keybind default can be changed
volumeDown: "Shift+PageDown"
volumeUp: "",
volumeDown: ""
},
savedVolume: undefined //plugin save volume between session here
},
Expand Down
21 changes: 21 additions & 0 deletions config/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ const migrations = {
store.set("plugins.discord.listenAlong", true);
}
},
">=1.12.0": (store) => {
const options = store.get("plugins.shortcuts")
let updated = false;
for (const optionType of ["global", "local"]) {
if (Array.isArray(options[optionType])) {
const updatedOptions = {};
for (const optionObject of options[optionType]) {
if (optionObject.action && optionObject.shortcut) {
updatedOptions[optionObject.action] = optionObject.shortcut;
}
}

options[optionType] = updatedOptions;
updated = true;
}
}

if (updated) {
store.set("plugins.shortcuts", options);
}
},
">=1.11.0": (store) => {
if (store.get("options.resumeOnStart") === undefined) {
store.set("options.resumeOnStart", true);
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ if (config.get("options.proxy")) {
}

// Adds debug features like hotkeys for triggering dev tools and reload
require("electron-debug")();
require("electron-debug")({
showDevTools: false //disable automatic devTools on new window
});

// Prevent window being garbage collected
let mainWindow;
Expand All @@ -60,7 +62,7 @@ function onClosed() {

function loadPlugins(win) {
injectCSS(win.webContents, path.join(__dirname, "youtube-music.css"));
win.webContents.on("did-finish-load", () => {
win.webContents.once("did-finish-load", () => {
if (is.dev()) {
console.log("did finish load");
win.webContents.openDevTools();
Expand Down
33 changes: 33 additions & 0 deletions menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const is = require("electron-is");
const { getAllPlugins } = require("./plugins/utils");
const config = require("./config");

const prompt = require("custom-electron-prompt");
const promptOptions = require("./providers/prompt-options");

// true only if in-app-menu was loaded on launch
const inAppMenuActive = config.plugins.isEnabled("in-app-menu");

Expand Down Expand Up @@ -149,6 +152,14 @@ const mainMenuTemplate = (win) => {
{
label: "Advanced options",
submenu: [
{
label: "Proxy",
type: "checkbox",
checked: !!config.get("options.proxy"),
click: (item) => {
setProxy(item, win);
},
},
{
label: "Disable hardware acceleration",
type: "checkbox",
Expand Down Expand Up @@ -275,3 +286,25 @@ module.exports.setApplicationMenu = (win) => {
const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);
};

async function setProxy(item, win) {
const output = await prompt({
title: 'Set Proxy',
label: 'Enter Proxy Address: (leave empty to disable)',
value: config.get("options.proxy"),
type: 'input',
inputAttrs: {
type: 'url',
placeholder: "Example: 'socks5://127.0.0.1:9999"
},
width: 450,
...promptOptions()
}, win);

if (typeof output === "string") {
config.set("options.proxy", output);
item.checked = output !== "";
} else { //user pressed cancel
item.checked = !item.checked; //reset checkbox
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"YoutubeNonStop": "git://github.com/lawfx/YoutubeNonStop.git#v0.9.0",
"async-mutex": "^0.3.2",
"browser-id3-writer": "^4.4.0",
"custom-electron-prompt": "^1.1.0",
"chokidar": "^3.5.2",
"custom-electron-titlebar": "^3.2.7",
"discord-rpc": "^3.2.0",
Expand Down
4 changes: 1 addition & 3 deletions plugins/precise-volume/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ module.exports = (options) => {

setupLocalArrowShortcuts(options);

if (options.globalShortcuts?.enabled) {
setupGlobalShortcuts(options);
}
setupGlobalShortcuts(options);

firstRun(options);

Expand Down
65 changes: 62 additions & 3 deletions plugins/precise-volume/menu.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,78 @@
const { enabled } = require("./back");
const { setOptions } = require("../../config/plugins");
const prompt = require("custom-electron-prompt");
const promptOptions = require("../../providers/prompt-options");


module.exports = (win, options) => [
{
label: "Arrowkeys controls",
label: "Local Arrowkeys Controls",
type: "checkbox",
checked: !!options.arrowsShortcut,
click: (item) => {
// Dynamically change setting if plugin enabled
click: item => {
// Dynamically change setting if plugin is enabled
if (enabled()) {
win.webContents.send("setArrowsShortcut", item.checked);
} else { // Fallback to usual method if disabled
options.arrowsShortcut = item.checked;
setOptions("precise-volume", options);
}
}
},
{
label: "Global Hotkeys",
type: "checkbox",
checked: !!options.globalShortcuts.volumeUp || !!options.globalShortcuts.volumeDown,
click: item => promptGlobalShortcuts(win, options, item)
},
{
label: "Set Custom Volume Steps",
click: () => promptVolumeSteps(win, options)
}
];

// Helper function for globalShortcuts prompt
const kb = (label_, value_, default_) => { return { value: value_, label: label_, default: default_ || undefined }; };

async function promptVolumeSteps(win, options) {
const output = await prompt({
title: "Volume Steps",
label: "Choose Volume Increase/Decrease Steps",
value: options.steps || 1,
type: "counter",
counterOptions: { minimum: 0, maximum: 100, multiFire: true },
width: 380,
...promptOptions()
}, win)

if (output || output === 0) { // 0 is somewhat valid
options.steps = output;
setOptions("precise-volume", options);
}
}

async function promptGlobalShortcuts(win, options, item) {
const output = await prompt({
title: "Global Volume Keybinds",
label: "Choose Global Volume Keybinds:",
type: "keybind",
keybindOptions: [
kb("Increase Volume", "volumeUp", options.globalShortcuts?.volumeUp),
kb("Decrease Volume", "volumeDown", options.globalShortcuts?.volumeDown)
],
...promptOptions()
}, win)

if (output) {
for (const { value, accelerator } of output) {
options.globalShortcuts[value] = accelerator;
}

setOptions("precise-volume", options);

item.checked = !!options.globalShortcuts.volumeUp || !!options.globalShortcuts.volumeDown;
} else {
// Reset checkbox if prompt was canceled
item.checked = !item.checked;
}
}
48 changes: 29 additions & 19 deletions plugins/shortcuts/back.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ function registerShortcuts(win, options) {
const songControls = getSongControls(win);
const { playPause, next, previous, search } = songControls;

_registerGlobalShortcut(win.webContents, "MediaPlayPause", playPause);
_registerGlobalShortcut(win.webContents, "MediaNextTrack", next);
_registerGlobalShortcut(win.webContents, "MediaPreviousTrack", previous);
if (options.overrideMediaKeys) {
_registerGlobalShortcut(win.webContents, "MediaPlayPause", playPause);
_registerGlobalShortcut(win.webContents, "MediaNextTrack", next);
_registerGlobalShortcut(win.webContents, "MediaPreviousTrack", previous);
}

_registerLocalShortcut(win, "CommandOrControl+F", search);
_registerLocalShortcut(win, "CommandOrControl+L", search);

Expand All @@ -44,24 +47,31 @@ function registerShortcuts(win, 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;
}
const shortcutOptions = { global, local };

_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;
}
for (const optionType in shortcutOptions) {
registerAllShortcuts(shortcutOptions[optionType], optionType);
}

_registerLocalShortcut(win, shortcut, songControls[action]);
});
function registerAllShortcuts(container, type) {
for (const action in container) {
if (!container[action]) {
continue; // Action accelerator is empty
}

console.debug(`Registering ${type} shortcut`, container[action], ":", action);
if (!songControls[action]) {
console.warn("Invalid action", action);
continue;
}

if (type === "global") {
_registerGlobalShortcut(win.webContents, container[action], songControls[action]);
} else { // type === "local"
_registerLocalShortcut(win, local[action], songControls[action]);
}
}
}
}

module.exports = registerShortcuts;
53 changes: 53 additions & 0 deletions plugins/shortcuts/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { setOptions } = require("../../config/plugins");
const prompt = require("custom-electron-prompt");
const promptOptions = require("../../providers/prompt-options");

module.exports = (win, options) => [
{
label: "Set Global Song Controls",
click: () => promptKeybind(options, win)
},
{
label: "Override MediaKeys",
type: "checkbox",
checked: options.overrideMediaKeys,
click: item => setOption(options, "overrideMediaKeys", item.checked)
}
];

function setOption(options, key = null, newValue = null) {
if (key && newValue !== null) {
options[key] = newValue;
}

setOptions("shortcuts", options);
}

// Helper function for keybind prompt
const kb = (label_, value_, default_) => { return { value: value_, label: label_, default: default_ }; };

async function promptKeybind(options, win) {
const output = await prompt({
title: "Global Keybinds",
label: "Choose Global Keybinds for Songs Control:",
type: "keybind",
keybindOptions: [ // If default=undefined then no default is used
kb("Previous", "previous", options.global?.previous),
kb("Play / Pause", "playPause", options.global?.playPause),
kb("Next", "next", options.global?.next)
],
height: 270,
...promptOptions()
}, win);

if (output) {
if (!options.global) {
options.global = {};
}
for (const { value, accelerator } of output) {
options.global[value] = accelerator;
}
setOption(options);
}
// else -> pressed cancel
}
14 changes: 14 additions & 0 deletions providers/prompt-custom-titlebar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const customTitlebar = require("custom-electron-titlebar");

module.exports = () => {
new customTitlebar.Titlebar({
backgroundColor: customTitlebar.Color.fromHex("#050505"),
minimizable: false,
maximizable: false,
menu: null
});
const mainStyle = document.querySelector("#container").style;
mainStyle.width = "100%";
mainStyle.position = "fixed";
mainStyle.border = "unset";
};
18 changes: 18 additions & 0 deletions providers/prompt-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const path = require("path");
const is = require("electron-is");

const iconPath = path.join(__dirname, "..", "assets", "youtube-music-tray.png");
const customTitlebarPath = path.join(__dirname, "prompt-custom-titlebar.js");

const promptOptions = is.macOS() ? {
customStylesheet: "dark",
icon: iconPath
} : {
customStylesheet: "dark",
// The following are used for custom titlebar
frame: false,
customScript: customTitlebarPath,
enableRemoteModule: true
};

module.exports = () => promptOptions;
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2960,6 +2960,11 @@ cssstyle@^2.2.0:
dependencies:
cssom "~0.3.6"

custom-electron-prompt@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/custom-electron-prompt/-/custom-electron-prompt-1.1.0.tgz#611b790047c91f6b532c7861355a0e1f9a81aef2"
integrity sha512-YZYmwZnMOdoWROUlJ+rEMHYsp4XJNNqLj6sZnx5aKBJ8cprEjKP4L5wfo6U+yyX/L9fxVOtvYD0Mp8ki5I9Kow==

custom-electron-titlebar@^3.2.7:
version "3.2.7"
resolved "https://registry.yarnpkg.com/custom-electron-titlebar/-/custom-electron-titlebar-3.2.7.tgz#fb249d6180cbda074b1d392bea755fa0743012a8"
Expand Down