Electron Menus, Tray Icons, and Multiple Windows
🎯 Introduction
This article covers two common features: opening a settings window from a menu and restoring a hidden main window through a tray icon.
The main process implements these features because application menus and windows are Electron desktop capabilities rather than page DOM elements.
By the end of this article, you will be able to:
- Create an application menu with
Menu. - Add
clickhandlers to menu items. - Create a new
BrowserWindowinside aclickhandler. - Understand where tray icons appear and what they do.
🧱 Prepare a settings page
Create settings.html, the page the settings window will display.
Place settings.html in the same directory as main.js:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="Content-Security-Policy" content="default-src 'self'" />
<title>Settings</title>
</head>
<body>
<h1>Settings window</h1>
<p>If you can see this page, the menu successfully opened a new window.</p>
</body>
</html>
This only prepares the page; no new window appears yet.
🛠 Write the main process code
Next, replace main.js.
After running this code, choosing “File → Settings” opens the settings.html page you just created.
const { app, BrowserWindow, Menu } = require('electron/main');
const path = require('node:path');
function createMainWindow() {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
});
mainWindow.loadFile(path.join(__dirname, 'index.html'));
}
function createMenu() {
const template = [];
if (process.platform === 'darwin') {
template.push({ role: 'appMenu' });
}
template.push({
label: 'File',
submenu: [
{
label: 'Settings',
click: () => {
const settingsWindow = new BrowserWindow({
width: 520,
height: 420,
});
settingsWindow.loadFile(path.join(__dirname, 'settings.html'));
},
},
{ type: 'separator' },
{ role: 'quit' },
],
});
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
app.whenReady().then(() => {
createMainWindow();
createMenu();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createMainWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
Run the project:
npm start
Choose “File → Settings”. A new settings window should open.
On macOS, click the Electron window first to focus the app, then find the “File” menu at the top of the screen.
✨ Understand the menu code
createMenu creates the application menu.
Start with these lines:
const template = [];
if (process.platform === 'darwin') {
template.push({ role: 'appMenu' });
}
template is an array of menu configuration objects.
macOS has a special convention: the first menu usually displays the application name. We therefore add { role: 'appMenu' } first on macOS, followed by our “File” menu.
Now look at the “File” menu itself:
template.push({
label: 'File',
submenu: [
{
label: 'Settings',
click: () => {
const settingsWindow = new BrowserWindow({
width: 520,
height: 420,
});
settingsWindow.loadFile(path.join(__dirname, 'settings.html'));
},
},
{ type: 'separator' },
{ role: 'quit' },
],
});
The relationship is straightforward:
label: 'File': The “File” menu in the menu bar.label: 'Settings': The “Settings” item under “File”.click: The function executed when “Settings” is clicked.new BrowserWindow(): Create a new window.loadFile(): Displaysettings.htmlin the new window.
These two lines actually open the settings window:
const settingsWindow = new BrowserWindow({
width: 520,
height: 420,
});
settingsWindow.loadFile(path.join(__dirname, 'settings.html'));
🪤 Common problems
The “File” menu is missing
On macOS, the menu is at the top of the screen rather than inside the window. Click the Electron window to focus the app, then check the screen's menu bar.
Clicking “Settings” does nothing
Check that settings.html is in the same directory as main.js. Completely quit Electron, then run npm start again.
Each click opens another settings window
This is expected. The article first demonstrates opening a window from a menu click. To allow only one settings window, you must also keep a window reference, which you can learn later.
Why menu code belongs in the main process
An application menu is part of the desktop app rather than an HTML element, so the main process creates it.
🖥 What is a tray icon?
Electron's Tray represents a small application icon at the edge of the system desktop. It is distinct from the macOS Dock or the Windows taskbar as a whole:
| System | Typical tray location | How it differs |
|---|---|---|
| Windows | Notification area on the right of the taskbar, also called the system tray | The taskbar also shows open windows; the tray is only one small area |
| macOS | Status area on the right of the menu bar at the top of the screen | The Dock launches and switches apps; tray icons show app menus or status |
| Linux | Depends on the desktop environment, usually a panel's status area | Tray support varies across desktop environments |
Tray icons suit apps that run in the background, such as download, synchronization, or chat tools. Clicking the icon can show the main window, pause tasks, or quit the app.
The main process usually creates the tray icon and keeps a main window reference. This example creates a tray menu whose “Show window” item restores a hidden main window:
const { Tray, Menu } = require('electron/main');
let tray;
function createTray() {
tray = new Tray(path.join(__dirname, 'assets/tray.png'));
tray.setContextMenu(
Menu.buildFromTemplate([
{
label: 'Show window',
click: () => mainWindow.show(),
},
{
label: 'Quit',
role: 'quit',
},
])
);
}
The main process manages windows, tray icons, and menus. Clear references when windows close, and define each window's role when using multiple windows. Avoid unconditionally creating another window on every menu click.
🧾 Summary
- Create Electron application menus with
Menu. Menu.buildFromTemplateconverts menu configuration into a menu object.Menu.setApplicationMenuinstalls the menu in the app.- A menu item's
clickhandler runs when the user clicks it. - Creating a
BrowserWindowinclicklets a menu open a new window. Trayis an application status icon at the desktop's edge, distinct from the Dock or the entire taskbar.- On macOS, menus appear at the top of the screen, outside the window.
❓ Questions and answers
Q1: Can the menu be written in index.html?
No. This is a desktop application menu rather than a web button, so it must be created in the main process.
Q2: Does click run automatically?
No. Its function runs only when the user clicks the corresponding menu item.
Q3: Why does the settings window load settings.html?
BrowserWindow creates the window. loadFile specifies what it displays.
🧪 Practice
Exercise 1: Change the settings window size to 600 × 400.
const settingsWindow = new BrowserWindow({
// Write your code here
});
Exercise 2: Rename the “Settings” menu item to “Preferences”.
{
label: 'Preferences',
click: () => {
// Keep the existing window creation code
},
}
🎉 You now know the basic pattern for opening an Electron window from a menu!
