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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
public abstract class Win32AutoscaleTestBase {
protected Display display;
protected Shell shell;
private boolean autoScaleOnRuntime;

@BeforeAll
public static void assumeIsFittingPlatform() {
Expand All @@ -29,9 +28,8 @@ public static void assumeIsFittingPlatform() {

@BeforeEach
public void setUpTest() {
autoScaleOnRuntime = DPITestUtil.isAutoScaleOnRuntimeActive();
display = Display.getDefault();
autoScaleOnRuntime(true);
display.setRescalingAtRuntime(true);
shell = new Shell(display);
}

Expand All @@ -40,13 +38,7 @@ public void tearDownTest() {
if (shell != null) {
shell.dispose();
}
autoScaleOnRuntime(autoScaleOnRuntime);
}

protected void autoScaleOnRuntime (boolean autoScaleOnRuntime) {
DPITestUtil.setAutoScaleOnRunTime(autoScaleOnRuntime);
// dispose a existing font registry for the default display
SWTFontProvider.disposeFontRegistry(display);
display.dispose();
}

protected void changeDPIZoom (int nativeZoom) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ControlWin32Tests extends Win32AutoscaleTestBase {

@Test
public void testScaleFontCorrectlyInAutoScaleSzenario() {
assertTrue("Autoscale property is not set to true", DPITestUtil.isAutoScaleOnRuntimeActive());
assertTrue("Autoscale property is not set to true", display.isRescalingAtRuntime());

int scalingFactor = 2;
Control control = new Composite(shell, SWT.NONE);
Expand All @@ -54,8 +54,8 @@ public void testScaleFontCorrectlyInAutoScaleSzenario() {

@Test
public void testDoNotScaleFontCorrectlyInNoAutoScaleSzenario() {
autoScaleOnRuntime(false);
assertFalse("Autoscale property is not set to false", DPITestUtil.isAutoScaleOnRuntimeActive());
display.setRescalingAtRuntime(false);
assertFalse("Autoscale property is not set to false", display.isRescalingAtRuntime());

int scalingFactor = 2;
Control control = new Composite(shell, SWT.NONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6832,4 +6832,33 @@ static long windowProc(long id, long sel, long arg0, long arg1, long arg2, long
static boolean isActivateShellOnForceFocus() {
return "true".equals(System.getProperty("org.eclipse.swt.internal.activateShellOnForceFocus", "true")); //$NON-NLS-1$
}

/**
* {@return whether rescaling of shells at runtime when the DPI scaling of a
* shell's monitor changes is activated for this device}
* <p>
* <b>Note:</b> This functionality is only available on Windows. Calling this
* method on other operating system will always return false.
*
* @since 3.127
*/
public boolean isRescalingAtRuntime() {
return false;
}

/**
* Activates or deactivates rescaling of shells at runtime whenever the DPI
* scaling of the shell's monitor changes. This is only safe to call as long as
* no shell has been created for this display. When changing the value after a
* shell has been created for this display, the effect is undefined.
* <p>
* <b>Note:</b> This functionality is only available on Windows. Calling this
* method on other operating system will have no effect.
*
* @param activate whether rescaling shall be activated or deactivated
* @since 3.127
*/
public void setRescalingAtRuntime(boolean activate) {
// not implemented for Cocoa
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,8 @@ private static enum AutoScaleMethod { AUTO, NEAREST, SMOOTH }
}
}

if ("win32".equals(SWT.getPlatform())) {
String updateOnRuntimeValue = System.getProperty (SWT_AUTOSCALE_UPDATE_ON_RUNTIME);
autoScaleOnRuntime = Boolean.parseBoolean(updateOnRuntimeValue);
}
String updateOnRuntimeValue = System.getProperty (SWT_AUTOSCALE_UPDATE_ON_RUNTIME);
autoScaleOnRuntime = Boolean.parseBoolean(updateOnRuntimeValue);
}

/**
Expand Down Expand Up @@ -669,10 +667,6 @@ public static boolean isAutoScaleOnRuntimeActive() {
return autoScaleOnRuntime;
}

static boolean setAutoScaleOnRuntimeActive(boolean value) {
return autoScaleOnRuntime = value;
}

/**
* AutoScale ImageDataProvider.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6266,4 +6266,34 @@ static int _getDeviceZoom (long monitor_num) {
static boolean isActivateShellOnForceFocus() {
return "true".equals(System.getProperty("org.eclipse.swt.internal.activateShellOnForceFocus", "true")); //$NON-NLS-1$
}

/**
* {@return whether rescaling of shells at runtime when the DPI scaling of a
* shell's monitor changes is activated for this device}
* <p>
* <b>Note:</b> This functionality is only available on Windows. Calling this
* method on other operating system will always return false.
*
* @since 3.127
*/
public boolean isRescalingAtRuntime() {
return false;
}

/**
* Activates or deactivates rescaling of shells at runtime whenever the DPI
* scaling of the shell's monitor changes. This is only safe to call as long as
* no shell has been created for this display. When changing the value after a
* shell has been created for this display, the effect is undefined.
* <p>
* <b>Note:</b> This functionality is only available on Windows. Calling this
* method on other operating system will have no effect.
*
* @param activate whether rescaling shall be activated or deactivated
* @since 3.127
*/
public void setRescalingAtRuntime(boolean activate) {
// not implemented for GTK
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.*;

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

/**
* This internal class is used to provide and cache fonts scaled for different zoom levels in the win32
Expand Down Expand Up @@ -47,7 +48,7 @@ public static void disposeFontRegistry(Device device) {
}

private static SWTFontRegistry newFontRegistry(Device device) {
if (DPIUtil.isAutoScaleOnRuntimeActive()) {
if (device instanceof Display display && display.isRescalingAtRuntime()) {
return new ScalingSWTFontRegistry(device);
}
return new DefaultSWTFontRegistry(device);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4910,13 +4910,13 @@ LRESULT WM_DPICHANGED (long wParam, long lParam) {
event.detail = newNativeZoom;
event.doit = true;

if (DPIUtil.isAutoScaleOnRuntimeActive()) {
if (getDisplay().isRescalingAtRuntime()) {
DPIUtil.setDeviceZoom (newNativeZoom);
}

notifyListeners(SWT.ZoomChanged, event);

if (DPIUtil.isAutoScaleOnRuntimeActive()) {
if (getDisplay().isRescalingAtRuntime()) {
RECT rect = new RECT ();
COM.MoveMemory(rect, lParam, RECT.sizeof);
this.setBoundsInPixels(rect.left, rect.top, rect.right - rect.left, rect.bottom-rect.top);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public class Display extends Device implements Executor {
EventTable eventTable, filterTable;
boolean useOwnDC;
boolean externalEventLoop; // events are dispatched outside SWT, e.g. TrackPopupMenu or DoDragDrop
private boolean rescalingAtRuntime;

/* Widget Table */
private Map<Long, Control> controlByHandle;
Expand Down Expand Up @@ -582,28 +583,8 @@ public Display () {
*/
public Display (DeviceData data) {
super (data);
initializeProperDPIAwareness();
}

private void initializeProperDPIAwareness() {
if (!DPIUtil.isAutoScaleOnRuntimeActive()) {
return;
}
// Auto scaling on runtime requires DPI awareness mode "Per Monitor V2"
boolean perMonitorV2Available = OS.WIN32_BUILD > OS.WIN32_BUILD_WIN10_1809;
if (!perMonitorV2Available) {
System.err.println(
"***WARNING: rescaling at runtime is activated but the OS version does not support required DPI awareness mode PerMonitorV2.");
return;
}

boolean alreadyUsesPerMonitorV2 = OS.GetThreadDpiAwarenessContext() == OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2;
if (alreadyUsesPerMonitorV2) {
return;
}
long setDpiAwarenessResult = OS.SetThreadDpiAwarenessContext(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
if (setDpiAwarenessResult == 0L) {
System.err.println("***WARNING: setting DPI awareness to PerMonitorV2 failed.");
if (DPIUtil.isAutoScaleOnRuntimeActive()) {
setRescalingAtRuntime(true);
}
}

Expand Down Expand Up @@ -5267,4 +5248,62 @@ static String withCrLf (String string) {
static boolean isActivateShellOnForceFocus() {
return "true".equals(System.getProperty("org.eclipse.swt.internal.activateShellOnForceFocus", "true")); //$NON-NLS-1$
}

/**
* {@return whether rescaling of shells at runtime when the DPI scaling of a
* shell's monitor changes is activated for this device}
* <p>
* <b>Note:</b> This functionality is only available on Windows. Calling this
* method on other operating system will always return false.
*
* @since 3.127
*/
public boolean isRescalingAtRuntime() {
return rescalingAtRuntime;
}

/**
* Activates or deactivates rescaling of shells at runtime whenever the DPI
* scaling of the shell's monitor changes. This is only safe to call as long as
* no shell has been created for this display. When changing the value after a
* shell has been created for this display, the effect is undefined.
* <p>
* <b>Note:</b> This functionality is only available on Windows. Calling this
* method on other operating system will have no effect.
*
* @param activate whether rescaling shall be activated or deactivated
* @since 3.127
*/
public void setRescalingAtRuntime(boolean activate) {
rescalingAtRuntime = activate;
// dispose a existing font registry for the default display
SWTFontProvider.disposeFontRegistry(this);
setProperDPIAwareness();
}

private void setProperDPIAwareness() {
long desiredDpiAwareness = OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE;
if (OS.WIN32_BUILD < OS.WIN32_BUILD_WIN10_1607) {
System.err.println("***WARNING: the OS version does not support setting DPI awareness.");
return;
}
if (rescalingAtRuntime) {
desiredDpiAwareness = OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2;
// Auto scaling on runtime requires DPI awareness mode "Per Monitor V2"
boolean perMonitorV2Available = OS.WIN32_BUILD >= OS.WIN32_BUILD_WIN10_1809;
if (!perMonitorV2Available) {
System.err.println(
"***WARNING: rescaling at runtime is activated but the OS version does not support required DPI awareness mode PerMonitorV2.");
return;
}
}
if (desiredDpiAwareness == OS.GetThreadDpiAwarenessContext()) {
return;
}
long setDpiAwarenessResult = OS.SetThreadDpiAwarenessContext(desiredDpiAwareness);
if (setDpiAwarenessResult == 0L) {
System.err.println("***WARNING: setting DPI awareness failed.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public Shell (Display display, int style) {
createWidget ();


if (DPIUtil.isAutoScaleOnRuntimeActive()) {
if (getDisplay().isRescalingAtRuntime()) {
addListener(SWT.ZoomChanged, this::handleZoomEvent);
}
}
Expand Down