@orielweb/api 0.5.0 → 0.6.0
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/LICENSE +21 -21
- package/README.md +9 -8
- package/package.json +3 -1
- package/src/core.js +290 -288
- package/src/dpi.js +392 -392
- package/src/event.js +559 -471
- package/src/fs.js +19 -5
- package/src/image.js +2 -1
- package/src/internal.js +127 -81
- package/src/menu.js +1218 -1216
- package/src/mocks.js +59 -18
- package/src/path.js +335 -335
- package/src/tray.js +451 -451
- package/src/window.js +114 -39
- package/types/core.d.ts +2 -0
- package/types/event.d.ts +50 -4
- package/types/fs.d.ts +21 -4
- package/types/internal.d.ts +15 -0
- package/types/mocks.d.ts +16 -12
- package/types/window.d.ts +41 -26
package/src/tray.js
CHANGED
|
@@ -1,451 +1,451 @@
|
|
|
1
|
-
// 对标 `@tauri-apps/api/tray` 的**托盘模块**。
|
|
2
|
-
//
|
|
3
|
-
// 与 Tauri 的对应关系与差别(如实标注,不假装一致):
|
|
4
|
-
//
|
|
5
|
-
// 1. **句柄是 id 字符串,不是 Resource**。Tauri 的 TrayIcon extends Resource(Rust 侧句柄表);
|
|
6
|
-
// 本库没有句柄表,页面持有的就是一个 id,宿主侧的对象经 `tray.*` 命令寻址。
|
|
7
|
-
// 由此派生:`rid` 是 `id` 的别名(补壳——迁移代码里的 `tray.rid` 不会报错,
|
|
8
|
-
// 但那不是资源句柄;debug 模式下调它会打一条警告)。
|
|
9
|
-
//
|
|
10
|
-
// 2. **图标走 `window.setIcon` 同一套 JsImage 形状**:路径(string)/ PNG·ICO 字节 /
|
|
11
|
-
// Image 实例(含裸 RGBA)。宿主解码(OrielImageCodec),三平台一致。
|
|
12
|
-
//
|
|
13
|
-
// 3. **`setTempDirPath` 是补壳**:那是 Tauri 在 Linux 上"图标要落盘给 AppIndicator 读"
|
|
14
|
-
// 的约束带来的 API;本库走 GtkStatusIcon 直吃 pixbuf,不需要临时目录。
|
|
15
|
-
// 调用不报错(零删改),但 debug 模式下会打警告——它对本库没有任何作用。
|
|
16
|
-
//
|
|
17
|
-
// 4. **`setMenuOnLeftClick` / `setShowMenuOnLeftClick` 只在 Windows 生效**:
|
|
18
|
-
// Tauri 标注 Linux Unsupported(平台惯例左键就弹菜单);本库 macOS 后端同样是
|
|
19
|
-
// 平台惯例行为,暂未接通开关,非 Windows 平台抛 `not_supported`——
|
|
20
|
-
// 宁可当场报错,也不让"关掉了左键菜单"变成一句空话。
|
|
21
|
-
//
|
|
22
|
-
// 5. **菜单项的 `action` 回调**复用 `menu.itemclicked` 事件通路:带 action 的项在
|
|
23
|
-
// `setMenu` 时被加上本托盘专属的 id 前缀,点击回来后按前缀认领。
|
|
24
|
-
// 与 menu.js 的规矩一样:action 只对**有 id 的项**有效。
|
|
25
|
-
|
|
26
|
-
import { invoke } from './core.js';
|
|
27
|
-
import { getTransport } from './internal.js';
|
|
28
|
-
import { toImageBytes, bytesToBase64, assertImageFormat } from './image-bytes.js';
|
|
29
|
-
import { __setDebugWarnings, warnCompat } from './internal.js';
|
|
30
|
-
|
|
31
|
-
/** 托盘事件名(宿主侧 OrielApp.TrayEventName)。 */
|
|
32
|
-
const TRAY_EVENT = 'tray.event';
|
|
33
|
-
|
|
34
|
-
/** 菜单项点击事件名(宿主侧 OrielApp.MenuItemClickedEventName)。 */
|
|
35
|
-
const ITEM_CLICKED_EVENT = 'menu.itemclicked';
|
|
36
|
-
|
|
37
|
-
// 补壳警告工具统一在 internal.js(B14):tray 的 rid/setTempDirPath 与 Image.close/
|
|
38
|
-
// Menu.rid/WebviewWindow.setAutoResize 共用同一套开关与文案前缀。
|
|
39
|
-
export { __setDebugWarnings };
|
|
40
|
-
|
|
41
|
-
/** 鸭子类型认 Image 实例(与 window.js 同一套判据:isOrielImage 标记)。 */
|
|
42
|
-
/** @param {unknown} value */
|
|
43
|
-
function isImageLike(value) {
|
|
44
|
-
return value !== null
|
|
45
|
-
&& typeof value === 'object'
|
|
46
|
-
&& (/** @type {Record<string, unknown>} */ (value)).isOrielImage === true;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* 把 `JsImage` 编成 `tray.setIcon` 能吃的参数(与 `window.setIcon` 三形态一致)。
|
|
51
|
-
* @param {unknown} icon
|
|
52
|
-
* @returns {Record<string, unknown>}
|
|
53
|
-
*/
|
|
54
|
-
function encodeIcon(icon) {
|
|
55
|
-
if (typeof icon === 'string') {
|
|
56
|
-
if (icon === '') {
|
|
57
|
-
throw new TypeError('setIcon: 路径不能是空字符串');
|
|
58
|
-
}
|
|
59
|
-
return { iconPath: icon };
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (isImageLike(icon)) {
|
|
63
|
-
const image = /** @type {any} */ (icon);
|
|
64
|
-
if (image.hasEncodedBytes === false) {
|
|
65
|
-
return {
|
|
66
|
-
rgba: bytesToBase64(image.rgbaBytes),
|
|
67
|
-
width: image.width,
|
|
68
|
-
height: image.height,
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
return { bytes: bytesToBase64(image.bytes) };
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const bytes = toImageBytes(icon);
|
|
75
|
-
if (bytes !== null) {
|
|
76
|
-
assertImageFormat(bytes, 'setIcon');
|
|
77
|
-
return { bytes: bytesToBase64(bytes) };
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
throw new TypeError(
|
|
81
|
-
'setIcon: icon 必须是文件路径(string)、Uint8Array / ArrayBuffer / number[] 形式的' +
|
|
82
|
-
'PNG/ICO 字节,或 @orielweb/api/image 的 Image 实例。');
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* 把一棵菜单树编成 `tray.setMenu` 的 `items`(线格式与 menu.js 的 wire 一致)。
|
|
87
|
-
*
|
|
88
|
-
* 带 `action` 的项会获得 `tray:<token>:<前id>` 形式的 id 前缀,点击回来后按前缀认领。
|
|
89
|
-
* @param {any} menu Menu / Submenu 实例(或已经是线格式的数组)。
|
|
90
|
-
* @returns {{ items: unknown[], actions: Map<string, Function>, token: string }}
|
|
91
|
-
*/
|
|
92
|
-
function wireMenu(menu) {
|
|
93
|
-
const actions = new Map();
|
|
94
|
-
const token = 'tray' + Math.random().toString(36).slice(2, 10);
|
|
95
|
-
|
|
96
|
-
if (menu === null || menu === undefined) {
|
|
97
|
-
return { items: [], actions, token };
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// 已经是线格式数组(或 Menu 实例的 _items)
|
|
101
|
-
const rawItems = Array.isArray(menu)
|
|
102
|
-
? menu
|
|
103
|
-
: Array.isArray(menu._items)
|
|
104
|
-
? menu._items
|
|
105
|
-
: [menu];
|
|
106
|
-
|
|
107
|
-
const items = rawItems.map((/** @type {any} */ item) => {
|
|
108
|
-
if (item && typeof item._wire === 'function') {
|
|
109
|
-
return item._wire(actions, token);
|
|
110
|
-
}
|
|
111
|
-
return item;
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
return { items, actions, token };
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/** 认领路由表:token → (原始id → 回调)。多托盘并存,按 token 索引。 */
|
|
118
|
-
const clickRouting = new Map();
|
|
119
|
-
/** 当前监听器挂在哪个 transport 上(mocks 切换后必须重装,否则点击永远不触发)。 */
|
|
120
|
-
/** @type {any} */
|
|
121
|
-
let clickListenerTransport = null;
|
|
122
|
-
/** @type {(() => void) | null} */
|
|
123
|
-
let clickListenerUnsubscribe = null;
|
|
124
|
-
|
|
125
|
-
async function ensureClickListener() {
|
|
126
|
-
const transport = getTransport();
|
|
127
|
-
if (clickListenerTransport === transport) {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
// 换了 transport(测试里换 mock、或页面自己 setTransportOverride):旧监听器已无意义
|
|
131
|
-
if (clickListenerUnsubscribe) {
|
|
132
|
-
clickListenerUnsubscribe();
|
|
133
|
-
}
|
|
134
|
-
clickListenerTransport = transport;
|
|
135
|
-
const on = transport.on;
|
|
136
|
-
if (typeof on !== 'function') {
|
|
137
|
-
throw new Error('orielweb/tray: 传输层不支持 on()——无法订阅托盘菜单点击');
|
|
138
|
-
}
|
|
139
|
-
clickListenerUnsubscribe = on.call(transport, ITEM_CLICKED_EVENT, (/** @type {any} */ value) => {
|
|
140
|
-
const id = value && typeof value === 'object' ? value.id : null;
|
|
141
|
-
if (typeof id !== 'string') {
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
// wire 出来的 id 形如 `<token>:<原始id>`;token 由本模块生成(tray 前缀)。
|
|
145
|
-
const sep = id.indexOf(':');
|
|
146
|
-
if (sep <= 0) {
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
const token = id.slice(0, sep);
|
|
150
|
-
const originalId = id.slice(sep + 1);
|
|
151
|
-
const route = clickRouting.get(token);
|
|
152
|
-
const handler = route ? route.get(originalId) : undefined;
|
|
153
|
-
if (typeof handler === 'function') {
|
|
154
|
-
try { handler(originalId); }
|
|
155
|
-
catch (err) { console.error('tray 菜单 action 处理器抛出', err); }
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* 系统托盘图标(对标 Tauri 2 的 `TrayIcon`)。
|
|
162
|
-
*
|
|
163
|
-
* 用 {@linkcode TrayIcon.new} 创建;本库中它**不是资源句柄**(见文件头说明)。
|
|
164
|
-
*
|
|
165
|
-
* @example
|
|
166
|
-
* ```js
|
|
167
|
-
* import { TrayIcon } from '@orielweb/api/tray';
|
|
168
|
-
* const tray = await TrayIcon.new({ tooltip: 'awesome tray tooltip' });
|
|
169
|
-
* await tray.setTooltip('new tooltip');
|
|
170
|
-
* ```
|
|
171
|
-
*/
|
|
172
|
-
export class TrayIcon {
|
|
173
|
-
/**
|
|
174
|
-
* @param {string} id 宿主侧的托盘 id(页面持有的唯一标识)。
|
|
175
|
-
* @internal 请用 {@linkcode TrayIcon.new}。
|
|
176
|
-
*/
|
|
177
|
-
constructor(id) {
|
|
178
|
-
/** 本托盘的 id。 */
|
|
179
|
-
this.id = id;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* 资源句柄的**补壳**:本库没有资源表,`rid` 就是 `id`。
|
|
184
|
-
*
|
|
185
|
-
* 迁移代码里的 `tray.rid` 不会报错,但它不是 Rust 侧句柄——
|
|
186
|
-
* debug 模式下调用会打一条警告(补壳必须可观测)。
|
|
187
|
-
*/
|
|
188
|
-
get rid() {
|
|
189
|
-
warnCompat('TrayIcon.rid 是补壳:本库没有资源表,返回值就是 id。');
|
|
190
|
-
return this.id;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* 按 id 取回句柄(不存在时返回 null)。
|
|
195
|
-
*
|
|
196
|
-
* @param {string} id
|
|
197
|
-
* @returns {Promise<TrayIcon | null>}
|
|
198
|
-
*/
|
|
199
|
-
static async getById(id) {
|
|
200
|
-
if (typeof id !== 'string' || id === '') {
|
|
201
|
-
throw new TypeError('TrayIcon.getById: id 必须为非空字符串');
|
|
202
|
-
}
|
|
203
|
-
const found = await invoke('tray.getById', { id });
|
|
204
|
-
return found === null || found === undefined ? null : new TrayIcon(found);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* 按 id 移除托盘(图标随之消失)。
|
|
209
|
-
*
|
|
210
|
-
* @param {string} id
|
|
211
|
-
* @returns {Promise<boolean>} 是否确实移除了一个。
|
|
212
|
-
*/
|
|
213
|
-
static async removeById(id) {
|
|
214
|
-
if (typeof id !== 'string' || id === '') {
|
|
215
|
-
throw new TypeError('TrayIcon.removeById: id 必须为非空字符串');
|
|
216
|
-
}
|
|
217
|
-
return invoke('tray.removeById', { id });
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* 新建托盘图标。
|
|
222
|
-
*
|
|
223
|
-
* @param {{ id?: string, menu?: any, icon?: any, tooltip?: string, title?: string,
|
|
224
|
-
* tempDirPath?: string, iconAsTemplate?: boolean, menuOnLeftClick?: boolean,
|
|
225
|
-
* showMenuOnLeftClick?: boolean, action?: (event: any) => void }} [options]
|
|
226
|
-
* @returns {Promise<TrayIcon>}
|
|
227
|
-
*
|
|
228
|
-
* #### 平台差异
|
|
229
|
-
* - **Linux**:有时要设了菜单图标才可见(Tauri 同);`showMenuOnLeftClick` 无效。
|
|
230
|
-
* - **Windows**:`title` 不支持(平台无此概念)。
|
|
231
|
-
* - **macOS**:`iconAsTemplate` 决定菜单栏图标的模板着色。
|
|
232
|
-
*/
|
|
233
|
-
static async new(options = {}) {
|
|
234
|
-
const opts = options ?? {};
|
|
235
|
-
|
|
236
|
-
const args = /** @type {Record<string, unknown>} */ ({
|
|
237
|
-
id: typeof opts.id === 'string' && opts.id !== '' ? opts.id : null,
|
|
238
|
-
tooltip: typeof opts.tooltip === 'string' ? opts.tooltip : null,
|
|
239
|
-
title: typeof opts.title === 'string' ? opts.title : null,
|
|
240
|
-
iconAsTemplate: opts.iconAsTemplate !== false,
|
|
241
|
-
menuOnClick: opts.showMenuOnLeftClick ?? opts.menuOnLeftClick ?? false,
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
if (opts.icon !== undefined && opts.icon !== null) {
|
|
245
|
-
Object.assign(args, encodeIcon(opts.icon));
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// 菜单:先装配 action 路由再发命令(菜单可能立刻被用户点)
|
|
249
|
-
if (opts.menu !== undefined && opts.menu !== null) {
|
|
250
|
-
const wired = wireMenu(opts.menu);
|
|
251
|
-
await ensureClickListener();
|
|
252
|
-
clickRouting.set(wired.token, wired.actions);
|
|
253
|
-
args.items = wired.items;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
if (opts.tempDirPath !== undefined) {
|
|
257
|
-
warnCompat('TrayIconOptions.tempDirPath 是补壳:本库不落盘图标,该参数不生效。');
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
const id = await invoke('tray.new', args);
|
|
261
|
-
|
|
262
|
-
const tray = new TrayIcon(id);
|
|
263
|
-
|
|
264
|
-
if (typeof opts.action === 'function') {
|
|
265
|
-
tray.onEvent(opts.action);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
return tray;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* 订阅本托盘的事件(对标 Tauri 的 `TrayIconOptions.action`,也可在 `new` 后随时加)。
|
|
273
|
-
*
|
|
274
|
-
* handler 收到 `{ id, type, button, buttonState, position, rect }`;
|
|
275
|
-
* 平台未提供的字段为 `null`(Linux 的 button、macOS 的 position 等)。
|
|
276
|
-
*
|
|
277
|
-
* @param {(event: any) => void} handler
|
|
278
|
-
* @returns {() => void} 取消订阅函数。
|
|
279
|
-
*/
|
|
280
|
-
onEvent(handler) {
|
|
281
|
-
if (typeof handler !== 'function') {
|
|
282
|
-
throw new TypeError('TrayIcon.onEvent: 需要一个函数');
|
|
283
|
-
}
|
|
284
|
-
const transport = getTransport();
|
|
285
|
-
const on = transport.on;
|
|
286
|
-
if (typeof on !== 'function') {
|
|
287
|
-
throw new Error('orielweb/tray: 传输层不支持 on()——无法订阅托盘事件');
|
|
288
|
-
}
|
|
289
|
-
const off = on.call(transport, TRAY_EVENT, (/** @type {any} */ value) => {
|
|
290
|
-
if (value && value.id === this.id) {
|
|
291
|
-
try { handler(value); }
|
|
292
|
-
catch (err) { console.error('TrayIcon.onEvent 处理器抛出', err); }
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
return off;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* 设置新图标;`null` 移除图标。
|
|
300
|
-
*
|
|
301
|
-
* @param {any} icon
|
|
302
|
-
* @returns {Promise<void>}
|
|
303
|
-
*/
|
|
304
|
-
setIcon(icon) {
|
|
305
|
-
if (icon === null || icon === undefined) {
|
|
306
|
-
return invoke('tray.setIcon', { id: this.id, iconPath: null });
|
|
307
|
-
}
|
|
308
|
-
return invoke('tray.setIcon', { id: this.id, ...encodeIcon(icon) });
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* 设置托盘菜单。
|
|
313
|
-
*
|
|
314
|
-
* @param {any} menu
|
|
315
|
-
* @returns {Promise<void>}
|
|
316
|
-
*
|
|
317
|
-
* #### 平台差异
|
|
318
|
-
* - **Linux**:菜单一旦设置就移除不掉,`null` 只清空项(Tauri 同)。
|
|
319
|
-
*/
|
|
320
|
-
async setMenu(menu) {
|
|
321
|
-
const wired = wireMenu(menu);
|
|
322
|
-
await ensureClickListener();
|
|
323
|
-
clickRouting.set(wired.token, wired.actions);
|
|
324
|
-
await invoke('tray.setMenu', { id: this.id, items: wired.items });
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* 设置悬停提示。
|
|
329
|
-
*
|
|
330
|
-
* @param {string | null} tooltip
|
|
331
|
-
* @returns {Promise<void>}
|
|
332
|
-
*
|
|
333
|
-
* #### 平台差异
|
|
334
|
-
* - **Linux**:不支持(GtkStatusIcon 无 tooltip 之外的等价物时行为各异)。
|
|
335
|
-
*/
|
|
336
|
-
setTooltip(tooltip) {
|
|
337
|
-
return invoke('tray.setTooltip', {
|
|
338
|
-
id: this.id,
|
|
339
|
-
tooltip: typeof tooltip === 'string' ? tooltip : null,
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* 设置图标旁的标题文本。
|
|
345
|
-
*
|
|
346
|
-
* @param {string | null} title
|
|
347
|
-
* @returns {Promise<void>}
|
|
348
|
-
*
|
|
349
|
-
* #### 平台差异
|
|
350
|
-
* - **Windows**:不支持。
|
|
351
|
-
* - **Linux**:只有同时有图标时才显示。
|
|
352
|
-
*/
|
|
353
|
-
setTitle(title) {
|
|
354
|
-
return invoke('tray.setTitle', {
|
|
355
|
-
id: this.id,
|
|
356
|
-
title: typeof title === 'string' ? title : null,
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
/**
|
|
361
|
-
* 显示/隐藏本托盘图标。
|
|
362
|
-
*
|
|
363
|
-
* @param {boolean} visible
|
|
364
|
-
* @returns {Promise<void>}
|
|
365
|
-
*/
|
|
366
|
-
setVisible(visible) {
|
|
367
|
-
return invoke('tray.setVisible', { id: this.id, visible: visible !== false });
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
/**
|
|
371
|
-
* 把当前图标设为 macOS 的模板图像(随菜单栏明暗自动着色)。
|
|
372
|
-
*
|
|
373
|
-
* @param {boolean} asTemplate
|
|
374
|
-
* @returns {Promise<void>}
|
|
375
|
-
*
|
|
376
|
-
* #### 平台差异
|
|
377
|
-
* - **macOS 专属**;其他平台接受参数但不生效(与 Tauri 一致)。
|
|
378
|
-
*/
|
|
379
|
-
setIconAsTemplate(asTemplate) {
|
|
380
|
-
return invoke('tray.setIconAsTemplate', {
|
|
381
|
-
id: this.id,
|
|
382
|
-
asTemplate: asTemplate === true,
|
|
383
|
-
});
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
/**
|
|
387
|
-
* 原子地"换图标 + 改模板标记"(Tauri 为了避免两次调用之间闪一下旧模板态)。
|
|
388
|
-
*
|
|
389
|
-
* 本库是两次调用的组合(宿主没有原子入口),短暂闪烁理论上可能出现;
|
|
390
|
-
* 与逐次调 `setIcon` + `setIconAsTemplate` 的差别只在顺序保证。
|
|
391
|
-
*
|
|
392
|
-
* @param {any} icon
|
|
393
|
-
* @param {boolean} asTemplate
|
|
394
|
-
* @returns {Promise<void>}
|
|
395
|
-
*/
|
|
396
|
-
async setIconWithAsTemplate(icon, asTemplate) {
|
|
397
|
-
await this.setIcon(icon);
|
|
398
|
-
await this.setIconAsTemplate(asTemplate);
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* 开关"左键点击弹出菜单"(`setShowMenuOnLeftClick` 的旧名,Tauri 已弃用同款)。
|
|
403
|
-
*
|
|
404
|
-
* @param {boolean} onLeft
|
|
405
|
-
* @returns {Promise<void>}
|
|
406
|
-
* @deprecated 用 {@linkcode TrayIcon.setShowMenuOnLeftClick}。
|
|
407
|
-
*/
|
|
408
|
-
setMenuOnLeftClick(onLeft) {
|
|
409
|
-
return this.setShowMenuOnLeftClick(onLeft);
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* 开关"左键点击弹出菜单"(默认开)。
|
|
414
|
-
*
|
|
415
|
-
* 关掉后左键点击交给你自己的 `action` 处理器,右键仍弹菜单。
|
|
416
|
-
*
|
|
417
|
-
* @param {boolean} onLeft
|
|
418
|
-
* @returns {Promise<void>}
|
|
419
|
-
*
|
|
420
|
-
* #### 平台差异
|
|
421
|
-
* - **Windows**:生效。
|
|
422
|
-
* - **Linux / macOS**:抛 `not_supported`——那两个平台"左键弹菜单"是平台惯例,
|
|
423
|
-
* 本库后端暂未接通开关(Tauri 标注 Linux Unsupported;macOS 有 API 而我们没接)。
|
|
424
|
-
*/
|
|
425
|
-
setShowMenuOnLeftClick(onLeft) {
|
|
426
|
-
return invoke('tray.setMenuOnLeftClick', {
|
|
427
|
-
id: this.id,
|
|
428
|
-
onLeft: onLeft !== false,
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
* 设置图标临时目录。**Linux 专属补壳**:本库不落盘图标,调用不报错但不生效。
|
|
434
|
-
*
|
|
435
|
-
* @param {string | null} _path
|
|
436
|
-
* @returns {Promise<void>}
|
|
437
|
-
*/
|
|
438
|
-
setTempDirPath(_path) {
|
|
439
|
-
warnCompat('TrayIcon.setTempDirPath 是补壳:本库不落盘图标,该调用不生效。');
|
|
440
|
-
return Promise.resolve();
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
/**
|
|
444
|
-
* 移除本托盘(等价 `TrayIcon.removeById(this.id)`)。
|
|
445
|
-
*
|
|
446
|
-
* @returns {Promise<boolean>}
|
|
447
|
-
*/
|
|
448
|
-
close() {
|
|
449
|
-
return TrayIcon.removeById(this.id);
|
|
450
|
-
}
|
|
451
|
-
}
|
|
1
|
+
// 对标 `@tauri-apps/api/tray` 的**托盘模块**。
|
|
2
|
+
//
|
|
3
|
+
// 与 Tauri 的对应关系与差别(如实标注,不假装一致):
|
|
4
|
+
//
|
|
5
|
+
// 1. **句柄是 id 字符串,不是 Resource**。Tauri 的 TrayIcon extends Resource(Rust 侧句柄表);
|
|
6
|
+
// 本库没有句柄表,页面持有的就是一个 id,宿主侧的对象经 `tray.*` 命令寻址。
|
|
7
|
+
// 由此派生:`rid` 是 `id` 的别名(补壳——迁移代码里的 `tray.rid` 不会报错,
|
|
8
|
+
// 但那不是资源句柄;debug 模式下调它会打一条警告)。
|
|
9
|
+
//
|
|
10
|
+
// 2. **图标走 `window.setIcon` 同一套 JsImage 形状**:路径(string)/ PNG·ICO 字节 /
|
|
11
|
+
// Image 实例(含裸 RGBA)。宿主解码(OrielImageCodec),三平台一致。
|
|
12
|
+
//
|
|
13
|
+
// 3. **`setTempDirPath` 是补壳**:那是 Tauri 在 Linux 上"图标要落盘给 AppIndicator 读"
|
|
14
|
+
// 的约束带来的 API;本库走 GtkStatusIcon 直吃 pixbuf,不需要临时目录。
|
|
15
|
+
// 调用不报错(零删改),但 debug 模式下会打警告——它对本库没有任何作用。
|
|
16
|
+
//
|
|
17
|
+
// 4. **`setMenuOnLeftClick` / `setShowMenuOnLeftClick` 只在 Windows 生效**:
|
|
18
|
+
// Tauri 标注 Linux Unsupported(平台惯例左键就弹菜单);本库 macOS 后端同样是
|
|
19
|
+
// 平台惯例行为,暂未接通开关,非 Windows 平台抛 `not_supported`——
|
|
20
|
+
// 宁可当场报错,也不让"关掉了左键菜单"变成一句空话。
|
|
21
|
+
//
|
|
22
|
+
// 5. **菜单项的 `action` 回调**复用 `menu.itemclicked` 事件通路:带 action 的项在
|
|
23
|
+
// `setMenu` 时被加上本托盘专属的 id 前缀,点击回来后按前缀认领。
|
|
24
|
+
// 与 menu.js 的规矩一样:action 只对**有 id 的项**有效。
|
|
25
|
+
|
|
26
|
+
import { invoke } from './core.js';
|
|
27
|
+
import { getTransport } from './internal.js';
|
|
28
|
+
import { toImageBytes, bytesToBase64, assertImageFormat } from './image-bytes.js';
|
|
29
|
+
import { __setDebugWarnings, warnCompat } from './internal.js';
|
|
30
|
+
|
|
31
|
+
/** 托盘事件名(宿主侧 OrielApp.TrayEventName)。 */
|
|
32
|
+
const TRAY_EVENT = 'tray.event';
|
|
33
|
+
|
|
34
|
+
/** 菜单项点击事件名(宿主侧 OrielApp.MenuItemClickedEventName)。 */
|
|
35
|
+
const ITEM_CLICKED_EVENT = 'menu.itemclicked';
|
|
36
|
+
|
|
37
|
+
// 补壳警告工具统一在 internal.js(B14):tray 的 rid/setTempDirPath 与 Image.close/
|
|
38
|
+
// Menu.rid/WebviewWindow.setAutoResize 共用同一套开关与文案前缀。
|
|
39
|
+
export { __setDebugWarnings };
|
|
40
|
+
|
|
41
|
+
/** 鸭子类型认 Image 实例(与 window.js 同一套判据:isOrielImage 标记)。 */
|
|
42
|
+
/** @param {unknown} value */
|
|
43
|
+
function isImageLike(value) {
|
|
44
|
+
return value !== null
|
|
45
|
+
&& typeof value === 'object'
|
|
46
|
+
&& (/** @type {Record<string, unknown>} */ (value)).isOrielImage === true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 把 `JsImage` 编成 `tray.setIcon` 能吃的参数(与 `window.setIcon` 三形态一致)。
|
|
51
|
+
* @param {unknown} icon
|
|
52
|
+
* @returns {Record<string, unknown>}
|
|
53
|
+
*/
|
|
54
|
+
function encodeIcon(icon) {
|
|
55
|
+
if (typeof icon === 'string') {
|
|
56
|
+
if (icon === '') {
|
|
57
|
+
throw new TypeError('setIcon: 路径不能是空字符串');
|
|
58
|
+
}
|
|
59
|
+
return { iconPath: icon };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (isImageLike(icon)) {
|
|
63
|
+
const image = /** @type {any} */ (icon);
|
|
64
|
+
if (image.hasEncodedBytes === false) {
|
|
65
|
+
return {
|
|
66
|
+
rgba: bytesToBase64(image.rgbaBytes),
|
|
67
|
+
width: image.width,
|
|
68
|
+
height: image.height,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return { bytes: bytesToBase64(image.bytes) };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const bytes = toImageBytes(icon);
|
|
75
|
+
if (bytes !== null) {
|
|
76
|
+
assertImageFormat(bytes, 'setIcon');
|
|
77
|
+
return { bytes: bytesToBase64(bytes) };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
throw new TypeError(
|
|
81
|
+
'setIcon: icon 必须是文件路径(string)、Uint8Array / ArrayBuffer / number[] 形式的' +
|
|
82
|
+
'PNG/ICO 字节,或 @orielweb/api/image 的 Image 实例。');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 把一棵菜单树编成 `tray.setMenu` 的 `items`(线格式与 menu.js 的 wire 一致)。
|
|
87
|
+
*
|
|
88
|
+
* 带 `action` 的项会获得 `tray:<token>:<前id>` 形式的 id 前缀,点击回来后按前缀认领。
|
|
89
|
+
* @param {any} menu Menu / Submenu 实例(或已经是线格式的数组)。
|
|
90
|
+
* @returns {{ items: unknown[], actions: Map<string, Function>, token: string }}
|
|
91
|
+
*/
|
|
92
|
+
function wireMenu(menu) {
|
|
93
|
+
const actions = new Map();
|
|
94
|
+
const token = 'tray' + Math.random().toString(36).slice(2, 10);
|
|
95
|
+
|
|
96
|
+
if (menu === null || menu === undefined) {
|
|
97
|
+
return { items: [], actions, token };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// 已经是线格式数组(或 Menu 实例的 _items)
|
|
101
|
+
const rawItems = Array.isArray(menu)
|
|
102
|
+
? menu
|
|
103
|
+
: Array.isArray(menu._items)
|
|
104
|
+
? menu._items
|
|
105
|
+
: [menu];
|
|
106
|
+
|
|
107
|
+
const items = rawItems.map((/** @type {any} */ item) => {
|
|
108
|
+
if (item && typeof item._wire === 'function') {
|
|
109
|
+
return item._wire(actions, token);
|
|
110
|
+
}
|
|
111
|
+
return item;
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
return { items, actions, token };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** 认领路由表:token → (原始id → 回调)。多托盘并存,按 token 索引。 */
|
|
118
|
+
const clickRouting = new Map();
|
|
119
|
+
/** 当前监听器挂在哪个 transport 上(mocks 切换后必须重装,否则点击永远不触发)。 */
|
|
120
|
+
/** @type {any} */
|
|
121
|
+
let clickListenerTransport = null;
|
|
122
|
+
/** @type {(() => void) | null} */
|
|
123
|
+
let clickListenerUnsubscribe = null;
|
|
124
|
+
|
|
125
|
+
async function ensureClickListener() {
|
|
126
|
+
const transport = getTransport();
|
|
127
|
+
if (clickListenerTransport === transport) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
// 换了 transport(测试里换 mock、或页面自己 setTransportOverride):旧监听器已无意义
|
|
131
|
+
if (clickListenerUnsubscribe) {
|
|
132
|
+
clickListenerUnsubscribe();
|
|
133
|
+
}
|
|
134
|
+
clickListenerTransport = transport;
|
|
135
|
+
const on = transport.on;
|
|
136
|
+
if (typeof on !== 'function') {
|
|
137
|
+
throw new Error('orielweb/tray: 传输层不支持 on()——无法订阅托盘菜单点击');
|
|
138
|
+
}
|
|
139
|
+
clickListenerUnsubscribe = on.call(transport, ITEM_CLICKED_EVENT, (/** @type {any} */ value) => {
|
|
140
|
+
const id = value && typeof value === 'object' ? value.id : null;
|
|
141
|
+
if (typeof id !== 'string') {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
// wire 出来的 id 形如 `<token>:<原始id>`;token 由本模块生成(tray 前缀)。
|
|
145
|
+
const sep = id.indexOf(':');
|
|
146
|
+
if (sep <= 0) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const token = id.slice(0, sep);
|
|
150
|
+
const originalId = id.slice(sep + 1);
|
|
151
|
+
const route = clickRouting.get(token);
|
|
152
|
+
const handler = route ? route.get(originalId) : undefined;
|
|
153
|
+
if (typeof handler === 'function') {
|
|
154
|
+
try { handler(originalId); }
|
|
155
|
+
catch (err) { console.error('tray 菜单 action 处理器抛出', err); }
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 系统托盘图标(对标 Tauri 2 的 `TrayIcon`)。
|
|
162
|
+
*
|
|
163
|
+
* 用 {@linkcode TrayIcon.new} 创建;本库中它**不是资源句柄**(见文件头说明)。
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```js
|
|
167
|
+
* import { TrayIcon } from '@orielweb/api/tray';
|
|
168
|
+
* const tray = await TrayIcon.new({ tooltip: 'awesome tray tooltip' });
|
|
169
|
+
* await tray.setTooltip('new tooltip');
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
export class TrayIcon {
|
|
173
|
+
/**
|
|
174
|
+
* @param {string} id 宿主侧的托盘 id(页面持有的唯一标识)。
|
|
175
|
+
* @internal 请用 {@linkcode TrayIcon.new}。
|
|
176
|
+
*/
|
|
177
|
+
constructor(id) {
|
|
178
|
+
/** 本托盘的 id。 */
|
|
179
|
+
this.id = id;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* 资源句柄的**补壳**:本库没有资源表,`rid` 就是 `id`。
|
|
184
|
+
*
|
|
185
|
+
* 迁移代码里的 `tray.rid` 不会报错,但它不是 Rust 侧句柄——
|
|
186
|
+
* debug 模式下调用会打一条警告(补壳必须可观测)。
|
|
187
|
+
*/
|
|
188
|
+
get rid() {
|
|
189
|
+
warnCompat('TrayIcon.rid 是补壳:本库没有资源表,返回值就是 id。');
|
|
190
|
+
return this.id;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 按 id 取回句柄(不存在时返回 null)。
|
|
195
|
+
*
|
|
196
|
+
* @param {string} id
|
|
197
|
+
* @returns {Promise<TrayIcon | null>}
|
|
198
|
+
*/
|
|
199
|
+
static async getById(id) {
|
|
200
|
+
if (typeof id !== 'string' || id === '') {
|
|
201
|
+
throw new TypeError('TrayIcon.getById: id 必须为非空字符串');
|
|
202
|
+
}
|
|
203
|
+
const found = await invoke('tray.getById', { id });
|
|
204
|
+
return found === null || found === undefined ? null : new TrayIcon(found);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* 按 id 移除托盘(图标随之消失)。
|
|
209
|
+
*
|
|
210
|
+
* @param {string} id
|
|
211
|
+
* @returns {Promise<boolean>} 是否确实移除了一个。
|
|
212
|
+
*/
|
|
213
|
+
static async removeById(id) {
|
|
214
|
+
if (typeof id !== 'string' || id === '') {
|
|
215
|
+
throw new TypeError('TrayIcon.removeById: id 必须为非空字符串');
|
|
216
|
+
}
|
|
217
|
+
return invoke('tray.removeById', { id });
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 新建托盘图标。
|
|
222
|
+
*
|
|
223
|
+
* @param {{ id?: string, menu?: any, icon?: any, tooltip?: string, title?: string,
|
|
224
|
+
* tempDirPath?: string, iconAsTemplate?: boolean, menuOnLeftClick?: boolean,
|
|
225
|
+
* showMenuOnLeftClick?: boolean, action?: (event: any) => void }} [options]
|
|
226
|
+
* @returns {Promise<TrayIcon>}
|
|
227
|
+
*
|
|
228
|
+
* #### 平台差异
|
|
229
|
+
* - **Linux**:有时要设了菜单图标才可见(Tauri 同);`showMenuOnLeftClick` 无效。
|
|
230
|
+
* - **Windows**:`title` 不支持(平台无此概念)。
|
|
231
|
+
* - **macOS**:`iconAsTemplate` 决定菜单栏图标的模板着色。
|
|
232
|
+
*/
|
|
233
|
+
static async new(options = {}) {
|
|
234
|
+
const opts = options ?? {};
|
|
235
|
+
|
|
236
|
+
const args = /** @type {Record<string, unknown>} */ ({
|
|
237
|
+
id: typeof opts.id === 'string' && opts.id !== '' ? opts.id : null,
|
|
238
|
+
tooltip: typeof opts.tooltip === 'string' ? opts.tooltip : null,
|
|
239
|
+
title: typeof opts.title === 'string' ? opts.title : null,
|
|
240
|
+
iconAsTemplate: opts.iconAsTemplate !== false,
|
|
241
|
+
menuOnClick: opts.showMenuOnLeftClick ?? opts.menuOnLeftClick ?? false,
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
if (opts.icon !== undefined && opts.icon !== null) {
|
|
245
|
+
Object.assign(args, encodeIcon(opts.icon));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// 菜单:先装配 action 路由再发命令(菜单可能立刻被用户点)
|
|
249
|
+
if (opts.menu !== undefined && opts.menu !== null) {
|
|
250
|
+
const wired = wireMenu(opts.menu);
|
|
251
|
+
await ensureClickListener();
|
|
252
|
+
clickRouting.set(wired.token, wired.actions);
|
|
253
|
+
args.items = wired.items;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (opts.tempDirPath !== undefined) {
|
|
257
|
+
warnCompat('TrayIconOptions.tempDirPath 是补壳:本库不落盘图标,该参数不生效。');
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const id = await invoke('tray.new', args);
|
|
261
|
+
|
|
262
|
+
const tray = new TrayIcon(id);
|
|
263
|
+
|
|
264
|
+
if (typeof opts.action === 'function') {
|
|
265
|
+
tray.onEvent(opts.action);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return tray;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* 订阅本托盘的事件(对标 Tauri 的 `TrayIconOptions.action`,也可在 `new` 后随时加)。
|
|
273
|
+
*
|
|
274
|
+
* handler 收到 `{ id, type, button, buttonState, position, rect }`;
|
|
275
|
+
* 平台未提供的字段为 `null`(Linux 的 button、macOS 的 position 等)。
|
|
276
|
+
*
|
|
277
|
+
* @param {(event: any) => void} handler
|
|
278
|
+
* @returns {() => void} 取消订阅函数。
|
|
279
|
+
*/
|
|
280
|
+
onEvent(handler) {
|
|
281
|
+
if (typeof handler !== 'function') {
|
|
282
|
+
throw new TypeError('TrayIcon.onEvent: 需要一个函数');
|
|
283
|
+
}
|
|
284
|
+
const transport = getTransport();
|
|
285
|
+
const on = transport.on;
|
|
286
|
+
if (typeof on !== 'function') {
|
|
287
|
+
throw new Error('orielweb/tray: 传输层不支持 on()——无法订阅托盘事件');
|
|
288
|
+
}
|
|
289
|
+
const off = on.call(transport, TRAY_EVENT, (/** @type {any} */ value) => {
|
|
290
|
+
if (value && value.id === this.id) {
|
|
291
|
+
try { handler(value); }
|
|
292
|
+
catch (err) { console.error('TrayIcon.onEvent 处理器抛出', err); }
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
return off;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* 设置新图标;`null` 移除图标。
|
|
300
|
+
*
|
|
301
|
+
* @param {any} icon
|
|
302
|
+
* @returns {Promise<void>}
|
|
303
|
+
*/
|
|
304
|
+
setIcon(icon) {
|
|
305
|
+
if (icon === null || icon === undefined) {
|
|
306
|
+
return invoke('tray.setIcon', { id: this.id, iconPath: null });
|
|
307
|
+
}
|
|
308
|
+
return invoke('tray.setIcon', { id: this.id, ...encodeIcon(icon) });
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* 设置托盘菜单。
|
|
313
|
+
*
|
|
314
|
+
* @param {any} menu
|
|
315
|
+
* @returns {Promise<void>}
|
|
316
|
+
*
|
|
317
|
+
* #### 平台差异
|
|
318
|
+
* - **Linux**:菜单一旦设置就移除不掉,`null` 只清空项(Tauri 同)。
|
|
319
|
+
*/
|
|
320
|
+
async setMenu(menu) {
|
|
321
|
+
const wired = wireMenu(menu);
|
|
322
|
+
await ensureClickListener();
|
|
323
|
+
clickRouting.set(wired.token, wired.actions);
|
|
324
|
+
await invoke('tray.setMenu', { id: this.id, items: wired.items });
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* 设置悬停提示。
|
|
329
|
+
*
|
|
330
|
+
* @param {string | null} tooltip
|
|
331
|
+
* @returns {Promise<void>}
|
|
332
|
+
*
|
|
333
|
+
* #### 平台差异
|
|
334
|
+
* - **Linux**:不支持(GtkStatusIcon 无 tooltip 之外的等价物时行为各异)。
|
|
335
|
+
*/
|
|
336
|
+
setTooltip(tooltip) {
|
|
337
|
+
return invoke('tray.setTooltip', {
|
|
338
|
+
id: this.id,
|
|
339
|
+
tooltip: typeof tooltip === 'string' ? tooltip : null,
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* 设置图标旁的标题文本。
|
|
345
|
+
*
|
|
346
|
+
* @param {string | null} title
|
|
347
|
+
* @returns {Promise<void>}
|
|
348
|
+
*
|
|
349
|
+
* #### 平台差异
|
|
350
|
+
* - **Windows**:不支持。
|
|
351
|
+
* - **Linux**:只有同时有图标时才显示。
|
|
352
|
+
*/
|
|
353
|
+
setTitle(title) {
|
|
354
|
+
return invoke('tray.setTitle', {
|
|
355
|
+
id: this.id,
|
|
356
|
+
title: typeof title === 'string' ? title : null,
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* 显示/隐藏本托盘图标。
|
|
362
|
+
*
|
|
363
|
+
* @param {boolean} visible
|
|
364
|
+
* @returns {Promise<void>}
|
|
365
|
+
*/
|
|
366
|
+
setVisible(visible) {
|
|
367
|
+
return invoke('tray.setVisible', { id: this.id, visible: visible !== false });
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* 把当前图标设为 macOS 的模板图像(随菜单栏明暗自动着色)。
|
|
372
|
+
*
|
|
373
|
+
* @param {boolean} asTemplate
|
|
374
|
+
* @returns {Promise<void>}
|
|
375
|
+
*
|
|
376
|
+
* #### 平台差异
|
|
377
|
+
* - **macOS 专属**;其他平台接受参数但不生效(与 Tauri 一致)。
|
|
378
|
+
*/
|
|
379
|
+
setIconAsTemplate(asTemplate) {
|
|
380
|
+
return invoke('tray.setIconAsTemplate', {
|
|
381
|
+
id: this.id,
|
|
382
|
+
asTemplate: asTemplate === true,
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* 原子地"换图标 + 改模板标记"(Tauri 为了避免两次调用之间闪一下旧模板态)。
|
|
388
|
+
*
|
|
389
|
+
* 本库是两次调用的组合(宿主没有原子入口),短暂闪烁理论上可能出现;
|
|
390
|
+
* 与逐次调 `setIcon` + `setIconAsTemplate` 的差别只在顺序保证。
|
|
391
|
+
*
|
|
392
|
+
* @param {any} icon
|
|
393
|
+
* @param {boolean} asTemplate
|
|
394
|
+
* @returns {Promise<void>}
|
|
395
|
+
*/
|
|
396
|
+
async setIconWithAsTemplate(icon, asTemplate) {
|
|
397
|
+
await this.setIcon(icon);
|
|
398
|
+
await this.setIconAsTemplate(asTemplate);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* 开关"左键点击弹出菜单"(`setShowMenuOnLeftClick` 的旧名,Tauri 已弃用同款)。
|
|
403
|
+
*
|
|
404
|
+
* @param {boolean} onLeft
|
|
405
|
+
* @returns {Promise<void>}
|
|
406
|
+
* @deprecated 用 {@linkcode TrayIcon.setShowMenuOnLeftClick}。
|
|
407
|
+
*/
|
|
408
|
+
setMenuOnLeftClick(onLeft) {
|
|
409
|
+
return this.setShowMenuOnLeftClick(onLeft);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* 开关"左键点击弹出菜单"(默认开)。
|
|
414
|
+
*
|
|
415
|
+
* 关掉后左键点击交给你自己的 `action` 处理器,右键仍弹菜单。
|
|
416
|
+
*
|
|
417
|
+
* @param {boolean} onLeft
|
|
418
|
+
* @returns {Promise<void>}
|
|
419
|
+
*
|
|
420
|
+
* #### 平台差异
|
|
421
|
+
* - **Windows**:生效。
|
|
422
|
+
* - **Linux / macOS**:抛 `not_supported`——那两个平台"左键弹菜单"是平台惯例,
|
|
423
|
+
* 本库后端暂未接通开关(Tauri 标注 Linux Unsupported;macOS 有 API 而我们没接)。
|
|
424
|
+
*/
|
|
425
|
+
setShowMenuOnLeftClick(onLeft) {
|
|
426
|
+
return invoke('tray.setMenuOnLeftClick', {
|
|
427
|
+
id: this.id,
|
|
428
|
+
onLeft: onLeft !== false,
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* 设置图标临时目录。**Linux 专属补壳**:本库不落盘图标,调用不报错但不生效。
|
|
434
|
+
*
|
|
435
|
+
* @param {string | null} _path
|
|
436
|
+
* @returns {Promise<void>}
|
|
437
|
+
*/
|
|
438
|
+
setTempDirPath(_path) {
|
|
439
|
+
warnCompat('TrayIcon.setTempDirPath 是补壳:本库不落盘图标,该调用不生效。');
|
|
440
|
+
return Promise.resolve();
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* 移除本托盘(等价 `TrayIcon.removeById(this.id)`)。
|
|
445
|
+
*
|
|
446
|
+
* @returns {Promise<boolean>}
|
|
447
|
+
*/
|
|
448
|
+
close() {
|
|
449
|
+
return TrayIcon.removeById(this.id);
|
|
450
|
+
}
|
|
451
|
+
}
|