main.rs 4.1 KB

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