Reputation: 7397
I am new to writing in Electron on windows. When I create a menuTemplate
this is showing on all of my windows that are opened. Is there a way to only have the menu appear on my mainWindow
? (Not on the pop up windows I create)
Index.js
const electron = require('electron');
const { app, BrowserWindow, Menu } = electron;
let mainWindow;
let addWindow;
app.on('ready', () => {
mainWindow = new BrowserWindow({});
mainWindow.loadURL(`file://${__dirname}/main.html`);
mainWindow.on('closed', () => app.quit());
const mainMenu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(mainMenu);
});
function createAddWindow() {
addWindow = new BrowserWindow({
width: 300,
height: 200,
title: 'Add New Todo'
});
addWindow.loadURL(`file://${__dirname}/add.html`)
}
const menuTemplate = [
{
label: 'File',
submenu: [
{
label: 'New Todo' ,
click() { createAddWindow(); }
},
{
label: 'Quit',
accelerator: process.platform === 'darwin' ? 'Command+Q' : 'Ctrl+Q',
click() {
app.quit();
}
}
]
}
];
if (process.platform === 'darwin') {
menuTemplate.unshift({});
}
Upvotes: 2
Views: 2557
Reputation: 1129
Change Menu.setApplicationMenu(mainMenu);
to mainWindow.setMenu(mainMenu)
.
The Menu.setApplicationMenu
sets the Menu for all BrowserWindows, while win.setMenu
sets it only for the window it is called on.
Upvotes: 8