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 System.err.println(setHiDPIScalePropertyName + " property give ("
120 + setHiDPIScaleProperty + ") but not parseable as integer");
123 if (setHiDPI && setHiDPIScale > 0)
125 setHiDPIScale(setHiDPIScale);
129 // check to see if the scale property has already been set by something else
131 String existingProperty = System.getProperty(scalePropertyName);
132 if (existingProperty != null)
136 int existingPropertyVal = Integer.parseInt(existingProperty);
137 System.out.println("Existing " + scalePropertyName + " is "
138 + existingPropertyVal);
139 if (existingPropertyVal > 1)
141 setHiDPIScale(existingPropertyVal);
144 } catch (NumberFormatException e)
146 System.out.println("Could not convert property " + scalePropertyName
147 + " vale '" + existingProperty + "' to number");
151 // Try and auto guess a good scale based on reported DPI (not trustworthy)
152 // and screen resolution (more trustworthy)
155 screenInfo = getScreenInfo();
158 dpi = screenInfo.getScreenResolution();
159 } catch (HeadlessException e)
164 .println("Cannot get screen resolution: " + e.getMessage());
168 // try and get screen size height and width
171 int height = screenInfo.getScreenHeight();
172 int width = screenInfo.getScreenWidth();
173 // using mindimension in case of portrait screens
174 mindimension = Math.min(height, width);
175 } catch (HeadlessException e)
179 System.err.println("Cannot get screen size height and width:"
184 // attempt at a formula for scaling based on screen dpi and mindimension.
185 // scale will be an integer >=1. This formula is based on some testing and
188 // scale based on reported dpi. if dpi>hidpiThreshold then scale=2+multiples
189 // of hidpiMultiThreshold (else scale=1)
190 // (e.g. dpi of 110 scales 1. dpi of 120 scales 2. dpi of 360 scales 3)
191 int dpiScale = (dpi - hidpiThreshold > 0)
192 ? 2 + ((dpi - hidpiThreshold) / hidpiMultiThreshold)
195 int dimensionScale = 1 + (mindimension / bigScreenThreshold);
197 // reject outrageous values -- dpiScale in particular could be mistaken
198 if (dpiScale > MAX_SCALE)
202 if (dimensionScale > MAX_SCALE)
207 // choose larger of dimensionScale or dpiScale (most likely dimensionScale
208 // as dpiScale often misreported)
209 int autoScale = Math.max(dpiScale, dimensionScale);
211 // only make an automatic change if scale is changed and other conditions
212 // (OS is linux) apply, or if setHiDPI has been specified
213 if ((autoScale > 1 && isLinux) || setHiDPI)
215 setHiDPIScale(autoScale);
219 // looks like we're not doing any scaling
223 public static void setHiDPIScale(int s)
226 allowScalePropertyArg = true;
230 public static String getScalePropertyArg(int s)
232 return "-D" + scalePropertyName + "=" + String.valueOf(s);
235 public static String getScalePropertyArg()
238 // HiDPI setting. Just looking at Linux to start with. Test with Windows.
239 return allowScalePropertyArg ? getScalePropertyArg(scale) : null;
242 public static void clear()
251 allowScalePropertyArg = false;
254 public static void setScreenInfo(ScreenInfo si)
259 public static ScreenInfo getScreenInfo()
261 if (screenInfo == null)
263 screenInfo = new ScreenInfo();