Electron アプリケーションからメニューバーを削除 / 作成するサンプルコード
Electron アプリケーションでメニューバーを削除するには、主に以下の 2 つの方法があります。
mainWindow.setMenu(null) を使用する
これは、Electron API の mainWindow.setMenu
メソッドを使用して、メニューバーに null
を設定する方法です。最も簡単でシンプルな方法ですが、Windows と Linux ではメニューバーだけでなく、ウィンドウのタイトルバーやボタンも削除されてしまうという点に注意が必要です。
const { app, BrowserWindow } = require('electron');
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
});
win.setMenu(null); // メニューバーを削除
// ... 他の処理
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
カスタムメニューバーを作成する
Electron では、独自のメニューバーを作成することもできます。この方法では、メニューバーの外観や機能を自由にカスタマイズできますが、コード量が増え、複雑になります。
まず、Menu
モジュールを使用して、メニューバーの構造を定義します。
const { Menu } = require('electron');
const template = [
{
label: 'Edit',
submenu: [
{ label: 'Undo', role: 'undo' },
{ label: 'Redo', role: 'redo' },
{ type: 'separator' },
{ label: 'Cut', role: 'cut' },
{ label: 'Copy', role: 'copy' },
{ label: 'Paste', role: 'paste' },
{ type: 'separator' },
{ label: 'Select All', role: 'selectAll' }
]
},
{
label: 'View',
submenu: [
{ label: 'Toggle DevTools', role: 'toggledevtools' }
]
},
{
label: 'Window',
submenu: [
{ label: 'Minimize', role: 'minimize' },
{ label: 'Zoom', submenu: [
{ label: 'Actual Size', role: 'zoomIn' },
{ label: 'Zoom Out', role: 'zoomOut' },
{ type: 'separator' },
{ label: 'Toggle Fullscreen', role: 'togglefullscreen' }
]}
]
},
{
label: 'Help',
submenu: [
{ label: 'Learn More', click: () => { shell.openExternal('https://electronjs.org'); } },
{ label: 'Documentation', click: () => { shell.openExternal('https://electronjs.org/docs'); } },
{ type: 'separator' },
{ label: 'Report Issues', click: () => { const url = 'https://github.com/electron/electron/issues/new'; shell.openExternal(url); } }
]
}
];
const menu = Menu.buildFromTemplate(template);
次に、mainWindow.setMenu
メソッドを使用して、作成したメニューバーをウィンドウに設定します。
const { app, BrowserWindow } = require('electron');
const menu = require('./menu'); // 作成したメニューバーをインポート
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
});
win.setMenu(menu); // 作成したメニューバーを設定
// ... 他の処理
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
補足
- Electron のバージョンによっては、上記の方法でメニューバーを削除 / 設定できない場合があります。最新バージョンの Electron を使用していることを確認してください。
- メニューバーのカスタマイズの詳細については、Electron の公式ドキュメントを参照してください:
Electron アプリケーションからメニューバーを削除 / 作成するサンプルコード
mainWindow.setMenu(null) を使用する
const { app, BrowserWindow } = require('electron');
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
});
win.setMenu(null); // メニューバーを削除
// ... 他の処理
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
ポイント
mainWindow.setMenu(null)
を使用して、メニューバーにnull
を設定しています。- この方法は、Windows と Linux ではメニューバーだけでなく、ウィンドウのタイトルバーやボタンも削除されてしまうことに注意が必要です。
カスタムメニューバーを作成する
メニューバーの構造を定義する
const { Menu } = require('electron');
const template = [
{
label: 'Edit',
submenu: [
{ label: 'Undo', role: 'undo' },
{ label: 'Redo', role: 'redo' },
{ type: 'separator' },
{ label: 'Cut', role: 'cut' },
{ label: 'Copy', role: 'copy' },
{ label: 'Paste', role: 'paste' },
{ type: 'separator' },
{ label: 'Select All', role: 'selectAll' }
]
},
{
label: 'View',
submenu: [
{ label: 'Toggle DevTools', role: 'toggledevtools' }
]
},
{
label: 'Window',
submenu: [
{ label: 'Minimize', role: 'minimize' },
{ label: 'Zoom', submenu: [
{ label: 'Actual Size', role: 'zoomIn' },
{ label: 'Zoom Out', role: 'zoomOut' },
{ type: 'separator' },
{ label: 'Toggle Fullscreen', role: 'togglefullscreen' }
]}
]
},
{
label: 'Help',
submenu: [
{ label: 'Learn More', click: () => { shell.openExternal('https://electronjs.org'); } },
{ label: 'Documentation', click: () => { shell.openExternal('https://electronjs.org/docs'); } },
{ type: 'separator' },
{ label: 'Report Issues', click: () => { const url = 'https://github.com/electron/electron/issues/new'; shell.openExternal(url); } }
]
}
];
const menu = Menu.buildFromTemplate(template);
label
プロパティでメニューバーのラベルを、submenu
プロパティでサブメニューを定義しています。role
プロパティを使用して、標準メニュー項目 (例:undo
,redo
,minimize
) を定義することができます。click
プロパティを使用して、メニュー項目をクリックしたときの処理を定義することができます。
作成したメニューバーを設定する
const { app, BrowserWindow } = require('electron');
const menu = require('./menu'); // 作成したメニューバーをインポート
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
});
win.setMenu(menu); // 作成したメニューバーを設定
// ... 他の処理
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
- このサンプルコードはあくまでも一例です
Electron アプリケーションからメニューバーを削除 / 作成するその他の方法
上記で紹介した 2 つの方法以外にも、Electron アプリケーションからメニューバーを削除 / 作成する方法があります。
BrowserWindow.setMenuBarVisibility(false) を使用する
この方法は、BrowserWindow
インスタンスの setMenuBarVisibility
メソッドを使用して、メニューバーの表示 / 非表示を切り替える方法です。
const { app, BrowserWindow } = require('electron');
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
});
win.setMenuBarVisibility(false); // メニューバーを非表示
// ... 他の処理
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
setMenuBarVisibility(false)
を使用して、メニューバーを非表示にしています。
フレームレスウィンドウを使用する
Electron では、フレームレスウィンドウを作成することもできます。フレームレスウィンドウには、デフォルトでメニューバーが表示されません。
const { app, BrowserWindow } = require('electron');
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
frame: false, // フレームレスウィンドウ
});
// ... 他の処理
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
frame: false
オプションを使用して、フレームレスウィンドウを作成しています。- フレームレスウィンドウを作成する場合は、ウィンドウのタイトルバーやボタンを独自に実装する必要があります。
タイトルバーのカスタムメニューを使用する
Electron では、タイトルバーにカスタムメニューを作成することもできます。この方法では、メニューバーを完全に削除することはできませんが、メニューバーの外観や機能をある程度カスタマイズすることができます。
詳細は、Electron の公式ドキュメントを参照してください: https://electronjs.org/docs/latest/tutorial/window-customization
注意事項
上記で紹介した方法は、あくまでも Electron の基本的な機能を利用したものです。より高度なカスタマイズを行う場合は、Electron の API やライブラリを組み合わせる必要がある場合があります。
node.js electron