I'm trying to get a custom menu bar with minimize, maximize, and close buttons to work (Windows 10, Electron 23.1.3 with React). I've got the whole ipc communication set up, or so I think. There are console.logs on both sides, which indicate that both ipcRenderer.send() and ipcMain.on() are actually called. Of the latter however, only close() works, minimize() and maximize() do nothing. As a little test, I've set up a globalShortcut that triggers minimize() just fine. What am I overlooking?
This is electron.ts:
import { app, BrowserWindow, Tray, ipcMain, globalShortcut } from 'electron';
import * as path from 'path';
let mainWindow: BrowserWindow | null = null;
function createWindow() {
mainWindow = new BrowserWindow({
// width, height, etc.
minimizable: true,
maximizable: true,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
devTools: true,
nodeIntegration: true,
contextIsolation: true,
},
autoHideMenuBar: true,
frame: false,
});
mainWindow.loadFile(path.join(__dirname, "index.html"));
// This triggers minimize()
globalShortcut.register('m', () => {
console.log('Global shortcut "m" pressed, minimizing window');
if (mainWindow) {
mainWindow.minimize();
}
});
// Logging happens, but minimize() is not triggered; maximize looks the same.
ipcMain.on("minimizeWindow", () => {
console.log("minimizeWindow event received");
if (mainWindow) {
mainWindow.minimize();
}
});
// This works...
ipcMain.on("closeWindow", () => {
if (mainWindow) {
mainWindow.close();
}
});
}
app.on("ready", createWindow);
//...
ipcRenderer.send() look like this:
const handleMinimizeClick = () => {
console.log("handleMinimizeClick called");
window.electron.ipcRenderer.send("minimizeWindow");
};
//...
<img
src={...}
className="..."
onMouseDown={handleMinimizeClick}
/>