2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.awt.HeadlessException;
24 import java.util.Locale;
26 import jalview.util.ErrorLog;
28 public class HiDPISetting
30 private static final int hidpiThreshold = 160;
32 private static final int hidpiMultiThreshold = 240;
34 private static final int bigScreenThreshold = 1400;
36 public static final String scalePropertyName = "sun.java2d.uiScale";
38 private static final boolean isLinux;
40 // private static final boolean isAMac;
42 // private static final boolean isWindows;
44 public static final String setHiDPIPropertyName = "setHiDPI";
46 public static final String setHiDPIScalePropertyName = "setHiDPIScale";
48 private static boolean setHiDPI = false;
50 private static int setHiDPIScale = 0;
52 public static int dpi = 0;
54 public static int mindimension = 0;
56 public static int width = 0;
58 public static int scale = 0;
60 public final static int MAX_SCALE = 8;
62 private static boolean doneInit = false;
64 private static boolean allowScalePropertyArg = false;
66 private static ScreenInfo screenInfo = new ScreenInfo();
70 String system = System.getProperty("os.name") == null ? null
71 : System.getProperty("os.name").toLowerCase(Locale.ROOT);
74 isLinux = system.indexOf("linux") > -1;
75 // isAMac = system.indexOf("mac") > -1;
76 // isWindows = system.indexOf("windows") > -1;
81 // isAMac = isWindows = false;
85 private static void init()
92 // get and use command line property values first
93 String setHiDPIProperty = System.getProperty(setHiDPIPropertyName);
94 boolean setHiDPIPropertyBool = Boolean.parseBoolean(setHiDPIProperty);
96 // allow -DsetHiDPI=false to turn off HiDPI scaling
97 if (setHiDPIProperty != null && !setHiDPIPropertyBool)
104 setHiDPI = setHiDPIProperty != null && setHiDPIPropertyBool;
106 String setHiDPIScaleProperty = System
107 .getProperty(setHiDPIScalePropertyName);
108 if (setHiDPIScaleProperty != null)
112 setHiDPIScale = Integer.parseInt(setHiDPIScaleProperty);
113 // if setHiDPIScale property is validly set and setHiDPI property wasn't
114 // attempted to be set we assume setHiDPIScale to be true
115 if (setHiDPIProperty == null)
119 } catch (NumberFormatException e)
121 ErrorLog.errPrintln(setHiDPIScalePropertyName + " property give ("
122 + setHiDPIScaleProperty + ") but not parseable as integer");
125 if (setHiDPI && setHiDPIScale > 0)
127 setHiDPIScale(setHiDPIScale);
131 // check to see if the scale property has already been set by something else
133 String existingProperty = System.getProperty(scalePropertyName);
134 if (existingProperty != null)
138 int existingPropertyVal = Integer.parseInt(existingProperty);
139 ErrorLog.outPrintln("Existing " + scalePropertyName + " is "
140 + existingPropertyVal);
141 if (existingPropertyVal > 1)
143 setHiDPIScale(existingPropertyVal);
146 } catch (NumberFormatException e)
149 "Could not convert property " + scalePropertyName
150 + " vale '" + existingProperty + "' to number");
154 // Try and auto guess a good scale based on reported DPI (not trustworthy)
155 // and screen resolution (more trustworthy)
158 screenInfo = getScreenInfo();
161 dpi = screenInfo.getScreenResolution();
162 } catch (HeadlessException e)
167 "Cannot get screen resolution: " + e.getMessage());
171 // try and get screen size height and width
174 int height = screenInfo.getScreenHeight();
175 int width = screenInfo.getScreenWidth();
176 // using mindimension in case of portrait screens
177 mindimension = Math.min(height, width);
178 } catch (HeadlessException e)
182 ErrorLog.errPrintln("Cannot get screen size height and width:"
187 // attempt at a formula for scaling based on screen dpi and mindimension.
188 // scale will be an integer >=1. This formula is based on some testing and
191 // scale based on reported dpi. if dpi>hidpiThreshold then scale=2+multiples
192 // of hidpiMultiThreshold (else scale=1)
193 // (e.g. dpi of 110 scales 1. dpi of 120 scales 2. dpi of 360 scales 3)
194 int dpiScale = (dpi - hidpiThreshold > 0)
195 ? 2 + ((dpi - hidpiThreshold) / hidpiMultiThreshold)
198 int dimensionScale = 1 + (mindimension / bigScreenThreshold);
200 // reject outrageous values -- dpiScale in particular could be mistaken
201 if (dpiScale > MAX_SCALE)
205 if (dimensionScale > MAX_SCALE)
210 // choose larger of dimensionScale or dpiScale (most likely dimensionScale
211 // as dpiScale often misreported)
212 int autoScale = Math.max(dpiScale, dimensionScale);
214 // only make an automatic change if scale is changed and other conditions
215 // (OS is linux) apply, or if setHiDPI has been specified
216 if ((autoScale > 1 && isLinux) || setHiDPI)
218 setHiDPIScale(autoScale);
222 // looks like we're not doing any scaling
226 public static void setHiDPIScale(int s)
229 allowScalePropertyArg = true;
233 public static String getScalePropertyArg(int s)
235 return "-D" + scalePropertyName + "=" + String.valueOf(s);
238 public static String getScalePropertyArg()
241 // HiDPI setting. Just looking at Linux to start with. Test with Windows.
242 return allowScalePropertyArg ? getScalePropertyArg(scale) : null;
245 public static void clear()
254 allowScalePropertyArg = false;
257 public static void setScreenInfo(ScreenInfo si)
262 public static ScreenInfo getScreenInfo()
264 if (screenInfo == null)
266 screenInfo = new ScreenInfo();