desktop: move open_path to rust (#15323)

This commit is contained in:
Brendan Allan
2026-02-27 14:17:24 +08:00
committed by GitHub
parent 2a4ed49551
commit 0da8af8a28
3 changed files with 23 additions and 29 deletions

View File

@@ -181,14 +181,31 @@ fn resolve_app_path(app_name: &str) -> Option<String> {
#[tauri::command] #[tauri::command]
#[specta::specta] #[specta::specta]
fn open_in_powershell(path: String) -> Result<(), String> { fn open_path(_app: AppHandle, path: String, app_name: Option<String>) -> Result<(), String> {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ {
let app_name = app_name.map(|v| os::windows::resolve_windows_app_path(&v).unwrap_or(v));
let is_powershell = app_name.as_ref().is_some_and(|v| {
std::path::Path::new(v)
.file_name()
.and_then(|name| name.to_str())
.is_some_and(|name| {
name.eq_ignore_ascii_case("powershell")
|| name.eq_ignore_ascii_case("powershell.exe")
})
});
if is_powershell {
return os::windows::open_in_powershell(path); return os::windows::open_in_powershell(path);
} }
return tauri_plugin_opener::open_path(path, app_name.as_deref())
.map_err(|e| format!("Failed to open path: {e}"));
}
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
Err("PowerShell is only supported on Windows".to_string()) tauri_plugin_opener::open_path(path, app_name.as_deref())
.map_err(|e| format!("Failed to open path: {e}"))
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
@@ -386,7 +403,7 @@ fn make_specta_builder() -> tauri_specta::Builder<tauri::Wry> {
check_app_exists, check_app_exists,
wsl_path, wsl_path,
resolve_app_path, resolve_app_path,
open_in_powershell open_path
]) ])
.events(tauri_specta::collect_events![ .events(tauri_specta::collect_events![
LoadingWindowComplete, LoadingWindowComplete,

View File

@@ -18,7 +18,7 @@ export const commands = {
checkAppExists: (appName: string) => __TAURI_INVOKE<boolean>("check_app_exists", { appName }), checkAppExists: (appName: string) => __TAURI_INVOKE<boolean>("check_app_exists", { appName }),
wslPath: (path: string, mode: "windows" | "linux" | null) => __TAURI_INVOKE<string>("wsl_path", { path, mode }), wslPath: (path: string, mode: "windows" | "linux" | null) => __TAURI_INVOKE<string>("wsl_path", { path, mode }),
resolveAppPath: (appName: string) => __TAURI_INVOKE<string | null>("resolve_app_path", { appName }), resolveAppPath: (appName: string) => __TAURI_INVOKE<string | null>("resolve_app_path", { appName }),
openInPowershell: (path: string) => __TAURI_INVOKE<null>("open_in_powershell", { path }), openPath: (path: string, appName: string | null) => __TAURI_INVOKE<null>("open_path", { path, appName }),
}; };
/** Events */ /** Events */

View File

@@ -17,7 +17,6 @@ import { getCurrent, onOpenUrl } from "@tauri-apps/plugin-deep-link"
import { open, save } from "@tauri-apps/plugin-dialog" import { open, save } from "@tauri-apps/plugin-dialog"
import { fetch as tauriFetch } from "@tauri-apps/plugin-http" import { fetch as tauriFetch } from "@tauri-apps/plugin-http"
import { isPermissionGranted, requestPermission } from "@tauri-apps/plugin-notification" import { isPermissionGranted, requestPermission } from "@tauri-apps/plugin-notification"
import { openPath as openerOpenPath } from "@tauri-apps/plugin-opener"
import { type as ostype } from "@tauri-apps/plugin-os" import { type as ostype } from "@tauri-apps/plugin-os"
import { relaunch } from "@tauri-apps/plugin-process" import { relaunch } from "@tauri-apps/plugin-process"
import { open as shellOpen } from "@tauri-apps/plugin-shell" import { open as shellOpen } from "@tauri-apps/plugin-shell"
@@ -116,29 +115,7 @@ const createPlatform = (): Platform => {
void shellOpen(url).catch(() => undefined) void shellOpen(url).catch(() => undefined)
}, },
async openPath(path: string, app?: string) { async openPath(path: string, app?: string) {
const os = ostype() await commands.openPath(path, app ?? null)
if (os === "windows") {
const resolvedPath = await (async () => {
if (window.__OPENCODE__?.wsl) {
const converted = await commands.wslPath(path, "windows").catch(() => null)
if (converted) return converted
}
return path
})()
const resolvedApp = (app && (await commands.resolveAppPath(app))) || app
const isPowershell = (value?: string) => {
if (!value) return false
const name = value.toLowerCase().replaceAll("/", "\\").split("\\").pop()
return name === "powershell" || name === "powershell.exe"
}
if (isPowershell(resolvedApp)) {
await commands.openInPowershell(resolvedPath)
return
}
return openerOpenPath(resolvedPath, resolvedApp)
}
return openerOpenPath(path, app)
}, },
back() { back() {