How to clip UI after setting GUI.matrix to custom Matrix4x4 in editor window?

I am working on a node-based editor and want to allow users to zoom in/out. I decided to modify GUI.matrix to let Unity take care of the scaling. The math is straight forward, it is easy to define a custom Matrix4x4 to render the scaled UI. The scaling works correctly.

But I have trouble making sure the scaled UI still fits inside the original bounding rectangle. It seems the bounding rectangle also get scaled! I tried using the original rectangle, scaled, inverse-scaled in the GUI.BeginGroup, GUI.BeginArea, GUI.BeginClip before resetting GUI.matrix. They all fail. It seems Unity recalculate the clipping rectangle when it render scaled UI.

Any suggestions? Or should I use a different approach? Thanks.

To prevent the parent scope’s clipping rect to affect your drawing, you can call GUI.EndClip(); to forcibly end it, then do your matrix and drawing stuff then call GUI.BeginClip(default); right after it to ensure the parent scopes’ GUI.EndClip() doesn’t send messages to the console. No idea how safe that is though but it works in my case.