diff --git a/frontend/src-tauri/src/commands/files.rs b/frontend/src-tauri/src/commands/files.rs index e77917e1f..a935e53e0 100644 --- a/frontend/src-tauri/src/commands/files.rs +++ b/frontend/src-tauri/src/commands/files.rs @@ -1,15 +1,35 @@ use crate::utils::add_log; +use std::sync::Mutex; + +// Store the opened file path globally +static OPENED_FILE: Mutex> = Mutex::new(None); + +// Set the opened file path (called by macOS file open events) +pub fn set_opened_file(file_path: String) { + let mut opened_file = OPENED_FILE.lock().unwrap(); + *opened_file = Some(file_path.clone()); + add_log(format!("📂 File opened via macOS event: {}", file_path)); +} // Command to get opened file path (if app was launched with a file) #[tauri::command] pub async fn get_opened_file() -> Result, String> { - // Get command line arguments + // First check if we have a file from macOS file open events + { + let opened_file = OPENED_FILE.lock().unwrap(); + if let Some(ref file_path) = *opened_file { + add_log(format!("📂 Returning stored opened file: {}", file_path)); + return Ok(Some(file_path.clone())); + } + } + + // Fallback to command line arguments (Windows/Linux) let args: Vec = std::env::args().collect(); // Look for a PDF file argument (skip the first arg which is the executable) for arg in args.iter().skip(1) { if arg.ends_with(".pdf") && std::path::Path::new(arg).exists() { - add_log(format!("📂 PDF file opened: {}", arg)); + add_log(format!("📂 PDF file opened via command line: {}", arg)); return Ok(Some(arg.clone())); } } @@ -17,3 +37,12 @@ pub async fn get_opened_file() -> Result, String> { Ok(None) } +// Command to clear the opened file (after processing) +#[tauri::command] +pub async fn clear_opened_file() -> Result<(), String> { + let mut opened_file = OPENED_FILE.lock().unwrap(); + *opened_file = None; + add_log("📂 Cleared opened file".to_string()); + Ok(()) +} + diff --git a/frontend/src-tauri/src/commands/mod.rs b/frontend/src-tauri/src/commands/mod.rs index 97cd657d2..773f5d2dd 100644 --- a/frontend/src-tauri/src/commands/mod.rs +++ b/frontend/src-tauri/src/commands/mod.rs @@ -4,4 +4,4 @@ pub mod files; pub use backend::{start_backend, cleanup_backend}; pub use health::check_backend_health; -pub use files::get_opened_file; \ No newline at end of file +pub use files::{get_opened_file, clear_opened_file, set_opened_file}; \ No newline at end of file diff --git a/frontend/src-tauri/src/lib.rs b/frontend/src-tauri/src/lib.rs index 084c57f6d..141573a45 100644 --- a/frontend/src-tauri/src/lib.rs +++ b/frontend/src-tauri/src/lib.rs @@ -3,7 +3,7 @@ use tauri::{RunEvent, WindowEvent}; mod utils; mod commands; -use commands::{start_backend, check_backend_health, get_opened_file, cleanup_backend}; +use commands::{start_backend, check_backend_health, get_opened_file, clear_opened_file, cleanup_backend, set_opened_file}; use utils::add_log; #[cfg_attr(mobile, tauri::mobile_entry_point)] @@ -12,7 +12,7 @@ pub fn run() { .plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_fs::init()) .setup(|_app| {Ok(())}) - .invoke_handler(tauri::generate_handler![start_backend, check_backend_health, get_opened_file]) + .invoke_handler(tauri::generate_handler![start_backend, check_backend_health, get_opened_file, clear_opened_file]) .build(tauri::generate_context!()) .expect("error while building tauri application") .run(|app_handle, event| { @@ -28,6 +28,21 @@ pub fn run() { cleanup_backend(); // Allow the window to close } + // Handle macOS file open events + RunEvent::Opened { urls } => { + for url in urls { + add_log(format!("📂 File opened via macOS event: {}", url)); + + // Convert URL to file path if it's a file URL + if let Ok(path) = url.to_file_path() { + if let Some(path_str) = path.to_str() { + if path_str.ends_with(".pdf") { + set_opened_file(path_str.to_string()); + } + } + } + } + } _ => {} } });