原始菜单未显示OS X Electron

时间:2016-11-05 16:39:58

标签: javascript electron

我使用电子快速启动来创建一个电子应用程序,我希望唯一的本机菜单显示为“编辑”菜单,其中包含常见的嫌疑人。

然而,在搜索了所有相关的谷歌搜索结果“电子菜单不能正常工作”之后,我感到很茫然。

我当前的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()
  }
})

我也用电子打包机包装它,但无济于事。

我在main.js文件中运行它,这是我从网上的大量模糊或复杂信息中收集到的,是主要过程,因此我应该创建菜单。< / p>

我也试过在render.js中这样做,我看到了建议。无济于事。它将显示默认的电子快速启动菜单,或者仅显示以应用程序命名的简单菜单,其中包含一个标记为“退出”的项目。

我可能做错了什么,以及我可能从可用信息中误解了什么?

修改:我实际上附加了错误的文件,第一次尝试使用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));

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

如@Vadim Macagon在评论中所述,请确保对<?php wp_get_current_user(); ?> 的呼叫在Menu.setApplicationMenu()中。对我来说,它解决了这个问题。

答案 2 :(得分:0)

也许您将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

答案 3 :(得分:0)

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

我浪费了大量时间来排查为什么它没有出现。