当前位置:   article > 正文

Tauri 系统托盘图标_tauri 托盘

tauri 托盘

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

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

配置系统托盘如下:

  1. #![cfg_attr(
  2. all(not(debug_assertions), target_os = "windows"),
  3. windows_subsystem = "windows"
  4. )]
  5. use tauri::{CustomMenuItem, SystemTray,SystemTrayMenuItem,SystemTrayEvent, SystemTrayMenu,Manager,Window};
  6. fn main() {
  7. let quit = CustomMenuItem::new("quit".to_string(), "退出");
  8. let show = CustomMenuItem::new("show".to_string(), "显示");
  9. let tray_menu = SystemTrayMenu::new()
  10. .add_item(show)
  11. .add_native_item(SystemTrayMenuItem::Separator)
  12. .add_item(quit);
  13. let tray = SystemTray::new().with_menu(tray_menu);
  14. tauri::Builder::default()
  15. // 加载完就关闭开屏
  16. .setup(|app| {
  17. let splashscreen_window = app.get_window("splashscreen").unwrap();
  18. let main_window = app.get_window("main").unwrap();
  19. // we perform the initialization code on a new task so the app doesn't freeze
  20. tauri::async_runtime::spawn(async move {
  21. // initialize your app here instead of sleeping :)
  22. println!("加载...");
  23. // from_secs(num)的num是加载时间
  24. std::thread::sleep(std::time::Duration::from_secs(2));
  25. println!("加载完成");
  26. // After it's done, close the splashscreen and display the main window
  27. splashscreen_window.close().unwrap();
  28. main_window.show().unwrap();
  29. });
  30. Ok(())
  31. })
  32. .system_tray(tray)// 系统托盘
  33. .on_system_tray_event(|app, event| menu_handle(app, event))
  34. .invoke_handler(tauri::generate_handler![init_process,app_exit])
  35. .build(tauri::generate_context!())
  36. .expect("error while building tauri application")
  37. .run(|_app_handle, event| match event {
  38. tauri::RunEvent::ExitRequested { api, .. } => {
  39. api.prevent_exit();
  40. }
  41. _ => {}
  42. });
  43. fn menu_handle(app_handle: &tauri::AppHandle, event: SystemTrayEvent) {
  44. match event {
  45. SystemTrayEvent::LeftClick {
  46. position: _,
  47. size: _,
  48. ..
  49. } => {
  50. let window = app_handle.get_window("main").unwrap();
  51. if window.is_visible().unwrap() {
  52. window.hide().unwrap();
  53. } else {
  54. window.show().unwrap();
  55. }
  56. println!("鼠标-左击");
  57. }
  58. SystemTrayEvent::RightClick {
  59. position: _,
  60. size: _,
  61. ..
  62. } => {
  63. println!("鼠标-右击");
  64. }
  65. SystemTrayEvent::DoubleClick {
  66. position: _,
  67. size: _,
  68. ..
  69. } => {
  70. println!("鼠标-双击");
  71. }
  72. SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
  73. "quit" => {
  74. std::process::exit(0);
  75. }
  76. "show" => {
  77. let window = app_handle.get_window("main").unwrap();
  78. window.show().unwrap();
  79. }
  80. _ => {}
  81. },
  82. _ => {}
  83. }
  84. }
  85. }

效果图:

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号