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";
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;
.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())
+
+ 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("Property '" + forceHiDPISettingPropertyName + "'="
- + forceHiDPISettingProperty);
System.out.println("boolean forceHiDPISetting=" + forceHiDPISetting);
System.out.println("DPI detected as " + dpi
+ ". Scaling factor set to " + scale + ".");
}
- }
- private static synchronized boolean doCondition()
- {
- return (scale > 1 && isLinux) || forceHiDPISetting;
+ doneInit = true;
}
public static synchronized String getScalePropertyArg()
{
// 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;
}
}
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";
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;
.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())
+
+ 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("Property '" + forceHiDPISettingPropertyName + "'="
- + forceHiDPISettingProperty);
System.out.println("boolean forceHiDPISetting=" + forceHiDPISetting);
System.out.println("DPI detected as " + dpi
+ ". Scaling factor set to " + scale + ".");
}
- }
- private static synchronized boolean doCondition()
- {
- return (scale > 1 && isLinux) || forceHiDPISetting;
+ doneInit = true;
}
public static synchronized String getScalePropertyArg()
{
// 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;
}
}