用一个 Tray来表示一个图标,这个图标处于正在运行的系统的通知区 ,通常被添加到一个 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);
}); 平台限制:
GtkStatusIcon代替.libappindicator1来让 tray icon 执行.MenuItem起效,需要再次调用 setContextMenu.例如:contextMenu.items[2].checked = false;
appIcon.setContextMenu(contextMenu);
如果想在所有平台保持完全相同的行为,不应该依赖点击事件,而是一直将一个 context menu 添加到 tray icon.
Tray是一个 事件发出者.
new Tray(image)imageNativeImage创建一个与 image相关的 icon.
Tray模块可发出下列事件:
注意: 一些事件只能在特定的os中运行,已经标明.
eventEventaltKeyBooleanshiftKeyBooleanctrlKeyBooleanmetaKeyBooleanboundsObject - tray icon 的 bounds.xIntegeryIntegerwidthIntegerheightInteger当tray icon被点击的时候发出事件.
注意: bounds只在 OS X 和 Windows 上起效.
eventEventaltKeyBooleanshiftKeyBooleanctrlKeyBooleanmetaKeyBooleanboundsObject - tray icon 的 bounds.xIntegeryIntegerwidthIntegerheightInteger当tray icon被鼠标右键点击的时候发出事件.
eventEventaltKeyBooleanshiftKeyBooleanctrlKeyBooleanmetaKeyBooleanboundsObject - tray icon 的 bounds.xIntegeryIntegerwidthIntegerheightInteger当tray icon被双击的时候发出事件.
当tray 气泡显示的时候发出事件.
当tray 气泡被点击的时候发出事件.
当tray 气泡关闭的时候发出事件,因为超时或人为关闭.
当tray icon上的任何可拖动项被删除的时候发出事件.
eventfilesArray - 已删除文件的路径.当tray icon上的可拖动文件被删除的时候发出事件.
当一个拖动操作进入tray icon的时候发出事件.
当一个拖动操作离开tray icon的时候发出事件. Emitted when a drag operation exits the tray icon.
当一个拖动操作在tray icon上或其它地方停止拖动的时候发出事件.
Tray模块有以下方法:
Note: 一些方法只能在特定的os中运行,已经标明.
Tray.destroy()立刻删除 tray icon.
Tray.setImage(image)imageNativeImage让 image与 tray icon 关联起来.
Tray.setPressedImage(image)OS XimageNativeImage当在 OS X 上按压 tray icon 的时候, 让 image与 tray icon 关联起来.
Tray.setToolTip(toolTip)toolTipString为 tray icon 设置 hover text.
Tray.setTitle(title)OS XtitleString在状态栏沿着 tray icon 设置标题.
Tray.setHighlightMode(highlight)OS XhighlightBoolean当 tray icon 被点击的时候,是否设置它的背景色变为高亮(blue).默认为 true.
Tray.displayBalloon(options)WindowsoptionsObjecticonNativeImagetitleStringcontentString展示一个 tray balloon.
Tray.popUpContextMenu([menu, position])OS X WindowsmenuMenu (optional)positionObject (可选) - 上托位置.xIntegeryInteger从 tray icon 上托出 context menu . 当划过 menu的时候, menu显示,代替 tray 的 context menu .
position只在 windows 上可用,默认为 (0, 0) .
Tray.setContextMenu(menu)menuMenu为这个 icon 设置 context menu .