-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathtray.ts
More file actions
340 lines (313 loc) · 9.97 KB
/
tray.ts
File metadata and controls
340 lines (313 loc) · 9.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import type { Menu, Submenu } from './menu'
import { Channel, invoke, Resource } from './core'
import { Image, transformImage } from './image'
import { PhysicalPosition, PhysicalSize } from './dpi'
export type MouseButtonState = 'Up' | 'Down'
export type MouseButton = 'Left' | 'Right' | 'Middle'
export type TrayIconEventType =
| 'Click'
| 'DoubleClick'
| 'Enter'
| 'Move'
| 'Leave'
export type TrayIconEventBase<T extends TrayIconEventType> = {
/** The tray icon event type */
type: T
/** Id of the tray icon which triggered this event. */
id: string
/** Physical position of the click the triggered this event. */
position: PhysicalPosition
/** Position and size of the tray icon. */
rect: {
position: PhysicalPosition
size: PhysicalSize
}
}
export type TrayIconClickEvent = {
/** Mouse button that triggered this event. */
button: MouseButton
/** Mouse button state when this event was triggered. */
buttonState: MouseButtonState
}
/**
* Describes a tray icon event.
*
* #### Platform-specific:
*
* - **Linux**: Unsupported. The event is not emitted even though the icon is shown,
* the icon will still show a context menu on right click.
*/
export type TrayIconEvent =
| (TrayIconEventBase<'Click'> & TrayIconClickEvent)
| (TrayIconEventBase<'DoubleClick'> & Omit<TrayIconClickEvent, 'buttonState'>)
| TrayIconEventBase<'Enter'>
| TrayIconEventBase<'Move'>
| TrayIconEventBase<'Leave'>
type RustTrayIconEvent = Omit<TrayIconEvent, 'rect'> & {
rect: {
position: {
Physical: { x: number; y: number }
}
size: {
Physical: { width: number; height: number }
}
}
}
/**
* Tray icon types and utilities.
*
* This package is also accessible with `window.__TAURI__.tray` when [`app.withGlobalTauri`](https://v2.tauri.app/reference/config/#withglobaltauri) in `tauri.conf.json` is set to `true`.
* @module
*/
/** {@link TrayIcon.new|`TrayIcon`} creation options */
export interface TrayIconOptions {
/** The tray icon id. If undefined, a random one will be assigned */
id?: string
/** The tray icon menu */
menu?: Menu | Submenu
/**
* The tray icon which could be icon bytes or path to the icon file.
*
* Note that you may need the `image-ico` or `image-png` Cargo features to use this API.
* To enable it, change your Cargo.toml file:
* ```toml
* [dependencies]
* tauri = { version = "...", features = ["...", "image-png"] }
* ```
*/
icon?: string | Uint8Array | ArrayBuffer | number[] | Image
/** The tray icon tooltip */
tooltip?: string
/**
* The tray title
*
* #### Platform-specific
*
* - **Linux:** The title will not be shown unless there is an icon
* as well. The title is useful for numerical and other frequently
* updated information. In general, it shouldn't be shown unless a
* user requests it as it can take up a significant amount of space
* on the user's panel. This may not be shown in all visualizations.
* - **Windows:** Unsupported.
*/
title?: string
/**
* The tray icon temp dir path. **Linux only**.
*
* On Linux, we need to write the icon to the disk and usually it will
* be `$XDG_RUNTIME_DIR/tray-icon` or `$TEMP/tray-icon`.
*/
tempDirPath?: string
/**
* Use the icon as a [template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc). **macOS only**.
*/
iconAsTemplate?: boolean
/**
* Whether to show the tray menu on left click or not, default is `true`.
*
* #### Platform-specific:
*
* - **Linux**: Unsupported.
*
* @deprecated use {@linkcode TrayIconOptions.showMenuOnLeftClick} instead.
*/
menuOnLeftClick?: boolean
/**
* Whether to show the tray menu on left click or not, default is `true`.
*
* #### Platform-specific:
*
* - **Linux**: Unsupported.
*
* @since 2.2.0
*/
showMenuOnLeftClick?: boolean
/** A handler for an event on the tray icon. */
action?: (event: TrayIconEvent) => void
}
/**
* Tray icon class and associated methods. This type constructor is private,
* instead, you should use the static method {@linkcode TrayIcon.new}.
*
* #### Warning
*
* Unlike Rust, javascript does not have any way to run cleanup code
* when an object is being removed by garbage collection, but this tray icon
* will be cleaned up when the tauri app exists, however if you want to cleanup
* this object early, you need to call {@linkcode TrayIcon.close}.
*
* @example
* ```ts
* import { TrayIcon } from '@tauri-apps/api/tray';
* const tray = await TrayIcon.new({ tooltip: 'awesome tray tooltip' });
* tray.set_tooltip('new tooltip');
* ```
*/
export class TrayIcon extends Resource {
/** The id associated with this tray icon. */
public id: string
private constructor(rid: number, id: string) {
super(rid)
this.id = id
}
/** Gets a tray icon using the provided id. */
static async getById(id: string): Promise<TrayIcon | null> {
return invoke<number>('plugin:tray|get_by_id', { id }).then((rid) =>
rid ? new TrayIcon(rid, id) : null
)
}
/**
* Removes a tray icon using the provided id from tauri's internal state.
*
* Note that this may cause the tray icon to disappear
* if it wasn't cloned somewhere else or referenced by JS.
*/
static async removeById(id: string): Promise<void> {
return invoke('plugin:tray|remove_by_id', { id })
}
/**
* Creates a new {@linkcode TrayIcon}
*
* #### Platform-specific:
*
* - **Linux:** Sometimes the icon won't be visible unless a menu is set.
* Setting an empty {@linkcode Menu} is enough.
*/
static async new(options?: TrayIconOptions): Promise<TrayIcon> {
if (options?.menu) {
// @ts-expect-error we only need the rid and kind
options.menu = [options.menu.rid, options.menu.kind]
}
if (options?.icon) {
options.icon = transformImage(options.icon)
}
const handler = new Channel<RustTrayIconEvent>()
if (options?.action) {
const action = options.action
handler.onmessage = (e) => action(mapEvent(e))
delete options.action
}
return invoke<[number, string]>('plugin:tray|new', {
options: options ?? {},
handler
}).then(([rid, id]) => new TrayIcon(rid, id))
}
/**
* Sets a new tray icon. If `null` is provided, it will remove the icon.
*
* Note that you may need the `image-ico` or `image-png` Cargo features to use this API.
* To enable it, change your Cargo.toml file:
* ```toml
* [dependencies]
* tauri = { version = "...", features = ["...", "image-png"] }
* ```
*/
async setIcon(
icon: string | Image | Uint8Array | ArrayBuffer | number[] | null
): Promise<void> {
let trayIcon = null
if (icon) {
trayIcon = transformImage(icon)
}
return invoke('plugin:tray|set_icon', { rid: this.rid, icon: trayIcon })
}
/**
* Sets a new tray menu.
*
* #### Platform-specific:
*
* - **Linux**: once a menu is set it cannot be removed so `null` has no effect
*/
async setMenu(menu: Menu | Submenu | null): Promise<void> {
if (menu) {
// @ts-expect-error we only need the rid and kind
menu = [menu.rid, menu.kind]
}
return invoke('plugin:tray|set_menu', { rid: this.rid, menu })
}
/**
* Sets the tooltip for this tray icon.
*
* #### Platform-specific:
*
* - **Linux:** Unsupported
*/
async setTooltip(tooltip: string | null): Promise<void> {
return invoke('plugin:tray|set_tooltip', { rid: this.rid, tooltip })
}
/**
* Sets the tooltip for this tray icon.
*
* #### Platform-specific:
*
* - **Linux:** The title will not be shown unless there is an icon
* as well. The title is useful for numerical and other frequently
* updated information. In general, it shouldn't be shown unless a
* user requests it as it can take up a significant amount of space
* on the user's panel. This may not be shown in all visualizations.
* - **Windows:** Unsupported
*/
async setTitle(title: string | null): Promise<void> {
return invoke('plugin:tray|set_title', { rid: this.rid, title })
}
/** Show or hide this tray icon. */
async setVisible(visible: boolean): Promise<void> {
return invoke('plugin:tray|set_visible', { rid: this.rid, visible })
}
/**
* Sets the tray icon temp dir path. **Linux only**.
*
* On Linux, we need to write the icon to the disk and usually it will
* be `$XDG_RUNTIME_DIR/tray-icon` or `$TEMP/tray-icon`.
*/
async setTempDirPath(path: string | null): Promise<void> {
return invoke('plugin:tray|set_temp_dir_path', { rid: this.rid, path })
}
/** Sets the current icon as a [template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc). **macOS only** */
async setIconAsTemplate(asTemplate: boolean): Promise<void> {
return invoke('plugin:tray|set_icon_as_template', {
rid: this.rid,
asTemplate
})
}
/**
* Disable or enable showing the tray menu on left click.
*
* #### Platform-specific:
*
* - **Linux**: Unsupported.
*
* @deprecated use {@linkcode TrayIcon.setShowMenuOnLeftClick} instead.
*/
async setMenuOnLeftClick(onLeft: boolean): Promise<void> {
return invoke('plugin:tray|set_show_menu_on_left_click', {
rid: this.rid,
onLeft
})
}
/**
* Disable or enable showing the tray menu on left click.
*
* #### Platform-specific:
*
* - **Linux**: Unsupported.
*
* @since 2.2.0
*/
async setShowMenuOnLeftClick(onLeft: boolean): Promise<void> {
return invoke('plugin:tray|set_show_menu_on_left_click', {
rid: this.rid,
onLeft
})
}
}
function mapEvent(e: RustTrayIconEvent): TrayIconEvent {
const out = e as unknown as TrayIconEvent
out.position = new PhysicalPosition(e.position)
out.rect.position = new PhysicalPosition(e.rect.position)
out.rect.size = new PhysicalSize(e.rect.size)
return out
}