Problems with ComponentBoundsConstrainer and fixed aspect ratio

I’m trying to limit a resizable window to a fixed aspect ratio. I tried modifying the DemoRunner’s WindowsDemo::showDocumentWindow to use the simple BorderBoundsConstrainer class from here: Resizing a window with native titlebar and fixed aspect ratio

I added this to the end of showDocumentWindow

BorderBoundsConstrainer* constrainer = new BorderBoundsConstrainer();
constrainer->SetBorder(native ? dw->getPeer()->getFrameSize() : dw->getContentComponentBorder());

When dragging the corner resizer, it works reasonably well for the native title bar case, but behaves very strangely (window position jumping around), when using the non-native titlebar.
Is there some other way I should be doing this?


Turns out the constrainer doesn’t work so well with the native titlebar either. It seems to ignore setMinimumOnscreenAmounts.
As an experiment, I added constrainer->setMinimumOnscreenAmounts(0xffffff, 0xffffff, 0xffffff, 0xffffff); to the above example. The native case appears to ignore those minimumOnscreenAmounts, while the non-native case works properly.
Seems buggy!

Any suggestions?


What platform is this on? Do you see the same behaviour using the standard ComponentBoundsConstrainer in JUCE?

I’m on macOS 10.14.3 (18D109). JUCE latest 5.4.3.

Using the regular ComponentBoundsConstrainer is much less glitchy, but still occasionally bounces around a bit when dragging the corner resizer. Also, the setMinimumOnscreenAmounts still has no effect when using native titlebar.

Here’s my modified DemoRunner’s WindowsDemo.h, with modifications in showDocumentWindow starting at line 322:

Here are two videos using ComponentBoundsConstrainer:

Note some glitchiness when resizing:

Note the native title bar minimumOnscreenAmounts are not working, while non-native title bar is staying on-screen properly:

And here’s a video of the resizing glitches using the BorderBoundsConstrainer:

Note the glitches when resizing the non-native title bar window:

I need to preserve the aspect ratio of the window content, not including the title bar. The obvious application is to put a photo in the content, and keep it scaled to fit the window entirely. I want the window content size to match the aspect ratio of the photo. Please advise how best to accomplish this.


Any thoughts on this?

I don’t see any resizing glitches when using the ComponentBoundsConstrainer, but I can’t comment on the BorderBoundsConstrainer class that you are using.

The minimum onscreen amounts will take effect when you release the mouse on macOS when using the native title bar as this is when we get the callback from the OS to constrain the bounds. If you want to enforce an aspect ratio for the window content without the title bar then you can use ComponentPeer::getFrameSize() to get the native title bar size and subtract that from the content size to work out the aspect ratio that you need.

Yes, using the peer’s getFrameSize() works ok for the native titlebar window, as I showed in my BorderBoundsConstrainer modification to the DemoRunner’s WindowsDemo.

But for a non-native titlebar window, I haven’t found a way to constrain it without it being very glitchy as I showed in the video. . What do you suggest? Do you have a working example of a fixed aspect ratio constrained non-native titlebar window which excludes the titlebar border from the ratio?

As for the regular ComponentBoundsConstrainer, you can see see a minor window-bouncing glitch at around the 2 second mark in my example video: : .

Thanks for the explanation of the minimum onscreen enforcement occurring on mouse-release for native titlebar window.