Skip to content

Commit c287e90

Browse files
ShahzaibIbrahimHeikoKlare
authored andcommitted
Move methods from Win32DPIUtil to DPIUtil for autoscaling
Move some methods from Win32DPIUtil to DPIUtil to make them available OS-independently such that they can be accessed from OS-independent code.
1 parent d4ddd4c commit c287e90

File tree

4 files changed

+63
-52
lines changed

4 files changed

+63
-52
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: 58 additions & 1 deletion
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,7 +102,6 @@ public static Optional<AutoScaleMethod> forString(String s) {
89102

90103
static {
91104
autoScaleValue = System.getProperty (SWT_AUTOSCALE);
92-
93105
String value = System.getProperty (SWT_AUTOSCALE_METHOD);
94106
AUTO_SCALE_METHOD_SETTING = AutoScaleMethod.forString(value).orElse(AutoScaleMethod.AUTO);
95107
autoScaleMethod = AUTO_SCALE_METHOD_SETTING != AutoScaleMethod.AUTO ? AUTO_SCALE_METHOD_SETTING : AutoScaleMethod.NEAREST;
@@ -103,6 +115,51 @@ static void setAutoScaleValue(String autoScaleValueArg) {
103115
autoScaleValue = autoScaleValueArg;
104116
}
105117

118+
/**
119+
* Returns {@code true} only if the current setup is compatible
120+
* with monitor-specific scaling. Returns {@code false} if:
121+
* <ul>
122+
* <li>Not running on Windows</li>
123+
* <li>The current auto-scale mode is incompatible</li>
124+
* </ul>
125+
*
126+
* The supported auto-scale modes are "quarter" and "exact" or explicit zoom values given
127+
* by the value itself or "false". Every other value will be treated as
128+
* "integer"/"integer200" and is thus not supported.
129+
*
130+
* <p>
131+
* <b>Background information:</b>
132+
* Monitor-specific scaling on Windows only supports auto-scale modes in which
133+
* all elements (font, images, control bounds etc.) are scaled equally or almost
134+
* equally. The previously default mode "integer"/"integer200", which rounded
135+
* the scale factor for everything but fonts to multiples of 100, is complex and
136+
* difficult to realize with monitor-specific rescaling of UI elements. Since a
137+
* uniform scale factor for everything should perspectively be used anyway,
138+
* there will be no support for complex auto-scale modes for monitor-specific
139+
* scaling.
140+
*/
141+
public static boolean isSetupCompatibleToMonitorSpecificScaling() {
142+
if (DPIUtil.getAutoScaleValue() == null) {
143+
return false;
144+
}
145+
146+
if (ALLOWED_AUTOSCALE_VALUES_FOR_UPDATE_ON_RUNTIME.contains(DPIUtil.getAutoScaleValue().toLowerCase())) {
147+
return true;
148+
}
149+
try {
150+
Integer.parseInt(DPIUtil.getAutoScaleValue());
151+
return true;
152+
} catch (NumberFormatException e) {
153+
// unsupported value, use default
154+
}
155+
return false;
156+
}
157+
158+
public static boolean isMonitorSpecificScalingActive() {
159+
boolean updateOnRuntimeValue = Boolean.getBoolean (DPIUtil.SWT_AUTOSCALE_UPDATE_ON_RUNTIME);
160+
return updateOnRuntimeValue;
161+
}
162+
106163
public static int pixelToPoint(int size, int zoom) {
107164
if (zoom == 100 || size == SWT.DEFAULT) return size;
108165
float scaleFactor = getScalingFactor (zoom);

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

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,9 @@
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";
4736

4837
static {
49-
DPIUtil.setUseSmoothScalingByDefaultProvider(() -> isMonitorSpecificScalingActive());
38+
DPIUtil.setUseSmoothScalingByDefaultProvider(() -> DPIUtil.isMonitorSpecificScalingActive());
5039
}
5140

5241
public static boolean setDPIAwareness(int desiredDpiAwareness) {
@@ -302,55 +291,20 @@ public static Rectangle pointToPixel(Drawable drawable, Rectangle rect, int zoom
302291
}
303292

304293
public static void setMonitorSpecificScaling(boolean activate) {
305-
System.setProperty(SWT_AUTOSCALE_UPDATE_ON_RUNTIME, Boolean.toString(activate));
294+
System.setProperty(DPIUtil.SWT_AUTOSCALE_UPDATE_ON_RUNTIME, Boolean.toString(activate));
306295
}
307296

308297
public static void setAutoScaleForMonitorSpecificScaling() {
309298
boolean isDefaultAutoScale = DPIUtil.getAutoScaleValue() == null;
310299
if (isDefaultAutoScale) {
311300
DPIUtil.setAutoScaleValue("quarter");
312-
} else if (!isSupportedAutoScaleForMonitorSpecificScaling()) {
301+
} else if (!DPIUtil.isSetupCompatibleToMonitorSpecificScaling()) {
313302
throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED,
314303
"monitor-specific scaling is only implemented for auto-scale values \"quarter\", \"exact\", \"false\" or a concrete zoom value, but \""
315304
+ DPIUtil.getAutoScaleValue() + "\" has been specified");
316305
}
317306
}
318307

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-
354308
public static int getPrimaryMonitorZoomAtStartup() {
355309
long hDC = OS.GetDC(0);
356310
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)