自定义菜单未在Electron中显示-MacOS

时间:2019-03-06 03:17:34

标签: javascript electron

我刚开始使用Electron并遵循《入门指南》。运行脚本以使用自定义菜单启动应用程序后,该应用程序的菜单仍显示Electron,而不是我的自定义名称“文件”。我尝试了类似问题的建议,特别是将Menu.setApplicationMenu()放在function createWindow()中,但这仍然行不通。

代码如下:

// main.js

const electron = require('electron');
const url = require('url'); 
const path = require('path');

const {app, BrowserWindow, Menu} = electron;

let mainWindow;

function createWindow() {
  // Create new window
  mainWindow = new BrowserWindow({width: 900, height: 550});

  // Load html into window
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'mainWindow.html'),
    protocol: 'file:',
    slashes: true
  }));

  // Create menu template
  const mainMenuTemplate = [
    {
        label: 'File',
        submenu: [
        {
            role: 'quit'
        }
        ]
    }
  ];

  // Insert menu
  Menu.setApplicationMenu(Menu.buildFromTemplate(mainMenuTemplate));
}

// Listen for app to be ready
app.on('ready', createWindow);

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>ShoppingList</title> 
</head>
<body>
  <h1>ShoppingList</h1>
</body>
</html>

知道我缺少什么吗?

1 个答案:

答案 0 :(得分:0)

这是为Mac创建菜单的工作示例:

    let template = [
  {
    label: 'My app title',
    submenu: [{
        label: 'menu command name',
        click: function() {
          //execute command
        }
      },
      {
        label: 'quit app command name',
        accelerator: 'CmdOrCtrl+Q',
        click: function() {
          //quit command execution 
        }
      }
    ]
  },
  {
    label: 'edit',
    submenu: [{
        label: 'undo',
        accelerator: 'CmdOrCtrl+Z',
        selector: 'undo:'
      },
      {
        label: 'redo',
        accelerator: 'Shift+CmdOrCtrl+Z',
        selector: 'redo:'
      },
      {
        type: 'separator'
      },
      {
        label: 'cut',
        accelerator: 'CmdOrCtrl+X',
        selector: 'cut:'
      },
      {
        label: 'copy',
        accelerator: 'CmdOrCtrl+C',
        selector: 'copy:'
      },
      {
        label: 'paste',
        accelerator: 'CmdOrCtrl+V',
        selector: 'paste:'
      },
      {
        label: 'selectAll',
        accelerator: 'CmdOrCtrl+A',
        selector: 'selectAll:'
      }]
  }
];

let osxMenu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(osxMenu);