Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
korffmo committed Dec 31, 2023
2 parents 0075fda + 724dfc1 commit eaf2fcb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Please follow the naming scheme YEAR.MONTH.RELEASE_NO_OF_MONTH
(eg. 2016.4.1 for second release in Apr 2016)
-->
<version>2023.12.3-SNAPSHOT</version>
<version>2023.12.4-SNAPSHOT</version>

<name>OpenChemLib</name>
<description>Open Source Chemistry Library</description>
Expand Down
59 changes: 40 additions & 19 deletions src/main/java/com/actelion/research/gui/hidpi/HiDPIHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Field;

public class HiDPIHelper {
Expand Down Expand Up @@ -65,33 +67,52 @@ public static float getRetinaScaleFactor() {
*/
public static float getUIScaleFactor() {
if (sUIScaleFactor == -1) {
if (getRetinaScaleFactor() != 1f)
sUIScaleFactor = 1f;
else {
float f = 0;
String dpiFactor = System.getProperty("dpifactor");
if (dpiFactor != null)
try { f = Float.parseFloat(dpiFactor); } catch (NumberFormatException nfe) {}
if (f != 0)
sUIScaleFactor = f;
else if (Platform.isMacintosh())
float f = 0;
String dpiFactor = System.getProperty("dpifactor");
if (dpiFactor != null)
try { f = Float.parseFloat(dpiFactor); } catch (NumberFormatException nfe) {}
if (f != 0)
sUIScaleFactor = f;
else if (Platform.isMacintosh()) {
sUIScaleFactor = 1.0f;
}
else if (Platform.isWindows()) {
try {
// with JRE8 we used (float)UIManager.getFont("Label.font").getSize() / 12f
sUIScaleFactor = Toolkit.getDefaultToolkit().getScreenResolution() / 96f;
}
catch (HeadlessException hle) {
sUIScaleFactor = 1.0f;
else {
try {
// with JRE8 we used (float)UIManager.getFont("Label.font").getSize() / 12f
sUIScaleFactor = Toolkit.getDefaultToolkit().getScreenResolution() / 96f;
if (sUIScaleFactor > 0.9f && sUIScaleFactor < 1.11f)
sUIScaleFactor = 1.0f;
}
catch (HeadlessException hle) {
sUIScaleFactor = 1.0f;
}
}
else { // Linux; Toolkit.getDefaultToolkit().getScreenResolution() always returns 1.0
try {
sUIScaleFactor = 1.0f; // default in case of error
Process process = Runtime.getRuntime().exec("xrdb -q");
process.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("Xft.dpi:")) {
int dpi = Integer.parseInt(line.substring(8).trim());
sUIScaleFactor = dpi / 96f;
break;
}
}
br.close();
}
catch (Exception ioe) {}
}
if (sUIScaleFactor < 1.1f)
sUIScaleFactor = 1.0f;
}
return sUIScaleFactor;
}

public static void setUIScaleFactor(float factor) {
sUIScaleFactor = factor;
}

/**
* This is a convenience method that scales the passed int value with getUIScaleFactor()
* and returns the rounded result.
Expand Down

0 comments on commit eaf2fcb

Please sign in to comment.