electron 自定义菜单未在电子中显示- MacOS

vi4fp9gy  于 2022-12-08  发布在  Electron
关注(0)|答案(1)|浏览(291)

我刚刚开始使用Electron并按照入门指南操作。在运行脚本启动一个带有自定义菜单的应用程序后,应用程序的菜单仍然显示Electron而不是我的自定义名称“File”。我尝试了类似问题中的建议,特别是将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);

于飞:

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

知道我错过了什么吗

smdncfj3

smdncfj31#

以下是为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);

相关问题