X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fbin%2FHiDPISetting.java;h=4472a1904acb3e4058a4509d97134f39a0ad305d;hb=62d10e940e86310c996659dd729aa61f7fd516fd;hp=bea9daecd8c7d07a10feb190f7d6a3d32e2fd9a9;hpb=841509c5eccc4b4d9997897004bd916a85a30943;p=jalview.git diff --git a/src/jalview/bin/HiDPISetting.java b/src/jalview/bin/HiDPISetting.java index bea9dae..4472a19 100644 --- a/src/jalview/bin/HiDPISetting.java +++ b/src/jalview/bin/HiDPISetting.java @@ -4,11 +4,9 @@ import java.awt.Toolkit; public class HiDPISetting { - public static final int dpi; + private static final int hidpiThreshold = 130; - public static final int scale; - - private static final int hidpi = 110; + private static final int tallScreenThreshold = 1300; private static final String scalePropertyName = "sun.java2d.uiScale"; @@ -18,9 +16,25 @@ public class HiDPISetting private static final boolean isWindows; - public static final String forceHiDPISettingPropertyName = "forceHiDPISetting"; + public static final String setHiDPIPropertyName = "setHiDPI"; + + public static final String setHiDPIScalePropertyName = "setHiDPIScale"; + + private static boolean setHiDPI = false; + + private static int setHiDPIScale = 0; + + public static int dpi = 0; - private static final boolean forceHiDPISetting; + public static int maxdimension = 0; + + public static int width = 0; + + public static int scale = 0; + + private static boolean doneInit = false; + + private static boolean allowScalePropertyArg = false; static { @@ -29,34 +43,106 @@ public class HiDPISetting .indexOf("linux") > -1; isWindows = System.getProperty("os.name").toLowerCase() .indexOf("windows") > -1; - dpi = Toolkit.getDefaultToolkit().getScreenResolution(); - scale = dpi / hidpi + 1; - String forceHiDPISettingProperty = System - .getProperty(forceHiDPISettingPropertyName); - forceHiDPISetting = forceHiDPISettingProperty != null - && forceHiDPISettingProperty.equalsIgnoreCase("true"); - if (doCondition()) - { - System.out.println("Property '" + forceHiDPISettingPropertyName + "'=" - + forceHiDPISettingProperty); - System.out.println("boolean forceHiDPISetting=" + forceHiDPISetting); + } + + private static void init() + { + if (doneInit) + { + return; + } + + String setHiDPIProperty = System.getProperty(setHiDPIPropertyName); + setHiDPI = setHiDPIProperty != null + && setHiDPIProperty.equalsIgnoreCase("true"); + + String setHiDPIScaleProperty = System + .getProperty(setHiDPIScalePropertyName); + if (setHiDPIScaleProperty != null) + { + try + { + setHiDPIScale = Integer.parseInt(setHiDPIScaleProperty); + } catch (NumberFormatException e) + { + System.err.println(setHiDPIScalePropertyName + " property give (" + + setHiDPIScaleProperty + ") but not parseable as integer"); + } + } + + // 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 and width + try + { + int height = Toolkit.getDefaultToolkit().getScreenSize().height; + int width = Toolkit.getDefaultToolkit().getScreenSize().width; + // using maxdimension in case of portrait screens + maxdimension = Math.max(height, width); + } catch (Throwable t) + { + System.err.println("Cannot get screen size height and width"); + } + + if (setHiDPI && setHiDPIScale > 0) + { + scale = setHiDPIScale; + } + else + { + // attempt at a formula for scaling based on screen dpi and maxdimension. + // scale + // will be an integer >=1 + if (dpi > 0 && maxdimension > 0) + { + scale = Math.min(dpi / hidpiThreshold, + maxdimension / tallScreenThreshold) + 1; + } + else if (dpi == 0 && maxdimension > tallScreenThreshold) + { + // dpi couldn't be found but the screen has a large vertical pixel count + scale = maxdimension / tallScreenThreshold + 1; + } + /* do nothing if maxdimension == 0 -- might be just a small HD screen (e.g. Gemini PDA) + else if (maxdimension == 0 && dpi > hidpiThreshold) + { + } + */ + } + + // only make a change if scale is changed and other conditions (OS is linux) + // apply, or if setHiDPI has been specified + allowScalePropertyArg = (scale > 1 && isLinux) || setHiDPI; + + if (allowScalePropertyArg) + { + System.out.println("boolean setHiDPI=" + setHiDPI); System.out.println("DPI detected as " + dpi + ". Scaling factor set to " + scale + "."); } + + doneInit = true; } - private static synchronized boolean doCondition() + public static void setHiDPIScale(int s) { - return (scale > 1 && isLinux) || forceHiDPISetting; + scale = s; + allowScalePropertyArg = true; + doneInit = true; } - public static synchronized String getScalePropertyArg() + public static String getScalePropertyArg() { + init(); // HiDPI setting. Just looking at Linux to start with. Test with Windows. - if (doCondition()) - { - return "-D" + scalePropertyName + "=" + scale; - } - return null; + return allowScalePropertyArg ? "-D" + scalePropertyName + "=" + scale + : null; } }