main.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #![cfg_attr(
  2. all(not(debug_assertions), target_os = "windows"),
  3. windows_subsystem = "windows"
  4. )]
  5. extern crate tauri;
  6. mod clash;
  7. mod sysopt;
  8. use tauri::{CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu};
  9. #[tauri::command]
  10. async fn get_config_data(url: String) -> Result<String, String> {
  11. match clash::fetch_url(&url).await {
  12. Ok(_) => Ok(String::from("success")),
  13. Err(_) => Err(String::from("error")),
  14. }
  15. }
  16. fn main() -> std::io::Result<()> {
  17. clash::run_clash_bin(&clash::get_config_dir().to_str().unwrap());
  18. let app = tauri::Builder::default()
  19. .system_tray(
  20. SystemTray::new().with_menu(
  21. SystemTrayMenu::new()
  22. .add_item(CustomMenuItem::new("event_show", "Show"))
  23. .add_item(CustomMenuItem::new("event_quit", "Quit")),
  24. ),
  25. )
  26. .on_system_tray_event(move |app, event| match event {
  27. SystemTrayEvent::LeftClick { .. } => {
  28. let window = app.get_window("main").unwrap();
  29. window.show().unwrap();
  30. window.set_focus().unwrap();
  31. }
  32. SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
  33. "event_show" => {
  34. let window = app.get_window("main").unwrap();
  35. window.show().unwrap();
  36. window.set_focus().unwrap();
  37. }
  38. "event_quit" => {
  39. app.exit(0);
  40. }
  41. _ => {}
  42. },
  43. _ => {}
  44. })
  45. .invoke_handler(tauri::generate_handler![get_config_data])
  46. .build(tauri::generate_context!())
  47. .expect("error while running tauri application");
  48. app.run(|app_handle, e| match e {
  49. tauri::Event::CloseRequested { label, api, .. } => {
  50. let app_handle = app_handle.clone();
  51. api.prevent_close();
  52. app_handle.get_window(&label).unwrap().hide().unwrap();
  53. }
  54. tauri::Event::ExitRequested { api, .. } => {
  55. api.prevent_exit();
  56. }
  57. _ => {}
  58. });
  59. Ok(())
  60. }