Skip to content

添加系统托盘

一、tauri.conf.json配置启用系统托盘

img

二、Rust中添加托盘及事件

封装到 system_tray_init.rs 文件中

Rust
// src/system_tray_init.rs

use tauri::{AppHandle, CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem, SystemTraySubmenu};

pub fn init_tray() -> SystemTray {
    let quit = CustomMenuItem::new("quit".to_string(), "退出窗口");
    let hide = CustomMenuItem::new("hide".to_string(), "隐藏窗口");
    let settings = CustomMenuItem::new("short-settings".to_string(), "快捷键设置");
    let about = CustomMenuItem::new("about".to_string(), "关于");
    let show = CustomMenuItem::new("show".to_string(), "显示窗口");

    // Create submenu for settings
    let developer_settings = CustomMenuItem::new("developer_settings".to_string(), "开发者设置");
    let general_settings = CustomMenuItem::new("general_settings".to_string(), "通用设置");
    let settings_submenu = SystemTrayMenu::new()
        .add_item(developer_settings)
        .add_item(general_settings);

    let tray_menu = SystemTrayMenu::new()
        .add_item(show)
        .add_item(hide)
        .add_native_item(SystemTrayMenuItem::Separator)
        .add_submenu(SystemTraySubmenu::new(
            "设置", // 语言菜单
            settings_submenu
        )) // Add submenu
        .add_native_item(SystemTrayMenuItem::Separator)
        .add_item(settings) // Add settings as a clickable item
        .add_item(about)
        .add_item(quit);

    SystemTray::new().with_menu(tray_menu)
}

pub fn handle_tray_event(app: &AppHandle, event: SystemTrayEvent) {
    match event {
        SystemTrayEvent::MenuItemClick { id, .. } => {
            let window = app.get_window("main").unwrap();
            match id.as_str() {
                "quit" => {
                    std::process::exit(0);
                }
                "hide" => {
                    window.hide().unwrap();
                }
                "short-settings" => {
                    window.show().unwrap();
                    window.emit("open-settings", "shortCommand").unwrap();
                }
                "developer_settings" => {
                    window.show().unwrap();
                    window.emit("open-settings", "developer").unwrap();
                }
                "general_settings" => {
                    window.show().unwrap();
                    window.emit("open-settings", "common").unwrap();
                }
                "show" => {
                    window.show().unwrap();
                }
                "about" => {
                    window.show().unwrap();
                    window.emit("open-settings", "about").unwrap();
                }
                _ => {}
            }
        }
        _ => {}
    }
}

main.rs 注册托盘及事件

Rust
use tauri::{CustomMenuItem, SystemTray, SystemTrayMenu, SystemTrayEvent};
use tauri::Manager;
use app::{system_tray_init::{init_tray, handle_tray_event}};
fn main() {
  tauri::Builder::default()
    .system_tray(init_tray())
    .on_system_tray_event(handle_tray_event)
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

img

前端知识体系 · wcrane