about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-07-10 11:09:31 +0000
committerbors <bors@rust-lang.org>2024-07-10 11:09:31 +0000
commitd81987661a06ae8d49a5f014f81824c655e87768 (patch)
tree7a75c609f04a5ae68afd814b94e018203a4e2ec8
parent0fdfb61795b40037955154638fe35f20b16c8e55 (diff)
parentb8b6d14de92986e420e782d9a587960f6792c7c2 (diff)
downloadrust-d81987661a06ae8d49a5f014f81824c655e87768.tar.gz
rust-d81987661a06ae8d49a5f014f81824c655e87768.zip
Auto merge of #126690 - andyolivares:feature/show_window, r=dtolnay
Exposing STARTUPINFOW.wShowWindow in CommandExt trait

Hi:

I needed a way to control how a new process's window is displayed in Windows (normal, minimized, maximized, etc).
I noticed that there is no direct way to do that (I even searched for crates doing this, but didn't find any).

Inspecting the standard library source code, I figured that it would be a good addition to CommandExt trait that allows some Windows specific customization to a Command.

This is my first time contributing to Rust, so please bear with me if I'm not following the rules :)
-rw-r--r--library/std/src/os/windows/process.rs13
-rw-r--r--library/std/src/sys/pal/windows/process.rs10
2 files changed, 23 insertions, 0 deletions
diff --git a/library/std/src/os/windows/process.rs b/library/std/src/os/windows/process.rs
index 9cca27fa5dd..05ffb8925a1 100644
--- a/library/std/src/os/windows/process.rs
+++ b/library/std/src/os/windows/process.rs
@@ -181,6 +181,14 @@ pub trait CommandExt: Sealed {
     #[stable(feature = "windows_process_extensions", since = "1.16.0")]
     fn creation_flags(&mut self, flags: u32) -> &mut process::Command;
 
+    /// Sets the field `wShowWindow` of [STARTUPINFO][1] that is passed to `CreateProcess`.
+    /// Allowed values are the ones listed in
+    /// <https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow>
+    ///
+    /// [1]: <https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow>
+    #[unstable(feature = "windows_process_extensions_show_window", issue = "127544")]
+    fn show_window(&mut self, cmd_show: u16) -> &mut process::Command;
+
     /// Forces all arguments to be wrapped in quote (`"`) characters.
     ///
     /// This is useful for passing arguments to [MSYS2/Cygwin][1] based
@@ -370,6 +378,11 @@ impl CommandExt for process::Command {
         self
     }
 
+    fn show_window(&mut self, cmd_show: u16) -> &mut process::Command {
+        self.as_inner_mut().show_window(Some(cmd_show));
+        self
+    }
+
     fn force_quotes(&mut self, enabled: bool) -> &mut process::Command {
         self.as_inner_mut().force_quotes(enabled);
         self
diff --git a/library/std/src/sys/pal/windows/process.rs b/library/std/src/sys/pal/windows/process.rs
index 2da986a1494..c62764696b8 100644
--- a/library/std/src/sys/pal/windows/process.rs
+++ b/library/std/src/sys/pal/windows/process.rs
@@ -163,6 +163,7 @@ pub struct Command {
     env: CommandEnv,
     cwd: Option<OsString>,
     flags: u32,
+    show_window: Option<u16>,
     detach: bool, // not currently exposed in std::process
     stdin: Option<Stdio>,
     stdout: Option<Stdio>,
@@ -194,6 +195,7 @@ impl Command {
             env: Default::default(),
             cwd: None,
             flags: 0,
+            show_window: None,
             detach: false,
             stdin: None,
             stdout: None,
@@ -224,6 +226,9 @@ impl Command {
     pub fn creation_flags(&mut self, flags: u32) {
         self.flags = flags;
     }
+    pub fn show_window(&mut self, cmd_show: Option<u16>) {
+        self.show_window = cmd_show;
+    }
 
     pub fn force_quotes(&mut self, enabled: bool) {
         self.force_quotes_enabled = enabled;
@@ -337,6 +342,11 @@ impl Command {
             si.hStdError = stderr.as_raw_handle();
         }
 
+        if let Some(cmd_show) = self.show_window {
+            si.dwFlags |= c::STARTF_USESHOWWINDOW;
+            si.wShowWindow = cmd_show;
+        }
+
         let si_ptr: *mut c::STARTUPINFOW;
 
         let mut proc_thread_attribute_list;