ComponentBoundsConstrainer documentation confusing

Hi,

The documentation for the ComponentBoundsConstrainer class says the following:
“A class that imposes restrictions on a Component’s size or position.
[…]
The base class can impose some basic size and position limits, but you can also subclass this for custom uses.”

The most basic position constrain I can think of is that you can confine a Component to a rectangle. However, I can’t find any trace of this functionality in the description of the methods of the ComponentBoundsConstrainer class.

Did I miss something? If indeed this functionality is not included, is there any example around for how to implement this simple constraint? And, does it really make sense to use this class (or subclass thereof)? Wouldn’t it be simpler to adjust positions by hand in mouseDrag()?

Best regards,
Fritz

It’s very confusing. I waste a good 15 minutes every time I come back to this trying to remember how it works. It would benefit from a rewrite with some examples I think.

It looks at the parent components size … and uses that plus the ‘offscreen’ settings to confine the component!

The secret is here I think:

Sets the amount by which the component is allowed to go off-screen.
The values indicate how many pixels must remain on-screen when dragged off one of its parent’s edges, so e.g. if minimumWhenOffTheTop is set to 10, then when the component goes off the top of the screen, its y-position will be clipped so that there are always at least 10 pixels on-screen. In other words, the lowest y-position it can take would be (10 - the component’s height).
If you pass 0 or less for one of these amounts, the component is allowed to move beyond that edge completely, with no restrictions at all.

1 Like

Thanks a lot for the explanation! What I need is to constrain the movement of an object to an arbitrary rectangle inside my GUI, so it’s not related really to the window bounds (from the JUCE Demo example I got the impression “on screen” actually means “inside the application window”). Is that possible by creating a subclass of ComponentBoundsConstrainer? Or is there a better way to do that?

Actually, I ended up just passing NULL for the constrainer parameter of ComponentDragger::dragComponent(), and do the position adjustment just after the call to dragComponent. That seems easier (having straightforward access to all parameters necessary for the bounds calculation) and more flexible (I can for example take care of interactions between different components if necessary). Looking at the code of the ComponentDragger class, it seems to me that the result of doing this is the same as implementing a custom ComponentBoundsConstrainer class, except that setBounds() of the dragged object gets called twice if the position needs to be constrained. Is that a bad thing? I didn’t notice any flicker or anything so far.

If you call setBounds twice during the same mouseDrag callback you won’t be seeing any flicking, just wasting a handleful of CPU cycles.

1 Like