Fix #655: set macOS Dock icon on jar build on newer Java versions (#703)

* Add OS X resource forks and other stuff to gitignore

* fix #655, try to set Dock icon only if not using app bundle

* update CHANGELOG.md

* try to set macOS icon only when using jar bundle, as in app bundle macOS sets everything automatically

Co-authored-by: Ryan Dowling <ryan@ryandowling.me>
This commit is contained in:
Mihail Yaremenko 2023-01-12 15:37:08 +02:00 committed by GitHub
parent 4de829b976
commit 8174e3e64f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 10 deletions

8
.gitignore vendored
View file

@ -17,6 +17,14 @@ testLauncher/
# OS X # OS X
.DS_Store .DS_Store
._*
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# ACT # ACT
.actrc .actrc

View file

@ -8,5 +8,6 @@ This changelog only contains the changes that are unreleased. For changes for in
### New Features ### New Features
### Fixes ### Fixes
- Default Java icon in macOS Dock when using Java 9 or later [#655] (@WhiteBear60)
### Misc ### Misc

View file

@ -135,7 +135,7 @@ public class App {
/** /**
* This allows skipping the system tray integration so that the launcher doesn't * This allows skipping the system tray integration so that the launcher doesn't
* even try to show the icon and menu etc, in the users system tray. It can be * even try to show the icon and menu etc., in the users system tray. It can be
* skipped with the below command line argument. * skipped with the below command line argument.
* <p/> * <p/>
* --skip-tray-integration * --skip-tray-integration
@ -637,17 +637,27 @@ public class App {
} }
private static void setupOSSpecificThings() { private static void setupOSSpecificThings() {
// do some Mac specific stuff, setting the name of theapplication and icon // do some Mac specific stuff, setting the name of the application and icon
if (OS.isMac()) { // set only when using jar bundle, as if using *.app, macOS sets icon and name automatically and apple.laf.useScreenMenuBar is set using build.gradle
if (!OS.isUsingMacApp()) {
System.setProperty("apple.laf.useScreenMenuBar", "true"); System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", System.setProperty("apple.awt.application.name", Constants.LAUNCHER_NAME); // setting the application name in menu bar
Constants.LAUNCHER_NAME + " " + Constants.VERSION);
try { try {
Class<?> util = Class.forName("com.apple.eawt.Application"); if (Java.isSystemJavaNewerThanJava8()) {
Method getApplication = util.getMethod("getApplication"); // if Java 9 or higher
Object application = getApplication.invoke(util); Class<?> util = Class.forName("java.awt.Taskbar");
Method setDockIconImage = util.getMethod("setDockIconImage", Image.class); Method getTaskbar = util.getMethod("getTaskbar");
setDockIconImage.invoke(application, Utils.getImage("/assets/image/icon-osx.png")); Object taskbar = getTaskbar.invoke(util);
Method setIconImage = util.getMethod("setIconImage", Image.class);
setIconImage.invoke(taskbar, Utils.getImage("/assets/image/icon-osx.png"));
} else {
// if Java 8 or lower
Class<?> util = Class.forName("com.apple.eawt.Application");
Method getApplication = util.getMethod("getApplication");
Object application = getApplication.invoke(util);
Method setDockIconImage = util.getMethod("setDockIconImage", Image.class);
setDockIconImage.invoke(application, Utils.getImage("/assets/image/icon-osx.png"));
}
} catch (Exception ex) { } catch (Exception ex) {
LogManager.logStackTrace("Failed to set dock icon", ex); LogManager.logStackTrace("Failed to set dock icon", ex);
} }