2016-11-05 76 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