Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show_in_folder api to open file/folder in system explorer #115

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]

#[cfg(target_os = "linux")]
use std::fs::metadata;
use std::path::PathBuf;
use std::process::Command;

extern crate percent_encoding;
use tauri::http::ResponseBuilder;
use tauri::GlobalWindowEvent;
Expand Down Expand Up @@ -48,6 +53,55 @@ fn toggle_devtools(window: tauri::Window) {
}
}

#[tauri::command]
fn show_in_folder(path: String) {
#[cfg(target_os = "windows")]
{
Command::new("explorer")
.args(["/select,", &path]) // The comma after select is not a typo
.spawn()
.unwrap();
}

#[cfg(target_os = "linux")]
{
if path.contains(",") {
// see https://gitlab.freedesktop.org/dbus/dbus/-/issues/76
let new_path = match metadata(&path).unwrap().is_dir() {
true => path,
false => {
let mut path2 = PathBuf::from(path);
path2.pop();
path2.into_os_string().into_string().unwrap()
}
};
Command::new("xdg-open")
.arg(&new_path)
.spawn()
.unwrap();
} else {
Command::new("dbus-send")
.arg("--session")
.arg("--dest=org.freedesktop.FileManager1")
.arg("--type=method_call")
.arg("/org/freedesktop/FileManager1")
.arg("org.freedesktop.FileManager1.ShowItems")
.arg(format!("array:string:file:///{}", path))
.arg("string:\"\"")
.spawn()
.unwrap();
}
}

#[cfg(target_os = "macos")]
{
Command::new("open")
.args(["-R", &path])
.spawn()
.unwrap();
}
}

fn process_window_event(event: &GlobalWindowEvent) {
if let tauri::WindowEvent::CloseRequested { .. } = event.event() {
let size = event.window().inner_size().unwrap();
Expand Down Expand Up @@ -96,7 +150,7 @@ fn main() {
.on_window_event(|event| process_window_event(&event))
.invoke_handler(tauri::generate_handler![
toggle_devtools, console_log, console_error,
_get_windows_drives, _rename_path])
_get_windows_drives, _rename_path, show_in_folder])
.setup(|app| {
init::init_app(app);
Ok(())
Expand Down