AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.23k stars 2.19k forks source link

TrayIcon Double Click (Linux;Windows) #10269

Open schmitch opened 1 year ago

schmitch commented 1 year ago

Is your feature request related to a problem? Please describe. add a way to track TrayIcon double clicks

Additional context at the moment double click events are not handled with the tray icon. (clicks are also only working in Linux/Windows)

maxkatz6 commented 1 year ago

It might not be technically possible. Native platform API should be investigated if they provide this kind of information.

schmitch commented 1 year ago

actually I‘m trying to port a wpf/windows forms application which does use that. But I’m unsure about linux.

(I tried the click event on macos and was confused why it didn’t work 😅, but yeah macos definitely has no support for it)

Sorien commented 1 year ago

isn't double click just two single clicks in some time interval? it should be easily implemented, windows and osx has some functions to retrieve double click time, linux probably not

codecat commented 1 year ago

I was able to work around this limitation on Windows:

[DllImport("user32.dll")]
private static extern uint GetDoubleClickTime();

private DateTime? _trayTimeClick;

public void OnTrayClicked(object sender, EventArgs e)
{
    if (_trayTimeClick == null) {
        _trayTimeClick = DateTime.Now;
        return;
    }

    var delta = (DateTime.Now - _trayTimeClick).Value;
    if (delta.TotalMilliseconds > GetDoubleClickTime()) {
        _trayTimeClick = DateTime.Now;
        return;
    }

    _trayTimeClick = null;
    // Do something when the icon is double clicked
}
<TrayIcon Icon="/Assets/Icon.ico" ToolTipText="Example" Clicked="OnTrayClicked">

Not sure how you would do this on Mac or Linux, but this is a possible workaround for anyone who needs it on Windows.