package com.ts3client.ui; import java.awt.GraphicsEnvironment; import java.awt.Toolkit; import java.lang.reflect.Field; import java.util.Locale; /** * Gives the client's windows their own X11 {@code WM_CLASS}. * *
AWT names it after the class at the bottom of the stack that first touched the * toolkit — for a Swing app that is usually {@code java-awt-EventDispatchThread}, * which every other Java program shares. Task bars group windows and look up their * launcher by that class (Plasma does so on Wayland too, through Xwayland), so the * client ended up filed under "Java" together with unrelated programs. * *
The name lives in a private field of {@code sun.awt.X11.XToolkit}; reaching it * needs {@code --add-opens java.desktop/sun.awt.X11=ALL-UNNAMED}, which the jar's * manifest grants. Without it the default name stays. */ public final class WindowClass { /** Matches {@code StartupWMClass} in a launcher's {@code .desktop} file. */ public static final String NAME = "ts3j-client"; private WindowClass() { } /** Call before the first window is created. */ public static void apply() { if (!System.getProperty("os.name", "").toLowerCase(Locale.ROOT).startsWith("linux") || GraphicsEnvironment.isHeadless()) { return; } Toolkit toolkit = Toolkit.getDefaultToolkit(); if (!toolkit.getClass().getName().equals("sun.awt.X11.XToolkit")) return; try { Field field = toolkit.getClass().getDeclaredField("awtAppClassName"); field.setAccessible(true); field.set(null, NAME); } catch (ReflectiveOperationException | RuntimeException e) { System.err.println("Cannot set the window class (run with " + "--add-opens java.desktop/sun.awt.X11=ALL-UNNAMED): " + e); } } }