Monitoring whether a window size changes in js

Lee's icon

Hi, is there a callback mechanism for this so that it's possible to tell when windows have been resized, closes etc?

thx Lee

11OLSEN's icon

you need to create a repeating task.

// 11oLsen, report changed window size

outlets = 2;
var    nowsize;
var lastsize;

var pollTsk = new Task(getsize, this);

function on()
{
    lastsize = -1;

    pollTsk.interval = 10; //set interval of polling in ms 

    pollTsk.repeat();
}

function off()
{
    pollTsk.cancel();
}

function getsize()
{    

    nowsize = this.patcher.wind.size;
    if (nowsize[0] != lastsize[0] || nowsize[1] != lastsize[1]) {
        //post(lastsize);
        //post(nowsize);
        outlet(1,nowsize[1]);
        outlet(0,nowsize[0]);
        lastsize = nowsize;

    }

}

btw: there's a windowwatcher external in the sdk ready to compile, don't ask me why they don't add this to the standard object set..

Lee's icon

excellent, thanks for the help :)

Sapo's icon

Thanks a lot 11olsen !

I'm digging up this thread, I'm beginning with javascript and I'm not sure how I can get the all the coords of the window instead of the size only... Can you help me please ?

11OLSEN's icon

You use wind.rect instead of size. I can post an example when I'm home..

11OLSEN's icon

I meant wind.location.

window_loc_watch.js
application/x-javascript 0.61 KB

Sapo's icon

Thanks a lot !
I have 2 questions if you don't mind answering them. It might sound stupid but this will probably help me understand better.


var pollTsk = new Task(getloc, this); 
// Where I can find documentation to know that's getloc I need to put here?..
function msg_int(s)
// What does this s correspond to ?
Sapo's icon

Oh ! I actually have 3 questions :)

Can you explain me why the values I get using "window getsize" message to a thispatcher object are different ?

tyler mazaika's icon

Bookmark this page: https://docs.cycling74.com/max8/vignettes/javascriptinmax

getloc is a reference to the function that's defined later in the file. Look at the Task object link in the above and it's examples.

In msg_int(s), the s is just the local variable name for the number (an integer). In this case it's getting either 1 or 0 from the toggle box outside the [js window_loc_watch.js]. FWIW I probably would've named that variable "enable_polling" or "polling_bool" or something like that to be more descriptive.

Sapo's icon

Thanks a lot Tyler, very clear, understood !

Do you know why I don't have the same values using "window getsize" message to [thispatcher] ?

11OLSEN's icon

Thanks Tyler for helping out.

11OLSEN's icon

"wind.location" includes the toolbars "window getsize" does not and reports x/y point of the bottom-right corner instead of height/width. You could probably build the same js around "this.patcher.getattr('rect')" which gives you the same coords like [thispatcher] but value 3 and 4 are height/width. You see there's a little inconsistency with the different methods to get window location in Max.

tyler mazaika's icon

@ 11Olsen thanks for your sample code 10 years ago (!) and your recent update :)

Sapo's icon

Thanks guys ! You are real gifts for this community ;)