app.createTrayIcon(), which registers the native resource with the application.
Tray icons are supported on Windows, macOS, and Linux. The Android backend
explicitly rejects tray creation, and the published FreeBSD target is a GUI
stub.
Creating a tray icon
Callapp.createTrayIcon(TrayIconOptions) through the application. A readiness
check is not required; you can create the icon before or after starting the
event pump.
TrayIconOptions
Pass aTrayIconOptions object to app.createTrayIcon().
Icon data formats
Theicon.data field accepts raw pixels or encoded image bytes:
- Raw RGBA bytes — supply
widthand optionallyheightalongside aBuffer. The byte length must equalwidth × height × 4; ifheightis omitted, the icon is treated as square. Each pixel is four bytes: R, G, B, A. - Encoded image bytes — supply a
Buffercontaining a PNG, JPEG, WebP, GIF, BMP, ICO, or TIFF file withoutwidthorheight. The native backend decodes the image automatically.
height without width is invalid.
On Linux, the tray backend may not show an icon unless a menu is set. The
title may also be hidden by the panel or tray implementation even when it is
configured.
Methods
Once you have aTrayIcon instance returned by app.createTrayIcon(), you can call these methods at any time before the icon is disposed.
tray.id
tray.setIcon(data, width?, height?)
data with width
and optionally height (a missing height means a square icon), or encoded image
bytes without dimensions. Providing height without width is invalid.
tray.removeIcon()
TrayIcon instance remains
valid; restore it by calling setIcon() with image data again.
tray.setMenu(menu?)
undefined or null to remove it where
the platform supports removal. On Linux, once a menu has been set, the native
tray backend does not remove or replace it through this API, so configure the
initial menu before creating the tray icon.
tray.setTooltip(tooltip?)
tray.setTitle(title?)
tray.setVisible(visible)
tray.setIconAsTemplate(value)
true, macOS renders the icon using the appropriate foreground colour for the current menu-bar appearance (dark or light). macOS only.
tray.setShowMenuOnLeftClick(value)
tray.setShowMenuOnRightClick(value)
tray.showMenu()
tray.rect()
null if the platform does not expose it.
Tray events
TrayIcon is a Node.js EventEmitter. Register listeners with tray.on(event, handler).
Menu click events
Clicking a menu item in the tray’s context menu does not fire a tray event. Instead, it fires thecustom-menu-click event on the app instance, exactly the same as window menu clicks. Use customMenuEvent.id to identify which item was selected.
Platform notes
Disposal
Calltray.dispose() to remove the icon from the system tray immediately. You can also use the ECMAScript explicit resource management syntax with Symbol.dispose.
app.exit() is called, the Application object removes all tray icons it owns, regardless of whether the TrayIcon wrapper object is still reachable in your code. You do not need to call tray.dispose() before app.exit().