diff --git a/backend/tauri/src/cmds.rs b/backend/tauri/src/cmds.rs index e9648c4de8..7d03e250cd 100644 --- a/backend/tauri/src/cmds.rs +++ b/backend/tauri/src/cmds.rs @@ -93,7 +93,7 @@ pub fn patch_profile(index: String, profile: PrfItem) -> CmdResult { } #[tauri::command] -pub fn view_profile(index: String) -> CmdResult { +pub fn view_profile(app_handle: tauri::AppHandle, index: String) -> CmdResult { let file = { wrap_err!(Config::profiles().latest().get_item(&index))? .file @@ -106,7 +106,7 @@ pub fn view_profile(index: String) -> CmdResult { ret_err!("the file not found"); } - wrap_err!(help::open_file(path)) + wrap_err!(help::open_file(app_handle, path)) } #[tauri::command] diff --git a/backend/tauri/src/utils/help.rs b/backend/tauri/src/utils/help.rs index 03a89f444a..5c2aaaa5d3 100644 --- a/backend/tauri/src/utils/help.rs +++ b/backend/tauri/src/utils/help.rs @@ -3,7 +3,10 @@ use nanoid::nanoid; use serde::{de::DeserializeOwned, Serialize}; use serde_yaml::{Mapping, Value}; use std::{fs, path::PathBuf, str::FromStr}; - +use tauri::{ + api::shell::{open, Program}, + Manager, +}; /// read data from yaml as struct T pub fn read_yaml(path: &PathBuf) -> Result { if !path.exists() { @@ -81,18 +84,20 @@ pub fn parse_str(target: &str, key: &str) -> Option { /// open file /// use vscode by default -pub fn open_file(path: PathBuf) -> Result<()> { +pub fn open_file(app: tauri::AppHandle, path: PathBuf) -> Result<()> { #[cfg(target_os = "macos")] let code = "Visual Studio Code"; #[cfg(not(target_os = "macos"))] let code = "code"; - // use vscode first - if let Err(err) = open::with(&path, code) { - log::error!(target: "app", "failed to open file with VScode `{err}`"); - // default open - open::that(path)?; - } + let _ = match Program::from_str(code) { + Ok(code) => open(&app.shell_scope(), &path.to_string_lossy(), Some(code)), + Err(err) => { + log::error!(target: "app", "Can't find VScode `{err}`"); + // default open + open(&app.shell_scope(), &path.to_string_lossy(), None) + } + }; Ok(()) }