Skip to content

Commit 8ab2da0

Browse files
committed
Refactor config for simpler use and advanced options in plugins
1 parent 1b54b19 commit 8ab2da0

File tree

11 files changed

+172
-139
lines changed

11 files changed

+172
-139
lines changed

config/defaults.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const defaultConfig = {
2+
"window-size": {
3+
width: 1100,
4+
height: 550,
5+
},
6+
url: "https://music.youtube.com",
7+
options: {
8+
tray: false,
9+
appVisible: true,
10+
autoUpdates: true,
11+
hideMenu: false,
12+
startAtLogin: false,
13+
disableHardwareAcceleration: false,
14+
},
15+
plugins: {
16+
navigation: {
17+
enabled: true,
18+
},
19+
shortcuts: {
20+
enabled: true,
21+
},
22+
adblocker: {
23+
enabled: true,
24+
},
25+
},
26+
};
27+
28+
module.exports = defaultConfig;

config/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const plugins = require("./plugins");
2+
const store = require("./store");
3+
4+
const set = (key, value) => {
5+
store.set(key, value);
6+
};
7+
8+
const get = (key) => {
9+
return store.get(key);
10+
};
11+
12+
module.exports = {
13+
get,
14+
set,
15+
plugins,
16+
};

config/plugins.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const store = require("./store");
2+
3+
function getEnabled() {
4+
const plugins = store.get("plugins");
5+
const enabledPlugins = Object.entries(plugins).filter(([plugin, options]) =>
6+
isEnabled(plugin)
7+
);
8+
return enabledPlugins;
9+
}
10+
11+
function isEnabled(plugin) {
12+
const pluginConfig = store.get("plugins")[plugin];
13+
return pluginConfig !== undefined && pluginConfig.enabled;
14+
}
15+
16+
function setOptions(plugin, options) {
17+
const plugins = store.get("plugins");
18+
store.set("plugins", {
19+
...plugins,
20+
[plugin]: {
21+
...plugins[plugin],
22+
...options,
23+
},
24+
});
25+
}
26+
27+
function enable(plugin) {
28+
setOptions(plugin, { enabled: true });
29+
}
30+
31+
function disable(plugin) {
32+
setOptions(plugin, { enabled: false });
33+
}
34+
35+
module.exports = {
36+
isEnabled,
37+
getEnabled,
38+
enable,
39+
disable,
40+
setOptions,
41+
};

config/store.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { dialog } = require("electron");
2+
const Store = require("electron-store");
3+
4+
const defaults = require("./defaults");
5+
6+
module.exports = new Store({
7+
defaults,
8+
clearInvalidConfig: false,
9+
migrations: {
10+
">=1.7.0": (store) => {
11+
const enabledPlugins = store.get("plugins");
12+
if (!Array.isArray(enabledPlugins)) {
13+
console.warn("Plugins are not in array format, cannot migrate");
14+
return;
15+
}
16+
17+
const plugins = {};
18+
enabledPlugins.forEach((enabledPlugin) => {
19+
plugins[enabledPlugin] = {
20+
enabled: true,
21+
};
22+
});
23+
store.set("plugins", plugins);
24+
},
25+
},
26+
});

index.js

+21-28
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,8 @@ const electron = require("electron");
55
const is = require("electron-is");
66
const { autoUpdater } = require("electron-updater");
77

8+
const config = require("./config");
89
const { setApplicationMenu } = require("./menu");
9-
const {
10-
autoUpdate,
11-
disableHardwareAcceleration,
12-
getEnabledPlugins,
13-
hideMenu,
14-
isAppVisible,
15-
isTrayEnabled,
16-
setOptions,
17-
store,
18-
startAtLogin,
19-
} = require("./store");
2010
const { fileExists, injectCSS } = require("./plugins/utils");
2111
const { isTesting } = require("./utils/testing");
2212
const { setUpTray } = require("./tray");
@@ -28,7 +18,7 @@ app.commandLine.appendSwitch(
2818
"--experimental-wasm-threads --experimental-wasm-bulk-memory"
2919
);
3020
app.allowRendererProcessReuse = true; // https://github.com/electron/electron/issues/18397
31-
if (disableHardwareAcceleration()) {
21+
if (config.get("options.disableHardwareAcceleration")) {
3222
if (is.dev()) {
3323
console.log("Disabling hardware acceleration");
3424
}
@@ -64,19 +54,19 @@ function loadPlugins(win) {
6454
}
6555
});
6656

