Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if size of electron window changed

Tags:

electron

I wanna have a listener ( or something ) to check if the size of the window changes, do something ( like re-rendering entire view page ).

How should I do this?

like image 862
meshkati Avatar asked Aug 28 '17 14:08

meshkati


People also ask

How do electrons maximize a window?

Call mainWindow. maximize() to maximize the window after you create it. Save this answer.

How do you make an electron window full screen?

To make an Electron app run in full-screen mode when it's started, pass the following configuration option when creating the BrowserWindow instance: mainWindow = new BrowserWindow({fullscreen: true});


2 Answers

You can use the resize event like so:

window.on('resize', function () {
    var size   = window.getSize();
    var width  = size[0];
    var height = size[1];
    console.log("width: " + width);
    console.log("height: " + height);
});

Docs here

like image 183
Joshua Avatar answered Oct 10 '22 13:10

Joshua


To clarify the above answer. You have to set the resize event on the main window you created. Further, if you want to access the width or height inside another file, you can emit another event and pass the width or height as a parameter. Here is a simple example of what I have done.

mainWindow.on("resize", function () {
    var size = mainWindow.getSize();
    var width = size[0];
    var height = size[1];
    mainWindow.webContents.send("resized", height);
    console.log(size);
    console.log("width: " + width);
    console.log("height: " + height);
  });
like image 35
Sanan Ali Avatar answered Oct 10 '22 11:10

Sanan Ali