Skip to content

Commit

Permalink
Store nativeDeviceZoom in widgets instead of zoom for win32
Browse files Browse the repository at this point in the history
This contribution is to provide the nativeDeviceZoom to all the widgets
so that it can be used later on for e.g. font scaling and more.
Currently, the nativeDeviceZoom is only available via the shell, which
is not sufficient later when there is no shell available, e.g. GC.

Contributes to eclipse-platform#62 and eclipse-platform#127
  • Loading branch information
amartya4256 committed May 15, 2024
1 parent d3df796 commit 300f089
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ public void drawImage (Image image, int srcX, int srcY, int srcWidth, int srcHei

void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
/* Refresh Image as per zoom level, if required. */
srcImage.handleDPIChange(DPIUtil.getDeviceZoom());
srcImage.handleDPIChange(DPIUtil.getNativeDeviceZoom());
if (data.gdipGraphics != 0) {
//TODO - cache bitmap
long [] gdipImage = srcImage.createGdipImage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public final class Image extends Resource implements Drawable {
*/
Image (Device device) {
super(device);
currentDeviceZoom = DPIUtil.getDeviceZoom ();
currentDeviceZoom = DPIUtil.getNativeDeviceZoom();
}

/**
Expand Down Expand Up @@ -201,7 +201,7 @@ public final class Image extends Resource implements Drawable {
*/
public Image(Device device, int width, int height) {
super(device);
currentDeviceZoom = DPIUtil.getDeviceZoom ();
currentDeviceZoom = DPIUtil.getNativeDeviceZoom();
width = DPIUtil.autoScaleUp (width);
height = DPIUtil.autoScaleUp (height);
init(width, height);
Expand Down Expand Up @@ -254,7 +254,7 @@ public Image(Device device, Image srcImage, int flag) {
this.imageDataProvider = srcImage.imageDataProvider;
this.imageFileNameProvider = srcImage.imageFileNameProvider;
this.styleFlag = srcImage.styleFlag | flag;
this.currentDeviceZoom = srcImage.currentDeviceZoom;
currentDeviceZoom = srcImage.currentDeviceZoom;
this.dataAtBaseZoom = srcImage.dataAtBaseZoom;
switch (flag) {
case SWT.IMAGE_COPY: {
Expand Down Expand Up @@ -293,7 +293,7 @@ public Image(Device device, Image srcImage, int flag) {
break;
}
case SWT.IMAGE_DISABLE: {
ImageData data = srcImage.getImageData(srcImage.currentDeviceZoom);
ImageData data = srcImage.getImageData(srcImage.getZoom());
PaletteData palette = data.palette;
RGB[] rgbs = new RGB[3];
rgbs[0] = device.getSystemColor(SWT.COLOR_BLACK).getRGB();
Expand Down Expand Up @@ -352,7 +352,7 @@ public Image(Device device, Image srcImage, int flag) {
break;
}
case SWT.IMAGE_GRAY: {
ImageData data = srcImage.getImageData(srcImage.currentDeviceZoom);
ImageData data = srcImage.getImageData(srcImage.getZoom());
PaletteData palette = data.palette;
ImageData newData = data;
if (!palette.isDirect) {
Expand Down Expand Up @@ -459,7 +459,7 @@ public Image(Device device, Image srcImage, int flag) {
public Image(Device device, Rectangle bounds) {
super(device);
if (bounds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
currentDeviceZoom = DPIUtil.getDeviceZoom ();
currentDeviceZoom = DPIUtil.getNativeDeviceZoom();
bounds = DPIUtil.autoScaleUp (bounds);
init(bounds.width, bounds.height);
init();
Expand Down Expand Up @@ -491,7 +491,7 @@ public Image(Device device, Rectangle bounds) {
public Image(Device device, ImageData data) {
super(device);
if (data == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
currentDeviceZoom = DPIUtil.getDeviceZoom ();
currentDeviceZoom = DPIUtil.getNativeDeviceZoom();
this.dataAtBaseZoom = new ElementAtZoom<>(data, 100);
data = DPIUtil.autoScaleUp(device, this.dataAtBaseZoom);
init(data);
Expand Down Expand Up @@ -535,7 +535,7 @@ public Image(Device device, ImageData source, ImageData mask) {
if (source.width != mask.width || source.height != mask.height) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
currentDeviceZoom = DPIUtil.getDeviceZoom ();
currentDeviceZoom = DPIUtil.getNativeDeviceZoom();
this.dataAtBaseZoom = new ElementAtZoom<>(applyMask(source, ImageData.convertMask(mask)), 100);
source = DPIUtil.autoScaleUp(device, source);
mask = DPIUtil.autoScaleUp(device, mask);
Expand Down Expand Up @@ -599,7 +599,7 @@ public Image(Device device, ImageData source, ImageData mask) {
*/
public Image (Device device, InputStream stream) {
super(device);
currentDeviceZoom = DPIUtil.getDeviceZoom ();
currentDeviceZoom = DPIUtil.getNativeDeviceZoom();
this.dataAtBaseZoom = new ElementAtZoom<>(new ImageData (stream), 100);
ImageData data = DPIUtil.autoScaleUp(device, this.dataAtBaseZoom);
init(data);
Expand Down Expand Up @@ -641,7 +641,7 @@ public Image (Device device, InputStream stream) {
public Image (Device device, String filename) {
super(device);
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
currentDeviceZoom = DPIUtil.getDeviceZoom ();
currentDeviceZoom = DPIUtil.getNativeDeviceZoom();
this.dataAtBaseZoom = new ElementAtZoom<>(new ImageData (filename), 100);
ImageData data = DPIUtil.autoScaleUp(device, this.dataAtBaseZoom);
init(data);
Expand Down Expand Up @@ -680,9 +680,9 @@ public Image (Device device, String filename) {
public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
super(device);
this.imageFileNameProvider = imageFileNameProvider;
currentDeviceZoom = DPIUtil.getDeviceZoom ();
ElementAtZoom<String> fileName = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, currentDeviceZoom);
if (fileName.zoom() == currentDeviceZoom) {
currentDeviceZoom = DPIUtil.getNativeDeviceZoom();
ElementAtZoom<String> fileName = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, getZoom());
if (fileName.zoom() == getZoom()) {
initNative (fileName.element());
if (this.handle == 0) init(new ImageData (fileName.element()));
} else {
Expand Down Expand Up @@ -724,8 +724,8 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
public Image(Device device, ImageDataProvider imageDataProvider) {
super(device);
this.imageDataProvider = imageDataProvider;
currentDeviceZoom = DPIUtil.getDeviceZoom ();
ElementAtZoom<ImageData> data = DPIUtil.validateAndGetImageDataAtZoom(imageDataProvider, currentDeviceZoom);
currentDeviceZoom = DPIUtil.getNativeDeviceZoom();
ElementAtZoom<ImageData> data = DPIUtil.validateAndGetImageDataAtZoom(imageDataProvider, getZoom());
ImageData resizedData = DPIUtil.autoScaleImageData(device, data.element(), data.zoom());
init (resizedData);
init();
Expand All @@ -738,10 +738,11 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
*
* @return true if image is refreshed
*/
boolean handleDPIChange (int deviceZoom) {
boolean handleDPIChange (int nativeZoom) {
int deviceZoom = DPIUtil.getZoomForAutoscaleProperty(nativeZoom);
boolean refreshed = false;
if (imageFileNameProvider != null) {
if (deviceZoom != currentDeviceZoom) {
if (deviceZoom != getZoom()) {
ElementAtZoom<String> filename = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, deviceZoom);
if (filename.zoom() == deviceZoom) {
/* Release current native resources */
Expand All @@ -758,27 +759,27 @@ boolean handleDPIChange (int deviceZoom) {
init ();
refreshed = true;
}
setCurrentDeviceZoom(deviceZoom);
setCurrentDeviceZoom(nativeZoom);
}
} else if (imageDataProvider != null) {
if (deviceZoom != currentDeviceZoom) {
if (deviceZoom != getZoom()) {
ElementAtZoom<ImageData> data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, deviceZoom);
/* Release current native resources */
destroy ();
ImageData resizedData = DPIUtil.autoScaleImageData (device, data.element(), data.zoom());
init(resizedData);
init();
refreshed = true;
setCurrentDeviceZoom(deviceZoom);
setCurrentDeviceZoom(nativeZoom);
}
} else if (this.dataAtBaseZoom != null) { // Resizing is only possible with a cached base image
if (deviceZoom != currentDeviceZoom) {
if (deviceZoom != getZoom()) {
ImageData resizedData = getImageData(deviceZoom);
destroy ();
init(resizedData);
init();
refreshed = true;
setCurrentDeviceZoom(deviceZoom);
setCurrentDeviceZoom(nativeZoom);
}
}
return refreshed;
Expand Down Expand Up @@ -1146,7 +1147,7 @@ public boolean equals (Object object) {
if (object == this) return true;
if (!(object instanceof Image)) return false;
Image image = (Image) object;
if (device != image.device || transparentPixel != image.transparentPixel || currentDeviceZoom != image.currentDeviceZoom) return false;
if (device != image.device || transparentPixel != image.transparentPixel || getZoom() != image.getZoom()) return false;
if (imageDataProvider != null && image.imageDataProvider != null) {
return (styleFlag == image.styleFlag) && imageDataProvider.equals (image.imageDataProvider);
} else if (imageFileNameProvider != null && image.imageFileNameProvider != null) {
Expand Down Expand Up @@ -1243,8 +1244,8 @@ Rectangle getBounds(int zoom) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
// Read the bounds in pixels from native layer.
Rectangle bounds = getBoundsInPixelsFromNative();
if (bounds != null && zoom != currentDeviceZoom) {
bounds = DPIUtil.autoScaleBounds(bounds, zoom, currentDeviceZoom);
if (bounds != null && zoom != getZoom()) {
bounds = DPIUtil.autoScaleBounds(bounds, zoom, getZoom());
}
return bounds;
}
Expand Down Expand Up @@ -1348,7 +1349,7 @@ public ImageData getImageData() {
public ImageData getImageData (int zoom) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);

if (zoom == currentDeviceZoom) {
if (zoom == getZoom()) {
return getImageDataAtCurrentZoom();
} else if (imageDataProvider != null) {
ElementAtZoom<ImageData> data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, zoom);
Expand All @@ -1365,12 +1366,12 @@ public ImageData getImageData (int zoom) {
}
if (this.dataAtBaseZoom == null) {
// Cache data at base zoom before resizing it.
this.dataAtBaseZoom = new ElementAtZoom<>(getImageData(this.currentDeviceZoom), this.currentDeviceZoom);
this.dataAtBaseZoom = new ElementAtZoom<>(getImageData(getZoom()), getZoom());
}
if (this.dataAtBaseZoom != null) {
return DPIUtil.autoScaleImageData(device, this.dataAtBaseZoom, zoom);
} else {
return DPIUtil.autoScaleImageData (device, getImageDataAtCurrentZoom (), zoom, currentDeviceZoom);
return DPIUtil.autoScaleImageData (device, getImageDataAtCurrentZoom (), zoom, getZoom());
}
}

Expand Down Expand Up @@ -1685,7 +1686,7 @@ public int hashCode () {
if (imageDataProvider != null) {
return imageDataProvider.hashCode();
} else if (imageFileNameProvider != null) {
return Objects.hash(imageFileNameProvider, styleFlag, transparentPixel, currentDeviceZoom);
return Objects.hash(imageFileNameProvider, styleFlag, transparentPixel, getZoom());
} else {
return (int)handle;
}
Expand Down Expand Up @@ -2257,14 +2258,18 @@ public void setBackground(Color color) {
}

private void setCurrentDeviceZoom(int newZoomFactor) {
if (this.currentDeviceZoom != newZoomFactor) {
if (this.getZoom() != newZoomFactor) {
this.currentDeviceZoom = newZoomFactor;
// width and height are tied to the current device zoom
// they must be reset the the zoom factor changes
width = height = -1;
}
}

private int getZoom() {
return DPIUtil.getZoomForAutoscaleProperty(currentDeviceZoom);
}

/**
* Returns a string containing a concise, human-readable
* description of the receiver.
Expand Down Expand Up @@ -2319,7 +2324,7 @@ public static Image win32_new(Device device, int type, long handle) {
* @noreference This method is not intended to be referenced by clients.
*/
public static Image win32_new(Image image, int targetZoom) {
if (targetZoom != image.currentDeviceZoom) {
if (targetZoom != image.getZoom()) {
image.handleDPIChange(targetZoom);
}
return image;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4885,20 +4885,18 @@ LRESULT WM_DESTROY (long wParam, long lParam) {
LRESULT WM_DPICHANGED (long wParam, long lParam) {
// Map DPI to Zoom and compare
int nativeZoom = DPIUtil.mapDPIToZoom (OS.HIWORD (wParam));
int newSWTZoom = DPIUtil.getZoomForAutoscaleProperty (nativeZoom);
int oldSWTZoom = getShell().getZoom();
int oldNativeZoom = getShell().getNativeZoom();

// Throw the DPI change event if zoom value changes
if (newSWTZoom != oldSWTZoom) {
if (nativeZoom != oldNativeZoom) {
Event event = new Event();
event.type = SWT.ZoomChanged;
event.widget = this;
event.detail = newSWTZoom;
event.detail = nativeZoom;
event.doit = true;

if (DPIUtil.isAutoScaleOnRuntimeActive()) {
DPIUtil.setDeviceZoom (nativeZoom);
getShell().setNativeZoom(nativeZoom);
}

notifyListeners(SWT.ZoomChanged, event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public class Shell extends Decorations {
ToolTip [] toolTips;
long hwndMDIClient, lpstrTip, toolTipHandle, balloonTipHandle, menuItemToolTipHandle;
int minWidth = SWT.DEFAULT, minHeight = SWT.DEFAULT, maxWidth = SWT.DEFAULT, maxHeight = SWT.DEFAULT;
private int nativeZoom;
long [] brushes;
boolean showWithParent, fullScreen, wasMaximized, modified, center;
String toolTitle, balloonTitle;
Expand Down Expand Up @@ -301,17 +300,13 @@ public Shell (Display display, int style) {
state |= FOREIGN_HANDLE;
}

int shellZoom;
int shellNativeZoom;
if (parent != null) {
shellZoom = parent.getZoom();
shellNativeZoom = parent.getNativeZoom();
} else {
int mappedDPIZoom = getMonitor().getZoom();
shellZoom = DPIUtil.getZoomForAutoscaleProperty(mappedDPIZoom);
shellNativeZoom = mappedDPIZoom;
}
this.setZoom(shellZoom);
this.setNativeZoom(shellNativeZoom);

reskinWidget();
Expand Down Expand Up @@ -2650,19 +2645,8 @@ LRESULT WM_WINDOWPOSCHANGING (long wParam, long lParam) {
return result;
}

/**
* The native zoom in % of the standard resolution the shell is scaled for
*/
int getNativeZoom() {
return nativeZoom;
}

void setNativeZoom(int nativeZoom) {
this.nativeZoom = nativeZoom;
}

private void handleZoomEvent(Event event) {
float scalingFactor = 1f * event.detail / getZoom();
float scalingFactor = 1f * DPIUtil.getZoomForAutoscaleProperty(event.detail) / getZoom();
DPIZoomChangeRegistry.applyChange(this, event.detail, scalingFactor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
*/
public abstract class Widget {

private int zoom;
private int nativeZoom;
int style, state;
Display display;
EventTable eventTable;
Expand Down Expand Up @@ -169,7 +169,7 @@ public Widget (Widget parent, int style) {
checkSubclass ();
checkParent (parent);
this.style = style;
this.zoom = parent != null ? parent.getZoom() : DPIUtil.getDeviceZoom();
this.nativeZoom = parent != null ? parent.getNativeZoom() : DPIUtil.getNativeDeviceZoom();
display = parent.display;
reskinWidget ();
notifyCreationTracker();
Expand Down Expand Up @@ -2639,15 +2639,19 @@ void notifyDisposalTracker() {
/**
* The current DPI zoom level the widget is scaled for
*/
int getZoom() {
return zoom;
int getNativeZoom() {
return nativeZoom;
}

void setNativeZoom(int zoom) {
this.nativeZoom = zoom;
}

void setZoom(int zoom) {
this.zoom = zoom;
int getZoom() {
return DPIUtil.getZoomForAutoscaleProperty(getNativeZoom());
}

private static void handleDPIChange(Widget widget, int newZoom, float scalingFactor) {
widget.setZoom(newZoom);
widget.setNativeZoom(newZoom);
}
}

0 comments on commit 300f089

Please sign in to comment.