Java2D on X11 (and so under XWayland) truncates its scale to a whole number, drawing 150 % at 100 %. On Linux, pin Java2D to 1 and hand the full factor to FlatLaf, taken from XSETTINGS Xft/DPI, KWin's Xwayland scale or GDK_SCALE, or chosen in Options → Design. The client's own sizes now go through UIScale: borders, gaps, dialog and row sizes, icons (rendered at the scaled size, so they stay crisp), custom-painted components and font sizes in the HTML views. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
135 lines
5.0 KiB
Java
135 lines
5.0 KiB
Java
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.
|
|
*
|
|
* <p>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.
|
|
*
|
|
* <p>The desktop's factor is read the way an X11 toolkit would learn it:
|
|
* <ol>
|
|
* <li>{@code Xft/DPI} from XSETTINGS — GNOME and Plasma (through xsettingsd) publish
|
|
* the device-pixel DPI there, fractional scale and text scaling included;</li>
|
|
* <li>KWin's {@code [Xwayland] Scale}, which Plasma sets when X11 apps are left to
|
|
* scale themselves — for sessions with no XSETTINGS daemon running;</li>
|
|
* <li>{@code GDK_SCALE}, which Java2D would otherwise have honoured itself.</li>
|
|
* </ol>
|
|
* With the compositor scaling X11 apps (GNOME's default, and Plasma's "Scaled by the
|
|
* system") the client sees 96 DPI and is drawn at 100 %, which is what those
|
|
* modes expect; they then upscale the window, blurry but the right size.
|
|
*
|
|
* <p>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<String> lines, String group, String key) {
|
|
boolean inGroup = false;
|
|
for (String line : lines) {
|
|
String t = line.trim();
|
|
if (t.startsWith("[")) {
|
|
inGroup = t.equals("[" + group + "]");
|
|
} else if (inGroup) {
|
|
int eq = t.indexOf('=');
|
|
if (eq > 0 && t.substring(0, eq).trim().equals(key)) {
|
|
return t.substring(eq + 1).trim();
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static double parseDouble(String s) {
|
|
if (s == null) return 0;
|
|
try {
|
|
return Double.parseDouble(s.trim());
|
|
} catch (NumberFormatException e) {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
private static boolean isLinux() {
|
|
return System.getProperty("os.name", "").toLowerCase(Locale.ROOT).startsWith("linux");
|
|
}
|
|
}
|