const {Menu, BrowserWindow, app} = require("electron");
const {windows, addWindow} = require("./multiWindow");
const {getLoadedUrl} = require("./loadedUrl");

function templateFunc() {
  var template = [
    {
      label: "Application",
      submenu: [
        {
          label: "About Application",
          selector: "orderFrontStandardAboutPanel:",
        },
        {
          label: "Quit",
          accelerator: "CmdOrCtrl+Alt+Q",
          click: function() {
            app.quit();
          },
        },
      ],
    },
    {
      label: "File",
      submenu: [
        {
          label: "New Window",
          accelerator: "CmdOrCtrl+N",
          click: function() {
            addWindow({loadedUrl: getLoadedUrl()});
          },
        },
        {
          label: "Reload",
          accelerator: "CmdOrCtrl+Alt+R",
          click: function() {
            windows.forEach(mainWindow => {
              if (!mainWindow.isDestroyed()) {
                mainWindow.reload();
              }
            });
          },
        },

        {
          label: "Kiosk",
          accelerator: "CmdOrCtrl+Alt+K",
          click: function() {
            if (windows[0] && windows[0].isKiosk()) {
              windows.forEach(mainWindow => {
                if (!mainWindow.isDestroyed()) {
                  mainWindow.setKiosk(false);
                  setMenubar();
                }
              });
            } else {
              windows.forEach(mainWindow => {
                mainWindow.setKiosk(true);

                clearMenubar();
              });
            }
          },
        },
        {
          label: "Dev Tools",
          accelerator: "CmdOrCtrl+Alt+I",
          click: function() {
            const focused = BrowserWindow.getFocusedWindow();
            focused && focused.webContents.openDevTools();
          },
        },
      ],
    },
    {
      label: "Edit",
      submenu: [
        {label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:"},
        {label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:"},
        {label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:"},
        {
          label: "Select All",
          accelerator: "CmdOrCtrl+A",
          selector: "selectAll:",
        },
      ],
    },
  ];
  return template;
}

function setMenubar() {
  const template = templateFunc();
  Menu.setApplicationMenu(Menu.buildFromTemplate(template));
  windows.forEach(w => {
    if (!w.isDestroyed()) {
      w.setMenuBarVisibility(true);
      w.autoHideMenuBar = false;
    }
  });
}

function clearMenubar() {
  Menu.setApplicationMenu(null);
}

module.exports.setMenubar = setMenubar;

module.exports.clearMenubar = clearMenubar;

@@ -1182,21 +1182,93 @@
 if (
-windows%5B0%5D &&
+%0A              windows%5B0%5D &&%0A              !windows%5B0%5D.isDestroyed() &&%0A             
  win
@@ -1284,16 +1284,29 @@
 sKiosk()
+%0A            
 ) %7B%0A    
@@ -1569,32 +1569,83 @@
 mainWindow =%3E %7B%0A
+                if (!mainWindow.isDestroyed()) %7B%0A  
                 
@@ -1672,16 +1672,18 @@
 true);%0A%0A
+  
         
@@ -1698,32 +1698,50 @@
 clearMenubar();%0A
+                %7D%0A
               %7D)

Fix an error when trying to enter the kiosk. Closes #2727