Skip to content
Tauri

System Tray

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

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

The tray API is available in both JavaScript and Rust.

Creata a Tray Icon

Use the TrayIcon.new static function to create a new tray icon:

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

See TrayIconOptions for more information on the customization options.

Change the Tray Icon

When creating the tray icon from Rust code, you can set the application icon as the tray icon:

let tray = TrayIconBuilder::new()
.icon(app.default_window_icon().unwrap().clone())
.build(app)?;

Add a Menu

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

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 = TrayIcon.new(options);

Listen to Menu Events

On JavaScript you can attach a menu click event listener directly to the menu item:

  • Using a shared menu click handler

    import { Menu } from '@tauri-apps/api/menu';
    function onTrayMenuClick(itemId) {
    // itemId === 'quit'
    }
    const menu = await Menu.new({
    items: [
    {
    id: 'quit',
    text: 'Quit',
    action: onTrayMenuClick,
    },
    ],
    });
  • Using a dedicated menu click handler

    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:

  • click: triggered when the cursor receives a single left, right or middle click, including information on whether the mouse press was released or not
  • Double click: triggered when the cursor receives a double left, right or middle click
  • Enter: triggered when the cursor enters the tray icon area
  • Move: triggered when the cursor moves around the tray icon area
  • Leave: triggered when the cursor leaves the tray icon area
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 = TrayIcon.new(options);

See TrayIconEvent for more information on the event payload.


© 2024 Tauri Contributors. CC-BY / MIT