main.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
  2. mod api;
  3. mod app_state;
  4. mod commands;
  5. mod config;
  6. mod poller;
  7. mod settings;
  8. mod state;
  9. mod token;
  10. mod tray;
  11. use app_state::AppState;
  12. use std::sync::Arc;
  13. use tauri::Manager;
  14. fn main() {
  15. tauri::Builder::default()
  16. // 单实例插件必须最先注册
  17. .plugin(tauri_plugin_single_instance::init(|app, _argv, _cwd| {
  18. if let Some(win) = app.get_webview_window("main") {
  19. let _ = win.show();
  20. let _ = win.set_focus();
  21. }
  22. }))
  23. .setup(|app| {
  24. let config_dir = app.path().app_config_dir().map_err(|e| e.to_string())?;
  25. let settings = settings::SettingsStore::new(config_dir);
  26. let cfg = settings.load();
  27. let token = token::TokenStore::new().load().ok().flatten().unwrap_or_else(|| "dev-token".into());
  28. let api = Arc::new(api::ApiClient::new(cfg.base_url.clone(), token));
  29. let handle = poller::spawn(app.handle().clone(), api.clone());
  30. app.manage(settings);
  31. app.manage(AppState {
  32. passthrough: std::sync::Mutex::new(false),
  33. poll_handle: std::sync::Mutex::new(Some(handle)),
  34. pending_task: std::sync::Mutex::new(None),
  35. api,
  36. });
  37. tray::setup_tray(app)?;
  38. Ok(())
  39. })
  40. .invoke_handler(tauri::generate_handler![
  41. commands::on_interact,
  42. commands::drag_start,
  43. commands::drag_end,
  44. commands::get_settings,
  45. commands::save_settings,
  46. commands::open_settings,
  47. commands::quit_app,
  48. commands::upload_mascot,
  49. commands::clear_custom_mascot,
  50. ])
  51. .run(tauri::generate_context!())
  52. .expect("error while running tauri application");
  53. }