Skip to content

Commit

Permalink
fix: add shellexecute-on-windows feature.
Browse files Browse the repository at this point in the history
That way, it's possible to toggle on a feature that might
cause issues in some dependency trees that contain `flate2`
with `zlib-ng` backend.
  • Loading branch information
Byron committed Mar 3, 2024
1 parent 8f26da4 commit fdaaa41
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/cross-platform-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ jobs:
echo "target flag is: ${{ env.TARGET_FLAGS }}"
echo "target dir is: ${{ env.TARGET_DIR }}"
- name: Build with all features
if: startsWith(matrix.os, 'windows')
run: ${{ env.CARGO }} build ${{ env.TARGET_FLAGS }} --all-features
- name: Build (and link)
run: ${{ env.CARGO }} build ${{ env.TARGET_FLAGS }}
- name: Run tests
Expand Down
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ keywords = ["open", "xdg-open", "start", "launch"]
include = ["src/**/*", "LICENSE.md", "README.md", "changelog.md"]
rust-version = "1.62"

[features]
## If enabled, link to `shell32` on Windows and use `ShellExecuteW` intead of a command invocation.
## That way, it should be possible to open currently opened (for writing) files as well.
##
## However, if the `flate2` crate with zlib-ng backend is also used in the same tree, it maybe that
## linkage fails.
##
## This feature is only effective on Windows.
shellexecute-on-windows = []

[[bin]]
test = false
doc = false
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub fn with_in_background<T: AsRef<OsStr>>(
///
/// See documentation of [`that()`] for more details.
pub fn that_detached(path: impl AsRef<OsStr>) -> io::Result<()> {
#[cfg(not(windows))]
#[cfg(any(not(feature = "shellexecute-on-windows"), not(windows)))]
{
let mut last_err = None;
for mut cmd in commands(path) {
Expand All @@ -251,7 +251,7 @@ pub fn that_detached(path: impl AsRef<OsStr>) -> io::Result<()> {
Err(last_err.expect("no launcher worked, at least one error"))
}

#[cfg(windows)]
#[cfg(all(windows, feature = "shellexecute-on-windows"))]
{
windows::that_detached(path)
}
Expand All @@ -263,13 +263,13 @@ pub fn that_detached(path: impl AsRef<OsStr>) -> io::Result<()> {
///
/// See documentation of [`with()`] for more details.
pub fn with_detached<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
#[cfg(not(windows))]
#[cfg(any(not(feature = "shellexecute-on-windows"), not(windows)))]
{
let mut cmd = with_command(path, app);
cmd.spawn_detached()
}

#[cfg(windows)]
#[cfg(all(windows, feature = "shellexecute-on-windows"))]
{
windows::with_detached(path, app)
}
Expand Down
4 changes: 4 additions & 0 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn wrap_in_quotes<T: AsRef<OsStr>>(path: T) -> OsString {
result
}

#[cfg(feature = "shellexecute-on-windows")]
pub fn that_detached<T: AsRef<OsStr>>(path: T) -> std::io::Result<()> {
let path = wide(path);

Expand All @@ -54,6 +55,7 @@ pub fn that_detached<T: AsRef<OsStr>>(path: T) -> std::io::Result<()> {
}
}

#[cfg(feature = "shellexecute-on-windows")]
pub fn with_detached<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> std::io::Result<()> {
let app = wide(app.into());
let path = wide(path);
Expand All @@ -79,6 +81,7 @@ fn wide<T: AsRef<OsStr>>(input: T) -> Vec<u16> {
///
/// <https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew>
#[allow(non_snake_case)]
#[cfg(feature = "shellexecute-on-windows")]
pub unsafe fn ShellExecuteW(
hwnd: isize,
lpoperation: *const u16,
Expand All @@ -105,6 +108,7 @@ pub unsafe fn ShellExecuteW(
}
}

#[cfg(feature = "shellexecute-on-windows")]
mod ffi {
/// Activates the window and displays it in its current size and position.
///
Expand Down

0 comments on commit fdaaa41

Please sign in to comment.