Centre a component in a Viewport


Hi, I used to centre a component in a Viewport by passing setViewPosition() negative x values but now you have changed

contentComp->setTopLeftPosition (-xPixelsOffset, -yPixelsOffset);


contentComp->setTopLeftPosition (jmax (jmin (0, contentHolder.getWidth() - contentComp->getWidth()), jmin (0, -xPixelsOffset)), jmax (jmin (0, contentHolder.getHeight() - contentComp->getHeight()), jmin (0, -yPixelsOffset)));

the jmins don’t allow for this. Was doing this a bad idea? If so what is the recommended way to achieve this now?

Cheers, Dave.


You can still position the viewed component directly with setBounds(), but the viewport doesn’t expect you to do this, so it’ll snap it back to the origin when you touch a scrollbar or resize. To handle centring correctly, it’d require the viewport itself to understand and control the logic involved.