package jalview.bin; import java.awt.Toolkit; public class HiDPISetting { private static final int hidpiThreshold = 130; private static final int tallScreenThreshold = 1300; private static final String scalePropertyName = "sun.java2d.uiScale"; private static final boolean isAMac; private static final boolean isLinux; private static final boolean isWindows; public static final String forceHiDPISettingPropertyName = "forceHiDPISetting"; public static final String forceHiDPISettingScalePropertyName = "forceHiDPISettingScale"; private static final boolean forceHiDPISetting; private static final int forceHiDPISettingScale; public static int dpi = 0; public static int height = 0; public static int scale = 0; private static boolean doneInit = false; private static boolean allowScalePropertyArg = false; static { isAMac = System.getProperty("os.name").indexOf("Mac") > -1; isLinux = System.getProperty("os.name").toLowerCase() .indexOf("linux") > -1; isWindows = System.getProperty("os.name").toLowerCase() .indexOf("windows") > -1; String forceHiDPISettingProperty = System .getProperty(forceHiDPISettingPropertyName); forceHiDPISetting = forceHiDPISettingProperty != null && forceHiDPISettingProperty.equalsIgnoreCase("true"); int tryForceHiDPISettingScale = 0; String forceHiDPISettingScaleProperty = System .getProperty(forceHiDPISettingScalePropertyName); if (forceHiDPISettingScaleProperty != null) { try { tryForceHiDPISettingScale = Integer .parseInt(forceHiDPISettingScaleProperty); } catch (NumberFormatException e) { System.err.println(forceHiDPISettingScalePropertyName + " property give (" + forceHiDPISettingScaleProperty + ") but not parseable as integer"); } } forceHiDPISettingScale = tryForceHiDPISettingScale; } private void init() { if (doneInit) { return; } // try and get screen resolution try { dpi = Toolkit.getDefaultToolkit().getScreenResolution(); } catch (Throwable t) { System.err.println("Cannot get screen resolution"); } // try and get screen size height try { height = Toolkit.getDefaultToolkit().getScreenSize().height; } catch (Throwable t) { System.err.println("Cannot get screen size height"); } if (forceHiDPISetting && forceHiDPISettingScale > 0) { scale = forceHiDPISettingScale; } else { // attempt at a formula for scaling based on screen dpi and height. scale // will be an integer >=1 scale = Math.min(dpi / hidpiThreshold, height / tallScreenThreshold) + 1; } allowScalePropertyArg = (scale > 1 && isLinux) || forceHiDPISetting; if (allowScalePropertyArg) { System.out.println("boolean forceHiDPISetting=" + forceHiDPISetting); System.out.println("DPI detected as " + dpi + ". Scaling factor set to " + scale + "."); } doneInit = true; } public static synchronized String getScalePropertyArg() { // HiDPI setting. Just looking at Linux to start with. Test with Windows. return allowScalePropertyArg ? "-D" + scalePropertyName + "=" + scale : null; } }