Skip to content

Commit

Permalink
Apply DPI zoom changes to basic controls
Browse files Browse the repository at this point in the history
This contribution adds and registers a DPI change handler for all widgets in the org.eclipse.swt.widgets package of the win32 implementation. These handler will be registered only, when the "swt.autoScale.updateOnRuntime"-flag is set to true. If a DPI change is detected they will be called accordingly.

Contributes to eclipse-platform#62
and eclipse-platform#127
  • Loading branch information
akoch-yatta committed Apr 19, 2024
1 parent ebcc084 commit 6ef2894
Show file tree
Hide file tree
Showing 36 changed files with 906 additions and 43 deletions.
14 changes: 13 additions & 1 deletion bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/os.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2022 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -3258,6 +3258,18 @@ JNIEXPORT jint JNICALL OS_NATIVE(GetSystemMetrics)
}
#endif

#ifndef NO_GetSystemMetricsForDpi
JNIEXPORT jint JNICALL OS_NATIVE(GetSystemMetricsForDpi)
(JNIEnv *env, jclass that, jint arg0, jint arg1)
{
jint rc = 0;
OS_NATIVE_ENTER(env, that, GetSystemMetricsForDpi_FUNC);
rc = (jint)GetSystemMetricsForDpi(arg0, arg1);
OS_NATIVE_EXIT(env, that, GetSystemMetricsForDpi_FUNC);
return rc;
}
#endif

#ifndef NO_GetTextColor
JNIEXPORT jint JNICALL OS_NATIVE(GetTextColor)
(JNIEnv *env, jclass that, jlong arg0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ typedef enum {
GetSysColorBrush_FUNC,
GetSystemMenu_FUNC,
GetSystemMetrics_FUNC,
GetSystemMetricsForDpi_FUNC,
GetTextColor_FUNC,
GetTextExtentPoint32_FUNC,
GetTextMetrics_FUNC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2976,6 +2976,7 @@ public static int HRESULT_FROM_WIN32(int x) {
/** @param hWnd cast=(HWND) */
public static final native long GetSystemMenu (long hWnd, boolean bRevert);
public static final native int GetSystemMetrics (int nIndex);
public static final native int GetSystemMetricsForDpi (int nIndex, int dpi);
/** @param hDC cast=(HDC) */
public static final native int GetTextColor (long hDC);
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ public static ImageData autoScaleImageData (Device device, final ImageData image
return autoScaleImageData(device, imageData, scaleFactor);
}


public static ImageData autoScaleImageData (Device device, final ElementAtZoom<ImageData> elementAtZoom, int targetZoom) {
return autoScaleImageData(device, elementAtZoom.element(), targetZoom, elementAtZoom.zoom());
}

private static ImageData autoScaleImageData (Device device, final ImageData imageData, float scaleFactor) {
// Guards are already implemented in callers: if (deviceZoom == 100 || imageData == null || scaleFactor == 1.0f) return imageData;
int width = imageData.width;
Expand Down Expand Up @@ -301,6 +306,10 @@ public static ImageData autoScaleUp (Device device, final ImageData imageData) {
return autoScaleImageData(device, imageData, 100);
}

public static ImageData autoScaleUp (Device device, final ElementAtZoom<ImageData> elementAtZoom) {
return autoScaleImageData(device, elementAtZoom.element(), elementAtZoom.zoom());
}

public static int[] autoScaleUp(int[] pointArray) {
if (deviceZoom == 100 || pointArray == null) return pointArray;
float scaleFactor = getScalingFactor ();
Expand All @@ -325,6 +334,14 @@ public static int autoScaleUp (int size) {
return Math.round (size * scaleFactor);
}

/**
* Auto-scale up int dimensions to match the given zoom level
*/
public static int autoScaleUp (int size, int zoom) {
float scaleFactor = getScalingFactor (zoom);
return Math.round (size * scaleFactor);
}

/**
* Auto-scale up int dimensions using Native DPI
*/
Expand Down Expand Up @@ -402,10 +419,18 @@ public static Rectangle autoScaleUp (Drawable drawable, Rectangle rect) {
* @return float scaling factor
*/
private static float getScalingFactor () {
return getScalingFactor(deviceZoom);
}

/**
* Returns scaling factor from the given device zoom
* @return float scaling factor
*/
private static float getScalingFactor (int shellDeviceZoom) {
if (useCairoAutoScale) {
return 1;
}
return deviceZoom / 100f;
return shellDeviceZoom / 100f;
}

/**
Expand All @@ -422,12 +447,12 @@ public static int mapDPIToZoom (int dpi) {
/**
* Compute the DPI value value based on the zoom.
*
* @return zoom
* @return DPI
*/
public static int mapZoomToDPI (int dpi) {
double zoom = (double) dpi / 100 * DPI_ZOOM_100;
int roundedZoom = (int) Math.round (zoom);
return roundedZoom;
public static int mapZoomToDPI (int zoom) {
double dpi = (double) zoom / 100 * DPI_ZOOM_100;
int roundedDpi = (int) Math.round (dpi);
return roundedDpi;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*******************************************************************************
* Copyright (c) 2024 Yatta Solutions and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Yatta Solutions - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.custom;

import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.widgets.*;

/**
* This class is used in the win32 implementation only to support
* adjusting widgets in the common package to DPI changes
* <p>
* <b>IMPORTANT:</b> This class is <em>not</em> part of the public
* API for SWT. It is marked public only so that it can be shared
* within the packages provided by SWT. It is not available on all
* platforms, and should never be called from application code.
* </p>
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
* @noreference This class is not intended to be referenced by clients
*/
public class CommonWidgetsDPIChangeHandlers {

public static void registerCommonHandlers() {
DPIZoomChangeRegistry.registerHandler(CommonWidgetsDPIChangeHandlers::handleItemDPIChange, Item.class);
}

private static void handleItemDPIChange(Widget widget, int newZoom, float scalingFactor) {
if (!(widget instanceof Item item)) {
return;
}
// Refresh the image
Image image = item.getImage();
if (image != null) {
item.setImage(Image.win32_new(image, newZoom));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ public void drawImage (Image image, int srcX, int srcY, int srcWidth, int srcHei
* the coordinates may be slightly off. The workaround is to restrict
* coordinates to the allowed bounds.
*/
Rectangle b = image.getBoundsInPixels();
Rectangle b = image.getBounds(deviceZoom);
int errX = src.x + src.width - b.width;
int errY = src.y + src.height - b.height;
if (errX != 0 || errY != 0) {
Expand All @@ -996,8 +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.refreshImageForZoom ();

srcImage.handleDPIChange(DPIUtil.getDeviceZoom());
if (data.gdipGraphics != 0) {
//TODO - cache bitmap
long [] gdipImage = srcImage.createGdipImage();
Expand Down Expand Up @@ -4387,7 +4386,7 @@ public void setFillRule(int rule) {
public void setFont (Font font) {
if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
if (font != null && font.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
data.font = font != null ? font : data.device.systemFont;
data.font = font != null ? Font.win32_new(font, DPIUtil.getNativeDeviceZoom()) : data.device.systemFont;
data.state &= ~FONT;
}

Expand Down
Loading

0 comments on commit 6ef2894

Please sign in to comment.