mod capture; mod fieldlens; mod model; mod printers; mod state; mod zpl; use parking_lot::Mutex; use tauri::{AppHandle, Manager, State}; use capture::CaptureRuntime; use model::{AppConfig, CaptureStatus, JobSummary, LoginPoll, LoginStart, SyncResult}; use printers::{SystemPrinter, VirtualPrinterStatus}; use state::SharedState; struct AppState { shared: SharedState, runtime: Mutex>, } #[tauri::command] fn get_config(state: State<'_, AppState>) -> AppConfig { state.shared.config.lock().clone() } #[tauri::command] fn save_config(config: AppConfig, state: State<'_, AppState>) -> Result { state .shared .replace_config(config) .map_err(|error| error.to_string()) } #[tauri::command] fn get_status(state: State<'_, AppState>) -> CaptureStatus { CaptureStatus { running: state.runtime.lock().is_some(), mode: state.shared.config.lock().mode.clone(), pending: state.shared.pending_count(), jobs: state.shared.jobs.lock().len(), } } #[tauri::command] fn recent_jobs(state: State<'_, AppState>) -> Vec { state.shared.jobs.lock().iter().cloned().collect() } #[tauri::command] fn start_capture(app: AppHandle, state: State<'_, AppState>) -> Result<(), String> { let mut runtime = state.runtime.lock(); if runtime.is_some() { return Ok(()); } let config = state.shared.config.lock().clone(); let next = capture::start(config, state.shared.clone(), app).map_err(|error| error.to_string())?; *runtime = Some(next); Ok(()) } #[tauri::command] fn stop_capture(state: State<'_, AppState>) { if let Some(runtime) = state.runtime.lock().take() { runtime.stop(); } } #[tauri::command] fn list_system_printers() -> Result, String> { printers::list_system_printers().map_err(|error| error.to_string()) } #[tauri::command] fn get_virtual_printer_status() -> Result { printers::virtual_printer_status().map_err(|error| error.to_string()) } #[tauri::command] fn install_virtual_printer(target_printer: String) -> Result { printers::install_virtual_printer(&target_printer).map_err(|error| error.to_string()) } #[tauri::command] fn remove_virtual_printer() -> Result { printers::remove_virtual_printer().map_err(|error| error.to_string()) } #[tauri::command] fn begin_login(state: State<'_, AppState>) -> Result { fieldlens::begin_login(&state.shared).map_err(|error| error.to_string()) } #[tauri::command] fn poll_login(state: State<'_, AppState>) -> Result { fieldlens::poll_login(&state.shared).map_err(|error| error.to_string()) } #[tauri::command] fn sync_pending(state: State<'_, AppState>) -> Result { fieldlens::sync_pending(&state.shared).map_err(|error| error.to_string()) } pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_opener::init()) .plugin(tauri_plugin_process::init()) .plugin(tauri_plugin_updater::Builder::new().build()) .setup(|app| { let data_dir = app.path().app_data_dir()?; let shared = SharedState::load(data_dir) .map_err(|error| std::io::Error::other(error.to_string()))?; app.manage(AppState { shared, runtime: Mutex::new(None), }); Ok(()) }) .invoke_handler(tauri::generate_handler![ get_config, save_config, get_status, recent_jobs, start_capture, stop_capture, list_system_printers, get_virtual_printer_status, install_virtual_printer, remove_virtual_printer, begin_login, poll_login, sync_pending ]) .run(tauri::generate_context!()) .expect("error while running FieldLens Print"); }