#Custom menu bar with minimize, maximize, close

1 messages · Page 1 of 1 (latest)

echo sable
#

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}
 />
weak condor
#

weird

#

does it log if you put the console.log inside the if?

snow glacier
#

use click event instead of mousedown

#

or mouseup

echo sable
#

Why though? What's the difference?

snow glacier
#

mousedown fired when you push you mouse button

#

at the same time you minimize and focus on the window

#

i cant say what happens before

echo sable
#

Ah. I guess that makes sense in a way... Gonna keep that in mind!