Fix TrayIcon attachment lifecycle - #22118
Conversation
- Create the native tray icon only after it is attached to Application - Unsubscribe from the previous TrayIcons collection when it is replaced - Ensure tray icons cannot escape cleanup during application shutdown
|
You can test this PR using the following package version. |
|
You can test this PR using the following package version. |
|
The initial However, on macOS, Removing the initial visibility toggle makes the macOS integration test pass. |
|
Regardless, I believe the test should verify the relative ordering of the modal and parent windows rather than asserting an absolute |
|
@MrJul Could someone please rerun the failed macOS integration job from build 69251? I would like to determine whether the original failure is reproducible or was a flaky/environment-specific result. |
MrJul
left a comment
There was a problem hiding this comment.
I've only left a small style comment, otherwise the implementation itself looks very good to me.
|
|
||
| private void Attach() | ||
| { | ||
| if (_isAttached || _isDisposed) return; |
There was a problem hiding this comment.
Style nit: put the return on its own line. Applicable to all changes made in this file.
Yes, it probably should.
I've re-run it: https://dev.azure.com/AvaloniaUI/AvaloniaUI/_build/results?buildId=69251&view=results edit: and it passes. |
Avalonia/tests/Avalonia.IntegrationTests.Appium/WindowTests_MacOS.cs Lines 24 to 33 in 774f561 Avalonia/samples/IntegrationTestApp/ShowWindowTest.axaml.cs Lines 53 to 56 in 774f561 Yes, I think I've found the reason. The wait here is ineffective; it should wait first and then retrieve. |
|
You can test this PR using the following package version. |
|
On Windows and macOS there's no issue, the PR works as expected. On Linux, the app crashes everytime the application exits, in |
What does the pull request do?
Changes the
TrayIconlifecycle so that constructing aTrayIcondoes not immediately register it with the operating system.A native tray icon is now created only after the
TrayIconis attached to anApplicationthroughTrayIcon.Icons.What is the current behavior?
Constructing a
TrayIconimmediately creates its platform implementation.On macOS, creating the platform implementation also creates an
NSStatusItem, causing the icon to appear in the menu bar even whenTrayIcon.SetIconshas not been called and the icon has not been added to aTrayIconscollection.Because
IsVisibledefaults totrue, a tray icon may also be displayed before all of its properties have been initialized.Additionally:
TrayIconscollections remain subscribed toCollectionChanged.TrayIconscollection does not explicitly attach them.TrayIconscollection does not expose the removed items throughOldItems, so their platform implementations cannot be detached.What is the updated/expected behavior with this PR?
Constructing a
TrayIcononly creates the managed object. It does not create or display a native tray icon.The native platform implementation is created only when the
TrayIconis:TrayIconscollection assigned throughTrayIcon.SetIcons.TrayIconscollection.When attached, the previously configured icon, tooltip, menu, visibility, and macOS template-icon properties are synchronized with the newly created platform implementation.
When a tray icon is removed, the collection is cleared, or the collection is replaced, its platform implementation is detached and disposed.
All tray icons attached to the application are disposed during dispatcher shutdown.
How was the solution implemented (if it's not obvious)?
An explicit attach/detach lifecycle was added to
TrayIcon.TrayIcon.Iconsproperty changes attach icons from the new collection and detach icons from the previous collection.CollectionChangedwhen replaced.TrayIconsuses remove notifications when cleared so the removed icons are available for cleanup.Checklist
Fixed issues
Fixes #18548