changeset 10952:8551b51ba769 11+16

8191661: FXCanvas on Win32 HiDPI produces wrong results Summary: [swt] Use DPIUtil to get the scale-factor SWT is running in Reviewed-by: kcr
author tschindl
date Mon, 25 Jun 2018 10:58:17 -0700
parents cc52395f2e3e
children c95b14b3d133
files modules/javafx.swt/src/main/java/javafx/embed/swt/FXCanvas.java
diffstat 1 files changed, 34 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/modules/javafx.swt/src/main/java/javafx/embed/swt/FXCanvas.java	Thu Jun 21 14:29:20 2018 -0700
+++ b/modules/javafx.swt/src/main/java/javafx/embed/swt/FXCanvas.java	Mon Jun 25 10:58:17 2018 -0700
@@ -184,6 +184,16 @@
             } catch (Exception e) {
                 // FAIL silently should the reflection fail
             }
+        } else if (SWT.getPlatform().equals("win32")) {
+            if (swtDPIUtilMethod == null) {
+                return 1.0;
+            }
+            try {
+                Integer value = (Integer) swtDPIUtilMethod.invoke(null);
+                return value.intValue() / 100.0;
+            } catch (Exception e) {
+                // FAIL silently should the reflection fail
+            }
         }
         return 1.0;
     }
@@ -225,6 +235,7 @@
     private static Method windowMethod;
     private static Method screenMethod;
     private static Method backingScaleFactorMethod;
+    private static Method swtDPIUtilMethod;
 
     static {
         if (SWT.getPlatform().equals("cocoa")) {
@@ -246,6 +257,16 @@
             } catch (Exception e) {
                 //Fail silently.  If we can't get the methods, then the current version of SWT has no retina support
             }
+        } else if (SWT.getPlatform().equals("win32")) {
+            try {
+                String autoScale = AccessController.doPrivileged((PrivilegedAction<String>)() -> System.getProperty("swt.autoScale"));
+                if (autoScale == null || ! "false".equalsIgnoreCase(autoScale)) {
+                    Class dpiUtilClass = Class.forName("org.eclipse.swt.internal.DPIUtil");
+                    swtDPIUtilMethod = dpiUtilClass.getMethod("getDeviceZoom");
+                }
+            } catch (Exception e) {
+                //Fail silently.  If we can't get the methods, then the current version of SWT has no retina support
+            }
         }
         initFx();
     }
@@ -311,8 +332,19 @@
         AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
             System.setProperty("com.sun.javafx.application.type", "FXCanvas");
             System.setProperty("javafx.embed.isEventThread", "true");
-            System.setProperty("glass.win.uiScale", "100%");
-            System.setProperty("glass.win.renderScale", "100%");
+            if (swtDPIUtilMethod == null) {
+                System.setProperty("glass.win.uiScale", "100%");
+                System.setProperty("glass.win.renderScale", "100%");
+            } else {
+                Integer scale = 100;
+                try {
+                    scale = (Integer) swtDPIUtilMethod.invoke(null);
+                } catch (Exception e) {
+                    //Fail silently
+                }
+                System.setProperty("glass.win.uiScale", scale + "%");
+                System.setProperty("glass.win.renderScale", scale + "%");
+            }
             System.setProperty("javafx.embed.eventProc", eventProcStr);
             return null;
         });