赞
踩
你的项目/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();
- }
- _ => {}
- },
- _ => {}
- }
- }
- }
效果图:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。