One-click backups for AT Protocol
1use tauri::{
2 tray::{MouseButton, MouseButtonState, TrayIconEvent},
3 Emitter, Manager,
4};
5// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
6use tauri_plugin_deep_link::DeepLinkExt;
7
8mod background;
9mod tray;
10use background::stop_background_scheduler;
11use tray::create_system_tray;
12
13use crate::background::{start_background_scheduler, BackgroundScheduler};
14
15#[tauri::command]
16fn greet(name: &str) -> String {
17 format!("Hello, {}! You've been greeted from Rust!", name)
18}
19
20#[cfg_attr(mobile, tauri::mobile_entry_point)]
21pub fn run() {
22 let builder = tauri::Builder::default()
23 .plugin(tauri_plugin_log::Builder::new().build())
24 .plugin(tauri_plugin_websocket::init())
25 .plugin(tauri_plugin_shell::init())
26 .plugin(tauri_plugin_process::init())
27 .plugin(tauri_plugin_updater::Builder::new().build())
28 .plugin(tauri_plugin_autostart::init(
29 tauri_plugin_autostart::MacosLauncher::LaunchAgent,
30 None,
31 ))
32 .plugin(tauri_plugin_fs::init())
33 .plugin(tauri_plugin_store::Builder::new().build())
34 .plugin(tauri_plugin_deep_link::init())
35 .plugin(tauri_plugin_opener::init())
36 .plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
37 println!("A new app instance was opened with {argv:?} and the deep link event was already triggered.");
38
39 let _ = app.get_webview_window("main")
40 .expect("no main window")
41 .set_focus();
42 }))
43 .plugin(tauri_plugin_log::Builder::new().build())
44 .invoke_handler(tauri::generate_handler![
45 greet,
46 start_background_scheduler,
47 stop_background_scheduler
48 ])
49 .on_menu_event(|app, event| match event.id.as_ref() {
50 "quit" => {
51 std::process::exit(0);
52 }
53 "show" => {
54 let window = app.get_webview_window("main").unwrap();
55 window.show().unwrap();
56 window.set_focus().unwrap();
57 }
58 "hide" => {
59 let window = app.get_webview_window("main").unwrap();
60 window.hide().unwrap();
61 }
62 "backup_now" => {
63 // Emit event to trigger backup
64 app.emit("perform-backup", ()).unwrap();
65 }
66 _ => {
67 println!("menu item {:?} not handled", event.id);
68 }
69 })
70 .on_tray_icon_event(|tray, event| match event {
71 TrayIconEvent::Click {
72 button: MouseButton::Left,
73 button_state: MouseButtonState::Up,
74 ..
75 } => {
76 println!("left click pressed and released");
77 // in this example, let's show and focus the main window when the tray is clicked
78 let app = tray.app_handle();
79 if let Some(window) = app.get_webview_window("main") {
80 let _ = window.show();
81 let _ = window.set_focus();
82 }
83 }
84 _ => {
85 println!("unhandled event {event:?}");
86 }
87 })
88 .setup(|app| {
89 #[cfg(any(windows, target_os = "linux"))]
90 {
91 app.deep_link().register_all()?;
92 }
93
94 #[cfg(target_os = "macos")]
95 app.set_activation_policy(tauri::ActivationPolicy::Accessory);
96
97 let tray = create_system_tray(app);
98
99 Ok(())
100 });
101
102 builder
103 .run(tauri::generate_context!())
104 .expect("error while running tauri application");
105}