Give the window its own identity in Linux task bars
AWT names the X11 window class after the thread that first touched the toolkit, so the client shared java-awt-EventDispatchThread with every other Swing app. Set it to ts3j-client instead (the jar's manifest opens the AWT internals this needs), and install a matching .desktop launcher and icon in the user's data directory: without a launcher claiming the class, Plasma falls back to the process's command line and files the window under Java. The window icon is now offered at several sizes instead of 16 px only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
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}.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user