From 002695cd2ea6631c0dc72b4c92f69fa0ccd4256c Mon Sep 17 00:00:00 2001 From: BDisp Date: Wed, 9 Aug 2023 01:05:31 +0100 Subject: [PATCH 01/10] Fixes #2793. Windows Terminal still show vertical scroll bar using WindowsDriver. --- Terminal.Gui/ConsoleDrivers/WindowsDriver.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs index 71bd85a97c..8e55264097 100644 --- a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs @@ -1436,6 +1436,8 @@ public override void Init (Action terminalResized) // wipe out the backscroll buffer when the application exits. Console.Out.Write ("\x1b[?1047h"); + Console.Out.Write ("\x1b[3J"); + var winSize = WinConsole.GetConsoleOutputWindow (out Point pos); cols = winSize.Width; rows = winSize.Height; @@ -1473,6 +1475,8 @@ public override void ResizeScreen () Bottom = (short)Rows, Right = (short)Cols }; + Console.Out.Write ("\x1b[3J"); + WinConsole.ForceRefreshCursorVisibility (); } From fe5a5513010d8b8e2ff7d4a0ffaa042a126fa21b Mon Sep 17 00:00:00 2001 From: BDisp Date: Wed, 9 Aug 2023 20:17:40 +0100 Subject: [PATCH 02/10] There is only need to use the Alternate Screen Buffer (1049) --- Terminal.Gui/ConsoleDrivers/WindowsDriver.cs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs index 8e55264097..14290257f6 100644 --- a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs @@ -1426,17 +1426,15 @@ public override void Init (Action terminalResized) try { // Needed for Windows Terminal - // ESC [ ? 1047 h Activate xterm alternative buffer (no backscroll) - // ESC [ ? 1047 l Restore xterm working buffer (with backscroll) + // ESC [ ? 1047 h Save cursor position and activate xterm alternative buffer (no backscroll) + // ESC [ ? 1047 l Restore cursor position and restore xterm working buffer (with backscroll) // ESC [ ? 1048 h Save cursor position // ESC [ ? 1048 l Restore cursor position - // ESC [ ? 1049 h Save cursor position and activate xterm alternative buffer (no backscroll) - // ESC [ ? 1049 l Restore cursor position and restore xterm working buffer (with backscroll) - // Per Issue #2264 using the alterantive screen buffer is required for Windows Terminal to not + // ESC [ ? 1049 h Activate xterm alternative buffer (no backscroll) + // ESC [ ? 1049 l Restore xterm working buffer (with backscroll) + // Per Issue #2264 using the alternative screen buffer is required for Windows Terminal to not // wipe out the backscroll buffer when the application exits. - Console.Out.Write ("\x1b[?1047h"); - - Console.Out.Write ("\x1b[3J"); + Console.Out.Write ("\x1b[?1049h"); var winSize = WinConsole.GetConsoleOutputWindow (out Point pos); cols = winSize.Width; @@ -1475,8 +1473,6 @@ public override void ResizeScreen () Bottom = (short)Rows, Right = (short)Cols }; - Console.Out.Write ("\x1b[3J"); - WinConsole.ForceRefreshCursorVisibility (); } @@ -1685,7 +1681,7 @@ public override void End () WinConsole = null; // Disable alternative screen buffer. - Console.Out.Write ("\x1b[?1047l"); + Console.Out.Write ("\x1b[?1049l"); } /// From de79709697f7f8f8b55c1ede2f0792e0702a1667 Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 10 Aug 2023 02:51:11 +0100 Subject: [PATCH 03/10] Add GetParentProcessName method. --- Terminal.Gui/ConsoleDrivers/WindowsDriver.cs | 45 +++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs index 14290257f6..ee410b7d25 100644 --- a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs @@ -1420,6 +1420,43 @@ private Key MapKeyModifiers (ConsoleKeyInfo keyInfo, Key key) return keyMod != Key.Null ? keyMod | key : key; } + private static string GetParentProcessName () + { + var myId = Process.GetCurrentProcess ().Id; + var query = string.Format ($"SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {myId}"); + var search = new ManagementObjectSearcher ("root\\CIMV2", query); + var queryObj = search.Get ().OfType ().FirstOrDefault (); + if (queryObj == null) { + return null; + } + var parentId = (uint)queryObj ["ParentProcessId"]; + var parent = Process.GetProcessById ((int)parentId); + var prevParent = parent; + + // Check if the parent is from other parent + while (queryObj != null) { + query = string.Format ($"SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {parentId}"); + search = new ManagementObjectSearcher ("root\\CIMV2", query); + queryObj = search.Get ().OfType ().FirstOrDefault (); + if (queryObj == null) { + return parent.ProcessName; + } + parentId = (uint)queryObj ["ParentProcessId"]; + try { + parent = Process.GetProcessById ((int)parentId); + if (string.Equals (parent.ProcessName, "explorer", StringComparison.InvariantCultureIgnoreCase)) { + return prevParent.ProcessName; + } + prevParent = parent; + } catch (ArgumentException) { + + return prevParent.ProcessName; + } + } + + return parent.ProcessName; + } + public override void Init (Action terminalResized) { TerminalResized = terminalResized; @@ -1434,7 +1471,9 @@ public override void Init (Action terminalResized) // ESC [ ? 1049 l Restore xterm working buffer (with backscroll) // Per Issue #2264 using the alternative screen buffer is required for Windows Terminal to not // wipe out the backscroll buffer when the application exits. - Console.Out.Write ("\x1b[?1049h"); + if (string.Equals (GetParentProcessName (), "WindowsTerminal", StringComparison.InvariantCultureIgnoreCase)) { + Console.Out.Write ("\x1b[?1049h"); + } var winSize = WinConsole.GetConsoleOutputWindow (out Point pos); cols = winSize.Width; @@ -1681,7 +1720,9 @@ public override void End () WinConsole = null; // Disable alternative screen buffer. - Console.Out.Write ("\x1b[?1049l"); + if (string.Equals (GetParentProcessName (), "WindowsTerminal", StringComparison.InvariantCultureIgnoreCase)) { + Console.Out.Write ("\x1b[?1049l"); + } } /// From dd8a658d6b665dc835a471596865913b6a41e8a4 Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 10 Aug 2023 02:52:58 +0100 Subject: [PATCH 04/10] Added debug profile for Windows Terminal. --- UICatalog/Properties/launchSettings.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/UICatalog/Properties/launchSettings.json b/UICatalog/Properties/launchSettings.json index 9a4d02e140..dec0cb3fea 100644 --- a/UICatalog/Properties/launchSettings.json +++ b/UICatalog/Properties/launchSettings.json @@ -52,7 +52,12 @@ "commandLineArgs": "\"Windows & FrameViews\"" }, "Docker": { - "commandName": "Docker" + "commandName": "Docker" + }, + "UICatalog WT": { + "commandName": "Executable", + "executablePath": "wt", + "commandLineArgs": "UICatalog.exe" } } } \ No newline at end of file From f6e5c7be85c96166adc7ae2e0da2b36865c221d1 Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 10 Aug 2023 02:55:21 +0100 Subject: [PATCH 05/10] Fix a bug where a row is less than zero on shrinking. --- Terminal.Gui/Core/Border.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Terminal.Gui/Core/Border.cs b/Terminal.Gui/Core/Border.cs index abf4438ce7..8dac63c46c 100644 --- a/Terminal.Gui/Core/Border.cs +++ b/Terminal.Gui/Core/Border.cs @@ -445,12 +445,10 @@ public View Child { private void Parent_Removed (View obj) { - if (borderBrush != null) - { + if (borderBrush != null) { BorderBrush = default; } - if (background != null) - { + if (background != null) { Background = default; } child.Removed -= Parent_Removed; @@ -800,7 +798,7 @@ private void DrawParentBorder (Rect frame, bool fill = true) SetBorderBrush (driver); // Draw the upper BorderThickness - for (int r = frame.Y; + for (int r = Math.Max (frame.Y, 0); r < Math.Min (frame.Y + borderThickness.Top, frame.Bottom); r++) { for (int c = frame.X; c < Math.Min (frame.Right, driver.Cols); c++) { @@ -810,7 +808,7 @@ private void DrawParentBorder (Rect frame, bool fill = true) } // Draw the left BorderThickness - for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom); + for (int r = Math.Max (Math.Min (frame.Y + borderThickness.Top, frame.Bottom), 0); r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) { for (int c = frame.X; c < Math.Min (frame.X + borderThickness.Left, frame.Right); c++) { @@ -820,7 +818,7 @@ private void DrawParentBorder (Rect frame, bool fill = true) } // Draw the right BorderThickness - for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom); + for (int r = Math.Max (Math.Min (frame.Y + borderThickness.Top, frame.Bottom), 0); r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) { for (int c = Math.Max (frame.Right - borderThickness.Right, frame.X); c < Math.Min (frame.Right, driver.Cols); c++) { @@ -842,7 +840,7 @@ private void DrawParentBorder (Rect frame, bool fill = true) SetBackground (driver); // Draw the upper Padding - for (int r = frame.Y + borderThickness.Top; + for (int r = Math.Max (frame.Y + borderThickness.Top, 0); r < Math.Min (frame.Y + sumThickness.Top, frame.Bottom - borderThickness.Bottom); r++) { for (int c = frame.X + borderThickness.Left; c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) { @@ -852,7 +850,7 @@ private void DrawParentBorder (Rect frame, bool fill = true) } // Draw the left Padding - for (int r = frame.Y + sumThickness.Top; + for (int r = Math.Max (frame.Y + sumThickness.Top, 0); r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) { for (int c = frame.X + borderThickness.Left; c < Math.Min (frame.X + sumThickness.Left, frame.Right - borderThickness.Right); c++) { @@ -862,7 +860,7 @@ private void DrawParentBorder (Rect frame, bool fill = true) } // Draw the right Padding - for (int r = frame.Y + sumThickness.Top; + for (int r = Math.Max (frame.Y + sumThickness.Top, 0); r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) { for (int c = Math.Max (frame.Right - sumThickness.Right, frame.X + sumThickness.Left); c < Math.Max (frame.Right - borderThickness.Right, frame.X + sumThickness.Left); c++) { From 3133d294fb71b36301a55d28530390c4610ea315 Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 10 Aug 2023 03:12:52 +0100 Subject: [PATCH 06/10] Fix warning. --- Terminal.Gui/ConsoleDrivers/WindowsDriver.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs index ee410b7d25..6262f73a38 100644 --- a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs @@ -1420,6 +1420,7 @@ private Key MapKeyModifiers (ConsoleKeyInfo keyInfo, Key key) return keyMod != Key.Null ? keyMod | key : key; } +#pragma warning disable CA1416 // Validate platform compatibility private static string GetParentProcessName () { var myId = Process.GetCurrentProcess ().Id; @@ -1456,6 +1457,7 @@ private static string GetParentProcessName () return parent.ProcessName; } +#pragma warning restore CA1416 // Validate platform compatibility public override void Init (Action terminalResized) { From 6376f465797b5da590374ba3e7f32ca57e69597e Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 10 Aug 2023 03:25:37 +0100 Subject: [PATCH 07/10] Trying fix the compiler error. --- Terminal.Gui/ConsoleDrivers/WindowsDriver.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs index 6262f73a38..f6ec17378d 100644 --- a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs @@ -5,7 +5,9 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics; using System.Linq; +using System.Management; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -1420,9 +1422,9 @@ private Key MapKeyModifiers (ConsoleKeyInfo keyInfo, Key key) return keyMod != Key.Null ? keyMod | key : key; } -#pragma warning disable CA1416 // Validate platform compatibility private static string GetParentProcessName () { +#pragma warning disable CA1416 // Validate platform compatibility var myId = Process.GetCurrentProcess ().Id; var query = string.Format ($"SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {myId}"); var search = new ManagementObjectSearcher ("root\\CIMV2", query); @@ -1456,8 +1458,8 @@ private static string GetParentProcessName () } return parent.ProcessName; - } #pragma warning restore CA1416 // Validate platform compatibility + } public override void Init (Action terminalResized) { From da3ab3a4ad37d92c1fe754a963f8d9f901373bc1 Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 14 Aug 2023 20:02:10 +0100 Subject: [PATCH 08/10] Revert "Fix a bug where a row is less than zero on shrinking." This reverts commit f6e5c7be85c96166adc7ae2e0da2b36865c221d1. --- Terminal.Gui/Core/Border.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Terminal.Gui/Core/Border.cs b/Terminal.Gui/Core/Border.cs index 8dac63c46c..abf4438ce7 100644 --- a/Terminal.Gui/Core/Border.cs +++ b/Terminal.Gui/Core/Border.cs @@ -445,10 +445,12 @@ public View Child { private void Parent_Removed (View obj) { - if (borderBrush != null) { + if (borderBrush != null) + { BorderBrush = default; } - if (background != null) { + if (background != null) + { Background = default; } child.Removed -= Parent_Removed; @@ -798,7 +800,7 @@ private void DrawParentBorder (Rect frame, bool fill = true) SetBorderBrush (driver); // Draw the upper BorderThickness - for (int r = Math.Max (frame.Y, 0); + for (int r = frame.Y; r < Math.Min (frame.Y + borderThickness.Top, frame.Bottom); r++) { for (int c = frame.X; c < Math.Min (frame.Right, driver.Cols); c++) { @@ -808,7 +810,7 @@ private void DrawParentBorder (Rect frame, bool fill = true) } // Draw the left BorderThickness - for (int r = Math.Max (Math.Min (frame.Y + borderThickness.Top, frame.Bottom), 0); + for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom); r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) { for (int c = frame.X; c < Math.Min (frame.X + borderThickness.Left, frame.Right); c++) { @@ -818,7 +820,7 @@ private void DrawParentBorder (Rect frame, bool fill = true) } // Draw the right BorderThickness - for (int r = Math.Max (Math.Min (frame.Y + borderThickness.Top, frame.Bottom), 0); + for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom); r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) { for (int c = Math.Max (frame.Right - borderThickness.Right, frame.X); c < Math.Min (frame.Right, driver.Cols); c++) { @@ -840,7 +842,7 @@ private void DrawParentBorder (Rect frame, bool fill = true) SetBackground (driver); // Draw the upper Padding - for (int r = Math.Max (frame.Y + borderThickness.Top, 0); + for (int r = frame.Y + borderThickness.Top; r < Math.Min (frame.Y + sumThickness.Top, frame.Bottom - borderThickness.Bottom); r++) { for (int c = frame.X + borderThickness.Left; c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) { @@ -850,7 +852,7 @@ private void DrawParentBorder (Rect frame, bool fill = true) } // Draw the left Padding - for (int r = Math.Max (frame.Y + sumThickness.Top, 0); + for (int r = frame.Y + sumThickness.Top; r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) { for (int c = frame.X + borderThickness.Left; c < Math.Min (frame.X + sumThickness.Left, frame.Right - borderThickness.Right); c++) { @@ -860,7 +862,7 @@ private void DrawParentBorder (Rect frame, bool fill = true) } // Draw the right Padding - for (int r = Math.Max (frame.Y + sumThickness.Top, 0); + for (int r = frame.Y + sumThickness.Top; r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) { for (int c = Math.Max (frame.Right - sumThickness.Right, frame.X + sumThickness.Left); c < Math.Max (frame.Right - borderThickness.Right, frame.X + sumThickness.Left); c++) { From b607280e09cd3d659df6207e368580ab9c557988 Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 14 Aug 2023 20:19:52 +0100 Subject: [PATCH 09/10] Implementing @tig suggestion change. --- Terminal.Gui/ConsoleDrivers/WindowsDriver.cs | 46 +++----------------- 1 file changed, 5 insertions(+), 41 deletions(-) diff --git a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs index f6ec17378d..1e37490bf1 100644 --- a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs @@ -711,6 +711,7 @@ internal class WindowsDriver : ConsoleDriver { WindowsConsole.SmallRect damageRegion; IClipboard clipboard; int [,,] contents; + readonly bool isWindowsTerminal; public override int Cols => cols; public override int Rows => rows; @@ -734,6 +735,8 @@ public WindowsDriver () { WinConsole = new WindowsConsole (); clipboard = new WindowsClipboard (); + + isWindowsTerminal = Environment.GetEnvironmentVariable ("WT_SESSION") != null; } public override void PrepareToRun (MainLoop mainLoop, Action keyHandler, Action keyDownHandler, Action keyUpHandler, Action mouseHandler) @@ -1422,45 +1425,6 @@ private Key MapKeyModifiers (ConsoleKeyInfo keyInfo, Key key) return keyMod != Key.Null ? keyMod | key : key; } - private static string GetParentProcessName () - { -#pragma warning disable CA1416 // Validate platform compatibility - var myId = Process.GetCurrentProcess ().Id; - var query = string.Format ($"SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {myId}"); - var search = new ManagementObjectSearcher ("root\\CIMV2", query); - var queryObj = search.Get ().OfType ().FirstOrDefault (); - if (queryObj == null) { - return null; - } - var parentId = (uint)queryObj ["ParentProcessId"]; - var parent = Process.GetProcessById ((int)parentId); - var prevParent = parent; - - // Check if the parent is from other parent - while (queryObj != null) { - query = string.Format ($"SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {parentId}"); - search = new ManagementObjectSearcher ("root\\CIMV2", query); - queryObj = search.Get ().OfType ().FirstOrDefault (); - if (queryObj == null) { - return parent.ProcessName; - } - parentId = (uint)queryObj ["ParentProcessId"]; - try { - parent = Process.GetProcessById ((int)parentId); - if (string.Equals (parent.ProcessName, "explorer", StringComparison.InvariantCultureIgnoreCase)) { - return prevParent.ProcessName; - } - prevParent = parent; - } catch (ArgumentException) { - - return prevParent.ProcessName; - } - } - - return parent.ProcessName; -#pragma warning restore CA1416 // Validate platform compatibility - } - public override void Init (Action terminalResized) { TerminalResized = terminalResized; @@ -1475,7 +1439,7 @@ public override void Init (Action terminalResized) // ESC [ ? 1049 l Restore xterm working buffer (with backscroll) // Per Issue #2264 using the alternative screen buffer is required for Windows Terminal to not // wipe out the backscroll buffer when the application exits. - if (string.Equals (GetParentProcessName (), "WindowsTerminal", StringComparison.InvariantCultureIgnoreCase)) { + if (isWindowsTerminal) { Console.Out.Write ("\x1b[?1049h"); } @@ -1724,7 +1688,7 @@ public override void End () WinConsole = null; // Disable alternative screen buffer. - if (string.Equals (GetParentProcessName (), "WindowsTerminal", StringComparison.InvariantCultureIgnoreCase)) { + if (isWindowsTerminal) { Console.Out.Write ("\x1b[?1049l"); } } From d0e29044458ca677dc37ae08b49a95198cdeddee Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 14 Aug 2023 21:00:51 +0100 Subject: [PATCH 10/10] Cleaning code. --- Terminal.Gui/ConsoleDrivers/WindowsDriver.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs index 1e37490bf1..f575de14b5 100644 --- a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs @@ -5,9 +5,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; -using System.Diagnostics; using System.Linq; -using System.Management; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -1683,7 +1681,6 @@ public override void UpdateCursor () public override void End () { - WinConsole.Cleanup (); WinConsole = null;