JAL-3609 system properties to force a scale, or automatically choose based on OS...
[jalview.git] / getdown / src / getdown / core / src / main / java / jalview / bin / HiDPISetting.java
1 package jalview.bin;
2
3 import java.awt.Toolkit;
4
5 public class HiDPISetting
6 {
7   private static final int hidpiThreshold = 130;
8
9   private static final int tallScreenThreshold = 1300;
10
11   private static final String scalePropertyName = "sun.java2d.uiScale";
12
13   private static final boolean isAMac;
14
15   private static final boolean isLinux;
16
17   private static final boolean isWindows;
18
19   public static final String forceHiDPISettingPropertyName = "forceHiDPISetting";
20
21   public static final String forceHiDPISettingScalePropertyName = "forceHiDPISettingScale";
22
23   private static final boolean forceHiDPISetting;
24
25   private static final int forceHiDPISettingScale;
26
27   public static int dpi = 0;
28
29   public static int height = 0;
30
31   public static int scale = 0;
32
33   private static boolean doneInit = false;
34
35   private static boolean allowScalePropertyArg = false;
36
37   static
38   {
39     isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
40     isLinux = System.getProperty("os.name").toLowerCase()
41             .indexOf("linux") > -1;
42     isWindows = System.getProperty("os.name").toLowerCase()
43             .indexOf("windows") > -1;
44
45     String forceHiDPISettingProperty = System
46             .getProperty(forceHiDPISettingPropertyName);
47     forceHiDPISetting = forceHiDPISettingProperty != null
48             && forceHiDPISettingProperty.equalsIgnoreCase("true");
49
50     int tryForceHiDPISettingScale = 0;
51     String forceHiDPISettingScaleProperty = System
52             .getProperty(forceHiDPISettingScalePropertyName);
53     if (forceHiDPISettingScaleProperty != null)
54     {
55       try
56       {
57         tryForceHiDPISettingScale = Integer
58                 .parseInt(forceHiDPISettingScaleProperty);
59       } catch (NumberFormatException e)
60       {
61         System.err.println(forceHiDPISettingScalePropertyName
62                 + " property give (" + forceHiDPISettingScaleProperty
63                 + ") but not parseable as integer");
64       }
65     }
66     forceHiDPISettingScale = tryForceHiDPISettingScale;
67   }
68
69   private void init()
70   {
71     if (doneInit)
72     {
73       return;
74     }
75
76     // try and get screen resolution
77     try
78     {
79       dpi = Toolkit.getDefaultToolkit().getScreenResolution();
80     } catch (Throwable t)
81     {
82       System.err.println("Cannot get screen resolution");
83     }
84
85     // try and get screen size height
86     try
87     {
88       height = Toolkit.getDefaultToolkit().getScreenSize().height;
89     } catch (Throwable t)
90     {
91       System.err.println("Cannot get screen size height");
92     }
93
94     if (forceHiDPISetting && forceHiDPISettingScale > 0)
95     {
96       scale = forceHiDPISettingScale;
97     }
98     else
99     {
100       // attempt at a formula for scaling based on screen dpi and height. scale
101       // will be an integer >=1
102       scale = Math.min(dpi / hidpiThreshold, height / tallScreenThreshold)
103               + 1;
104     }
105
106     allowScalePropertyArg = (scale > 1 && isLinux) || forceHiDPISetting;
107
108     if (allowScalePropertyArg)
109     {
110       System.out.println("boolean forceHiDPISetting=" + forceHiDPISetting);
111       System.out.println("DPI detected as " + dpi
112               + ". Scaling factor set to " + scale + ".");
113     }
114
115     doneInit = true;
116   }
117
118   public static synchronized String getScalePropertyArg()
119   {
120     // HiDPI setting. Just looking at Linux to start with. Test with Windows.
121     return allowScalePropertyArg ? "-D" + scalePropertyName + "=" + scale
122             : null;
123   }
124 }