mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-15 12:21:47 +00:00
Plugin doesn't create a new menu anymore
instead it modifies the original menu
This commit is contained in:
@ -1,46 +1,43 @@
|
|||||||
const {injectCSS} = require('../utils');
|
const {injectCSS} = require('../utils');
|
||||||
const {Menu , app} = require('electron');
|
const {Menu} = require('electron');
|
||||||
const { existsSync } = require("fs");
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const electronLocalshortcut = require("electron-localshortcut");
|
const electronLocalshortcut = require("electron-localshortcut");
|
||||||
const is = require('electron-is');
|
|
||||||
const {getAllPlugins} = require('../../plugins/utils');
|
|
||||||
const config = require('../../config');
|
const config = require('../../config');
|
||||||
|
var { mainMenuTemplate } = require("../../menu");
|
||||||
|
|
||||||
const pluginEnabledMenu = (plugin, label = "") => ({
|
//override menu template for custom menu
|
||||||
label: label || plugin,
|
const originTemplate = mainMenuTemplate;
|
||||||
type: "checkbox",
|
mainMenuTemplate = function (winHook) {
|
||||||
checked: config.plugins.isEnabled(plugin),
|
//get template
|
||||||
click: (item) => {
|
let template = originTemplate(winHook);
|
||||||
if (item.checked) {
|
//fix checkbox and roles
|
||||||
config.plugins.enable(plugin);
|
fixCheck(template);
|
||||||
} else {
|
//return as normal
|
||||||
config.plugins.disable(plugin);
|
return template;
|
||||||
}
|
|
||||||
checkCheckbox(item);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
//override Menu.setApplicationMenu to also point to also refresh custom menu
|
|
||||||
const setMenu = Menu.setApplicationMenu;
|
|
||||||
Menu.setApplicationMenu = function (menu) {
|
|
||||||
setMenu.apply(Menu,menu)
|
|
||||||
win.webContents.send('updateMenu', true);
|
|
||||||
}
|
}
|
||||||
|
//win hook for fixing menu
|
||||||
|
let win;
|
||||||
|
|
||||||
module.exports = win => {
|
//check that menu doesn't get created twice
|
||||||
|
let isFixed = false;
|
||||||
|
|
||||||
|
module.exports = winImport => {
|
||||||
|
win = winImport;
|
||||||
// css for custom scrollbar + disable drag area(was causing bugs)
|
// css for custom scrollbar + disable drag area(was causing bugs)
|
||||||
injectCSS(win.webContents, path.join(__dirname, 'style.css'));
|
injectCSS(win.webContents, path.join(__dirname, 'style.css'));
|
||||||
win.on('ready-to-show', () => {
|
win.on('ready-to-show', () => {
|
||||||
//build new menu
|
//build new menu (once)
|
||||||
const template = mainMenuTemplate(win)
|
if(!isFixed) {
|
||||||
const menu = Menu.buildFromTemplate(template);
|
let template = mainMenuTemplate(win);
|
||||||
setMenu(menu);
|
let menu = Menu.buildFromTemplate(template);
|
||||||
|
Menu.setApplicationMenu(menu);
|
||||||
|
isFixed = true;
|
||||||
|
}
|
||||||
|
|
||||||
//register keyboard shortcut && hide menu if hideMenu is enabled
|
//register keyboard shortcut && hide menu if hideMenu is enabled
|
||||||
if (config.get('options.hideMenu')) {
|
if (config.get('options.hideMenu')) {
|
||||||
win.webContents.send('updateMenu', null);
|
win.webContents.send('updateMenu', null);
|
||||||
var enabled= false;
|
let enabled= false;
|
||||||
electronLocalshortcut.register(win, 'Esc', () => {
|
electronLocalshortcut.register(win, 'Esc', () => {
|
||||||
if(enabled) {
|
if(enabled) {
|
||||||
win.webContents.send('updateMenu', null);
|
win.webContents.send('updateMenu', null);
|
||||||
@ -54,247 +51,60 @@ module.exports = win => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function checkCheckbox(item, updateMenu, win) {
|
//go over each item in menu
|
||||||
item.checked = !item.checked;
|
function fixCheck(ogTemplate){
|
||||||
if(updateMenu) {
|
for(let position in ogTemplate) {
|
||||||
win.webContents.send('updateMenu', true);
|
let item = ogTemplate[position];
|
||||||
|
//apply function on submenu
|
||||||
|
if(item.submenu != null) {
|
||||||
|
fixCheck(item.submenu);
|
||||||
|
}
|
||||||
|
//change onClick of checkbox+radio
|
||||||
|
else if(item.type === 'checkbox' || item.type === 'radio') {
|
||||||
|
let ogOnclick = item.click;
|
||||||
|
item.click = (itemClicked) => {
|
||||||
|
ogOnclick(itemClicked);
|
||||||
|
checkCheckbox(itemClicked);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
//customize roles
|
||||||
|
else if (item.role != null) {
|
||||||
|
fixRoles(item)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new template because it works abit different (need to manually change checkbox + tray is out of submenu)
|
function fixRoles(MenuItem) {
|
||||||
const mainMenuTemplate = win => [
|
switch (MenuItem.role) {
|
||||||
{
|
case 'reload':
|
||||||
label: 'Plugins',
|
MenuItem['label'] = 'Reload';
|
||||||
submenu: [
|
MenuItem.click = () => {win.webContents.reload();}
|
||||||
...getAllPlugins().map(plugin => {
|
break;
|
||||||
const pluginPath = path.join(path.dirname(path.dirname(__dirname))
|
case 'forceReload' :
|
||||||
, "plugins", plugin, "menu.js");
|
MenuItem.label = 'Force Reload';
|
||||||
|
MenuItem.click = () => {win.webContents.reloadIgnoringCache();}
|
||||||
if (!config.plugins.isEnabled(plugin)) {
|
break;
|
||||||
return pluginEnabledMenu(plugin);
|
case 'zoomIn':
|
||||||
}
|
MenuItem.label = 'Zoom In';
|
||||||
if (existsSync(pluginPath)) {
|
MenuItem.click = () => {win.webContents.setZoomLevel(win.webContents.getZoomLevel() + 1);}
|
||||||
const getPluginMenu = require(pluginPath);
|
break;
|
||||||
return {
|
case 'zoomOut':
|
||||||
label: plugin,
|
MenuItem.label = 'Zoom Out';
|
||||||
submenu: [
|
MenuItem.click = () => {win.webContents.setZoomLevel(win.webContents.getZoomLevel() - 1);}
|
||||||
pluginEnabledMenu(plugin, "Enabled"),
|
break;
|
||||||
...getPluginMenu(win, config.plugins.getOptions(plugin), () =>
|
case 'resetZoom':
|
||||||
module.exports.setApplicationMenu(win)
|
MenuItem.label = 'Reset Zoom';
|
||||||
),
|
MenuItem.click = () => {win.webContents.setZoomLevel(0);}
|
||||||
],
|
break;
|
||||||
};
|
default :
|
||||||
}
|
console.log(MenuItem.role +' was not expected');
|
||||||
|
|
||||||
return pluginEnabledMenu(plugin);
|
|
||||||
}),
|
|
||||||
{type: 'separator'},
|
|
||||||
{
|
|
||||||
label: 'Advanced options',
|
|
||||||
click: () => {
|
|
||||||
config.edit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Options',
|
|
||||||
submenu: [
|
|
||||||
{
|
|
||||||
label: 'Auto-update',
|
|
||||||
type: 'checkbox',
|
|
||||||
checked: config.get('options.autoUpdates'),
|
|
||||||
click: item => {
|
|
||||||
config.set('options.autoUpdates', item.checked);
|
|
||||||
checkCheckbox(item);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Disable hardware acceleration',
|
|
||||||
type: 'checkbox',
|
|
||||||
checked: config.get('options.disableHardwareAcceleration'),
|
|
||||||
click: item => {
|
|
||||||
config.set('options.disableHardwareAcceleration', item.checked);
|
|
||||||
checkCheckbox(item);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Restart on config changes',
|
|
||||||
type: 'checkbox',
|
|
||||||
checked: config.get('options.restartOnConfigChanges'),
|
|
||||||
click: item => {
|
|
||||||
config.set('options.restartOnConfigChanges', item.checked);
|
|
||||||
checkCheckbox(item);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Reset App cache when app starts',
|
|
||||||
type: 'checkbox',
|
|
||||||
checked: config.get('options.autoResetAppCache'),
|
|
||||||
click: item => {
|
|
||||||
config.set('options.autoResetAppCache', item.checked);
|
|
||||||
checkCheckbox(item);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Resume last song when app starts',
|
|
||||||
type: 'checkbox',
|
|
||||||
checked: config.get('options.resumeOnStart'),
|
|
||||||
click: item => {
|
|
||||||
config.set('options.resumeOnStart', item.checked);
|
|
||||||
checkCheckbox(item);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
...(is.windows() || is.linux() ?
|
|
||||||
[
|
|
||||||
{
|
|
||||||
label: 'Hide menu (show with Escape key)',
|
|
||||||
type: 'checkbox',
|
|
||||||
checked: config.get('options.hideMenu'),
|
|
||||||
click: item => {
|
|
||||||
config.set('options.hideMenu', item.checked);
|
|
||||||
checkCheckbox(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
] :
|
|
||||||
[]),
|
|
||||||
...(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);
|
|
||||||
checkCheckbox(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
] :
|
|
||||||
[]),
|
|
||||||
{
|
|
||||||
label: 'Tray',
|
|
||||||
submenu: [
|
|
||||||
{
|
|
||||||
label: 'Disabled',
|
|
||||||
type: 'radio',
|
|
||||||
checked: !config.get('options.tray'),
|
|
||||||
click: item => {
|
|
||||||
config.set('options.tray', false);
|
|
||||||
config.set('options.appVisible', true);
|
|
||||||
checkCheckbox(item, true, win)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Enabled + app visible',
|
|
||||||
type: 'radio',
|
|
||||||
checked:
|
|
||||||
config.get('options.tray') && config.get('options.appVisible'),
|
|
||||||
click: item => {
|
|
||||||
config.set('options.tray', true);
|
|
||||||
config.set('options.appVisible', true);
|
|
||||||
checkCheckbox(item, true, win)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Enabled + app hidden',
|
|
||||||
type: 'radio',
|
|
||||||
checked:
|
|
||||||
config.get('options.tray') && !config.get('options.appVisible'),
|
|
||||||
click: item => {
|
|
||||||
config.set('options.tray', true);
|
|
||||||
config.set('options.appVisible', false);
|
|
||||||
checkCheckbox(item, true, win)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{type: 'separator'},
|
|
||||||
{
|
|
||||||
label: 'Play/Pause on click',
|
|
||||||
type: 'checkbox',
|
|
||||||
checked: config.get('options.trayClickPlayPause'),
|
|
||||||
click: item => {
|
|
||||||
config.set('options.trayClickPlayPause', item.checked);
|
|
||||||
checkCheckbox(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
|
|
||||||
{type: 'separator'},
|
|
||||||
{
|
|
||||||
label: 'Toggle DevTools',
|
|
||||||
// Cannot use "toggleDevTools" role in MacOS
|
|
||||||
click: () => {
|
|
||||||
const {webContents} = win;
|
|
||||||
if (webContents.isDevToolsOpened()) {
|
|
||||||
webContents.closeDevTools();
|
|
||||||
} else {
|
|
||||||
const devToolsOptions = {};
|
|
||||||
webContents.openDevTools(devToolsOptions);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Advanced options',
|
|
||||||
click: () => {
|
|
||||||
config.edit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'View',
|
|
||||||
submenu: [
|
|
||||||
{
|
|
||||||
label: 'Reload',
|
|
||||||
click: () => {win.webContents.reload();}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Force Reload',
|
|
||||||
click: () => {win.webContents.reloadIgnoringCache();}
|
|
||||||
},
|
|
||||||
{type: 'separator'},
|
|
||||||
{
|
|
||||||
label: 'Zoom In',
|
|
||||||
click: () => {win.webContents.setZoomLevel(win.webContents.getZoomLevel() + 1);}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Zoom Out',
|
|
||||||
click: () => {win.webContents.setZoomLevel(win.webContents.getZoomLevel() - 1);}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Reset Zoom',
|
|
||||||
click: () => {win.webContents.setZoomLevel(0)}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Navigation',
|
|
||||||
submenu: [
|
|
||||||
{
|
|
||||||
label: 'Go back',
|
|
||||||
click: () => {
|
|
||||||
if (win.webContents.canGoBack()) {
|
|
||||||
win.webContents.goBack();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Go forward',
|
|
||||||
click: () => {
|
|
||||||
if (win.webContents.canGoForward()) {
|
|
||||||
win.webContents.goForward();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} ,
|
|
||||||
{
|
|
||||||
label: 'Restart App',
|
|
||||||
click: () => {app.relaunch(); app.quit();}
|
|
||||||
} ,
|
|
||||||
{
|
|
||||||
label: 'Quit App',
|
|
||||||
click: () => {app.quit();}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
];
|
delete MenuItem.role;
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkCheckbox(item) {
|
||||||
|
//check item
|
||||||
|
item.checked = !item.checked;
|
||||||
|
//update menu
|
||||||
|
win.webContents.send('updateMenu', true);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user