Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Electron Tray module


May 25, 2021 Electron


Table of contents


A Tray is Tray to represent an icon that is in the notification area of a running system and is usually added to a context menu.

const electron = require('electron');
const app = electron.app;
const Menu = electron.Menu;
const Tray = electron.Tray;

var appIcon = null;
app.on('ready', function(){
  appIcon = new Tray('/path/to/my/icon');
  var contextMenu = Menu.buildFromTemplate([
    { label: 'Item1', type: 'radio' },
    { label: 'Item2', type: 'radio' },
    { label: 'Item3', type: 'radio', checked: true },
    { label: 'Item4', type: 'radio' }
  ]);
  appIcon.setToolTip('This is my application.');
  appIcon.setContextMenu(contextMenu);
});

Platform restrictions:

  • In Linux, if the app indicator is supported, use it otherwise GtkStatusIcon
  • In Linux, configured only with the support of the app indicator, you libappindicator1 let the icon tray execute.
  • The app indicator is displayed only if it has context menu.
  • Click events are ignored when an app indicator is used on linux.
  • In Linux, in order for MenuItem work, you need to setContextMenu For example:
contextMenu.items[2].checked = false;
appIcon.setContextMenu(contextMenu);

If you want to maintain exactly the same behavior across all platforms, you should not rely on click events, but always add a context menu to the tray icon.

Class: Tray

Tray is an event issuer .

new Tray(image)

Create an image icon.

Event

Tray module emits the following events:

Note: Some events can only run in certain os and have been indicated.

Event: 'click'

  • event Event
    • altKey Boolean
    • shiftKey Boolean
    • ctrlKey Boolean
    • metaKey Boolean
  • bounds Object - the bounds of tray icon.
    • x Integer
    • y Integer
    • width Integer
    • height Integer

An event is sent when the tray icon is clicked.

Note: bounds only work on OS X and Windows.

Event: 'right-click' OS X Windows

  • event Event
    • altKey Boolean
    • shiftKey Boolean
    • ctrlKey Boolean
    • metaKey Boolean
  • bounds Object - the bounds of tray icon.
    • x Integer
    • y Integer
    • width Integer
    • height Integer

An event is issued when the tray icon is right -clicked by the mouse.

Event: 'double-click' OS X Windows

  • event Event
    • altKey Boolean
    • shiftKey Boolean
    • ctrlKey Boolean
    • metaKey Boolean
  • bounds Object - the bounds of tray icon.
    • x Integer
    • y Integer
    • width Integer
    • height Integer

An event is issued when the tray icon is double-clicked.

Event: 'balloon-show' Windows

An event is issued when the tray bubble is displayed.

Event: 'balloon-click' Windows

An event is sent out when the tray bubble is clicked.

Event: 'balloon-closed' Windows

An event is issued when the tray bubble is closed because of a timeout or artificial shutdown.

Event: 'drop' OS X

An event is issued when any draggable item on the tray icon is deleted.

Event: 'drop-files' OS X

  • event
  • files Array - The path to the deleted file.

An event is issued when a draggable file on the tray icon is deleted.

Event: 'drag-enter' OS X

An event is issued when a drag operation enters the tray icon.

Event: 'drag-leave' OS X

An event is issued when a drag operation leaves the tray icon. Emitted when a drag operation exits the tray icon.

Event: 'drag-end' OS X

An event is issued when a drag operation stops dragging on the tray icon or elsewhere.

Method

Tray module has the following methods:

Note: Some methods can only run in certain os and have been indicated.

Tray.destroy()

Delete the tray icon now.

Tray.setImage(image)

Associate image with tray icon.

Tray.setPressedImage(image) OS X

When pressing the tray icon on OS X, associate the image with the tray icon.

Tray.setToolTip(toolTip)

  • toolTip String

Set hover text for tray icon.

Tray.setTitle(title) OS X

  • title String

Set the title along the tray icon in the status bar.

Tray.setHighlightMode(highlight) OS X

  • highlight Boolean

When the tray icon is clicked, whether to set its background color to highlight (blue).

Tray.displayBalloon(options) Windows

Show a tray balloon.

Tray.popUpContextMenu([menu, position]) OS X Windows

  • menu Menu (optional)
  • position Object (optional) - top position.
    • x Integer
    • y Integer

Remove context menu from the tray icon . When the menu is menu menu is displayed in place of the context menu of the tray .

position is only available on windows and defaults to (0, 0).

Tray.setContextMenu(menu)

  • menu Menu

Set context menu for this icon .