Skip to content

Commit 5aeb924

Browse files
akoch-yattaHeikoKlare
authored andcommitted
[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 5aeb924

File tree

1 file changed

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

1 file changed

+30
-2
lines changed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.eclipse.swt.internal.DPIUtil.*;
2323
import org.eclipse.swt.internal.gdip.*;
2424
import org.eclipse.swt.internal.win32.*;
25+
import org.eclipse.swt.widgets.*;
2526

2627
/**
2728
* Instances of this class are graphics which have been prepared
@@ -819,7 +820,10 @@ public static Long win32_getHandle (Image image, int zoom) {
819820
image.init(newData, zoom);
820821
}
821822
} else if (image.imageDataProvider != null) {
822-
ElementAtZoom<ImageData> imageCandidate = DPIUtil.validateAndGetImageDataAtZoom (image.imageDataProvider, zoom);
823+
ElementAtZoom<ImageData> imageCandidate;
824+
try (StaticZoomUpdater unused = image.new StaticZoomUpdater(zoom)) {
825+
imageCandidate = DPIUtil.validateAndGetImageDataAtZoom (image.imageDataProvider, zoom);
826+
}
823827
ImageData resizedData = DPIUtil.scaleImageData (image.device, imageCandidate.element(), zoom, imageCandidate.zoom());
824828
ImageData newData = image.adaptImageDataIfDisabledOrGray(resizedData);
825829
image.init(newData, zoom);
@@ -1443,7 +1447,10 @@ public ImageData getImageData (int zoom) {
14431447
if (zoom == currentZoom) {
14441448
return getImageDataAtCurrentZoom();
14451449
} else if (imageDataProvider != null) {
1446-
ElementAtZoom<ImageData> data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, zoom);
1450+
ElementAtZoom<ImageData> data;
1451+
try (StaticZoomUpdater unused = new StaticZoomUpdater(zoom)) {
1452+
data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, zoom);
1453+
}
14471454
return DPIUtil.scaleImageData (device, data.element(), zoom, data.zoom());
14481455
} else if (imageFileNameProvider != null) {
14491456
ElementAtZoom<String> fileName = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, zoom);
@@ -2397,4 +2404,25 @@ public static Image win32_new(Device device, int type, long handle) {
23972404
image.setHandleForZoomLevel(handle, image.getZoom());
23982405
return image;
23992406
}
2407+
2408+
// This class is only used for a workaround and will be removed again
2409+
private class StaticZoomUpdater implements AutoCloseable {
2410+
private final boolean updateStaticZoom;
2411+
private final int currentNativeDeviceZoom;
2412+
2413+
private StaticZoomUpdater(int targetZoom) {
2414+
this.currentNativeDeviceZoom = DPIUtil.getNativeDeviceZoom();
2415+
this.updateStaticZoom = this.currentNativeDeviceZoom != targetZoom && device instanceof Display display && display.isRescalingAtRuntime();
2416+
if (updateStaticZoom) {
2417+
DPIUtil.setDeviceZoom(targetZoom);
2418+
}
2419+
}
2420+
2421+
@Override
2422+
public void close() {
2423+
if (updateStaticZoom) {
2424+
DPIUtil.setDeviceZoom(currentNativeDeviceZoom);
2425+
}
2426+
}
2427+
}
24002428
}

0 commit comments

Comments
 (0)