Skip to content

Commit cf1d1b3

Browse files
Copilotshannah
andauthored
Fix ImageViewer getCroppedImage() inconsistency after panning operations (#3943)
* Initial plan * Fix ImageViewer cropBox calculation to respect viewport bounds Co-authored-by: shannah <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: shannah <[email protected]>
1 parent 84e305a commit cf1d1b3

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

CodenameOne/src/com/codename1/components/ImageViewer.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,26 @@ private void updatePositions() {
503503
imageDrawHeight = prefH;
504504
imageX = prefX;
505505
imageY = prefY;
506-
cropBox.set(-imageY/(double)imageDrawHeight, (imageX + imageDrawWidth - getWidth())/(double)imageDrawWidth, (imageY + imageDrawHeight - getHeight())/(double)imageDrawHeight, -imageX/(double)imageDrawWidth);
506+
507+
// Apply the same constraints used in paint() method to ensure cropBox matches what's actually visible
508+
int constrainedImageX = imageX;
509+
int constrainedImageY = imageY;
510+
511+
if (imageDrawWidth > getInnerWidth()) {
512+
constrainedImageX = Math.max(
513+
Math.min(0, imageX),
514+
-imageDrawWidth + getInnerWidth()
515+
);
516+
}
517+
518+
if (imageDrawHeight > getInnerHeight()) {
519+
constrainedImageY = Math.max(
520+
Math.min(0, imageY),
521+
-imageDrawHeight + getInnerHeight()
522+
);
523+
}
524+
525+
cropBox.set(-constrainedImageY/(double)imageDrawHeight, (constrainedImageX + imageDrawWidth - getWidth())/(double)imageDrawWidth, (constrainedImageY + imageDrawHeight - getHeight())/(double)imageDrawHeight, -constrainedImageX/(double)imageDrawWidth);
507526
return;
508527
}
509528
int iW = image.getWidth();
@@ -529,7 +548,26 @@ private void updatePositions() {
529548

530549
imageY += (getInnerHeight() - imageDrawHeight)/2;
531550
}
532-
cropBox.set(-imageY/(double)imageDrawHeight, (imageX + imageDrawWidth - getWidth())/(double)imageDrawWidth, (imageY + imageDrawHeight - getHeight())/(double)imageDrawHeight, -imageX/(double)imageDrawWidth);
551+
552+
// Apply the same constraints used in paint() method to ensure cropBox matches what's actually visible
553+
int constrainedImageX = imageX;
554+
int constrainedImageY = imageY;
555+
556+
if (imageDrawWidth > getInnerWidth()) {
557+
constrainedImageX = Math.max(
558+
Math.min(0, imageX),
559+
-imageDrawWidth + getInnerWidth()
560+
);
561+
}
562+
563+
if (imageDrawHeight > getInnerHeight()) {
564+
constrainedImageY = Math.max(
565+
Math.min(0, imageY),
566+
-imageDrawHeight + getInnerHeight()
567+
);
568+
}
569+
570+
cropBox.set(-constrainedImageY/(double)imageDrawHeight, (constrainedImageX + imageDrawWidth - getWidth())/(double)imageDrawWidth, (constrainedImageY + imageDrawHeight - getHeight())/(double)imageDrawHeight, -constrainedImageX/(double)imageDrawWidth);
533571
}
534572

535573
/**

0 commit comments

Comments
 (0)