allencoded
allencoded

Reputation: 7265

Electron how to load Main Menu when mainWindow is severing external HTML

In electron I have read how to include the menu code typically in the index.html. But in my application I am loading an external website and would like to know how to do this.

'use strict';

var electron = require('electron');
var app = electron.app;
var BrowserWindow = require('browser-window');

app.on('ready', function () {

  var mainWindow = new BrowserWindow ({
    width: 800,
    height: 600,
    webPreferences: {
      webSecurity: false
    }
  });

  mainWindow.loadURL('https://example.com/');
  mainWindow.webContents.openDevTools();

});

I do not have control over what is served on mysite.net, so how do I make a custom menu in this case the Proper Way! I figure I could just do it in app.js but curious if that would be proper?

UPDATE

Here is how I exactly handled it if anyone is curious:

'use strict';

var electron = require('electron');
var app = electron.app;
var BrowserWindow = require('browser-window');

var Menu = require('menu');
var customMenu = require('./libs/custom-menu');

app.on('ready', function () {

  var mainWindow = new BrowserWindow ({
    width: 800,
    height: 600
  });

  mainWindow.loadURL('https://www.example.com');

  var template = customMenu.getTemplate();
  var menu = Menu.buildFromTemplate(template);
  Menu.setApplicationMenu(menu);
});

custom-menu.js:

exports.getTemplate = function () {
  var template = [
    {
      label: 'MyApp',
      submenu: [
        {
          label: 'Sub Navigation',
          click: function () {
            console.log('test');
          }
        }
      ]
    }
  ];

  return template;
}

Upvotes: 0

Views: 954

Answers (1)

inukshuk
inukshuk

Reputation: 1457

You just use the Menu module in your main process. Actually, Menu is part of the main process modules anyway, so I'd say this is indeed the 'proper' way to use it. The example in the docs calls it via remote to demonstrate the possibility of setting the menu via the renderer; but you can just call Menu.setApplicationMenu() from the main process directly.

Upvotes: 1

Related Questions