I'm new to Electron and having problems finding a working example of an application menu.
When trying to combine the Quick Start app with the example from the Class: Menu page of the Electron Documentation, nothing seems to happen — chaning the label
values has no effect.
Googling raises more questions than it solves — such as, do I need to package my application to effect change in the application menu, or do I need to move my main.js
and package.json
to $projectRoot/resources/app
(and if so, do I need to package it to run it)?
Is there a better way to get the hang of Electron...?
When I add the below to my app on OSX, the Application menu has one entry — Electron
, with one option, Quit
:
const electron = require('electron');
var menu = electron.Menu.buildFromTemplate([
{
label: 'Electron',
submenu: [
{
label: 'Options',
click: function() {
alert('Test');
}
}
]
}
]);
electron.Menu.setApplicationMenu(menu);
We will be using two modules – the Menu and the MenuItem modules. Note that the Menu and the MenuItem modules are only available in the main process. For using these modules in the renderer process, you need the remote module. We will come across this when we create a context menu.
Inserting a custom application menu For creating a custom electron menu, first, we have to import the menu module into the main application file – 'main. js'. After importing the module, create a template for the menu using “buildFromTemplate” function from the menu module.
The Application Menu is composed of a drop-down button control that displays a menu containing Commands that expose functionality related to a complete project, such as an entire document, picture, or movie. Examples include the New, Open, Save, and Exit Commands.
You don't need to package your app to change the application menu. Check that you're calling Menu.setApplicationMenu()
after the ready
event is emitted, e.g.
app.on('ready', () => {
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
})
It is difficult to see whats the prb since you did not provide any code. However, here how this should work: In the main.js do this :
var MenuItem = require('menu-item');
And then make a function createMenu :
global.yourApp = {
init() {
whatsApp.createMenu();
config.init();
},
createMenu() {
yourApp.menu =
AppMenu.buildFromTemplate(require('./menu'));
AppMenu.setApplicationMenu(yourApp.menu);
}
}
./menu you will have the menu.js file that should look something like this:
(function(scope) {
"use strict";
var template = [
{
label: 'Edit',
submenu: [
{
label: 'Undo',
accelerator: 'CmdOrCtrl+Z',
role: 'undo'
}
......
]
}]
})(this);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With