tray
Create and control a Windows system tray icon. Supports a tooltip, a right-click context menu with nested sub-menus, and event handlers for click, scroll, and double-click interactions.
import { tray } from "novadesk";Availability
Available in the Main script only.
Keep a reference
Always store the tray instance in a variable. If the reference is garbage-collected, the tray icon disappears and all event handlers are lost.
Table of Contents
Constructor
Creates and displays a new system tray icon. The icon appears in the taskbar notification area immediately. Passing a non-string argument throws a TypeError.
PARAMETERS
import { tray } from "novadesk";
const appTray = new tray(path.join(__dirname, "assets", "icon.ico"));
appTray.setToolTip("My Widget");Methods
Updates the tray icon image at runtime. Useful for reflecting different application states (active, idle, error, etc.). Throws TypeError if the argument is missing or not a string.
PARAMETERS
// Show a different icon when connected
appTray.setImage(path.join(__dirname, "assets", "icon-active.ico"));
// Revert to idle state icon
appTray.setImage(path.join(__dirname, "assets", "icon.ico"));Sets the tooltip shown when the cursor hovers over the tray icon. Throws TypeError if the argument is missing or not a string.
PARAMETERS
appTray.setToolTip("My Widget — Connected");Registers an event listener on the tray icon. Throws TypeError if the event name is not one of the supported values, or if the handler is not a function.
Supported events:
| Event | Trigger |
|---|---|
"click" | Left mouse button released on the tray icon |
"right-click" | Right mouse button released on the tray icon |
"double-click" | Left mouse button double-clicked on the tray icon |
"scroll-up" | Mouse wheel scrolled up over the tray icon |
"scroll-down" | Mouse wheel scrolled down over the tray icon |
Event names are case-sensitive. Passing any other string throws TypeError: tray.on: unknown event.
PARAMETERS
// Toggle window visibility on left click
appTray.on("click", () => {
if (win.isVisible()) {
win.hide();
} else {
win.show();
}
});
// Show context menu hint on right click (menu opens automatically)
appTray.on("right-click", () => {
console.log("Right-clicked");
});
// Scroll to adjust a value
appTray.on("scroll-up", () => {
ipcMain.send("volume-up");
});
appTray.on("scroll-down", () => {
ipcMain.send("volume-down");
});Removes the tray icon from the taskbar and clears all event handlers and context menu callbacks. After calling this, the tray instance is no longer active. Throws TypeError if called on an invalid tray instance.
appTray.destroy();Practical Examples
Basic tray with show/hide and exit
import { tray, app } from "novadesk";
import { widgetWindow } from "novadesk";
const win = new widgetWindow({
id: "demo",
width: 400,
height: 300,
script: "script.ui.js",
backgroundColor: "rgb(10,10,10)"
});
const appTray = new tray(path.join(__dirname, "assets", "icon.ico"));
appTray.setToolTip("My Widget");
appTray.setContextMenu([
{ text: "Show", action: () => win.show() },
{ text: "Hide", action: () => win.hide() },
{ type: "separator" },
{ text: "Exit", action: () => app.exit() }
]);
appTray.on("click", () => win.show());Dynamic icon reflecting connection state
import { tray } from "novadesk";
const appTray = new tray(path.join(__dirname, "assets", "icon-offline.ico"));
appTray.setToolTip("Status: Offline");
ipcMain.on("status-change", (event, payload) => {
if (payload.connected) {
appTray.setImage(path.join(__dirname, "assets", "icon-online.ico"));
appTray.setToolTip("Status: Connected");
} else {
appTray.setImage(path.join(__dirname, "assets", "icon-offline.ico"));
appTray.setToolTip("Status: Offline");
}
});Volume control via scroll wheel
import { tray } from "novadesk";
let volume = 50;
const appTray = new tray(path.join(__dirname, "assets", "speaker.ico"));
appTray.setToolTip("Volume: 50%");
appTray.on("scroll-up", () => {
volume = Math.min(100, volume + 5);
appTray.setToolTip("Volume: " + volume + "%");
ipcMain.send("set-volume", { value: volume });
});
appTray.on("scroll-down", () => {
volume = Math.max(0, volume - 5);
appTray.setToolTip("Volume: " + volume + "%");
ipcMain.send("set-volume", { value: volume });
});Destroy tray on widget close
import { tray } from "novadesk";
import { widgetWindow } from "novadesk";
const win = new widgetWindow({ id: "demo", width: 300, height: 200 });
const appTray = new tray(path.join(__dirname, "assets", "icon.ico"));
win.on("close", () => {
appTray.destroy();
});