main.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #![cfg_attr(
  2. all(not(debug_assertions), target_os = "windows"),
  3. windows_subsystem = "windows"
  4. )]
  5. mod cmds;
  6. mod core;
  7. mod states;
  8. mod utils;
  9. use crate::utils::{resolve, server};
  10. use tauri::{
  11. api, CustomMenuItem, Manager, Menu, MenuItem, Submenu, SystemTray, SystemTrayEvent,
  12. SystemTrayMenu, SystemTrayMenuItem,
  13. };
  14. fn main() -> std::io::Result<()> {
  15. if server::check_singleton().is_err() {
  16. println!("app exists");
  17. return Ok(());
  18. }
  19. let submenu_file = Submenu::new(
  20. "File",
  21. Menu::new()
  22. .add_native_item(MenuItem::Undo)
  23. .add_native_item(MenuItem::Redo)
  24. .add_native_item(MenuItem::Copy)
  25. .add_native_item(MenuItem::Paste)
  26. .add_native_item(MenuItem::Cut)
  27. .add_native_item(MenuItem::SelectAll),
  28. );
  29. let tray_menu = SystemTrayMenu::new()
  30. .add_item(CustomMenuItem::new("open_window", "Show"))
  31. .add_item(CustomMenuItem::new("restart_clash", "Restart Clash"))
  32. .add_native_item(SystemTrayMenuItem::Separator)
  33. .add_item(CustomMenuItem::new("quit", "Quit").accelerator("CmdOrControl+Q"));
  34. tauri::Builder::default()
  35. .manage(states::VergeState::default())
  36. .manage(states::ClashState::default())
  37. .manage(states::ProfilesState::default())
  38. .setup(|app| Ok(resolve::resolve_setup(app)))
  39. .menu(Menu::new().add_submenu(submenu_file))
  40. .system_tray(SystemTray::new().with_menu(tray_menu))
  41. .on_system_tray_event(move |app_handle, event| match event {
  42. SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
  43. "open_window" => {
  44. let window = app_handle.get_window("main").unwrap();
  45. window.unminimize().unwrap();
  46. window.show().unwrap();
  47. window.set_focus().unwrap();
  48. }
  49. "restart_clash" => {
  50. let clash_state = app_handle.state::<states::ClashState>();
  51. let profiles_state = app_handle.state::<states::ProfilesState>();
  52. let mut clash = clash_state.0.lock().unwrap();
  53. let mut profiles = profiles_state.0.lock().unwrap();
  54. match clash.restart_sidecar(&mut profiles) {
  55. Ok(_) => {
  56. let window = app_handle.get_window("main").unwrap();
  57. window.emit("restart_clash", "yes").unwrap();
  58. }
  59. Err(err) => log::error!("{}", err),
  60. }
  61. }
  62. "quit" => {
  63. resolve::resolve_reset(app_handle);
  64. api::process::kill_children();
  65. std::process::exit(0);
  66. }
  67. _ => {}
  68. },
  69. SystemTrayEvent::LeftClick { .. } => {
  70. if cfg![target_os = "windows"] {
  71. let window = app_handle.get_window("main").unwrap();
  72. window.unminimize().unwrap();
  73. window.show().unwrap();
  74. window.set_focus().unwrap();
  75. }
  76. }
  77. _ => {}
  78. })
  79. .invoke_handler(tauri::generate_handler![
  80. // common
  81. cmds::restart_sidecar,
  82. cmds::get_sys_proxy,
  83. cmds::get_cur_proxy,
  84. cmds::kill_sidecars,
  85. cmds::open_app_dir,
  86. cmds::open_logs_dir,
  87. // clash
  88. cmds::get_clash_info,
  89. cmds::patch_clash_config,
  90. // verge
  91. cmds::get_verge_config,
  92. cmds::patch_verge_config,
  93. // profile
  94. cmds::new_profile,
  95. cmds::view_profile,
  96. cmds::patch_profile,
  97. cmds::import_profile,
  98. cmds::update_profile,
  99. cmds::delete_profile,
  100. cmds::select_profile,
  101. cmds::get_profiles,
  102. cmds::sync_profiles,
  103. ])
  104. .build(tauri::generate_context!())
  105. .expect("error while running tauri application")
  106. .run(|app_handle, e| match e {
  107. tauri::RunEvent::CloseRequested { label, api, .. } => {
  108. let app_handle = app_handle.clone();
  109. api.prevent_close();
  110. app_handle.get_window(&label).unwrap().hide().unwrap();
  111. }
  112. tauri::RunEvent::ExitRequested { .. } => {
  113. resolve::resolve_reset(app_handle);
  114. api::process::kill_children();
  115. }
  116. _ => {}
  117. });
  118. Ok(())
  119. }