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.
First of all, update your Cargo.toml
to include the necessary feature for the system tray.
tauri = { version = " 2.0.0 " , features = [ " tray-icon " ] }
The tray API is available in both JavaScript and Rust.
Use the TrayIcon.new
static function to create a new tray icon:
import { TrayIcon } from ' @tauri-apps/api/tray ' ;
// here you can add a tray menu, title, tooltip, event handler, etc
const tray = await TrayIcon . new ( options );
See TrayIconOptions
for more information on the customization options.
use tauri :: tray :: TrayIconBuilder;
tauri :: Builder :: default ()
let tray = TrayIconBuilder :: new () . build ( app ) ? ;
See TrayIconBuilder
for more information on customization options.
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 ' ;
icon: await defaultWindowIcon () ,
const tray = await TrayIcon . new ( options );
let tray = TrayIconBuilder :: new ()
. icon ( app . default_window_icon () . unwrap () . clone ())
To attach a menu that is displayed when the tray is clicked, you can use the menu
option.
Note
By default the menu is displayed on both left and right clicks.
To prevent the menu from popping up on left click, call the 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 ( {
const tray = await TrayIcon . new ( options );
let quit_i = MenuItem :: with_id ( app , " quit " , " Quit " , true , None :: < & str>) ? ;
let menu = Menu :: with_items ( app , & [ & quit_i ]) ? ;
let tray = TrayIconBuilder :: new ()
. menu_on_left_click ( true )
On JavaScript you can attach a menu click event listener directly to the menu item:
Use the TrayIconBuilder::on_menu_event
method to attach a tray menu click event listener:
use tauri :: tray :: TrayIconBuilder;
. on_menu_event ( | app , event | match event . id . as_ref () {
println! ( " quit menu item was clicked " );
println! ( " menu item {:?} not handled " , event . id);
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 ' ;
` mouse ${ event . button } button pressed, state: ${ event . buttonState } `
console . log ( ` mouse ${ event . button } button pressed ` ) ;
` mouse hovered tray at ${ event . rect . position . x } , ${ event . rect . position . y } `
` mouse moved on tray at ${ event . rect . position . x } , ${ event . rect . position . y } `
` mouse left tray at ${ event . rect . position . x } , ${ event . rect . position . y } `
const tray = await TrayIcon . new ( options );
See TrayIconEvent
for more information on the event payload.
tray :: {MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}
. on_tray_icon_event ( | tray , event | match event {
button : MouseButton :: Left,
button_state : MouseButtonState :: Up,
println! ( " left click pressed and released " );
// in this example, let's show and focus the main window when the tray is clicked
let app = tray . app_handle ();
if let Some( window ) = app . get_webview_window ( " main " ) {
let _ = window . set_focus ();
println! ( " unhandled event {event:?} " );
See TrayIconEvent
for more information on the event type.
© 2024 Tauri Contributors. CC-BY / MIT