Skip to content

Commit f01daf7

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 7d649c8 commit f01daf7

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()) {
@@ -278,55 +263,20 @@ public static Rectangle pointToPixel(Drawable drawable, Rectangle rect, int zoom
278263
}
279264

280265
public static void setMonitorSpecificScaling(boolean activate) {
281-
System.setProperty(SWT_AUTOSCALE_UPDATE_ON_RUNTIME, Boolean.toString(activate));
266+
System.setProperty(DPIUtil.SWT_AUTOSCALE_UPDATE_ON_RUNTIME, Boolean.toString(activate));
282267
}
283268

284269
public static void setAutoScaleForMonitorSpecificScaling() {
285270
boolean isDefaultAutoScale = DPIUtil.getAutoScaleValue() == null;
286271
if (isDefaultAutoScale) {
287272
DPIUtil.setAutoScaleValue("quarter");
288-
} else if (!isSupportedAutoScaleForMonitorSpecificScaling()) {
273+
} else if (!DPIUtil.isSetupCompatibleToMonitorSpecificScaling()) {
289274
throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED,
290275
"monitor-specific scaling is only implemented for auto-scale values \"quarter\", \"exact\", \"false\" or a concrete zoom value, but \""
291276
+ DPIUtil.getAutoScaleValue() + "\" has been specified");
292277
}
293278
}
294279

295-
/**
296-
* Monitor-specific scaling on Windows only supports auto-scale modes in which
297-
* all elements (font, images, control bounds etc.) are scaled equally or almost
298-
* equally. The previously default mode "integer"/"integer200", which rounded
299-
* the scale factor for everything but fonts to multiples of 100, is complex and
300-
* difficult to realize with monitor-specific rescaling of UI elements. Since a
301-
* uniform scale factor for everything should perspectively be used anyway,
302-
* there will be support for complex auto-scale modes for monitor-specific
303-
* scaling.
304-
*
305-
* The supported modes are "quarter" and "exact" or explicit zoom values given
306-
* by the value itself or "false". Every other value will be treated as
307-
* "integer"/"integer200" and is thus not supported.
308-
*/
309-
private static boolean isSupportedAutoScaleForMonitorSpecificScaling() {
310-
if (DPIUtil.getAutoScaleValue() == null) {
311-
return false;
312-
}
313-
switch (DPIUtil.getAutoScaleValue().toLowerCase()) {
314-
case "false", "quarter", "exact": return true;
315-
}
316-
try {
317-
Integer.parseInt(DPIUtil.getAutoScaleValue());
318-
return true;
319-
} catch (NumberFormatException e) {
320-
// unsupported value, use default
321-
}
322-
return false;
323-
}
324-
325-
public static boolean isMonitorSpecificScalingActive() {
326-
boolean updateOnRuntimeValue = Boolean.getBoolean (SWT_AUTOSCALE_UPDATE_ON_RUNTIME);
327-
return updateOnRuntimeValue;
328-
}
329-
330280
public static int getPrimaryMonitorZoomAtStartup() {
331281
long hDC = OS.GetDC(0);
332282
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)