Get size/visibility of toolbar in Max?

Neil Baldwin's icon

Neil Baldwin

8月 22 2023 | 11:30 午前

I'm trying to determine the global (screen) location of the top-left of a patcher but it seems that you need to know the height/size of the toolbar otherwise the reported location from patcher.win.location is incorrect (can be confirmed by turning off the toolbars).

Does anyone know the proper way to compensate for this? I've tried searching for information regarding the toolbarvisible property but there seems to be no way of reading this or if there is there's no information regarding the size of the toolbar.

11OLSEN's icon

11OLSEN

8月 22 2023 | 10:38 午後

The thispatcher Max object can give this information

But I don't see a way to do it in js. this.patcher.window("getwindowrect") doesn't work.

This is a way to get the toolbarvisible attribute: this.patcher.getattr("toolbarvisible")

Roman Thilenius's icon

Roman Thilenius

8月 23 2023 | 5:45 午前

the toolbar size could change over runtime versions, so it makes sense that an external can do it but not js.
i had a similar issue with the menubars of the 4 different macos i use and "solved" it by always hiding it, then 0 0 is always 0 0.

11OLSEN's icon

11OLSEN

8月 23 2023 | 10:22 午前

Fortunately, we can script-create any object, send it a message and get the result before you can even say cheating.

function bang()
{
    var myObject = this.patcher.newdefault(0,0,"thispatcher");
    this.patcher.connect(myObject,0,this.box,0);
    myObject.message("window", "getwindowrect");
    this.patcher.remove(myObject);
}
function window()
{
    if (arguments[0] == "windowrect")
        post("windowrect:", arguments[1],arguments[2],arguments[3],arguments[4]);
}

Neil Baldwin's icon

Neil Baldwin

8月 23 2023 | 10:57 午前

Yeah, there's a few way to fake it. Something I'm doing at the moment is constantly sending the output from `mousestate` (global screen position) into my JSUI object (using polling mode). Inside the JSUI I'm using the difference between two points. Point A is the X/Y origin of where the pointer was when the user clicks inside the JSUI subtracted from the X/Y position of the JSUI window inside the Max window (`this.box.rect` information of the JSUI). Point B is the global position of the mouse (`mousestate`) when the user clicks the mouse button. The X/Y difference between these two gives you the correct offset to calculate the position of the click inside your JSUI, *inside* the Max window, *inside* your screen. It works but having to pipe the `mousestate` constantly into the JSUI is a bit icky.

Neil Baldwin's icon

Neil Baldwin

8月 23 2023 | 11:05 午前

That wasn't aimed at you @11olsen

11OLSEN's icon

11OLSEN

8月 23 2023 | 1:25 午後

This is confusing. What type of coordinates you want to get for the click?

Neil Baldwin's icon

Neil Baldwin

8月 23 2023 | 1:45 午後

What I'm trying to do is hide the mouse pointer when you click and drag an element inside a JSUI so when you release the mouse button it's position is restored to where you clicked via `pupdate(x,y)`. The difficulty arises because pupdate() uses screen-based coordinates and the window/rectangle parameters inside Max don't take into account the toolbar.

Neil Baldwin's icon

Neil Baldwin

8月 23 2023 | 1:59 午後

Here's a video of it in action using the `mousestate` method I described. You'll see the mouse pointer reappears over the node after I've stopped dragging it, even though in the real world the mouse pointer has likely gone way outside of the frame of the JSUI.

11OLSEN's icon

11OLSEN

8月 23 2023 | 3:45 午後

Ok, I get it now. If you use the patcher rect attr instead of the wind.location, your calculation should work without [mousestate].

function onclick(x,y,but,cmd,shift,capslock,option,ctrl)
{
    //box2screen
    post(this.patcher.getattr('rect')[0] + x + this.box.rect[0]);
    post(this.patcher.getattr('rect')[1] + y + this.box.rect[1]);
    post();
}

Neil Baldwin's icon

Neil Baldwin

8月 23 2023 | 4:00 午後

Thanks! I'll give that a try.

Neil Baldwin's icon

Neil Baldwin

8月 23 2023 | 4:10 午後

That's working perfectly! Thank you so much 🙏

11OLSEN's icon

11OLSEN

8月 23 2023 | 5:25 午後

You also need to have in mind that patcher scrolling is changing the position. And zooming may also affect the result.

Neil Baldwin's icon

Neil Baldwin

8月 23 2023 | 5:58 午後

Good point. It's a great start though!