JAL-3609 changed property names to something more palatable and catered for portrait...
[jalview.git] / getdown / src / getdown / core / src / main / java / jalview / bin / HiDPISetting.java
index 5e20d76..4472a19 100644 (file)
@@ -4,45 +4,145 @@ import java.awt.Toolkit;
 
 public class HiDPISetting
 {
-  public static final int dpi;
+  private static final int hidpiThreshold = 130;
 
-  public static final int scale;
+  private static final int tallScreenThreshold = 1300;
 
-  private static final int hidpi = 110;
+  private static final String scalePropertyName = "sun.java2d.uiScale";
 
-  private static final String scaleProperty = "sun.java2d.uiScale";
+  private static final boolean isAMac;
 
-  private static final boolean isAMac = System.getProperty("os.name")
-          .indexOf("Mac") > -1;
+  private static final boolean isLinux;
 
-  private static final boolean isLinux = System.getProperty("os.name")
-          .toLowerCase().indexOf("linux") > -1;
+  private static final boolean isWindows;
 
-  private static final boolean isWindows = System.getProperty("os.name")
-          .toLowerCase().indexOf("windows") > -1;
+  public static final String setHiDPIPropertyName = "setHiDPI";
+
+  public static final String setHiDPIScalePropertyName = "setHiDPIScale";
+
+  private static boolean setHiDPI = false;
+
+  private static int setHiDPIScale = 0;
+
+  public static int dpi = 0;
+
+  public static int maxdimension = 0;
+
+  public static int width = 0;
+
+  public static int scale = 0;
+
+  private static boolean doneInit = false;
+
+  private static boolean allowScalePropertyArg = false;
 
   static
   {
-    dpi = Toolkit.getDefaultToolkit().getScreenResolution();
-    scale = dpi / hidpi + 1;
-    if (scale > 1 && isLinux)
+    isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
+    isLinux = System.getProperty("os.name").toLowerCase()
+            .indexOf("linux") > -1;
+    isWindows = System.getProperty("os.name").toLowerCase()
+            .indexOf("windows") > -1;
+  }
+
+  private static void init()
+  {
+    if (doneInit)
+    {
+      return;
+    }
+
+    String setHiDPIProperty = System.getProperty(setHiDPIPropertyName);
+    setHiDPI = setHiDPIProperty != null
+            && setHiDPIProperty.equalsIgnoreCase("true");
+
+    String setHiDPIScaleProperty = System
+            .getProperty(setHiDPIScalePropertyName);
+    if (setHiDPIScaleProperty != null)
     {
+      try
+      {
+        setHiDPIScale = Integer.parseInt(setHiDPIScaleProperty);
+      } catch (NumberFormatException e)
+      {
+        System.err.println(setHiDPIScalePropertyName + " property give ("
+                + setHiDPIScaleProperty + ") but not parseable as integer");
+      }
+    }
+
+    // try and get screen resolution
+    try
+    {
+      dpi = Toolkit.getDefaultToolkit().getScreenResolution();
+    } catch (Throwable t)
+    {
+      System.err.println("Cannot get screen resolution");
+    }
+
+    // try and get screen size height and width
+    try
+    {
+      int height = Toolkit.getDefaultToolkit().getScreenSize().height;
+      int width = Toolkit.getDefaultToolkit().getScreenSize().width;
+      // using maxdimension in case of portrait screens
+      maxdimension = Math.max(height, width);
+    } catch (Throwable t)
+    {
+      System.err.println("Cannot get screen size height and width");
+    }
+
+    if (setHiDPI && setHiDPIScale > 0)
+    {
+      scale = setHiDPIScale;
+    }
+    else
+    {
+      // attempt at a formula for scaling based on screen dpi and maxdimension.
+      // scale
+      // will be an integer >=1
+      if (dpi > 0 && maxdimension > 0)
+      {
+        scale = Math.min(dpi / hidpiThreshold,
+                maxdimension / tallScreenThreshold) + 1;
+      }
+      else if (dpi == 0 && maxdimension > tallScreenThreshold)
+      {
+        // dpi couldn't be found but the screen has a large vertical pixel count
+        scale = maxdimension / tallScreenThreshold + 1;
+      }
+      /* do nothing if maxdimension == 0 -- might be just a small HD screen (e.g. Gemini PDA)
+      else if (maxdimension == 0 && dpi > hidpiThreshold)
+      {
+      }
+      */
+    }
+
+    // only make a change if scale is changed and other conditions (OS is linux)
+    // apply, or if setHiDPI has been specified
+    allowScalePropertyArg = (scale > 1 && isLinux) || setHiDPI;
+
+    if (allowScalePropertyArg)
+    {
+      System.out.println("boolean setHiDPI=" + setHiDPI);
       System.out.println("DPI detected as " + dpi
               + ". Scaling factor set to " + scale + ".");
     }
+
+    doneInit = true;
+  }
+
+  public static void setHiDPIScale(int s)
+  {
+    scale = s;
+    allowScalePropertyArg = true;
+    doneInit = true;
   }
 
   public static String getScalePropertyArg()
   {
+    init();
     // HiDPI setting. Just looking at Linux to start with. Test with Windows.
-    if (!isLinux)
-    {
-      return null;
-    }
-    if (scale > 1)
-    {
-      return "-D" + scaleProperty + "=" + scale;
-    }
-    return null;
+    return allowScalePropertyArg ? "-D" + scalePropertyName + "=" + scale
+            : null;
   }
 }