本机菜单未显示 OS X Electron

Native Menus not showing OS X Electron

我使用 electron-quick-start 创建了一个 Electron 应用程序,我希望唯一显示的本机菜单是 'Edit' 菜单,里面有常见的嫌疑人。

然而,在搜索并耗尽 'electron menu not working' 的所有相关 Google 结果后,我不知所措。

我当前的 main.js 文件:

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

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

app.setName('mathulator');

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

  // and load the index.html of the app.
  mainWindow.loadURL(`file://${__dirname}/index.html`)

  // In this file you can include the rest of your app's specific main process
  // code. You can also put them in separate files and require them here.

  const template = [
    {
        label: 'Mathulator',
        submenu: [
            {
                role: 'quit'
            }
        ]
    },
    {
      label: 'Edit',
      submenu: [
        {
          role: 'undo'
        },
        {
          role: 'redo'
        },
        {
          type: 'separator'
        },
        {
          role: 'cut'
        },
        {
          role: 'copy'
        },
        {
          role: 'paste'
        },
        {
          role: 'pasteandmatchstyle'
        },
        {
          role: 'delete'
        },
        {
          role: 'selectall'
        }
      ]
    }
   ]

  mainWindow.setMenu(Menu.buildFromTemplate(template))

  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow()
  }
})

我也用electron-packager打包过,没用

我 运行 它在 main.js 文件中,这是我从网络上大量模糊或令人费解的信息中收集到的主要过程,因此是其中的一个我应该创建菜单。

我也在 render.js 中尝试这样做,我看到了建议。无济于事。它会显示默认的电子快速启动菜单,或者只是一个以应用程序命名的简单菜单,包含一个标记为 'Quit'.

的项目

我可能做错了什么,我可能从现有信息中误解了什么?


编辑: 我实际上附加了错误的文件,第一次尝试使用 Menu.setApplicationMenu(),像这样:

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

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

app.setName('mathulator');

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

    // and load the index.html of the app.
    mainWindow.loadURL(`file://${__dirname}/index.html`);

    // Emitted when the window is closed.
    mainWindow.on('closed', function () {
        // Dereference the window object, usually you would store windows
        // in an array if your app supports multi windows, this is the time
        // when you should delete the corresponding element.
        mainWindow = null;
    });
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', function () {
    // On OS X it is common for applications and their menu bar
    // to stay active until the user quits explicitly with Cmd + Q
    if (process.platform !== 'darwin') {
        app.quit();
    }
})

app.on('activate', function () {
    // On OS X it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (mainWindow === null) {
        createWindow();
    }
})

const template = [
    {
        label: 'Mathulator',
        submenu: [
            {
                role: 'quit'
            }
        ]
    },
    {
        label: 'Edit',
        submenu: [
            {
                role: 'undo'
            },
            {
                role: 'redo'
            },
            {
                type: 'separator'
            },
            {
                role: 'cut'
            },
            {
                role: 'copy'
            },
            {
                role: 'paste'
            },
            {
                role: 'pasteandmatchstyle'
            },
            {
                role: 'delete'
            },
            {
                role: 'selectall'
            }
        ]
    }
];

Menu.setApplicationMenu(Menu.buildFromTemplate(template));

这里的问题是 BrowserWindow.setMenu() 仅适用于 Windows 和 Linux。在 macOS 上你应该使用 Menu.setApplicationMenu().

正如@Vadim Macagon 在评论中所述,确保对 Menu.setApplicationMenu() 的调用在 createWindow() 中。对我来说,它解决了这个问题。

也许你将LSUIElement设置为1,这意味着代理应用程序,即不应出现在Dock中的应用程序。将 LSUIElement 更改为 0,将显示构建应用程序的菜单。

电子构建配置

mac: {
            icon: 'build/icons/icon.icons',
            extendInfo: {
              LSUIElement: 0
            }
          }

LSUIElement 的详细信息在这里 https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html#//apple_ref/doc/uid/20001431-108256

请注意,在 OSX 上,菜单不在 window 本身上,而是在 dosktop 的顶部。

我浪费了很多时间来解决它没有出现的原因。