Last active
February 28, 2024 19:37
-
-
Save elibroftw/78486b49858adda9f0e7da3ffbabdb26 to your computer and use it in GitHub Desktop.
Tauri show in folder snippet. sync with https://github.com/tauri-apps/tauri/issues/4062#issuecomment-1338048169
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[target.'cfg(target_os = "linux")'.dependencies] | |
dbus = "0.9" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[cfg(target_os = "linux")] | |
pub struct DbusState(Mutex<Option<dbus::blocking::SyncConnection>>); | |
mod show_in_folder; | |
use show_in_folder::{show_item_in_folder}; | |
// in main ... | |
tauri::Builder::default() | |
.invoke_handler(tauri::generate_handler![ | |
show_item_in_folder | |
]) | |
.setup(|app| { | |
#[cfg(target_os = "linux")] | |
app.manage(DbusState(Mutex::new(dbus::blocking::SyncConnection::new_session().ok()))); | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::process::Command; | |
// State is used by linux | |
use tauri::{Manager, State}; | |
#[cfg(not(target_os = "windows"))] | |
use std::path::PathBuf; | |
#[cfg(target_os = "linux")] | |
use crate::DbusState; | |
#[cfg(target_os = "linux")] | |
use std::time::Duration; | |
#[cfg(target_os = "linux")] | |
#[tauri::command] | |
pub fn show_item_in_folder(path: String, dbus_state: State<DbusState>) -> Result<(), String> { | |
let dbus_guard = dbus_state.0.lock().map_err(|e| e.to_string())?; | |
// see https://gitlab.freedesktop.org/dbus/dbus/-/issues/76 | |
if dbus_guard.is_none() || path.contains(",") { | |
let mut path_buf = PathBuf::from(&path); | |
let new_path = match path_buf.is_dir() { | |
true => path, | |
false => { | |
path_buf.pop(); | |
path_buf.into_os_string().into_string().unwrap() | |
} | |
}; | |
Command::new("xdg-open") | |
.arg(&new_path) | |
.spawn() | |
.map_err(|e| format!("{e:?}"))?; | |
} else { | |
// https://docs.rs/dbus/latest/dbus/ | |
let dbus = dbus_guard.as_ref().unwrap(); | |
let proxy = dbus.with_proxy( | |
"org.freedesktop.FileManager1", | |
"/org/freedesktop/FileManager1", | |
Duration::from_secs(5), | |
); | |
let (_,): (bool,) = proxy | |
.method_call( | |
"org.freedesktop.FileManager1", | |
"ShowItems", | |
(vec![format!("file://{path}")], ""), | |
) | |
.map_err(|e| e.to_string())?; | |
} | |
Ok(()) | |
} | |
#[cfg(not(target_os = "linux"))] | |
#[tauri::command] | |
pub fn show_item_in_folder(path: String) -> Result<(), String> { | |
#[cfg(target_os = "windows")] | |
{ | |
Command::new("explorer") | |
.args(["/select,", &path]) // The comma after select is not a typo | |
.spawn() | |
.map_err(|e| e.to_string())?; | |
} | |
#[cfg(target_os = "macos")] | |
{ | |
let path_buf = PathBuf::from(&path); | |
if path_buf.is_dir() { | |
Command::new("open") | |
.args([&path]) | |
.spawn() | |
.map_err(|e| e.to_string())?; | |
} else { | |
Command::new("open") | |
.args(["-R", &path]) | |
.spawn() | |
.map_err(|e| e.to_string())?; | |
} | |
} | |
Ok(()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { tauri } from '@tauri-apps/api'; | |
async function show_in_folder(path) { | |
await tauri.invoke('show_item_in_folder', {path}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment