Mac Tray Icon Question

Philip Race philip.race at oracle.com
Wed Aug 10 15:41:53 UTC 2022


This was implemented in JDK 17 as 
https://bugs.openjdk.org/browse/JDK-8252015
You need to set that property yourself and then ALL your images for 
TrayIcon had better
all be template images else it will likely look very bad ..

It was done this way to avoid "accidentally" interpretation of images as 
template images
Personally I wonder if the definition of template images is simple and 
stable we could
just scan the images to see if they meet the criteria and then mark them 
as template
images automatically .. but very clearly the developer would need to 
know the rules
for template images and supply these for mac. There's nothing I can 
think of which
would allow someone writing a Java app on Windows and not a mac aware 
developer
to have this magically done for them ...

-phil.

On 8/10/22 7:47 AM, Alan Snyder wrote:
> It appears that CTrayIcon has an option to use template images.
>
> apple.awt.enableTemplateImages
>
> So, either this option is off or your image does not satisfy the rules 
> for template images.
>
>
>
>
>> On Aug 10, 2022, at 7:28 AM, Jeremy Wood <mickleness at gmail.com> wrote:
>>
>> I like that theory, thanks. Is it possible to make/assign a template 
>> image in Java?
>>
>> Glancing around just now didn’t turn up any leads. Specifically:
>>
>> In CTrayIcon#updateNativeImage(Image), we call:
>> CImage.getCreator().createFromImage(image, observer)
>>
>> … and that method appears to convert ‘image’ (a java.awt.Image) into 
>> a TYPE_INT_ARGB_PRE formatted int array. The word “template” isn’t in 
>> the CImage class at all. So even if I had the imagePtr I don’t see 
>> how I can toggle the template attribute.
>>
>> Regards,
>>  - Jeremy
>>
>> ------ Original Message ------
>> From "Alan Snyder" <javalists at cbfiddle.com>
>> To "Jeremy Wood" <mickleness at gmail.com>
>> Cc "client-libs-dev at openjdk.org" <client-libs-dev at openjdk.org>
>> Date 8/10/2022 8:25:11 AM
>> Subject Re: Mac Tray Icon Question
>>
>>> I suspect the images that work are template images, rather than 
>>> pairs of images.
>>>
>>> See 
>>> https://developer.apple.com/documentation/appkit/nsimage/1520017-template
>>>
>>>
>>>
>>>> On Aug 9, 2022, at 7:12 PM, Jeremy Wood <mickleness at gmail.com> wrote:
>>>>
>>>> On Macs the tray can present black or white icons, depending on the 
>>>> color of the desktop underneath the menubar.
>>>>
>>>> For example:
>>>>
>>>> <galpmnvf.png>
>>>>
>>>> <qtqoidmn.png>
>>>>
>>>> Most icons switch from a black icon to a white icon as needed. A 
>>>> few 3rd party app icons do not switch to a white icon against the 
>>>> black background (for ex: Evernote). And interestingly: Apple’s 
>>>> Airdrop icon (the concentric circles shaped like a sideways pacman) 
>>>> also appears to do the wrong thing too.
>>>>
>>>> Is there a way in a Java desktop app to set up a tray icon that can 
>>>> toggle between white and black icons as needed?
>>>>
>>>> Obviously we’d love it if this “just worked” without further 
>>>> intervention by inverting our icon, but we’d also be happy if a 
>>>> solution involved adding a PropertyChangeListener somewhere to 
>>>> identify the menubar change and we could update the tray icons 
>>>> ourselves.
>>>>
>>>> For ex:
>>>> If I change the highlight/accent color (in “System Preferences -> 
>>>> General”), then 8 UIManager Colors change (including 
>>>> “textHighlight”, “Button.light”, etc.) as of JDK 18. As far as I 
>>>> can tell: there is no similar model to identify when the menubar 
>>>> changes colors.
>>>>
>>>> For now we’ve made a work-around by using the Robot class to grab a 
>>>> pixel from the Apple menu icon every few seconds to determine if 
>>>> it’s light or dark.
>>>>
>>>> Is there an existing way to resolve this we don’t know about?
>>>>
>>>> Or if not: is anyone reading this with “author” status interested 
>>>> in helping write this up as an openjdk bug?
>>>>
>>>> Regards,
>>>>  - Jeremy
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/client-libs-dev/attachments/20220810/125b9757/attachment.htm>


More information about the client-libs-dev mailing list