Skip to content

Commit 0da8af8

Browse files
authored
desktop: move open_path to rust (anomalyco#15323)
1 parent 2a4ed49 commit 0da8af8

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

packages/desktop/src-tauri/src/lib.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,31 @@ fn resolve_app_path(app_name: &str) -> Option<String> {
181181

182182
#[tauri::command]
183183
#[specta::specta]
184-
fn open_in_powershell(path: String) -> Result<(), String> {
184+
fn open_path(_app: AppHandle, path: String, app_name: Option<String>) -> Result<(), String> {
185185
#[cfg(target_os = "windows")]
186186
{
187-
return os::windows::open_in_powershell(path);
187+
let app_name = app_name.map(|v| os::windows::resolve_windows_app_path(&v).unwrap_or(v));
188+
let is_powershell = app_name.as_ref().is_some_and(|v| {
189+
std::path::Path::new(v)
190+
.file_name()
191+
.and_then(|name| name.to_str())
192+
.is_some_and(|name| {
193+
name.eq_ignore_ascii_case("powershell")
194+
|| name.eq_ignore_ascii_case("powershell.exe")
195+
})
196+
});
197+
198+
if is_powershell {
199+
return os::windows::open_in_powershell(path);
200+
}
201+
202+
return tauri_plugin_opener::open_path(path, app_name.as_deref())
203+
.map_err(|e| format!("Failed to open path: {e}"));
188204
}
189205

190206
#[cfg(not(target_os = "windows"))]
191-
Err("PowerShell is only supported on Windows".to_string())
207+
tauri_plugin_opener::open_path(path, app_name.as_deref())
208+
.map_err(|e| format!("Failed to open path: {e}"))
192209
}
193210

194211
#[cfg(target_os = "macos")]
@@ -386,7 +403,7 @@ fn make_specta_builder() -> tauri_specta::Builder<tauri::Wry> {
386403
check_app_exists,
387404
wsl_path,
388405
resolve_app_path,
389-
open_in_powershell
406+
open_path
390407
])
391408
.events(tauri_specta::collect_events![
392409
LoadingWindowComplete,

packages/desktop/src/bindings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const commands = {
1818
checkAppExists: (appName: string) => __TAURI_INVOKE<boolean>("check_app_exists", { appName }),
1919
wslPath: (path: string, mode: "windows" | "linux" | null) => __TAURI_INVOKE<string>("wsl_path", { path, mode }),
2020
resolveAppPath: (appName: string) => __TAURI_INVOKE<string | null>("resolve_app_path", { appName }),
21-
openInPowershell: (path: string) => __TAURI_INVOKE<null>("open_in_powershell", { path }),
21+
openPath: (path: string, appName: string | null) => __TAURI_INVOKE<null>("open_path", { path, appName }),
2222
};
2323

2424
/** Events */

packages/desktop/src/index.tsx

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { getCurrent, onOpenUrl } from "@tauri-apps/plugin-deep-link"
1717
import { open, save } from "@tauri-apps/plugin-dialog"
1818
import { fetch as tauriFetch } from "@tauri-apps/plugin-http"
1919
import { isPermissionGranted, requestPermission } from "@tauri-apps/plugin-notification"
20-
import { openPath as openerOpenPath } from "@tauri-apps/plugin-opener"
2120
import { type as ostype } from "@tauri-apps/plugin-os"
2221
import { relaunch } from "@tauri-apps/plugin-process"
2322
import { open as shellOpen } from "@tauri-apps/plugin-shell"
@@ -116,29 +115,7 @@ const createPlatform = (): Platform => {
116115
void shellOpen(url).catch(() => undefined)
117116
},
118117
async openPath(path: string, app?: string) {
119-
const os = ostype()
120-
if (os === "windows") {
121-
const resolvedPath = await (async () => {
122-
if (window.__OPENCODE__?.wsl) {
123-
const converted = await commands.wslPath(path, "windows").catch(() => null)
124-
if (converted) return converted
125-
}
126-
127-
return path
128-
})()
129-
const resolvedApp = (app && (await commands.resolveAppPath(app))) || app
130-
const isPowershell = (value?: string) => {
131-
if (!value) return false
132-
const name = value.toLowerCase().replaceAll("/", "\\").split("\\").pop()
133-
return name === "powershell" || name === "powershell.exe"
134-
}
135-
if (isPowershell(resolvedApp)) {
136-
await commands.openInPowershell(resolvedPath)
137-
return
138-
}
139-
return openerOpenPath(resolvedPath, resolvedApp)
140-
}
141-
return openerOpenPath(path, app)
118+
await commands.openPath(path, app ?? null)
142119
},
143120

144121
back() {

0 commit comments

Comments
 (0)