Skip to content

Commit 4fe6c71

Browse files
committed
Deprecate insufficient Display#setRescalingAtRuntime() method
The method Display#setRescalingAtRuntime() allows to activate the monitor-specific scaling mechanism on Windows. This must, however, actually be activated before the Display is instantiated, like it is done during the Display constructor execution if the system property for global activation of monitor-specific scaling is specified. In consequence, calling #setRescalingAtRuntime() does not take the full intended effect. This change deprecates the method so that the system property needs to be set for proper initialization of the monitor-specific scaling functionality. It also adapts the according tests to the deprecation of the API and alternatives to be used.
1 parent a6dd128 commit 4fe6c71

File tree

7 files changed

+90
-26
lines changed

7 files changed

+90
-26
lines changed

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/internal/Win32AutoscaleTestBase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ public static void assumeIsFittingPlatform() {
2727

2828
@BeforeEach
2929
public void setUpTest() {
30-
display = Display.getDefault();
31-
display.setRescalingAtRuntime(true);
30+
Display.getDefault().dispose();
31+
DPIUtil.setMonitorSpecificScaling(true);
32+
display = new Display();
3233
shell = new Shell(display);
3334
}
3435

3536
@AfterEach
3637
public void tearDownTest() {
38+
DPIUtil.setMonitorSpecificScaling(false);
3739
if (shell != null) {
3840
shell.dispose();
3941
}

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/ControlWin32Tests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ public void testScaleFontCorrectlyInAutoScaleSzenario() {
5454

5555
@Test
5656
public void testDoNotScaleFontCorrectlyInNoAutoScaleSzenario() {
57-
display.setRescalingAtRuntime(false);
57+
display.dispose();
58+
DPIUtil.setMonitorSpecificScaling(false);
59+
Display display = new Display();
60+
shell = new Shell(display);
5861
assertFalse("Autoscale property is not set to false", display.isRescalingAtRuntime());
5962

6063
int scalingFactor = 2;

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/DisplayWin32Test.java

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,89 @@
88

99
public class DisplayWin32Test {
1010

11-
private Display display;
11+
private boolean initialMonitorSpecificScaling;
1212

1313
@BeforeAll
1414
public static void assumeIsFittingPlatform() {
1515
PlatformSpecificExecution.assumeIsFittingPlatform();
1616
}
1717

1818
@BeforeEach
19-
public void createDisplay() {
20-
display = new Display();
19+
public void storeState() {
20+
initialMonitorSpecificScaling = DPIUtil.isMonitorSpecificScalingActive();
2121
}
2222

2323
@AfterEach
24-
public void destroyDisplay() {
25-
display.dispose();
24+
public void restoreState() {
25+
DPIUtil.setMonitorSpecificScaling(initialMonitorSpecificScaling);
2626
}
2727

2828
@Test
29+
public void monitorSpecificScaling_activate() {
30+
DPIUtil.setMonitorSpecificScaling(true);
31+
Display display = new Display();
32+
try {
33+
assertTrue(display.isRescalingAtRuntime());
34+
assertEquals(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, OS.GetThreadDpiAwarenessContext());
35+
} finally {
36+
display.dispose();
37+
}
38+
}
39+
40+
@Test
41+
public void monitorSpecificScaling_deactivate() {
42+
DPIUtil.setMonitorSpecificScaling(false);
43+
Display display = new Display();
44+
try {
45+
assertFalse(display.isRescalingAtRuntime());
46+
} finally {
47+
display.dispose();
48+
}
49+
}
50+
51+
@Test
52+
@SuppressWarnings("removal")
2953
public void setRescaleAtRuntime_activate() {
30-
display.setRescalingAtRuntime(true);
31-
assertTrue(display.isRescalingAtRuntime());
32-
assertEquals(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, OS.GetThreadDpiAwarenessContext());
54+
Display display = new Display();
55+
try {
56+
display.setRescalingAtRuntime(true);
57+
assertTrue(display.isRescalingAtRuntime());
58+
assertEquals(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, OS.GetThreadDpiAwarenessContext());
59+
} finally {
60+
display.dispose();
61+
}
3362
}
3463

3564
@Test
65+
@SuppressWarnings("removal")
3666
public void setRescaleAtRuntime_deactivate() {
37-
display.setRescalingAtRuntime(false);
38-
assertFalse(display.isRescalingAtRuntime());
39-
assertEquals(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE, OS.GetThreadDpiAwarenessContext());
67+
Display display = new Display();
68+
try {
69+
display.setRescalingAtRuntime(false);
70+
assertFalse(display.isRescalingAtRuntime());
71+
assertEquals(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE, OS.GetThreadDpiAwarenessContext());
72+
} finally {
73+
display.dispose();
74+
}
4075
}
4176

4277
@Test
78+
@SuppressWarnings("removal")
4379
public void setRescaleAtRuntime_toggling() {
44-
display.setRescalingAtRuntime(false);
45-
assertFalse(display.isRescalingAtRuntime());
46-
assertEquals(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE, OS.GetThreadDpiAwarenessContext());
47-
display.setRescalingAtRuntime(true);
48-
assertTrue(display.isRescalingAtRuntime());
49-
assertEquals(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, OS.GetThreadDpiAwarenessContext());
50-
display.setRescalingAtRuntime(false);
51-
assertFalse(display.isRescalingAtRuntime());
52-
assertEquals(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE, OS.GetThreadDpiAwarenessContext());
80+
Display display = new Display();
81+
try {
82+
display.setRescalingAtRuntime(false);
83+
assertFalse(display.isRescalingAtRuntime());
84+
assertEquals(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE, OS.GetThreadDpiAwarenessContext());
85+
display.setRescalingAtRuntime(true);
86+
assertTrue(display.isRescalingAtRuntime());
87+
assertEquals(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, OS.GetThreadDpiAwarenessContext());
88+
display.setRescalingAtRuntime(false);
89+
assertFalse(display.isRescalingAtRuntime());
90+
assertEquals(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE, OS.GetThreadDpiAwarenessContext());
91+
} finally {
92+
display.dispose();
93+
}
5394
}
5495

5596
}

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6858,7 +6858,10 @@ public boolean isRescalingAtRuntime() {
68586858
* @param activate whether rescaling shall be activated or deactivated
68596859
* @return whether activating or deactivating the rescaling was successful
68606860
* @since 3.127
6861+
* @deprecated this method should not be used as it needs to be called already
6862+
* during instantiation to take proper effect
68616863
*/
6864+
@Deprecated(since = "2025-03", forRemoval = true)
68626865
public boolean setRescalingAtRuntime(boolean activate) {
68636866
// not implemented for Cocoa
68646867
return false;

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,11 @@ public static int getZoomForAutoscaleProperty (int nativeDeviceZoom) {
650650
return zoom;
651651
}
652652

653-
public static boolean isAutoScaleOnRuntimeActive() {
653+
public static void setMonitorSpecificScaling(boolean active) {
654+
System.setProperty(SWT_AUTOSCALE_UPDATE_ON_RUNTIME, Boolean.toString(active));
655+
}
656+
657+
public static boolean isMonitorSpecificScalingActive() {
654658
boolean updateOnRuntimeValue = Boolean.getBoolean (SWT_AUTOSCALE_UPDATE_ON_RUNTIME);
655659
return updateOnRuntimeValue;
656660
}

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6294,7 +6294,10 @@ public boolean isRescalingAtRuntime() {
62946294
* @param activate whether rescaling shall be activated or deactivated
62956295
* @return whether activating or deactivating the rescaling was successful
62966296
* @since 3.127
6297+
* @deprecated this method should not be used as it needs to be called already
6298+
* during instantiation to take proper effect
62976299
*/
6300+
@Deprecated(since = "2025-03", forRemoval = true)
62986301
public boolean setRescalingAtRuntime(boolean activate) {
62996302
// not implemented for GTK
63006303
return false;

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,8 +948,8 @@ public void close () {
948948
protected void create (DeviceData data) {
949949
checkSubclass ();
950950
checkDisplay (thread = Thread.currentThread (), true);
951-
if (DPIUtil.isAutoScaleOnRuntimeActive()) {
952-
setRescalingAtRuntime(true);
951+
if (DPIUtil.isMonitorSpecificScalingActive()) {
952+
setMonitorSpecificScaling(true);
953953
}
954954
createDisplay (data);
955955
register (this);
@@ -5261,6 +5261,7 @@ private long openThemeData(final char[] themeName) {
52615261
return OS.OpenThemeData(hwndMessage, themeName, dpi);
52625262
}
52635263
}
5264+
52645265
/**
52655266
* {@return whether rescaling of shells at runtime when the DPI scaling of a
52665267
* shell's monitor changes is activated for this device}
@@ -5286,8 +5287,15 @@ public boolean isRescalingAtRuntime() {
52865287
* @param activate whether rescaling shall be activated or deactivated
52875288
* @return whether activating or deactivating the rescaling was successful
52885289
* @since 3.127
5290+
* @deprecated this method should not be used as it needs to be called already
5291+
* during instantiation to take proper effect
52895292
*/
5293+
@Deprecated(since = "2025-03", forRemoval = true)
52905294
public boolean setRescalingAtRuntime(boolean activate) {
5295+
return setMonitorSpecificScaling(activate);
5296+
}
5297+
5298+
private boolean setMonitorSpecificScaling(boolean activate) {
52915299
int desiredApiAwareness = activate ? OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 : OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE;
52925300
if (setDPIAwareness(desiredApiAwareness)) {
52935301
rescalingAtRuntime = activate;

0 commit comments

Comments
 (0)