@trayjs/trayjs 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -0
- package/index.mjs +127 -0
- package/package.json +18 -0
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# @trayjs/trayjs
|
|
2
|
+
|
|
3
|
+
Cross-platform system tray for Node.js. Works on Linux, macOS and Windows.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm install @trayjs/trayjs
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The correct native binary for your platform is installed automatically.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { readFileSync } from 'node:fs';
|
|
17
|
+
import { Tray } from '@trayjs/trayjs';
|
|
18
|
+
|
|
19
|
+
const tray = new Tray({
|
|
20
|
+
tooltip: 'My App',
|
|
21
|
+
icon: readFileSync('icon.png'),
|
|
22
|
+
onMenuRequested: () => [
|
|
23
|
+
{ id: 'open', title: 'Open' },
|
|
24
|
+
{ separator: true },
|
|
25
|
+
{ id: 'quit', title: 'Quit' },
|
|
26
|
+
],
|
|
27
|
+
onClicked: (id) => {
|
|
28
|
+
if (id === 'quit') tray.quit();
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
tray.on('close', () => process.exit(0));
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## API
|
|
36
|
+
|
|
37
|
+
### `new Tray(options)`
|
|
38
|
+
|
|
39
|
+
| Option | Type | Description |
|
|
40
|
+
|--------|------|-------------|
|
|
41
|
+
| `icon` | `Buffer` | PNG icon buffer |
|
|
42
|
+
| `tooltip` | `string` | Tray tooltip text |
|
|
43
|
+
| `onMenuRequested` | `() => MenuItem[] \| Promise<MenuItem[]>` | Called every time the tray menu is opened |
|
|
44
|
+
| `onClicked` | `(id: string) => void` | Called when a menu item is clicked |
|
|
45
|
+
|
|
46
|
+
### `MenuItem`
|
|
47
|
+
|
|
48
|
+
| Field | Type | Description |
|
|
49
|
+
|-------|------|-------------|
|
|
50
|
+
| `id` | `string` | Unique identifier |
|
|
51
|
+
| `title` | `string` | Display text |
|
|
52
|
+
| `tooltip` | `string` | Hover tooltip |
|
|
53
|
+
| `enabled` | `boolean` | Clickable (default `true`) |
|
|
54
|
+
| `checked` | `boolean` | Show check mark |
|
|
55
|
+
| `separator` | `boolean` | Render as separator line |
|
|
56
|
+
|
|
57
|
+
### Methods
|
|
58
|
+
|
|
59
|
+
- `tray.setIcon(pngBuffer)` — update the icon at runtime
|
|
60
|
+
- `tray.setTooltip(text)` — update the tooltip at runtime
|
|
61
|
+
- `tray.quit()` — close the tray
|
|
62
|
+
|
|
63
|
+
### Events
|
|
64
|
+
|
|
65
|
+
- `'ready'` — tray is visible and accepting commands
|
|
66
|
+
- `'close'` — tray process exited
|
|
67
|
+
|
|
68
|
+
## Supported platforms
|
|
69
|
+
|
|
70
|
+
| Platform | Package |
|
|
71
|
+
|----------|---------|
|
|
72
|
+
| Linux x64 | @trayjs/linux-x64 |
|
|
73
|
+
| Linux arm64 | @trayjs/linux-arm64 |
|
|
74
|
+
| macOS x64 | @trayjs/darwin-x64 |
|
|
75
|
+
| macOS arm64 | @trayjs/darwin-arm64 |
|
|
76
|
+
| Windows x64 | @trayjs/win32-x64 |
|
|
77
|
+
| Windows arm64 | @trayjs/win32-arm64 |
|
package/index.mjs
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { createInterface } from 'node:readline';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { dirname, join } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { EventEmitter } from 'node:events';
|
|
7
|
+
|
|
8
|
+
const require = createRequire(import.meta.url);
|
|
9
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
|
|
11
|
+
const PLATFORMS = {
|
|
12
|
+
'linux-x64': '@trayjs/linux-x64',
|
|
13
|
+
'linux-arm64': '@trayjs/linux-arm64',
|
|
14
|
+
'darwin-x64': '@trayjs/darwin-x64',
|
|
15
|
+
'darwin-arm64': '@trayjs/darwin-arm64',
|
|
16
|
+
'win32-x64': '@trayjs/win32-x64',
|
|
17
|
+
'win32-arm64': '@trayjs/win32-arm64',
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const BIN_NAME = process.platform === 'win32' ? 'tray.exe' : 'tray';
|
|
21
|
+
|
|
22
|
+
function getBinaryPath() {
|
|
23
|
+
const key = `${process.platform}-${process.arch}`;
|
|
24
|
+
const pkg = PLATFORMS[key];
|
|
25
|
+
if (!pkg)
|
|
26
|
+
throw new Error(`@trayjs/trayjs: unsupported platform ${key}`);
|
|
27
|
+
try {
|
|
28
|
+
// Published: resolve from installed optional dependency.
|
|
29
|
+
const pkgJson = require.resolve(`${pkg}/package.json`);
|
|
30
|
+
return join(dirname(pkgJson), 'bin', BIN_NAME);
|
|
31
|
+
} catch {
|
|
32
|
+
// Monorepo dev: binary is in sibling platform package.
|
|
33
|
+
return join(__dirname, '..', key, 'bin', BIN_NAME);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @typedef {object} MenuItem
|
|
39
|
+
* @property {string} id
|
|
40
|
+
* @property {string} [title]
|
|
41
|
+
* @property {string} [tooltip]
|
|
42
|
+
* @property {boolean} [enabled]
|
|
43
|
+
* @property {boolean} [checked]
|
|
44
|
+
* @property {boolean} [separator]
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @typedef {object} TrayOptions
|
|
49
|
+
* @property {Buffer} [icon] - PNG buffer for the tray icon
|
|
50
|
+
* @property {string} [tooltip]
|
|
51
|
+
* @property {() => MenuItem[] | Promise<MenuItem[]>} [onMenuRequested]
|
|
52
|
+
* @property {(id: string) => void} [onClicked]
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
export class Tray extends EventEmitter {
|
|
56
|
+
#proc;
|
|
57
|
+
#rl;
|
|
58
|
+
#menuRequestedCb;
|
|
59
|
+
#clickedCb;
|
|
60
|
+
#pendingIcon;
|
|
61
|
+
|
|
62
|
+
/** @param {TrayOptions} opts */
|
|
63
|
+
constructor({ icon, tooltip, onMenuRequested, onClicked } = {}) {
|
|
64
|
+
super();
|
|
65
|
+
this.#menuRequestedCb = onMenuRequested;
|
|
66
|
+
this.#clickedCb = onClicked;
|
|
67
|
+
this.#pendingIcon = icon;
|
|
68
|
+
|
|
69
|
+
const bin = getBinaryPath();
|
|
70
|
+
const args = [];
|
|
71
|
+
if (tooltip) args.push('--tooltip', tooltip);
|
|
72
|
+
|
|
73
|
+
this.#proc = spawn(bin, args, {
|
|
74
|
+
stdio: ['pipe', 'pipe', 'inherit'],
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
this.#rl = createInterface({ input: this.#proc.stdout });
|
|
78
|
+
this.#rl.on('line', (line) => this.#handle(JSON.parse(line)));
|
|
79
|
+
this.#proc.on('close', (code) => this.emit('close', code));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
#send(msg) {
|
|
83
|
+
this.#proc.stdin.write(JSON.stringify(msg) + '\n');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async #handle(msg) {
|
|
87
|
+
switch (msg.method) {
|
|
88
|
+
case 'ready':
|
|
89
|
+
if (this.#pendingIcon) {
|
|
90
|
+
this.setIcon(this.#pendingIcon);
|
|
91
|
+
this.#pendingIcon = null;
|
|
92
|
+
}
|
|
93
|
+
await this.#refreshMenu();
|
|
94
|
+
this.emit('ready');
|
|
95
|
+
break;
|
|
96
|
+
case 'menuRequested':
|
|
97
|
+
await this.#refreshMenu();
|
|
98
|
+
break;
|
|
99
|
+
case 'clicked':
|
|
100
|
+
this.#clickedCb?.(msg.params.id);
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async #refreshMenu() {
|
|
106
|
+
if (!this.#menuRequestedCb) return;
|
|
107
|
+
const items = await this.#menuRequestedCb();
|
|
108
|
+
this.#send({ method: 'setMenu', params: { items } });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** @param {Buffer} pngBuffer */
|
|
112
|
+
setIcon(pngBuffer) {
|
|
113
|
+
this.#send({
|
|
114
|
+
method: 'setIcon',
|
|
115
|
+
params: { base64: pngBuffer.toString('base64') },
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** @param {string} text */
|
|
120
|
+
setTooltip(text) {
|
|
121
|
+
this.#send({ method: 'setTooltip', params: { text } });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
quit() {
|
|
125
|
+
this.#send({ method: 'quit' });
|
|
126
|
+
}
|
|
127
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@trayjs/trayjs",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": "./index.mjs",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.mjs"
|
|
9
|
+
],
|
|
10
|
+
"optionalDependencies": {
|
|
11
|
+
"@trayjs/linux-x64": "0.0.1",
|
|
12
|
+
"@trayjs/linux-arm64": "0.0.1",
|
|
13
|
+
"@trayjs/darwin-x64": "0.0.1",
|
|
14
|
+
"@trayjs/darwin-arm64": "0.0.1",
|
|
15
|
+
"@trayjs/win32-x64": "0.0.1",
|
|
16
|
+
"@trayjs/win32-arm64": "0.0.1"
|
|
17
|
+
}
|
|
18
|
+
}
|