Skip to content

Conversation

memorydream
Copy link
Contributor

@memorydream memorydream commented Sep 30, 2025

What does the pull request do?

When set tray icon IsVisible property from true to false , destroy the tray icon and path handler connection method handler be remove , but then set to true do not re add they in CreateTrayIcon.

The PR add missing logic , should not crash now.


I try add a unit test for the pr but i just found it already exists TryIconTests.Can_Toggle_TrayIcon_Visibility , but i can not enable it.

Can manual test it use IntegrationTestApp DesktopPage

What is the current behavior?

  1. Create a tray icon
  2. icon.IsVisible = false
  3. icon.IsVisible = true
  4. Application crash

What is the updated/expected behavior with this PR?

Do not crash

Checklist

Breaking changes

Should not

Fixed issues

Fixes #19332
Fixes #18156

@avaloniaui-bot
Copy link

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

@cla-avalonia
Copy link
Collaborator

cla-avalonia commented Sep 30, 2025

  • All contributors have signed the CLA.

@memorydream
Copy link
Contributor Author

@cla-avalonia agree

@maxkatz6 maxkatz6 requested review from jmacato and kekekeks October 1, 2025 00:00
@maxkatz6 maxkatz6 added bug os-linux backport-candidate-11.3.x Consider this PR for backporting to 11.3 branch labels Oct 1, 2025
@memorydream
Copy link
Contributor Author

memorydream commented Oct 1, 2025

This tray icon impl might has other issue , if i set tooltip text when not visible , because path handler already removed form StatusNotifierItemDbusObj , it crash

Should fix it in this PR?

@d-khalid
Copy link

d-khalid commented Oct 6, 2025

I tested this PR with a basic app that lets you toggle a TrayIcon's visiblity.

My Configuration:

  • OS: Fedora 42
  • DE: KDE Plasma 6.4.5 on Wayland
  • Avalonia Version: 12.0.999-cibuild0059109-alpha (from the Nuget feed)

Test Code (MainWindow.axaml.cs, MainWindow.axaml only has one button)

using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Media.Imaging;
using Avalonia.Platform;

namespace IconTest.Views;

public partial class MainWindow : Window
{
    private TrayIcon _trayIcon;

    public MainWindow()
    {
        InitializeComponent();
        SetupTrayIcon();
    }

    private void SetupTrayIcon()
    {
        _trayIcon = new TrayIcon
        {
            Icon = new WindowIcon(new Bitmap(AssetLoader.Open(new Uri("avares://IconTest/Assets/avalonia-logo.ico")))),
            ToolTipText = "IconTest",
            IsVisible = true
        };

        var menu = new NativeMenu();
        var item1 = new NativeMenuItem("Item 1");
        var item2 = new NativeMenuItem("Item 2");
        menu.Add(item1);
        menu.Add(item2);


        _trayIcon.Menu = menu;
    }

    private void ToggleTrayIcon(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        _trayIcon.IsVisible = !_trayIcon.IsVisible;
        ToggleButton.Content = _trayIcon.IsVisible ? "Hide Tray Icon" : "Show Tray Icon";
    }
}

Result:
Showing and hiding the TrayIcon doesn't cause any crashes or exceptions. Works smoothly.

I also tried the same app on Avalonia version 11.3.6 where it threw a NullReferenceException when making the TrayIcon visible again after hiding it.

So I believe does fix the bug.

Test Screenshots:
Initial
Screenshot_20251006_224238

Hiding the TrayIcon
Screenshot_20251006_224246

Showing the Trayicon Again
Screenshot_20251006_224256

Screenshot of Exception on 11.3.6:
Screenshot_20251006_221627

i hope this helps

@memorydream
Copy link
Contributor Author

I push a new commit for other issue , same as first commit , i dont know how add test for it.
Any suggestions please tell me.

@avaloniaui-bot
Copy link

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

@memorydream
Copy link
Contributor Author

await _statusNotifierWatcher.RegisterStatusNotifierItemAsync(_sysTrayServiceName);

There is only register but in DestroyTrayIcon no unregister , is problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-candidate-11.3.x Consider this PR for backporting to 11.3 branch bug os-linux

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Working with TrayIcon in KDE causes NullPointerException Unhandled exception during tray icon creation on Linux

5 participants