dyorgio / macos-tray-icon-fixer

Fix appearance of Java TrayIcon on Mojave, Catalina and BigSur
MIT License
5 stars 2 forks source link

Updating of TrayIcon image does not work #11

Open edmandiesamonte opened 2 years ago

edmandiesamonte commented 2 years ago

Sample Code:

public static void main(String[] args) throws AWTException {
    final var tray = SystemTray.getSystemTray();
    final var blackIcon = new ImageIcon(ResourceUtils.get("/tray/images/recording-dark.png")).getImage();
    final var whiteIcon = new ImageIcon(ResourceUtils.get("/tray/images/recording.png")).getImage();
    final var popUpMenu = new PopupMenu();
    popUpMenu.add("Hello World");
    final var trayIcon = new TrayIcon(
            MacOSTrayIconFixer.getInitialIcon(blackIcon, whiteIcon),
            "Tray Icon Test",
            popUpMenu
    );

    tray.add(trayIcon);
    MacOSTrayIconFixer.fix(trayIcon, blackIcon, whiteIcon);

    final var pausedBlackIcon = new ImageIcon(ResourceUtils.get("/tray/images/paused-dark.png")).getImage();
    final var pausedWhiteIcon = new ImageIcon(ResourceUtils.get("/tray/images/paused.png")).getImage();

    new Timer().schedule(new TimerTask() {
      @Override
      public void run() {
        SwingUtilities.invokeLater(()->{
          trayIcon.setImage(MacOSTrayIconFixer.getInitialIcon(pausedBlackIcon, pausedWhiteIcon));
        });
      }
    }, 5000);
  }

With the code above, the icon image does not get changed after 5000ms. If I comment out the MacOSTrayIconFixer.fix(trayIcon, blackIcon, whiteIcon);, the switching if icon image works just fine.

Am I just missing something?

I also tried to calling MacOSTrayIconFixer.fix again but it is not working too. It somehow has empty icon graphic but still alive in the menu bar.


OS Version: MacOS 11.5.2 Machine: Mac Mini (2018) Architecture: Intel Chipset

dyorgio commented 2 years ago

Hi @edmandiesamonte, take a look on last version (fixer and test).