简体   繁体   English

本机菜单未显示 OS X Electron

[英]Native Menus not showing OS X Electron

I used the electron-quick-start to create an Electron app, and I want the only native menu to show to be the 'Edit' menu, with the usual suspects inside.我使用 electron-quick-start 创建了一个 Electron 应用程序,我希望唯一显示的本机菜单是“编辑”菜单,里面有通常的嫌疑人。

However, after searching and exhausting all relevant Google results for 'electron menu not working', I'm at a loss.然而,在搜索并用尽“电子菜单不起作用”的所有相关谷歌结果后,我不知所措。

My current main.js file:我当前的 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()
  }
})

I've also packaged it up with electron-packager, to no avail.我也用electron-packager打包了,没用。

I'm running it in the main.js file, which from what I can gather from the masses of either vague or convoluted information around the web, is the main process and therefore one in which I should create the menus.我在 main.js 文件中运行它,从我可以从网络上大量模糊或复杂的信息中收集到的信息是主要过程,因此我应该在其中创建菜单。

I also tried doing it in render.js, which I saw suggested.我也试过在 render.js 中这样做,我看到了建议。 To no avail.无济于事。 It'll either show up with the default electron-quick-start menu, or just a simple menu named after the app, containing one item labelled 'Quit'.它将显示默认的电子快速启动菜单,或者只是一个以应用程序命名的简单菜单,其中包含一个标记为“退出”的项目。

What might I be doing wrong, and what might I have misunderstood from the available information?我可能做错了什么,我可能从可用信息中误解了什么?


Edit: I actually attached the wrong file, tried using Menu.setApplicationMenu() the first time, like so:编辑:我实际上附加了错误的文件,第一次尝试使用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));

The issue here is that BrowserWindow.setMenu() is only available on Windows and Linux.这里的问题是BrowserWindow.setMenu()仅在 Windows 和 Linux 上可用。 On macOS you should use Menu.setApplicationMenu() .在 macOS 上,您应该使用Menu.setApplicationMenu()

As @Vadim Macagon stated in comment, make sure that the call to Menu.setApplicationMenu() is in createWindow() .正如@Vadim Macagon 在评论中所述,确保对Menu.setApplicationMenu()的调用在createWindow() For me it fixed the problem.对我来说它解决了这个问题。

maybe you set LSUIElement to 1, that means an agent app, that is, an app that should not appear in the Dock.也许你把 LSUIElement 设置为 1,这意味着一个代理应用程序,也就是一个不应该出现在 Dock 中的应用程序。 Change the LSUIElement to 0, the build app's menu will show up.将 LSUIElement 更改为 0,将显示构建应用程序的菜单。

electron build config电子构建配置

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

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

Note that on OSX the menu is not on the window itself but on the top of dosktop.请注意,在 OSX 上,菜单不在窗口本身上,而是在 dosktop 的顶部。

I lost bunch of time trying to troubleshoot why it was not showing up.我浪费了大量时间试图解决为什么它没有出现。

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

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