diff --git a/Cargo.lock b/Cargo.lock index 9b1c5367..b159e6ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4124,6 +4124,7 @@ dependencies = [ "tracing", "tracing-subscriber", "uuid", + "windows-sys 0.60.2", ] [[package]] diff --git a/packages/oneclient_app/Cargo.toml b/packages/oneclient_app/Cargo.toml index 02042199..977ff266 100644 --- a/packages/oneclient_app/Cargo.toml +++ b/packages/oneclient_app/Cargo.toml @@ -48,6 +48,9 @@ freya = { git = "https://github.com/marc2332/freya.git", features = ["engine", " [target.'cfg(target_os = "macos")'.dependencies] mimalloc = "=0.1.52" +[target.'cfg(target_os = "windows")'.dependencies] +windows-sys = { version = "0.60", features = ["Win32_UI_Shell", "Win32_Foundation"] } + [patch.crates-io] freya = { git = "https://github.com/marc2332/freya.git", branch = "fix/blur" } # Build then package (per OS): diff --git a/packages/oneclient_app/src/platform.rs b/packages/oneclient_app/src/platform.rs index 6260415f..fb5dee59 100644 --- a/packages/oneclient_app/src/platform.rs +++ b/packages/oneclient_app/src/platform.rs @@ -1,4 +1,5 @@ +#[cfg(not(target_os = "windows"))] pub fn open_url(url: &str) { let result = { #[cfg(target_os = "linux")] @@ -9,10 +10,6 @@ pub fn open_url(url: &str) { { std::process::Command::new("open").arg(url).spawn() } - #[cfg(target_os = "windows")] - { - std::process::Command::new("explorer").arg(url).spawn() - } }; if let Err(err) = result { @@ -20,6 +17,40 @@ pub fn open_url(url: &str) { } } +#[cfg(target_os = "windows")] +pub fn open_url(url: &str) { + use std::os::windows::ffi::OsStrExt; + use windows_sys::Win32::UI::Shell::ShellExecuteW; + + // SW_SHOWNORMAL + const SW_SHOWNORMAL: i32 = 1; + + fn wide(s: &str) -> Vec { + std::ffi::OsStr::new(s) + .encode_wide() + .chain(std::iter::once(0)) + .collect() + } + + let operation = wide("open"); + let file = wide(url); + + let ret = unsafe { + ShellExecuteW( + std::ptr::null_mut(), + operation.as_ptr(), + file.as_ptr(), + std::ptr::null(), + std::ptr::null(), + SW_SHOWNORMAL, + ) + }; + + if (ret as isize) <= 32 { + tracing::warn!("failed to open url {url}: ShellExecuteW returned {}", ret as isize); + } +} + pub fn copy_image_to_clipboard(path: std::path::PathBuf) { std::thread::spawn(move || { let img = match image::open(&path) {