Skip to content

Commit 9d9bee6

Browse files
akoch-yattaHeikoKlare
authored andcommitted
Reset the cached image width/height when the currentZoom of an image has changed
1 parent 332eaf8 commit 9d9bee6

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ boolean refreshImageForZoom () {
745745
init ();
746746
refreshed = true;
747747
}
748-
currentDeviceZoom = deviceZoomLevel;
748+
setCurrentDeviceZoom(deviceZoomLevel);
749749
}
750750
} else if (imageDataProvider != null) {
751751
if (deviceZoomLevel != currentDeviceZoom) {
@@ -756,7 +756,7 @@ boolean refreshImageForZoom () {
756756
init(resizedData);
757757
init();
758758
refreshed = true;
759-
currentDeviceZoom = deviceZoomLevel;
759+
setCurrentDeviceZoom(deviceZoomLevel);
760760
}
761761
} else {
762762
if (deviceZoomLevel != currentDeviceZoom) {
@@ -766,7 +766,7 @@ boolean refreshImageForZoom () {
766766
init(resizedData);
767767
init();
768768
refreshed = true;
769-
currentDeviceZoom = deviceZoomLevel;
769+
setCurrentDeviceZoom(deviceZoomLevel);
770770
}
771771
}
772772
return refreshed;
@@ -2200,6 +2200,15 @@ public void setBackground(Color color) {
22002200
device.internal_dispose_GC(hDC, null);
22012201
}
22022202

2203+
private void setCurrentDeviceZoom(int newZoomFactor) {
2204+
if (this.currentDeviceZoom != newZoomFactor) {
2205+
this.currentDeviceZoom = newZoomFactor;
2206+
// width and height are tied to the current device zoom
2207+
// they must be reset the the zoom factor changes
2208+
width = height = -1;
2209+
}
2210+
}
2211+
22032212
/**
22042213
* Returns a string containing a concise, human-readable
22052214
* description of the receiver.

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,4 +1177,23 @@ public void test_bug566545_efficientGrayscaleImage() {
11771177
outImageDirect.dispose();
11781178
}
11791179

1180+
@Test
1181+
public void test_updateWidthHeightAfterDPIChange() {
1182+
int deviceZoom = DPIUtil.getDeviceZoom();
1183+
try {
1184+
Rectangle imageSize = new Rectangle(0, 0, 16, 16);
1185+
Image baseImage = new Image(display, imageSize.width, imageSize.height);
1186+
GC gc = new GC(display);
1187+
gc.drawImage(baseImage, 10, 10);
1188+
assertEquals("Base image size differs unexpectedly", imageSize, baseImage.getBounds());
1189+
1190+
DPIUtil.setDeviceZoom(deviceZoom * 2);
1191+
gc.drawImage(baseImage, 10, 10);
1192+
assertEquals("Image size at 100% must always stay the same despite the zoom factor", imageSize, baseImage.getBounds());
1193+
gc.dispose();
1194+
baseImage.dispose();
1195+
} finally {
1196+
DPIUtil.setDeviceZoom(deviceZoom);
1197+
}
1198+
}
11801199
}

0 commit comments

Comments
 (0)