Add --minimized command-line argument (#2817)

* Add experimental support for --minimized on Windows

* Simplify the code
This commit is contained in:
Łukasz Domeradzki
2023-02-08 16:11:50 +01:00
committed by GitHub
parent 573080e6c0
commit d3490b4e92
3 changed files with 36 additions and 2 deletions

View File

@@ -32,6 +32,9 @@ internal static partial class NativeMethods {
[SupportedOSPlatform("Windows")]
internal const uint EnableQuickEditMode = 0x0040;
[SupportedOSPlatform("Windows")]
internal const byte ShowWindowMinimize = 6;
[SupportedOSPlatform("Windows")]
internal const sbyte StandardInputHandle = -10;
@@ -103,6 +106,17 @@ internal static partial class NativeMethods {
internal static partial EExecutionState SetThreadExecutionState(EExecutionState executionState);
#endif
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[SupportedOSPlatform("Windows")]
[return: MarshalAs(UnmanagedType.Bool)]
#if NETFRAMEWORK
[DllImport("user32.dll")]
internal static extern bool ShowWindow(nint hWnd, int nCmdShow);
#else
[LibraryImport("user32.dll")]
internal static partial bool ShowWindow(nint hWnd, int nCmdShow);
#endif
[Flags]
[SupportedOSPlatform("Windows")]
internal enum EExecutionState : uint {

View File

@@ -92,8 +92,12 @@ internal static class OS {
private static string? BackingVersion;
private static Mutex? SingleInstance;
internal static void CoreInit(bool systemRequired) {
internal static void CoreInit(bool minimized, bool systemRequired) {
if (OperatingSystem.IsWindows()) {
if (minimized) {
WindowsMinimizeConsoleWindow();
}
if (systemRequired) {
WindowsKeepSystemActive();
}
@@ -328,6 +332,17 @@ internal static class OS {
}
}
[SupportedOSPlatform("Windows")]
private static void WindowsMinimizeConsoleWindow() {
if (!OperatingSystem.IsWindows()) {
throw new PlatformNotSupportedException();
}
using Process process = Process.GetCurrentProcess();
NativeMethods.ShowWindow(process.MainWindowHandle, NativeMethods.ShowWindowMinimize);
}
[Flags]
[SupportedOSPlatform("FreeBSD")]
[SupportedOSPlatform("Linux")]

View File

@@ -71,6 +71,7 @@ internal static class Program {
private static bool IgnoreUnsupportedEnvironment;
private static bool InputCryptkeyManually;
private static bool Minimized;
private static bool SystemRequired;
internal static async Task Exit(byte exitCode = 0) {
@@ -283,7 +284,7 @@ internal static class Program {
return false;
}
OS.CoreInit(SystemRequired);
OS.CoreInit(Minimized, SystemRequired);
Console.Title = SharedInfo.ProgramIdentifier;
ASF.ArchiLogger.LogGenericInfo(SharedInfo.ProgramIdentifier);
@@ -580,6 +581,10 @@ internal static class Program {
case "--INPUT-CRYPTKEY" when noArgumentValueNext():
InputCryptkeyManually = true;
break;
case "--MINIMIZED" when noArgumentValueNext():
Minimized = true;
break;
case "--NETWORK-GROUP" when noArgumentValueNext():
networkGroupNext = true;