From 88bf87b8bb870dcc12f9d23ff2b78bf20956c0b8 Mon Sep 17 00:00:00 2001 From: BDisp Date: Wed, 30 Jul 2025 22:50:33 +0100 Subject: [PATCH] Fixes issue on restore window size after maximize causing width shrinking --- .../Drivers/WindowsDriver/WindowsConsole.cs | 7 +++++++ .../Drivers/WindowsDriver/WindowsDriver.cs | 15 ++++----------- .../Drivers/WindowsDriver/WindowsMainLoop.cs | 18 +++++++++++++++++- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/Terminal.Gui/Drivers/WindowsDriver/WindowsConsole.cs b/Terminal.Gui/Drivers/WindowsDriver/WindowsConsole.cs index e2611fd05..7073a903c 100644 --- a/Terminal.Gui/Drivers/WindowsDriver/WindowsConsole.cs +++ b/Terminal.Gui/Drivers/WindowsDriver/WindowsConsole.cs @@ -632,6 +632,13 @@ internal partial class WindowsConsole return new (winRect.Right + 1, newRows - 1 < 0 ? 0 : winRect.Bottom + 1); } + internal Size GetLargestConsoleWindowSize () + { + Coord maxWinSize = GetLargestConsoleWindowSize (IsVirtualTerminal ? _outputHandle : _screenBuffer); + + return new (maxWinSize.X, maxWinSize.Y); + } + private void SetConsoleOutputWindow (CONSOLE_SCREEN_BUFFER_INFOEX csbi) { if ((IsVirtualTerminal diff --git a/Terminal.Gui/Drivers/WindowsDriver/WindowsDriver.cs b/Terminal.Gui/Drivers/WindowsDriver/WindowsDriver.cs index 7a265e8dc..c4e333a5e 100644 --- a/Terminal.Gui/Drivers/WindowsDriver/WindowsDriver.cs +++ b/Terminal.Gui/Drivers/WindowsDriver/WindowsDriver.cs @@ -420,9 +420,9 @@ internal class WindowsDriver : ConsoleDriver { if (WinConsole is { }) { - // BUGBUG: The results from GetConsoleOutputWindow are incorrect when called from Init. - // Our thread in WindowsMainLoop.CheckWin will get the correct results. See #if HACK_CHECK_WINCHANGED - Size winSize = WinConsole.GetConsoleOutputWindow (out _); + // The results from GetConsoleBufferWindow are correct when called from Init. + // Our thread in WindowsMainLoop.CheckWin will get the resize event. See #if HACK_CHECK_WINCHANGED + Size winSize = WinConsole.GetConsoleBufferWindow (out _); Cols = winSize.Width; Rows = winSize.Height; OnSizeChanged (new SizeChangedEventArgs (new (Cols, Rows))); @@ -602,13 +602,6 @@ internal class WindowsDriver : ConsoleDriver return; } - int w = e.Size.Value.Width; - - if (w == Cols - 3 && e.Size.Value.Height < Rows) - { - w += 3; - } - Left = 0; Top = 0; Cols = e.Size.Value.Width; @@ -617,7 +610,7 @@ internal class WindowsDriver : ConsoleDriver if (!RunningUnitTests) { Size newSize = WinConsole.SetConsoleWindow ( - (short)Math.Max (w, 16), + (short)Math.Max (e.Size.Value.Width, 16), (short)Math.Max (e.Size.Value.Height, 0)); Cols = newSize.Width; diff --git a/Terminal.Gui/Drivers/WindowsDriver/WindowsMainLoop.cs b/Terminal.Gui/Drivers/WindowsDriver/WindowsMainLoop.cs index 73308ae85..1dc098145 100644 --- a/Terminal.Gui/Drivers/WindowsDriver/WindowsMainLoop.cs +++ b/Terminal.Gui/Drivers/WindowsDriver/WindowsMainLoop.cs @@ -220,6 +220,7 @@ internal class WindowsMainLoop : IMainLoopDriver private readonly ManualResetEventSlim _winChange = new (false); private bool _winChanged; private Size _windowSize; + private Size? _lastWindowSizeBeforeMaximized = null; private void CheckWinChange () { while (_mainLoop is { }) @@ -232,7 +233,22 @@ internal class WindowsMainLoop : IMainLoopDriver while (_mainLoop is { }) { Task.Delay (500).Wait (); - _windowSize = _winConsole.GetConsoleBufferWindow (out _); + Size largestWindowSize = _winConsole!.GetLargestConsoleWindowSize (); + _windowSize = _winConsole!.GetConsoleBufferWindow (out _); + + if (_lastWindowSizeBeforeMaximized is null && _windowSize == largestWindowSize) + { + _lastWindowSizeBeforeMaximized = new (_consoleDriver.Cols, _consoleDriver.Rows); + } + else if (_lastWindowSizeBeforeMaximized is { } && _windowSize != largestWindowSize) + { + if (_windowSize != _lastWindowSizeBeforeMaximized) + { + _windowSize = _lastWindowSizeBeforeMaximized.Value; + } + + _lastWindowSizeBeforeMaximized = null; + } if (_windowSize != Size.Empty && (_windowSize.Width != _consoleDriver.Cols