The `iconAsTemplate` is a boolean value that determines whether the image represents a [template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc) image on macOS.
To create a native system tray, import the `SystemTray` type:
```rust
use tauri::SystemTray;
```
Initialize a new tray instance:
```rust
let tray = SystemTray::new();
```
### Configuring a system tray context menu
Optionally you can add a context menu that is visible when the tray icon is right clicked. Import the `SystemTrayMenu`, `SystemTrayMenuItem` and `CustomMenuItem` types:
```rust
use tauri::{CustomMenuItem, SystemTrayMenu, SystemTrayMenuItem};
```
Create the `SystemTrayMenu`:
```rust
// here `"quit".to_string()` defines the menu item id, and the second parameter is the menu item label.
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
let hide = CustomMenuItem::new("hide".to_string(), "Hide");
let tray_menu = SystemTrayMenu::new()
.add_item(quit)
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(hide);
```
Add the tray menu to the `SystemTray` instance:
```rust
let tray = SystemTray::new().with_menu(tray_menu);
```
### Configure the app system tray
The created `SystemTray` instance can be set using the `system_tray` API on the `tauri::Builder` struct:
```rust
use tauri::{CustomMenuItem, SystemTray, SystemTrayMenu};
fn main() {
let tray_menu = SystemTrayMenu::new(); // insert the menu items here
let system_tray = SystemTray::new()
.with_menu(tray_menu);
tauri::Builder::default()
.system_tray(system_tray)
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
```
### Listening to system tray events
Each `CustomMenuItem` triggers an event when clicked.
Also, Tauri emits tray icon click events.
Use the `on_system_tray_event` API to handle them:
```rust
use tauri::{CustomMenuItem, SystemTray, SystemTrayMenu};
use tauri::Manager;
fn main() {
let tray_menu = SystemTrayMenu::new(); // insert the menu items here