package com.ts3client.ui; import java.awt.Toolkit; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import java.util.Locale; /** * Decides how large the user interface is drawn on Linux, where Java2D only knows * whole-number scale factors: asked for 150 %, it draws at 100 % — or, told * 2 by the desktop, at 200 %, while FlatLaf's own scaling goes on top of that. * *
So on Linux Java2D is pinned to 1 and the whole factor, fractional or not, is * handed to FlatLaf, which scales fonts and its own metrics; the client scales its own * sizes through FlatLaf's {@link com.formdev.flatlaf.util.UIScale} to match. * *
The desktop's factor is read the way an X11 toolkit would learn it: *
A {@code sun.java2d.uiScale} or {@code flatlaf.uiScale} given on the command line
* turns all of this off.
*/
public final class DisplayScale {
private static final String JAVA2D_SCALE = "sun.java2d.uiScale";
private static final String FLATLAF_SCALE = "flatlaf.uiScale";
private static final double BASE_DPI = 96;
private static final double MAX_SCALE = 4;
private static boolean managed;
private static double detected = 1;
private DisplayScale() {
}
/**
* Sets the scale up; call before anything touches AWT.
*
* @param override the user's chosen factor, or 0 to follow the desktop
*/
public static void configure(double override) {
if (!isLinux() || System.getProperty(JAVA2D_SCALE) != null
|| System.getProperty(FLATLAF_SCALE) != null) {
return;
}
System.setProperty(JAVA2D_SCALE, "1");
managed = true;
detected = detect();
double factor = override > 0 ? override : detected;
System.setProperty(FLATLAF_SCALE, Double.toString(factor));
}
/** Whether the client sets the scale itself, so that the user may choose one. */
public static boolean isManaged() {
return managed;
}
/** The desktop's factor, as found at startup. */
public static double detected() {
return detected;
}
private static double detect() {
double scale = xftScale();
if (scale <= 0) scale = kwinXwaylandScale();
scale = Math.max(scale, gdkScale());
return Math.max(1, Math.min(MAX_SCALE, scale));
}
private static double xftScale() {
// The XSETTINGS value is in 1024ths of a DPI; -1 means "unset".
Object dpi = Toolkit.getDefaultToolkit().getDesktopProperty("gnome.Xft/DPI");
return dpi instanceof Integer d && d > 0 ? d / 1024.0 / BASE_DPI : 0;
}
private static double kwinXwaylandScale() {
// Left behind by a Wayland session, the key would be stale in an X11 one.
if (System.getenv("WAYLAND_DISPLAY") == null) return 0;
String configHome = System.getenv("XDG_CONFIG_HOME");
Path kwinrc = (configHome == null || configHome.isEmpty()
? Path.of(System.getProperty("user.home"), ".config")
: Path.of(configHome)).resolve("kwinrc");
try {
return parseDouble(iniValue(Files.readAllLines(kwinrc), "Xwayland", "Scale"));
} catch (IOException e) {
return 0;
}
}
private static double gdkScale() {
return parseDouble(System.getenv("GDK_SCALE"));
}
/** The value of {@code key} in {@code [group]} of a KConfig-style file, or {@code null}. */
static String iniValue(List