跳转到内容
Tauri

tray

此内容尚不支持你的语言。

Tray icon class and associated methods. This type constructor is private, instead, you should use the static method TrayIcon.new.

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 TrayIcon.close.

import { TrayIcon } from '@tauri-apps/api/tray';
const tray = await TrayIcon.new({ tooltip: 'awesome tray tooltip' });
tray.set_tooltip('new tooltip');
PropertyModifierTypeDescriptionDefined in
idpublicstringThe id associated with this tray icon.Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L160

get rid(): number

number

Resource.rid

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L309

close(): Promise<void>

Destroys and cleans up this resource from memory. You should not call any method on this object anymore and should drop any reference to it.

Promise<void>

Resource.close

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L321

setIcon(icon): Promise<void>

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:

[dependencies]
tauri = { version = "...", features = ["...", "image-png"] }
ParameterType
icon| null | string | number[] | ArrayBuffer | Uint8Array<ArrayBufferLike> | Image

Promise<void>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L224

setIconAsTemplate(asTemplate): Promise<void>

Sets the current icon as a template. macOS only

ParameterType
asTemplateboolean

Promise<void>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L292

setMenu(menu): Promise<void>

Sets a new tray menu.

  • Linux: once a menu is set it cannot be removed so null has no effect
ParameterType
menunull | Submenu | Menu

Promise<void>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L241

setMenuOnLeftClick(onLeft): Promise<void>

Disable or enable showing the tray menu on left click.

  • Linux: Unsupported.
ParameterType
onLeftboolean

Promise<void>

use TrayIcon.setShowMenuOnLeftClick instead.

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L308

setShowMenuOnLeftClick(onLeft): Promise<void>

Disable or enable showing the tray menu on left click.

  • Linux: Unsupported.
ParameterType
onLeftboolean

Promise<void>

2.2.0

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L324

setTempDirPath(path): Promise<void>

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.

ParameterType
pathnull | string

Promise<void>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L287

setTitle(title): Promise<void>

Sets the tooltip for this tray icon.

  • 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
ParameterType
titlenull | string

Promise<void>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L272

setTooltip(tooltip): Promise<void>

Sets the tooltip for this tray icon.

  • Linux: Unsupported
ParameterType
tooltipnull | string

Promise<void>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L256

setVisible(visible): Promise<void>

Show or hide this tray icon.

ParameterType
visibleboolean

Promise<void>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L277

static getById(id): Promise<null | TrayIcon>

Gets a tray icon using the provided id.

ParameterType
idstring

Promise<null | TrayIcon>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L168

static new(options?): Promise<TrayIcon>

Creates a new TrayIcon

  • Linux: Sometimes the icon won’t be visible unless a menu is set. Setting an empty Menu is enough.
ParameterType
options?TrayIconOptions

Promise<TrayIcon>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L192

static removeById(id): Promise<void>

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.

ParameterType
idstring

Promise<void>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L180

TrayIcon creation options

PropertyTypeDescriptionDefined in
action?(event: TrayIconEvent) => voidA handler for an event on the tray icon.Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L137
icon?| string | number[] | ArrayBuffer | Uint8Array<ArrayBufferLike> | ImageThe 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: [dependencies] tauri = { version = "...", features = ["...", "image-png"] }Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L89
iconAsTemplate?booleanUse the icon as a template. macOS only.Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L115
id?stringThe tray icon id. If undefined, a random one will be assignedSource: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L76
menu?Submenu | MenuThe tray icon menuSource: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L78
menuOnLeftClick?booleanWhether to show the tray menu on left click or not, default is true. #### Platform-specific: - Linux: Unsupported. Deprecated use TrayIconOptions.showMenuOnLeftClick instead.Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L125
showMenuOnLeftClick?booleanWhether to show the tray menu on left click or not, default is true. #### Platform-specific: - Linux: Unsupported. Since 2.2.0Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L135
tempDirPath?stringThe 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.Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L111
title?stringThe 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.Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L104
tooltip?stringThe tray icon tooltipSource: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L91

type MouseButton: "Left" | "Right" | "Middle";

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L11


type MouseButtonState: "Up" | "Down";

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L10


type TrayIconClickEvent: object;
NameTypeDescriptionDefined in
buttonMouseButtonMouse button that triggered this event.Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L35
buttonStateMouseButtonStateMouse button state when this event was triggered.Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L37

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L33


type TrayIconEvent:
| TrayIconEventBase<"Click"> & TrayIconClickEvent
| TrayIconEventBase<"DoubleClick"> & Omit<TrayIconClickEvent, "buttonState">
| TrayIconEventBase<"Enter">
| TrayIconEventBase<"Move">
| TrayIconEventBase<"Leave">;

Describes a tray icon event.

  • Linux: Unsupported. The event is not emitted even though the icon is shown, the icon will still show a context menu on right click.

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L48


type TrayIconEventBase<T>: object;
Type Parameter
T extends TrayIconEventType
NameTypeDescriptionDefined in
idstringId of the tray icon which triggered this event.Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L23
positionPhysicalPositionPhysical position of the click the triggered this event.Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L25
rectobjectPosition and size of the tray icon.Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L27
rect.positionPhysicalPosition-Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L28
rect.sizePhysicalSize-Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L29
typeTThe tray icon event typeSource: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L21

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L19


type TrayIconEventType:
| "Click"
| "DoubleClick"
| "Enter"
| "Move"
| "Leave";

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/tray.ts#L12


© 2025 Tauri Contributors. CC-BY / MIT