- 系统托盘
- new Tray(image)
- 实例事件
- 事件: 'click'
- Event: 'right-click' macOS Windows
- Event: 'double-click' macOS Windows
- Event: 'balloon-show' Windows
- Event: 'balloon-click' Windows
- Event: 'balloon-closed' Windows
- Event: 'drop' macOS
- Event: 'drop-files' macOS
- Event: 'drop-text' macOS
- Event: 'drag-enter' macOS
- Event: 'drag-leave' macOS
- Event: 'drag-end' macOS
- Event: 'mouse-enter' macOS
- Event: 'mouse-leave' macOS
- Event: 'mouse-move' macOS
- 实例方法
- tray.destroy()
- tray.setImage(image)
- tray.setPressedImage(image) macOS
- tray.setToolTip(toolTip)
- tray.setTitle(title) macOS
- tray.getTitle() macOS
- tray.setHighlightMode(mode) macOS
- tray.setIgnoreDoubleClickEvents(ignore) macOS
- tray.getIgnoreDoubleClickEvents() macOS
- tray.displayBalloon(options) Windows
- tray.popUpContextMenu([menu, position]) macOS Windows
- tray.setContextMenu(menu)
- tray.getBounds() macOS Windows
- tray.isDestroyed()
系统托盘
添加图标和上下文菜单到系统通知区
进程:主进程
Tray 是一个 EventEmitter.
const { app, Menu, Tray } = require('electron')let tray = nullapp.on('ready', () => {tray = new Tray('/path/to/my/icon')const contextMenu = Menu.buildFromTemplate([{ label: 'Item1', type: 'radio' },{ label: 'Item2', type: 'radio' },{ label: 'Item3', type: 'radio', checked: true },{ label: 'Item4', type: 'radio' }])tray.setToolTip('This is my application.')tray.setContextMenu(contextMenu)})
平台限制:
- 在Linux上,如果支持,就使用应用程序指示器,否则将使用
GtkStatusIcon。 - 在仅支持应用程序指标的Linux发行版中,必须安装
libappindicator1才能使任务栏图标正常工作。 - 应用程序指标只有当它有一个上下文菜单时才会显示。
- 当在Linux上使用应用程序指标时,它的
click事件将被忽略 - 在Linux上,为了改变单独的
MenuItem,你必须再次调用setContextMenu。 例如:
const { app, Menu, Tray } = require('electron')let appIcon = nullapp.on('ready', () => {appIcon = new Tray('/path/to/my/icon')const contextMenu = Menu.buildFromTemplate([{ label: 'Item1', type: 'radio' },{ label: 'Item2', type: 'radio' }])// Make a change to the context menucontextMenu.items[1].checked = false// Call this again for Linux because we modified the context menuappIcon.setContextMenu(contextMenu)})
- 在 Windows 上, 建议使用
ICO图标来获得最佳视觉效果。如果要在所有平台上保持完全相同的行为, 则不应依赖click事件, 并且始终将上下文菜单附加到任务栏图标。
new Tray(image)
image(NativeImage | String)创建与image关联的新任务栏图标。
实例事件
Tray 对象会发出以下事件:
事件: 'click'
eventKeyboardEventboundsRectangle - 系统托盘图标的边界。positionPoint - 事件的位置信息。当该图标被点击时触发。
Event: 'right-click' macOS Windows
eventKeyboardEventboundsRectangle - 系统托盘图标的边界。当该图标被右击时触发。
Event: 'double-click' macOS Windows
eventKeyboardEventboundsRectangle - 系统托盘图标的边界。当该图标被双击时触发。
Event: 'balloon-show' Windows
当系统托盘图标气泡显示时,触发该事件。
Event: 'balloon-click' Windows
当系统托盘气泡被点击时,触发该事件。
Event: 'balloon-closed' Windows
当系统托盘气泡因为超时被关闭或者用户手动关闭时,触发该事件。
Event: 'drop' macOS
当有任何拖动项拖到该任务栏图标上时,触发该事件。
Event: 'drop-files' macOS
eventEventfilesString[] - 拖至任务栏图标上的文件的路径。当有任何文件被拖到该任务栏图标上时,触发该事件。
Event: 'drop-text' macOS
eventEventtextString - 拖至任务栏图标上的文字内容。当有任何文字被拖到该任务栏图标上时,触发该事件。
Event: 'drag-enter' macOS
当有任何拖动操作进入(拖动未结束)该任务栏图标时,触发该事件。
Event: 'drag-leave' macOS
当有任何拖动操作离开该任务栏图标时,触发该事件。
Event: 'drag-end' macOS
当有任何拖动操作在托盘或其他地方结束时,触发该事件。
Event: 'mouse-enter' macOS
eventKeyboardEventpositionPoint - 事件的位置信息。当鼠标进入该任务栏图标时,触发该事件。
Event: 'mouse-leave' macOS
eventKeyboardEventpositionPoint - 事件的位置信息。当鼠标离开该任务栏图标时,触发该事件。
Event: 'mouse-move' macOS
eventKeyboardEventpositionPoint - 事件的位置信息。当鼠标在该任务栏图标上移动时,触发该事件。
实例方法
Tray 类拥有以下方法:
tray.destroy()
立即销毁该任务栏图标
tray.setImage(image)
image(NativeImage | String)设置image作为托盘中显示的图标
tray.setPressedImage(image) macOS
image(NativeImage | String)在 macOS 中,设置image作为托盘图标被按下时显示的图标
tray.setToolTip(toolTip)
toolTipString设置鼠标指针在托盘图标上悬停时显示的文本
tray.setTitle(title) macOS
titleStringSets the title displayed next to the tray icon in the status bar (Support ANSI colors).
tray.getTitle() macOS
titleStringReturnsString- the title displayed next to the tray icon in the status bar
tray.setHighlightMode(mode) macOS
modeString - 高亮模式选项,以下为可选值selection- 当托盘图标本点击或托盘的上下文菜单打开时高亮显示托盘图标,这是mode的默认值always- 总是高亮托盘图标never- 从不高亮托盘图标设置托盘图标背景 (蓝色) 高亮的时机
过时的
Note: 当窗口可见状态变化时你可以在BrowserWindow中使用 highlightMode 实现 'never' 和'always' 模式的切换
const { BrowserWindow, Tray } = require('electron')const win = new BrowserWindow({ width: 800, height: 600 })const tray = new Tray('/path/to/my/icon')tray.on('click', () => {win.isVisible() ? win.hide() : win.show()})win.on('show', () => {tray.setHighlightMode('always')})win.on('hide', () => {tray.setHighlightMode('never')})
tray.setIgnoreDoubleClickEvents(ignore) macOS
ignoreBooleanSets the option to ignore double click events. Ignoring these events allows you to detect every individual click of the tray icon.
This value is set to false by default.
tray.getIgnoreDoubleClickEvents() macOS
Returns Boolean - Whether double click events will be ignored.
tray.displayBalloon(options) Windows
optionsObjecticon(NativeImage | String) (可选) -titleStringcontentString显示一个托盘气球通知.
tray.popUpContextMenu([menu, position]) macOS Windows
menuMenu (可选)positionPoint (可选) - 菜单弹出的位置.弹出托盘图标的上下文菜单。如果传入了menu参数,将会弹出menu而不是托盘图标的上下文菜单
参数 position 只在 Windows 上可用, 并拥有默认值 (0, 0)。
tray.setContextMenu(menu)
menuMenu | null设置这个图标的内容菜单
tray.getBounds() macOS Windows
返回 Rectangle
以Object类型返回托盘图标的bounds
tray.isDestroyed()
返回 Boolean -判断托盘图标是否被销毁
