Skip to content
Tauri 中文网

系统托盘

Tauri 允许你为你的应用创建和自定义系统托盘。这可以通过提供对常用操作的快速访问来增强用户体验。

🌐 Tauri allows you to create and customize a system tray for your application. This can enhance the user experience by providing quick access to common actions.

🌐 Configuration

首先,更新你的 Cargo.toml 以包含系统托盘所需的功能。

🌐 First of all, update your Cargo.toml to include the necessary feature for the system tray.

src-tauri/Cargo.toml
tauri = { version = "2.0.0", features = [ "tray-icon" ] }

🌐 Usage

托盘 API 在 JavaScript 和 Rust 中均可用。

🌐 The tray API is available in both JavaScript and Rust.

🌐 Create a Tray Icon

使用 TrayIcon.new 静态函数创建一个新的托盘图标:

import { TrayIcon } from '@tauri-apps/api/tray';
const options = {
// here you can add a tray menu, title, tooltip, event handler, etc
};
const tray = await TrayIcon.new(options);

有关自定义选项的更多信息,请参见 TrayIconOptions

🌐 See TrayIconOptions for more information on the customization options.

🌐 Change the Tray Icon

创建托盘时,你可以使用应用图标作为托盘图标:

🌐 When creating the tray you can use the application icon as the tray icon:

import { TrayIcon } from '@tauri-apps/api/tray';
import { defaultWindowIcon } from '@tauri-apps/api/app';
const options = {
icon: await defaultWindowIcon(),
};
const tray = await TrayIcon.new(options);

🌐 Add a Menu

要附加在点击托盘时显示的菜单,可以使用 menu 选项。

🌐 To attach a menu that is displayed when the tray is clicked, you can use the menu option.

🌐 To prevent the menu from popping up on left click, call the show_menu_on_left_click(false) Rust function or set the menuOnLeftClick JavaScript option to false. :::

import { TrayIcon } from '@tauri-apps/api/tray';
import { Menu } from '@tauri-apps/api/menu';
const menu = await Menu.new({
items: [
{
id: 'quit',
text: 'Quit',
},
],
});
const options = {
menu,
menuOnLeftClick: true,
};
const tray = await TrayIcon.new(options);

🌐 Listen to Menu Events

在 JavaScript 中,你可以直接将菜单点击事件监听器附加到菜单项上:

  • 使用共享菜单点击处理程序

    import { Menu } from '@tauri-apps/api/menu';
    function onTrayMenuClick(itemId) {
    // itemId === 'quit'
    }
    const menu = await Menu.new({
    items: [
    {
    id: 'quit',
    text: 'Quit',
    action: onTrayMenuClick,
    },
    ],
    });
  • 使用专用菜单点击处理程序

    import { Menu } from '@tauri-apps/api/menu';
    const menu = await Menu.new({
    items: [
    {
    id: 'quit',
    text: 'Quit',
    action: () => {
    console.log('quit pressed');
    },
    },
    ],
    });

🌐 Listen to Tray Events

托盘图标会触发以下鼠标事件的事件:

🌐 The tray icon emits events for the following mouse events:

  • 点击:当光标收到单次左键、右键或中键点击时触发,包括点击是否已释放的信息
  • 双击:当光标接收到左键、右键或中键的双击时触发
  • 进入:当光标进入托盘图标区域时触发
  • 移动:当光标在托盘图标区域周围移动时触发
  • 离开:当光标离开托盘图标区域时触发
import { TrayIcon } from '@tauri-apps/api/tray';
const options = {
action: (event) => {
switch (event.type) {
case 'Click':
console.log(
`mouse ${event.button} button pressed, state: ${event.buttonState}`
);
break;
case 'DoubleClick':
console.log(`mouse ${event.button} button pressed`);
break;
case 'Enter':
console.log(
`mouse hovered tray at ${event.rect.position.x}, ${event.rect.position.y}`
);
break;
case 'Move':
console.log(
`mouse moved on tray at ${event.rect.position.x}, ${event.rect.position.y}`
);
break;
case 'Leave':
console.log(
`mouse left tray at ${event.rect.position.x}, ${event.rect.position.y}`
);
break;
}
},
};
const tray = await TrayIcon.new(options);

有关事件有效载荷的更多信息,请参见 TrayIconEvent

🌐 See TrayIconEvent for more information on the event payload.

有关创建菜单的详细信息,包括菜单项、子菜单和动态更新,请参阅 窗口菜单 文档。

🌐 For detailed information about creating menus, including menu items, submenus, and dynamic updates, see the Window Menu documentation.


Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站