Fix ImageViewer overlapping images during zoom and pan operations #3944
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The ImageViewer component exhibited overlapping images when users zoomed in and panned to the right side of large images. This issue was particularly problematic for chart viewing applications where precise zoom/pan functionality is critical.
The suggested workaround
viewer.setImageInitialPosition(ImageViewer.IMAGE_FILL)did not fully resolve the issue, especially with large images.Root Cause
The issue stemmed from inconsistent coordinate constraint logic between the
updatePositions()andpaint()methods:updatePositions()used relative coordinate constraints:Math.max(Math.min(0, imageX), -imageDrawWidth + getInnerWidth())paint()used absolute coordinate constraints:Math.max(Math.min(getX(), getX() + imageX), getX() - imageDrawWidth + getInnerWidth())This inconsistency caused:
Solution
Aligned the coordinate constraint logic in the
paint()method to matchupdatePositions():The fix ensures both methods use identical constraint calculations, with
paint()properly adding the component offset after constraining the relative position.Benefits
IMAGE_FITandIMAGE_FILLmodesgetCroppedImage()functionalityTesting
Added comprehensive unit test
ImageViewerCoordinateTest.javathat verifies:The fix has been mathematically verified and passes all security checks with no new vulnerabilities introduced.
Fixes the issue described in https://stackoverflow.com/questions/74330133/solving-overlapping-images-when-zooming-in-codenameone
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
https://api.github.com/repos/codenameone/codenameone%2FCodenameOne/languages/home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js(http block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.