Skip to content

Commit 81ef5e3

Browse files
Moving methods from Win32DPIUtil to DPIUtil for autoscaling
Workbench needs few methods from DPIUtil that were only present for Win32. Keeping the functionality for Win32DPIUtil as is, just making them available for DPIUtil.
1 parent d4ddd4c commit 81ef5e3

File tree

4 files changed

+52
-56
lines changed

4 files changed

+52
-56
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected ResetMonitorSpecificScalingExtension() {
2525

2626
@Override
2727
public void beforeEach(ExtensionContext context) throws Exception {
28-
wasMonitorSpecificScalingActive = Win32DPIUtils.isMonitorSpecificScalingActive();
28+
wasMonitorSpecificScalingActive = DPIUtil.isMonitorSpecificScalingActive();
2929
Display.getDefault().dispose();
3030
}
3131

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

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ public static Optional<AutoScaleMethod> forString(String s) {
5151

5252
private static String autoScaleValue;
5353

54+
private static final Set<String> ALLOWED_AUTOSCALE_VALUES_FOR_UPDATE_ON_RUNTIME = Set.of("quarter", "exact", "false");
55+
/**
56+
* System property to enable to scale the application on runtime
57+
* when a DPI change is detected.
58+
* <ul>
59+
* <li>"true": the application is scaled on DPI changes</li>
60+
* <li>"false": the application will remain in its initial scaling</li>
61+
* </ul>
62+
* <b>Important:</b> This flag is only parsed and used on Win32. Setting it to
63+
* true on GTK or cocoa will be ignored.
64+
*/
65+
static final String SWT_AUTOSCALE_UPDATE_ON_RUNTIME = "swt.autoScale.updateOnRuntime";
66+
5467
/**
5568
* System property that controls the autoScale functionality.
5669
* <ul>
@@ -89,20 +102,53 @@ public static Optional<AutoScaleMethod> forString(String s) {
89102

90103
static {
91104
autoScaleValue = System.getProperty (SWT_AUTOSCALE);
92-
105+
setUseSmoothScalingByDefaultProvider(() -> isMonitorSpecificScalingActive());
93106
String value = System.getProperty (SWT_AUTOSCALE_METHOD);
94107
AUTO_SCALE_METHOD_SETTING = AutoScaleMethod.forString(value).orElse(AutoScaleMethod.AUTO);
95108
autoScaleMethod = AUTO_SCALE_METHOD_SETTING != AutoScaleMethod.AUTO ? AUTO_SCALE_METHOD_SETTING : AutoScaleMethod.NEAREST;
96109
}
97110

98-
static String getAutoScaleValue() {
111+
public static String getAutoScaleValue() {
99112
return autoScaleValue;
100113
}
101114

102115
static void setAutoScaleValue(String autoScaleValueArg) {
103116
autoScaleValue = autoScaleValueArg;
104117
}
105118

119+
/**
120+
* Returns {@code true} only if the current setup is compatible
121+
* with monitor-specific scaling. Returns {@code false} if:
122+
* <ul>
123+
* <li>Not running on Windows</li>
124+
* <li>The current auto-scale mode is incompatible</li>
125+
* </ul>
126+
*
127+
* <p>Allowed values: {@code quarter}, {@code exact}.
128+
*
129+
*/
130+
public static boolean isSetupCompatibleToMonitorSpecificScaling() {
131+
if (DPIUtil.getAutoScaleValue() == null) {
132+
return false;
133+
}
134+
135+
if(ALLOWED_AUTOSCALE_VALUES_FOR_UPDATE_ON_RUNTIME.contains(DPIUtil.getAutoScaleValue().toLowerCase())) {
136+
return true;
137+
}
138+
try {
139+
Integer.parseInt(DPIUtil.getAutoScaleValue());
140+
return true;
141+
} catch (NumberFormatException e) {
142+
// unsupported value, use default
143+
}
144+
return false;
145+
}
146+
147+
public static boolean isMonitorSpecificScalingActive() {
148+
boolean updateOnRuntimeValue = Boolean.getBoolean (DPIUtil.SWT_AUTOSCALE_UPDATE_ON_RUNTIME);
149+
return updateOnRuntimeValue;
150+
}
151+
106152
public static int pixelToPoint(int size, int zoom) {
107153
if (zoom == 100 || size == SWT.DEFAULT) return size;
108154
float scaleFactor = getScalingFactor (zoom);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,6 @@
3333
* @noreference This class is not intended to be referenced by clients
3434
*/
3535
public class Win32DPIUtils {
36-
/**
37-
* System property to enable to scale the application on runtime
38-
* when a DPI change is detected.
39-
* <ul>
40-
* <li>"true": the application is scaled on DPI changes</li>
41-
* <li>"false": the application will remain in its initial scaling</li>
42-
* </ul>
43-
* <b>Important:</b> This flag is only parsed and used on Win32. Setting it to
44-
* true on GTK or cocoa will be ignored.
45-
*/
46-
private static final String SWT_AUTOSCALE_UPDATE_ON_RUNTIME = "swt.autoScale.updateOnRuntime";
47-
48-
static {
49-
DPIUtil.setUseSmoothScalingByDefaultProvider(() -> isMonitorSpecificScalingActive());
50-
}
5136

5237
public static boolean setDPIAwareness(int desiredDpiAwareness) {
5338
if (desiredDpiAwareness == OS.GetThreadDpiAwarenessContext()) {
@@ -302,55 +287,20 @@ public static Rectangle pointToPixel(Drawable drawable, Rectangle rect, int zoom
302287
}
303288

304289
public static void setMonitorSpecificScaling(boolean activate) {
305-
System.setProperty(SWT_AUTOSCALE_UPDATE_ON_RUNTIME, Boolean.toString(activate));
290+
System.setProperty(DPIUtil.SWT_AUTOSCALE_UPDATE_ON_RUNTIME, Boolean.toString(activate));
306291
}
307292

308293
public static void setAutoScaleForMonitorSpecificScaling() {
309294
boolean isDefaultAutoScale = DPIUtil.getAutoScaleValue() == null;
310295
if (isDefaultAutoScale) {
311296
DPIUtil.setAutoScaleValue("quarter");
312-
} else if (!isSupportedAutoScaleForMonitorSpecificScaling()) {
297+
} else if (!DPIUtil.isSetupCompatibleToMonitorSpecificScaling()) {
313298
throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED,
314299
"monitor-specific scaling is only implemented for auto-scale values \"quarter\", \"exact\", \"false\" or a concrete zoom value, but \""
315300
+ DPIUtil.getAutoScaleValue() + "\" has been specified");
316301
}
317302
}
318303

319-
/**
320-
* Monitor-specific scaling on Windows only supports auto-scale modes in which
321-
* all elements (font, images, control bounds etc.) are scaled equally or almost
322-
* equally. The previously default mode "integer"/"integer200", which rounded
323-
* the scale factor for everything but fonts to multiples of 100, is complex and
324-
* difficult to realize with monitor-specific rescaling of UI elements. Since a
325-
* uniform scale factor for everything should perspectively be used anyway,
326-
* there will be support for complex auto-scale modes for monitor-specific
327-
* scaling.
328-
*
329-
* The supported modes are "quarter" and "exact" or explicit zoom values given
330-
* by the value itself or "false". Every other value will be treated as
331-
* "integer"/"integer200" and is thus not supported.
332-
*/
333-
private static boolean isSupportedAutoScaleForMonitorSpecificScaling() {
334-
if (DPIUtil.getAutoScaleValue() == null) {
335-
return false;
336-
}
337-
switch (DPIUtil.getAutoScaleValue().toLowerCase()) {
338-
case "false", "quarter", "exact": return true;
339-
}
340-
try {
341-
Integer.parseInt(DPIUtil.getAutoScaleValue());
342-
return true;
343-
} catch (NumberFormatException e) {
344-
// unsupported value, use default
345-
}
346-
return false;
347-
}
348-
349-
public static boolean isMonitorSpecificScalingActive() {
350-
boolean updateOnRuntimeValue = Boolean.getBoolean (SWT_AUTOSCALE_UPDATE_ON_RUNTIME);
351-
return updateOnRuntimeValue;
352-
}
353-
354304
public static int getPrimaryMonitorZoomAtStartup() {
355305
long hDC = OS.GetDC(0);
356306
int dpi = OS.GetDeviceCaps(hDC, OS.LOGPIXELSX);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ public void close () {
949949
protected void create (DeviceData data) {
950950
checkSubclass ();
951951
checkDisplay (thread = Thread.currentThread (), true);
952-
if (Win32DPIUtils.isMonitorSpecificScalingActive()) {
952+
if (DPIUtil.isMonitorSpecificScalingActive()) {
953953
setMonitorSpecificScaling(true);
954954
Win32DPIUtils.setAutoScaleForMonitorSpecificScaling();
955955
}

0 commit comments

Comments
 (0)