To create an Electron app with a toolbar (also known as a title bar or menu bar) on Windows, including an icon, you need to customize the BrowserWindow and use Electron's Menu module. Below is a step-by-step guide to achieve this:
Prerequisites
-
Ensure you have Node.js and npm installed.
-
Initialize an Electron project if you haven't already:
mkdir electron-app
cd electron-app
npm init -y
npm install electron --save-dev
-
Create the basic structure of your Electron app with a main.js file, an index.html file, and an icon file (e.g., icon.ico for Windows).
Step 1: Set Up the Basic Electron App
Here’s a basic main.js file to start with:
const { app, BrowserWindow, Menu } = require('electron');
const path = require('path');
function createWindow() {
// Create the browser window
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__dirname, 'icon.ico'), // Set the app icon
webPreferences: {
nodeIntegration: true, // Enable Node.js integration in the renderer process
contextIsolation: false, // Disable context isolation for simplicity (not recommended for production)
},
});
// Load the index.html file
mainWindow.loadFile('index.html');
// Remove the default menu bar (optional, if you want a custom toolbar)
mainWindow.setMenuBarVisibility(false); // Hides the default menu bar
}
// This method will be called when Electron has finished initialization
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
// Quit when all windows are closed, except on macOS
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
Step 2: Create an Icon
- Prepare an icon file in
.ico format for Windows. You can use tools like GIMP, Photoshop, or online converters (e.g., favicon.io) to create an .ico file.
- Place the
icon.ico file in the root directory of your project (or adjust the path in the icon option of BrowserWindow).
Step 3: Create a Custom Toolbar (Menu Bar)
To add a custom toolbar (menu bar) with an icon, you can use Electron's Menu module. Here’s how to create a custom menu bar:
const { app, BrowserWindow, Menu } = require('electron');
const path = require('path');
function createWindow() {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__dirname, 'icon.ico'), // Set the app icon
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
mainWindow.loadFile('index.html');
// Create a custom menu
const menuTemplate = [
{
label: 'File',
submenu: [
{
label: 'New Window',
click: () => {
createWindow(); // Open a new window
},
},
{
label: 'Exit',
click: () => {
app.quit(); // Quit the app
},
},
],
},
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
],
},
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{ type: 'separator' },
{ role: 'resetZoom' },
{ role: 'zoomIn' },
{ role: 'zoomOut' },
{ type: 'separator' },
{ role: 'togglefullscreen' },
],
},
];
// Build the menu from the template
const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu); // Set the custom menu
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
Step 4: Create a Basic index.html
Create an index.html file in the root directory of your project:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Electron App</title>
</head>
<body>
<h1>Welcome to Your Electron App</h1>
<p>This is a sample Electron app with a custom toolbar and icon.</p>
</body>
</html>
Step 5: Update package.json
Ensure your package.json has a main field pointing to main.js and a start script to run the app:
{
"name": "electron-app",
"version": "1.0.0",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "^21.0.0"
}
}
Step 6: Run the App
Run the app using the following command:
npm start
Step 7: Customize the Toolbar Further (Optional)
- Add Icons to Menu Items: Electron does not natively support icons in the menu bar, but you can use third-party libraries like
electron-menu-icons or create custom solutions using HTML/CSS in the renderer process.
- Custom Title Bar: If you want a completely custom title bar (e.g., like VS Code), you can disable the default title bar by setting
frame: false in the BrowserWindow options and then create a custom title bar using HTML/CSS/JavaScript in the renderer process. Example:
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__dirname, 'icon.ico'),
frame: false, // Disable the default title bar
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
Then, in your index.html, add a custom title bar:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Electron App</title>
<style>
.titlebar {
height: 30px;
background: #333;
color: white;
display: flex;
justify-content: space-between;
align-items: center;
-webkit-app-region: drag; /* Make the title bar draggable */
}
.titlebar-button {
padding: 0 10px;
cursor: pointer;
-webkit-app-region: no-drag; /* Prevent buttons from being draggable */
}
.titlebar-button:hover {
background: #555;
}
</style>
</head>
<body>
<div class="titlebar">
<div>My Electron App</div>
<div>
<span class="titlebar-button" onclick="minimizeWindow()">-</span>
<span class="titlebar-button" onclick="maximizeWindow()">□</span>
<span class="titlebar-button" onclick="closeWindow()">x</span>
</div>
</div>
<h1>Welcome to Your Electron App</h1>
<p>This is a sample Electron app with a custom toolbar and icon.</p>
<script>
const { ipcRenderer } = require('electron');
function minimizeWindow() {
ipcRenderer.send('minimize-window');
}
function maximizeWindow() {
ipcRenderer.send('maximize-window');
}
function closeWindow() {
ipcRenderer.send('close-window');
}
</script>
</body>
</html>
And in your main.js, handle the IPC events:
const { app, BrowserWindow, Menu, ipcMain } = require('electron');
const path = require('path');
function createWindow() {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__dirname, 'icon.ico'),
frame: false, // Disable the default title bar
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
mainWindow.loadFile('index.html');
// Handle window controls via IPC
ipcMain.on('minimize-window', () => {
mainWindow.minimize();
});
ipcMain.on('maximize-window', () => {
if (mainWindow.isMaximized()) {
mainWindow.unmaximize();
} else {
mainWindow.maximize();
}
});
ipcMain.on('close-window', () => {
mainWindow.close();
});
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
Step 8: Test on Windows
Ensure you test the app on a Windows machine to verify the icon and toolbar behavior. The .ico format is specific to Windows, so if you plan to support other platforms (e.g., macOS or Linux), you’ll need additional icon formats (e.g., .icns for macOS, .png for Linux).
Notes
- Security: For production apps, avoid using
nodeIntegration: true and contextIsolation: false. Instead, use Electron's secure practices, such as preload scripts and context isolation.
- Packaging: To distribute your app, use tools like
electron-builder or electron-packager to package it into an executable with the icon embedded.
This setup provides a basic Electron app with a toolbar and icon on Windows. You can further customize the toolbar and UI based on your needs.