2016-11-05 80 views
1

我使用电子快速启动来创建一个Electron应用程序,并且我希望唯一的本机菜单显示为“编辑”菜单,其中包含常见的嫌疑犯。本地菜单不显示OS X 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文件中运行它,从我可以从大量围绕Web的模糊或复杂信息中收集的信息是主要过程,因此我应该创建菜单。

我也尝试过在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)); 

回答

2

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

+0

请参阅编辑。试过,原来,仍然没有应用程序菜单。 – thephpdev

+1

@thephpdev在应用程序准备就绪后,您必须调用'Menu.setApplicationMenu()',因此将该调用移动到'createWindow()'中。 –

+0

我明白了。我现在感觉有点厚,我以为我尝试过,但现在我意识到我称之为'mainWindow.setMenu()'。我是新来的电子,这是我的第一个电子应用程序,如果你有兴趣。 https://github.com/CaelanStewart/Mathulator – thephpdev