From 27aa9924f386f29c14f6da2ee14e35e2b57bebbe Mon Sep 17 00:00:00 2001 From: liushuyu Date: Sun, 19 Jul 2020 19:27:09 -0600 Subject: [PATCH] ui/ux: implement "View Local Files" function... ... for Windows --- src/native/mod.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/native/mod.rs b/src/native/mod.rs index 246337b..69951d8 100644 --- a/src/native/mod.rs +++ b/src/native/mod.rs @@ -18,6 +18,9 @@ mod natives { use crate::logging::LoggingErrors; use std::env; + use std::os::windows::ffi::OsStrExt; + use std::path::Path; + use std::process::Command; use winapi::shared::minwindef::{DWORD, FALSE, MAX_PATH}; @@ -26,9 +29,11 @@ mod natives { use winapi::um::psapi::{ EnumProcessModulesEx, GetModuleFileNameExW, K32EnumProcesses, LIST_MODULES_ALL, }; + use winapi::um::shellapi::ShellExecuteW; use winapi::um::winnt::{ HANDLE, PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE, PROCESS_VM_READ, }; + use winapi::um::winuser::SW_SHOWDEFAULT; use widestring::U16CString; @@ -103,8 +108,23 @@ mod natives { } } + // Needed to call unsafe function `ShellExecuteW` from `winapi` crate + #[allow(unsafe_code)] pub fn open_in_shell(path: &Path) { - // TODO + let native_verb = U16CString::from_str("open").unwrap(); + // https://doc.rust-lang.org/std/os/windows/ffi/trait.OsStrExt.html#tymethod.encode_wide + let mut native_path: Vec = path.as_os_str().encode_wide().collect(); + native_path.push(0); // NULL terminator + unsafe { + ShellExecuteW( + std::ptr::null_mut(), + native_verb.as_ptr(), + native_path.as_ptr(), + std::ptr::null_mut(), + std::ptr::null_mut(), + SW_SHOWDEFAULT, + ); + } } /// Cleans up the installer