Skip to content

Commit f8a0e23

Browse files
authored
Merge pull request #3809 from BDisp/v1_3752_windowsdriver-wt-preview-fix
Fixes #3752. Tracking Windows Terminal Preview Issue - App size is corrupted
2 parents 07672c3 + 5c0dc43 commit f8a0e23

File tree

1 file changed

+133
-126
lines changed

1 file changed

+133
-126
lines changed

Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

+133-126
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ internal class WindowsConsole {
1818
public const int STD_ERROR_HANDLE = -12;
1919

2020
internal IntPtr InputHandle, OutputHandle;
21-
IntPtr ScreenBuffer;
2221
readonly uint originalConsoleMode;
2322
CursorVisibility? initialCursorVisibility = null;
2423
CursorVisibility? currentCursorVisibility = null;
@@ -40,47 +39,47 @@ public WindowsConsole ()
4039

4140
public bool WriteToConsole (Size size, CharInfo [] charInfoBuffer, Coord coords, SmallRect window)
4241
{
43-
if (ScreenBuffer == IntPtr.Zero) {
44-
ReadFromConsoleOutput (size, coords, ref window);
45-
}
42+
//if (OutputHandle == IntPtr.Zero) {
43+
// ReadFromConsoleOutput (size, coords, ref window);
44+
//}
4645

47-
return WriteConsoleOutput (ScreenBuffer, charInfoBuffer, coords, new Coord () { X = window.Left, Y = window.Top }, ref window);
46+
return WriteConsoleOutput (OutputHandle, charInfoBuffer, coords, new Coord () { X = window.Left, Y = window.Top }, ref window);
4847
}
4948

50-
public void ReadFromConsoleOutput (Size size, Coord coords, ref SmallRect window)
51-
{
52-
ScreenBuffer = CreateConsoleScreenBuffer (
53-
DesiredAccess.GenericRead | DesiredAccess.GenericWrite,
54-
ShareMode.FileShareRead | ShareMode.FileShareWrite,
55-
IntPtr.Zero,
56-
1,
57-
IntPtr.Zero
58-
);
59-
if (ScreenBuffer == INVALID_HANDLE_VALUE) {
60-
var err = Marshal.GetLastWin32Error ();
49+
//public void ReadFromConsoleOutput (Size size, Coord coords, ref SmallRect window)
50+
//{
51+
// OutputHandle = CreateConsoleScreenBuffer (
52+
// DesiredAccess.GenericRead | DesiredAccess.GenericWrite,
53+
// ShareMode.FileShareRead | ShareMode.FileShareWrite,
54+
// IntPtr.Zero,
55+
// 1,
56+
// IntPtr.Zero
57+
// );
58+
// if (ScreenBuffer == INVALID_HANDLE_VALUE) {
59+
// var err = Marshal.GetLastWin32Error ();
6160

62-
if (err != 0)
63-
throw new System.ComponentModel.Win32Exception (err);
64-
}
61+
// if (err != 0)
62+
// throw new System.ComponentModel.Win32Exception (err);
63+
// }
6564

66-
if (!initialCursorVisibility.HasValue && GetCursorVisibility (out CursorVisibility visibility)) {
67-
initialCursorVisibility = visibility;
68-
}
65+
// if (!initialCursorVisibility.HasValue && GetCursorVisibility (out CursorVisibility visibility)) {
66+
// initialCursorVisibility = visibility;
67+
// }
6968

70-
if (!SetConsoleActiveScreenBuffer (ScreenBuffer)) {
71-
throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
72-
}
69+
// if (!SetConsoleActiveScreenBuffer (ScreenBuffer)) {
70+
// throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
71+
// }
7372

74-
OriginalStdOutChars = new CharInfo [size.Height * size.Width];
73+
// OriginalStdOutChars = new CharInfo [size.Height * size.Width];
7574

76-
if (!ReadConsoleOutput (ScreenBuffer, OriginalStdOutChars, coords, new Coord () { X = 0, Y = 0 }, ref window)) {
77-
throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
78-
}
79-
}
75+
// if (!ReadConsoleOutput (ScreenBuffer, OriginalStdOutChars, coords, new Coord () { X = 0, Y = 0 }, ref window)) {
76+
// throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
77+
// }
78+
//}
8079

8180
public bool SetCursorPosition (Coord position)
8281
{
83-
return SetConsoleCursorPosition (ScreenBuffer, position);
82+
return SetConsoleCursorPosition (OutputHandle, position);
8483
}
8584

8685
public void SetInitialCursorVisibility ()
@@ -92,11 +91,11 @@ public void SetInitialCursorVisibility ()
9291

9392
public bool GetCursorVisibility (out CursorVisibility visibility)
9493
{
95-
if (ScreenBuffer == IntPtr.Zero) {
94+
if (OutputHandle == IntPtr.Zero) {
9695
visibility = CursorVisibility.Invisible;
9796
return false;
9897
}
99-
if (!GetConsoleCursorInfo (ScreenBuffer, out ConsoleCursorInfo info)) {
98+
if (!GetConsoleCursorInfo (OutputHandle, out ConsoleCursorInfo info)) {
10099
var err = Marshal.GetLastWin32Error ();
101100
if (err != 0) {
102101
throw new System.ComponentModel.Win32Exception (err);
@@ -149,7 +148,7 @@ public bool SetCursorVisibility (CursorVisibility visibility)
149148
bVisible = ((uint)visibility & 0xFF00) != 0
150149
};
151150

152-
if (!SetConsoleCursorInfo (ScreenBuffer, ref info))
151+
if (!SetConsoleCursorInfo (OutputHandle, ref info))
153152
return false;
154153

155154
currentCursorVisibility = visibility;
@@ -165,28 +164,28 @@ public void Cleanup ()
165164
}
166165

167166
ConsoleMode = originalConsoleMode;
168-
if (!SetConsoleActiveScreenBuffer (OutputHandle)) {
169-
var err = Marshal.GetLastWin32Error ();
170-
Console.WriteLine ("Error: {0}", err);
171-
}
167+
//if (!SetConsoleActiveScreenBuffer (OutputHandle)) {
168+
// var err = Marshal.GetLastWin32Error ();
169+
// Console.WriteLine ("Error: {0}", err);
170+
//}
172171

173-
if (ScreenBuffer != IntPtr.Zero) {
174-
CloseHandle (ScreenBuffer);
175-
}
172+
//if (ScreenBuffer != IntPtr.Zero) {
173+
// CloseHandle (ScreenBuffer);
174+
//}
176175

177-
ScreenBuffer = IntPtr.Zero;
176+
//ScreenBuffer = IntPtr.Zero;
178177
}
179178

180179
internal Size GetConsoleBufferWindow (out Point position)
181180
{
182-
if (ScreenBuffer == IntPtr.Zero) {
181+
if (OutputHandle == IntPtr.Zero) {
183182
position = Point.Empty;
184183
return Size.Empty;
185184
}
186185

187186
var csbi = new CONSOLE_SCREEN_BUFFER_INFOEX ();
188187
csbi.cbSize = (uint)Marshal.SizeOf (csbi);
189-
if (!GetConsoleScreenBufferInfoEx (ScreenBuffer, ref csbi)) {
188+
if (!GetConsoleScreenBufferInfoEx (OutputHandle, ref csbi)) {
190189
//throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
191190
position = Point.Empty;
192191
return Size.Empty;
@@ -212,65 +211,65 @@ internal Size GetConsoleOutputWindow (out Point position)
212211
return sz;
213212
}
214213

215-
internal Size SetConsoleWindow (short cols, short rows)
216-
{
217-
var csbi = new CONSOLE_SCREEN_BUFFER_INFOEX ();
218-
csbi.cbSize = (uint)Marshal.SizeOf (csbi);
219-
220-
if (!GetConsoleScreenBufferInfoEx (ScreenBuffer, ref csbi)) {
221-
throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
222-
}
223-
var maxWinSize = GetLargestConsoleWindowSize (ScreenBuffer);
224-
var newCols = Math.Min (cols, maxWinSize.X);
225-
var newRows = Math.Min (rows, maxWinSize.Y);
226-
csbi.dwSize = new Coord (newCols, Math.Max (newRows, (short)1));
227-
csbi.srWindow = new SmallRect (0, 0, newCols, newRows);
228-
csbi.dwMaximumWindowSize = new Coord (newCols, newRows);
229-
if (!SetConsoleScreenBufferInfoEx (ScreenBuffer, ref csbi)) {
230-
throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
231-
}
232-
var winRect = new SmallRect (0, 0, (short)(newCols - 1), (short)Math.Max (newRows - 1, 0));
233-
if (!SetConsoleWindowInfo (OutputHandle, true, ref winRect)) {
234-
//throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
235-
return new Size (cols, rows);
236-
}
237-
SetConsoleOutputWindow (csbi);
238-
return new Size (winRect.Right + 1, newRows - 1 < 0 ? 0 : winRect.Bottom + 1);
239-
}
240-
241-
void SetConsoleOutputWindow (CONSOLE_SCREEN_BUFFER_INFOEX csbi)
242-
{
243-
if (ScreenBuffer != IntPtr.Zero && !SetConsoleScreenBufferInfoEx (ScreenBuffer, ref csbi)) {
244-
throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
245-
}
246-
}
247-
248-
internal Size SetConsoleOutputWindow (out Point position)
249-
{
250-
if (ScreenBuffer == IntPtr.Zero) {
251-
position = Point.Empty;
252-
return Size.Empty;
253-
}
254-
255-
var csbi = new CONSOLE_SCREEN_BUFFER_INFOEX ();
256-
csbi.cbSize = (uint)Marshal.SizeOf (csbi);
257-
if (!GetConsoleScreenBufferInfoEx (ScreenBuffer, ref csbi)) {
258-
throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
259-
}
260-
var sz = new Size (csbi.srWindow.Right - csbi.srWindow.Left + 1,
261-
Math.Max (csbi.srWindow.Bottom - csbi.srWindow.Top + 1, 0));
262-
position = new Point (csbi.srWindow.Left, csbi.srWindow.Top);
263-
SetConsoleOutputWindow (csbi);
264-
var winRect = new SmallRect (0, 0, (short)(sz.Width - 1), (short)Math.Max (sz.Height - 1, 0));
265-
if (!SetConsoleScreenBufferInfoEx (OutputHandle, ref csbi)) {
266-
throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
267-
}
268-
if (!SetConsoleWindowInfo (OutputHandle, true, ref winRect)) {
269-
throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
270-
}
271-
272-
return sz;
273-
}
214+
//internal Size SetConsoleWindow (short cols, short rows)
215+
//{
216+
// var csbi = new CONSOLE_SCREEN_BUFFER_INFOEX ();
217+
// csbi.cbSize = (uint)Marshal.SizeOf (csbi);
218+
219+
// if (!GetConsoleScreenBufferInfoEx (ScreenBuffer, ref csbi)) {
220+
// throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
221+
// }
222+
// var maxWinSize = GetLargestConsoleWindowSize (ScreenBuffer);
223+
// var newCols = Math.Min (cols, maxWinSize.X);
224+
// var newRows = Math.Min (rows, maxWinSize.Y);
225+
// csbi.dwSize = new Coord (newCols, Math.Max (newRows, (short)1));
226+
// csbi.srWindow = new SmallRect (0, 0, newCols, newRows);
227+
// csbi.dwMaximumWindowSize = new Coord (newCols, newRows);
228+
// if (!SetConsoleScreenBufferInfoEx (ScreenBuffer, ref csbi)) {
229+
// throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
230+
// }
231+
// var winRect = new SmallRect (0, 0, (short)(newCols - 1), (short)Math.Max (newRows - 1, 0));
232+
// if (!SetConsoleWindowInfo (OutputHandle, true, ref winRect)) {
233+
// //throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
234+
// return new Size (cols, rows);
235+
// }
236+
// SetConsoleOutputWindow (csbi);
237+
// return new Size (winRect.Right + 1, newRows - 1 < 0 ? 0 : winRect.Bottom + 1);
238+
//}
239+
240+
//void SetConsoleOutputWindow (CONSOLE_SCREEN_BUFFER_INFOEX csbi)
241+
//{
242+
// if (ScreenBuffer != IntPtr.Zero && !SetConsoleScreenBufferInfoEx (ScreenBuffer, ref csbi)) {
243+
// throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
244+
// }
245+
//}
246+
247+
//internal Size SetConsoleOutputWindow (out Point position)
248+
//{
249+
// if (ScreenBuffer == IntPtr.Zero) {
250+
// position = Point.Empty;
251+
// return Size.Empty;
252+
// }
253+
254+
// var csbi = new CONSOLE_SCREEN_BUFFER_INFOEX ();
255+
// csbi.cbSize = (uint)Marshal.SizeOf (csbi);
256+
// if (!GetConsoleScreenBufferInfoEx (ScreenBuffer, ref csbi)) {
257+
// throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
258+
// }
259+
// var sz = new Size (csbi.srWindow.Right - csbi.srWindow.Left + 1,
260+
// Math.Max (csbi.srWindow.Bottom - csbi.srWindow.Top + 1, 0));
261+
// position = new Point (csbi.srWindow.Left, csbi.srWindow.Top);
262+
// SetConsoleOutputWindow (csbi);
263+
// var winRect = new SmallRect (0, 0, (short)(sz.Width - 1), (short)Math.Max (sz.Height - 1, 0));
264+
// if (!SetConsoleScreenBufferInfoEx (OutputHandle, ref csbi)) {
265+
// throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
266+
// }
267+
// if (!SetConsoleWindowInfo (OutputHandle, true, ref winRect)) {
268+
// throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
269+
// }
270+
271+
// return sz;
272+
//}
274273

275274
//bool ContinueListeningForConsoleEvents = true;
276275

@@ -734,7 +733,7 @@ public WindowsDriver ()
734733
WinConsole = new WindowsConsole ();
735734
clipboard = new WindowsClipboard ();
736735

737-
isWindowsTerminal = Environment.GetEnvironmentVariable ("WT_SESSION") != null;
736+
isWindowsTerminal = Environment.GetEnvironmentVariable ("WT_SESSION") != null || Environment.GetEnvironmentVariable ("VSAPPIDNAME") != null;
738737
}
739738

740739
public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
@@ -748,28 +747,28 @@ public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandle
748747

749748
mLoop.ProcessInput = (e) => ProcessInput (e);
750749

751-
mLoop.WinChanged = (e) => {
752-
ChangeWin (e);
753-
};
754-
}
755-
756-
private void ChangeWin (Size e)
757-
{
758-
var w = e.Width;
759-
if (w == cols - 3 && e.Height < rows) {
760-
w += 3;
761-
}
762-
var newSize = WinConsole.SetConsoleWindow (
763-
(short)Math.Max (w, 16), (short)Math.Max (e.Height, 0));
764-
765-
left = 0;
766-
top = 0;
767-
cols = newSize.Width;
768-
rows = newSize.Height;
769-
ResizeScreen ();
770-
UpdateOffScreen ();
771-
TerminalResized.Invoke ();
772-
}
750+
//mLoop.WinChanged = (e) => {
751+
// ChangeWin (e);
752+
//};
753+
}
754+
755+
//private void ChangeWin (Size e)
756+
//{
757+
// var w = e.Width;
758+
// if (w == cols - 3 && e.Height < rows) {
759+
// w += 3;
760+
// }
761+
// var newSize = WinConsole.SetConsoleWindow (
762+
// (short)Math.Max (w, 16), (short)Math.Max (e.Height, 0));
763+
764+
// left = 0;
765+
// top = 0;
766+
// cols = newSize.Width;
767+
// rows = newSize.Height;
768+
// ResizeScreen ();
769+
// UpdateOffScreen ();
770+
// TerminalResized.Invoke ();
771+
//}
773772

774773
void ProcessInput (WindowsConsole.InputRecord inputEvent)
775774
{
@@ -893,6 +892,14 @@ void ProcessInput (WindowsConsole.InputRecord inputEvent)
893892
case WindowsConsole.EventType.Focus:
894893
keyModifiers = null;
895894
break;
895+
896+
case WindowsConsole.EventType.WindowBufferSize:
897+
cols = inputEvent.WindowBufferSizeEvent.size.X;
898+
rows = inputEvent.WindowBufferSizeEvent.size.Y;
899+
900+
ResizeScreen ();
901+
TerminalResized.Invoke ();
902+
break;
896903
}
897904
}
898905

0 commit comments

Comments
 (0)