Skip to content

Commit

Permalink
Merge pull request #79 from th-ch/advanced-config
Browse files Browse the repository at this point in the history
Refactor config, custom plugin options
  • Loading branch information
th-ch authored Dec 3, 2020
2 parents 1b54b19 + 4dcbd2e commit 6ab0105
Show file tree
Hide file tree
Showing 15 changed files with 282 additions and 165 deletions.
37 changes: 37 additions & 0 deletions config/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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: {
// Enabled plugins
navigation: {
enabled: true,
},
shortcuts: {
enabled: true,
},
adblocker: {
enabled: true,
cache: true,
additionalBlockLists: [], // Additional list of filters, e.g "https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/filters.txt"
},
// Disabled plugins
downloader: {
enabled: false,
ffmpegArgs: [], // e.g. ["-b:a", "192k"] for an audio bitrate of 192kb/s
downloadFolder: undefined, // Custom download folder (absolute path)
},
},
};

module.exports = defaultConfig;
18 changes: 18 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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,
edit: () => store.openInEditor(),
watch: (cb) => store.onDidAnyChange(cb),
plugins,
};
41 changes: 41 additions & 0 deletions config/plugins.js
Original file line number Diff line number Diff line change
@@ -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,
};
40 changes: 40 additions & 0 deletions config/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const Store = require("electron-store");

const defaults = require("./defaults");

const 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;
}

// Include custom options
const plugins = {
adblocker: {
enabled: true,
cache: true,
additionalBlockLists: [],
},
downloader: {
enabled: false,
ffmpegArgs: [], // e.g. ["-b:a", "192k"] for an audio bitrate of 192kb/s
downloadFolder: undefined, // Custom download folder (absolute path)
},
};
enabledPlugins.forEach((enabledPlugin) => {
plugins[enabledPlugin] = {
...plugins[enabledPlugin],
enabled: true,
};
});
store.set("plugins", plugins);
},
};

module.exports = new Store({
defaults,
clearInvalidConfig: false,
migrations,
});
52 changes: 24 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
}
Expand Down Expand Up @@ -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,
Expand All @@ -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();
}
});
Expand All @@ -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);
}
});

Expand Down Expand Up @@ -196,14 +189,17 @@ app.on("activate", () => {
app.on("ready", () => {
mainWindow = createMainWindow();
setApplicationMenu(mainWindow);
config.watch(() => {
setApplicationMenu(mainWindow);
});
setUpTray(app, mainWindow);

// 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 =
Expand All @@ -223,7 +219,7 @@ app.on("ready", () => {
break;
// Disable updates
case 2:
setOptions({ autoUpdates: false });
config.set("options.autoUpdates", false);
break;
default:
break;
Expand All @@ -234,7 +230,7 @@ app.on("ready", () => {

// Optimized for Mac OS X
if (is.macOS()) {
if (!isAppVisible()) {
if (!config.get("options.appVisible")) {
app.dock.hide();
}
}
Expand All @@ -244,7 +240,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) {
Expand Down
Loading

0 comments on commit 6ab0105

Please sign in to comment.