WebExeBuilder Documentation

onTrayMenuItemClick

Category: Events

Namespace: web.events.onTrayMenuItemClick

Description

Fires when the user clicks any item in the system tray right-click menu created via web.trayMenu.createMenu. The callback receives the integer id of the clicked item.

Syntax

web.events.onTrayMenuItemClick = function(itemId) {
    // itemId is the number id assigned in web.trayMenu.createMenu()
};

Parameters

The callback receives one argument:

  • itemId number — The integer id of the tray menu item that was clicked, matching the id used when building the menu structure

Returns

Nothing. This is an event handler assignment, not a function call.

Examples

Basic usage

web.events.onTrayMenuItemClick = function(itemId) {
    console.log('Tray item clicked:', itemId);
};

Full tray menu setup with handler

document.addEventListener('DOMContentLoaded', async () => {

    web.events.onTrayMenuItemClick = async function(itemId) {
        switch (itemId) {
            case 1: // Open / restore window
                await web.window.restore();
                break;
            case 2: // Settings
                openSettings();
                break;
            case 3: // Exit
                await web.app.close();
                break;
        }
    };

    await web.trayMenu.createMenu({
        items: JSON.stringify([
            { id: 1, caption: 'Open',     enabled: true },
            { id: 2, caption: 'Settings', enabled: true },
            { separator: true },
            { id: 3, caption: 'Exit',     enabled: true }
        ])
    });

});

Update item state after a click

web.events.onTrayMenuItemClick = async function(itemId) {
    if (itemId === 2) { // Toggle notifications
        notificationsEnabled = !notificationsEnabled;
        await web.trayMenu.updateMenu({
            items: JSON.stringify([{
                id: 2,
                caption: notificationsEnabled ? 'Notifications: On' : 'Notifications: Off'
            }])
        });
    }
};

Use Cases

  • Restore or show the app window from the tray
  • Open settings or preferences
  • Toggle app features (notifications, auto-start, etc.)
  • Exit the application cleanly

Notes

  • Tray menu item id values must be integers — string IDs are not supported
  • Register the handler before or alongside web.trayMenu.createMenu()
  • Only one handler can be assigned at a time — reassigning replaces the previous handler
  • Separator items (separator: true) do not have an id and never fire the callback
  • id values are numbers — unlike web.appMenu which also uses integers but is a separate namespace
  • See web.trayMenu.createMenu for building the menu structure
  • See web.trayMenu.updateMenu for changing item state after creation