67-
getEnabledPlugins().forEach((plugin) => {
57+
config.plugins.getEnabled().forEach(([plugin, options]) => {
6858
console.log("Loaded plugin - " + plugin);
6959
const pluginPath = path.join(__dirname, "plugins", plugin, "back.js");
7060
fileExists(pluginPath, () => {
7161
const handle = require(pluginPath);
72-
handle(win);
62+
handle(win, options);
7363
});
7464
});
7565
}
7666

7767
function createMainWindow() {
78-
const windowSize = store.get("window-size");
79-
const windowMaximized = store.get("window-maximized");
68+
const windowSize = config.get("window-size");
69+
const windowMaximized = config.get("window-maximized");
8070

8171
const win = new electron.BrowserWindow({
8272
icon: icon,
@@ -94,31 +84,34 @@ function createMainWindow() {
9484
},
9585
frame: !is.macOS(),
9686
titleBarStyle: is.macOS() ? "hiddenInset" : "default",
97-
autoHideMenuBar: hideMenu(),
87+
autoHideMenuBar: config.get("options.hideMenu"),
9888
});
9989
if (windowMaximized) {
10090
win.maximize();
10191
}
10292

103-
win.webContents.loadURL(store.get("url"));
93+
win.webContents.loadURL(config.get("url"));
10494
win.on("closed", onClosed);
10595

10696
win.on("move", () => {
10797
let position = win.getPosition();
108-
store.set("window-position", { x: position[0], y: position[1] });
98+
config.set("window-position", { x: position[0], y: position[1] });
10999
});
110100

111101
win.on("resize", () => {
112102
const windowSize = win.getSize();
113103

114-
store.set("window-maximized", win.isMaximized());
104+
config.set("window-maximized", win.isMaximized());
115105
if (!win.isMaximized()) {
116-
store.set("window-size", { width: windowSize[0], height: windowSize[1] });
106+
config.set("window-size", {
107+
width: windowSize[0],
108+
height: windowSize[1],
109+
});
117110
}
118111
});
119112

120113
win.once("ready-to-show", () => {
121-
if (isAppVisible()) {
114+
if (config.get("options.appVisible")) {
122115
win.show();
123116
}
124117
});
@@ -143,7 +136,7 @@ app.on("browser-window-created", (event, win) => {
143136
win.webContents.on("did-navigate-in-page", () => {
144137
const url = win.webContents.getURL();
145138
if (url.startsWith("https://music.youtube.com")) {
146-
store.set("url", url);
139+
config.set("url", url);
147140
}
148141
});
149142

@@ -200,10 +193,10 @@ app.on("ready", () => {
200193

201194
// Autostart at login
202195
app.setLoginItemSettings({
203-
openAtLogin: startAtLogin(),
196+
openAtLogin: config.get("options.startAtLogin"),
204197
});
205198

206-
if (!is.dev() && autoUpdate()) {
199+
if (!is.dev() && config.get("options.autoUpdates")) {
207200
autoUpdater.checkForUpdatesAndNotify();
208201
autoUpdater.on("update-available", () => {
209202
const downloadLink =
@@ -223,7 +216,7 @@ app.on("ready", () => {
223216
break;
224217
// Disable updates
225218
case 2:
226-
setOptions({ autoUpdates: false });
219+
config.set("options.autoUpdates", false);
227220
break;
228221
default:
229222
break;
@@ -234,7 +227,7 @@ app.on("ready", () => {
234227

235228
// Optimized for Mac OS X
236229
if (is.macOS()) {
237-
if (!isAppVisible()) {
230+
if (!config.get("options.appVisible")) {
238231
app.dock.hide();
239232
}
240233
}
@@ -244,7 +237,7 @@ app.on("ready", () => {
244237
forceQuit = true;
245238
});
246239

247-
if (is.macOS() || isTrayEnabled()) {
240+
if (is.macOS() || config.get("options.tray")) {
248241
mainWindow.on("close", (event) => {
249242
// Hide the window instead of quitting (quit is available in tray options)
250243
if (!forceQuit) {

menu.js

+29-29
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,7 @@ const { app, Menu } = require("electron");
22
const is = require("electron-is");
33

44
const { getAllPlugins } = require("./plugins/utils");
5-
const {
6-
isPluginEnabled,
7-
enablePlugin,
8-
disablePlugin,
9-
autoUpdate,
10-
hideMenu,
11-
isAppVisible,
12-
isTrayEnabled,
13-
setOptions,
14-
startAtLogin,
15-
disableHardwareAcceleration,
16-
} = require("./store");
5+
const config = require("./config");
176

187
const mainMenuTemplate = (win) => [
198
{
@@ -22,12 +11,12 @@ const mainMenuTemplate = (win) => [
2211
return {
2312
label: plugin,
2413
type: "checkbox",
25-
checked: isPluginEnabled(plugin),
14+
checked: config.plugins.isEnabled(plugin),
2615
click: (item) => {
2716
if (item.checked) {
28-
enablePlugin(plugin);
17+
config.plugins.enable(plugin);
2918
} else {
30-
disablePlugin(plugin);
19+
config.plugins.disable(plugin);
3120
}
3221
},
3322
};
@@ -39,27 +28,27 @@ const mainMenuTemplate = (win) => [
3928
{
4029
label: "Auto-update",
4130
type: "checkbox",
42-
checked: autoUpdate(),
31+
checked: config.get("options.autoUpdates"),
4332
click: (item) => {
44-
setOptions({ autoUpdates: item.checked });
33+
config.set("options.autoUpdates", item.checked);
4534
},
4635
},
4736
{
4837
label: "Disable hardware acceleration",
4938
type: "checkbox",
50-
checked: disableHardwareAcceleration(),
39+
checked: config.get("options.disableHardwareAcceleration"),
5140
click: (item) => {
52-
setOptions({ disableHardwareAcceleration: item.checked });
41+
config.set("options.disableHardwareAcceleration", item.checked);
5342
},
5443
},
5544
...(is.windows() || is.linux()
5645
? [
5746
{
5847
label: "Hide menu",
5948
type: "checkbox",
60-
checked: hideMenu(),
49+
checked: config.get("options.hideMenu"),
6150
click: (item) => {
62-
setOptions({ hideMenu: item.checked });
51+
config.set("options.hideMenu", item.checked);
6352
},
6453
},
6554
]
@@ -71,9 +60,9 @@ const mainMenuTemplate = (win) => [
7160
{
7261
label: "Start at login",
7362
type: "checkbox",
74-
checked: startAtLogin(),
63+
checked: config.get("options.startAtLogin"),
7564
click: (item) => {
76-
setOptions({ startAtLogin: item.checked });
65+
config.set("options.startAtLogin", item.checked);
7766
},
7867
},
7968
]
@@ -84,20 +73,31 @@ const mainMenuTemplate = (win) => [
8473
{
8574
label: "Disabled",
8675
type: "radio",
87-
checked: !isTrayEnabled(),
88-
click: () => setOptions({ tray: false, appVisible: true }),
76+
checked: !config.get("options.tray"),
77+
click: () => {
78+
config.set("options.tray", false);
79+
config.set("options.appVisible", true);
80+
},
8981
},
9082
{
9183
label: "Enabled + app visible",
9284
type: "radio",
93-
checked: isTrayEnabled() && isAppVisible(),
94-
click: () => setOptions({ tray: true, appVisible: true }),
85+
checked:
86+
config.get("options.tray") && config.get("options.appVisible"),
87+
click: () => {
88+
config.set("options.tray", true);
89+
config.set("options.appVisible", true);
90+
},
9591
},
9692
{
9793
label: "Enabled + app hidden",
9894
type: "radio",
99-
checked: isTrayEnabled() && !isAppVisible(),
100-
click: () => setOptions({ tray: true, appVisible: false }),
95+
checked:
96+
config.get("options.tray") && !config.get("options.appVisible"),
97+
click: () => {
98+
config.set("options.tray", true);
99+
config.set("options.appVisible", false);
100+
},
101101
},
102102
],
103103
},

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "youtube-music",
33
"productName": "YouTube Music",
4-
"version": "1.6.5",
4+
"version": "1.7.0",
55
"description": "YouTube Music Desktop App - including custom plugins",
66
"license": "MIT",
77
"repository": "th-ch/youtube-music",

0 commit comments

Comments
 (0)