HavenDV / H.NotifyIcon

TrayIcon for WPF/WinUI/Uno/MAUI
MIT License
543 stars 43 forks source link

Add Activate() to tray icon OnClick logic #108

Closed vadimffe closed 11 months ago

vadimffe commented 1 year ago

What would you like to be added:

With following code you can hide your MAUI application once it gets Minimized. Then once clicking on H.NotifyIcon, application icon gets visible in task bar, but it stays hidden. With recent observations Activate() makes application visible. As a request it would be very welcomed to have the same functionality for this library, so that MAUI application on Windows not only appear in Task bar, but also be visible on top of other windows.

  Microsoft.Maui.Handlers.WindowHandler.Mapper.AppendToMapping(nameof(IWindow), (handler, view) =>
    {
#if WINDOWS
        var nativeWindow = handler.PlatformView;
        var appWindow = nativeWindow.GetAppWindow();
        if (appWindow is not null)
        {
            appWindow.Changed += (sender, args) =>
            {
                if (appWindow.Presenter is OverlappedPresenter overlappedPresenter)
                {
                    if (args.DidPresenterChange && overlappedPresenter.State == OverlappedPresenterState.Minimized)
                    {
                        appWindow.Hide();
                        Thread.Sleep(5000);
                        nativeWindow.Activate();
                    }
                }
            };
        }

#endif
    });

Why is this needed:

It is a bit not very logical currently that you need to click on icon in tray and then one more time in task bar to get your application window cisible.

For which Platform:

Anything else we need to know?

HavenDV commented 1 year ago

I added a test extension for MAUI Window - Activate(). I would be glad if you could test its use within a regular ICommand.

vadimffe commented 1 year ago

I am struggling to get it working in my current application. However I remember previously, with above code, it was working. I will create a test project in the upcoming days and share results here.

vadimffe commented 1 year ago

I already had one MAUI test project. Added tray icon implementation there. So currently there is some issue as currently while clicking icon in tray, window still stays somehow hidden. It is not behind other windows, but hidden. Only clicking on task bar icon gets window back to visible. Needs to be investigated further.

Test project: https://github.com/vadimffe/DraggingIssueNetMAUI

Demonstration: https://github.com/HavenDV/H.NotifyIcon/assets/72302395/cb43acaf-7a02-4c26-8841-8d37bab6af91

HavenDV commented 1 year ago

Have you tried adding Thread.Sleep(5000); before Activate?

vadimffe commented 12 months ago

Have you tried adding Thread.Sleep(5000); before Activate?

I think issue is with Show() or Activate(). For me it seems that window stays minimized. It gets visible in task bar, but State is still Minimized. I previously did something similar for bringing windows back from minimized. I think something like this needs to be used in net maui. I will try to test further.

private const int SW_RESTORE = 9;

https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow

vadimffe commented 12 months ago

It seems to be working with ShowWindow and SW_RESTORE from link above. Don't know if you want to add it to your library directly.

Like:

[DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

Then pass minimized window and 9 as arguments. Then add this to Activate() for example.

I have also noticed that icon stays in the tray even when application is closed. This is probably a topic for new issue.

HavenDV commented 12 months ago

I have also noticed that icon stays in the tray even when application is closed. This is probably a topic for new issue.

Yes, now you need to explicitly call Dispose on TrayIcon to avoid this. For WPF there is a subscription to the Application.Current.Exit event followed by a call to Dispose, but this can never be guaranteed. For MAUI there seems to be something similar https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/app-lifecycle

HavenDV commented 12 months ago

Both issues will likely be fixed in the latest update. I've also listed you as a co-author, thanks for your research!

P.S. Strange that it didn't work: Co-authored-by: vadimffe <xxx.ffegmail.com>

vadimffe commented 11 months ago

After some recent checks, I can confirm that everything seems to be working now fine with latest version, at least on Windows. This issue can be closed.

The only thing that I am currently looking for is applying custom themes for context menu. I have not read all the docs yet. Maybe it is there already.

And thank you for co-authoring, although I haven't done much at all.

P.S. Also please no emails as text strings in this world of Internet. Trying to stay away from all these crawling spamming bots, don't trust any website for their possible protections.

HavenDV commented 11 months ago

I think context menu themes are possible, but I haven't invested in that direction. I think it will be like a development of the WinUI context menu, but I don’t have enough free time yet to do it for free

vadimffe commented 11 months ago

Understood, I was checking from the phone, maybe it shows differently.

Open source is what it is, don't know how supportive is community nowadays. Anyway thank you for what has been done already! I see there are a lot of people looking for tray icon implementation.

I will try to check on the latest topic as well.