Skip to content

Commit 7df9a79

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 7df9a79

File tree

2 files changed

+65
-16
lines changed

2 files changed

+65
-16
lines changed

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

Lines changed: 63 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,57 @@ 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 false}, {@code quarter}, {@code exact}.
134+
*
135+
*/
136+
public static boolean isSetupCompatibleToMonitorSpecificScaling() {
137+
if("true".equalsIgnoreCase(System.getProperty(SWT_AUTOSCALE_DISABLE_COMPATIBILITY_CHECK))) {
138+
return true;
139+
}
140+
// Per-monitor DPI supported only on Windows and when monitor
141+
if (!"win32".equals(SWT.getPlatform())) {
142+
return false;
143+
}
144+
145+
// Default means: treat as "quarter" (compatible)
146+
if (autoScaleValue == null) {
147+
return true;
148+
}
149+
150+
String value = autoScaleValue.toLowerCase(Locale.ROOT);
151+
152+
// Compatible only if one of the known values
153+
return ALLOWED_AUTOSCALE_VALUES_FOR_UPDATE_ON_RUNTIME.contains(value);
154+
}
155+
156+
public static void setAutoScaleForMonitorSpecificScaling() {
157+
boolean isDefaultAutoScale = DPIUtil.getAutoScaleValue() == null;
158+
if (isDefaultAutoScale) {
159+
DPIUtil.setAutoScaleValue("quarter");
160+
}
161+
}
162+
163+
public static boolean isMonitorSpecificScalingActive() {
164+
boolean updateOnRuntimeValue = Boolean.getBoolean (DPIUtil.SWT_AUTOSCALE_UPDATE_ON_RUNTIME);
165+
return updateOnRuntimeValue;
166+
}
167+
106168
public static int pixelToPoint(int size, int zoom) {
107169
if (zoom == 100 || size == SWT.DEFAULT) return size;
108170
float scaleFactor = getScalingFactor (zoom);

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

Lines changed: 2 additions & 15 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
}
@@ -278,7 +266,7 @@ public static Rectangle pointToPixel(Drawable drawable, Rectangle rect, int zoom
278266
}
279267

280268
public static void setMonitorSpecificScaling(boolean activate) {
281-
System.setProperty(SWT_AUTOSCALE_UPDATE_ON_RUNTIME, Boolean.toString(activate));
269+
System.setProperty(DPIUtil.SWT_AUTOSCALE_UPDATE_ON_RUNTIME, Boolean.toString(activate));
282270
}
283271

284272
public static void setAutoScaleForMonitorSpecificScaling() {
@@ -323,8 +311,7 @@ private static boolean isSupportedAutoScaleForMonitorSpecificScaling() {
323311
}
324312

325313
public static boolean isMonitorSpecificScalingActive() {
326-
boolean updateOnRuntimeValue = Boolean.getBoolean (SWT_AUTOSCALE_UPDATE_ON_RUNTIME);
327-
return updateOnRuntimeValue;
314+
return DPIUtil.isMonitorSpecificScalingActive();
328315
}
329316

330317
public static int getPrimaryMonitorZoomAtStartup() {

0 commit comments

Comments
 (0)