3ff683e2eee4cde076e712c8306b547ccc8c84eb
[jalview.git] / src / 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 = 1400;
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 setHiDPIPropertyName = "setHiDPI";
20
21   public static final String setHiDPIScalePropertyName = "setHiDPIScale";
22
23   private static boolean setHiDPI = false;
24
25   private static int setHiDPIScale = 0;
26
27   public static int dpi = 0;
28
29   public static int mindimension = 0;
30
31   public static int width = 0;
32
33   public static int scale = 0;
34
35   private static boolean doneInit = false;
36
37   private static boolean allowScalePropertyArg = false;
38
39   static
40   {
41     isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
42     isLinux = System.getProperty("os.name").toLowerCase()
43             .indexOf("linux") > -1;
44     isWindows = System.getProperty("os.name").toLowerCase()
45             .indexOf("windows") > -1;
46   }
47
48   private static void init()
49   {
50     if (doneInit)
51     {
52       return;
53     }
54
55     String setHiDPIProperty = System.getProperty(setHiDPIPropertyName);
56     setHiDPI = setHiDPIProperty != null
57             && setHiDPIProperty.equalsIgnoreCase("true");
58
59     String setHiDPIScaleProperty = System
60             .getProperty(setHiDPIScalePropertyName);
61     if (setHiDPIScaleProperty != null)
62     {
63       try
64       {
65         setHiDPIScale = Integer.parseInt(setHiDPIScaleProperty);
66       } catch (NumberFormatException e)
67       {
68         System.err.println(setHiDPIScalePropertyName + " property give ("
69                 + setHiDPIScaleProperty + ") but not parseable as integer");
70       }
71     }
72
73     // try and get screen resolution
74     try
75     {
76       dpi = Toolkit.getDefaultToolkit().getScreenResolution();
77     } catch (Throwable t)
78     {
79       System.err.println("Cannot get screen resolution");
80     }
81
82     // try and get screen size height and width
83     try
84     {
85       int height = Toolkit.getDefaultToolkit().getScreenSize().height;
86       int width = Toolkit.getDefaultToolkit().getScreenSize().width;
87       // using mindimension in case of portrait screens
88       mindimension = Math.min(height, width);
89     } catch (Throwable t)
90     {
91       System.err.println("Cannot get screen size height and width");
92     }
93
94     if (setHiDPI && setHiDPIScale > 0)
95     {
96       scale = setHiDPIScale;
97     }
98     else
99     {
100       // attempt at a formula for scaling based on screen dpi and mindimension.
101       // scale
102       // will be an integer >=1
103       if (dpi > 0 && mindimension > 0)
104       {
105         scale = Math.min(dpi / hidpiThreshold,
106                 mindimension / tallScreenThreshold) + 1;
107       }
108       else if (dpi == 0 && mindimension > tallScreenThreshold)
109       {
110         // dpi couldn't be found but the screen has a large vertical pixel count
111         scale = mindimension / tallScreenThreshold + 1;
112       }
113       /* do nothing if mindimension == 0 -- might be just a small HD screen (e.g. Gemini PDA)
114       else if (mindimension == 0 && dpi > hidpiThreshold)
115       {
116       }
117       */
118     }
119
120     // only make a change if scale is changed and other conditions (OS is linux)
121     // apply, or if setHiDPI has been specified
122     allowScalePropertyArg = (scale > 1 && isLinux) || setHiDPI;
123
124     if (allowScalePropertyArg)
125     {
126       System.out.println("boolean setHiDPI=" + setHiDPI);
127       System.out.println("DPI detected as " + dpi
128               + ". Scaling factor set to " + scale + ".");
129     }
130
131     doneInit = true;
132   }
133
134   public static void setHiDPIScale(int s)
135   {
136     scale = s;
137     allowScalePropertyArg = true;
138     doneInit = true;
139   }
140
141   public static String getScalePropertyArg()
142   {
143     init();
144     // HiDPI setting. Just looking at Linux to start with. Test with Windows.
145     return allowScalePropertyArg ? "-D" + scalePropertyName + "=" + scale
146             : null;
147   }
148 }