Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116use tauri::{ tray::{MouseButton, MouseButtonState, TrayIconEvent}, Emitter, Manager,};// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/use tauri_plugin_deep_link::DeepLinkExt;
mod background;mod tray;use background::stop_background_scheduler;use tray::create_system_tray;
use crate::background::start_background_scheduler;
#[tauri::command]fn greet(name: &str) -> String { format!("Hello, {}! You've been greeted from Rust!", name)}
#[tauri::command]async fn select_backup_directory(default_path: Option<String>) -> Result<Option<String>, String> { let mut dialog = rfd::AsyncFileDialog::new().set_title("Select Backup Directory"); if let Some(ref path) = default_path { dialog = dialog.set_directory(path); } let folder = dialog.pick_folder().await; Ok(folder.map(|f| f.path().to_string_lossy().to_string()))}
#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() { let builder = tauri::Builder::default() .plugin(tauri_plugin_log::Builder::new().build()) .plugin(tauri_plugin_websocket::init()) .plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_process::init()) .plugin(tauri_plugin_updater::Builder::new().build()) .plugin(tauri_plugin_autostart::init( tauri_plugin_autostart::MacosLauncher::LaunchAgent, None, )) .plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_store::Builder::new().build()) .plugin(tauri_plugin_deep_link::init()) .plugin(tauri_plugin_opener::init()) .plugin(tauri_plugin_single_instance::init(|app, argv, _cwd| { println!("A new app instance was opened with {argv:?} and the deep link event was already triggered.");
let _ = app.get_webview_window("main") .expect("no main window") .set_focus(); })) .invoke_handler(tauri::generate_handler![ greet, start_background_scheduler, stop_background_scheduler, select_backup_directory ]) .on_menu_event(|app, event| match event.id.as_ref() { "quit" => { std::process::exit(0); } "show" => { let window = app.get_webview_window("main").unwrap(); window.show().unwrap(); window.set_focus().unwrap(); } "hide" => { let window = app.get_webview_window("main").unwrap(); window.hide().unwrap(); } "backup_now" => { // Emit event to trigger backup app.emit("perform-backup", ()).unwrap(); } _ => { println!("menu item {:?} not handled", event.id); } }) .on_tray_icon_event(|tray, event| match event { TrayIconEvent::Click { button: MouseButton::Left, button_state: MouseButtonState::Up, .. } => { println!("left click pressed and released"); // in this example, let's show and focus the main window when the tray is clicked let app = tray.app_handle(); if let Some(window) = app.get_webview_window("main") { let _ = window.show(); let _ = window.set_focus(); } } _ => { println!("unhandled event {event:?}"); } }) .setup(|app| { #[cfg(any(windows, target_os = "linux"))] { app.deep_link().register_all()?; }
#[cfg(target_os = "macos")] app.set_activation_policy(tauri::ActivationPolicy::Accessory);
let _tray = create_system_tray(app)?;
Ok(()) });
builder .run(tauri::generate_context!()) .expect("error while running tauri application");}