0

I want to detect when a user maximizes the window in my Electron app. I know I can listen the the maximize event but I don't know how to use it. The docs are not very clear on the usage.

My current code -

main.js

const { app, BrowserWindow } = require('electron')

function createWindow () {
    const mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        frame: false,
        show: false
    });

    mainWindow.loadFile('index.html')
    mainWindow.removeMenu();
    mainWindow.once('ready-to-show', () => {
        mainWindow.show();
    });
    mainWindow.on('maximize',(e) =>{
        document.getElementById("topBar").style.display="none";
        //Does not work as we cannot access the DOM in this process
        console.log("Maximized") //Does not work too
    });
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

I know I need to use some logic in the renderer process but I don't know how.

If I do something like this inside a script tag

window.addEventListener("maximize", function(){
    //Do something
});

it does not work too.

Praneet Dixit
  • 1,393
  • 9
  • 25
  • 1
    I never heard of a `maximize` event... Where is that unclear documentation? Also, have a look [here](https://stackoverflow.com/questions/3371973/detect-browser-window-maximized-minimized-with-javascript). – Louys Patrice Bessette Jun 12 '21 at 18:12
  • Thanks. The solution in the link worked for me. By the way, we can find the `maximize` event in the [docs](https://www.electronjs.org/docs/api/browser-window#event-maximize) – Praneet Dixit Jun 12 '21 at 18:26

0 Answers0