用一個 Tray
來表示一個圖標,這個圖標處于正在運行的系統(tǒng)的通知區(qū) ,通常被添加到一個 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 執(zhí)行.MenuItem
起效,需要再次調(diào)用 setContextMenu
.例如:contextMenu.items[2].checked = false;
appIcon.setContextMenu(contextMenu);
如果想在所有平臺保持完全相同的行為,不應(yīng)該依賴點擊事件,而是一直將一個 context menu 添加到 tray icon.
Tray
是一個 事件發(fā)出者.
new Tray(image)
image
NativeImage創(chuàng)建一個與 image
相關(guān)的 icon.
Tray
模塊可發(fā)出下列事件:
注意: 一些事件只能在特定的os中運行,已經(jīng)標明.
event
EventaltKey
BooleanshiftKey
BooleanctrlKey
BooleanmetaKey
Booleanbounds
Object - tray icon 的 bounds.x
Integery
Integerwidth
Integerheight
Integer當tray icon被點擊的時候發(fā)出事件.
注意: bounds
只在 OS X 和 Windows 上起效.
event
EventaltKey
BooleanshiftKey
BooleanctrlKey
BooleanmetaKey
Booleanbounds
Object - tray icon 的 bounds.x
Integery
Integerwidth
Integerheight
Integer當tray icon被鼠標右鍵點擊的時候發(fā)出事件.
event
EventaltKey
BooleanshiftKey
BooleanctrlKey
BooleanmetaKey
Booleanbounds
Object - tray icon 的 bounds.x
Integery
Integerwidth
Integerheight
Integer當tray icon被雙擊的時候發(fā)出事件.
當tray 氣泡顯示的時候發(fā)出事件.
當tray 氣泡被點擊的時候發(fā)出事件.
當tray 氣泡關(guān)閉的時候發(fā)出事件,因為超時或人為關(guān)閉.
當tray icon上的任何可拖動項被刪除的時候發(fā)出事件.
event
files
Array - 已刪除文件的路徑.當tray icon上的可拖動文件被刪除的時候發(fā)出事件.
當一個拖動操作進入tray icon的時候發(fā)出事件.
當一個拖動操作離開tray icon的時候發(fā)出事件. Emitted when a drag operation exits the tray icon.
當一個拖動操作在tray icon上或其它地方停止拖動的時候發(fā)出事件.
Tray
模塊有以下方法:
Note: 一些方法只能在特定的os中運行,已經(jīng)標明.
Tray.destroy()
立刻刪除 tray icon.
Tray.setImage(image)
image
NativeImage讓 image
與 tray icon 關(guān)聯(lián)起來.
Tray.setPressedImage(image)
OS Ximage
NativeImage當在 OS X 上按壓 tray icon 的時候, 讓 image
與 tray icon 關(guān)聯(lián)起來.
Tray.setToolTip(toolTip)
toolTip
String為 tray icon 設(shè)置 hover text.
Tray.setTitle(title)
OS Xtitle
String在狀態(tài)欄沿著 tray icon 設(shè)置標題.
Tray.setHighlightMode(highlight)
OS Xhighlight
Boolean當 tray icon 被點擊的時候,是否設(shè)置它的背景色變?yōu)楦吡?blue).默認為 true.
Tray.displayBalloon(options)
Windowsoptions
Objecticon
NativeImagetitle
Stringcontent
String展示一個 tray balloon.
Tray.popUpContextMenu([menu, position])
OS X Windowsmenu
Menu (optional)position
Object (可選) - 上托位置.x
Integery
Integer從 tray icon 上托出 context menu . 當劃過 menu
的時候, menu
顯示,代替 tray 的 context menu .
position
只在 windows 上可用,默認為 (0, 0) .
Tray.setContextMenu(menu)
menu
Menu為這個 icon 設(shè)置 context menu .
更多建議: