mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 18:41:47 +00:00
84 lines
2.2 KiB
JavaScript
84 lines
2.2 KiB
JavaScript
const path = require("path");
|
|
|
|
const { Menu } = require("electron");
|
|
const electronLocalshortcut = require("electron-localshortcut");
|
|
|
|
const config = require("../../config");
|
|
const { setApplicationMenu } = require("../../menu");
|
|
const { injectCSS } = require("../utils");
|
|
|
|
//tracks menu visibility
|
|
let visible = true;
|
|
// win hook for fixing menu
|
|
let win;
|
|
|
|
const originalBuildMenu = Menu.buildFromTemplate;
|
|
// This function natively gets called on all submenu so no more reason to use recursion
|
|
Menu.buildFromTemplate = (template) => {
|
|
// Fix checkboxes and radio buttons
|
|
updateTemplate(template);
|
|
|
|
// return as normal
|
|
return originalBuildMenu(template);
|
|
};
|
|
|
|
module.exports = (winImport) => {
|
|
win = winImport;
|
|
|
|
// css for custom scrollbar + disable drag area(was causing bugs)
|
|
injectCSS(win.webContents, path.join(__dirname, "style.css"));
|
|
|
|
win.once("ready-to-show", () => {
|
|
|
|
setApplicationMenu(win);
|
|
|
|
//register keyboard shortcut && hide menu if hideMenu is enabled
|
|
if (config.get("options.hideMenu")) {
|
|
electronLocalshortcut.register(win, "Esc", () => {
|
|
switchMenuVisibility();
|
|
});
|
|
}
|
|
});
|
|
|
|
//set menu visibility on load
|
|
win.webContents.once("did-finish-load", () => {
|
|
// fix bug with menu not applying on start when no internet connection available
|
|
setMenuVisibility(!config.get("options.hideMenu"));
|
|
});
|
|
};
|
|
|
|
function switchMenuVisibility() {
|
|
setMenuVisibility(!visible);
|
|
}
|
|
|
|
function setMenuVisibility(value) {
|
|
visible = value;
|
|
win.webContents.send("updateMenu", visible);
|
|
}
|
|
|
|
function updateCheckboxesAndRadioButtons(item, isRadio, hasSubmenu) {
|
|
if (!isRadio) {
|
|
//fix checkbox
|
|
item.checked = !item.checked;
|
|
}
|
|
//update menu if radio / hasSubmenu
|
|
if (isRadio || hasSubmenu) {
|
|
win.webContents.send("updateMenu", true);
|
|
}
|
|
}
|
|
|
|
// Update checkboxes/radio buttons
|
|
function updateTemplate(template) {
|
|
for (let item of template) {
|
|
// Change onClick of checkbox+radio
|
|
if ((item.type === "checkbox" || item.type === "radio") && !item.fixed) {
|
|
const originalOnclick = item.click;
|
|
item.click = (itemClicked) => {
|
|
originalOnclick(itemClicked);
|
|
updateCheckboxesAndRadioButtons(itemClicked, item.type === "radio", item.hasSubmenu);
|
|
};
|
|
item.fixed = true;
|
|
}
|
|
}
|
|
}
|