May 25, 2021 Electron
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:
GtkStatusIcon
libappindicator1
let the icon tray execute.
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.
Tray
is
an
event issuer
.
new Tray(image)
image
NativeImage
Create an
image
icon.
Tray
module emits the following events:
Note: Some events can only run in certain os and have been indicated.
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
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
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.
An event is issued when the tray bubble is displayed.
An event is sent out when the tray bubble is clicked.
An event is issued when the tray bubble is closed because of a timeout or artificial shutdown.
An event is issued when any draggable item on the tray icon is deleted.
event
files
Array - The path to the deleted file.
An event is issued when a draggable file on the tray icon is deleted.
An event is issued when a drag operation enters the tray icon.
An event is issued when a drag operation leaves the tray icon. Emitted when a drag operation exits the tray icon.
An event is issued when a drag operation stops dragging on the tray icon or elsewhere.
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)
image
NativeImage
Associate
image
with tray icon.
Tray.setPressedImage(image)
OS X
image
NativeImage
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
options
Object
icon
NativeImage
title
String
content
String
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 .