2016-11-05 3 views
1

Я использовал электронное быстрое начало для создания приложения Electron, и я хочу, чтобы единственное собственное меню отображалось как меню «Редактировать», с обычными подозреваемыми внутри.Родные меню, не отображающие OS X Electron

Однако после поиска и исчерпания всех соответствующих результатов 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() 
    } 
}) 

Я также упаковывают его с электронно-упаковщик, но безрезультатно.

Я запускаю его в файле main.js, который из того, что я могу собрать из масс либо смутной, либо извилистой информации по сети, является основным процессом, и поэтому я должен создать меню.

Я также пробовал делать это в render.js, который я видел. Но безрезультатно. Он будет отображаться по умолчанию с электронным быстром запуском меню или просто с простым меню, названным в честь приложения, содержащим один элемент с надписью «Выход».

Что я могу делать неправильно, и что я мог бы неправильно понять из имеющейся информации?


Edit: Я на самом деле прилагается неправильный файл, попытался с помощью 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

Смежные вопросы