Tauri 系统托盘图标

1、在src-tauri目录下找到src目录里面的main.rs

你的项目/src-tauri/src/main.rs

配置系统托盘如下:

#![cfg_attr(
    all(not(debug_assertions), target_os = "windows"),
    windows_subsystem = "windows"
)]
use tauri::{CustomMenuItem, SystemTray,SystemTrayMenuItem,SystemTrayEvent, SystemTrayMenu,Manager,Window};

fn main() {
  let quit = CustomMenuItem::new("quit".to_string(), "退出");
  let show = CustomMenuItem::new("show".to_string(), "显示");
  let tray_menu = SystemTrayMenu::new()
    .add_item(show)
    .add_native_item(SystemTrayMenuItem::Separator)
    .add_item(quit);
  let tray = SystemTray::new().with_menu(tray_menu);

  tauri::Builder::default()
  // 加载完就关闭开屏
    .setup(|app| {
      let splashscreen_window = app.get_window("splashscreen").unwrap();
      let main_window = app.get_window("main").unwrap();
      // we perform the initialization code on a new task so the app doesn't freeze
      tauri::async_runtime::spawn(async move {
        // initialize your app here instead of sleeping :)
        println!("加载...");
        // from_secs(num)的num是加载时间
        std::thread::sleep(std::time::Duration::from_secs(2));
        println!("加载完成");

        // After it's done, close the splashscreen and display the main window
        splashscreen_window.close().unwrap();
        main_window.show().unwrap();
      });
      Ok(())
    })
    .system_tray(tray)// 系统托盘
    .on_system_tray_event(|app, event| menu_handle(app, event))
    .invoke_handler(tauri::generate_handler![init_process,app_exit])

    .build(tauri::generate_context!())
    
    .expect("error while building tauri application")

    .run(|_app_handle, event| match event {
      tauri::RunEvent::ExitRequested { api, .. } => {
        api.prevent_exit();
      }
      _ => {}
    });
    

    fn menu_handle(app_handle: &tauri::AppHandle, event: SystemTrayEvent) {
      match event {
        SystemTrayEvent::LeftClick {
          position: _,
          size: _,
          ..
        } => {
          let window = app_handle.get_window("main").unwrap();
          if window.is_visible().unwrap() {
            window.hide().unwrap();
          } else {
            window.show().unwrap();
          }
          println!("鼠标-左击");
        }
        SystemTrayEvent::RightClick {
          position: _,
          size: _,
          ..
        } => {
          println!("鼠标-右击");
        }
        SystemTrayEvent::DoubleClick {
          position: _,
          size: _,
          ..
        } => {
          println!("鼠标-双击");
        }
        SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
          "quit" => {
            std::process::exit(0);
          }
          "show" => {
            let window = app_handle.get_window("main").unwrap();
            window.show().unwrap();
          }
          _ => {}
        },
        _ => {}
      }
    }
}

效果图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豪先生5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值