Open with stirling-pdf for mac

This commit is contained in:
Connor Yoh 2025-07-15 15:48:33 +01:00
parent f9e9834eb4
commit 499829bd69
3 changed files with 49 additions and 5 deletions

View File

@ -1,15 +1,35 @@
use crate::utils::add_log;
use std::sync::Mutex;
// Store the opened file path globally
static OPENED_FILE: Mutex<Option<String>> = 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<Option<String>, 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<String> = 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<Option<String>, 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(())
}

View File

@ -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;
pub use files::{get_opened_file, clear_opened_file, set_opened_file};

View File

@ -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());
}
}
}
}
}
_ => {}
}
});