|
|
@@ -62,26 +62,42 @@ pub async fn set_window_title(app: tauri::AppHandle, title: String) -> Result<()
|
|
|
|
|
|
/// 选择目录
|
|
|
#[tauri::command]
|
|
|
-pub async fn select_directory(_default_path: Option<String>) -> DirectoryResult {
|
|
|
- #[allow(unused_imports)]
|
|
|
- use tauri_plugin_dialog::DialogExt;
|
|
|
-
|
|
|
- // 注意:这个函数需要在有 app handle 的上下文中调用
|
|
|
- // 由于 Tauri 2.0 的 dialog API 变化,这里使用简化实现
|
|
|
-
|
|
|
+pub async fn select_directory(default_path: Option<String>) -> DirectoryResult {
|
|
|
#[cfg(target_os = "windows")]
|
|
|
{
|
|
|
- use crate::utils::shell::{run_shell, CommandOptions};
|
|
|
+ use std::process::{Command, Stdio};
|
|
|
|
|
|
- // 使用 PowerShell 打开文件夹选择对话框
|
|
|
- // 需要显示窗口因为这是一个 GUI 对话框
|
|
|
- let script = r#"Add-Type -AssemblyName System.Windows.Forms; $dialog = New-Object System.Windows.Forms.FolderBrowserDialog; $dialog.Description = 'Select a folder'; $dialog.ShowNewFolderButton = $true; if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { $dialog.SelectedPath }"#;
|
|
|
+ // 处理默认路径:如果路径存在则使用,否则使用空字符串
|
|
|
+ let initial_path = default_path
|
|
|
+ .filter(|p| !p.is_empty() && std::path::Path::new(p).exists())
|
|
|
+ .unwrap_or_default();
|
|
|
|
|
|
- let output = run_shell(
|
|
|
- &format!("powershell -Command \"{}\"", script),
|
|
|
- CommandOptions::visible(), // 需要显示对话框
|
|
|
+ // 使用 PowerShell 打开文件夹选择对话框
|
|
|
+ // 注意:需要转义路径中的特殊字符
|
|
|
+ let escaped_path = initial_path.replace("'", "''");
|
|
|
+ let script = format!(
|
|
|
+ r#"
|
|
|
+Add-Type -AssemblyName System.Windows.Forms
|
|
|
+$dialog = New-Object System.Windows.Forms.FolderBrowserDialog
|
|
|
+$dialog.ShowNewFolderButton = $true
|
|
|
+$initialPath = '{}'
|
|
|
+if ($initialPath -and (Test-Path $initialPath)) {{
|
|
|
+ $dialog.SelectedPath = $initialPath
|
|
|
+}}
|
|
|
+$null = $dialog.ShowDialog()
|
|
|
+if ($dialog.DialogResult -eq [System.Windows.Forms.DialogResult]::OK) {{
|
|
|
+ Write-Output $dialog.SelectedPath
|
|
|
+}}
|
|
|
+"#,
|
|
|
+ escaped_path
|
|
|
);
|
|
|
|
|
|
+ let output = Command::new("powershell")
|
|
|
+ .args(["-NoProfile", "-Command", &script])
|
|
|
+ .stdout(Stdio::piped())
|
|
|
+ .stderr(Stdio::piped())
|
|
|
+ .output();
|
|
|
+
|
|
|
match output {
|
|
|
Ok(out) if out.status.success() => {
|
|
|
let path = String::from_utf8_lossy(&out.stdout).trim().to_string();
|
|
|
@@ -108,6 +124,7 @@ pub async fn select_directory(_default_path: Option<String>) -> DirectoryResult
|
|
|
{
|
|
|
// 在非 Windows 平台上,返回取消状态
|
|
|
// 实际应用中应该使用 tauri_plugin_dialog
|
|
|
+ let _ = default_path; // 避免未使用警告
|
|
|
DirectoryResult {
|
|
|
canceled: true,
|
|
|
path: None,
|