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 public class HiDPISetting
28 private static final int hidpiThreshold = 160;
30 private static final int hidpiMultiThreshold = 240;
32 private static final int bigScreenThreshold = 1400;
34 public static final String scalePropertyName = "sun.java2d.uiScale";
36 private static final boolean isLinux;
38 // private static final boolean isAMac;
40 // private static final boolean isWindows;
42 public static final String setHiDPIPropertyName = "setHiDPI";
44 public static final String setHiDPIScalePropertyName = "setHiDPIScale";
46 private static boolean setHiDPI = false;
48 private static int setHiDPIScale = 0;
50 public static int dpi = 0;
52 public static int mindimension = 0;
54 public static int width = 0;
56 public static int scale = 0;
58 public final static int MAX_SCALE = 8;
60 private static boolean doneInit = false;
62 private static boolean allowScalePropertyArg = false;
64 private static ScreenInfo screenInfo = new ScreenInfo();
68 String system = System.getProperty("os.name") == null ? null
69 : System.getProperty("os.name").toLowerCase(Locale.ROOT);
72 isLinux = system.indexOf("linux") > -1;
73 // isAMac = system.indexOf("mac") > -1;
74 // isWindows = system.indexOf("windows") > -1;
79 // isAMac = isWindows = false;
83 private static void init()
90 // get and use command line property values first
91 String setHiDPIProperty = System.getProperty(setHiDPIPropertyName);
92 boolean setHiDPIPropertyBool = Boolean.parseBoolean(setHiDPIProperty);
94 // allow -DsetHiDPI=false to turn off HiDPI scaling
95 if (setHiDPIProperty != null && !setHiDPIPropertyBool)
102 setHiDPI = setHiDPIProperty != null && setHiDPIPropertyBool;
104 String setHiDPIScaleProperty = System
105 .getProperty(setHiDPIScalePropertyName);
106 if (setHiDPIScaleProperty != null)
110 setHiDPIScale = Integer.parseInt(setHiDPIScaleProperty);
111 // if setHiDPIScale property is validly set and setHiDPI property wasn't
112 // attempted to be set we assume setHiDPIScale to be true
113 if (setHiDPIProperty == null)
117 } catch (NumberFormatException e)
119 jalview.bin.Console.errPrintln(setHiDPIScalePropertyName
120 + " property give (" + setHiDPIScaleProperty
121 + ") but not parseable as integer");
124 if (setHiDPI && setHiDPIScale > 0)
126 setHiDPIScale(setHiDPIScale);
130 // check to see if the scale property has already been set by something else
132 String existingProperty = System.getProperty(scalePropertyName);
133 if (existingProperty != null)
137 int existingPropertyVal = Integer.parseInt(existingProperty);
138 jalview.bin.Console.outPrintln("Existing " + scalePropertyName
139 + " is " + existingPropertyVal);
140 if (existingPropertyVal > 1)
142 setHiDPIScale(existingPropertyVal);
145 } catch (NumberFormatException e)
147 jalview.bin.Console.outPrintln(
148 "Could not convert property " + scalePropertyName
149 + " vale '" + existingProperty + "' to number");
153 // Try and auto guess a good scale based on reported DPI (not trustworthy)
154 // and screen resolution (more trustworthy)
157 screenInfo = getScreenInfo();
160 dpi = screenInfo.getScreenResolution();
161 } catch (HeadlessException e)
165 jalview.bin.Console.errPrintln(
166 "Cannot get screen resolution: " + e.getMessage());
170 // try and get screen size height and width
173 int height = screenInfo.getScreenHeight();
174 int width = screenInfo.getScreenWidth();
175 // using mindimension in case of portrait screens
176 mindimension = Math.min(height, width);
177 } catch (HeadlessException e)
182 .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();