dotnet / maui-samples

Samples for .NET Multi-Platform App UI (.NET MAUI)
https://dot.net/maui
MIT License
3.03k stars 1.25k forks source link

Minimize to tray and Context Menu support on Tray Icon Windows MacOS #351

Open vadimffe opened 1 year ago

vadimffe commented 1 year ago

It would be great if you could extend your example for WeatherTwentyOne to support:

  1. Minimize net maui app to tray
  2. Context Menu support on Tray Icon right click

Windows MacOS

https://github.com/dotnet/maui-samples/tree/main/7.0/Apps/WeatherTwentyOne/src/WeatherTwentyOne/Platforms/Windows

ITaluone commented 1 year ago

Finally I got this working:

 builder.ConfigureLifecycleEvents(lifecycle =>
        {
            lifecycle.AddWindows(lifecycleBuilder => lifecycleBuilder.OnWindowCreated(window =>
            {
                window.ExtendsContentIntoTitleBar = true;
                var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
                var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);

                appWindow.Closing += async (s, e) =>
                {
                    e.Cancel = true;
                    var result = await Application.Current?.MainPage?.DisplayAlert(
                        "App close",
                        "Do you really want to quit?",
                        "Close",
                        "Minimize to system tray")!;

                    if (result)
                    {
                        Application.Current?.Quit();
                    }
                    WindowExtensions.MinimizeToTray();
                };
            }));
        });

https://stackoverflow.com/questions/74129182/how-to-edit-the-function-of-the-close-button-on-maui-blazor-hybrid

Irasil commented 11 months ago

And for MacOS? I've been looking for an implementation option for a long time, unfortunately I can't find any information and I'm dreaming of examples like the one for Windows. I'm starting to think that I'll have to wait until MAUI supports MacOS and not just the stripped-down Mac Catalyst version like it currently does.

vadimffe commented 11 months ago

And for MacOS? I've been looking for an implementation option for a long time, unfortunately I can't find any information and I'm dreaming of examples like the one for Windows. I'm starting to think that I'll have to wait until MAUI supports MacOS and not just the stripped-down Mac Catalyst version like it currently does.

Have you tried this? I don't know about MacOS, but it seems to be supporting it as well. It may need some custom code, but in general has necessary functions.

https://github.com/HavenDV/H.NotifyIcon

ITaluone commented 10 months ago

Have you tried this? I don't know about MacOS, but it seems to be supporting it as well. It may need some custom code, but > in general has necessary functions.

https://github.com/HavenDV/H.NotifyIcon

This was actually a very useful hint :)

Thank you! @vadimffe