Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron Application Menu Working Example

Tags:

electron

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...?

Edit

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);
like image 787
Lee Goddard Avatar asked Dec 21 '16 09:12

Lee Goddard


People also ask

How do you create an Electron 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.

How do you customize the menu bar on the Electron app?

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.

What are application menus?

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.


2 Answers

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)
})
like image 94
Vadim Macagon Avatar answered Oct 26 '22 22:10

Vadim Macagon


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);
like image 21
Beslinda N. Avatar answered Oct 26 '22 22:10

Beslinda N.