Browse Source

Merge pull request #801 from BDisp/console-init-restore

Fixes #787. Console default size is always restored on Driver Init.
Charlie Kindel 5 years ago
parent
commit
f395d3fd6a
2 changed files with 22 additions and 23 deletions
  1. 21 19
      Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
  2. 1 4
      Terminal.Gui/Core/Application.cs

+ 21 - 19
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -439,6 +439,23 @@ namespace Terminal.Gui {
 			}
 			}
 		}
 		}
 
 
+		[DllImport ("kernel32.dll", ExactSpelling = true)]
+		private static extern IntPtr GetConsoleWindow ();
+
+		[DllImport ("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
+		private static extern bool ShowWindow (IntPtr hWnd, int nCmdShow);
+
+		public const int HIDE = 0;
+		public const int MAXIMIZE = 3;
+		public const int MINIMIZE = 6;
+		public const int RESTORE = 9;
+
+		internal void ShowWindow (int state)
+		{
+			IntPtr thisConsole = GetConsoleWindow ();
+			ShowWindow (thisConsole, state);
+		}
+
 #if false // See: https://github.com/migueldeicaza/gui.cs/issues/357
 #if false // See: https://github.com/migueldeicaza/gui.cs/issues/357
 		[StructLayout (LayoutKind.Sequential)]
 		[StructLayout (LayoutKind.Sequential)]
 		public struct SMALL_RECT {
 		public struct SMALL_RECT {
@@ -495,30 +512,15 @@ namespace Terminal.Gui {
 		public override int Rows => rows;
 		public override int Rows => rows;
 
 
 		public WindowsDriver ()
 		public WindowsDriver ()
-		{
-			Initialize ();
-		}
-
-		public WindowsDriver (int cols, int rows)
-		{
-			this.cols = cols;
-			this.rows = rows;
-
-			Initialize ();
-		}
-
-		void Initialize ()
 		{
 		{
 			winConsole = new WindowsConsole ();
 			winConsole = new WindowsConsole ();
 
 
 			SetupColorsAndBorders ();
 			SetupColorsAndBorders ();
 
 
-			if (cols == 0 && rows == 0) {
-				cols = Console.WindowWidth;
-				rows = Console.WindowHeight;
-			} else {
-				Console.SetWindowSize (cols, rows);
-			}
+			cols = Console.WindowWidth;
+			rows = Console.WindowHeight;
+			winConsole.ShowWindow (WindowsConsole.RESTORE);
+
 			WindowsConsole.SmallRect.MakeEmpty (ref damageRegion);
 			WindowsConsole.SmallRect.MakeEmpty (ref damageRegion);
 
 
 			ResizeScreen ();
 			ResizeScreen ();

+ 1 - 4
Terminal.Gui/Core/Application.cs

@@ -161,7 +161,6 @@ namespace Terminal.Gui {
 
 
 		internal static bool _initialized = false;
 		internal static bool _initialized = false;
 
 
-		static int cols, rows;
 		static IMainLoopDriver oldMainLoopDriver;
 		static IMainLoopDriver oldMainLoopDriver;
 		static ConsoleDriver oldDriver;
 		static ConsoleDriver oldDriver;
 
 
@@ -189,7 +188,7 @@ namespace Terminal.Gui {
 					mainLoopDriver = new NetMainLoop (() => Console.ReadKey (true));
 					mainLoopDriver = new NetMainLoop (() => Console.ReadKey (true));
 					Driver = new NetDriver ();
 					Driver = new NetDriver ();
 				} else if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows) {
 				} else if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows) {
-					var windowsDriver = cols == 0 && rows == 0 ? new WindowsDriver () : new WindowsDriver (cols, rows);
+					var windowsDriver = new WindowsDriver ();
 					mainLoopDriver = windowsDriver;
 					mainLoopDriver = windowsDriver;
 					Driver = windowsDriver;
 					Driver = windowsDriver;
 				} else {
 				} else {
@@ -557,8 +556,6 @@ namespace Terminal.Gui {
 			}
 			}
 			last?.PositionCursor ();
 			last?.PositionCursor ();
 			Driver.Refresh ();
 			Driver.Refresh ();
-			cols = Driver.Cols;
-			rows = Driver.Rows;
 		}
 		}
 
 
 		internal static void End (View view, bool closeDriver = true)
 		internal static void End (View view, bool closeDriver = true)