Skip to content

Commit

Permalink
[win32] workaround for ImageDataProvider implementation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
akoch-yatta authored and HeikoKlare committed Nov 5, 2024
1 parent ce2795a commit 5aeb924
Showing 1 changed file with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.swt.internal.DPIUtil.*;
import org.eclipse.swt.internal.gdip.*;
import org.eclipse.swt.internal.win32.*;
import org.eclipse.swt.widgets.*;

/**
* Instances of this class are graphics which have been prepared
Expand Down Expand Up @@ -819,7 +820,10 @@ public static Long win32_getHandle (Image image, int zoom) {
image.init(newData, zoom);
}
} else if (image.imageDataProvider != null) {
ElementAtZoom<ImageData> imageCandidate = DPIUtil.validateAndGetImageDataAtZoom (image.imageDataProvider, zoom);
ElementAtZoom<ImageData> imageCandidate;
try (StaticZoomUpdater unused = image.new StaticZoomUpdater(zoom)) {
imageCandidate = DPIUtil.validateAndGetImageDataAtZoom (image.imageDataProvider, zoom);
}
ImageData resizedData = DPIUtil.scaleImageData (image.device, imageCandidate.element(), zoom, imageCandidate.zoom());
ImageData newData = image.adaptImageDataIfDisabledOrGray(resizedData);
image.init(newData, zoom);
Expand Down Expand Up @@ -1443,7 +1447,10 @@ public ImageData getImageData (int zoom) {
if (zoom == currentZoom) {
return getImageDataAtCurrentZoom();
} else if (imageDataProvider != null) {
ElementAtZoom<ImageData> data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, zoom);
ElementAtZoom<ImageData> data;
try (StaticZoomUpdater unused = new StaticZoomUpdater(zoom)) {
data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, zoom);
}
return DPIUtil.scaleImageData (device, data.element(), zoom, data.zoom());
} else if (imageFileNameProvider != null) {
ElementAtZoom<String> fileName = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, zoom);
Expand Down Expand Up @@ -2397,4 +2404,25 @@ public static Image win32_new(Device device, int type, long handle) {
image.setHandleForZoomLevel(handle, image.getZoom());
return image;
}

// This class is only used for a workaround and will be removed again
private class StaticZoomUpdater implements AutoCloseable {
private final boolean updateStaticZoom;
private final int currentNativeDeviceZoom;

private StaticZoomUpdater(int targetZoom) {
this.currentNativeDeviceZoom = DPIUtil.getNativeDeviceZoom();
this.updateStaticZoom = this.currentNativeDeviceZoom != targetZoom && device instanceof Display display && display.isRescalingAtRuntime();
if (updateStaticZoom) {
DPIUtil.setDeviceZoom(targetZoom);
}
}

@Override
public void close() {
if (updateStaticZoom) {
DPIUtil.setDeviceZoom(currentNativeDeviceZoom);
}
}
}
}

0 comments on commit 5aeb924

Please sign in to comment.