From 8ab2da0482b6211b6b6d43423ec06daed48dac4f Mon Sep 17 00:00:00 2001 From: TC Date: Thu, 3 Dec 2020 18:16:37 +0100 Subject: [PATCH] Refactor config for simpler use and advanced options in plugins --- config/defaults.js | 28 ++++++++++++++++++++++ config/index.js | 16 +++++++++++++ config/plugins.js | 41 ++++++++++++++++++++++++++++++++ config/store.js | 26 +++++++++++++++++++++ index.js | 49 +++++++++++++++++---------------------- menu.js | 58 +++++++++++++++++++++++----------------------- package.json | 2 +- preload.js | 16 ++++++------- store/index.js | 40 -------------------------------- store/plugins.js | 31 ------------------------- tray.js | 4 ++-- 11 files changed, 172 insertions(+), 139 deletions(-) create mode 100644 config/defaults.js create mode 100644 config/index.js create mode 100644 config/plugins.js create mode 100644 config/store.js delete mode 100644 store/index.js delete mode 100644 store/plugins.js diff --git a/config/defaults.js b/config/defaults.js new file mode 100644 index 0000000000..48745e7f12 --- /dev/null +++ b/config/defaults.js @@ -0,0 +1,28 @@ +const defaultConfig = { + "window-size": { + width: 1100, + height: 550, + }, + url: "https://music.youtube.com", + options: { + tray: false, + appVisible: true, + autoUpdates: true, + hideMenu: false, + startAtLogin: false, + disableHardwareAcceleration: false, + }, + plugins: { + navigation: { + enabled: true, + }, + shortcuts: { + enabled: true, + }, + adblocker: { + enabled: true, + }, + }, +}; + +module.exports = defaultConfig; diff --git a/config/index.js b/config/index.js new file mode 100644 index 0000000000..8791a1e598 --- /dev/null +++ b/config/index.js @@ -0,0 +1,16 @@ +const plugins = require("./plugins"); +const store = require("./store"); + +const set = (key, value) => { + store.set(key, value); +}; + +const get = (key) => { + return store.get(key); +}; + +module.exports = { + get, + set, + plugins, +}; diff --git a/config/plugins.js b/config/plugins.js new file mode 100644 index 0000000000..03962c3d07 --- /dev/null +++ b/config/plugins.js @@ -0,0 +1,41 @@ +const store = require("./store"); + +function getEnabled() { + const plugins = store.get("plugins"); + const enabledPlugins = Object.entries(plugins).filter(([plugin, options]) => + isEnabled(plugin) + ); + return enabledPlugins; +} + +function isEnabled(plugin) { + const pluginConfig = store.get("plugins")[plugin]; + return pluginConfig !== undefined && pluginConfig.enabled; +} + +function setOptions(plugin, options) { + const plugins = store.get("plugins"); + store.set("plugins", { + ...plugins, + [plugin]: { + ...plugins[plugin], + ...options, + }, + }); +} + +function enable(plugin) { + setOptions(plugin, { enabled: true }); +} + +function disable(plugin) { + setOptions(plugin, { enabled: false }); +} + +module.exports = { + isEnabled, + getEnabled, + enable, + disable, + setOptions, +}; diff --git a/config/store.js b/config/store.js new file mode 100644 index 0000000000..7536d77306 --- /dev/null +++ b/config/store.js @@ -0,0 +1,26 @@ +const { dialog } = require("electron"); +const Store = require("electron-store"); + +const defaults = require("./defaults"); + +module.exports = new Store({ + defaults, + clearInvalidConfig: false, + migrations: { + ">=1.7.0": (store) => { + const enabledPlugins = store.get("plugins"); + if (!Array.isArray(enabledPlugins)) { + console.warn("Plugins are not in array format, cannot migrate"); + return; + } + + const plugins = {}; + enabledPlugins.forEach((enabledPlugin) => { + plugins[enabledPlugin] = { + enabled: true, + }; + }); + store.set("plugins", plugins); + }, + }, +}); diff --git a/index.js b/index.js index de4218d37b..5a47eef0e6 100644 --- a/index.js +++ b/index.js @@ -5,18 +5,8 @@ const electron = require("electron"); const is = require("electron-is"); const { autoUpdater } = require("electron-updater"); +const config = require("./config"); const { setApplicationMenu } = require("./menu"); -const { - autoUpdate, - disableHardwareAcceleration, - getEnabledPlugins, - hideMenu, - isAppVisible, - isTrayEnabled, - setOptions, - store, - startAtLogin, -} = require("./store"); const { fileExists, injectCSS } = require("./plugins/utils"); const { isTesting } = require("./utils/testing"); const { setUpTray } = require("./tray"); @@ -28,7 +18,7 @@ app.commandLine.appendSwitch( "--experimental-wasm-threads --experimental-wasm-bulk-memory" ); app.allowRendererProcessReuse = true; // https://github.com/electron/electron/issues/18397 -if (disableHardwareAcceleration()) { +if (config.get("options.disableHardwareAcceleration")) { if (is.dev()) { console.log("Disabling hardware acceleration"); } @@ -64,19 +54,19 @@ function loadPlugins(win) { } }); - getEnabledPlugins().forEach((plugin) => { + config.plugins.getEnabled().forEach(([plugin, options]) => { console.log("Loaded plugin - " + plugin); const pluginPath = path.join(__dirname, "plugins", plugin, "back.js"); fileExists(pluginPath, () => { const handle = require(pluginPath); - handle(win); + handle(win, options); }); }); } function createMainWindow() { - const windowSize = store.get("window-size"); - const windowMaximized = store.get("window-maximized"); + const windowSize = config.get("window-size"); + const windowMaximized = config.get("window-maximized"); const win = new electron.BrowserWindow({ icon: icon, @@ -94,31 +84,34 @@ function createMainWindow() { }, frame: !is.macOS(), titleBarStyle: is.macOS() ? "hiddenInset" : "default", - autoHideMenuBar: hideMenu(), + autoHideMenuBar: config.get("options.hideMenu"), }); if (windowMaximized) { win.maximize(); } - win.webContents.loadURL(store.get("url")); + win.webContents.loadURL(config.get("url")); win.on("closed", onClosed); win.on("move", () => { let position = win.getPosition(); - store.set("window-position", { x: position[0], y: position[1] }); + config.set("window-position", { x: position[0], y: position[1] }); }); win.on("resize", () => { const windowSize = win.getSize(); - store.set("window-maximized", win.isMaximized()); + config.set("window-maximized", win.isMaximized()); if (!win.isMaximized()) { - store.set("window-size", { width: windowSize[0], height: windowSize[1] }); + config.set("window-size", { + width: windowSize[0], + height: windowSize[1], + }); } }); win.once("ready-to-show", () => { - if (isAppVisible()) { + if (config.get("options.appVisible")) { win.show(); } }); @@ -143,7 +136,7 @@ app.on("browser-window-created", (event, win) => { win.webContents.on("did-navigate-in-page", () => { const url = win.webContents.getURL(); if (url.startsWith("https://music.youtube.com")) { - store.set("url", url); + config.set("url", url); } }); @@ -200,10 +193,10 @@ app.on("ready", () => { // Autostart at login app.setLoginItemSettings({ - openAtLogin: startAtLogin(), + openAtLogin: config.get("options.startAtLogin"), }); - if (!is.dev() && autoUpdate()) { + if (!is.dev() && config.get("options.autoUpdates")) { autoUpdater.checkForUpdatesAndNotify(); autoUpdater.on("update-available", () => { const downloadLink = @@ -223,7 +216,7 @@ app.on("ready", () => { break; // Disable updates case 2: - setOptions({ autoUpdates: false }); + config.set("options.autoUpdates", false); break; default: break; @@ -234,7 +227,7 @@ app.on("ready", () => { // Optimized for Mac OS X if (is.macOS()) { - if (!isAppVisible()) { + if (!config.get("options.appVisible")) { app.dock.hide(); } } @@ -244,7 +237,7 @@ app.on("ready", () => { forceQuit = true; }); - if (is.macOS() || isTrayEnabled()) { + if (is.macOS() || config.get("options.tray")) { mainWindow.on("close", (event) => { // Hide the window instead of quitting (quit is available in tray options) if (!forceQuit) { diff --git a/menu.js b/menu.js index 0932238c4e..81ef175f78 100644 --- a/menu.js +++ b/menu.js @@ -2,18 +2,7 @@ const { app, Menu } = require("electron"); const is = require("electron-is"); const { getAllPlugins } = require("./plugins/utils"); -const { - isPluginEnabled, - enablePlugin, - disablePlugin, - autoUpdate, - hideMenu, - isAppVisible, - isTrayEnabled, - setOptions, - startAtLogin, - disableHardwareAcceleration, -} = require("./store"); +const config = require("./config"); const mainMenuTemplate = (win) => [ { @@ -22,12 +11,12 @@ const mainMenuTemplate = (win) => [ return { label: plugin, type: "checkbox", - checked: isPluginEnabled(plugin), + checked: config.plugins.isEnabled(plugin), click: (item) => { if (item.checked) { - enablePlugin(plugin); + config.plugins.enable(plugin); } else { - disablePlugin(plugin); + config.plugins.disable(plugin); } }, }; @@ -39,17 +28,17 @@ const mainMenuTemplate = (win) => [ { label: "Auto-update", type: "checkbox", - checked: autoUpdate(), + checked: config.get("options.autoUpdates"), click: (item) => { - setOptions({ autoUpdates: item.checked }); + config.set("options.autoUpdates", item.checked); }, }, { label: "Disable hardware acceleration", type: "checkbox", - checked: disableHardwareAcceleration(), + checked: config.get("options.disableHardwareAcceleration"), click: (item) => { - setOptions({ disableHardwareAcceleration: item.checked }); + config.set("options.disableHardwareAcceleration", item.checked); }, }, ...(is.windows() || is.linux() @@ -57,9 +46,9 @@ const mainMenuTemplate = (win) => [ { label: "Hide menu", type: "checkbox", - checked: hideMenu(), + checked: config.get("options.hideMenu"), click: (item) => { - setOptions({ hideMenu: item.checked }); + config.set("options.hideMenu", item.checked); }, }, ] @@ -71,9 +60,9 @@ const mainMenuTemplate = (win) => [ { label: "Start at login", type: "checkbox", - checked: startAtLogin(), + checked: config.get("options.startAtLogin"), click: (item) => { - setOptions({ startAtLogin: item.checked }); + config.set("options.startAtLogin", item.checked); }, }, ] @@ -84,20 +73,31 @@ const mainMenuTemplate = (win) => [ { label: "Disabled", type: "radio", - checked: !isTrayEnabled(), - click: () => setOptions({ tray: false, appVisible: true }), + checked: !config.get("options.tray"), + click: () => { + config.set("options.tray", false); + config.set("options.appVisible", true); + }, }, { label: "Enabled + app visible", type: "radio", - checked: isTrayEnabled() && isAppVisible(), - click: () => setOptions({ tray: true, appVisible: true }), + checked: + config.get("options.tray") && config.get("options.appVisible"), + click: () => { + config.set("options.tray", true); + config.set("options.appVisible", true); + }, }, { label: "Enabled + app hidden", type: "radio", - checked: isTrayEnabled() && !isAppVisible(), - click: () => setOptions({ tray: true, appVisible: false }), + checked: + config.get("options.tray") && !config.get("options.appVisible"), + click: () => { + config.set("options.tray", true); + config.set("options.appVisible", false); + }, }, ], }, diff --git a/package.json b/package.json index a1e92f35b2..243d864ad5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "youtube-music", "productName": "YouTube Music", - "version": "1.6.5", + "version": "1.7.0", "description": "YouTube Music Desktop App - including custom plugins", "license": "MIT", "repository": "th-ch/youtube-music", diff --git a/preload.js b/preload.js index add2597977..362c917032 100644 --- a/preload.js +++ b/preload.js @@ -2,31 +2,31 @@ const path = require("path"); const { remote } = require("electron"); -const { getEnabledPlugins, store } = require("./store"); -const { fileExists } = require("./plugins/utils"); +const config = require("./config"); +const { fileExists } = require("./plugins/utils"); -const plugins = getEnabledPlugins(); +const plugins = config.plugins.getEnabled(); -plugins.forEach(plugin => { +plugins.forEach(([plugin, options]) => { const pluginPath = path.join(__dirname, "plugins", plugin, "actions.js"); fileExists(pluginPath, () => { const actions = require(pluginPath).global || {}; - Object.keys(actions).forEach(actionName => { + Object.keys(actions).forEach((actionName) => { global[actionName] = actions[actionName]; }); }); }); document.addEventListener("DOMContentLoaded", () => { - plugins.forEach(plugin => { + plugins.forEach(([plugin, options]) => { const pluginPath = path.join(__dirname, "plugins", plugin, "front.js"); fileExists(pluginPath, () => { const run = require(pluginPath); - run(); + run(options); }); }); // Add action for reloading global.reload = () => - remote.getCurrentWindow().webContents.loadURL(store.get("url")); + remote.getCurrentWindow().webContents.loadURL(config.get("url")); }); diff --git a/store/index.js b/store/index.js deleted file mode 100644 index b49681cb4e..0000000000 --- a/store/index.js +++ /dev/null @@ -1,40 +0,0 @@ -const Store = require("electron-store"); -const plugins = require("./plugins"); - -const store = new Store({ - defaults: { - "window-size": { - width: 1100, - height: 550, - }, - url: "https://music.youtube.com", - plugins: ["navigation", "shortcuts", "adblocker"], - options: { - tray: false, - appVisible: true, - autoUpdates: true, - hideMenu: false, - startAtLogin: false, - disableHardwareAcceleration: false, - }, - }, -}); - -module.exports = { - store: store, - // Plugins - isPluginEnabled: plugin => plugins.isEnabled(store, plugin), - getEnabledPlugins: () => plugins.getEnabledPlugins(store), - enablePlugin: plugin => plugins.enablePlugin(store, plugin), - disablePlugin: plugin => plugins.disablePlugin(store, plugin), - // Options - setOptions: options => - store.set("options", { ...store.get("options"), ...options }), - isTrayEnabled: () => store.get("options.tray"), - isAppVisible: () => store.get("options.appVisible"), - autoUpdate: () => store.get("options.autoUpdates"), - hideMenu: () => store.get("options.hideMenu"), - startAtLogin: () => store.get("options.startAtLogin"), - disableHardwareAcceleration: () => - store.get("options.disableHardwareAcceleration"), -}; diff --git a/store/plugins.js b/store/plugins.js deleted file mode 100644 index 28eef1fc3c..0000000000 --- a/store/plugins.js +++ /dev/null @@ -1,31 +0,0 @@ -function getEnabledPlugins(store) { - return store.get("plugins"); -} - -function isEnabled(store, plugin) { - return store.get("plugins").indexOf(plugin) > -1; -} - -function enablePlugin(store, plugin) { - let plugins = getEnabledPlugins(store); - if (plugins.indexOf(plugin) === -1) { - plugins.push(plugin); - store.set("plugins", plugins); - } -} - -function disablePlugin(store, plugin) { - let plugins = getEnabledPlugins(store); - let index = plugins.indexOf(plugin); - if (index > -1) { - plugins.splice(index, 1); - store.set("plugins", plugins); - } -} - -module.exports = { - isEnabled : isEnabled, - getEnabledPlugins: getEnabledPlugins, - enablePlugin : enablePlugin, - disablePlugin : disablePlugin -}; diff --git a/tray.js b/tray.js index e3ae9bb3c6..ff5f91eb07 100644 --- a/tray.js +++ b/tray.js @@ -2,15 +2,15 @@ const path = require("path"); const { Menu, nativeImage, Tray } = require("electron"); +const config = require("./config"); const { mainMenuTemplate } = require("./menu"); -const { isTrayEnabled } = require("./store"); const { clickInYoutubeMusic } = require("./utils/youtube-music"); // Prevent tray being garbage collected let tray; module.exports.setUpTray = (app, win) => { - if (!isTrayEnabled()) { + if (!config.get("options.tray")) { tray = undefined; return; }