简体   繁体   English

如何为 electron 中的特定 window 制作单独的菜单?

[英]How do i make a separate menu for a specific window in electron?

I am working on an about this app window on Windows and I want to make a custom menu bar for my about window.我正在 Windows 上开发一个关于这个应用程序 window 的应用程序,我想为我的关于 window 制作一个自定义菜单栏。 Since I already have a custom menu is there a way I can create another one and apply it only to that specific window?由于我已经有一个自定义菜单,有没有办法可以创建另一个菜单并将其仅应用于特定的 window?

Side note: Here is my code for the new window that is supposed to stop it from being adjusted and going into full screen, but for some reason the minimize and enlarge button still work.旁注:这是我的新 window 的代码,它应该阻止它被调整并进入全屏,但由于某种原因,最小化和放大按钮仍然有效。

app.on('ready', createWindow);
electron.app.on('ready', () => {
  //Triger update check
  if (!isDev) {
    autoUpdater.checkForUpdates();
  }
})

function createWindow(){
  //create brower window
  win = new BrowserWindow({
  backgroundColor: '#2e2c29',
  width: 800,
  height: 600,
  //transparent: true,
  frame: false,
  titleBarStyle: 'hidden',
  backgroundColor: '#0000',
  webPreferences: {
    nodeIntegration: true
}
  });


//Quit when all windows are closed
  app.on('window-all-closed', () => {
    app.quit()
  })

app.once('ready', function() {

const template = [
  {
    label: 'File',
    submenu: [
      {
          label: 'About Hubris',
          click: () =>
          openAboutWindow()
          },
      { type: 'separator' },
      { role: 'hide' },
      { role: 'hideothers' },
      { role: 'unhide' },
      { type: 'separator' },
      { role: 'quit' }
    ]
  },
  {
label: 'View',
submenu: [

  { role: 'minimize' },
  { role: 'zoom' },
  { type: 'separator' },
  { role: 'togglefullscreen' }
    ]
  },
]

const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)



//here is the code for the about window
var newWindow = null

function openAboutWindow() {

  if (newWindow) {
    newWindow.focus()
    return
  }

  newWindow = new BrowserWindow({
    height: 439,
    resizable: false,
    width: 599,
    title: 'About Hubris',
    minimizable: false,
    fullscreenable: false,
    frame: false,
    titleBarStyle: 'hidden',

  })

  newWindow.loadURL('file://' + __dirname + '/about-this-app.html')

  newWindow.on('closed', function() {
    newWindow = null
  })
}
});

To do this, you have to import menu from electron in at the top of our main.js file:为此,您必须从 main.js 文件顶部的 electron 导入菜单:

// From
const {app, BrowserWindow} = require('electron')

// To
const {app, BrowserWindow, Menu} = require('electron'). 

Then, near the bottom of our createWindow() function, we add:然后,在我们的 createWindow() function 的底部附近,我们添加:

function createWindow () {

  // some code here removed for brevity 

  var menu = Menu.buildFromTemplate([
      {
          label: 'Menu',
          submenu: [
              {label:'Adjust Notification Value'},
              {label:'CoinMarketCap'},
              {label:'Exit'}
          ]
      }
  ])
  Menu.setApplicationMenu(menu); 
}

Next, we reference Menu.buildFromTemplate([{}]), which is where our menu is actually defined and built, within a series of arrays and objects.接下来,我们在一系列 arrays 和对象中引用 Menu.buildFromTemplate([{}]),这是我们的菜单实际定义和构建的地方。

The "label" represents the name you want your menu to display so put what you like. “标签”代表您希望菜单显示的名称,因此请输入您喜欢的名称。

The "submenu" property is an array of objects, with each object defining the actual menu items displayed when the label is clicked. “子菜单”属性是一个对象数组,每个 object 定义单击 label 时显示的实际菜单项。

Finally, use.setApplicationMenu to set the menu.最后,使用.setApplicationMenu 设置菜单。 If you save the project, and run npm start in the console, you will see the menu with it's items (array) being displayed but if your click on them nothing happens.如果您保存项目,并在控制台中运行 npm start,您将看到菜单及其项目(数组)正在显示,但如果您单击它们,则没有任何反应。 You can change this by going back to our main.js, add the following code to make our Exit button close the application:您可以通过返回到我们的 main.js 来更改它,添加以下代码以使我们的退出按钮关闭应用程序:

var menu = Menu.buildFromTemplate([
    {
        label: 'Menu',
            submenu: [
            {label:'Adjust Notification Value'},
            {label:'CoinMarketCap'},
            {
                label:'Exit', 
                click() { 
                    app.quit() 
                } 
            }
        ]
    }
  ])

So, to make a menu item clickable, we simply add a comma after the label value, and reference "click() { }"因此,要使菜单项可点击,我们只需在 label 值后添加一个逗号,并引用“click() { }”

In this case, we're calling app.quit()" when the Exit submenu item is clicked. Give it a try by running npm start in the console and click Exit. That's it!在这种情况下,我们在单击 Exit 子菜单项时调用 app.quit()"。通过在控制台中运行 npm start 并单击 Exit 来尝试一下。就是这样!

You can switch menus on the fly.您可以即时切换菜单。 I have an app with 'editor' and 'presentation' modes.我有一个具有“编辑器”和“演示”模式的应用程序。 I create and store a menu for each mode (they have different menu items):我为每种模式创建并存储一个菜单(它们有不同的菜单项):

let editorMenu = Menu.buildFromTemplate(menuTemplate);

and subscribe to the relevant window events (focus, blur, etc).并订阅相关的 window 事件(焦点、模糊等)。 Then when a window gets focus然后当 window 获得焦点时

Menu.setApplicationMenu(editorMenu);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM