Skip to content

Commit fadc48c

Browse files
ShahzaibIbrahimakoch-yatta
authored andcommitted
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 bcd5988 commit fadc48c

File tree

3 files changed

+57
-48
lines changed

3 files changed

+57
-48
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void testCorrectScaleUpUsingDifferentSetBoundsMethod() {
111111
}
112112

113113
@ParameterizedTest
114-
@CsvSource({ "0.5, 100, true", "1.0, 200, true", "2.0, 200, true", "2.0, quarter, true", "0.5, 100, false",
114+
@CsvSource({ "2.0, quarter, true", "0.5, 100, false",
115115
"1.0, 200, false", "2.0, 200, false", "2.0, quarter, false", })
116116
public void testAutoScaleImageData(float scaleFactor, String autoScale, boolean monitorSpecificScaling) {
117117
Win32DPIUtils.setMonitorSpecificScaling(monitorSpecificScaling);

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ public static Optional<AutoScaleMethod> forString(String s) {
5050
private static AutoScaleMethod autoScaleMethod;
5151

5252
private static String autoScaleValue;
53+
/**
54+
* System property to enable to scale the application on runtime
55+
* when a DPI change is detected.
56+
* <ul>
57+
* <li>"true": the application is scaled on DPI changes</li>
58+
* <li>"false": the application will remain in its initial scaling</li>
59+
* </ul>
60+
* <b>Important:</b> This flag is only parsed and used on Win32. Setting it to
61+
* true on GTK or cocoa will be ignored.
62+
*/
63+
static final String SWT_AUTOSCALE_UPDATE_ON_RUNTIME = "swt.autoScale.updateOnRuntime";
5364

5465
/**
5566
* System property that controls the autoScale functionality.
@@ -87,6 +98,14 @@ public static Optional<AutoScaleMethod> forString(String s) {
8798
*/
8899
private static final String SWT_AUTOSCALE_METHOD = "swt.autoScale.method";
89100

101+
/**
102+
* System property that enforces to use autoScale value despite incompatibility
103+
* For e.g. Monitor-specific scaling with int200 autoscale value
104+
*/
105+
private static final String SWT_AUTOSCALE_DISABLE_COMPATIBILITY_CHECK = "swt.autoScale.force";
106+
107+
private static final Set<String> ALLOWED_AUTOSCALE_VALUES_FOR_UPDATE_ON_RUNTIME = Set.of("quarter", "exact");
108+
90109
static {
91110
autoScaleValue = System.getProperty (SWT_AUTOSCALE);
92111

@@ -95,14 +114,47 @@ public static Optional<AutoScaleMethod> forString(String s) {
95114
autoScaleMethod = AUTO_SCALE_METHOD_SETTING != AutoScaleMethod.AUTO ? AUTO_SCALE_METHOD_SETTING : AutoScaleMethod.NEAREST;
96115
}
97116

98-
static String getAutoScaleValue() {
117+
public static String getAutoScaleValue() {
99118
return autoScaleValue;
100119
}
101120

102121
static void setAutoScaleValue(String autoScaleValueArg) {
103122
autoScaleValue = autoScaleValueArg;
104123
}
105124

125+
/**
126+
* Returns {@code true} only if the current setup is compatible
127+
* with monitor-specific scaling. Returns {@code false} if:
128+
* <ul>
129+
* <li>Not running on Windows</li>
130+
* <li>The current auto-scale mode is incompatible</li>
131+
* </ul>
132+
*
133+
* <p>Allowed values: {@code quarter}, {@code exact}.
134+
*
135+
*/
136+
public static boolean isSetupCompatibleToMonitorSpecificScaling() {
137+
// Per-monitor DPI supported only on Windows
138+
if (!"win32".equals(SWT.getPlatform())) {
139+
return false;
140+
}
141+
142+
// Default means: treat as "quarter" (compatible)
143+
if (autoScaleValue == null || "true".equalsIgnoreCase(System.getProperty(SWT_AUTOSCALE_DISABLE_COMPATIBILITY_CHECK))) {
144+
return true;
145+
}
146+
147+
String value = autoScaleValue.toLowerCase(Locale.ROOT);
148+
149+
// Compatible only if one of the known values
150+
return ALLOWED_AUTOSCALE_VALUES_FOR_UPDATE_ON_RUNTIME.contains(value);
151+
}
152+
153+
public static boolean isMonitorSpecificScalingActive() {
154+
boolean updateOnRuntimeValue = Boolean.getBoolean (DPIUtil.SWT_AUTOSCALE_UPDATE_ON_RUNTIME);
155+
return updateOnRuntimeValue;
156+
}
157+
106158
public static int pixelToPoint(int size, int zoom) {
107159
if (zoom == 100 || size == SWT.DEFAULT) return size;
108160
float scaleFactor = getScalingFactor (zoom);

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

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +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-
4836
static {
4937
DPIUtil.setUseSmoothScalingByDefaultProvider(() -> isMonitorSpecificScalingActive());
5038
}
@@ -302,53 +290,22 @@ public static Rectangle pointToPixel(Drawable drawable, Rectangle rect, int zoom
302290
}
303291

304292
public static void setMonitorSpecificScaling(boolean activate) {
305-
System.setProperty(SWT_AUTOSCALE_UPDATE_ON_RUNTIME, Boolean.toString(activate));
293+
System.setProperty(DPIUtil.SWT_AUTOSCALE_UPDATE_ON_RUNTIME, Boolean.toString(activate));
306294
}
307295

308296
public static void setAutoScaleForMonitorSpecificScaling() {
309297
boolean isDefaultAutoScale = DPIUtil.getAutoScaleValue() == null;
310298
if (isDefaultAutoScale) {
311299
DPIUtil.setAutoScaleValue("quarter");
312-
} else if (!isSupportedAutoScaleForMonitorSpecificScaling()) {
300+
} else if (!DPIUtil.isSetupCompatibleToMonitorSpecificScaling()) {
313301
throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED,
314302
"monitor-specific scaling is only implemented for auto-scale values \"quarter\", \"exact\", \"false\" or a concrete zoom value, but \""
315303
+ DPIUtil.getAutoScaleValue() + "\" has been specified");
316304
}
317305
}
318306

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-
349307
public static boolean isMonitorSpecificScalingActive() {
350-
boolean updateOnRuntimeValue = Boolean.getBoolean (SWT_AUTOSCALE_UPDATE_ON_RUNTIME);
351-
return updateOnRuntimeValue;
308+
return DPIUtil.isMonitorSpecificScalingActive();
352309
}
353310

354311
public static int getPrimaryMonitorZoomAtStartup() {

0 commit comments

Comments
 (0)