dustinkredmond / FXTrayIcon

Tray Icon implementation for JavaFX applications. Say goodbye to using AWT's SystemTray icon, instead use a JavaFX Tray Icon.
MIT License
324 stars 25 forks source link

Icon resolution on OSX #16

Closed craigraw closed 3 years ago

craigraw commented 3 years ago

I suspect that there may be no way to improve this, but the resolution of the tray icon on OSX is much poorer than other icons in the tray.

With a JavaFX Image one can use the imagename@2x.png format to supply a larger image which will be selected on a higher DPI screen. Is there any way to improve the image resolution here?

dustinkredmond commented 3 years ago

@craigraw Talk about perfect timing. 😄

In the last hour, I released version 2.8.0 which provides an overloaded constructor to set the icon size.

public FXTrayIcon(Stage parentStage, URL iconImagePath, int iconWidth, int iconHeight)

Please check this out, and let me know if this resolves your issue. AWT doesn't provide a lot in terms of improving resolution otherwise.

craigraw commented 3 years ago

Good timing indeed! Unfortunately it doesn't really help though.

It does seem however that Java 9 and up supports higher DPI images through the MultiResolutionImage class which should be implemented by subclasses of java.awt.Image.

See the answer here: https://stackoverflow.com/questions/38802885/java-9-hdpi-display-support-multi-resolution-images-name-convention-and-load

Any chance of supporting this? (Possibly through an overloaded constructor where the Image is supplied)?

EasyG0ing1 commented 3 years ago

@craigraw I found that when I create icons following Apples guidelines then set the size to 24 x 24 in the instantiation of FXTrayIcon, it doesn't look too bad at all. It is slightly off from the other icons in the tray but you kind of have to look hard to see that.

dustinkredmond commented 3 years ago

@craigraw I was hoping to eliminate any AWT from the public API, I will look into this, though.

I have only done minimal testing on MacOS. I will review @EasyG0ing1's suggestion and decide how to move forward. Thank you for creating the Issue! 👍🏻

craigraw commented 3 years ago

FWIW I've tried the MultiResolutionImage approach, and it results in better defined icons on OSX. It also works on Windows (without having to resize). Ubuntu doesn't seem to support a system tray, and prints a GTK error even if you check for support.

dustinkredmond commented 3 years ago

@craigraw, the Ubuntu not supporting this is a known issue, at this time, due to the complexity of adding the support, we don't feel like it's something we want to address. See #12 for further details on this.

I'm glad that the MultiResolutionImage works for OSX and Windows. If you could, would you mind creating a pull request to add this functionality to FXTrayIcon? You could create an overloaded constructor, something like below:

public FXTrayIcon(Stage parentStage, MultiResolutionImage iconImage) {
    // call this(...)
}

If you don't have time to do this, I'll leave the issue open and work on doing it myself when my small amount of free time allows. 😅

craigraw commented 3 years ago

I found my needs were best handled by my own implementation, but essentially I'm just doing something similar to the following:

List<Image> imgList = new ArrayList<>();
imgList.add(ImageIO.read(getClass().getResource("/image.png")));
imgList.add(ImageIO.read(getClass().getResource("/image@2x.png")));
imgList.add(ImageIO.read(getClass().getResource("/image@3x.png")));
BaseMultiResolutionImage mrImage = new BaseMultiResolutionImage(imgList.toArray(new Image[0]));

this.trayIcon = new TrayIcon(mrImage, "App", popupMenu);

Here, image.png is 16x16px, image@2x.png is 32x32, and image@3x.png is 64x64.

Even though I didn't end up using FXTrayIcon, it was still valuable to reference. Thanks!

dustinkredmond commented 3 years ago

@craigraw, thanks for looking into it.

I will look into support for this once I migrate the project to use the latest JDK. A lot of folks using JavaFX still stick with Oracle's JDK8, simply out of convenience, so I won't look into this yet.

I'm glad, even if you're not using FXTrayIcon, that we were able to provide some good reference material for you! 🤣 In all seriousness, thanks for opening the issue, I will definitely add this to my TODO for the library.