Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ protected ResetMonitorSpecificScalingExtension() {

@Override
public void beforeEach(ExtensionContext context) throws Exception {
wasMonitorSpecificScalingActive = DPIUtil.isMonitorSpecificScalingActive();
wasMonitorSpecificScalingActive = Win32DPIUtils.isMonitorSpecificScalingActive();
Display.getDefault().dispose();
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
DPIUtil.setMonitorSpecificScaling(wasMonitorSpecificScalingActive);
Win32DPIUtils.setMonitorSpecificScaling(wasMonitorSpecificScalingActive);
Display.getDefault().dispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private WithMonitorSpecificScalingExtension() {
@Override
public void beforeEach(ExtensionContext context) throws Exception {
super.beforeEach(context);
DPIUtil.setMonitorSpecificScaling(true);
Win32DPIUtils.setMonitorSpecificScaling(true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ControlWin32Tests {

@Test
public void testScaleFontCorrectlyInAutoScaleScenario() {
DPIUtil.setMonitorSpecificScaling(true);
Win32DPIUtils.setMonitorSpecificScaling(true);
Display display = Display.getDefault();

assertTrue("Autoscale property is not set to true", display.isRescalingAtRuntime());
Expand All @@ -48,7 +48,7 @@ public void testScaleFontCorrectlyInAutoScaleScenario() {

@Test
public void testSetFontWithMonitorSpecificScalingEnabled() {
DPIUtil.setMonitorSpecificScaling(true);
Win32DPIUtils.setMonitorSpecificScaling(true);
Display display = Display.getDefault();
Image colorImage = new Image(display, 10, 10);
GC gc = new GC(colorImage);
Expand All @@ -59,7 +59,7 @@ public void testSetFontWithMonitorSpecificScalingEnabled() {

@Test
public void testScaleFontCorrectlyInNoAutoScaleScenario() {
DPIUtil.setMonitorSpecificScaling(false);
Win32DPIUtils.setMonitorSpecificScaling(false);
Display display = Display.getDefault();

assertFalse("Autoscale property is not set to false", display.isRescalingAtRuntime());
Expand All @@ -71,7 +71,7 @@ public void testScaleFontCorrectlyInNoAutoScaleScenario() {

@Test
public void testDoNotScaleFontInNoAutoScaleScenarioWithLegacyFontRegistry() {
DPIUtil.setMonitorSpecificScaling(false);
Win32DPIUtils.setMonitorSpecificScaling(false);
String originalValue = System.getProperty("swt.fontRegistry");
System.setProperty("swt.fontRegistry", "legacy");
try {
Expand All @@ -93,7 +93,7 @@ public void testDoNotScaleFontInNoAutoScaleScenarioWithLegacyFontRegistry() {

@Test
public void testCorrectScaleUpUsingDifferentSetBoundsMethod() {
DPIUtil.setMonitorSpecificScaling(true);
Win32DPIUtils.setMonitorSpecificScaling(true);
Display display = Display.getDefault();
Shell shell = new Shell(display);
Button button = new Button(shell, SWT.PUSH);
Expand All @@ -114,7 +114,7 @@ public void testCorrectScaleUpUsingDifferentSetBoundsMethod() {
@CsvSource({ "0.5, 100, true", "1.0, 200, true", "2.0, 200, true", "2.0, quarter, true", "0.5, 100, false",
"1.0, 200, false", "2.0, 200, false", "2.0, quarter, false", })
public void testAutoScaleImageData(float scaleFactor, String autoScale, boolean monitorSpecificScaling) {
DPIUtil.setMonitorSpecificScaling(monitorSpecificScaling);
Win32DPIUtils.setMonitorSpecificScaling(monitorSpecificScaling);
DPIUtil.runWithAutoScaleValue(autoScale, () -> {
Display display = new Display();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ class DisplayWin32Test {

@Test
public void monitorSpecificScaling_activate() {
DPIUtil.setMonitorSpecificScaling(true);
Win32DPIUtils.setMonitorSpecificScaling(true);
Display display = Display.getDefault();
assertTrue(display.isRescalingAtRuntime());
assertEquals(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, OS.GetThreadDpiAwarenessContext());
}

@Test
public void monitorSpecificScaling_deactivate() {
DPIUtil.setMonitorSpecificScaling(false);
Win32DPIUtils.setMonitorSpecificScaling(false);
Display display = Display.getDefault();
assertFalse(display.isRescalingAtRuntime());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1831,22 +1831,5 @@ public static void drawScaled(GC gc, ImageData imageData, int width, int height,
imageToDraw.dispose();
}

private final class CocoaDPIUtil {

/**
* Auto-scale down int dimensions.
*/
public static int pixelToPoint(int size) {
return DPIUtil.pixelToPoint(size, DPIUtil.getDeviceZoom());
}

/**
* Auto-scale down float dimensions.
*/
public static float pixelToPoint(float size) {
return DPIUtil.pixelToPoint(size, DPIUtil.getDeviceZoom());
}
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*******************************************************************************
* Copyright (c) 2025 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.internal;

public class CocoaDPIUtil {

/**
* Auto-scale down int dimensions.
*/
public static int pixelToPoint(int size) {
return DPIUtil.pixelToPoint(size, DPIUtil.getDeviceZoom());
}

/**
* Auto-scale down float dimensions.
*/
public static float pixelToPoint(float size) {
return DPIUtil.pixelToPoint(size, DPIUtil.getDeviceZoom());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,6 @@ public static Optional<AutoScaleMethod> forString(String s) {
*/
private static final String SWT_AUTOSCALE_METHOD = "swt.autoScale.method";

/**
* System property to enable to scale the application on runtime
* when a DPI change is detected.
* <ul>
* <li>"true": the application is scaled on DPI changes</li>
* <li>"false": the application will remain in its initial scaling</li>
* </ul>
* <b>Important:</b> This flag is only parsed and used on Win32. Setting it to
* true on GTK or cocoa will be ignored.
*/
private static final String SWT_AUTOSCALE_UPDATE_ON_RUNTIME = "swt.autoScale.updateOnRuntime";
static {
autoScaleValue = System.getProperty (SWT_AUTOSCALE);

Expand All @@ -115,6 +104,13 @@ public static Optional<AutoScaleMethod> forString(String s) {
autoScaleMethod = AUTO_SCALE_METHOD_SETTING != AutoScaleMethod.AUTO ? AUTO_SCALE_METHOD_SETTING : AutoScaleMethod.NEAREST;
}

static String getAutoScaleValue() {
return autoScaleValue;
}

static void setAutoScaleValue(String autoScaleValueArg) {
autoScaleValue = autoScaleValueArg;
}

public static int pixelToPoint(int size, int zoom) {
if (zoom == 100 || size == SWT.DEFAULT) return size;
Expand Down Expand Up @@ -327,20 +323,23 @@ public static void setDeviceZoom (int nativeDeviceZoom) {
// in GTK, preserve the current method when switching to a 100% monitor
boolean preserveScalingMethod = SWT.getPlatform().equals("gtk") && deviceZoom == 100;
if (!preserveScalingMethod && AUTO_SCALE_METHOD_SETTING == AutoScaleMethod.AUTO) {
if (sholdUseSmoothScaling()) {
if (useSmoothScalingByDefaultProvider.shouldUseSmoothScaling()) {
autoScaleMethod = AutoScaleMethod.SMOOTH;
} else {
autoScaleMethod = AutoScaleMethod.NEAREST;
}
}
}

private static boolean sholdUseSmoothScaling() {
return switch (SWT.getPlatform()) {
case "gtk" -> deviceZoom / 100 * 100 != deviceZoom;
case "win32" -> isMonitorSpecificScalingActive();
default -> false;
};
@FunctionalInterface
interface UseSmoothScalingProvider {
boolean shouldUseSmoothScaling();
}

private static UseSmoothScalingProvider useSmoothScalingByDefaultProvider = () -> false;

static void setUseSmoothScalingByDefaultProvider(UseSmoothScalingProvider provider) {
useSmoothScalingByDefaultProvider = provider;
}

public static int getZoomForAutoscaleProperty (int nativeDeviceZoom) {
Expand Down Expand Up @@ -388,54 +387,4 @@ public static void runWithAutoScaleValue(String autoScaleValue, Runnable runnabl
}
}

public static void setMonitorSpecificScaling(boolean activate) {
System.setProperty(SWT_AUTOSCALE_UPDATE_ON_RUNTIME, Boolean.toString(activate));
}

public static boolean isMonitorSpecificScalingActive() {
boolean updateOnRuntimeValue = Boolean.getBoolean (SWT_AUTOSCALE_UPDATE_ON_RUNTIME);
return updateOnRuntimeValue;
}

public static void setAutoScaleForMonitorSpecificScaling() {
boolean isDefaultAutoScale = autoScaleValue == null;
if (isDefaultAutoScale) {
autoScaleValue = "quarter";
} else if (!isSupportedAutoScaleForMonitorSpecificScaling()) {
throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED,
"monitor-specific scaling is only implemented for auto-scale values \"quarter\", \"exact\", \"false\" or a concrete zoom value, but \""
+ autoScaleValue + "\" has been specified");
}
}

/**
* Monitor-specific scaling on Windows only supports auto-scale modes in which
* all elements (font, images, control bounds etc.) are scaled equally or almost
* equally. The previously default mode "integer"/"integer200", which rounded
* the scale factor for everything but fonts to multiples of 100, is complex and
* difficult to realize with monitor-specific rescaling of UI elements. Since a
* uniform scale factor for everything should perspectively be used anyway,
* there will be support for complex auto-scale modes for monitor-specific
* scaling.
*
* The supported modes are "quarter" and "exact" or explicit zoom values given
* by the value itself or "false". Every other value will be treated as
* "integer"/"integer200" and is thus not supported.
*/
private static boolean isSupportedAutoScaleForMonitorSpecificScaling() {
if (autoScaleValue == null) {
return false;
}
switch (autoScaleValue.toLowerCase()) {
case "false", "quarter", "exact": return true;
}
try {
Integer.parseInt(autoScaleValue);
return true;
} catch (NumberFormatException e) {
// unsupported value, use default
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1592,17 +1592,4 @@ public static void drawScaled(GC gc, ImageData imageData, int width, int height,
imageToDraw.dispose();
}

private final class GtkDPIUtil {

/**
* Auto-scale up ImageData to device zoom that is at 100%.
*/
public static ImageData pointToPixel (Device device, final ImageData imageData) {
int imageDataZoomFactor = 100;
if (DPIUtil.getDeviceZoom() == imageDataZoomFactor || imageData == null || (device != null && !device.isAutoScalable())) return imageData;
float scaleFactor = (float) DPIUtil.getDeviceZoom() / imageDataZoomFactor;
return DPIUtil.autoScaleImageData(device, imageData, scaleFactor);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* Copyright (c) 2025 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.internal;

import org.eclipse.swt.graphics.*;

public class GtkDPIUtil {
static {
DPIUtil.setUseSmoothScalingByDefaultProvider(() -> {
return DPIUtil.getDeviceZoom() / 100 * 100 != DPIUtil.getDeviceZoom();
});
}

/**
* Auto-scale up ImageData to device zoom that is at 100%.
*/
public static ImageData pointToPixel (Device device, final ImageData imageData) {
int imageDataZoomFactor = 100;
if (DPIUtil.getDeviceZoom() == imageDataZoomFactor || imageData == null || (device != null && !device.isAutoScalable())) return imageData;
float scaleFactor = (float) DPIUtil.getDeviceZoom() / imageDataZoomFactor;
return DPIUtil.autoScaleImageData(device, imageData, scaleFactor);
}
}
Loading
Loading