Windows Notification Icons
Because calling it the system tray would be wrong.
Description
Provides fairly direct access to the Win32
Shell_NotifyIcon
API, which allows adding an icon to the notification area, and
support for the related windows APIs to load icons and use context
(e.g. right-click) menus.
Prebuilt as N-API (64-bit only for now, sorry), meaning you
don't need Visual Studio or build tools installed on your
development machine or CI server.
Note that this is using the Win32 desktop APIs, so you don't
need to be running under the UWP (Windows Store) model,
and thus has odd limitations like requiring an icon in the
notification area to show a notification message, and not
supporting notification actions (e.g. buttons).
Usage
The type definitions have fuller jsdoc descriptions,
but as a basic overview:
import { NotifyIcon, Icon, Menu } from "not-the-systray";
const emptyIcon = new NotifyIcon();
emptyIcon.remove();
const appIcon = new NotifyIcon({
icon: Icon.load("my-icon.ico", Icon.small),
tooltip: "My icon from nodejs",
onSelect({ target, rightButton, mouseX, mouseY }) {
if (rightButton) {
handleMenu(mouseX, mouseY);
} else {
}
},
});
const notificationIcon = Icon.load("notification-icon.ico", Icon.large);
const errorIcon = Icon.load(Icon.ids.error, Icon.large);
const toggleId = 1;
const notificationId = 2;
const submenuId = 3;
const menu = new Menu([
{ id: toggleId, text: "Toggle item", checked: true },
{ id: notificationId, text: "Show notification" },
{ separator: true },
{ text: "Sub-menu", items: [
{ id: submenuId, text: "Sub-menu item" },
] }
]);
function handleMenu(x, y) {
const id = menu.showSync(x, y);
switch (id) {
case null:
break;
case toggleId: {
const { checked } = menu.get(toggleId);
menu.update(toggleId, { checked: !checked });
break;
}
case notificationId:
appIcon.update({
notification: {
icon: notificationIcon,
title: "Hello, world",
text: "A notification from nodejs",
},
});
break;
case submenuId:
console.log("Selected the submenu item");
break;
}
}