2017-02-12 14:51:36 +00:00
|
|
|
|
# Class:Tray
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
> 将图标和上下文菜单添加到系统的通知区域。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
可使用的进程: [主进程](../tutorial/quick-start.md#main-process)
|
2016-10-03 03:47:16 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
`Tray` 是一个 [事件发出者][event-emitter]。
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
const {app, Menu, Tray} = require('electron')
|
|
|
|
|
|
|
|
|
|
let tray = null
|
|
|
|
|
app.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'}
|
2016-10-03 03:47:16 +00:00
|
|
|
|
])
|
2017-02-12 14:51:36 +00:00
|
|
|
|
tray.setToolTip('This is my application.')
|
|
|
|
|
tray.setContextMenu(contextMenu)
|
2016-10-03 03:47:16 +00:00
|
|
|
|
})
|
2016-03-15 06:00:40 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
__平台限制:__
|
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
* 在 Linux, 如果支持应用指示器则使用它,否则使用 `GtkStatusIcon` 代替。
|
|
|
|
|
* 在 Linux ,配置了只有有了应用指示器的支持, 你必须安装 `libappindicator1` 来让 tray icon 执行。
|
|
|
|
|
* 应用指示器只有在它拥有 context menu 时才会显示。
|
|
|
|
|
* 当在linux 上使用了应用指示器,将忽略点击事件。
|
|
|
|
|
* 在 Linux,为了让单独的 `MenuItem` 起效,需要再次调用 `setContextMenu` 。例如:
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
|
|
|
|
```javascript
|
2017-02-12 14:51:36 +00:00
|
|
|
|
const {app, Menu, Tray} = require('electron')
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
let appIcon = null
|
|
|
|
|
app.on('ready', () => {
|
|
|
|
|
appIcon = new Tray('/path/to/my/icon')
|
|
|
|
|
const contextMenu = Menu.buildFromTemplate([
|
|
|
|
|
{label: 'Item1', type: 'radio'},
|
|
|
|
|
{label: 'Item2', type: 'radio'}
|
|
|
|
|
])
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
// Make a change to the context menu
|
|
|
|
|
contextMenu.items[1].checked = false
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
// Call this again for Linux because we modified the context menu
|
|
|
|
|
appIcon.setContextMenu(contextMenu)
|
|
|
|
|
})
|
|
|
|
|
```
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
如果想在所有平台保持完全相同的行为,不应该依赖点击事件,而是一直将一个 context menu 添加到 tray icon。
|
|
|
|
|
|
|
|
|
|
### `new Tray(image)`
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
* `image` ([NativeImage](native-image.md) | String)
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
创建一个与 `image` 相关的 icon。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
### Instance Events
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
`Tray` 模块可发出下列事件:
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### Event: 'click'
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
|
|
|
|
* `event` Event
|
|
|
|
|
* `altKey` Boolean
|
|
|
|
|
* `shiftKey` Boolean
|
|
|
|
|
* `ctrlKey` Boolean
|
|
|
|
|
* `metaKey` Boolean
|
2017-02-12 14:51:36 +00:00
|
|
|
|
* `bounds` [Rectangle](structures/rectangle.md) - tray icon 的 bounds
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
当 tray icon 被点击的时候发出事件。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### Event: 'right-click' _macOS_ _Windows_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
|
|
|
|
* `event` Event
|
|
|
|
|
* `altKey` Boolean
|
|
|
|
|
* `shiftKey` Boolean
|
|
|
|
|
* `ctrlKey` Boolean
|
|
|
|
|
* `metaKey` Boolean
|
2017-02-12 14:51:36 +00:00
|
|
|
|
* `bounds` [Rectangle](structures/rectangle.md) - tray icon 的 bounds
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
当 tray icon 被鼠标右键点击的时候发出事件。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### Event: 'double-click' _macOS_ _Windows_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
|
|
|
|
* `event` Event
|
|
|
|
|
* `altKey` Boolean
|
|
|
|
|
* `shiftKey` Boolean
|
|
|
|
|
* `ctrlKey` Boolean
|
|
|
|
|
* `metaKey` Boolean
|
2017-02-12 14:51:36 +00:00
|
|
|
|
* `bounds` [Rectangle](structures/rectangle.md) - tray icon 的 bounds
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
当 tray icon 被双击的时候发出事件。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### Event: 'balloon-show' _Windows_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
当 tray 气泡显示的时候发出事件。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### Event: 'balloon-click' _Windows_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
当 tray 气泡被点击的时候发出事件。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### Event: 'balloon-closed' _Windows_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
当 tray 气泡关闭的时候发出事件,因为超时或人为关闭。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### Event: 'drop' _macOS_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
当 tray icon 上的任何可拖动项被删除的时候发出事件。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### Event: 'drop-files' _macOS_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
|
|
|
|
* `event`
|
|
|
|
|
* `files` Array - 已删除文件的路径.
|
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
当 tray icon 上的可拖动文件被删除的时候发出事件。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### Event: 'drag-enter' _macOS_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
当一个拖动操作进入 tray icon 的时候发出事件。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### Event: 'drag-leave' _macOS_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
当一个拖动操作离开 tray icon 的时候发出事件。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### Event: 'drag-end' _macOS_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
当一个拖动操作在 tray icon 上或其它地方停止拖动的时候发出事件。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
### 方法
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
`Tray` 模块有以下方法:
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### `tray.destroy()`
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
立刻删除 tray icon。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### `tray.setImage(image)`
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
* `image` ([NativeImage](native-image.md) | String)
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
让 `image` 与 tray icon 关联起来。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### `tray.setPressedImage(image)` _macOS_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
|
|
|
|
* `image` [NativeImage](native-image.md)
|
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
当在 macOS 上按压 tray icon 的时候,让 `image` 与 tray icon 关联起来。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### `tray.setToolTip(toolTip)`
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
|
|
|
|
* `toolTip` String
|
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
为 tray icon 设置 hover text。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### `tray.setTitle(title)` _macOS_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
|
|
|
|
* `title` String
|
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
在状态栏沿着 tray icon 设置标题。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### `tray.setHighlightMode(mode)` _macOS_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
* `mode` String - Highlight mode with one of the following values:
|
|
|
|
|
* `selection` - Highlight the tray icon when it is clicked and also when
|
|
|
|
|
its context menu is open. This is the default.
|
|
|
|
|
* `always` - Always highlight the tray icon.
|
|
|
|
|
* `never` - Never highlight the tray icon.
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
设置 tray icon 的背景色变为高亮(blue)。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
**注意:** 你可以使用 `highlightMode` 和一个 [`BrowserWindow`](browser-window.md)
|
|
|
|
|
通过在窗口可见性时切换 `'never'` 和 `'always'` 模式变化。
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
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.displayBalloon(options)` _Windows_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
|
|
|
|
* `options` Object
|
2017-02-12 14:51:36 +00:00
|
|
|
|
* `icon` ([NativeImage](native-image.md) | String) - (可选)
|
|
|
|
|
* `title` String - (可选)
|
|
|
|
|
* `content` String - (可选)
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
展示一个 tray balloon。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### `tray.popUpContextMenu([menu, position])` _macOS_ _Windows_
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
|
|
|
|
* `menu` Menu (optional)
|
|
|
|
|
* `position` Object (可选) - 上托位置.
|
|
|
|
|
* `x` Integer
|
|
|
|
|
* `y` Integer
|
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
从 tray icon 上托出 context menu。当划过 `menu` 的时候, `menu` 显示,代替 tray 的 context menu。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
`position` 只在 windows 上可用,默认为 (0, 0)。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
#### `tray.setContextMenu(menu)`
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
|
|
|
|
* `menu` Menu
|
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
为这个 icon 设置 context menu。
|
|
|
|
|
|
|
|
|
|
#### `tray.getBounds()` _macOS_ _Windows_
|
|
|
|
|
|
|
|
|
|
返回 [`Rectangle`](structures/rectangle.md)
|
|
|
|
|
|
|
|
|
|
这个 tray icon 的 `bounds` 对象。
|
|
|
|
|
|
|
|
|
|
#### `tray.isDestroyed()`
|
|
|
|
|
|
|
|
|
|
返回 `Boolean` - tray icon 是否销毁。
|
2016-03-15 06:00:40 +00:00
|
|
|
|
|
2017-02-12 14:51:36 +00:00
|
|
|
|
[event-emitter]: https://nodejs.org/api/events.html#events_class_eventemitter
|