Drastic.TrayWindow 1.0.9

dotnet add package Drastic.TrayWindow --version 1.0.9                
NuGet\Install-Package Drastic.TrayWindow -Version 1.0.9                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Drastic.TrayWindow" Version="1.0.9" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Drastic.TrayWindow --version 1.0.9                
#r "nuget: Drastic.TrayWindow, 1.0.9"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Drastic.TrayWindow as a Cake Addin
#addin nuget:?package=Drastic.TrayWindow&version=1.0.9

// Install Drastic.TrayWindow as a Cake Tool
#tool nuget:?package=Drastic.TrayWindow&version=1.0.9                

NuGet Version License

Drastic.TrayWindow

Drastic.TrayWindow is a API for creating tray icons that contain popup windows, designed for dotnet Mac Catalyst and macOS applications.

animation

IMPORTANT

For this to work on Mac Catalyst, we are using Objective-C Selectors for directly accessing AppKit APIs from UIKit. These are private, and are frowd upon by Apple. You may have issues publishing apps using this library within the Mac App Store. While it should "work" it is best seen as experimental.

How To Use

You can check in this repos samples directory for a complete sample of how to use this library. For macOS it should be straight forward, but Mac Catalyst requires some additional setup.

For both Mac Catalyst and macOS, you need to create a TrayIcon. You can read how to make one in the Drastic.Tray README.

For macOS,

var menuItems = new List<TrayMenuItem>();
var trayImage = new TrayImage(NSImage.ImageNamed("TrayIcon.ico")!);
menuItems.Add(new TrayMenuItem("Hello!", trayImage, async () => { }, "h"));
menuItems.Add(new TrayMenuItem("From!", trayImage, async () => { }, "f"));
menuItems.Add(new TrayMenuItem("MacOS!", trayImage, async () => { }, "m", NSEventModifierMask.ControlKeyMask | NSEventModifierMask.CommandKeyMask));
var trayIcon = new Drastic.Tray.TrayIcon(trayImage, menuItems);
var trayWindow = new NSTrayWindow(trayIcon, new TrayWindowOptions(), new SampleViewController());
trayIcon.LeftClicked += (object? sender, TrayClickedEventArgs e) => trayWindow.ToggleVisibility();
trayIcon.RightClicked += (object? sender, TrayClickedEventArgs e) => trayIcon.OpenMenu();

Create the TrayIcon, then create the TrayWindow, using the TrayIcon you created. It will now be "attached" to that TrayIcon. Once you run ToggleVisibility you should the see the window open.

For Mac Catalyst, you need to add some additional items.

First, in your Info.plist, you must add support for multiple windows. The documentation for handling this with iOS (https://learn.microsoft.com/en-us/xamarin/ios/platform/ios13/multi-window-ipad) applies here too.

    <key>UIApplicationSceneManifest</key>
    <dict>
        <key>UIApplicationSupportsMultipleScenes</key>
        <true/>
        <key>UISceneConfigurations</key>
        <dict>
            <key>UIWindowSceneSessionRoleApplication</key>
            <array>
                <dict>
                    <key>UISceneConfigurationName</key>
                    <string>Default Configuration</string>
                    <key>UISceneDelegateClassName</key>
                    <string>SceneDelegate</string>
                </dict>
            </array>
        </dict>
    </dict>

Next, change your AppDelegate to use TrayAppDelegate

using Drastic.Tray;

namespace Drastic.TrayWindow.Sample.MacCatalyst;

[Register("AppDelegate")]
public class AppDelegate : TrayAppDelegate
{

Unlike macOS, you can only have one TrayIcon with one TrayWindow in your application. This is because TrayAppDelegate sets up the infrastructure for handling these windows and, trust me, it's for the best.

From your TrayAppDelegate instance, you can now add your TrayIcon and setup the window.

var image = UIImage.GetSystemImage("trophy.circle");
var trayImage = new TrayImage(image!);
var icon = new Tray.TrayIcon(trayImage);
this.CreateTrayWindow(icon, new TrayWindowOptions(), new SampleViewController("Welcome to the tray!"));

Now it should appear on your systems menu bar.

WinUI

WinUI is similar to macOS. You can create any number of WinUITrayWindows and apply them to TrayIcon.

            var trayImage = new TrayImage(GetResourceFileContent("TrayIcon.ico")!);
            var menuItems = new List<TrayMenuItem>
            {
                new TrayMenuItem("Hello!", trayImage, async () => { }),
                new TrayMenuItem("From!", trayImage, async () => { }),
                new TrayMenuItem("Windows!", trayImage, async () => { }),
            };
            this.icon = new TrayIcon("Tray Icon", trayImage, menuItems);
            this.trayWindow = new SampleTrayWindow(this.icon, new TrayWindowOptions(500, 700));
            this.icon.RightClicked += (object? sender, TrayClickedEventArgs e) =>
            {
                System.Diagnostics.Debug.WriteLine("Right Click!");
            };
            this.icon.LeftClicked += (object? sender, TrayClickedEventArgs e) =>
            {
                this.trayWindow.ToggleVisibility();
            };
    public sealed partial class SampleTrayWindow : WinUITrayWindow
    {
        public SampleTrayWindow(TrayIcon icon, TrayWindowOptions options)
            : base(icon, options)
        {
            this.InitializeComponent();
        }
    ...

The WinUITrayWindow can be created from codebehind or XAML. It is the same as a regular Window.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-maccatalyst15.4 is compatible.  net7.0-macos was computed.  net7.0-macos12.3 is compatible.  net7.0-tvos was computed.  net7.0-windows was computed.  net7.0-windows10.0.19041 is compatible.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Drastic.TrayWindow:

Package Downloads
Drastic.TrayWindow.Maui

Create cross-platform Mac Catalyst and WinUI Tray Window views using MAUI.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.9 438 12/31/2022
1.0.8 289 12/27/2022
1.0.7 314 12/26/2022
1.0.6 307 12/25/2022
1.0.5 295 12/24/2022
1.0.4 306 12/24/2022
1.0.3 299 12/23/2022
1.0.2 285 12/23/2022
1.0.1 316 12/23/2022
1.0.0 313 12/22/2022