Skip to main content

Mountain/IPC/WindServiceHandlers/NativeHost/
ShowItemInFolder.rs

1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3//! Wire method: `native:showItemInFolder`, `nativeHost:showItemInFolder`.
4//! Reveals a path in the platform file manager (Finder / Explorer / Linux FM).
5
6use std::sync::Arc;
7
8use serde_json::Value;
9
10use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
11
12pub async fn ShowItemInFolder(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
13	let path_str = Arguments
14		.get(0)
15		.ok_or("Missing file path".to_string())?
16		.as_str()
17		.ok_or("File path must be a string".to_string())?;
18
19	dev_log!("vfs", "showInFolder: {}", path_str);
20
21	let path = std::path::PathBuf::from(path_str);
22
23	if !path.exists() {
24		return Err(format!("Path does not exist: {}", path_str));
25	}
26
27	#[cfg(target_os = "macos")]
28	{
29		use std::process::Command;
30
31		let result = Command::new("open")
32			.arg("-R")
33			.arg(&path)
34			.output()
35			.map_err(|Error| format!("Failed to execute open command: {}", Error))?;
36
37		if !result.status.success() {
38			return Err(format!(
39				"Failed to show item in folder: {}",
40				String::from_utf8_lossy(&result.stderr)
41			));
42		}
43	}
44
45	#[cfg(target_os = "windows")]
46	{
47		use std::process::Command;
48
49		let result = Command::new("explorer")
50			.arg("/select,")
51			.arg(&path)
52			.output()
53			.map_err(|Error| format!("Failed to execute explorer command: {}", Error))?;
54
55		if !result.status.success() {
56			return Err(format!(
57				"Failed to show item in folder: {}",
58				String::from_utf8_lossy(&result.stderr)
59			));
60		}
61	}
62
63	#[cfg(target_os = "linux")]
64	{
65		use std::process::Command;
66
67		let file_managers = ["nautilus", "dolphin", "thunar", "pcmanfm", "nemo"];
68		let mut last_error = String::new();
69
70		for manager in file_managers.iter() {
71			let result = Command::new(manager).arg(&path).output();
72
73			match result {
74				Ok(output) if output.status.success() => {
75					dev_log!("lifecycle", "opened with {}", manager);
76					break;
77				},
78				Err(e) => {
79					last_error = e.to_string();
80					continue;
81				},
82				_ => continue,
83			}
84		}
85
86		if !last_error.is_empty() {
87			return Err(format!("Failed to show item in folder with any file manager: {}", last_error));
88		}
89	}
90
91	dev_log!("vfs", "showed in folder: {}", path_str);
92	Ok(Value::Bool(true))
93}