Skip to content

Fix hidden Win32 TrayIcon appearing after icon updates - #22125

Merged
MrJul merged 1 commit into
AvaloniaUI:mainfrom
Linlccc:fix-windows-trayicon-unexpectedly-displayed
Sep 2, 2026
Merged

Fix hidden Win32 TrayIcon appearing after icon updates#22125
MrJul merged 1 commit into
AvaloniaUI:mainfrom
Linlccc:fix-windows-trayicon-unexpectedly-displayed

Conversation

@Linlccc

@Linlccc Linlccc commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

What does the pull request do?

Preserves the current Win32 tray icon registration state when its icon is updated.

A hidden TrayIcon now remains hidden until SetIsVisible(true) is called.

What is the current behavior?

TrayIconImpl.SetIcon calls UpdateIcon() without considering whether the tray icon is currently registered.

When _iconAdded is false, UpdateIcon() executes Shell_NotifyIcon(NIM.ADD, ...), causing a hidden or initializing TrayIcon to unexpectedly appear in the Windows system tray.

For example, updating the icon after hiding it can make it visible again:

trayIcon.IsVisible = false;
trayIcon.Icon = anotherIcon;

What is the updated/expected behavior with this PR?

Updating the icon preserves the current registration state:

  • A hidden tray icon remains hidden.
  • The tray icon is added only when SetIsVisible(true) is called.

To test the change:

  1. Create and attach a TrayIcon.
  2. Set IsVisible to false.
  3. Change its Icon.
  4. Verify that it does not appear in the Windows system tray.
  5. Set IsVisible to true and verify that it appears.

How was the solution implemented (if it's not obvious)?

UpdateIcon(!_iconAdded);

When the tray icon is already registered, it is updated normally. When it is not registered, updating its icon does not add it to the system tray.

…urrent tray icon registration state when updating its icon, so a hidden TrayIcon remains hidden until SetIsVisible(true) is called.
@MrJul MrJul added bug os-windows backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Sep 1, 2026
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.2.999-cibuild0069313-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@MrJul MrJul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@MrJul
MrJul added this pull request to the merge queue Sep 2, 2026
Merged via the queue into AvaloniaUI:main with commit 76ff633 Sep 2, 2026
11 checks passed
MrJul pushed a commit to MrJul/Avalonia that referenced this pull request Sep 2, 2026
…urrent tray icon registration state when updating its icon, so a hidden TrayIcon remains hidden until SetIsVisible(true) is called. (AvaloniaUI#22125)
@MrJul MrJul added backported-12.1.x and removed backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Sep 2, 2026
@Linlccc
Linlccc deleted the fix-windows-trayicon-unexpectedly-displayed branch September 3, 2026 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants