Bladeren bron

默认安装路径

黄中银 2 weken geleden
bovenliggende
commit
db8e1bfe5a
5 gewijzigde bestanden met toevoegingen van 59 en 18 verwijderingen
  1. 31 14
      src-tauri/src/commands/window.rs
  2. 10 1
      src/views/BatchInstallView.vue
  3. 6 1
      src/views/GitView.vue
  4. 6 1
      src/views/NodejsView.vue
  5. 6 1
      src/views/VscodeView.vue

+ 31 - 14
src-tauri/src/commands/window.rs

@@ -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,

+ 10 - 1
src/views/BatchInstallView.vue

@@ -101,6 +101,13 @@ async function handleCancel() {
   await installStore.cancelInstall()
 }
 
+// 各软件的默认安装路径
+const defaultPaths = {
+  nodejs: 'C:\\Program Files\\nodejs',
+  vscode: 'C:\\Program Files\\Microsoft VS Code',
+  git: 'C:\\Program Files\\Git'
+}
+
 async function selectDirectory(software: 'nodejs' | 'vscode' | 'git') {
   const currentPath = software === 'nodejs'
     ? installStore.installOptions.all.nodejsPath
@@ -108,7 +115,9 @@ async function selectDirectory(software: 'nodejs' | 'vscode' | 'git') {
       ? installStore.installOptions.all.vscodePath
       : installStore.installOptions.all.gitPath
 
-  const result = await window.electronAPI.selectDirectory(currentPath || undefined)
+  // 优先使用界面上显示的路径,否则使用默认安装路径
+  const initialPath = currentPath || defaultPaths[software]
+  const result = await window.electronAPI.selectDirectory(initialPath)
   if (!result.canceled && result.path) {
     if (software === 'nodejs') {
       installStore.installOptions.all.nodejsPath = result.path

+ 6 - 1
src/views/GitView.vue

@@ -62,8 +62,13 @@ function handleInstall() {
   }
 }
 
+// Git 默认安装路径
+const defaultGitPath = 'C:\\Program Files\\Git'
+
 async function handleSelectDirectory() {
-  const result = await window.electronAPI.selectDirectory(customPath.value || undefined)
+  // 优先使用界面上显示的路径,否则使用默认安装路径
+  const initialPath = customPath.value || defaultGitPath
+  const result = await window.electronAPI.selectDirectory(initialPath)
   if (!result.canceled && result.path) {
     installStore.installOptions.all.gitPath = result.path
   }

+ 6 - 1
src/views/NodejsView.vue

@@ -62,8 +62,13 @@ function handleInstall() {
   }
 }
 
+// Node.js 默认安装路径
+const defaultNodejsPath = 'C:\\Program Files\\nodejs'
+
 async function handleSelectDirectory() {
-  const result = await window.electronAPI.selectDirectory(customPath.value || undefined)
+  // 优先使用界面上显示的路径,否则使用默认安装路径
+  const initialPath = customPath.value || defaultNodejsPath
+  const result = await window.electronAPI.selectDirectory(initialPath)
   if (!result.canceled && result.path) {
     installStore.installOptions.all.nodejsPath = result.path
   }

+ 6 - 1
src/views/VscodeView.vue

@@ -62,8 +62,13 @@ function handleInstall() {
   }
 }
 
+// VS Code 默认安装路径
+const defaultVscodePath = 'C:\\Program Files\\Microsoft VS Code'
+
 async function handleSelectDirectory() {
-  const result = await window.electronAPI.selectDirectory(customPath.value || undefined)
+  // 优先使用界面上显示的路径,否则使用默认安装路径
+  const initialPath = customPath.value || defaultVscodePath
+  const result = await window.electronAPI.selectDirectory(initialPath)
   if (!result.canceled && result.path) {
     installStore.installOptions.all.vscodePath = result.path
   }