|
42 | 42 | import org.scijava.plugin.Parameter; |
43 | 43 | import org.scijava.plugin.Plugin; |
44 | 44 |
|
| 45 | +import java.awt.GraphicsEnvironment; |
| 46 | +import java.awt.Toolkit; |
45 | 47 | import java.io.IOException; |
| 48 | +import java.lang.reflect.Field; |
46 | 49 | import java.net.URL; |
47 | 50 | import java.nio.charset.StandardCharsets; |
48 | 51 | import java.nio.file.Files; |
@@ -101,6 +104,8 @@ public String osName() { |
101 | 104 | public void configure(final PlatformService service) { |
102 | 105 | super.configure(service); |
103 | 106 |
|
| 107 | + applyWMClass(); |
| 108 | + |
104 | 109 | // Create or update .desktop file for desktop integration. |
105 | 110 | try { |
106 | 111 | installDesktopFile(); |
@@ -222,7 +227,7 @@ public void syncDesktopIntegration(final boolean webLinks, |
222 | 227 | df.setCategories(appCategories); |
223 | 228 | df.setTerminal(false); |
224 | 229 | df.setStartupNotify(true); |
225 | | - final String appWMClass = System.getProperty("sun.awt.wmclass"); |
| 230 | + final String appWMClass = System.getProperty("scijava.app.wmclass"); |
226 | 231 | if (appWMClass != null) df.setStartupWMClass(appWMClass); |
227 | 232 | final String appIcon = System.getProperty("scijava.app.icon"); |
228 | 233 | if (appIcon != null) df.setIcon(appIcon); |
@@ -362,6 +367,30 @@ private String sanitizeFileName(final String name) { |
362 | 367 | return name.replaceAll("[^a-zA-Z0-9._-]", "-").toLowerCase(); |
363 | 368 | } |
364 | 369 |
|
| 370 | + /** |
| 371 | + * Sets the AWT app class name via reflection so the running application's |
| 372 | + * windows carry the WM_CLASS specified by {@code scijava.app.wmclass}. |
| 373 | + * This must be called before any AWT windows are created. |
| 374 | + */ |
| 375 | + private void applyWMClass() { |
| 376 | + final String appWMClass = System.getProperty("scijava.app.wmclass"); |
| 377 | + if (appWMClass == null) return; |
| 378 | + if (GraphicsEnvironment.isHeadless()) return; |
| 379 | + try { |
| 380 | + final Toolkit toolkit = Toolkit.getDefaultToolkit(); |
| 381 | + if (toolkit == null) return; |
| 382 | + final Class<?> clazz = toolkit.getClass(); |
| 383 | + if (!"sun.awt.X11.XToolkit".equals(clazz.getName())) return; |
| 384 | + final Field field = clazz.getDeclaredField("awtAppClassName"); |
| 385 | + field.setAccessible(true); |
| 386 | + field.set(toolkit, appWMClass); |
| 387 | + if (log != null) log.debug("Set AWT app class name: " + appWMClass); |
| 388 | + } |
| 389 | + catch (final Throwable t) { |
| 390 | + if (log != null) log.debug("Could not set AWT app class name", t); |
| 391 | + } |
| 392 | + } |
| 393 | + |
365 | 394 | // -- Helper methods -- |
366 | 395 |
|
367 | 396 | private LinkService linkService() { |
|
0 commit comments