Problems with ComponentBoundsConstrainer - resizing the plugin window overrides the constraints

I’m using a custom ComponentBoundsConstrainer subclass to handle my resizable plugin. It makes sure that the main component always has a fixed aspect ratio and then adds some extra constant height for a preset menu placed above the content. My checkBounds implementation looks like that.

void MyConstrainer::checkBounds (juce::Rectangle<int>& bounds, const juce::Rectangle<int>&, const juce::Rectangle<int>&, bool, bool, bool, bool)
{
    auto contentHeight = juce::jmax (contentMinHeight, bounds.getHeight() - presetManagerComponentHeight);
    auto contentWidth  = juce::jmax (contentMinWitdh, juce::roundToInt (contentHeight * contentAspectRatio));

    bounds.setSize (contentWidth, contentHeight + presetManagerComponentHeight);
}

I set it all up like

setResizable (true, true);
setConstrainer (&myConstrainer);

In the editor constructor. This works as expected when using the resizer. However, it’s still possible to resize the plugin window in width/heigtht individually and override the constraints this way, which leads to ugly looking misplaced sub components etc. This can be reproduced under e.g. Cubase or Reaper.

I see that the first argument passed to the setResizable call is allowHostToResize. Setting this to false does not help either, I’m still able to resize the window in any direction and override the constrainer, but it makes the resizer disappear :face_with_raised_eyebrow:

I get the feeling that I’m misunderstanding something here. Is there anything totally wrong with my approach?

Okay, found it. If anyone else comes across the same problem, my misunderstanding was that I expected the bounds check to be executed on each resize. However, with the code above, checkBounds is only called when the resizer component was used to trigger the resize operation. In order to let it constrain a resize operation coming from the plugin window, I added this line as the first call in my editors resize function:

myConstrainer.checkComponentBounds (this);

1 Like