app.createTrayIcon() — this ensures creation happens on the event-loop thread, which is required for native UI objects on all three platforms.
Creating a tray icon
Callapp.createTrayIcon(TrayIconOptions) inside (or after) app.whenReady() to guarantee the event loop is running before the native tray icon is registered.
TrayIconOptions
Pass aTrayIconOptions object to app.createTrayIcon().
Icon data formats
Theicon.data field accepts two formats:
- Raw RGBA bytes — supply
widthandheightalongside aBufferwhose length equalswidth × height × 4. 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.
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 height, or encoded image bytes without dimensions.
tray.removeIcon()
TrayIcon instance remains valid and you can restore it by calling setIcon().
tray.setMenu(menu?)
undefined or call without arguments to remove the menu entirely.
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().