Skip to content

Commit 1237105

Browse files
committed
[win32] workaround for ImageDataProvider implementation
This commit adds a workaround for ImageDataProvider implementations that instantiate new Images inside the getImageData(zoom) implementation. This image can be instantiated with the wrong zoom in a multi zoom setting as Images still relay on the static zoom value in DPIUtil. This workaround should be replaced by a proper solution. Contributes to #62 and #131
1 parent ce2795a commit 1237105

File tree

1 file changed

+29
-2
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+29
-2
lines changed

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,10 @@ public static Long win32_getHandle (Image image, int zoom) {
819819
image.init(newData, zoom);
820820
}
821821
} else if (image.imageDataProvider != null) {
822-
ElementAtZoom<ImageData> imageCandidate = DPIUtil.validateAndGetImageDataAtZoom (image.imageDataProvider, zoom);
822+
ElementAtZoom<ImageData> imageCandidate;
823+
try (StaticZoomUpdater ignored = new StaticZoomUpdater(zoom)) {
824+
imageCandidate = DPIUtil.validateAndGetImageDataAtZoom (image.imageDataProvider, zoom);
825+
}
823826
ImageData resizedData = DPIUtil.scaleImageData (image.device, imageCandidate.element(), zoom, imageCandidate.zoom());
824827
ImageData newData = image.adaptImageDataIfDisabledOrGray(resizedData);
825828
image.init(newData, zoom);
@@ -1443,7 +1446,10 @@ public ImageData getImageData (int zoom) {
14431446
if (zoom == currentZoom) {
14441447
return getImageDataAtCurrentZoom();
14451448
} else if (imageDataProvider != null) {
1446-
ElementAtZoom<ImageData> data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, zoom);
1449+
ElementAtZoom<ImageData> data;
1450+
try (StaticZoomUpdater ignored = new StaticZoomUpdater(zoom)) {
1451+
data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, zoom);
1452+
}
14471453
return DPIUtil.scaleImageData (device, data.element(), zoom, data.zoom());
14481454
} else if (imageFileNameProvider != null) {
14491455
ElementAtZoom<String> fileName = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, zoom);
@@ -2397,4 +2403,25 @@ public static Image win32_new(Device device, int type, long handle) {
23972403
image.setHandleForZoomLevel(handle, image.getZoom());
23982404
return image;
23992405
}
2406+
2407+
// This class is only used for a workaround and will be removed again
2408+
private static class StaticZoomUpdater implements AutoCloseable {
2409+
int currentNativeDeviceZoom;
2410+
int targetZoom;
2411+
2412+
private StaticZoomUpdater(int targetZoom) {
2413+
this.currentNativeDeviceZoom = DPIUtil.getNativeDeviceZoom();
2414+
this.targetZoom = targetZoom;
2415+
if (currentNativeDeviceZoom != targetZoom) {
2416+
DPIUtil.setDeviceZoom(targetZoom);
2417+
}
2418+
}
2419+
2420+
@Override
2421+
public void close() {
2422+
if (currentNativeDeviceZoom != targetZoom) {
2423+
DPIUtil.setDeviceZoom(currentNativeDeviceZoom);
2424+
}
2425+
}
2426+
}
24002427
}

0 commit comments

Comments
 (0)