use tauri::{command, AppHandle}; use serde::{Deserialize, Serialize}; /// 应用信息结构 #[derive(Debug, Serialize, Deserialize)] pub struct AppInfo { pub name: String, pub version: String, pub platform: String, } /// 选择目录命令 /// 遵循 Tauri 开发规范的系统集成设计 #[command] pub fn select_directory(app: AppHandle) -> Result, String> { use tauri_plugin_dialog::DialogExt; // 使用同步方式打开文件夹选择对话框 let dialog = app.dialog().file().set_title("选择项目目录"); // 创建一个简单的阻塞实现 let (tx, rx) = std::sync::mpsc::channel(); dialog.pick_folder(move |folder_path| { let _ = tx.send(folder_path); }); // 等待结果,设置超时 match rx.recv_timeout(std::time::Duration::from_secs(30)) { Ok(Some(path)) => { let path_str = path.to_string(); Ok(Some(path_str)) } Ok(None) => Ok(None), Err(_) => Err("对话框操作超时或失败".to_string()), } } /// 获取应用信息命令 #[command] pub async fn get_app_info() -> Result { Ok(AppInfo { name: "MixVideo Desktop".to_string(), version: env!("CARGO_PKG_VERSION").to_string(), platform: std::env::consts::OS.to_string(), }) } /// 验证目录是否存在命令 #[command] pub async fn validate_directory(path: String) -> Result { use std::path::Path; let path = Path::new(&path); Ok(path.exists() && path.is_dir()) } /// 获取目录名称命令 #[command] pub async fn get_directory_name(path: String) -> Result { use std::path::Path; let path = Path::new(&path); match path.file_name() { Some(name) => Ok(name.to_string_lossy().to_string()), None => Err("无效的目录路径".to_string()), } }