update refreshMenu() function

This commit is contained in:
Araxeus
2021-05-23 17:57:54 +03:00
parent 541c7f34b7
commit 33855f17dd

409
menu.js
View File

@ -10,7 +10,7 @@ const config = require("./config");
// true only if in-app-menu was loaded on launch // true only if in-app-menu was loaded on launch
const inAppMenuActive = config.plugins.isEnabled("in-app-menu"); const inAppMenuActive = config.plugins.isEnabled("in-app-menu");
const pluginEnabledMenu = (win, plugin, label = "", hasSubmenu = false) => ({ const pluginEnabledMenu = (plugin, label = "", hasSubmenu = false, refreshMenu = undefined) => ({
label: label || plugin, label: label || plugin,
type: "checkbox", type: "checkbox",
checked: config.plugins.isEnabled(plugin), checked: config.plugins.isEnabled(plugin),
@ -21,223 +21,226 @@ const pluginEnabledMenu = (win, plugin, label = "", hasSubmenu = false) => ({
config.plugins.disable(plugin); config.plugins.disable(plugin);
} }
if (hasSubmenu) { if (hasSubmenu) {
this.setApplicationMenu(win); refreshMenu();
if (inAppMenuActive) {
win.webContents.send("updateMenu", true);
}
} }
}, },
}); });
const mainMenuTemplate = (win) => [ const mainMenuTemplate = (win) => {
{ const refreshMenu = () => {
label: "Plugins", this.setApplicationMenu(win);
submenu: [ if (inAppMenuActive) {
...getAllPlugins().map((plugin) => { win.webContents.send("updateMenu", true);
const pluginPath = path.join(__dirname, "plugins", plugin, "menu.js") }
if (existsSync(pluginPath)) { }
if (!config.plugins.isEnabled(plugin)) { return [
return pluginEnabledMenu(win, plugin, "", true); {
label: "Plugins",
submenu: [
...getAllPlugins().map((plugin) => {
const pluginPath = path.join(__dirname, "plugins", plugin, "menu.js")
if (existsSync(pluginPath)) {
if (!config.plugins.isEnabled(plugin)) {
return pluginEnabledMenu(plugin, "", true, refreshMenu);
}
const getPluginMenu = require(pluginPath);
return {
label: plugin,
submenu: [
pluginEnabledMenu(plugin, "Enabled", true, refreshMenu),
...getPluginMenu(win, config.plugins.getOptions(plugin), refreshMenu),
],
};
} }
const getPluginMenu = require(pluginPath);
return {
label: plugin,
submenu: [
pluginEnabledMenu(win, plugin, "Enabled", true),
...getPluginMenu(win, config.plugins.getOptions(plugin), () =>
module.exports.setApplicationMenu(win)
),
],
};
}
return pluginEnabledMenu(win, plugin); return pluginEnabledMenu(plugin);
}), }),
], ],
}, },
{ {
label: "Options", label: "Options",
submenu: [ submenu: [
{ {
label: "Auto-update", label: "Auto-update",
type: "checkbox", type: "checkbox",
checked: config.get("options.autoUpdates"), checked: config.get("options.autoUpdates"),
click: (item) => { click: (item) => {
config.set("options.autoUpdates", item.checked); config.set("options.autoUpdates", item.checked);
},
}, },
}, {
{ label: "Resume last song when app starts",
label: "Resume last song when app starts", type: "checkbox",
type: "checkbox", checked: config.get("options.resumeOnStart"),
checked: config.get("options.resumeOnStart"), click: (item) => {
click: (item) => { config.set("options.resumeOnStart", item.checked);
config.set("options.resumeOnStart", item.checked); },
}, },
}, ...(is.windows() || is.linux()
...(is.windows() || is.linux() ? [
? [
{
label: "Hide menu",
type: "checkbox",
checked: config.get("options.hideMenu"),
click: (item) => {
config.set("options.hideMenu", item.checked);
},
},
]
: []),
...(is.windows() || is.macOS()
? // Only works on Win/Mac
// https://www.electronjs.org/docs/api/app#appsetloginitemsettingssettings-macos-windows
[
{
label: "Start at login",
type: "checkbox",
checked: config.get("options.startAtLogin"),
click: (item) => {
config.set("options.startAtLogin", item.checked);
},
},
]
: []),
{
label: "Tray",
submenu: [
{
label: "Disabled",
type: "radio",
checked: !config.get("options.tray"),
click: () => {
config.set("options.tray", false);
config.set("options.appVisible", true);
},
},
{
label: "Enabled + app visible",
type: "radio",
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:
config.get("options.tray") && !config.get("options.appVisible"),
click: () => {
config.set("options.tray", true);
config.set("options.appVisible", false);
},
},
{ type: "separator" },
{
label: "Play/Pause on click",
type: "checkbox",
checked: config.get("options.trayClickPlayPause"),
click: (item) => {
config.set("options.trayClickPlayPause", item.checked);
},
},
],
},
{ type: "separator" },
{
label: "Advanced options",
submenu: [
{
label: "Disable hardware acceleration",
type: "checkbox",
checked: config.get("options.disableHardwareAcceleration"),
click: (item) => {
config.set("options.disableHardwareAcceleration", item.checked);
},
},
{
label: "Restart on config changes",
type: "checkbox",
checked: config.get("options.restartOnConfigChanges"),
click: (item) => {
config.set("options.restartOnConfigChanges", item.checked);
},
},
{
label: "Reset App cache when app starts",
type: "checkbox",
checked: config.get("options.autoResetAppCache"),
click: (item) => {
config.set("options.autoResetAppCache", item.checked);
},
},
{ type: "separator" },
is.macOS() ?
{ {
label: "Toggle DevTools", label: "Hide menu",
// Cannot use "toggleDevTools" role in MacOS type: "checkbox",
click: () => { checked: config.get("options.hideMenu"),
const { webContents } = win; click: (item) => {
if (webContents.isDevToolsOpened()) { config.set("options.hideMenu", item.checked);
webContents.closeDevTools();
} else {
const devToolsOptions = {};
webContents.openDevTools(devToolsOptions);
}
}, },
} :
{ role: "toggleDevTools" },
{
label: "Edit config.json",
click: () => {
config.edit();
}, },
]
: []),
...(is.windows() || is.macOS()
? // Only works on Win/Mac
// https://www.electronjs.org/docs/api/app#appsetloginitemsettingssettings-macos-windows
[
{
label: "Start at login",
type: "checkbox",
checked: config.get("options.startAtLogin"),
click: (item) => {
config.set("options.startAtLogin", item.checked);
},
},
]
: []),
{
label: "Tray",
submenu: [
{
label: "Disabled",
type: "radio",
checked: !config.get("options.tray"),
click: () => {
config.set("options.tray", false);
config.set("options.appVisible", true);
},
},
{
label: "Enabled + app visible",
type: "radio",
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:
config.get("options.tray") && !config.get("options.appVisible"),
click: () => {
config.set("options.tray", true);
config.set("options.appVisible", false);
},
},
{ type: "separator" },
{
label: "Play/Pause on click",
type: "checkbox",
checked: config.get("options.trayClickPlayPause"),
click: (item) => {
config.set("options.trayClickPlayPause", item.checked);
},
},
],
},
{ type: "separator" },
{
label: "Advanced options",
submenu: [
{
label: "Disable hardware acceleration",
type: "checkbox",
checked: config.get("options.disableHardwareAcceleration"),
click: (item) => {
config.set("options.disableHardwareAcceleration", item.checked);
},
},
{
label: "Restart on config changes",
type: "checkbox",
checked: config.get("options.restartOnConfigChanges"),
click: (item) => {
config.set("options.restartOnConfigChanges", item.checked);
},
},
{
label: "Reset App cache when app starts",
type: "checkbox",
checked: config.get("options.autoResetAppCache"),
click: (item) => {
config.set("options.autoResetAppCache", item.checked);
},
},
{ type: "separator" },
is.macOS() ?
{
label: "Toggle DevTools",
// Cannot use "toggleDevTools" role in MacOS
click: () => {
const { webContents } = win;
if (webContents.isDevToolsOpened()) {
webContents.closeDevTools();
} else {
const devToolsOptions = {};
webContents.openDevTools(devToolsOptions);
}
},
} :
{ role: "toggleDevTools" },
{
label: "Edit config.json",
click: () => {
config.edit();
},
},
]
},
],
},
{
label: "View",
submenu: [
{ role: "reload" },
{ role: "forceReload" },
{ type: "separator" },
{ role: "zoomIn" },
{ role: "zoomOut" },
{ role: "resetZoom" },
],
},
{
label: "Navigation",
submenu: [
{
label: "Go back",
click: () => {
if (win.webContents.canGoBack()) {
win.webContents.goBack();
}
}, },
]
},
],
},
{
label: "View",
submenu: [
{ role: "reload" },
{ role: "forceReload" },
{ type: "separator" },
{ role: "zoomIn" },
{ role: "zoomOut" },
{ role: "resetZoom" },
],
},
{
label: "Navigation",
submenu: [
{
label: "Go back",
click: () => {
if (win.webContents.canGoBack()) {
win.webContents.goBack();
}
}, },
}, {
{ label: "Go forward",
label: "Go forward", click: () => {
click: () => { if (win.webContents.canGoForward()) {
if (win.webContents.canGoForward()) { win.webContents.goForward();
win.webContents.goForward(); }
} },
}, },
}, {
{ label: "Restart App",
label: "Restart App", click: () => {
click: () => { app.relaunch();
app.relaunch(); app.quit();
app.quit(); },
}, },
}, { role: "quit" },
{ role: "quit" }, ],
], },
}, ];
]; }
module.exports.mainMenuTemplate = mainMenuTemplate; module.exports.mainMenuTemplate = mainMenuTemplate;
module.exports.setApplicationMenu = (win) => { module.exports.setApplicationMenu = (win) => {